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