2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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
56 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
58 Note this driver is used to test the FreeRTOS port. It is NOT intended to
\r
59 be an example of an efficient implementation!
\r
62 /* Standard includes. */
\r
65 /* Scheduler includes. */
\r
66 #include "FreeRTOS.h"
\r
70 /* Demo application includes. */
\r
73 /*-----------------------------------------------------------*/
\r
75 /* Constants to setup and access the UART. */
\r
76 #define serDLAB ( ( unsigned char ) 0x80 )
\r
77 #define serENABLE_INTERRUPTS ( ( unsigned char ) 0x03 )
\r
78 #define serNO_PARITY ( ( unsigned char ) 0x00 )
\r
79 #define ser1_STOP_BIT ( ( unsigned char ) 0x00 )
\r
80 #define ser8_BIT_CHARS ( ( unsigned char ) 0x03 )
\r
81 #define serFIFO_ON ( ( unsigned char ) 0x01 )
\r
82 #define serCLEAR_FIFO ( ( unsigned char ) 0x06 )
\r
83 #define serWANTED_CLOCK_SCALING ( ( unsigned long ) 16 )
\r
85 /* Constants to setup and access the VIC. */
\r
86 #define serU1VIC_CHANNEL ( ( unsigned long ) 0x0007 )
\r
87 #define serU1VIC_CHANNEL_BIT ( ( unsigned long ) 0x0080 )
\r
88 #define serU1VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
91 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
92 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
93 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
95 /* Constant to access the VIC. */
\r
96 #define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
\r
98 /* Constants to determine the ISR source. */
\r
99 #define serSOURCE_THRE ( ( unsigned char ) 0x02 )
\r
100 #define serSOURCE_RX_TIMEOUT ( ( unsigned char ) 0x0c )
\r
101 #define serSOURCE_ERROR ( ( unsigned char ) 0x06 )
\r
102 #define serSOURCE_RX ( ( unsigned char ) 0x04 )
\r
103 #define serINTERRUPT_SOURCE_MASK ( ( unsigned char ) 0x0f )
\r
104 #define serINTERRUPT_IS_PENDING ( ( unsigned char ) 0x01 )
\r
106 /*-----------------------------------------------------------*/
\r
109 * The asm wrapper for the interrupt service routine.
\r
111 extern void vUART_ISREntry( void );
\r
114 * The C function called from the asm wrapper.
\r
116 void vUART_ISRHandler( void );
\r
118 /*-----------------------------------------------------------*/
\r
120 /* Queues used to hold received characters, and characters waiting to be
\r
122 static xQueueHandle xRxedChars;
\r
123 static xQueueHandle xCharsForTx;
\r
125 /* Communication flag between the interrupt service routine and serial API. */
\r
126 static volatile long lTHREEmpty;
\r
128 /*-----------------------------------------------------------*/
\r
130 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
132 unsigned long ulDivisor, ulWantedClock;
\r
133 xComPortHandle xReturn = serHANDLE;
\r
135 /* Create the queues used to hold Rx and Tx characters. */
\r
136 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
137 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
139 /* Initialise the THRE empty flag. */
\r
140 lTHREEmpty = pdTRUE;
\r
143 ( xRxedChars != serINVALID_QUEUE ) &&
\r
144 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
145 ( ulWantedBaud != ( unsigned long ) 0 )
\r
148 portENTER_CRITICAL()
\r
150 /* Setup the baud rate: Calculate the divisor value. */
\r
151 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
\r
152 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
\r
154 /* Set the DLAB bit so we can access the divisor. */
\r
157 /* Setup the divisor. */
\r
158 U1DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
160 U1DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
162 /* Turn on the FIFO's and clear the buffers. */
\r
163 U1FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
165 /* Setup transmission format. */
\r
166 U1LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
168 /* Setup the VIC for the UART. */
\r
169 VICIntSelect &= ~( serU1VIC_CHANNEL_BIT );
\r
170 VICIntEnable |= serU1VIC_CHANNEL_BIT;
\r
171 VICVectAddr1 = ( unsigned long ) vUART_ISREntry;
\r
172 VICVectCntl1 = serU1VIC_CHANNEL | serU1VIC_ENABLE;
\r
174 /* Enable UART0 interrupts. */
\r
175 U1IER |= serENABLE_INTERRUPTS;
\r
177 portEXIT_CRITICAL();
\r
181 xReturn = ( xComPortHandle ) 0;
\r
186 /*-----------------------------------------------------------*/
\r
188 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
190 /* The port handle is not required as this driver only supports UART0. */
\r
193 /* Get the next character from the buffer. Return false if no characters
\r
194 are available, or arrive before xBlockTime expires. */
\r
195 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
204 /*-----------------------------------------------------------*/
\r
206 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
208 signed char *pxNext;
\r
210 /* NOTE: This implementation does not handle the queue being full as no
\r
211 block time is used! */
\r
213 /* The port handle is not required as this driver only supports UART0. */
\r
215 ( void ) usStringLength;
\r
217 /* Send each character in the string, one at a time. */
\r
218 pxNext = ( signed char * ) pcString;
\r
221 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
225 /*-----------------------------------------------------------*/
\r
227 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
229 signed portBASE_TYPE xReturn;
\r
231 /* The port handle is not required as this driver only supports UART0. */
\r
234 portENTER_CRITICAL();
\r
236 /* Is there space to write directly to the UART? */
\r
237 if( lTHREEmpty == ( long ) pdTRUE )
\r
239 /* We wrote the character directly to the UART, so was
\r
241 lTHREEmpty = pdFALSE;
\r
247 /* We cannot write directly to the UART, so queue the character.
\r
248 Block for a maximum of xBlockTime if there is no space in the
\r
249 queue. It is ok to block within a critical section as each
\r
250 task has it's own critical section management. */
\r
251 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
253 /* Depending on queue sizing and task prioritisation: While we
\r
254 were blocked waiting to post interrupts were not disabled. It is
\r
255 possible that the serial ISR has emptied the Tx queue, in which
\r
256 case we need to start the Tx off again. */
\r
257 if( lTHREEmpty == ( long ) pdTRUE )
\r
259 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
260 lTHREEmpty = pdFALSE;
\r
265 portEXIT_CRITICAL();
\r
269 /*-----------------------------------------------------------*/
\r
271 void vUART_ISRHandler( void )
\r
274 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
275 unsigned char ucInterrupt;
\r
277 ucInterrupt = U1IIR;
\r
279 /* The interrupt pending bit is active low. */
\r
280 while( ( ucInterrupt & serINTERRUPT_IS_PENDING ) == 0 )
\r
282 /* What caused the interrupt? */
\r
283 switch( ucInterrupt & serINTERRUPT_SOURCE_MASK )
\r
285 case serSOURCE_ERROR : /* Not handling this, but clear the interrupt. */
\r
289 case serSOURCE_THRE : /* The THRE is empty. If there is another
\r
290 character in the Tx queue, send it now. */
\r
291 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
297 /* There are no further characters
\r
298 queued to send so we can indicate
\r
299 that the THRE is available. */
\r
300 lTHREEmpty = pdTRUE;
\r
304 case serSOURCE_RX_TIMEOUT :
\r
305 case serSOURCE_RX : /* A character was received. Place it in
\r
306 the queue of received characters. */
\r
308 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
311 default : /* There is nothing to do, leave the ISR. */
\r
315 ucInterrupt = U1IIR;
\r
318 /* Clear the ISR in the VIC. */
\r
319 VICVectAddr = serCLEAR_VIC_INTERRUPT;
\r
321 /* Exit the ISR. If a task was woken by either a character being received
\r
322 or transmitted then a context switch will occur. */
\r
323 portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
325 /*-----------------------------------------------------------*/
\r