]> git.sur5r.net Git - freertos/blob - Demo/Flshlite/serial/serial.c
Change to the file headers only.
[freertos] / Demo / Flshlite / serial / serial.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55 Changes from V1.00:\r
56         \r
57         + Call to the more efficient portSWITCH_CONTEXT() replaces the call to \r
58           taskYIELD() in the ISR.\r
59 \r
60 Changes from V1.01:\r
61 \r
62         + The semaphore task is not operational.  This does nothing but check\r
63           the semaphore from ISR functionality.\r
64         + ISR modified slightly so only Rx or Tx is serviced per ISR - not both.\r
65 \r
66 Changes from V1.2.0:\r
67 \r
68         + Change so Tx uses a DMA channel, and Rx uses an interrupt.\r
69 \r
70 Changes from V1.2.3\r
71 \r
72         + The function xPortInitMinimal() has been renamed to \r
73           xSerialPortInitMinimal() and the function xPortInit() has been renamed\r
74           to xSerialPortInit().\r
75 \r
76 Changes from V1.2.5\r
77 \r
78         + Reverted back to the non-DMA serial port driver, with a slightly modified\r
79           ISR.  This is a better test of the scheduler mechanisms.\r
80         + A critical section is now used in vInterruptOn().\r
81         + Flag sTxInterruptOn has been added to the port structure.  This allows\r
82           checking of the interrupt enable status without performing any IO.\r
83 \r
84 Changes from V2.0.0\r
85 \r
86         + Use portTickType in place of unsigned pdLONG for delay periods.\r
87         + Slightly more efficient vSerialSendString() implementation.\r
88         + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.\r
89 */\r
90 \r
91 #include <stdlib.h>\r
92 #include <dos.h>\r
93 #include "FreeRTOS.h"\r
94 #include "queue.h"\r
95 #include "task.h"\r
96 #include "portasm.h"\r
97 #include "semphr.h"\r
98 \r
99 #define serMAX_PORTS                    ( ( unsigned short ) 2 )\r
100 \r
101 #define serPORT_0_INT_REG               ( 0xff44 )\r
102 #define serPORT_0_BAUD_REG              ( 0xff88 )\r
103 #define serPORT_0_RX_REG                ( 0xff86 )\r
104 #define serPORT_0_TX_REG                ( 0xff84 )\r
105 #define serPORT_0_STATUS_REG    ( 0xff82 )\r
106 #define serPORT_0_CTRL_REG              ( 0xff80 )\r
107 #define serPORT_0_IRQ                   ( 0x14 )\r
108 \r
109 #define serPORT_1_INT_REG               ( 0xff42 )\r
110 #define serPORT_1_BAUD_REG              ( 0xff18 )\r
111 #define serPORT_1_RX_REG                ( 0xff16 )\r
112 #define serPORT_1_TX_REG                ( 0xff14 )\r
113 #define serPORT_1_STATUS_REG    ( 0xff12 )\r
114 #define serPORT_1_CTRL_REG              ( 0xff10 )\r
115 #define serPORT_1_IRQ                   ( 0x11 )\r
116 \r
117 #define serTX_EMPTY                             ( ( unsigned short ) 0x40 )\r
118 #define serRX_READY                             ( ( unsigned short ) 0x80 )\r
119 \r
120 #define serRESET_PIC( usEOI_TYPE )      portOUTPUT_WORD( ( unsigned short ) 0xff22, usEOI_TYPE )\r
121 #define serTX_HOLD_EMPTY_INT            ( ( unsigned short ) 0x100 )\r
122 \r
123 #define serENABLE_INTERRUPTS            ( ( unsigned short ) 0x80 )\r
124 #define serMODE                                         ( ( unsigned short ) 0x01 )\r
125 #define serENABLE_TX_MACHINES           ( ( unsigned short ) 0x40 )\r
126 #define serENABLE_RX_MACHINES           ( ( unsigned short ) 0x20 )\r
127 #define serINTERRUPT_MASK                       ( ( unsigned short ) 0x08 )\r
128 #define serCLEAR_ALL_STATUS_BITS        ( ( unsigned short ) 0x00 )\r
129 #define serINTERRUPT_PRIORITY           ( ( unsigned short ) 0x01 ) /*< Just below the scheduler priority. */\r
130 \r
131 #define serDONT_BLOCK                           ( ( portTickType ) 0 )\r
132 \r
133 typedef enum\r
134\r
135         serCOM1 = 0, \r
136         serCOM2, \r
137         serCOM3, \r
138         serCOM4, \r
139         serCOM5, \r
140         serCOM6, \r
141         serCOM7, \r
142         serCOM8 \r
143 } eCOMPort;\r
144 \r
145 typedef enum \r
146\r
147         serNO_PARITY, \r
148         serODD_PARITY, \r
149         serEVEN_PARITY, \r
150         serMARK_PARITY, \r
151         serSPACE_PARITY \r
152 } eParity;\r
153 \r
154 typedef enum \r
155\r
156         serSTOP_1, \r
157         serSTOP_2 \r
158 } eStopBits;\r
159 \r
160 typedef enum \r
161\r
162         serBITS_5, \r
163         serBITS_6, \r
164         serBITS_7, \r
165         serBITS_8 \r
166 } eDataBits;\r
167 \r
168 typedef enum \r
169\r
170         ser50 = 0,\r
171         ser75,          \r
172         ser110,         \r
173         ser134,         \r
174         ser150,    \r
175         ser200,\r
176         ser300,         \r
177         ser600,         \r
178         ser1200,        \r
179         ser1800,        \r
180         ser2400,   \r
181         ser4800,\r
182         ser9600,                \r
183         ser19200,       \r
184         ser38400,       \r
185         ser57600,       \r
186         ser115200\r
187 } eBaud;\r
188 \r
189 /* Must be same order as eBaud definitions. */\r
190 static const unsigned short usBaudRateDivisor[] = \r
191 {\r
192         0, /* Not sure if the first 6 are correct.  First cannot be used. */\r
193         29127,\r
194         19859,\r
195         16302,\r
196         14564,\r
197         10923,  \r
198         6879,\r
199         3437,\r
200         1718,\r
201         1145,\r
202         859,\r
203         429,\r
204         214,\r
205         107,\r
206         54,\r
207         35,\r
208         18\r
209 };\r
210 \r
211 \r
212 typedef struct xCOM_PORT\r
213 {\r
214         /* Hardware parameters for this port. */\r
215         short sTxInterruptOn;\r
216         unsigned short usIntReg;\r
217         unsigned short usBaudReg;\r
218         unsigned short usRxReg;\r
219         unsigned short usTxReg;\r
220         unsigned short usStatusReg;\r
221         unsigned short usCtrlReg;\r
222 \r
223         unsigned short usIRQVector;\r
224 \r
225         /* Queues used for communications with com test task. */\r
226         xQueueHandle xRxedChars; \r
227         xQueueHandle xCharsForTx;\r
228 \r
229         /* This semaphore does nothing useful except test a feature of the\r
230         scheduler. */\r
231         xSemaphoreHandle xTestSem;\r
232 \r
233 } xComPort;\r
234 \r
235 static xComPort xPorts[ serMAX_PORTS ] = \r
236 {\r
237         { pdFALSE, serPORT_0_INT_REG, serPORT_0_BAUD_REG, serPORT_0_RX_REG, serPORT_0_TX_REG, serPORT_0_STATUS_REG, serPORT_0_CTRL_REG, serPORT_0_IRQ, NULL, NULL, NULL },\r
238         { pdFALSE, serPORT_1_INT_REG, serPORT_1_BAUD_REG, serPORT_1_RX_REG, serPORT_1_TX_REG, serPORT_1_STATUS_REG, serPORT_1_CTRL_REG, serPORT_1_IRQ, NULL, NULL, NULL }\r
239 };\r
240 \r
241 typedef xComPort * xComPortHandle;\r
242 \r
243 /* These prototypes are repeated here so we don't have to include the serial header.  This allows\r
244 the xComPortHandle structure details to be private to this file. */\r
245 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );\r
246 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType xBlockTime );\r
247 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime );\r
248 void vSerialClose( xComPortHandle xPort );\r
249 short sSerialWaitForSemaphore( xComPortHandle xPort );\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 static short xComPortISR( xComPort * const pxPort );\r
253 \r
254 #define vInterruptOn( pxPort, usInterrupt )                                                                             \\r
255 {                                                                                                                                                               \\r
256 unsigned short usIn;                                                                                                            \\r
257                                                                                                                                                                 \\r
258         portENTER_CRITICAL();                                                                                                           \\r
259         {                                                                                                                                                       \\r
260                 if( pxPort->sTxInterruptOn == pdFALSE )                                                                 \\r
261                 {                                                                                                                                               \\r
262                         usIn = portINPUT_WORD( pxPort->usCtrlReg );                                                     \\r
263                         portOUTPUT_WORD( pxPort->usCtrlReg, usIn | usInterrupt );                       \\r
264                                                                                                                                                                 \\r
265                         pxPort->sTxInterruptOn = pdTRUE;                                                                        \\r
266                 }                                                                                                                                               \\r
267         }                                                                                                                                                       \\r
268         portEXIT_CRITICAL();                                                                                                                    \\r
269 }                                                                                                                                                               \r
270 /*-----------------------------------------------------------*/\r
271 \r
272 #define vInterruptOff( pxPort, usInterrupt )                                                                    \\r
273 {                                                                                                                                                               \\r
274         unsigned short usIn = portINPUT_WORD( pxPort->usCtrlReg );                              \\r
275         if( usIn & usInterrupt )                                                                                                        \\r
276         {                                                                                                                                                       \\r
277                 portOUTPUT_WORD( pxPort->usCtrlReg, usIn & ~usInterrupt);                               \\r
278                 pxPort->sTxInterruptOn = pdFALSE;                                                                               \\r
279         }                                                                                                                                                       \\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 \r
284 /* Define an interrupt handler for each port */\r
285 #define COM_IRQ_WRAPPER(N)                                                                              \\r
286         static void __interrupt COM_IRQ##N##_WRAPPER( void )            \\r
287         {                                                                                                                       \\r
288         if( xComPortISR( &( xPorts[##N##] ) ) )                 \\r
289         {                                                       \\r
290                         portSWITCH_CONTEXT();                             \\r
291                 }                                                       \\r
292         }\r
293 \r
294   \r
295 \r
296 COM_IRQ_WRAPPER( 0 )\r
297 COM_IRQ_WRAPPER( 1 )\r
298 \r
299 static pxISR xISRs[ serMAX_PORTS ] = \r
300 {\r
301         COM_IRQ0_WRAPPER, \r
302         COM_IRQ1_WRAPPER\r
303 };\r
304 \r
305 /*-----------------------------------------------------------*/\r
306 \r
307 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )\r
308 {\r
309 unsigned short usPort;\r
310 xComPortHandle pxPort = NULL;\r
311 \r
312 /* BAUDDIV = ( Microprocessor Clock / Baud Rate ) / 16 */\r
313 \r
314         /* Only n, 8, 1 is supported so these parameters are not required for this\r
315         port. */\r
316         ( void ) eWantedParity;\r
317         ( void ) eWantedDataBits;\r
318     ( void ) eWantedStopBits;\r
319 \r
320         /* Currently only n,8,1 is supported. */\r
321 \r
322         usPort = ( unsigned short ) ePort;\r
323         \r
324         if( usPort < serMAX_PORTS )\r
325         {\r
326                 pxPort = &( xPorts[ usPort ] );\r
327 \r
328                 portENTER_CRITICAL();\r
329                 {\r
330                         unsigned short usInWord;\r
331 \r
332                         /* Create the queues used by the com test task. */\r
333                         pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
334                         pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
335 \r
336                         /* Create the test semaphore.  This does nothing useful except test a feature of the scheduler. */\r
337                         vSemaphoreCreateBinary( pxPort->xTestSem );\r
338 \r
339                         /* There is no ISR here already to restore later. */\r
340                         _dos_setvect( ( short ) pxPort->usIRQVector, xISRs[ usPort ] );\r
341 \r
342                         usInWord = portINPUT_WORD( pxPort->usIntReg );\r
343                         usInWord &= ~serINTERRUPT_MASK;\r
344                         usInWord |= serINTERRUPT_PRIORITY;\r
345                         portOUTPUT_WORD( pxPort->usIntReg, usInWord );\r
346 \r
347                         portOUTPUT_WORD( pxPort->usBaudReg, usBaudRateDivisor[ eWantedBaud ] );\r
348                         portOUTPUT_WORD( pxPort->usCtrlReg, serENABLE_INTERRUPTS | serMODE | serENABLE_TX_MACHINES | serENABLE_RX_MACHINES );\r
349 \r
350                         portOUTPUT_WORD( pxPort->usStatusReg, serCLEAR_ALL_STATUS_BITS );\r
351                 }\r
352                 portEXIT_CRITICAL();\r
353         }\r
354 \r
355         return pxPort;\r
356 } /*lint !e715 Some parameters are not used as only a subset of the serial port functionality is currently implemented. */\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 void vSerialPutString( xComPortHandle pxPort, const char * const pcString, unsigned short usStringLength )\r
360 {\r
361 unsigned short usByte;\r
362 char *pcNextChar;\r
363 \r
364         pcNextChar = ( char * ) pcString;\r
365 \r
366         for( usByte = 0; usByte < usStringLength; usByte++ )\r
367         {\r
368                 xQueueSend( pxPort->xCharsForTx, pcNextChar, serDONT_BLOCK );\r
369                 pcNextChar++;\r
370         }\r
371 \r
372         vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );\r
373 }\r
374 /*-----------------------------------------------------------*/\r
375 \r
376 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType xBlockTime )\r
377 {\r
378         /* Get the next character from the buffer, note that this routine is only \r
379         called having checked that the is (at least) one to get */\r
380         if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )\r
381         {\r
382                 return pdTRUE;\r
383         }\r
384         else\r
385         {\r
386                 return pdFALSE;\r
387         }\r
388 }\r
389 /*-----------------------------------------------------------*/\r
390 \r
391 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime )\r
392 {\r
393         if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
394         {\r
395                 return pdFAIL;\r
396         }\r
397 \r
398         vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );\r
399 \r
400         return pdPASS;\r
401 }\r
402 /*-----------------------------------------------------------*/\r
403 \r
404 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )\r
405 {\r
406 const portTickType xBlockTime = ( portTickType ) 0xffff;\r
407 \r
408         /* This function does nothing interesting, but test the \r
409         semaphore from ISR mechanism. */\r
410         return xSemaphoreTake( xPort->xTestSem, xBlockTime );\r
411 }\r
412 /*-----------------------------------------------------------*/\r
413 \r
414 void vSerialClose( xComPortHandle xPort )\r
415 {\r
416 unsigned short usOutput;\r
417 \r
418         /* Turn off the interrupts.  We may also want to delete the queues and/or\r
419         re-install the original ISR. */\r
420 \r
421         portENTER_CRITICAL();\r
422         {\r
423                 usOutput = portINPUT_WORD( xPort->usCtrlReg );\r
424 \r
425                 usOutput &= ~serENABLE_INTERRUPTS;\r
426                 usOutput &= ~serENABLE_TX_MACHINES;\r
427                 usOutput &= ~serENABLE_RX_MACHINES;\r
428                 portOUTPUT_WORD( xPort->usCtrlReg, usOutput );\r
429 \r
430                 usOutput = portINPUT_WORD( xPort->usIntReg );\r
431                 usOutput |= serINTERRUPT_MASK;\r
432                 portOUTPUT_WORD( xPort->usIntReg, usOutput );\r
433         }\r
434         portEXIT_CRITICAL();\r
435 }\r
436 /*-----------------------------------------------------------*/\r
437 \r
438 static portBASE_TYPE xComPortISR( xComPort * const pxPort )\r
439 {\r
440 unsigned short usStatusRegister;\r
441 char cChar;\r
442 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xContinue = pdTRUE;\r
443 \r
444         /* NOTE:  THIS IS NOT AN EFFICIENT ISR AS IT IS DESIGNED SOLELY TO TEST\r
445         THE SCHEDULER FUNCTIONALITY.  REAL APPLICATIONS SHOULD NOT USE THIS\r
446         FUNCTION. */\r
447 \r
448 \r
449         while( xContinue == pdTRUE )\r
450         {\r
451                 xContinue = pdFALSE;\r
452                 usStatusRegister = portINPUT_WORD( pxPort->usStatusReg );\r
453 \r
454                 if( usStatusRegister & serRX_READY )\r
455                 {\r
456                         cChar = ( char ) portINPUT_WORD( pxPort->usRxReg );\r
457                         xQueueSendFromISR( pxPort->xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
458 \r
459                         /* Also release the semaphore - this does nothing interesting and is just a test. */\r
460                         xSemaphoreGiveFromISR( pxPort->xTestSem, &xHigherPriorityTaskWoken );\r
461 \r
462                         /* We have performed an action this cycle - there may be other to perform. */\r
463                         xContinue = pdTRUE;\r
464                 }\r
465 \r
466                 if( pxPort->sTxInterruptOn && ( usStatusRegister & serTX_EMPTY ) )\r
467                 {\r
468                         if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
469                         {\r
470                                 portOUTPUT_WORD( pxPort->usTxReg, ( unsigned short ) cChar );\r
471 \r
472                                 /* We have performed an action this cycle - there may be others to perform. */\r
473                                 xContinue = pdTRUE;\r
474                         }\r
475                         else\r
476                         {\r
477                                 /* Queue empty, nothing to send */\r
478                                 vInterruptOff( pxPort, serTX_HOLD_EMPTY_INT );\r
479                         }\r
480                 }\r
481         }\r
482 \r
483         serRESET_PIC( pxPort->usIRQVector );\r
484 \r
485         /* If posting to the queue woke a task that was blocked on the queue we may\r
486         want to switch to the woken task - depending on its priority relative to\r
487         the task interrupted by this ISR. */\r
488         return xHigherPriorityTaskWoken;\r
489 }\r
490 \r
491 \r
492 \r
493 \r
494 \r
495 \r