2 FreeRTOS V7.0.1 - Copyright (C) 2011 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 + Made serial ISR handling more complete and robust.
\r
61 + Split serial.c into serial.c and serialISR.c. serial.c can be
\r
62 compiled using ARM or THUMB modes. serialISR.c must always be
\r
63 compiled in ARM mode.
\r
64 + Another small change to cSerialPutChar().
\r
68 + In cSerialPutChar() an extra check is made to ensure the post to
\r
69 the queue was successful if then attempting to retrieve the posted
\r
75 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
77 This file contains all the serial port components that can be compiled to
\r
78 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
79 contained in serialISR.c.
\r
82 /* Standard includes. */
\r
85 /* Scheduler includes. */
\r
86 #include "FreeRTOS.h"
\r
90 /* Demo application includes. */
\r
93 /*-----------------------------------------------------------*/
\r
95 /* Constants to setup and access the UART. */
\r
96 #define serDLAB ( ( unsigned char ) 0x80 )
\r
97 #define serENABLE_INTERRUPTS ( ( unsigned char ) 0x03 )
\r
98 #define serNO_PARITY ( ( unsigned char ) 0x00 )
\r
99 #define ser1_STOP_BIT ( ( unsigned char ) 0x00 )
\r
100 #define ser8_BIT_CHARS ( ( unsigned char ) 0x03 )
\r
101 #define serFIFO_ON ( ( unsigned char ) 0x01 )
\r
102 #define serCLEAR_FIFO ( ( unsigned char ) 0x06 )
\r
103 #define serWANTED_CLOCK_SCALING ( ( unsigned long ) 16 )
\r
105 /* Constants to setup and access the VIC. */
\r
106 #define serUART0_VIC_CHANNEL ( ( unsigned long ) 0x0006 )
\r
107 #define serUART0_VIC_CHANNEL_BIT ( ( unsigned long ) 0x0040 )
\r
108 #define serUART0_VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
109 #define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
\r
111 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
112 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
113 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
115 /*-----------------------------------------------------------*/
\r
117 /* Queues used to hold received characters, and characters waiting to be
\r
119 static xQueueHandle xRxedChars;
\r
120 static xQueueHandle xCharsForTx;
\r
122 /*-----------------------------------------------------------*/
\r
124 /* Communication flag between the interrupt service routine and serial API. */
\r
125 static volatile long *plTHREEmpty;
\r
128 * The queues are created in serialISR.c as they are used from the ISR.
\r
129 * Obtain references to the queues and THRE Empty flag.
\r
131 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx, long volatile **pplTHREEmptyFlag );
\r
133 /*-----------------------------------------------------------*/
\r
135 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
137 unsigned long ulDivisor, ulWantedClock;
\r
138 xComPortHandle xReturn = serHANDLE;
\r
139 extern void ( vUART_ISR_Wrapper )( void );
\r
141 /* The queues are used in the serial ISR routine, so are created from
\r
142 serialISR.c (which is always compiled to ARM mode. */
\r
143 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );
\r
146 ( xRxedChars != serINVALID_QUEUE ) &&
\r
147 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
148 ( ulWantedBaud != ( unsigned long ) 0 )
\r
151 portENTER_CRITICAL();
\r
153 /* Setup the baud rate: Calculate the divisor value. */
\r
154 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
\r
155 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
\r
157 /* Set the DLAB bit so we can access the divisor. */
\r
158 UART0_LCR |= serDLAB;
\r
160 /* Setup the divisor. */
\r
161 UART0_DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
163 UART0_DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
165 /* Turn on the FIFO's and clear the buffers. */
\r
166 UART0_FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
168 /* Setup transmission format. */
\r
169 UART0_LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
171 /* Setup the VIC for the UART. */
\r
172 VICIntSelect &= ~( serUART0_VIC_CHANNEL_BIT );
\r
173 VICIntEnable |= serUART0_VIC_CHANNEL_BIT;
\r
174 VICVectAddr1 = ( long ) vUART_ISR_Wrapper;
\r
175 VICVectCntl1 = serUART0_VIC_CHANNEL | serUART0_VIC_ENABLE;
\r
177 /* Enable UART0 interrupts. */
\r
178 UART0_IER |= serENABLE_INTERRUPTS;
\r
180 portEXIT_CRITICAL();
\r
184 xReturn = ( xComPortHandle ) 0;
\r
189 /*-----------------------------------------------------------*/
\r
191 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
193 /* The port handle is not required as this driver only supports UART0. */
\r
196 /* Get the next character from the buffer. Return false if no characters
\r
197 are available, or arrive before xBlockTime expires. */
\r
198 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
207 /*-----------------------------------------------------------*/
\r
209 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
211 signed char *pxNext;
\r
213 /* NOTE: This implementation does not handle the queue being full as no
\r
214 block time is used! */
\r
216 /* The port handle is not required as this driver only supports UART0. */
\r
218 ( void ) usStringLength;
\r
220 /* Send each character in the string, one at a time. */
\r
221 pxNext = ( signed char * ) pcString;
\r
224 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
228 /*-----------------------------------------------------------*/
\r
230 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
232 signed portBASE_TYPE xReturn;
\r
234 /* This demo driver only supports one port so the parameter is not used. */
\r
237 portENTER_CRITICAL();
\r
239 /* Is there space to write directly to the UART? */
\r
240 if( *plTHREEmpty == ( long ) pdTRUE )
\r
242 /* We wrote the character directly to the UART, so was
\r
244 *plTHREEmpty = pdFALSE;
\r
245 UART0_THR = cOutChar;
\r
250 /* We cannot write directly to the UART, so queue the character.
\r
251 Block for a maximum of xBlockTime if there is no space in the
\r
253 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
255 /* Depending on queue sizing and task prioritisation: While we
\r
256 were blocked waiting to post interrupts were not disabled. It is
\r
257 possible that the serial ISR has emptied the Tx queue, in which
\r
258 case we need to start the Tx off again. */
\r
259 if( ( *plTHREEmpty == ( long ) pdTRUE ) && ( xReturn == pdPASS ) )
\r
261 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
262 *plTHREEmpty = pdFALSE;
\r
263 UART0_THR = cOutChar;
\r
267 portEXIT_CRITICAL();
\r
271 /*-----------------------------------------------------------*/
\r
273 void vSerialClose( xComPortHandle xPort )
\r
275 /* Not supported as not required by the demo application. */
\r
278 /*-----------------------------------------------------------*/
\r