2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
73 + Call to the more efficient portSWITCH_CONTEXT() replaces the call to
\r
74 taskYIELD() in the ISR.
\r
78 + The semaphore task is not operational. This does nothing but check
\r
79 the semaphore from ISR functionality.
\r
80 + ISR modified slightly so only Rx or Tx is serviced per ISR - not both.
\r
82 Changes from V1.2.0:
\r
84 + Change so Tx uses a DMA channel, and Rx uses an interrupt.
\r
88 + The function xPortInitMinimal() has been renamed to
\r
89 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
90 to xSerialPortInit().
\r
94 + Reverted back to the non-DMA serial port driver, with a slightly modified
\r
95 ISR. This is a better test of the scheduler mechanisms.
\r
96 + A critical section is now used in vInterruptOn().
\r
97 + Flag sTxInterruptOn has been added to the port structure. This allows
\r
98 checking of the interrupt enable status without performing any IO.
\r
100 Changes from V2.0.0
\r
102 + Use TickType_t in place of unsigned pdLONG for delay periods.
\r
103 + Slightly more efficient vSerialSendString() implementation.
\r
104 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
107 #include <stdlib.h>
\r
109 #include "FreeRTOS.h"
\r
112 #include "portasm.h"
\r
113 #include "semphr.h"
\r
115 #define serMAX_PORTS ( ( unsigned short ) 2 )
\r
117 #define serPORT_0_INT_REG ( 0xff44 )
\r
118 #define serPORT_0_BAUD_REG ( 0xff88 )
\r
119 #define serPORT_0_RX_REG ( 0xff86 )
\r
120 #define serPORT_0_TX_REG ( 0xff84 )
\r
121 #define serPORT_0_STATUS_REG ( 0xff82 )
\r
122 #define serPORT_0_CTRL_REG ( 0xff80 )
\r
123 #define serPORT_0_IRQ ( 0x14 )
\r
125 #define serPORT_1_INT_REG ( 0xff42 )
\r
126 #define serPORT_1_BAUD_REG ( 0xff18 )
\r
127 #define serPORT_1_RX_REG ( 0xff16 )
\r
128 #define serPORT_1_TX_REG ( 0xff14 )
\r
129 #define serPORT_1_STATUS_REG ( 0xff12 )
\r
130 #define serPORT_1_CTRL_REG ( 0xff10 )
\r
131 #define serPORT_1_IRQ ( 0x11 )
\r
133 #define serTX_EMPTY ( ( unsigned short ) 0x40 )
\r
134 #define serRX_READY ( ( unsigned short ) 0x80 )
\r
136 #define serRESET_PIC( usEOI_TYPE ) portOUTPUT_WORD( ( unsigned short ) 0xff22, usEOI_TYPE )
\r
137 #define serTX_HOLD_EMPTY_INT ( ( unsigned short ) 0x100 )
\r
139 #define serENABLE_INTERRUPTS ( ( unsigned short ) 0x80 )
\r
140 #define serMODE ( ( unsigned short ) 0x01 )
\r
141 #define serENABLE_TX_MACHINES ( ( unsigned short ) 0x40 )
\r
142 #define serENABLE_RX_MACHINES ( ( unsigned short ) 0x20 )
\r
143 #define serINTERRUPT_MASK ( ( unsigned short ) 0x08 )
\r
144 #define serCLEAR_ALL_STATUS_BITS ( ( unsigned short ) 0x00 )
\r
145 #define serINTERRUPT_PRIORITY ( ( unsigned short ) 0x01 ) /*< Just below the scheduler priority. */
\r
147 #define serDONT_BLOCK ( ( TickType_t ) 0 )
\r
205 /* Must be same order as eBaud definitions. */
\r
206 static const unsigned short usBaudRateDivisor[] =
\r
208 0, /* Not sure if the first 6 are correct. First cannot be used. */
\r
228 typedef struct xCOM_PORT
\r
230 /* Hardware parameters for this port. */
\r
231 short sTxInterruptOn;
\r
232 unsigned short usIntReg;
\r
233 unsigned short usBaudReg;
\r
234 unsigned short usRxReg;
\r
235 unsigned short usTxReg;
\r
236 unsigned short usStatusReg;
\r
237 unsigned short usCtrlReg;
\r
239 unsigned short usIRQVector;
\r
241 /* Queues used for communications with com test task. */
\r
242 QueueHandle_t xRxedChars;
\r
243 QueueHandle_t xCharsForTx;
\r
245 /* This semaphore does nothing useful except test a feature of the
\r
247 SemaphoreHandle_t xTestSem;
\r
251 static xComPort xPorts[ serMAX_PORTS ] =
\r
253 { 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
254 { 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
257 typedef xComPort * xComPortHandle;
\r
259 /* These prototypes are repeated here so we don't have to include the serial header. This allows
\r
260 the xComPortHandle structure details to be private to this file. */
\r
261 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );
\r
262 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, TickType_t xBlockTime );
\r
263 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, TickType_t xBlockTime );
\r
264 void vSerialClose( xComPortHandle xPort );
\r
265 short sSerialWaitForSemaphore( xComPortHandle xPort );
\r
266 /*-----------------------------------------------------------*/
\r
268 static short xComPortISR( xComPort * const pxPort );
\r
270 #define vInterruptOn( pxPort, usInterrupt ) \
\r
272 unsigned short usIn; \
\r
274 portENTER_CRITICAL(); \
\r
276 if( pxPort->sTxInterruptOn == pdFALSE ) \
\r
278 usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
279 portOUTPUT_WORD( pxPort->usCtrlReg, usIn | usInterrupt ); \
\r
281 pxPort->sTxInterruptOn = pdTRUE; \
\r
284 portEXIT_CRITICAL(); \
\r
286 /*-----------------------------------------------------------*/
\r
288 #define vInterruptOff( pxPort, usInterrupt ) \
\r
290 unsigned short usIn = portINPUT_WORD( pxPort->usCtrlReg ); \
\r
291 if( usIn & usInterrupt ) \
\r
293 portOUTPUT_WORD( pxPort->usCtrlReg, usIn & ~usInterrupt); \
\r
294 pxPort->sTxInterruptOn = pdFALSE; \
\r
297 /*-----------------------------------------------------------*/
\r
300 /* Define an interrupt handler for each port */
\r
301 #define COM_IRQ_WRAPPER(N) \
\r
302 static void __interrupt COM_IRQ##N##_WRAPPER( void ) \
\r
304 if( xComPortISR( &( xPorts[##N##] ) ) ) \
\r
306 portSWITCH_CONTEXT(); \
\r
312 COM_IRQ_WRAPPER( 0 )
\r
313 COM_IRQ_WRAPPER( 1 )
\r
315 static pxISR xISRs[ serMAX_PORTS ] =
\r
321 /*-----------------------------------------------------------*/
\r
323 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
325 unsigned short usPort;
\r
326 xComPortHandle pxPort = NULL;
\r
328 /* BAUDDIV = ( Microprocessor Clock / Baud Rate ) / 16 */
\r
330 /* Only n, 8, 1 is supported so these parameters are not required for this
\r
332 ( void ) eWantedParity;
\r
333 ( void ) eWantedDataBits;
\r
334 ( void ) eWantedStopBits;
\r
336 /* Currently only n,8,1 is supported. */
\r
338 usPort = ( unsigned short ) ePort;
\r
340 if( usPort < serMAX_PORTS )
\r
342 pxPort = &( xPorts[ usPort ] );
\r
344 portENTER_CRITICAL();
\r
346 unsigned short usInWord;
\r
348 /* Create the queues used by the com test task. */
\r
349 pxPort->xRxedChars = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
350 pxPort->xCharsForTx = xQueueCreate( uxBufferLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
352 /* Create the test semaphore. This does nothing useful except test a feature of the scheduler. */
\r
353 vSemaphoreCreateBinary( pxPort->xTestSem );
\r
355 /* There is no ISR here already to restore later. */
\r
356 _dos_setvect( ( short ) pxPort->usIRQVector, xISRs[ usPort ] );
\r
358 usInWord = portINPUT_WORD( pxPort->usIntReg );
\r
359 usInWord &= ~serINTERRUPT_MASK;
\r
360 usInWord |= serINTERRUPT_PRIORITY;
\r
361 portOUTPUT_WORD( pxPort->usIntReg, usInWord );
\r
363 portOUTPUT_WORD( pxPort->usBaudReg, usBaudRateDivisor[ eWantedBaud ] );
\r
364 portOUTPUT_WORD( pxPort->usCtrlReg, serENABLE_INTERRUPTS | serMODE | serENABLE_TX_MACHINES | serENABLE_RX_MACHINES );
\r
366 portOUTPUT_WORD( pxPort->usStatusReg, serCLEAR_ALL_STATUS_BITS );
\r
368 portEXIT_CRITICAL();
\r
372 } /*lint !e715 Some parameters are not used as only a subset of the serial port functionality is currently implemented. */
\r
373 /*-----------------------------------------------------------*/
\r
375 void vSerialPutString( xComPortHandle pxPort, const char * const pcString, unsigned short usStringLength )
\r
377 unsigned short usByte;
\r
380 pcNextChar = ( char * ) pcString;
\r
382 for( usByte = 0; usByte < usStringLength; usByte++ )
\r
384 xQueueSend( pxPort->xCharsForTx, pcNextChar, serDONT_BLOCK );
\r
388 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
390 /*-----------------------------------------------------------*/
\r
392 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, TickType_t xBlockTime )
\r
394 /* Get the next character from the buffer, note that this routine is only
\r
395 called having checked that the is (at least) one to get */
\r
396 if( xQueueReceive( pxPort->xRxedChars, pcRxedChar, xBlockTime ) )
\r
405 /*-----------------------------------------------------------*/
\r
407 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, TickType_t xBlockTime )
\r
409 if( xQueueSend( pxPort->xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
414 vInterruptOn( pxPort, serTX_HOLD_EMPTY_INT );
\r
418 /*-----------------------------------------------------------*/
\r
420 portBASE_TYPE xSerialWaitForSemaphore( xComPortHandle xPort )
\r
422 const TickType_t xBlockTime = ( TickType_t ) 0xffff;
\r
424 /* This function does nothing interesting, but test the
\r
425 semaphore from ISR mechanism. */
\r
426 return xSemaphoreTake( xPort->xTestSem, xBlockTime );
\r
428 /*-----------------------------------------------------------*/
\r
430 void vSerialClose( xComPortHandle xPort )
\r
432 unsigned short usOutput;
\r
434 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
435 re-install the original ISR. */
\r
437 portENTER_CRITICAL();
\r
439 usOutput = portINPUT_WORD( xPort->usCtrlReg );
\r
441 usOutput &= ~serENABLE_INTERRUPTS;
\r
442 usOutput &= ~serENABLE_TX_MACHINES;
\r
443 usOutput &= ~serENABLE_RX_MACHINES;
\r
444 portOUTPUT_WORD( xPort->usCtrlReg, usOutput );
\r
446 usOutput = portINPUT_WORD( xPort->usIntReg );
\r
447 usOutput |= serINTERRUPT_MASK;
\r
448 portOUTPUT_WORD( xPort->usIntReg, usOutput );
\r
450 portEXIT_CRITICAL();
\r
452 /*-----------------------------------------------------------*/
\r
454 static portBASE_TYPE xComPortISR( xComPort * const pxPort )
\r
456 unsigned short usStatusRegister;
\r
458 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xContinue = pdTRUE;
\r
460 /* NOTE: THIS IS NOT AN EFFICIENT ISR AS IT IS DESIGNED SOLELY TO TEST
\r
461 THE SCHEDULER FUNCTIONALITY. REAL APPLICATIONS SHOULD NOT USE THIS
\r
465 while( xContinue == pdTRUE )
\r
467 xContinue = pdFALSE;
\r
468 usStatusRegister = portINPUT_WORD( pxPort->usStatusReg );
\r
470 if( usStatusRegister & serRX_READY )
\r
472 cChar = ( char ) portINPUT_WORD( pxPort->usRxReg );
\r
473 xQueueSendFromISR( pxPort->xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
475 /* Also release the semaphore - this does nothing interesting and is just a test. */
\r
476 xSemaphoreGiveFromISR( pxPort->xTestSem, &xHigherPriorityTaskWoken );
\r
478 /* We have performed an action this cycle - there may be other to perform. */
\r
479 xContinue = pdTRUE;
\r
482 if( pxPort->sTxInterruptOn && ( usStatusRegister & serTX_EMPTY ) )
\r
484 if( xQueueReceiveFromISR( pxPort->xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
486 portOUTPUT_WORD( pxPort->usTxReg, ( unsigned short ) cChar );
\r
488 /* We have performed an action this cycle - there may be others to perform. */
\r
489 xContinue = pdTRUE;
\r
493 /* Queue empty, nothing to send */
\r
494 vInterruptOff( pxPort, serTX_HOLD_EMPTY_INT );
\r
499 serRESET_PIC( pxPort->usIRQVector );
\r
501 /* If posting to the queue woke a task that was blocked on the queue we may
\r
502 want to switch to the woken task - depending on its priority relative to
\r
503 the task interrupted by this ISR. */
\r
504 return xHigherPriorityTaskWoken;
\r