2 FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
39 + Call to the more efficient portSWITCH_CONTEXT() replaces the call to
\r
40 taskYIELD() in the ISR.
\r
44 + The semaphore task is not operational. This does nothing but check
\r
45 the semaphore from ISR functionality.
\r
46 + ISR modified slightly so only Rx or Tx is serviced per ISR - not both.
\r
48 Changes from V1.2.0:
\r
50 + Change so Tx uses a DMA channel, and Rx uses an interrupt.
\r
54 + The function xPortInitMinimal() has been renamed to
\r
55 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
56 to xSerialPortInit().
\r
60 + Reverted back to the non-DMA serial port driver, with a slightly modified
\r
61 ISR. This is a better test of the scheduler mechanisms.
\r
62 + A critical section is now used in vInterruptOn().
\r
63 + Flag sTxInterruptOn has been added to the port structure. This allows
\r
64 checking of the interrupt enable status without performing any IO.
\r
68 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
69 + Slightly more efficient vSerialSendString() implementation.
\r
70 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
75 #include "FreeRTOS.h"
\r
78 #include "portasm.h"
\r
81 #define serMAX_PORTS ( ( unsigned portSHORT ) 2 )
\r
83 #define serPORT_0_INT_REG ( 0xff44 )
\r
84 #define serPORT_0_BAUD_REG ( 0xff88 )
\r
85 #define serPORT_0_RX_REG ( 0xff86 )
\r
86 #define serPORT_0_TX_REG ( 0xff84 )
\r
87 #define serPORT_0_STATUS_REG ( 0xff82 )
\r
88 #define serPORT_0_CTRL_REG ( 0xff80 )
\r
89 #define serPORT_0_IRQ ( 0x14 )
\r
91 #define serPORT_1_INT_REG ( 0xff42 )
\r
92 #define serPORT_1_BAUD_REG ( 0xff18 )
\r
93 #define serPORT_1_RX_REG ( 0xff16 )
\r
94 #define serPORT_1_TX_REG ( 0xff14 )
\r
95 #define serPORT_1_STATUS_REG ( 0xff12 )
\r
96 #define serPORT_1_CTRL_REG ( 0xff10 )
\r
97 #define serPORT_1_IRQ ( 0x11 )
\r
99 #define serTX_EMPTY ( ( unsigned portSHORT ) 0x40 )
\r
100 #define serRX_READY ( ( unsigned portSHORT ) 0x80 )
\r
102 #define serRESET_PIC( usEOI_TYPE ) portOUTPUT_WORD( ( unsigned portSHORT ) 0xff22, usEOI_TYPE )
\r
103 #define serTX_HOLD_EMPTY_INT ( ( unsigned portSHORT ) 0x100 )
\r
105 #define serENABLE_INTERRUPTS ( ( unsigned portSHORT ) 0x80 )
\r
106 #define serMODE ( ( unsigned portSHORT ) 0x01 )
\r
107 #define serENABLE_TX_MACHINES ( ( unsigned portSHORT ) 0x40 )
\r
108 #define serENABLE_RX_MACHINES ( ( unsigned portSHORT ) 0x20 )
\r
109 #define serINTERRUPT_MASK ( ( unsigned portSHORT ) 0x08 )
\r
110 #define serCLEAR_ALL_STATUS_BITS ( ( unsigned portSHORT ) 0x00 )
\r
111 #define serINTERRUPT_PRIORITY ( ( unsigned portSHORT ) 0x01 ) /*< Just below the scheduler priority. */
\r
113 #define serDONT_BLOCK ( ( portTickType ) 0 )
\r
171 /* Must be same order as eBaud definitions. */
\r
172 static const unsigned portSHORT usBaudRateDivisor[] =
\r
174 0, /* Not sure if the first 6 are correct. First cannot be used. */
\r
194 typedef struct xCOM_PORT
\r
196 /* Hardware parameters for this port. */
\r
197 portSHORT sTxInterruptOn;
\r
198 unsigned portSHORT usIntReg;
\r
199 unsigned portSHORT usBaudReg;
\r
200 unsigned portSHORT usRxReg;
\r
201 unsigned portSHORT usTxReg;
\r
202 unsigned portSHORT usStatusReg;
\r
203 unsigned portSHORT usCtrlReg;
\r
205 unsigned portSHORT usIRQVector;
\r
207 /* Queues used for communications with com test task. */
\r
208 xQueueHandle xRxedChars;
\r
209 xQueueHandle xCharsForTx;
\r
211 /* This semaphore does nothing useful except test a feature of the
\r
213 xSemaphoreHandle xTestSem;
\r
217 static xComPort xPorts[ serMAX_PORTS ] =
\r
219 { pdFALSE, serPORT_0_INT_REG, serPORT_0_BAUD_REG, serPORT_0_RX_REG, serPORT_0_TX_REG, serPORT_0_STATUS_REG, serPORT_0_CTRL_REG, serPORT_0_IRQ, NULL, NULL, NULL },
\r
220 { pdFALSE, serPORT_1_INT_REG, serPORT_1_BAUD_REG, serPORT_1_RX_REG, serPORT_1_TX_REG, serPORT_1_STATUS_REG, serPORT_1_CTRL_REG, serPORT_1_IRQ, NULL, NULL, NULL }
\r
223 typedef xComPort * xComPortHandle;
\r
225 /* These prototypes are repeated here so we don't have to include the serial header. This allows
\r
226 the xComPortHandle structure details to be private to this file. */
\r
227 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );
\r
228 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime );
\r
229 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime );
\r
230 void vSerialClose( xComPortHandle xPort );
\r
231 portSHORT sSerialWaitForSemaphore( xComPortHandle xPort );
\r
232 /*-----------------------------------------------------------*/
\r
234 static portSHORT xComPortISR( xComPort * const pxPort );
\r
236 #define vInterruptOn( pxPort, usInterrupt ) \
\r
238 unsigned portSHORT usIn; \
\r
240 portENTER_CRITICAL(); \
\r
242 if( pxPort->sTxInterruptOn == pdFALSE ) \
\r
244 usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
245 portOUTPUT_WORD( pxPort->usCtrlReg, usIn | usInterrupt ); \
\r
247 pxPort->sTxInterruptOn = pdTRUE; \
\r
250 portEXIT_CRITICAL(); \
\r
252 /*-----------------------------------------------------------*/
\r
254 #define vInterruptOff( pxPort, usInterrupt ) \
\r
256 unsigned portSHORT usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
257 if( usIn & usInterrupt ) \
\r
259 portOUTPUT_WORD( pxPort->usCtrlReg, usIn & ~usInterrupt); \
\r
260 pxPort->sTxInterruptOn = pdFALSE; \
\r
263 /*-----------------------------------------------------------*/
\r
266 /* Define an interrupt handler for each port */
\r
267 #define COM_IRQ_WRAPPER(N) \
\r
268 static void __interrupt COM_IRQ##N##_WRAPPER( void ) \
\r
270 if( xComPortISR( &( xPorts[##N##] ) ) ) \
\r
272 portSWITCH_CONTEXT(); \
\r
278 COM_IRQ_WRAPPER( 0 )
\r
279 COM_IRQ_WRAPPER( 1 )
\r
281 static pxISR xISRs[ serMAX_PORTS ] =
\r
287 /*-----------------------------------------------------------*/
\r
289 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
291 unsigned portSHORT usPort;
\r
292 xComPortHandle pxPort = NULL;
\r
294 /* BAUDDIV = ( Microprocessor Clock / Baud Rate ) / 16 */
\r
296 /* Only n, 8, 1 is supported so these parameters are not required for this
\r
298 ( void ) eWantedParity;
\r
299 ( void ) eWantedDataBits;
\r
300 ( void ) eWantedStopBits;
\r
302 /* Currently only n,8,1 is supported. */
\r
304 usPort = ( unsigned portSHORT ) ePort;
\r
306 if( usPort < serMAX_PORTS )
\r
308 pxPort = &( xPorts[ usPort ] );
\r
310 portENTER_CRITICAL();
\r
312 unsigned portSHORT usInWord;
\r
314 /* Create the queues used by the com test task. */
\r
315 pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
316 pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
318 /* Create the test semaphore. This does nothing useful except test a feature of the scheduler. */
\r
319 vSemaphoreCreateBinary( pxPort->xTestSem );
\r
321 /* There is no ISR here already to restore later. */
\r
322 _dos_setvect( ( portSHORT ) pxPort->usIRQVector, xISRs[ usPort ] );
\r
324 usInWord = portINPUT_WORD( pxPort->usIntReg );
\r
325 usInWord &= ~serINTERRUPT_MASK;
\r
326 usInWord |= serINTERRUPT_PRIORITY;
\r
327 portOUTPUT_WORD( pxPort->usIntReg, usInWord );
\r
329 portOUTPUT_WORD( pxPort->usBaudReg, usBaudRateDivisor[ eWantedBaud ] );
\r
330 portOUTPUT_WORD( pxPort->usCtrlReg, serENABLE_INTERRUPTS | serMODE | serENABLE_TX_MACHINES | serENABLE_RX_MACHINES );
\r
332 portOUTPUT_WORD( pxPort->usStatusReg, serCLEAR_ALL_STATUS_BITS );
\r
334 portEXIT_CRITICAL();
\r
338 } /*lint !e715 Some parameters are not used as only a subset of the serial port functionality is currently implemented. */
\r
339 /*-----------------------------------------------------------*/
\r
341 void vSerialPutString( xComPortHandle pxPort, const portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
343 unsigned portSHORT usByte;
\r
344 portCHAR *pcNextChar;
\r
346 pcNextChar = ( portCHAR * ) pcString;
\r
348 for( usByte = 0; usByte < usStringLength; usByte++ )
\r
350 xQueueSend( pxPort->xCharsForTx, pcNextChar, serDONT_BLOCK );
\r
354 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
356 /*-----------------------------------------------------------*/
\r
358 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )
\r
360 /* Get the next character from the buffer, note that this routine is only
\r
361 called having checked that the is (at least) one to get */
\r
362 if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )
\r
371 /*-----------------------------------------------------------*/
\r
373 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )
\r
375 if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
380 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
384 /*-----------------------------------------------------------*/
\r
386 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )
\r
388 const portTickType xBlockTime = ( portTickType ) 0xffff;
\r
390 /* This function does nothing interesting, but test the
\r
391 semaphore from ISR mechanism. */
\r
392 return xSemaphoreTake( xPort->xTestSem, xBlockTime );
\r
394 /*-----------------------------------------------------------*/
\r
396 void vSerialClose( xComPortHandle xPort )
\r
398 unsigned portSHORT usOutput;
\r
400 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
401 re-install the original ISR. */
\r
403 portENTER_CRITICAL();
\r
405 usOutput = portINPUT_WORD( xPort->usCtrlReg );
\r
407 usOutput &= ~serENABLE_INTERRUPTS;
\r
408 usOutput &= ~serENABLE_TX_MACHINES;
\r
409 usOutput &= ~serENABLE_RX_MACHINES;
\r
410 portOUTPUT_WORD( xPort->usCtrlReg, usOutput );
\r
412 usOutput = portINPUT_WORD( xPort->usIntReg );
\r
413 usOutput |= serINTERRUPT_MASK;
\r
414 portOUTPUT_WORD( xPort->usIntReg, usOutput );
\r
416 portEXIT_CRITICAL();
\r
418 /*-----------------------------------------------------------*/
\r
420 static portBASE_TYPE xComPortISR( xComPort * const pxPort )
\r
422 unsigned portSHORT usStatusRegister;
\r
424 portBASE_TYPE xTaskWokenByPost = pdFALSE, xAnotherTaskWokenByPost = pdFALSE, xTaskWokenByTx = pdFALSE, xContinue = pdTRUE;
\r
426 /* NOTE: THIS IS NOT AN EFFICIENT ISR AS IT IS DESIGNED SOLELY TO TEST
\r
427 THE SCHEDULER FUNCTIONALITY. REAL APPLICATIONS SHOULD NOT USE THIS
\r
431 while( xContinue == pdTRUE )
\r
433 xContinue = pdFALSE;
\r
434 usStatusRegister = portINPUT_WORD( pxPort->usStatusReg );
\r
436 if( usStatusRegister & serRX_READY )
\r
438 cChar = ( portCHAR ) portINPUT_WORD( pxPort->usRxReg );
\r
439 xTaskWokenByPost = xQueueSendFromISR( pxPort->xRxedChars, &cChar, xTaskWokenByPost );
\r
441 /* Also release the semaphore - this does nothing interesting and is just a test. */
\r
442 xAnotherTaskWokenByPost = xSemaphoreGiveFromISR( pxPort->xTestSem, xAnotherTaskWokenByPost );
\r
444 /* We have performed an action this cycle - there may be other to perform. */
\r
445 xContinue = pdTRUE;
\r
448 if( pxPort->sTxInterruptOn && ( usStatusRegister & serTX_EMPTY ) )
\r
450 if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cChar, &xTaskWokenByTx ) == pdTRUE )
\r
452 portOUTPUT_WORD( pxPort->usTxReg, ( unsigned portSHORT ) cChar );
\r
454 /* We have performed an action this cycle - there may be others to perform. */
\r
455 xContinue = pdTRUE;
\r
459 /* Queue empty, nothing to send */
\r
460 vInterruptOff( pxPort, serTX_HOLD_EMPTY_INT );
\r
465 serRESET_PIC( pxPort->usIRQVector );
\r
467 /* If posting to the queue woke a task that was blocked on the queue we may
\r
468 want to switch to the woken task - depending on its priority relative to
\r
469 the task interrupted by this ISR. */
\r
470 if( xTaskWokenByPost || xAnotherTaskWokenByPost || xTaskWokenByTx)
\r