2 FreeRTOS V7.1.1 - 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
69 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
71 Note this driver is used to test the FreeRTOS port. It is NOT intended to
\r
72 be an example of an efficient implementation!
\r
75 /* Standard includes. */
\r
78 /* Scheduler includes. */
\r
79 #include "FreeRTOS.h"
\r
83 /* Demo application includes. */
\r
86 /*-----------------------------------------------------------*/
\r
88 /* Constants to setup and access the UART. */
\r
89 #define serDLAB ( ( unsigned char ) 0x80 )
\r
90 #define serENABLE_INTERRUPTS ( ( unsigned char ) 0x03 )
\r
91 #define serNO_PARITY ( ( unsigned char ) 0x00 )
\r
92 #define ser1_STOP_BIT ( ( unsigned char ) 0x00 )
\r
93 #define ser8_BIT_CHARS ( ( unsigned char ) 0x03 )
\r
94 #define serFIFO_ON ( ( unsigned char ) 0x01 )
\r
95 #define serCLEAR_FIFO ( ( unsigned char ) 0x06 )
\r
96 #define serWANTED_CLOCK_SCALING ( ( unsigned long ) 16 )
\r
98 /* Constants to setup and access the VIC. */
\r
99 #define serU1VIC_CHANNEL ( ( unsigned long ) 0x0007 )
\r
100 #define serU1VIC_CHANNEL_BIT ( ( unsigned long ) 0x0080 )
\r
101 #define serU1VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
104 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
105 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
106 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
108 /* Constant to access the VIC. */
\r
109 #define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
\r
111 /* Constants to determine the ISR source. */
\r
112 #define serSOURCE_THRE ( ( unsigned char ) 0x02 )
\r
113 #define serSOURCE_RX_TIMEOUT ( ( unsigned char ) 0x0c )
\r
114 #define serSOURCE_ERROR ( ( unsigned char ) 0x06 )
\r
115 #define serSOURCE_RX ( ( unsigned char ) 0x04 )
\r
116 #define serINTERRUPT_SOURCE_MASK ( ( unsigned char ) 0x0f )
\r
117 #define serINTERRUPT_IS_PENDING ( ( unsigned char ) 0x01 )
\r
119 /*-----------------------------------------------------------*/
\r
122 * The asm wrapper for the interrupt service routine.
\r
124 extern void vUART_ISREntry( void );
\r
127 * The C function called from the asm wrapper.
\r
129 void vUART_ISRHandler( void );
\r
131 /*-----------------------------------------------------------*/
\r
133 /* Queues used to hold received characters, and characters waiting to be
\r
135 static xQueueHandle xRxedChars;
\r
136 static xQueueHandle xCharsForTx;
\r
138 /* Communication flag between the interrupt service routine and serial API. */
\r
139 static volatile long lTHREEmpty;
\r
141 /*-----------------------------------------------------------*/
\r
143 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
145 unsigned long ulDivisor, ulWantedClock;
\r
146 xComPortHandle xReturn = serHANDLE;
\r
148 /* Create the queues used to hold Rx and Tx characters. */
\r
149 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
150 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
152 /* Initialise the THRE empty flag. */
\r
153 lTHREEmpty = pdTRUE;
\r
156 ( xRxedChars != serINVALID_QUEUE ) &&
\r
157 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
158 ( ulWantedBaud != ( unsigned long ) 0 )
\r
161 portENTER_CRITICAL()
\r
163 /* Setup the baud rate: Calculate the divisor value. */
\r
164 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
\r
165 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
\r
167 /* Set the DLAB bit so we can access the divisor. */
\r
170 /* Setup the divisor. */
\r
171 U1DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
173 U1DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
175 /* Turn on the FIFO's and clear the buffers. */
\r
176 U1FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
178 /* Setup transmission format. */
\r
179 U1LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
181 /* Setup the VIC for the UART. */
\r
182 VICIntSelect &= ~( serU1VIC_CHANNEL_BIT );
\r
183 VICIntEnable |= serU1VIC_CHANNEL_BIT;
\r
184 VICVectAddr1 = ( unsigned long ) vUART_ISREntry;
\r
185 VICVectCntl1 = serU1VIC_CHANNEL | serU1VIC_ENABLE;
\r
187 /* Enable UART0 interrupts. */
\r
188 U1IER |= serENABLE_INTERRUPTS;
\r
190 portEXIT_CRITICAL();
\r
194 xReturn = ( xComPortHandle ) 0;
\r
199 /*-----------------------------------------------------------*/
\r
201 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
203 /* The port handle is not required as this driver only supports UART0. */
\r
206 /* Get the next character from the buffer. Return false if no characters
\r
207 are available, or arrive before xBlockTime expires. */
\r
208 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
217 /*-----------------------------------------------------------*/
\r
219 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
221 signed char *pxNext;
\r
223 /* NOTE: This implementation does not handle the queue being full as no
\r
224 block time is used! */
\r
226 /* The port handle is not required as this driver only supports UART0. */
\r
228 ( void ) usStringLength;
\r
230 /* Send each character in the string, one at a time. */
\r
231 pxNext = ( signed char * ) pcString;
\r
234 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
238 /*-----------------------------------------------------------*/
\r
240 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
242 signed portBASE_TYPE xReturn;
\r
244 /* The port handle is not required as this driver only supports UART0. */
\r
247 portENTER_CRITICAL();
\r
249 /* Is there space to write directly to the UART? */
\r
250 if( lTHREEmpty == ( long ) pdTRUE )
\r
252 /* We wrote the character directly to the UART, so was
\r
254 lTHREEmpty = pdFALSE;
\r
260 /* We cannot write directly to the UART, so queue the character.
\r
261 Block for a maximum of xBlockTime if there is no space in the
\r
262 queue. It is ok to block within a critical section as each
\r
263 task has it's own critical section management. */
\r
264 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
266 /* Depending on queue sizing and task prioritisation: While we
\r
267 were blocked waiting to post interrupts were not disabled. It is
\r
268 possible that the serial ISR has emptied the Tx queue, in which
\r
269 case we need to start the Tx off again. */
\r
270 if( lTHREEmpty == ( long ) pdTRUE )
\r
272 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
273 lTHREEmpty = pdFALSE;
\r
278 portEXIT_CRITICAL();
\r
282 /*-----------------------------------------------------------*/
\r
284 void vUART_ISRHandler( void )
\r
287 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
288 unsigned char ucInterrupt;
\r
290 ucInterrupt = U1IIR;
\r
292 /* The interrupt pending bit is active low. */
\r
293 while( ( ucInterrupt & serINTERRUPT_IS_PENDING ) == 0 )
\r
295 /* What caused the interrupt? */
\r
296 switch( ucInterrupt & serINTERRUPT_SOURCE_MASK )
\r
298 case serSOURCE_ERROR : /* Not handling this, but clear the interrupt. */
\r
302 case serSOURCE_THRE : /* The THRE is empty. If there is another
\r
303 character in the Tx queue, send it now. */
\r
304 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
310 /* There are no further characters
\r
311 queued to send so we can indicate
\r
312 that the THRE is available. */
\r
313 lTHREEmpty = pdTRUE;
\r
317 case serSOURCE_RX_TIMEOUT :
\r
318 case serSOURCE_RX : /* A character was received. Place it in
\r
319 the queue of received characters. */
\r
321 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
324 default : /* There is nothing to do, leave the ISR. */
\r
328 ucInterrupt = U1IIR;
\r
331 /* Clear the ISR in the VIC. */
\r
332 VICVectAddr = serCLEAR_VIC_INTERRUPT;
\r
334 /* Exit the ISR. If a task was woken by either a character being received
\r
335 or transmitted then a context switch will occur. */
\r
336 portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
338 /*-----------------------------------------------------------*/
\r