]> git.sur5r.net Git - freertos/blob - Demo/PC/serial/serial.c
Update to V5.1.2.
[freertos] / Demo / PC / serial / serial.c
1 /*\r
2         This serial port driver is borrowed heavily from DZComm.  I have\r
3         simplified it by removing a lot of the functionality (hardware \r
4         flow control, etc.).  For more details and the full version see\r
5         http://dzcomm.sourceforge.net\r
6 \r
7 \r
8         FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 Richard Barry.\r
9 \r
10         This file is part of the FreeRTOS.org distribution.\r
11 \r
12         FreeRTOS.org is free software; you can redistribute it and/or modify\r
13         it under the terms of the GNU General Public License as published by\r
14         the Free Software Foundation; either version 2 of the License, or\r
15         (at your option) any later version.\r
16 \r
17         FreeRTOS.org is distributed in the hope that it will be useful,\r
18         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
19         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
20         GNU General Public License for more details.\r
21 \r
22         You should have received a copy of the GNU General Public License\r
23         along with FreeRTOS.org; if not, write to the Free Software\r
24         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
25 \r
26         A special exception to the GPL can be applied should you wish to distribute\r
27         a combined work that includes FreeRTOS.org, without being obliged to provide\r
28         the source code for any proprietary components.  See the licensing section \r
29         of http://www.FreeRTOS.org for full details of how and when the exception\r
30         can be applied.\r
31 \r
32     ***************************************************************************\r
33     ***************************************************************************\r
34     *                                                                         *\r
35     * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
36         *                                                                         *\r
37         * This is a concise, step by step, 'hands on' guide that describes both   *\r
38         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
39         * explains numerous examples that are written using the FreeRTOS API.     *\r
40         * Full source code for all the examples is provided in an accompanying    *\r
41         * .zip file.                                                              *\r
42     *                                                                         *\r
43     ***************************************************************************\r
44     ***************************************************************************\r
45 \r
46         Please ensure to read the configuration and relevant port sections of the\r
47         online documentation.\r
48 \r
49         http://www.FreeRTOS.org - Documentation, latest information, license and \r
50         contact details.\r
51 \r
52         http://www.SafeRTOS.com - A version that is certified for use in safety \r
53         critical systems.\r
54 \r
55         http://www.OpenRTOS.com - Commercial support, development, porting, \r
56         licensing and training services.\r
57 */\r
58 \r
59 /*\r
60 Changes from V1.00:\r
61         \r
62         + Call to the more efficient portSWITCH_CONTEXT() replaces the call to \r
63           taskYIELD() in the ISR.\r
64 \r
65 Changes from V1.2.0:\r
66 \r
67         + Added vSerialPutString().\r
68 \r
69 Changes from V1.2.3\r
70 \r
71         + The function xPortInitMinimal() has been renamed to \r
72           xSerialPortInitMinimal() and the function xPortInit() has been renamed\r
73           to xSerialPortInit().\r
74 \r
75 Changes From V2.0.0\r
76 \r
77         + Use portTickType in place of unsigned pdLONG for delay periods.\r
78         + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.\r
79 */\r
80 \r
81 \r
82 #include <stdlib.h>\r
83 #include <dos.h>\r
84 #include "FreeRTOS.h"\r
85 #include "queue.h"\r
86 #include "task.h"\r
87 #include "semphr.h"\r
88 #include "portasm.h"\r
89 \r
90 #define serMAX_IRQs                                             ( 16 )\r
91 #define serTRANSMIT_HOLD_EMPTY_INT              ( 0x02 )\r
92 #define serCOM1_STANDARD_IRQ                    ( ( unsigned portCHAR ) 4 )\r
93 #define serCOM2_STANDARD_IRQ                    ( ( unsigned portCHAR ) 3 )\r
94 \r
95 \r
96 #define serIMR_8259_0                                   ( ( unsigned portCHAR ) 0x21 )\r
97 #define serIMR_8259_1                                   ( ( unsigned portCHAR ) 0xa1 )\r
98 #define serISR_8259_0                                   ( ( unsigned portCHAR ) 0x20 )\r
99 #define serISR_8259_1                                   ( ( unsigned portCHAR ) 0xa0 )\r
100 #define serALL_COMS_INTERRUPTS                  ( ( unsigned portCHAR ) 0x0f )\r
101 #define serALL_MODEM_CTRL_INTERRUPTS    ( ( unsigned portCHAR ) 0x0f )\r
102 \r
103 #define serTRANSMIT_HOLD_OFFSET                                 ( 0 )\r
104 #define serRECEIVE_DATA_OFFSET                                  ( 0 )\r
105 #define serBAUD_RATE_DIVISOR_LOW_OFFSET                 ( 0 )\r
106 #define serBAUD_RATE_DIVISOR_HIGH_OFFSET                ( 1 )\r
107 #define serINTERRUPT_ENABLE_OFFSET                              ( 1 )\r
108 #define serINTERRUPT_ID_OFFSET                                  ( 2 )\r
109 #define serFIFO_CTRL_OFFSET                                             ( 2 )\r
110 #define serLINE_CTRL_OFFSET                                             ( 3 )\r
111 #define serMODEM_CTRL_OFFSET                                    ( 4 )\r
112 #define serLINE_STATUS_OFFSET                                   ( 5 )\r
113 #define serMODEM_STATUS_OFFSET                                  ( 6 )\r
114 #define serSCR_OFFSET                                                   ( 7 )\r
115 \r
116 #define serMAX_BAUD                     ( ( unsigned portLONG ) 115200UL )\r
117 \r
118 #define serNO_INTERRUPTS        ( 0x00 )\r
119 \r
120 #define vInterruptOn( pxPort, ucInterrupt )                                                                             \\r
121 {                                                                                                                                                               \\r
122         unsigned portCHAR ucIn = portINPUT_BYTE( pxPort->usInterruptEnableReg );        \\r
123         if( !( ucIn & ucInterrupt ) )                                                                                           \\r
124         {                                                                                                                                                       \\r
125                 portOUTPUT_BYTE( pxPort->usInterruptEnableReg, ucIn | ucInterrupt );    \\r
126         }                                                                                                                                                       \\r
127 }                                                                                                                                                               \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 #define vInterruptOff( pxPort, ucInterrupt )                                                                    \\r
131 {                                                                                                                                                               \\r
132         unsigned portCHAR ucIn = portINPUT_BYTE( pxPort->usInterruptEnableReg );        \\r
133         if( ucIn & ucInterrupt )                                                                                                        \\r
134         {                                                                                                                                                       \\r
135                 portOUTPUT_BYTE( pxPort->usInterruptEnableReg, ucIn & ~ucInterrupt);    \\r
136         }                                                                                                                                                       \\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 typedef enum\r
141\r
142         serCOM1, \r
143         serCOM2, \r
144         serCOM3, \r
145         serCOM4, \r
146         serCOM5, \r
147         serCOM6, \r
148         serCOM7, \r
149         serCOM8 \r
150 } eCOMPort;\r
151 \r
152 typedef enum \r
153\r
154         serNO_PARITY, \r
155         serODD_PARITY, \r
156         serEVEN_PARITY, \r
157         serMARK_PARITY, \r
158         serSPACE_PARITY \r
159 } eParity;\r
160 \r
161 typedef enum \r
162\r
163         serSTOP_1, \r
164         serSTOP_2 \r
165 } eStopBits;\r
166 \r
167 typedef enum \r
168\r
169         serBITS_5, \r
170         serBITS_6, \r
171         serBITS_7, \r
172         serBITS_8 \r
173 } eDataBits;\r
174 \r
175 typedef enum \r
176\r
177         ser50,          \r
178         ser75,          \r
179         ser110,         \r
180         ser134,         \r
181         ser150,    \r
182         ser200,\r
183         ser300,         \r
184         ser600,         \r
185         ser1200,        \r
186         ser1800,        \r
187         ser2400,   \r
188         ser4800,\r
189         ser9600,                \r
190         ser19200,       \r
191         ser38400,       \r
192         ser57600,       \r
193         ser115200\r
194 } eBaud;\r
195 \r
196 /* This *MUST* match the order in the eBaud definition. */\r
197 unsigned portLONG ulBaudFromEnum[] = \r
198\r
199         ( unsigned portLONG ) 50, \r
200         ( unsigned portLONG ) 75, \r
201         ( unsigned portLONG ) 110, \r
202         ( unsigned portLONG ) 134, \r
203         ( unsigned portLONG ) 150,      \r
204         ( unsigned portLONG ) 200, \r
205         ( unsigned portLONG ) 300, \r
206         ( unsigned portLONG ) 600, \r
207         ( unsigned portLONG ) 1200, \r
208         ( unsigned portLONG ) 1800, \r
209         ( unsigned portLONG ) 2400,   \r
210         ( unsigned portLONG ) 4800,   \r
211         ( unsigned portLONG ) 9600,  \r
212         ( unsigned portLONG ) 19200,  \r
213         ( unsigned portLONG ) 38400UL,\r
214         ( unsigned portLONG ) 57600UL,\r
215         ( unsigned portLONG ) 115200UL\r
216 };\r
217 \r
218 typedef struct xCOM_PORT\r
219\r
220         unsigned portSHORT sPort;   /* comm port address eg. 0x3f8    */\r
221         unsigned portCHAR ucIRQ;    /* comm IRQ eg. 3                 */\r
222 \r
223         /* Next two fields used for setting up the IRQ routine and\r
224         * (un)masking the interrupt in certain circumstances.\r
225         */\r
226         unsigned portSHORT usIRQVector;\r
227         unsigned portCHAR ucInterruptEnableMast;\r
228 \r
229         /* Read/Write buffers. */\r
230         xQueueHandle xRxedChars; \r
231         xQueueHandle xCharsForTx;\r
232 \r
233         /* This lot are set up to minimise CPU time where accessing the comm\r
234         * port's registers.\r
235         */\r
236         unsigned portSHORT usTransmitHoldReg; \r
237         unsigned portSHORT usReceiveDataRegister;\r
238         unsigned portSHORT usBaudRateDivisorLow; \r
239         unsigned portSHORT usBaudRateDivisorHigh;\r
240         unsigned portSHORT usInterruptEnableReg;\r
241         unsigned portSHORT usInterruptIDReg;\r
242         unsigned portSHORT usFIFOCtrlReg;\r
243         unsigned portSHORT usLineCtrlReg;\r
244         unsigned portSHORT usModemCtrlReg;\r
245         unsigned portSHORT usLineStatusReg;\r
246         unsigned portSHORT usModemStatusReg;\r
247         unsigned portSHORT usSCRReg;\r
248         unsigned portSHORT us8259InterruptServiceReg;\r
249         unsigned portSHORT us8259InterruptMaskReg;\r
250 \r
251         /* This semaphore does nothing useful except test a feature of the\r
252         scheduler. */\r
253         xSemaphoreHandle xTestSem;\r
254 \r
255 } xComPort;\r
256 \r
257 typedef xComPort *xComPortHandle;\r
258 \r
259 /* A xComPort structure can be associated with each IRQ.  Initially none\r
260 are create/installed. */\r
261 xComPort *xPortStatus[ serMAX_IRQs ] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };\r
262 \r
263 /*-----------------------------------------------------------*/\r
264 \r
265 /* These prototypes are repeated here so we don't have to include the serial header.  This allows\r
266 the xComPortHandle structure details to be private to this file. */\r
267 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );\r
268 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime );\r
269 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime );\r
270 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort );\r
271 \r
272 static void prvSetupPortHardware( xComPort *pxPort, eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits );\r
273 static portSHORT sComPortISR( const xComPort * const pxPort );\r
274 \r
275 /*-----------------------------------------------------------*/\r
276 \r
277 /* Define an interrupt handler for each slot in the xPortStatus array. */\r
278 \r
279 #define COM_IRQ_WRAPPER(N)                                                                              \\r
280         static void __interrupt COM_IRQ##N##_WRAPPER( void )            \\r
281         {                                                                                                                       \\r
282                 portDISABLE_INTERRUPTS();                                                               \\r
283                 if( sComPortISR( xPortStatus[##N##] ) )                                 \\r
284                 {                                                                                                               \\r
285                         portSWITCH_CONTEXT();                                                           \\r
286                 }                                                                                                               \\r
287         } \r
288 \r
289 COM_IRQ_WRAPPER( 0 )\r
290 COM_IRQ_WRAPPER( 1 )\r
291 COM_IRQ_WRAPPER( 2 )\r
292 COM_IRQ_WRAPPER( 3 )\r
293 COM_IRQ_WRAPPER( 4 )\r
294 COM_IRQ_WRAPPER( 5 )\r
295 COM_IRQ_WRAPPER( 6 )\r
296 COM_IRQ_WRAPPER( 7 )\r
297 COM_IRQ_WRAPPER( 8 )\r
298 COM_IRQ_WRAPPER( 9 )\r
299 COM_IRQ_WRAPPER( 10 )\r
300 COM_IRQ_WRAPPER( 11 )\r
301 COM_IRQ_WRAPPER( 12 )\r
302 COM_IRQ_WRAPPER( 13 )\r
303 COM_IRQ_WRAPPER( 14 )\r
304 COM_IRQ_WRAPPER( 15 )\r
305 \r
306 static pxISR xISRs[ serMAX_IRQs ] = \r
307 {\r
308         COM_IRQ0_WRAPPER, \r
309         COM_IRQ1_WRAPPER, \r
310         COM_IRQ2_WRAPPER, \r
311         COM_IRQ3_WRAPPER, \r
312         COM_IRQ4_WRAPPER, \r
313         COM_IRQ5_WRAPPER, \r
314         COM_IRQ6_WRAPPER, \r
315         COM_IRQ7_WRAPPER, \r
316         COM_IRQ8_WRAPPER, \r
317         COM_IRQ9_WRAPPER, \r
318         COM_IRQ10_WRAPPER, \r
319         COM_IRQ11_WRAPPER, \r
320         COM_IRQ12_WRAPPER, \r
321         COM_IRQ13_WRAPPER, \r
322         COM_IRQ14_WRAPPER,\r
323         COM_IRQ15_WRAPPER\r
324 };\r
325 \r
326 static pxISR xOldISRs[ serMAX_IRQs ] = { NULL };\r
327 \r
328 /*-----------------------------------------------------------*/\r
329 \r
330 \r
331 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )\r
332 {\r
333 xComPort *pxPort;\r
334 \r
335         /* Create a structure to handle this port. */\r
336         pxPort = ( xComPort * ) pvPortMalloc( sizeof( xComPort ) );\r
337         \r
338         if( pxPort != NULL )\r
339         {\r
340                 /* Create the queues used by the comtest task. */\r
341                 pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
342                 pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
343 \r
344                 /* Create the test semaphore.  This does nothing useful except test a feature of the scheduler. */\r
345                 vSemaphoreCreateBinary( pxPort->xTestSem );\r
346 \r
347                 prvSetupPortHardware( pxPort, ePort, eWantedBaud, eWantedParity, eWantedDataBits, eWantedStopBits );\r
348 \r
349                 return pxPort;\r
350         }\r
351 \r
352         return NULL;\r
353 }\r
354 /*-----------------------------------------------------------*/\r
355 \r
356 static void prvSetupPortHardware( xComPort *pxPort, eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits )\r
357 {\r
358 portSHORT sIn;\r
359 unsigned portLONG ulDivisor;\r
360 unsigned portCHAR ucDivisorLow;\r
361 unsigned portCHAR ucDivisorHigh;\r
362 unsigned portCHAR ucCommParam;\r
363 \r
364         /* IRQ numbers - standard */\r
365         if( ( ePort == serCOM1 ) || ( ePort == serCOM3 ) || ( ePort == serCOM5 ) || ( ePort == serCOM7 ) )\r
366         {\r
367                 pxPort->ucIRQ = serCOM1_STANDARD_IRQ;\r
368                 pxPort->sPort = 0x3f8;\r
369         }\r
370         else\r
371         {\r
372                 pxPort->ucIRQ = serCOM2_STANDARD_IRQ;\r
373                 pxPort->sPort = 0x2f8;\r
374         }\r
375 \r
376         /* Set up variables in port making it easy to see which sIn/o address is which */\r
377         pxPort->usTransmitHoldReg = pxPort->sPort + serTRANSMIT_HOLD_OFFSET;\r
378         pxPort->usReceiveDataRegister = pxPort->sPort + serRECEIVE_DATA_OFFSET;\r
379         pxPort->usBaudRateDivisorLow = pxPort->sPort + serBAUD_RATE_DIVISOR_LOW_OFFSET;\r
380         pxPort->usBaudRateDivisorHigh = pxPort->sPort + serBAUD_RATE_DIVISOR_HIGH_OFFSET;\r
381         pxPort->usInterruptEnableReg = pxPort->sPort + serINTERRUPT_ENABLE_OFFSET;\r
382         pxPort->usInterruptIDReg = pxPort->sPort + serINTERRUPT_ID_OFFSET;\r
383         pxPort->usFIFOCtrlReg = pxPort->sPort + serFIFO_CTRL_OFFSET;\r
384         pxPort->usLineCtrlReg = pxPort->sPort + serLINE_CTRL_OFFSET;\r
385         pxPort->usModemCtrlReg = pxPort->sPort + serMODEM_CTRL_OFFSET;\r
386         pxPort->usLineStatusReg = pxPort->sPort + serLINE_STATUS_OFFSET;\r
387         pxPort->usModemStatusReg = pxPort->sPort + serMODEM_STATUS_OFFSET;\r
388         pxPort->usSCRReg = pxPort->sPort + serSCR_OFFSET;\r
389 \r
390         /* Set communication parameters. */\r
391         ulDivisor = serMAX_BAUD / ulBaudFromEnum[ eWantedBaud ];\r
392         ucDivisorLow = ( unsigned portCHAR ) ulDivisor & ( unsigned portCHAR ) 0xff;\r
393         ucDivisorHigh = ( unsigned portCHAR ) ( ( ( unsigned portSHORT ) ulDivisor >> 8 ) & 0xff );\r
394         \r
395         switch( eWantedParity )\r
396         {       \r
397                 case serNO_PARITY:              ucCommParam = 0x00;\r
398                                                                 break;\r
399                 case serODD_PARITY:             ucCommParam = 0x08;\r
400                                                                 break;\r
401                 case serEVEN_PARITY:    ucCommParam = 0x18;\r
402                                                                 break;\r
403                 case serMARK_PARITY:    ucCommParam = 0x28;\r
404                                                                 break;\r
405                 case serSPACE_PARITY:   ucCommParam = 0x38;\r
406                                                                 break;\r
407                 default:                                ucCommParam = 0x00;\r
408                                                                 break;\r
409         }\r
410 \r
411         switch ( eWantedDataBits )\r
412         {\r
413                 case serBITS_5: ucCommParam |= 0x00;\r
414                                                 break;\r
415                 case serBITS_6: ucCommParam |= 0x01;\r
416                                                 break;\r
417                 case serBITS_7: ucCommParam |= 0x02;\r
418                                                 break;\r
419                 case serBITS_8: ucCommParam |= 0x03;\r
420                                                 break;\r
421                 default:                ucCommParam |= 0x03;\r
422                                                 break;\r
423         }\r
424 \r
425         if( eWantedStopBits == serSTOP_2 ) \r
426         {\r
427                 ucCommParam |= 0x04;\r
428         }\r
429 \r
430         /* Reset UART into known state - Thanks to Bradley Town */\r
431         portOUTPUT_BYTE( pxPort->usLineCtrlReg, 0x00 );                 /* Access usTransmitHoldReg/RBR/usInterruptEnableReg */\r
432         portOUTPUT_BYTE( pxPort->usInterruptEnableReg, 0x00 );  /* Disable interrupts from UART */\r
433         portOUTPUT_BYTE( pxPort->usModemCtrlReg, 0x04 );                /* Enable some multi-port cards */\r
434 \r
435         /* Code based on stuff from SVAsync lib. Clear UART Status and data registers\r
436         setting up FIFO if possible */\r
437         sIn = portINPUT_BYTE( pxPort->usSCRReg );\r
438         portOUTPUT_BYTE( pxPort->usSCRReg, 0x55 );\r
439 \r
440         if( portINPUT_BYTE( pxPort->usSCRReg ) == 0x55 )\r
441         {\r
442                 /* The chip is better than an 8250 */\r
443                 portOUTPUT_BYTE( pxPort->usSCRReg, sIn );       /* Set usSCRReg back to what it was before */\r
444                 portINPUT_BYTE( pxPort->usSCRReg);                      /* Give slow motherboards a chance */\r
445 \r
446                 /* Try and start the FIFO. It appears that some chips need a two call \r
447                 protocol, but those that don't seem to work even if you do start it twice. \r
448                 The first call is simply to start it, the second starts it and sets an 8 \r
449                 byte FIFO trigger level. */\r
450                 portOUTPUT_BYTE( pxPort->usFIFOCtrlReg, 0x01 );\r
451                 portINPUT_BYTE( pxPort->usFIFOCtrlReg );                        /* Give slow motherboards a chance to catch up */\r
452                 portOUTPUT_BYTE( pxPort->usFIFOCtrlReg, 0x87 );\r
453 \r
454                 /* Check that the FIFO initialised */\r
455                 if( ( portINPUT_BYTE( pxPort->usInterruptIDReg ) & 0xc0 ) != 0xc0 )\r
456                 {\r
457                         /* It didn't so we assume it isn't there but disable it to be on the \r
458                         safe side. */\r
459                         portOUTPUT_BYTE( pxPort->usInterruptIDReg, 0xfe );\r
460                 }\r
461         }\r
462 \r
463         /* End of (modified) SVAsync code.  \r
464         Set interrupt parameters calculating mask for 8259 controller's \r
465         IMR and number of interrupt handler for given irq level  */\r
466         if (pxPort->ucIRQ <= 7)\r
467         {       \r
468                 /* if 0<=irq<=7 first IMR address used */\r
469                 pxPort->ucInterruptEnableMast = ~(0x01 << pxPort->ucIRQ);\r
470                 pxPort->usIRQVector = pxPort->ucIRQ + 8;\r
471                 pxPort->us8259InterruptMaskReg = serIMR_8259_0;\r
472                 pxPort->us8259InterruptServiceReg = serISR_8259_0;\r
473         }\r
474         else\r
475         {\r
476                 pxPort->ucInterruptEnableMast = ~( 0x01 << ( pxPort->ucIRQ % 8 ) );\r
477                 pxPort->usIRQVector = 0x70 + ( pxPort->ucIRQ - 8) ;\r
478                 pxPort->us8259InterruptMaskReg = serIMR_8259_1;\r
479                 pxPort->us8259InterruptServiceReg = serISR_8259_1;\r
480         }\r
481 \r
482         /* Set Port Toggle to usBaudRateDivisorLow/usBaudRateDivisorHigh registers \r
483         to set baud rate */\r
484         portOUTPUT_BYTE( pxPort->usLineCtrlReg, ucCommParam | 0x80 );\r
485         portOUTPUT_BYTE( pxPort->usBaudRateDivisorLow, ucDivisorLow );\r
486         portOUTPUT_BYTE( pxPort->usBaudRateDivisorHigh, ucDivisorHigh );\r
487 \r
488         /* reset usLineCtrlReg and Port Toggleout */\r
489         portOUTPUT_BYTE( pxPort->usLineCtrlReg, ucCommParam & 0x7F );\r
490 \r
491         portENTER_CRITICAL();\r
492 \r
493         if( xPortStatus[ pxPort->ucIRQ ] == NULL )\r
494         {       \r
495                 xPortStatus[ pxPort->ucIRQ ] = pxPort;\r
496         }\r
497         \r
498         xOldISRs[ pxPort->ucIRQ ] = _dos_getvect( pxPort->usIRQVector );\r
499         _dos_setvect( pxPort->usIRQVector, xISRs[ pxPort->ucIRQ ] );\r
500 \r
501         /* enable interrupt pxPort->ucIRQ level */\r
502         portOUTPUT_BYTE( pxPort->us8259InterruptMaskReg, portINPUT_BYTE( pxPort->us8259InterruptMaskReg ) & pxPort->ucInterruptEnableMast );\r
503 \r
504         /* And allow interrupts again now the hairy bit's done */\r
505         portEXIT_CRITICAL();            \r
506 \r
507         /* This version does not allow flow control. */\r
508         portOUTPUT_BYTE( pxPort->usModemCtrlReg, serALL_MODEM_CTRL_INTERRUPTS );\r
509 \r
510         /* enable all communication's interrupts */\r
511         portOUTPUT_BYTE( pxPort->usInterruptEnableReg, serALL_COMS_INTERRUPTS );\r
512 }\r
513 /*-----------------------------------------------------------*/\r
514 \r
515 static portSHORT sComPortISR( const xComPort * const pxPort )\r
516 {\r
517 portSHORT sInterruptID;\r
518 portCHAR cIn, cOut;\r
519 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
520 extern void vComTestUnsuspendTask( void );\r
521 \r
522         portOUTPUT_BYTE( pxPort->us8259InterruptMaskReg, ( portINPUT_BYTE( pxPort->us8259InterruptMaskReg) | ~pxPort->ucInterruptEnableMast ) );\r
523 \r
524         /* Decide which UART has issued the interrupt */\r
525         sInterruptID = portINPUT_BYTE( pxPort->usInterruptIDReg );\r
526 \r
527         /* service whatever requests the calling UART may have. The top 4 bits are\r
528         either unused or indicate the presence of a functioning FIFO, which we don't\r
529         need to know. So trim them off to simplify the switch statement below. */\r
530         sInterruptID &= 0x0f;\r
531         do\r
532         {\r
533                 switch( sInterruptID )\r
534                 {\r
535                         case 0x0c:      /* Timeout\r
536                                                 Called when FIFO not up to trigger level but no activity for \r
537                                                 a while. Handled exactly as RDAINT, see below for \r
538                                                 description. */\r
539                                                 do\r
540                                                 {\r
541                                                         cIn = ( portCHAR ) portINPUT_BYTE( pxPort->usReceiveDataRegister );                                             \r
542                                                         xQueueSendFromISR( pxPort->xRxedChars, &cIn, &xHigherPriorityTaskWoken );\r
543 \r
544                                                         /* Also release the semaphore - this does nothing interesting and is just a test.\r
545                                                         We first attempt to unsuspend the task to check the scheduler correctely detects\r
546                                                         this as an invalid call, then give the semaphore for real. */\r
547                                                         vComTestUnsuspendTask();\r
548                                                         xSemaphoreGiveFromISR( pxPort->xTestSem, &xHigherPriorityTaskWoken );\r
549 \r
550                                                 } while( portINPUT_BYTE( pxPort->usLineStatusReg ) & 0x01 );\r
551                                                 break;\r
552 \r
553                         case 0x06:      /* LSINT */\r
554                                                 portINPUT_BYTE( pxPort->usLineStatusReg );\r
555                                                 break;\r
556 \r
557                         case 0x04:      /* RDAINT */\r
558                                                 /* The usInterruptIDReg flag tested above stops when the \r
559                                                 FIFO is below the trigger level rather than empty, whereas \r
560                                                 this flag allows one to empty it: (do loop because there \r
561                                                 must be at least one to read by virtue of having got here.) */\r
562                                                 do\r
563                                                 {\r
564                                                         cIn = ( portCHAR ) portINPUT_BYTE( pxPort->usReceiveDataRegister );                                             \r
565                                                         xQueueSendFromISR( pxPort->xRxedChars, &cIn, &xHigherPriorityTaskWoken );\r
566 \r
567                                                         /* Also release the semaphore - this does nothing interesting and is just a test.\r
568                                                         We first attempt to unsuspend the task to check the scheduler correctely detects\r
569                                                         this as an invalid call, then give the semaphore for real. */\r
570                                                         vComTestUnsuspendTask();\r
571                                                         xSemaphoreGiveFromISR( pxPort->xTestSem, &xHigherPriorityTaskWoken );\r
572 \r
573                                                 } while( portINPUT_BYTE( pxPort->usLineStatusReg ) & 0x01 );\r
574                                                 break;\r
575 \r
576                         case 0x02:      /* serTRANSMIT_HOLD_EMPTY_INT */\r
577                                                 if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cOut, &xHigherPriorityTaskWoken ) != pdTRUE )                                           \r
578                                                 {                                                                                                                                                                               \r
579                                                         /* Queue empty, nothing to send */                                                                                                      \r
580                                                         vInterruptOff( pxPort, serTRANSMIT_HOLD_EMPTY_INT);                                                                     \r
581                                                 }                                                                                                                                                                               \r
582                                                 else                                                                                                                                                                    \r
583                                                 {                                                                                                                                                                               \r
584                                                         portOUTPUT_BYTE( pxPort->usTransmitHoldReg, ( portSHORT ) cOut );                                       \r
585                                                 }\r
586                                                 break;\r
587 \r
588                         case 0x00:      /* MSINT */\r
589                                                 portINPUT_BYTE( pxPort->usModemStatusReg );\r
590                                                 break;\r
591                 }               \r
592 \r
593                 /* Get the next instruction, trimming as above */\r
594                 sInterruptID = portINPUT_BYTE( pxPort->usInterruptIDReg ) & 0x0f;\r
595 \r
596         } while( !( sInterruptID & 0x01 ) );\r
597 \r
598         if( pxPort->ucIRQ > 7 )\r
599         {\r
600                 portOUTPUT_BYTE( 0xA0, 0x60 + ( pxPort->ucIRQ & 0x07 ) );\r
601                 portOUTPUT_BYTE( 0x20, 0x62);\r
602         }\r
603         else\r
604         {\r
605                 portOUTPUT_BYTE( 0x20, 0x60 + pxPort->ucIRQ );\r
606         }\r
607 \r
608         portOUTPUT_BYTE( pxPort->us8259InterruptMaskReg, portINPUT_BYTE( pxPort->us8259InterruptMaskReg ) & pxPort->ucInterruptEnableMast );\r
609 \r
610         /* If posting any of the characters to a queue woke a task that was blocked on\r
611         the queue we may want to return to the task just woken (depending on its \r
612         priority relative to the task this ISR interrupted. */\r
613         return xHigherPriorityTaskWoken;\r
614 }\r
615 /*-----------------------------------------------------------*/\r
616 \r
617 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )\r
618 {\r
619         /* Get the next character from the buffer, note that this routine is only \r
620         called having checked that the is (at least) one to get */\r
621         if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )\r
622         {\r
623                 return pdTRUE;\r
624         }\r
625         else\r
626         {\r
627                 return pdFALSE;\r
628         }\r
629 }\r
630 /*-----------------------------------------------------------*/\r
631 \r
632 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )\r
633 {\r
634         if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
635         {\r
636                 return pdFAIL;\r
637         }\r
638 \r
639         vInterruptOn( pxPort, serTRANSMIT_HOLD_EMPTY_INT );\r
640 \r
641         return pdPASS;\r
642 }\r
643 /*-----------------------------------------------------------*/\r
644 \r
645 void vSerialPutString( xComPortHandle pxPort, const portCHAR * const pcString, unsigned portSHORT usStringLength )\r
646 {\r
647 portCHAR * pcNextChar;\r
648 const portTickType xNoBlock = ( portTickType ) 0;\r
649 \r
650         /* Stop warnings. */\r
651         ( void ) usStringLength;\r
652 \r
653         pcNextChar = ( portCHAR * ) pcString;\r
654         while( *pcNextChar )\r
655         {\r
656                 xSerialPutChar( pxPort, *pcNextChar, xNoBlock );\r
657                 pcNextChar++;\r
658         }\r
659 }\r
660 /*-----------------------------------------------------------*/\r
661 \r
662 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )\r
663 {\r
664 const portTickType xBlockTime = ( portTickType ) 0xffff;\r
665 \r
666         /* This function does nothing interesting, but test the \r
667         semaphore from ISR mechanism. */\r
668         return xSemaphoreTake( xPort->xTestSem, xBlockTime );   \r
669 }\r
670 /*-----------------------------------------------------------*/\r
671 \r
672 void vSerialClose( xComPortHandle xPort )\r
673 {\r
674         portENTER_CRITICAL();\r
675 \r
676         /* Turn off the interrupts. */\r
677         portOUTPUT_BYTE( xPort->usModemCtrlReg, serNO_INTERRUPTS );\r
678         portOUTPUT_BYTE( xPort->usInterruptEnableReg, serNO_INTERRUPTS );\r
679 \r
680         /* Put back the original ISR. */\r
681         _dos_setvect( xPort->usIRQVector, xOldISRs[ xPort->ucIRQ ] );\r
682 \r
683         /* Remove the reference in the array of xComPort structures. */\r
684         xPortStatus[ xPort->ucIRQ ] = NULL;\r
685 \r
686         /* Delete the queues. */\r
687         vQueueDelete( xPort->xRxedChars );\r
688         vQueueDelete( xPort->xCharsForTx );\r
689 \r
690         vPortFree( ( void * ) xPort );\r
691 \r
692         portEXIT_CRITICAL();\r
693 }\r
694 \r