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