2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the 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.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 #include <embedded.h>
\r
55 #include "FreeRTOS.h"
\r
56 #include "portasm.h"
\r
61 #define serMAX_PORTS ( ( unsigned portSHORT ) 2 )
\r
63 #define serPORT_0_INT_REG ( 0xff44 )
\r
64 #define serPORT_0_BAUD_REG ( 0xff88 )
\r
65 #define serPORT_0_RX_REG ( 0xff86 )
\r
66 #define serPORT_0_TX_REG ( 0xff84 )
\r
67 #define serPORT_0_STATUS_REG ( 0xff82 )
\r
68 #define serPORT_0_CTRL_REG ( 0xff80 )
\r
69 #define serPORT_0_IRQ ( 0x14 )
\r
71 #define serPORT_1_INT_REG ( 0xff42 )
\r
72 #define serPORT_1_BAUD_REG ( 0xff18 )
\r
73 #define serPORT_1_RX_REG ( 0xff16 )
\r
74 #define serPORT_1_TX_REG ( 0xff14 )
\r
75 #define serPORT_1_STATUS_REG ( 0xff12 )
\r
76 #define serPORT_1_CTRL_REG ( 0xff10 )
\r
77 #define serPORT_1_IRQ ( 0x11 )
\r
79 #define serTX_EMPTY ( ( unsigned portSHORT ) 0x40 )
\r
80 #define serRX_READY ( ( unsigned portSHORT ) 0x80 )
\r
82 #define serRESET_PIC( usEOI_TYPE ) portOUTPUT_WORD( ( unsigned portSHORT ) 0xff22, usEOI_TYPE )
\r
83 #define serTX_HOLD_EMPTY_INT ( ( unsigned portSHORT ) 0x100 )
\r
85 #define serENABLE_INTERRUPTS ( ( unsigned portSHORT ) 0x80 )
\r
86 #define serMODE ( ( unsigned portSHORT ) 0x01 )
\r
87 #define serENABLE_TX_MACHINES ( ( unsigned portSHORT ) 0x40 )
\r
88 #define serENABLE_RX_MACHINES ( ( unsigned portSHORT ) 0x20 )
\r
89 #define serINTERRUPT_MASK ( ( unsigned portSHORT ) 0x08 )
\r
90 #define serCLEAR_ALL_STATUS_BITS ( ( unsigned portSHORT ) 0x00 )
\r
91 #define serINTERRUPT_PRIORITY ( ( unsigned portSHORT ) 0x01 ) /*< Just below the scheduler priority. */
\r
93 #define serDONT_BLOCK ( ( portTickType ) 0 )
\r
151 typedef struct xCOM_PORT
\r
153 /* Hardware parameters for this port. */
\r
154 portSHORT sTxInterruptOn;
\r
155 unsigned portSHORT usIntReg;
\r
156 unsigned portSHORT usBaudReg;
\r
157 unsigned portSHORT usRxReg;
\r
158 unsigned portSHORT usTxReg;
\r
159 unsigned portSHORT usStatusReg;
\r
160 unsigned portSHORT usCtrlReg;
\r
162 unsigned portSHORT usIRQVector;
\r
164 /* Queues used for communications with com test task. */
\r
165 xQueueHandle xRxedChars;
\r
166 xQueueHandle xCharsForTx;
\r
168 /* This semaphore does nothing useful except test a feature of the
\r
170 xSemaphoreHandle xTestSem;
\r
174 static xComPort xPorts[ serMAX_PORTS ] =
\r
176 { 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
177 { 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
180 typedef xComPort * xComPortHandle;
\r
183 * Lookup the baud rate from the enum.
\r
185 static unsigned portLONG prvBaud( eBaud eWantedBaud );
\r
187 /* These prototypes are repeated here so we don't have to include the serial header. This allows
\r
188 the xComPortHandle structure details to be private to this file. */
\r
189 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );
\r
190 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime );
\r
191 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime );
\r
192 void vSerialClose( xComPortHandle xPort );
\r
193 portSHORT sSerialWaitForSemaphore( xComPortHandle xPort );
\r
194 /*-----------------------------------------------------------*/
\r
196 static portSHORT xComPortISR( xComPort * const pxPort );
\r
198 #define vInterruptOn( pxPort, usInterrupt ) \
\r
200 unsigned portSHORT usIn; \
\r
202 portENTER_CRITICAL(); \
\r
204 if( pxPort->sTxInterruptOn == pdFALSE ) \
\r
206 usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
207 portOUTPUT_WORD( pxPort->usCtrlReg, usIn | usInterrupt ); \
\r
209 pxPort->sTxInterruptOn = pdTRUE; \
\r
212 portEXIT_CRITICAL(); \
\r
214 /*-----------------------------------------------------------*/
\r
216 #define vInterruptOff( pxPort, usInterrupt ) \
\r
218 unsigned portSHORT usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
219 if( usIn & usInterrupt ) \
\r
221 portOUTPUT_WORD( pxPort->usCtrlReg, usIn & ~usInterrupt); \
\r
222 pxPort->sTxInterruptOn = pdFALSE; \
\r
225 /*-----------------------------------------------------------*/
\r
228 /* Define an interrupt handler for each port */
\r
229 #define COM_IRQ_WRAPPER(N) \
\r
230 static void __interrupt COM_IRQ##N##_WRAPPER( void ) \
\r
232 if( xComPortISR( &( xPorts[##N##] ) ) ) \
\r
234 portEND_SWITCHING_ISR(); \
\r
240 COM_IRQ_WRAPPER( 0 )
\r
241 COM_IRQ_WRAPPER( 1 )
\r
243 static pxISR xISRs[ serMAX_PORTS ] =
\r
249 /*-----------------------------------------------------------*/
\r
251 static unsigned portLONG prvBaud( eBaud eWantedBaud )
\r
253 switch( eWantedBaud )
\r
255 case ser50 : return 50UL;
\r
256 case ser75 : return 75UL;
\r
257 case ser110 : return 110UL;
\r
258 case ser134 : return 134UL;
\r
259 case ser150 : return 150UL;
\r
260 case ser200 : return 200UL;
\r
261 case ser300 : return 300UL;
\r
262 case ser600 : return 600UL;
\r
263 case ser1200 : return 1200UL;
\r
264 case ser1800 : return 1800UL;
\r
265 case ser2400 : return 2400UL;
\r
266 case ser4800 : return 4800UL;
\r
267 case ser19200 : return 19200UL;
\r
268 case ser38400 : return 38400UL;
\r
269 case ser57600 : return 57600UL;
\r
270 case ser115200 : return 115200UL;
\r
271 default : return 9600UL;
\r
275 /*-----------------------------------------------------------*/
\r
277 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
279 unsigned portSHORT usPort;
\r
280 xComPortHandle pxPort = NULL;
\r
281 unsigned portLONG ulBaudDiv;
\r
283 /* BAUDDIV = ( Microprocessor Clock / Baud Rate ) / 16 */
\r
284 ulBaudDiv = ( configCPU_CLOCK_HZ / prvBaud( eWantedBaud ) ) / 16UL;
\r
286 /* Only n, 8, 1 is supported so these parameters are not required for this
\r
288 ( void ) eWantedParity;
\r
289 ( void ) eWantedDataBits;
\r
290 ( void ) eWantedStopBits;
\r
292 /* Currently only n,8,1 is supported. */
\r
294 usPort = ( unsigned portSHORT ) ePort;
\r
296 if( usPort < serMAX_PORTS )
\r
298 pxPort = &( xPorts[ usPort ] );
\r
300 portENTER_CRITICAL();
\r
302 unsigned portSHORT usInWord;
\r
304 /* Create the queues used by the com test task. */
\r
305 pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
306 pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
308 /* Create the test semaphore. This does nothing useful except test a feature of the scheduler. */
\r
309 vSemaphoreCreateBinary( pxPort->xTestSem );
\r
311 /* There is no ISR here already to restore later. */
\r
312 setvect( ( portSHORT ) pxPort->usIRQVector, xISRs[ usPort ] );
\r
314 usInWord = portINPUT_WORD( pxPort->usIntReg );
\r
315 usInWord &= ~serINTERRUPT_MASK;
\r
316 usInWord |= serINTERRUPT_PRIORITY;
\r
317 portOUTPUT_WORD( pxPort->usIntReg, usInWord );
\r
319 portOUTPUT_WORD( pxPort->usBaudReg, ( unsigned portSHORT ) ulBaudDiv );
\r
320 portOUTPUT_WORD( pxPort->usCtrlReg, serENABLE_INTERRUPTS | serMODE | serENABLE_TX_MACHINES | serENABLE_RX_MACHINES );
\r
322 portOUTPUT_WORD( pxPort->usStatusReg, serCLEAR_ALL_STATUS_BITS );
\r
324 portEXIT_CRITICAL();
\r
328 } /*lint !e715 Some parameters are not used as only a subset of the serial port functionality is currently implemented. */
\r
329 /*-----------------------------------------------------------*/
\r
331 void vSerialPutString( xComPortHandle pxPort, const portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
333 unsigned portSHORT usByte;
\r
334 portCHAR *pcNextChar;
\r
336 pcNextChar = ( portCHAR * ) pcString;
\r
338 for( usByte = 0; usByte < usStringLength; usByte++ )
\r
340 xQueueSend( pxPort->xCharsForTx, pcNextChar, serDONT_BLOCK );
\r
344 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
346 /*-----------------------------------------------------------*/
\r
348 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )
\r
350 /* Get the next character from the buffer, note that this routine is only
\r
351 called having checked that the is (at least) one to get */
\r
352 if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )
\r
361 /*-----------------------------------------------------------*/
\r
363 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )
\r
365 if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
370 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
374 /*-----------------------------------------------------------*/
\r
376 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )
\r
378 const portTickType xBlockTime = ( portTickType ) 0xffff;
\r
380 /* This function does nothing interesting, but test the
\r
381 semaphore from ISR mechanism. */
\r
382 return xSemaphoreTake( xPort->xTestSem, xBlockTime );
\r
384 /*-----------------------------------------------------------*/
\r
386 void vSerialClose( xComPortHandle xPort )
\r
388 unsigned portSHORT usOutput;
\r
390 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
391 re-install the original ISR. */
\r
393 portENTER_CRITICAL();
\r
395 usOutput = portINPUT_WORD( xPort->usCtrlReg );
\r
397 usOutput &= ~serENABLE_INTERRUPTS;
\r
398 usOutput &= ~serENABLE_TX_MACHINES;
\r
399 usOutput &= ~serENABLE_RX_MACHINES;
\r
400 portOUTPUT_WORD( xPort->usCtrlReg, usOutput );
\r
402 usOutput = portINPUT_WORD( xPort->usIntReg );
\r
403 usOutput |= serINTERRUPT_MASK;
\r
404 portOUTPUT_WORD( xPort->usIntReg, usOutput );
\r
406 portEXIT_CRITICAL();
\r
408 /*-----------------------------------------------------------*/
\r
409 unsigned short usStatus;
\r
410 static portBASE_TYPE xComPortISR( xComPort * const pxPort )
\r
412 unsigned portSHORT usStatusRegister;
\r
414 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
416 /* NOTE: THIS IS NOT AN EFFICIENT ISR AS IT IS DESIGNED SOLELY TO TEST
\r
417 THE SCHEDULER FUNCTIONALITY. REAL APPLICATIONS SHOULD NOT USE THIS
\r
420 usStatusRegister = portINPUT_WORD( pxPort->usStatusReg );
\r
422 if( usStatusRegister & serRX_READY )
\r
424 cChar = ( portCHAR ) portINPUT_WORD( pxPort->usRxReg );
\r
425 xQueueSendFromISR( pxPort->xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
427 /* Also release the semaphore - this does nothing interesting and is just a test. */
\r
428 xSemaphoreGiveFromISR( pxPort->xTestSem, &xHigherPriorityTaskWoken );
\r
430 else if( pxPort->sTxInterruptOn && ( usStatusRegister & serTX_EMPTY ) )
\r
432 if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
434 portOUTPUT_WORD( pxPort->usTxReg, ( unsigned portSHORT ) cChar );
\r
438 /* Queue empty, nothing to send */
\r
439 vInterruptOff( pxPort, serTX_HOLD_EMPTY_INT );
\r
443 serRESET_PIC( pxPort->usIRQVector );
\r
445 /* If posting to the queue woke a task that was blocked on the queue we may
\r
446 want to switch to the woken task - depending on its priority relative to
\r
447 the task interrupted by this ISR. */
\r
448 return xHigherPriorityTaskWoken;
\r