comparison src/gpf2/tst/drv/ser.c @ 294:cd37d228dae0

src/gpf2/{misc,tst}: import from TCS211 semi-src with CRLF line endings and directory name capitalization cleaned up
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 21 Oct 2017 01:12:15 +0000
parents
children
comparison
equal deleted inserted replaced
293:5b2ebc94cae4 294:cd37d228dae0
1 /*
2 +------------------------------------------------------------------------------
3 | File: ser.c
4 +------------------------------------------------------------------------------
5 | Copyright 2004 Texas Instruments Deutschland, AG
6 | All rights reserved.
7 |
8 | This file is confidential and a trade secret of Texas
9 | Instruments Berlin, AG
10 | The receipt of or possession of this file does not convey
11 | any rights to reproduce or disclose its contents or to
12 | manufacture, use, or sell anything it may describe, in
13 | whole, or in part, without the specific written consent of
14 | Texas Instruments Deutschland, AG.
15 +-----------------------------------------------------------------------------
16 | Purpose : This Modul contains the serial driver adaptation
17 +-----------------------------------------------------------------------------
18 */
19
20 #ifndef __SER_C__
21 #define __SER_C__
22 #endif
23
24 #include <windows.h>
25 #include <stdio.h>
26 #include "usart.h"
27 #include "stdlib.h"
28 #include "string.h"
29 #include "tools.h"
30 #include "typedefs.h"
31 #include "tstheader.h"
32 #include "os.h"
33 #include "gdi.h"
34
35 /*==== TYPES ======================================================*/
36
37 typedef struct
38 {
39 USHORT Handle;
40 USHORT EnabledSignalType;
41 T_DRV_CB_FUNC Callback;
42 #ifndef _TARGET_
43 OS_HANDLE TaskHandle;
44 #endif
45 char Connected;
46 } T_SER_DATA;
47
48 /*==== CONSTANTS ==================================================*/
49
50 #define ALLOWED_SER_SIGNALS (DRV_SIGTYPE_READ|DRV_SIGTYPE_CONNECT)
51 #define MAX_CONFIGSTR_LEN 50
52
53 /*==== EXTERNALS ==================================================*/
54
55 #ifndef _TARGET_
56 extern OS_HANDLE ext_data_pool_handle;
57 #endif
58
59 /*==== VARIABLES ==================================================*/
60
61 T_SER_DATA SER_Data;
62 static T_DRV_SIGNAL Signal;
63 GLOBAL int extPort = 1;
64
65 #ifndef _TARGET_
66 static char m_last_config[MAX_CONFIGSTR_LEN+1];
67 #endif
68
69 #ifdef _TOOLS_
70 OS_TIME extr_creation_time = 0;
71 #endif
72
73 /*==== FUNCTIONS ==================================================*/
74
75 #ifndef RUN_INT_RAM
76 #ifdef _TOOLS_
77 OS_TIME tst_get_init_time ( void )
78 {
79 return extr_creation_time;
80 }
81 #endif
82 #endif
83 /*
84 +--------------------------------------------------------------------+
85 | PROJECT : GSM-Frame (8415) MODULE : SER |
86 | STATE : code ROUTINE : Callback |
87 +--------------------------------------------------------------------+
88
89 PURPOSE : callback function of the driver
90
91 */
92 void Callback ( void )
93 {
94 if ( SER_Data.EnabledSignalType & DRV_SIGTYPE_READ )
95 {
96 Signal.SignalType = DRV_SIGTYPE_READ;
97 Signal.DrvHandle = SER_Data.Handle;
98
99 (SER_Data.Callback)( &Signal );
100 }
101 }
102
103 #ifndef _TARGET_
104
105 GLOBAL void SER_Receiver (void)
106 {
107
108 for(;;)
109 {
110 UT_IsChar ();
111 Callback();
112 }
113 }
114
115 #endif
116
117 /*
118 +--------------------------------------------------------------------+
119 | PROJECT : GSM-Frame (8415) MODULE : SER |
120 | STATE : code ROUTINE : SER_Exit |
121 +--------------------------------------------------------------------+
122
123 PURPOSE : exit a driver
124
125 */
126 void SER_Exit ( void )
127 {
128 os_DestroyTask( 0, SER_Data.TaskHandle );
129 SER_Data.TaskHandle = OS_ERROR;
130 UT_Close();
131 }
132
133 /*
134 +--------------------------------------------------------------------+
135 | PROJECT : GSM-Frame (8415) MODULE : SER |
136 | STATE : code ROUTINE : SER_Read |
137 +--------------------------------------------------------------------+
138
139 PURPOSE : read data from driver
140
141 */
142 USHORT SER_Read ( void *Buffer, ULONG *BytesToRead )
143 {
144 *BytesToRead = (USHORT)UT_ReadNChars (UT_DEVICE_0, (BYTE *)Buffer, *BytesToRead);
145 return DRV_OK;
146 }
147
148
149 /*
150 +--------------------------------------------------------------------+
151 | PROJECT : GSM-Frame (8415) MODULE : SER |
152 | STATE : code ROUTINE : SER_Write |
153 +--------------------------------------------------------------------+
154
155 PURPOSE : write data to driver
156
157 */
158 USHORT SER_Write ( void *Buffer, ULONG *BytesToWrite )
159 {
160 ULONG ToWrite = *BytesToWrite & ~PRIM_FLAG_MASK;
161
162 UT_WriteNChars(UT_DEVICE_0, (void*)Buffer, ToWrite );
163 return ( DRV_OK );
164 }
165 /*
166 +--------------------------------------------------------------------+
167 | PROJECT : GSM-Frame (8415) MODULE : SER |
168 | STATE : code ROUTINE : SER_SetSignal |
169 +--------------------------------------------------------------------+
170
171 PURPOSE : enable signal for the driver
172
173 */
174 USHORT SER_SetSignal ( USHORT SignalType )
175 {
176 if ( !(SignalType & ALLOWED_SER_SIGNALS) )
177 return DRV_INVALID_PARAMS;
178 else
179 SER_Data.EnabledSignalType |= SignalType;
180
181 return DRV_OK;
182 }
183
184 /*
185 +--------------------------------------------------------------------+
186 | PROJECT : GSM-Frame (8415) MODULE : SER |
187 | STATE : code ROUTINE : SER_ResetSignal |
188 +--------------------------------------------------------------------+
189
190 PURPOSE : disable signal for the driver
191
192 */
193 USHORT SER_ResetSignal ( USHORT SignalType )
194 {
195 if ( !(SignalType & ALLOWED_SER_SIGNALS) )
196 return DRV_INVALID_PARAMS;
197 else
198 SER_Data.EnabledSignalType &= ~SignalType;
199
200 return DRV_OK;
201 }
202
203 /*
204 +--------------------------------------------------------------------+
205 | PROJECT : GSM-Frame (8415) MODULE : SER |
206 | STATE : code ROUTINE : SER_SetConfig |
207 +--------------------------------------------------------------------+
208
209 PURPOSE : set configuration for the driver
210
211 */
212 USHORT SER_SetConfig ( char *Buffer )
213 {
214 /*lint -e813, suppress Info 813: auto variable 'osver' has size '148' */
215 char token [MAX_PATH+1];
216 /*lint +e813 */
217 unsigned int baudrate=0;
218 int mode;
219 char flow_ctrl='N';
220 unsigned int len;
221 const char *fname=NULL;
222
223 if ( !SER_Data.Connected )
224 {
225 Signal.SignalType = DRV_SIGTYPE_CONNECT;
226 Signal.DrvHandle = SER_Data.Handle;
227 Signal.UserData = NULL;
228 (SER_Data.Callback)( &Signal );
229 SER_Data.Connected = TRUE;
230 extPort=0;
231 return DRV_OK;
232 }
233
234 if (strlen(m_last_config))
235 {
236 /* check if we are already configured right */
237 if (strcmp(m_last_config,Buffer)==0)
238 {
239 #ifdef _DEBUG
240 fprintf(stdout,"SER: keeping configuration\n");
241 #endif
242 return DRV_OK;
243 }
244 }
245
246 if (!(strcmp(Buffer,"EXIT")))
247 {
248 SER_Exit();
249 return DRV_OK;
250 }
251 /* save configuration string */
252 strncpy(m_last_config,Buffer,MAX_CONFIGSTR_LEN);
253
254 /* interprete configuration string */
255 if ( (len = GetNextToken (Buffer, token, " #")) == 0 )
256 return DRV_INVALID_PARAMS;
257 else
258 Buffer += (len+1);
259
260 /* set mode */
261 if (!strcmp(token,"SIM"))
262 {
263 mode=UT_MODE_SIM;
264 printf("TST: simulated USART mode selected\n");
265 }
266 else if (!strcmp(token,"FILE"))
267 {
268 if ( (len = GetNextToken (Buffer, token, "\"")) == 0 )
269 {
270 return DRV_INVALID_PARAMS;
271 }
272 mode=UT_MODE_FILE;
273 fname=token;
274 printf("TST: file mode selected\n");
275 strcpy(m_last_config,""); /* don't store file-mode */
276 }
277 else
278 {
279 if (!strcmp(token,"NT")) {
280 mode=UT_MODE_NT;
281 } else if (!strcmp(token,"95")) {
282 mode=UT_MODE_95;
283 } else if (!strcmp(token,"REAL")) {
284 /*lint -e813, suppress Info 813: auto variable 'osver' has size '148' */
285 OSVERSIONINFO osver;
286 /*lint +e813 */
287 osver.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
288 GetVersionEx( &osver);
289 if (osver.dwPlatformId==VER_PLATFORM_WIN32_NT) {
290 mode=UT_MODE_NT;
291 } else {
292 mode=UT_MODE_95;
293 }
294 } else {
295 return DRV_INVALID_PARAMS;
296 }
297
298 if (mode==UT_MODE_NT) {
299 printf("TST: real USART mode for winNT selected\n");
300 } else {
301 printf("TST: real USART mode for win95 selected\n");
302 }
303
304 /* com port setting */
305 if ( (len = GetNextToken (Buffer, token, " #")) == 0 ) {
306 return DRV_INVALID_PARAMS;
307 } else {
308 Buffer += (len+1);
309 }
310
311 extPort=atoi(&token[3]);
312
313 /*
314 * Get baudrate from configuration string
315 */
316 if ( (len = GetNextToken (Buffer, token, " #")) == 0 ) {
317 return DRV_INVALID_PARAMS;
318 } else {
319 Buffer += (len+1);
320 }
321
322 printf("SER: requested baudrate: %s\n",token);
323 switch ( atoi(token) )
324 {
325 case 38400:
326 baudrate = UT_BAUD_38400;
327 break;
328 case 19200:
329 baudrate = UT_BAUD_19200;
330 break;
331 case 9600:
332 baudrate = UT_BAUD_9600;
333 break;
334 case 57600:
335 baudrate = UT_BAUD_57600;
336 break;
337 case 115200:
338 baudrate = UT_BAUD_115200;
339 break;
340 case 128000:
341 baudrate = UT_BAUD_128000;
342 break;
343 case 256000:
344 baudrate = UT_BAUD_256000;
345 break;
346 default:
347 printf("SER: no CBR_xxx constant found for requested baudrate\n");
348 baudrate = atoi(token);
349 break;
350 }
351
352 /*
353 * Get flow control from configuration string
354 */
355 if ( (len = GetNextToken (Buffer, token, " #")) == 0 ) {
356 return DRV_INVALID_PARAMS;
357 } else {
358 Buffer += (len+1);
359 }
360
361 flow_ctrl=token[0];
362 printf("SER: requested flow control: %c\n",flow_ctrl);
363 }
364
365 /* exit receiver task */
366 if ( SER_Data.TaskHandle != OS_ERROR )
367 {
368 os_DestroyTask( 0, SER_Data.TaskHandle );
369 }
370 #ifdef _TOOLS_
371 os_GetTime ( 0, &extr_creation_time );
372 #endif
373 UT_Close();
374 UT_set_mode(mode);
375 if (UT_Init (baudrate, 100, flow_ctrl, NULL, fname)!=0)
376 {
377 strcpy(m_last_config,"");
378 return DRV_INITFAILURE;
379 };
380 /* restart receiver task */
381 if ( os_CreateTask ( 0, (char*)"EXTR", (void (*)(OS_HANDLE, ULONG))(SER_Receiver), 2048, 1,
382 &SER_Data.TaskHandle, ext_data_pool_handle) != OS_OK ||
383 os_StartTask ( 0, SER_Data.TaskHandle, 0 ) != OS_OK)
384 {
385 return DRV_INITFAILURE;
386 }
387
388 return DRV_OK;
389 }
390
391
392 /*
393 +--------------------------------------------------------------------+
394 | PROJECT : GSM-Frame (8415) MODULE : SER |
395 | STATE : code ROUTINE : SER_Init |
396 +--------------------------------------------------------------------+
397
398 PURPOSE : initialize driver
399
400 */
401 USHORT SER_Init ( USHORT DrvHandle, T_DRV_CB_FUNC CallbackFunc, T_DRV_EXPORT const **DrvInfo )
402 {
403 static const T_DRV_EXPORT SER_Info =
404 {
405 "SER",
406 CALLED_FROM_ISR,
407 {
408 #ifdef _TOOLS_
409 SER_Init,
410 #endif
411 SER_Exit,
412 SER_Read,
413 SER_Write,
414 NULL,
415 NULL,
416 NULL,
417 SER_SetSignal,
418 SER_ResetSignal,
419 SER_SetConfig,
420 NULL,
421 NULL,
422 }
423 };
424
425 SER_Data.Handle = DrvHandle;
426
427 SER_Data.EnabledSignalType = 0;
428
429 SER_Data.Callback = CallbackFunc;
430
431 SER_Data.Connected = FALSE;
432
433 SER_Data.TaskHandle = OS_ERROR;
434
435 #ifndef _TOOLS_
436 UT_set_mode(UT_MODE_SIM);
437 UT_Init (2, 100, 'N', NULL, NULL);
438 UT_SetFlowCtrl ('N');
439 if ( os_CreateTask ( 0, (char*)"EXTR", (void (*)(OS_HANDLE, ULONG))(SER_Receiver), 2048, 1,
440 &SER_Data.TaskHandle, ext_data_pool_handle) == OS_OK )
441 {
442 if ( os_StartTask ( 0, SER_Data.TaskHandle, 0 ) != OS_OK)
443 printf ( "SYSTEM ERROR: Cannot start task EXTR" );
444 }
445 else
446 printf ( "SYSTEM ERROR: Cannot create task EXTR" );
447 #endif
448
449 m_last_config[0]='\0';
450
451 *DrvInfo = &SER_Info;
452
453 return DRV_OK;
454 }
455