2 FreeRTOS.org V5.0.0 - 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
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
53 + Call to the more efficient portSWITCH_CONTEXT() replaces the call to
\r
54 taskYIELD() in the ISR.
\r
58 + The semaphore task is not operational. This does nothing but check
\r
59 the semaphore from ISR functionality.
\r
60 + ISR modified slightly so only Rx or Tx is serviced per ISR - not both.
\r
62 Changes from V1.2.0:
\r
64 + Change so Tx uses a DMA channel, and Rx uses an interrupt.
\r
68 + The function xPortInitMinimal() has been renamed to
\r
69 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
70 to xSerialPortInit().
\r
74 + Reverted back to the non-DMA serial port driver, with a slightly modified
\r
75 ISR. This is a better test of the scheduler mechanisms.
\r
76 + A critical section is now used in vInterruptOn().
\r
77 + Flag sTxInterruptOn has been added to the port structure. This allows
\r
78 checking of the interrupt enable status without performing any IO.
\r
82 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
83 + Slightly more efficient vSerialSendString() implementation.
\r
84 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
89 #include "FreeRTOS.h"
\r
92 #include "portasm.h"
\r
95 #define serMAX_PORTS ( ( unsigned portSHORT ) 2 )
\r
97 #define serPORT_0_INT_REG ( 0xff44 )
\r
98 #define serPORT_0_BAUD_REG ( 0xff88 )
\r
99 #define serPORT_0_RX_REG ( 0xff86 )
\r
100 #define serPORT_0_TX_REG ( 0xff84 )
\r
101 #define serPORT_0_STATUS_REG ( 0xff82 )
\r
102 #define serPORT_0_CTRL_REG ( 0xff80 )
\r
103 #define serPORT_0_IRQ ( 0x14 )
\r
105 #define serPORT_1_INT_REG ( 0xff42 )
\r
106 #define serPORT_1_BAUD_REG ( 0xff18 )
\r
107 #define serPORT_1_RX_REG ( 0xff16 )
\r
108 #define serPORT_1_TX_REG ( 0xff14 )
\r
109 #define serPORT_1_STATUS_REG ( 0xff12 )
\r
110 #define serPORT_1_CTRL_REG ( 0xff10 )
\r
111 #define serPORT_1_IRQ ( 0x11 )
\r
113 #define serTX_EMPTY ( ( unsigned portSHORT ) 0x40 )
\r
114 #define serRX_READY ( ( unsigned portSHORT ) 0x80 )
\r
116 #define serRESET_PIC( usEOI_TYPE ) portOUTPUT_WORD( ( unsigned portSHORT ) 0xff22, usEOI_TYPE )
\r
117 #define serTX_HOLD_EMPTY_INT ( ( unsigned portSHORT ) 0x100 )
\r
119 #define serENABLE_INTERRUPTS ( ( unsigned portSHORT ) 0x80 )
\r
120 #define serMODE ( ( unsigned portSHORT ) 0x01 )
\r
121 #define serENABLE_TX_MACHINES ( ( unsigned portSHORT ) 0x40 )
\r
122 #define serENABLE_RX_MACHINES ( ( unsigned portSHORT ) 0x20 )
\r
123 #define serINTERRUPT_MASK ( ( unsigned portSHORT ) 0x08 )
\r
124 #define serCLEAR_ALL_STATUS_BITS ( ( unsigned portSHORT ) 0x00 )
\r
125 #define serINTERRUPT_PRIORITY ( ( unsigned portSHORT ) 0x01 ) /*< Just below the scheduler priority. */
\r
127 #define serDONT_BLOCK ( ( portTickType ) 0 )
\r
185 /* Must be same order as eBaud definitions. */
\r
186 static const unsigned portSHORT usBaudRateDivisor[] =
\r
188 0, /* Not sure if the first 6 are correct. First cannot be used. */
\r
208 typedef struct xCOM_PORT
\r
210 /* Hardware parameters for this port. */
\r
211 portSHORT sTxInterruptOn;
\r
212 unsigned portSHORT usIntReg;
\r
213 unsigned portSHORT usBaudReg;
\r
214 unsigned portSHORT usRxReg;
\r
215 unsigned portSHORT usTxReg;
\r
216 unsigned portSHORT usStatusReg;
\r
217 unsigned portSHORT usCtrlReg;
\r
219 unsigned portSHORT usIRQVector;
\r
221 /* Queues used for communications with com test task. */
\r
222 xQueueHandle xRxedChars;
\r
223 xQueueHandle xCharsForTx;
\r
225 /* This semaphore does nothing useful except test a feature of the
\r
227 xSemaphoreHandle xTestSem;
\r
231 static xComPort xPorts[ serMAX_PORTS ] =
\r
233 { 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
234 { 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
237 typedef xComPort * xComPortHandle;
\r
239 /* These prototypes are repeated here so we don't have to include the serial header. This allows
\r
240 the xComPortHandle structure details to be private to this file. */
\r
241 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );
\r
242 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime );
\r
243 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime );
\r
244 void vSerialClose( xComPortHandle xPort );
\r
245 portSHORT sSerialWaitForSemaphore( xComPortHandle xPort );
\r
246 /*-----------------------------------------------------------*/
\r
248 static portSHORT xComPortISR( xComPort * const pxPort );
\r
250 #define vInterruptOn( pxPort, usInterrupt ) \
\r
252 unsigned portSHORT usIn; \
\r
254 portENTER_CRITICAL(); \
\r
256 if( pxPort->sTxInterruptOn == pdFALSE ) \
\r
258 usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
259 portOUTPUT_WORD( pxPort->usCtrlReg, usIn | usInterrupt ); \
\r
261 pxPort->sTxInterruptOn = pdTRUE; \
\r
264 portEXIT_CRITICAL(); \
\r
266 /*-----------------------------------------------------------*/
\r
268 #define vInterruptOff( pxPort, usInterrupt ) \
\r
270 unsigned portSHORT usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
271 if( usIn & usInterrupt ) \
\r
273 portOUTPUT_WORD( pxPort->usCtrlReg, usIn & ~usInterrupt); \
\r
274 pxPort->sTxInterruptOn = pdFALSE; \
\r
277 /*-----------------------------------------------------------*/
\r
280 /* Define an interrupt handler for each port */
\r
281 #define COM_IRQ_WRAPPER(N) \
\r
282 static void __interrupt COM_IRQ##N##_WRAPPER( void ) \
\r
284 if( xComPortISR( &( xPorts[##N##] ) ) ) \
\r
286 portSWITCH_CONTEXT(); \
\r
292 COM_IRQ_WRAPPER( 0 )
\r
293 COM_IRQ_WRAPPER( 1 )
\r
295 static pxISR xISRs[ serMAX_PORTS ] =
\r
301 /*-----------------------------------------------------------*/
\r
303 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
305 unsigned portSHORT usPort;
\r
306 xComPortHandle pxPort = NULL;
\r
308 /* BAUDDIV = ( Microprocessor Clock / Baud Rate ) / 16 */
\r
310 /* Only n, 8, 1 is supported so these parameters are not required for this
\r
312 ( void ) eWantedParity;
\r
313 ( void ) eWantedDataBits;
\r
314 ( void ) eWantedStopBits;
\r
316 /* Currently only n,8,1 is supported. */
\r
318 usPort = ( unsigned portSHORT ) ePort;
\r
320 if( usPort < serMAX_PORTS )
\r
322 pxPort = &( xPorts[ usPort ] );
\r
324 portENTER_CRITICAL();
\r
326 unsigned portSHORT usInWord;
\r
328 /* Create the queues used by the com test task. */
\r
329 pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
330 pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
332 /* Create the test semaphore. This does nothing useful except test a feature of the scheduler. */
\r
333 vSemaphoreCreateBinary( pxPort->xTestSem );
\r
335 /* There is no ISR here already to restore later. */
\r
336 _dos_setvect( ( portSHORT ) pxPort->usIRQVector, xISRs[ usPort ] );
\r
338 usInWord = portINPUT_WORD( pxPort->usIntReg );
\r
339 usInWord &= ~serINTERRUPT_MASK;
\r
340 usInWord |= serINTERRUPT_PRIORITY;
\r
341 portOUTPUT_WORD( pxPort->usIntReg, usInWord );
\r
343 portOUTPUT_WORD( pxPort->usBaudReg, usBaudRateDivisor[ eWantedBaud ] );
\r
344 portOUTPUT_WORD( pxPort->usCtrlReg, serENABLE_INTERRUPTS | serMODE | serENABLE_TX_MACHINES | serENABLE_RX_MACHINES );
\r
346 portOUTPUT_WORD( pxPort->usStatusReg, serCLEAR_ALL_STATUS_BITS );
\r
348 portEXIT_CRITICAL();
\r
352 } /*lint !e715 Some parameters are not used as only a subset of the serial port functionality is currently implemented. */
\r
353 /*-----------------------------------------------------------*/
\r
355 void vSerialPutString( xComPortHandle pxPort, const portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
357 unsigned portSHORT usByte;
\r
358 portCHAR *pcNextChar;
\r
360 pcNextChar = ( portCHAR * ) pcString;
\r
362 for( usByte = 0; usByte < usStringLength; usByte++ )
\r
364 xQueueSend( pxPort->xCharsForTx, pcNextChar, serDONT_BLOCK );
\r
368 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
370 /*-----------------------------------------------------------*/
\r
372 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )
\r
374 /* Get the next character from the buffer, note that this routine is only
\r
375 called having checked that the is (at least) one to get */
\r
376 if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )
\r
385 /*-----------------------------------------------------------*/
\r
387 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )
\r
389 if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
394 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
398 /*-----------------------------------------------------------*/
\r
400 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )
\r
402 const portTickType xBlockTime = ( portTickType ) 0xffff;
\r
404 /* This function does nothing interesting, but test the
\r
405 semaphore from ISR mechanism. */
\r
406 return xSemaphoreTake( xPort->xTestSem, xBlockTime );
\r
408 /*-----------------------------------------------------------*/
\r
410 void vSerialClose( xComPortHandle xPort )
\r
412 unsigned portSHORT usOutput;
\r
414 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
415 re-install the original ISR. */
\r
417 portENTER_CRITICAL();
\r
419 usOutput = portINPUT_WORD( xPort->usCtrlReg );
\r
421 usOutput &= ~serENABLE_INTERRUPTS;
\r
422 usOutput &= ~serENABLE_TX_MACHINES;
\r
423 usOutput &= ~serENABLE_RX_MACHINES;
\r
424 portOUTPUT_WORD( xPort->usCtrlReg, usOutput );
\r
426 usOutput = portINPUT_WORD( xPort->usIntReg );
\r
427 usOutput |= serINTERRUPT_MASK;
\r
428 portOUTPUT_WORD( xPort->usIntReg, usOutput );
\r
430 portEXIT_CRITICAL();
\r
432 /*-----------------------------------------------------------*/
\r
434 static portBASE_TYPE xComPortISR( xComPort * const pxPort )
\r
436 unsigned portSHORT usStatusRegister;
\r
438 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xContinue = pdTRUE;
\r
440 /* NOTE: THIS IS NOT AN EFFICIENT ISR AS IT IS DESIGNED SOLELY TO TEST
\r
441 THE SCHEDULER FUNCTIONALITY. REAL APPLICATIONS SHOULD NOT USE THIS
\r
445 while( xContinue == pdTRUE )
\r
447 xContinue = pdFALSE;
\r
448 usStatusRegister = portINPUT_WORD( pxPort->usStatusReg );
\r
450 if( usStatusRegister & serRX_READY )
\r
452 cChar = ( portCHAR ) portINPUT_WORD( pxPort->usRxReg );
\r
453 xQueueSendFromISR( pxPort->xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
455 /* Also release the semaphore - this does nothing interesting and is just a test. */
\r
456 xSemaphoreGiveFromISR( pxPort->xTestSem, &xHigherPriorityTaskWoken );
\r
458 /* We have performed an action this cycle - there may be other to perform. */
\r
459 xContinue = pdTRUE;
\r
462 if( pxPort->sTxInterruptOn && ( usStatusRegister & serTX_EMPTY ) )
\r
464 if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
466 portOUTPUT_WORD( pxPort->usTxReg, ( unsigned portSHORT ) cChar );
\r
468 /* We have performed an action this cycle - there may be others to perform. */
\r
469 xContinue = pdTRUE;
\r
473 /* Queue empty, nothing to send */
\r
474 vInterruptOff( pxPort, serTX_HOLD_EMPTY_INT );
\r
479 serRESET_PIC( pxPort->usIRQVector );
\r
481 /* If posting to the queue woke a task that was blocked on the queue we may
\r
482 want to switch to the woken task - depending on its priority relative to
\r
483 the task interrupted by this ISR. */
\r
484 return xHigherPriorityTaskWoken;
\r