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