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