2 FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 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
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
46 + Call to the more efficient portSWITCH_CONTEXT() replaces the call to
\r
47 taskYIELD() in the ISR.
\r
51 + The semaphore task is not operational. This does nothing but check
\r
52 the semaphore from ISR functionality.
\r
53 + ISR modified slightly so only Rx or Tx is serviced per ISR - not both.
\r
55 Changes from V1.2.0:
\r
57 + Change so Tx uses a DMA channel, and Rx uses an interrupt.
\r
61 + The function xPortInitMinimal() has been renamed to
\r
62 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
63 to xSerialPortInit().
\r
67 + Reverted back to the non-DMA serial port driver, with a slightly modified
\r
68 ISR. This is a better test of the scheduler mechanisms.
\r
69 + A critical section is now used in vInterruptOn().
\r
70 + Flag sTxInterruptOn has been added to the port structure. This allows
\r
71 checking of the interrupt enable status without performing any IO.
\r
75 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
76 + Slightly more efficient vSerialSendString() implementation.
\r
77 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
82 #include "FreeRTOS.h"
\r
85 #include "portasm.h"
\r
88 #define serMAX_PORTS ( ( unsigned portSHORT ) 2 )
\r
90 #define serPORT_0_INT_REG ( 0xff44 )
\r
91 #define serPORT_0_BAUD_REG ( 0xff88 )
\r
92 #define serPORT_0_RX_REG ( 0xff86 )
\r
93 #define serPORT_0_TX_REG ( 0xff84 )
\r
94 #define serPORT_0_STATUS_REG ( 0xff82 )
\r
95 #define serPORT_0_CTRL_REG ( 0xff80 )
\r
96 #define serPORT_0_IRQ ( 0x14 )
\r
98 #define serPORT_1_INT_REG ( 0xff42 )
\r
99 #define serPORT_1_BAUD_REG ( 0xff18 )
\r
100 #define serPORT_1_RX_REG ( 0xff16 )
\r
101 #define serPORT_1_TX_REG ( 0xff14 )
\r
102 #define serPORT_1_STATUS_REG ( 0xff12 )
\r
103 #define serPORT_1_CTRL_REG ( 0xff10 )
\r
104 #define serPORT_1_IRQ ( 0x11 )
\r
106 #define serTX_EMPTY ( ( unsigned portSHORT ) 0x40 )
\r
107 #define serRX_READY ( ( unsigned portSHORT ) 0x80 )
\r
109 #define serRESET_PIC( usEOI_TYPE ) portOUTPUT_WORD( ( unsigned portSHORT ) 0xff22, usEOI_TYPE )
\r
110 #define serTX_HOLD_EMPTY_INT ( ( unsigned portSHORT ) 0x100 )
\r
112 #define serENABLE_INTERRUPTS ( ( unsigned portSHORT ) 0x80 )
\r
113 #define serMODE ( ( unsigned portSHORT ) 0x01 )
\r
114 #define serENABLE_TX_MACHINES ( ( unsigned portSHORT ) 0x40 )
\r
115 #define serENABLE_RX_MACHINES ( ( unsigned portSHORT ) 0x20 )
\r
116 #define serINTERRUPT_MASK ( ( unsigned portSHORT ) 0x08 )
\r
117 #define serCLEAR_ALL_STATUS_BITS ( ( unsigned portSHORT ) 0x00 )
\r
118 #define serINTERRUPT_PRIORITY ( ( unsigned portSHORT ) 0x01 ) /*< Just below the scheduler priority. */
\r
120 #define serDONT_BLOCK ( ( portTickType ) 0 )
\r
178 /* Must be same order as eBaud definitions. */
\r
179 static const unsigned portSHORT usBaudRateDivisor[] =
\r
181 0, /* Not sure if the first 6 are correct. First cannot be used. */
\r
201 typedef struct xCOM_PORT
\r
203 /* Hardware parameters for this port. */
\r
204 portSHORT sTxInterruptOn;
\r
205 unsigned portSHORT usIntReg;
\r
206 unsigned portSHORT usBaudReg;
\r
207 unsigned portSHORT usRxReg;
\r
208 unsigned portSHORT usTxReg;
\r
209 unsigned portSHORT usStatusReg;
\r
210 unsigned portSHORT usCtrlReg;
\r
212 unsigned portSHORT usIRQVector;
\r
214 /* Queues used for communications with com test task. */
\r
215 xQueueHandle xRxedChars;
\r
216 xQueueHandle xCharsForTx;
\r
218 /* This semaphore does nothing useful except test a feature of the
\r
220 xSemaphoreHandle xTestSem;
\r
224 static xComPort xPorts[ serMAX_PORTS ] =
\r
226 { 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
227 { 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
230 typedef xComPort * xComPortHandle;
\r
232 /* These prototypes are repeated here so we don't have to include the serial header. This allows
\r
233 the xComPortHandle structure details to be private to this file. */
\r
234 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );
\r
235 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime );
\r
236 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime );
\r
237 void vSerialClose( xComPortHandle xPort );
\r
238 portSHORT sSerialWaitForSemaphore( xComPortHandle xPort );
\r
239 /*-----------------------------------------------------------*/
\r
241 static portSHORT xComPortISR( xComPort * const pxPort );
\r
243 #define vInterruptOn( pxPort, usInterrupt ) \
\r
245 unsigned portSHORT usIn; \
\r
247 portENTER_CRITICAL(); \
\r
249 if( pxPort->sTxInterruptOn == pdFALSE ) \
\r
251 usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
252 portOUTPUT_WORD( pxPort->usCtrlReg, usIn | usInterrupt ); \
\r
254 pxPort->sTxInterruptOn = pdTRUE; \
\r
257 portEXIT_CRITICAL(); \
\r
259 /*-----------------------------------------------------------*/
\r
261 #define vInterruptOff( pxPort, usInterrupt ) \
\r
263 unsigned portSHORT usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
264 if( usIn & usInterrupt ) \
\r
266 portOUTPUT_WORD( pxPort->usCtrlReg, usIn & ~usInterrupt); \
\r
267 pxPort->sTxInterruptOn = pdFALSE; \
\r
270 /*-----------------------------------------------------------*/
\r
273 /* Define an interrupt handler for each port */
\r
274 #define COM_IRQ_WRAPPER(N) \
\r
275 static void __interrupt COM_IRQ##N##_WRAPPER( void ) \
\r
277 if( xComPortISR( &( xPorts[##N##] ) ) ) \
\r
279 portSWITCH_CONTEXT(); \
\r
285 COM_IRQ_WRAPPER( 0 )
\r
286 COM_IRQ_WRAPPER( 1 )
\r
288 static pxISR xISRs[ serMAX_PORTS ] =
\r
294 /*-----------------------------------------------------------*/
\r
296 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
298 unsigned portSHORT usPort;
\r
299 xComPortHandle pxPort = NULL;
\r
301 /* BAUDDIV = ( Microprocessor Clock / Baud Rate ) / 16 */
\r
303 /* Only n, 8, 1 is supported so these parameters are not required for this
\r
305 ( void ) eWantedParity;
\r
306 ( void ) eWantedDataBits;
\r
307 ( void ) eWantedStopBits;
\r
309 /* Currently only n,8,1 is supported. */
\r
311 usPort = ( unsigned portSHORT ) ePort;
\r
313 if( usPort < serMAX_PORTS )
\r
315 pxPort = &( xPorts[ usPort ] );
\r
317 portENTER_CRITICAL();
\r
319 unsigned portSHORT usInWord;
\r
321 /* Create the queues used by the com test task. */
\r
322 pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
323 pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
325 /* Create the test semaphore. This does nothing useful except test a feature of the scheduler. */
\r
326 vSemaphoreCreateBinary( pxPort->xTestSem );
\r
328 /* There is no ISR here already to restore later. */
\r
329 _dos_setvect( ( portSHORT ) pxPort->usIRQVector, xISRs[ usPort ] );
\r
331 usInWord = portINPUT_WORD( pxPort->usIntReg );
\r
332 usInWord &= ~serINTERRUPT_MASK;
\r
333 usInWord |= serINTERRUPT_PRIORITY;
\r
334 portOUTPUT_WORD( pxPort->usIntReg, usInWord );
\r
336 portOUTPUT_WORD( pxPort->usBaudReg, usBaudRateDivisor[ eWantedBaud ] );
\r
337 portOUTPUT_WORD( pxPort->usCtrlReg, serENABLE_INTERRUPTS | serMODE | serENABLE_TX_MACHINES | serENABLE_RX_MACHINES );
\r
339 portOUTPUT_WORD( pxPort->usStatusReg, serCLEAR_ALL_STATUS_BITS );
\r
341 portEXIT_CRITICAL();
\r
345 } /*lint !e715 Some parameters are not used as only a subset of the serial port functionality is currently implemented. */
\r
346 /*-----------------------------------------------------------*/
\r
348 void vSerialPutString( xComPortHandle pxPort, const portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
350 unsigned portSHORT usByte;
\r
351 portCHAR *pcNextChar;
\r
353 pcNextChar = ( portCHAR * ) pcString;
\r
355 for( usByte = 0; usByte < usStringLength; usByte++ )
\r
357 xQueueSend( pxPort->xCharsForTx, pcNextChar, serDONT_BLOCK );
\r
361 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
363 /*-----------------------------------------------------------*/
\r
365 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )
\r
367 /* Get the next character from the buffer, note that this routine is only
\r
368 called having checked that the is (at least) one to get */
\r
369 if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )
\r
378 /*-----------------------------------------------------------*/
\r
380 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )
\r
382 if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
387 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
391 /*-----------------------------------------------------------*/
\r
393 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )
\r
395 const portTickType xBlockTime = ( portTickType ) 0xffff;
\r
397 /* This function does nothing interesting, but test the
\r
398 semaphore from ISR mechanism. */
\r
399 return xSemaphoreTake( xPort->xTestSem, xBlockTime );
\r
401 /*-----------------------------------------------------------*/
\r
403 void vSerialClose( xComPortHandle xPort )
\r
405 unsigned portSHORT usOutput;
\r
407 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
408 re-install the original ISR. */
\r
410 portENTER_CRITICAL();
\r
412 usOutput = portINPUT_WORD( xPort->usCtrlReg );
\r
414 usOutput &= ~serENABLE_INTERRUPTS;
\r
415 usOutput &= ~serENABLE_TX_MACHINES;
\r
416 usOutput &= ~serENABLE_RX_MACHINES;
\r
417 portOUTPUT_WORD( xPort->usCtrlReg, usOutput );
\r
419 usOutput = portINPUT_WORD( xPort->usIntReg );
\r
420 usOutput |= serINTERRUPT_MASK;
\r
421 portOUTPUT_WORD( xPort->usIntReg, usOutput );
\r
423 portEXIT_CRITICAL();
\r
425 /*-----------------------------------------------------------*/
\r
427 static portBASE_TYPE xComPortISR( xComPort * const pxPort )
\r
429 unsigned portSHORT usStatusRegister;
\r
431 portBASE_TYPE xTaskWokenByPost = pdFALSE, xAnotherTaskWokenByPost = pdFALSE, xTaskWokenByTx = pdFALSE, xContinue = pdTRUE;
\r
433 /* NOTE: THIS IS NOT AN EFFICIENT ISR AS IT IS DESIGNED SOLELY TO TEST
\r
434 THE SCHEDULER FUNCTIONALITY. REAL APPLICATIONS SHOULD NOT USE THIS
\r
438 while( xContinue == pdTRUE )
\r
440 xContinue = pdFALSE;
\r
441 usStatusRegister = portINPUT_WORD( pxPort->usStatusReg );
\r
443 if( usStatusRegister & serRX_READY )
\r
445 cChar = ( portCHAR ) portINPUT_WORD( pxPort->usRxReg );
\r
446 xTaskWokenByPost = xQueueSendFromISR( pxPort->xRxedChars, &cChar, xTaskWokenByPost );
\r
448 /* Also release the semaphore - this does nothing interesting and is just a test. */
\r
449 xAnotherTaskWokenByPost = xSemaphoreGiveFromISR( pxPort->xTestSem, xAnotherTaskWokenByPost );
\r
451 /* We have performed an action this cycle - there may be other to perform. */
\r
452 xContinue = pdTRUE;
\r
455 if( pxPort->sTxInterruptOn && ( usStatusRegister & serTX_EMPTY ) )
\r
457 if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cChar, &xTaskWokenByTx ) == pdTRUE )
\r
459 portOUTPUT_WORD( pxPort->usTxReg, ( unsigned portSHORT ) cChar );
\r
461 /* We have performed an action this cycle - there may be others to perform. */
\r
462 xContinue = pdTRUE;
\r
466 /* Queue empty, nothing to send */
\r
467 vInterruptOff( pxPort, serTX_HOLD_EMPTY_INT );
\r
472 serRESET_PIC( pxPort->usIRQVector );
\r
474 /* If posting to the queue woke a task that was blocked on the queue we may
\r
475 want to switch to the woken task - depending on its priority relative to
\r
476 the task interrupted by this ISR. */
\r
477 if( xTaskWokenByPost || xAnotherTaskWokenByPost || xTaskWokenByTx)
\r