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