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