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