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