simple-FreeRTOS-demo application is created
[armadillo_firmware.git] / simple-FreeRTOS-demo / serial / serial.c
blob706d886f689e3d567da5b8246041fe05041288d6
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
55 Changes from V2.4.0
57 + Made serial ISR handling more complete and robust.
59 Changes from V2.4.1
61 + Split serial.c into serial.c and serialISR.c. serial.c can be
62 compiled using ARM or THUMB modes. serialISR.c must always be
63 compiled in ARM mode.
64 + Another small change to cSerialPutChar().
66 Changed from V2.5.1
68 + In cSerialPutChar() an extra check is made to ensure the post to
69 the queue was successful if then attempting to retrieve the posted
70 character.
74 /*
75 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
77 This file contains all the serial port components that can be compiled to
78 either ARM or THUMB mode. Components that must be compiled to ARM mode are
79 contained in serialISR.c.
82 /* Standard includes. */
83 #include <stdlib.h>
85 /* Scheduler includes. */
86 #include "FreeRTOS.h"
87 #include "queue.h"
88 #include "task.h"
90 /* Demo application includes. */
91 #include "serial.h"
93 /*-----------------------------------------------------------*/
95 /* Constants to setup and access the UART. */
96 #define serDLAB ( ( unsigned char ) 0x80 )
97 #define serENABLE_INTERRUPTS ( ( unsigned char ) 0x03 )
98 #define serNO_PARITY ( ( unsigned char ) 0x00 )
99 #define ser1_STOP_BIT ( ( unsigned char ) 0x00 )
100 #define ser8_BIT_CHARS ( ( unsigned char ) 0x03 )
101 #define serFIFO_ON ( ( unsigned char ) 0x01 )
102 #define serCLEAR_FIFO ( ( unsigned char ) 0x06 )
103 #define serWANTED_CLOCK_SCALING ( ( unsigned long ) 16 )
105 /* Constants to setup and access the VIC. */
106 #define serUART0_VIC_CHANNEL ( ( unsigned long ) 0x0006 )
107 #define serUART0_VIC_CHANNEL_BIT ( ( unsigned long ) 0x0040 )
108 #define serUART0_VIC_ENABLE ( ( unsigned long ) 0x0020 )
109 #define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
111 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
112 #define serHANDLE ( ( xComPortHandle ) 1 )
113 #define serNO_BLOCK ( ( portTickType ) 0 )
115 /*-----------------------------------------------------------*/
117 /* Queues used to hold received characters, and characters waiting to be
118 transmitted. */
119 static xQueueHandle xRxedChars;
120 static xQueueHandle xCharsForTx;
122 /*-----------------------------------------------------------*/
124 /* Communication flag between the interrupt service routine and serial API. */
125 static volatile long *plTHREEmpty;
128 * The queues are created in serialISR.c as they are used from the ISR.
129 * Obtain references to the queues and THRE Empty flag.
131 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx, long volatile **pplTHREEmptyFlag );
133 /*-----------------------------------------------------------*/
135 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
137 unsigned long ulDivisor, ulWantedClock;
138 xComPortHandle xReturn = serHANDLE;
139 extern void ( vUART_ISR_Wrapper )( void );
141 /* The queues are used in the serial ISR routine, so are created from
142 serialISR.c (which is always compiled to ARM mode. */
143 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );
145 if(
146 ( xRxedChars != serINVALID_QUEUE ) &&
147 ( xCharsForTx != serINVALID_QUEUE ) &&
148 ( ulWantedBaud != ( unsigned long ) 0 )
151 portENTER_CRITICAL();
153 /* Setup the baud rate: Calculate the divisor value. */
154 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
155 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
157 /* Set the DLAB bit so we can access the divisor. */
158 UART0_LCR |= serDLAB;
160 /* Setup the divisor. */
161 UART0_DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
162 ulDivisor >>= 8;
163 UART0_DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
165 /* Turn on the FIFO's and clear the buffers. */
166 UART0_FCR = ( serFIFO_ON | serCLEAR_FIFO );
168 /* Setup transmission format. */
169 UART0_LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
171 /* Setup the VIC for the UART. */
172 VICIntSelect &= ~( serUART0_VIC_CHANNEL_BIT );
173 VICIntEnable |= serUART0_VIC_CHANNEL_BIT;
174 VICVectAddr1 = ( long ) vUART_ISR_Wrapper;
175 VICVectCntl1 = serUART0_VIC_CHANNEL | serUART0_VIC_ENABLE;
177 /* Enable UART0 interrupts. */
178 UART0_IER |= serENABLE_INTERRUPTS;
180 portEXIT_CRITICAL();
182 else
184 xReturn = ( xComPortHandle ) 0;
187 return xReturn;
189 /*-----------------------------------------------------------*/
191 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
193 /* The port handle is not required as this driver only supports UART0. */
194 ( void ) pxPort;
196 /* Get the next character from the buffer. Return false if no characters
197 are available, or arrive before xBlockTime expires. */
198 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
200 return pdTRUE;
202 else
204 return pdFALSE;
207 /*-----------------------------------------------------------*/
209 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
211 signed char *pxNext;
213 /* NOTE: This implementation does not handle the queue being full as no
214 block time is used! */
216 /* The port handle is not required as this driver only supports UART0. */
217 ( void ) pxPort;
218 ( void ) usStringLength;
220 /* Send each character in the string, one at a time. */
221 pxNext = ( signed char * ) pcString;
222 while( *pxNext )
224 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
225 pxNext++;
228 /*-----------------------------------------------------------*/
230 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
232 signed portBASE_TYPE xReturn;
234 /* This demo driver only supports one port so the parameter is not used. */
235 ( void ) pxPort;
237 portENTER_CRITICAL();
239 /* Is there space to write directly to the UART? */
240 if( *plTHREEmpty == ( long ) pdTRUE )
242 /* We wrote the character directly to the UART, so was
243 successful. */
244 *plTHREEmpty = pdFALSE;
245 UART0_THR = cOutChar;
246 xReturn = pdPASS;
248 else
250 /* We cannot write directly to the UART, so queue the character.
251 Block for a maximum of xBlockTime if there is no space in the
252 queue. */
253 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
255 /* Depending on queue sizing and task prioritisation: While we
256 were blocked waiting to post interrupts were not disabled. It is
257 possible that the serial ISR has emptied the Tx queue, in which
258 case we need to start the Tx off again. */
259 if( ( *plTHREEmpty == ( long ) pdTRUE ) && ( xReturn == pdPASS ) )
261 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
262 *plTHREEmpty = pdFALSE;
263 UART0_THR = cOutChar;
267 portEXIT_CRITICAL();
269 return xReturn;
271 /*-----------------------------------------------------------*/
273 void vSerialClose( xComPortHandle xPort )
275 /* Not supported as not required by the demo application. */
276 ( void ) xPort;
278 /*-----------------------------------------------------------*/