2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
52 + Call to the more efficient portSWITCH_CONTEXT() replaces the call to
\r
53 taskYIELD() in the ISR.
\r
57 + The semaphore task is not operational. This does nothing but check
\r
58 the semaphore from ISR functionality.
\r
59 + ISR modified slightly so only Rx or Tx is serviced per ISR - not both.
\r
61 Changes from V1.2.0:
\r
63 + Change so Tx uses a DMA channel, and Rx uses an interrupt.
\r
67 + The function xPortInitMinimal() has been renamed to
\r
68 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
69 to xSerialPortInit().
\r
73 + Reverted back to the non-DMA serial port driver, with a slightly modified
\r
74 ISR. This is a better test of the scheduler mechanisms.
\r
75 + A critical section is now used in vInterruptOn().
\r
76 + Flag sTxInterruptOn has been added to the port structure. This allows
\r
77 checking of the interrupt enable status without performing any IO.
\r
81 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
82 + Slightly more efficient vSerialSendString() implementation.
\r
83 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
88 #include "FreeRTOS.h"
\r
91 #include "portasm.h"
\r
94 #define serMAX_PORTS ( ( unsigned short ) 2 )
\r
96 #define serPORT_0_INT_REG ( 0xff44 )
\r
97 #define serPORT_0_BAUD_REG ( 0xff88 )
\r
98 #define serPORT_0_RX_REG ( 0xff86 )
\r
99 #define serPORT_0_TX_REG ( 0xff84 )
\r
100 #define serPORT_0_STATUS_REG ( 0xff82 )
\r
101 #define serPORT_0_CTRL_REG ( 0xff80 )
\r
102 #define serPORT_0_IRQ ( 0x14 )
\r
104 #define serPORT_1_INT_REG ( 0xff42 )
\r
105 #define serPORT_1_BAUD_REG ( 0xff18 )
\r
106 #define serPORT_1_RX_REG ( 0xff16 )
\r
107 #define serPORT_1_TX_REG ( 0xff14 )
\r
108 #define serPORT_1_STATUS_REG ( 0xff12 )
\r
109 #define serPORT_1_CTRL_REG ( 0xff10 )
\r
110 #define serPORT_1_IRQ ( 0x11 )
\r
112 #define serTX_EMPTY ( ( unsigned short ) 0x40 )
\r
113 #define serRX_READY ( ( unsigned short ) 0x80 )
\r
115 #define serRESET_PIC( usEOI_TYPE ) portOUTPUT_WORD( ( unsigned short ) 0xff22, usEOI_TYPE )
\r
116 #define serTX_HOLD_EMPTY_INT ( ( unsigned short ) 0x100 )
\r
118 #define serENABLE_INTERRUPTS ( ( unsigned short ) 0x80 )
\r
119 #define serMODE ( ( unsigned short ) 0x01 )
\r
120 #define serENABLE_TX_MACHINES ( ( unsigned short ) 0x40 )
\r
121 #define serENABLE_RX_MACHINES ( ( unsigned short ) 0x20 )
\r
122 #define serINTERRUPT_MASK ( ( unsigned short ) 0x08 )
\r
123 #define serCLEAR_ALL_STATUS_BITS ( ( unsigned short ) 0x00 )
\r
124 #define serINTERRUPT_PRIORITY ( ( unsigned short ) 0x01 ) /*< Just below the scheduler priority. */
\r
126 #define serDONT_BLOCK ( ( portTickType ) 0 )
\r
184 /* Must be same order as eBaud definitions. */
\r
185 static const unsigned short usBaudRateDivisor[] =
\r
187 0, /* Not sure if the first 6 are correct. First cannot be used. */
\r
207 typedef struct xCOM_PORT
\r
209 /* Hardware parameters for this port. */
\r
210 short sTxInterruptOn;
\r
211 unsigned short usIntReg;
\r
212 unsigned short usBaudReg;
\r
213 unsigned short usRxReg;
\r
214 unsigned short usTxReg;
\r
215 unsigned short usStatusReg;
\r
216 unsigned short usCtrlReg;
\r
218 unsigned short usIRQVector;
\r
220 /* Queues used for communications with com test task. */
\r
221 xQueueHandle xRxedChars;
\r
222 xQueueHandle xCharsForTx;
\r
224 /* This semaphore does nothing useful except test a feature of the
\r
226 xSemaphoreHandle xTestSem;
\r
230 static xComPort xPorts[ serMAX_PORTS ] =
\r
232 { 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
233 { 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
236 typedef xComPort * xComPortHandle;
\r
238 /* These prototypes are repeated here so we don't have to include the serial header. This allows
\r
239 the xComPortHandle structure details to be private to this file. */
\r
240 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );
\r
241 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType xBlockTime );
\r
242 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime );
\r
243 void vSerialClose( xComPortHandle xPort );
\r
244 short sSerialWaitForSemaphore( xComPortHandle xPort );
\r
245 /*-----------------------------------------------------------*/
\r
247 static short xComPortISR( xComPort * const pxPort );
\r
249 #define vInterruptOn( pxPort, usInterrupt ) \
\r
251 unsigned short usIn; \
\r
253 portENTER_CRITICAL(); \
\r
255 if( pxPort->sTxInterruptOn == pdFALSE ) \
\r
257 usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
258 portOUTPUT_WORD( pxPort->usCtrlReg, usIn | usInterrupt ); \
\r
260 pxPort->sTxInterruptOn = pdTRUE; \
\r
263 portEXIT_CRITICAL(); \
\r
265 /*-----------------------------------------------------------*/
\r
267 #define vInterruptOff( pxPort, usInterrupt ) \
\r
269 unsigned short usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
270 if( usIn & usInterrupt ) \
\r
272 portOUTPUT_WORD( pxPort->usCtrlReg, usIn & ~usInterrupt); \
\r
273 pxPort->sTxInterruptOn = pdFALSE; \
\r
276 /*-----------------------------------------------------------*/
\r
279 /* Define an interrupt handler for each port */
\r
280 #define COM_IRQ_WRAPPER(N) \
\r
281 static void __interrupt COM_IRQ##N##_WRAPPER( void ) \
\r
283 if( xComPortISR( &( xPorts[##N##] ) ) ) \
\r
285 portSWITCH_CONTEXT(); \
\r
291 COM_IRQ_WRAPPER( 0 )
\r
292 COM_IRQ_WRAPPER( 1 )
\r
294 static pxISR xISRs[ serMAX_PORTS ] =
\r
300 /*-----------------------------------------------------------*/
\r
302 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
304 unsigned short usPort;
\r
305 xComPortHandle pxPort = NULL;
\r
307 /* BAUDDIV = ( Microprocessor Clock / Baud Rate ) / 16 */
\r
309 /* Only n, 8, 1 is supported so these parameters are not required for this
\r
311 ( void ) eWantedParity;
\r
312 ( void ) eWantedDataBits;
\r
313 ( void ) eWantedStopBits;
\r
315 /* Currently only n,8,1 is supported. */
\r
317 usPort = ( unsigned short ) ePort;
\r
319 if( usPort < serMAX_PORTS )
\r
321 pxPort = &( xPorts[ usPort ] );
\r
323 portENTER_CRITICAL();
\r
325 unsigned short usInWord;
\r
327 /* Create the queues used by the com test task. */
\r
328 pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
329 pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
331 /* Create the test semaphore. This does nothing useful except test a feature of the scheduler. */
\r
332 vSemaphoreCreateBinary( pxPort->xTestSem );
\r
334 /* There is no ISR here already to restore later. */
\r
335 _dos_setvect( ( short ) pxPort->usIRQVector, xISRs[ usPort ] );
\r
337 usInWord = portINPUT_WORD( pxPort->usIntReg );
\r
338 usInWord &= ~serINTERRUPT_MASK;
\r
339 usInWord |= serINTERRUPT_PRIORITY;
\r
340 portOUTPUT_WORD( pxPort->usIntReg, usInWord );
\r
342 portOUTPUT_WORD( pxPort->usBaudReg, usBaudRateDivisor[ eWantedBaud ] );
\r
343 portOUTPUT_WORD( pxPort->usCtrlReg, serENABLE_INTERRUPTS | serMODE | serENABLE_TX_MACHINES | serENABLE_RX_MACHINES );
\r
345 portOUTPUT_WORD( pxPort->usStatusReg, serCLEAR_ALL_STATUS_BITS );
\r
347 portEXIT_CRITICAL();
\r
351 } /*lint !e715 Some parameters are not used as only a subset of the serial port functionality is currently implemented. */
\r
352 /*-----------------------------------------------------------*/
\r
354 void vSerialPutString( xComPortHandle pxPort, const char * const pcString, unsigned short usStringLength )
\r
356 unsigned short usByte;
\r
359 pcNextChar = ( char * ) pcString;
\r
361 for( usByte = 0; usByte < usStringLength; usByte++ )
\r
363 xQueueSend( pxPort->xCharsForTx, pcNextChar, serDONT_BLOCK );
\r
367 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
369 /*-----------------------------------------------------------*/
\r
371 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType xBlockTime )
\r
373 /* Get the next character from the buffer, note that this routine is only
\r
374 called having checked that the is (at least) one to get */
\r
375 if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )
\r
384 /*-----------------------------------------------------------*/
\r
386 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime )
\r
388 if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
393 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
397 /*-----------------------------------------------------------*/
\r
399 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )
\r
401 const portTickType xBlockTime = ( portTickType ) 0xffff;
\r
403 /* This function does nothing interesting, but test the
\r
404 semaphore from ISR mechanism. */
\r
405 return xSemaphoreTake( xPort->xTestSem, xBlockTime );
\r
407 /*-----------------------------------------------------------*/
\r
409 void vSerialClose( xComPortHandle xPort )
\r
411 unsigned short usOutput;
\r
413 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
414 re-install the original ISR. */
\r
416 portENTER_CRITICAL();
\r
418 usOutput = portINPUT_WORD( xPort->usCtrlReg );
\r
420 usOutput &= ~serENABLE_INTERRUPTS;
\r
421 usOutput &= ~serENABLE_TX_MACHINES;
\r
422 usOutput &= ~serENABLE_RX_MACHINES;
\r
423 portOUTPUT_WORD( xPort->usCtrlReg, usOutput );
\r
425 usOutput = portINPUT_WORD( xPort->usIntReg );
\r
426 usOutput |= serINTERRUPT_MASK;
\r
427 portOUTPUT_WORD( xPort->usIntReg, usOutput );
\r
429 portEXIT_CRITICAL();
\r
431 /*-----------------------------------------------------------*/
\r
433 static portBASE_TYPE xComPortISR( xComPort * const pxPort )
\r
435 unsigned short usStatusRegister;
\r
437 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xContinue = pdTRUE;
\r
439 /* NOTE: THIS IS NOT AN EFFICIENT ISR AS IT IS DESIGNED SOLELY TO TEST
\r
440 THE SCHEDULER FUNCTIONALITY. REAL APPLICATIONS SHOULD NOT USE THIS
\r
444 while( xContinue == pdTRUE )
\r
446 xContinue = pdFALSE;
\r
447 usStatusRegister = portINPUT_WORD( pxPort->usStatusReg );
\r
449 if( usStatusRegister & serRX_READY )
\r
451 cChar = ( char ) portINPUT_WORD( pxPort->usRxReg );
\r
452 xQueueSendFromISR( pxPort->xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
454 /* Also release the semaphore - this does nothing interesting and is just a test. */
\r
455 xSemaphoreGiveFromISR( pxPort->xTestSem, &xHigherPriorityTaskWoken );
\r
457 /* We have performed an action this cycle - there may be other to perform. */
\r
458 xContinue = pdTRUE;
\r
461 if( pxPort->sTxInterruptOn && ( usStatusRegister & serTX_EMPTY ) )
\r
463 if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
465 portOUTPUT_WORD( pxPort->usTxReg, ( unsigned short ) cChar );
\r
467 /* We have performed an action this cycle - there may be others to perform. */
\r
468 xContinue = pdTRUE;
\r
472 /* Queue empty, nothing to send */
\r
473 vInterruptOff( pxPort, serTX_HOLD_EMPTY_INT );
\r
478 serRESET_PIC( pxPort->usIRQVector );
\r
480 /* If posting to the queue woke a task that was blocked on the queue we may
\r
481 want to switch to the woken task - depending on its priority relative to
\r
482 the task interrupted by this ISR. */
\r
483 return xHigherPriorityTaskWoken;
\r