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