2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 + Call to the more efficient portSWITCH_CONTEXT() replaces the call to
\r
71 taskYIELD() in the ISR.
\r
75 + The semaphore task is not operational. This does nothing but check
\r
76 the semaphore from ISR functionality.
\r
77 + ISR modified slightly so only Rx or Tx is serviced per ISR - not both.
\r
79 Changes from V1.2.0:
\r
81 + Change so Tx uses a DMA channel, and Rx uses an interrupt.
\r
85 + The function xPortInitMinimal() has been renamed to
\r
86 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
87 to xSerialPortInit().
\r
91 + Reverted back to the non-DMA serial port driver, with a slightly modified
\r
92 ISR. This is a better test of the scheduler mechanisms.
\r
93 + A critical section is now used in vInterruptOn().
\r
94 + Flag sTxInterruptOn has been added to the port structure. This allows
\r
95 checking of the interrupt enable status without performing any IO.
\r
99 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
100 + Slightly more efficient vSerialSendString() implementation.
\r
101 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
104 #include <stdlib.h>
\r
106 #include "FreeRTOS.h"
\r
109 #include "portasm.h"
\r
110 #include "semphr.h"
\r
112 #define serMAX_PORTS ( ( unsigned short ) 2 )
\r
114 #define serPORT_0_INT_REG ( 0xff44 )
\r
115 #define serPORT_0_BAUD_REG ( 0xff88 )
\r
116 #define serPORT_0_RX_REG ( 0xff86 )
\r
117 #define serPORT_0_TX_REG ( 0xff84 )
\r
118 #define serPORT_0_STATUS_REG ( 0xff82 )
\r
119 #define serPORT_0_CTRL_REG ( 0xff80 )
\r
120 #define serPORT_0_IRQ ( 0x14 )
\r
122 #define serPORT_1_INT_REG ( 0xff42 )
\r
123 #define serPORT_1_BAUD_REG ( 0xff18 )
\r
124 #define serPORT_1_RX_REG ( 0xff16 )
\r
125 #define serPORT_1_TX_REG ( 0xff14 )
\r
126 #define serPORT_1_STATUS_REG ( 0xff12 )
\r
127 #define serPORT_1_CTRL_REG ( 0xff10 )
\r
128 #define serPORT_1_IRQ ( 0x11 )
\r
130 #define serTX_EMPTY ( ( unsigned short ) 0x40 )
\r
131 #define serRX_READY ( ( unsigned short ) 0x80 )
\r
133 #define serRESET_PIC( usEOI_TYPE ) portOUTPUT_WORD( ( unsigned short ) 0xff22, usEOI_TYPE )
\r
134 #define serTX_HOLD_EMPTY_INT ( ( unsigned short ) 0x100 )
\r
136 #define serENABLE_INTERRUPTS ( ( unsigned short ) 0x80 )
\r
137 #define serMODE ( ( unsigned short ) 0x01 )
\r
138 #define serENABLE_TX_MACHINES ( ( unsigned short ) 0x40 )
\r
139 #define serENABLE_RX_MACHINES ( ( unsigned short ) 0x20 )
\r
140 #define serINTERRUPT_MASK ( ( unsigned short ) 0x08 )
\r
141 #define serCLEAR_ALL_STATUS_BITS ( ( unsigned short ) 0x00 )
\r
142 #define serINTERRUPT_PRIORITY ( ( unsigned short ) 0x01 ) /*< Just below the scheduler priority. */
\r
144 #define serDONT_BLOCK ( ( portTickType ) 0 )
\r
202 /* Must be same order as eBaud definitions. */
\r
203 static const unsigned short usBaudRateDivisor[] =
\r
205 0, /* Not sure if the first 6 are correct. First cannot be used. */
\r
225 typedef struct xCOM_PORT
\r
227 /* Hardware parameters for this port. */
\r
228 short sTxInterruptOn;
\r
229 unsigned short usIntReg;
\r
230 unsigned short usBaudReg;
\r
231 unsigned short usRxReg;
\r
232 unsigned short usTxReg;
\r
233 unsigned short usStatusReg;
\r
234 unsigned short usCtrlReg;
\r
236 unsigned short usIRQVector;
\r
238 /* Queues used for communications with com test task. */
\r
239 xQueueHandle xRxedChars;
\r
240 xQueueHandle xCharsForTx;
\r
242 /* This semaphore does nothing useful except test a feature of the
\r
244 xSemaphoreHandle xTestSem;
\r
248 static xComPort xPorts[ serMAX_PORTS ] =
\r
250 { 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
251 { 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
254 typedef xComPort * xComPortHandle;
\r
256 /* These prototypes are repeated here so we don't have to include the serial header. This allows
\r
257 the xComPortHandle structure details to be private to this file. */
\r
258 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );
\r
259 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType xBlockTime );
\r
260 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime );
\r
261 void vSerialClose( xComPortHandle xPort );
\r
262 short sSerialWaitForSemaphore( xComPortHandle xPort );
\r
263 /*-----------------------------------------------------------*/
\r
265 static short xComPortISR( xComPort * const pxPort );
\r
267 #define vInterruptOn( pxPort, usInterrupt ) \
\r
269 unsigned short usIn; \
\r
271 portENTER_CRITICAL(); \
\r
273 if( pxPort->sTxInterruptOn == pdFALSE ) \
\r
275 usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
276 portOUTPUT_WORD( pxPort->usCtrlReg, usIn | usInterrupt ); \
\r
278 pxPort->sTxInterruptOn = pdTRUE; \
\r
281 portEXIT_CRITICAL(); \
\r
283 /*-----------------------------------------------------------*/
\r
285 #define vInterruptOff( pxPort, usInterrupt ) \
\r
287 unsigned short usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
288 if( usIn & usInterrupt ) \
\r
290 portOUTPUT_WORD( pxPort->usCtrlReg, usIn & ~usInterrupt); \
\r
291 pxPort->sTxInterruptOn = pdFALSE; \
\r
294 /*-----------------------------------------------------------*/
\r
297 /* Define an interrupt handler for each port */
\r
298 #define COM_IRQ_WRAPPER(N) \
\r
299 static void __interrupt COM_IRQ##N##_WRAPPER( void ) \
\r
301 if( xComPortISR( &( xPorts[##N##] ) ) ) \
\r
303 portSWITCH_CONTEXT(); \
\r
309 COM_IRQ_WRAPPER( 0 )
\r
310 COM_IRQ_WRAPPER( 1 )
\r
312 static pxISR xISRs[ serMAX_PORTS ] =
\r
318 /*-----------------------------------------------------------*/
\r
320 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
322 unsigned short usPort;
\r
323 xComPortHandle pxPort = NULL;
\r
325 /* BAUDDIV = ( Microprocessor Clock / Baud Rate ) / 16 */
\r
327 /* Only n, 8, 1 is supported so these parameters are not required for this
\r
329 ( void ) eWantedParity;
\r
330 ( void ) eWantedDataBits;
\r
331 ( void ) eWantedStopBits;
\r
333 /* Currently only n,8,1 is supported. */
\r
335 usPort = ( unsigned short ) ePort;
\r
337 if( usPort < serMAX_PORTS )
\r
339 pxPort = &( xPorts[ usPort ] );
\r
341 portENTER_CRITICAL();
\r
343 unsigned short usInWord;
\r
345 /* Create the queues used by the com test task. */
\r
346 pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
347 pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
349 /* Create the test semaphore. This does nothing useful except test a feature of the scheduler. */
\r
350 vSemaphoreCreateBinary( pxPort->xTestSem );
\r
352 /* There is no ISR here already to restore later. */
\r
353 _dos_setvect( ( short ) pxPort->usIRQVector, xISRs[ usPort ] );
\r
355 usInWord = portINPUT_WORD( pxPort->usIntReg );
\r
356 usInWord &= ~serINTERRUPT_MASK;
\r
357 usInWord |= serINTERRUPT_PRIORITY;
\r
358 portOUTPUT_WORD( pxPort->usIntReg, usInWord );
\r
360 portOUTPUT_WORD( pxPort->usBaudReg, usBaudRateDivisor[ eWantedBaud ] );
\r
361 portOUTPUT_WORD( pxPort->usCtrlReg, serENABLE_INTERRUPTS | serMODE | serENABLE_TX_MACHINES | serENABLE_RX_MACHINES );
\r
363 portOUTPUT_WORD( pxPort->usStatusReg, serCLEAR_ALL_STATUS_BITS );
\r
365 portEXIT_CRITICAL();
\r
369 } /*lint !e715 Some parameters are not used as only a subset of the serial port functionality is currently implemented. */
\r
370 /*-----------------------------------------------------------*/
\r
372 void vSerialPutString( xComPortHandle pxPort, const char * const pcString, unsigned short usStringLength )
\r
374 unsigned short usByte;
\r
377 pcNextChar = ( char * ) pcString;
\r
379 for( usByte = 0; usByte < usStringLength; usByte++ )
\r
381 xQueueSend( pxPort->xCharsForTx, pcNextChar, serDONT_BLOCK );
\r
385 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
387 /*-----------------------------------------------------------*/
\r
389 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType xBlockTime )
\r
391 /* Get the next character from the buffer, note that this routine is only
\r
392 called having checked that the is (at least) one to get */
\r
393 if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )
\r
402 /*-----------------------------------------------------------*/
\r
404 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime )
\r
406 if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
411 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
415 /*-----------------------------------------------------------*/
\r
417 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )
\r
419 const portTickType xBlockTime = ( portTickType ) 0xffff;
\r
421 /* This function does nothing interesting, but test the
\r
422 semaphore from ISR mechanism. */
\r
423 return xSemaphoreTake( xPort->xTestSem, xBlockTime );
\r
425 /*-----------------------------------------------------------*/
\r
427 void vSerialClose( xComPortHandle xPort )
\r
429 unsigned short usOutput;
\r
431 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
432 re-install the original ISR. */
\r
434 portENTER_CRITICAL();
\r
436 usOutput = portINPUT_WORD( xPort->usCtrlReg );
\r
438 usOutput &= ~serENABLE_INTERRUPTS;
\r
439 usOutput &= ~serENABLE_TX_MACHINES;
\r
440 usOutput &= ~serENABLE_RX_MACHINES;
\r
441 portOUTPUT_WORD( xPort->usCtrlReg, usOutput );
\r
443 usOutput = portINPUT_WORD( xPort->usIntReg );
\r
444 usOutput |= serINTERRUPT_MASK;
\r
445 portOUTPUT_WORD( xPort->usIntReg, usOutput );
\r
447 portEXIT_CRITICAL();
\r
449 /*-----------------------------------------------------------*/
\r
451 static portBASE_TYPE xComPortISR( xComPort * const pxPort )
\r
453 unsigned short usStatusRegister;
\r
455 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xContinue = pdTRUE;
\r
457 /* NOTE: THIS IS NOT AN EFFICIENT ISR AS IT IS DESIGNED SOLELY TO TEST
\r
458 THE SCHEDULER FUNCTIONALITY. REAL APPLICATIONS SHOULD NOT USE THIS
\r
462 while( xContinue == pdTRUE )
\r
464 xContinue = pdFALSE;
\r
465 usStatusRegister = portINPUT_WORD( pxPort->usStatusReg );
\r
467 if( usStatusRegister & serRX_READY )
\r
469 cChar = ( char ) portINPUT_WORD( pxPort->usRxReg );
\r
470 xQueueSendFromISR( pxPort->xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
472 /* Also release the semaphore - this does nothing interesting and is just a test. */
\r
473 xSemaphoreGiveFromISR( pxPort->xTestSem, &xHigherPriorityTaskWoken );
\r
475 /* We have performed an action this cycle - there may be other to perform. */
\r
476 xContinue = pdTRUE;
\r
479 if( pxPort->sTxInterruptOn && ( usStatusRegister & serTX_EMPTY ) )
\r
481 if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
483 portOUTPUT_WORD( pxPort->usTxReg, ( unsigned short ) cChar );
\r
485 /* We have performed an action this cycle - there may be others to perform. */
\r
486 xContinue = pdTRUE;
\r
490 /* Queue empty, nothing to send */
\r
491 vInterruptOff( pxPort, serTX_HOLD_EMPTY_INT );
\r
496 serRESET_PIC( pxPort->usIRQVector );
\r
498 /* If posting to the queue woke a task that was blocked on the queue we may
\r
499 want to switch to the woken task - depending on its priority relative to
\r
500 the task interrupted by this ISR. */
\r
501 return xHigherPriorityTaskWoken;
\r