2 FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\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 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
51 + Call to the more efficient portSWITCH_CONTEXT() replaces the call to
\r
52 taskYIELD() in the ISR.
\r
56 + The semaphore task is not operational. This does nothing but check
\r
57 the semaphore from ISR functionality.
\r
58 + ISR modified slightly so only Rx or Tx is serviced per ISR - not both.
\r
60 Changes from V1.2.0:
\r
62 + Change so Tx uses a DMA channel, and Rx uses an interrupt.
\r
66 + The function xPortInitMinimal() has been renamed to
\r
67 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
68 to xSerialPortInit().
\r
72 + Reverted back to the non-DMA serial port driver, with a slightly modified
\r
73 ISR. This is a better test of the scheduler mechanisms.
\r
74 + A critical section is now used in vInterruptOn().
\r
75 + Flag sTxInterruptOn has been added to the port structure. This allows
\r
76 checking of the interrupt enable status without performing any IO.
\r
80 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
81 + Slightly more efficient vSerialSendString() implementation.
\r
82 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
87 #include "FreeRTOS.h"
\r
90 #include "portasm.h"
\r
93 #define serMAX_PORTS ( ( unsigned portSHORT ) 2 )
\r
95 #define serPORT_0_INT_REG ( 0xff44 )
\r
96 #define serPORT_0_BAUD_REG ( 0xff88 )
\r
97 #define serPORT_0_RX_REG ( 0xff86 )
\r
98 #define serPORT_0_TX_REG ( 0xff84 )
\r
99 #define serPORT_0_STATUS_REG ( 0xff82 )
\r
100 #define serPORT_0_CTRL_REG ( 0xff80 )
\r
101 #define serPORT_0_IRQ ( 0x14 )
\r
103 #define serPORT_1_INT_REG ( 0xff42 )
\r
104 #define serPORT_1_BAUD_REG ( 0xff18 )
\r
105 #define serPORT_1_RX_REG ( 0xff16 )
\r
106 #define serPORT_1_TX_REG ( 0xff14 )
\r
107 #define serPORT_1_STATUS_REG ( 0xff12 )
\r
108 #define serPORT_1_CTRL_REG ( 0xff10 )
\r
109 #define serPORT_1_IRQ ( 0x11 )
\r
111 #define serTX_EMPTY ( ( unsigned portSHORT ) 0x40 )
\r
112 #define serRX_READY ( ( unsigned portSHORT ) 0x80 )
\r
114 #define serRESET_PIC( usEOI_TYPE ) portOUTPUT_WORD( ( unsigned portSHORT ) 0xff22, usEOI_TYPE )
\r
115 #define serTX_HOLD_EMPTY_INT ( ( unsigned portSHORT ) 0x100 )
\r
117 #define serENABLE_INTERRUPTS ( ( unsigned portSHORT ) 0x80 )
\r
118 #define serMODE ( ( unsigned portSHORT ) 0x01 )
\r
119 #define serENABLE_TX_MACHINES ( ( unsigned portSHORT ) 0x40 )
\r
120 #define serENABLE_RX_MACHINES ( ( unsigned portSHORT ) 0x20 )
\r
121 #define serINTERRUPT_MASK ( ( unsigned portSHORT ) 0x08 )
\r
122 #define serCLEAR_ALL_STATUS_BITS ( ( unsigned portSHORT ) 0x00 )
\r
123 #define serINTERRUPT_PRIORITY ( ( unsigned portSHORT ) 0x01 ) /*< Just below the scheduler priority. */
\r
125 #define serDONT_BLOCK ( ( portTickType ) 0 )
\r
183 /* Must be same order as eBaud definitions. */
\r
184 static const unsigned portSHORT usBaudRateDivisor[] =
\r
186 0, /* Not sure if the first 6 are correct. First cannot be used. */
\r
206 typedef struct xCOM_PORT
\r
208 /* Hardware parameters for this port. */
\r
209 portSHORT sTxInterruptOn;
\r
210 unsigned portSHORT usIntReg;
\r
211 unsigned portSHORT usBaudReg;
\r
212 unsigned portSHORT usRxReg;
\r
213 unsigned portSHORT usTxReg;
\r
214 unsigned portSHORT usStatusReg;
\r
215 unsigned portSHORT usCtrlReg;
\r
217 unsigned portSHORT usIRQVector;
\r
219 /* Queues used for communications with com test task. */
\r
220 xQueueHandle xRxedChars;
\r
221 xQueueHandle xCharsForTx;
\r
223 /* This semaphore does nothing useful except test a feature of the
\r
225 xSemaphoreHandle xTestSem;
\r
229 static xComPort xPorts[ serMAX_PORTS ] =
\r
231 { 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
232 { 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
235 typedef xComPort * xComPortHandle;
\r
237 /* These prototypes are repeated here so we don't have to include the serial header. This allows
\r
238 the xComPortHandle structure details to be private to this file. */
\r
239 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );
\r
240 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime );
\r
241 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime );
\r
242 void vSerialClose( xComPortHandle xPort );
\r
243 portSHORT sSerialWaitForSemaphore( xComPortHandle xPort );
\r
244 /*-----------------------------------------------------------*/
\r
246 static portSHORT xComPortISR( xComPort * const pxPort );
\r
248 #define vInterruptOn( pxPort, usInterrupt ) \
\r
250 unsigned portSHORT usIn; \
\r
252 portENTER_CRITICAL(); \
\r
254 if( pxPort->sTxInterruptOn == pdFALSE ) \
\r
256 usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
257 portOUTPUT_WORD( pxPort->usCtrlReg, usIn | usInterrupt ); \
\r
259 pxPort->sTxInterruptOn = pdTRUE; \
\r
262 portEXIT_CRITICAL(); \
\r
264 /*-----------------------------------------------------------*/
\r
266 #define vInterruptOff( pxPort, usInterrupt ) \
\r
268 unsigned portSHORT usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
269 if( usIn & usInterrupt ) \
\r
271 portOUTPUT_WORD( pxPort->usCtrlReg, usIn & ~usInterrupt); \
\r
272 pxPort->sTxInterruptOn = pdFALSE; \
\r
275 /*-----------------------------------------------------------*/
\r
278 /* Define an interrupt handler for each port */
\r
279 #define COM_IRQ_WRAPPER(N) \
\r
280 static void __interrupt COM_IRQ##N##_WRAPPER( void ) \
\r
282 if( xComPortISR( &( xPorts[##N##] ) ) ) \
\r
284 portSWITCH_CONTEXT(); \
\r
290 COM_IRQ_WRAPPER( 0 )
\r
291 COM_IRQ_WRAPPER( 1 )
\r
293 static pxISR xISRs[ serMAX_PORTS ] =
\r
299 /*-----------------------------------------------------------*/
\r
301 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
303 unsigned portSHORT usPort;
\r
304 xComPortHandle pxPort = NULL;
\r
306 /* BAUDDIV = ( Microprocessor Clock / Baud Rate ) / 16 */
\r
308 /* Only n, 8, 1 is supported so these parameters are not required for this
\r
310 ( void ) eWantedParity;
\r
311 ( void ) eWantedDataBits;
\r
312 ( void ) eWantedStopBits;
\r
314 /* Currently only n,8,1 is supported. */
\r
316 usPort = ( unsigned portSHORT ) ePort;
\r
318 if( usPort < serMAX_PORTS )
\r
320 pxPort = &( xPorts[ usPort ] );
\r
322 portENTER_CRITICAL();
\r
324 unsigned portSHORT usInWord;
\r
326 /* Create the queues used by the com test task. */
\r
327 pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
328 pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
330 /* Create the test semaphore. This does nothing useful except test a feature of the scheduler. */
\r
331 vSemaphoreCreateBinary( pxPort->xTestSem );
\r
333 /* There is no ISR here already to restore later. */
\r
334 _dos_setvect( ( portSHORT ) pxPort->usIRQVector, xISRs[ usPort ] );
\r
336 usInWord = portINPUT_WORD( pxPort->usIntReg );
\r
337 usInWord &= ~serINTERRUPT_MASK;
\r
338 usInWord |= serINTERRUPT_PRIORITY;
\r
339 portOUTPUT_WORD( pxPort->usIntReg, usInWord );
\r
341 portOUTPUT_WORD( pxPort->usBaudReg, usBaudRateDivisor[ eWantedBaud ] );
\r
342 portOUTPUT_WORD( pxPort->usCtrlReg, serENABLE_INTERRUPTS | serMODE | serENABLE_TX_MACHINES | serENABLE_RX_MACHINES );
\r
344 portOUTPUT_WORD( pxPort->usStatusReg, serCLEAR_ALL_STATUS_BITS );
\r
346 portEXIT_CRITICAL();
\r
350 } /*lint !e715 Some parameters are not used as only a subset of the serial port functionality is currently implemented. */
\r
351 /*-----------------------------------------------------------*/
\r
353 void vSerialPutString( xComPortHandle pxPort, const portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
355 unsigned portSHORT usByte;
\r
356 portCHAR *pcNextChar;
\r
358 pcNextChar = ( portCHAR * ) pcString;
\r
360 for( usByte = 0; usByte < usStringLength; usByte++ )
\r
362 xQueueSend( pxPort->xCharsForTx, pcNextChar, serDONT_BLOCK );
\r
366 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
368 /*-----------------------------------------------------------*/
\r
370 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )
\r
372 /* Get the next character from the buffer, note that this routine is only
\r
373 called having checked that the is (at least) one to get */
\r
374 if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )
\r
383 /*-----------------------------------------------------------*/
\r
385 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )
\r
387 if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
392 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
396 /*-----------------------------------------------------------*/
\r
398 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )
\r
400 const portTickType xBlockTime = ( portTickType ) 0xffff;
\r
402 /* This function does nothing interesting, but test the
\r
403 semaphore from ISR mechanism. */
\r
404 return xSemaphoreTake( xPort->xTestSem, xBlockTime );
\r
406 /*-----------------------------------------------------------*/
\r
408 void vSerialClose( xComPortHandle xPort )
\r
410 unsigned portSHORT usOutput;
\r
412 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
413 re-install the original ISR. */
\r
415 portENTER_CRITICAL();
\r
417 usOutput = portINPUT_WORD( xPort->usCtrlReg );
\r
419 usOutput &= ~serENABLE_INTERRUPTS;
\r
420 usOutput &= ~serENABLE_TX_MACHINES;
\r
421 usOutput &= ~serENABLE_RX_MACHINES;
\r
422 portOUTPUT_WORD( xPort->usCtrlReg, usOutput );
\r
424 usOutput = portINPUT_WORD( xPort->usIntReg );
\r
425 usOutput |= serINTERRUPT_MASK;
\r
426 portOUTPUT_WORD( xPort->usIntReg, usOutput );
\r
428 portEXIT_CRITICAL();
\r
430 /*-----------------------------------------------------------*/
\r
432 static portBASE_TYPE xComPortISR( xComPort * const pxPort )
\r
434 unsigned portSHORT usStatusRegister;
\r
436 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xContinue = pdTRUE;
\r
438 /* NOTE: THIS IS NOT AN EFFICIENT ISR AS IT IS DESIGNED SOLELY TO TEST
\r
439 THE SCHEDULER FUNCTIONALITY. REAL APPLICATIONS SHOULD NOT USE THIS
\r
443 while( xContinue == pdTRUE )
\r
445 xContinue = pdFALSE;
\r
446 usStatusRegister = portINPUT_WORD( pxPort->usStatusReg );
\r
448 if( usStatusRegister & serRX_READY )
\r
450 cChar = ( portCHAR ) portINPUT_WORD( pxPort->usRxReg );
\r
451 xQueueSendFromISR( pxPort->xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
453 /* Also release the semaphore - this does nothing interesting and is just a test. */
\r
454 xSemaphoreGiveFromISR( pxPort->xTestSem, &xHigherPriorityTaskWoken );
\r
456 /* We have performed an action this cycle - there may be other to perform. */
\r
457 xContinue = pdTRUE;
\r
460 if( pxPort->sTxInterruptOn && ( usStatusRegister & serTX_EMPTY ) )
\r
462 if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
464 portOUTPUT_WORD( pxPort->usTxReg, ( unsigned portSHORT ) cChar );
\r
466 /* We have performed an action this cycle - there may be others to perform. */
\r
467 xContinue = pdTRUE;
\r
471 /* Queue empty, nothing to send */
\r
472 vInterruptOff( pxPort, serTX_HOLD_EMPTY_INT );
\r
477 serRESET_PIC( pxPort->usIRQVector );
\r
479 /* If posting to the queue woke a task that was blocked on the queue we may
\r
480 want to switch to the woken task - depending on its priority relative to
\r
481 the task interrupted by this ISR. */
\r
482 return xHigherPriorityTaskWoken;
\r