]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2106_GCC/serial/serial.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / ARM7_LPC2106_GCC / serial / serial.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*\r
97         Changes from V2.4.0\r
98 \r
99                 + Made serial ISR handling more complete and robust.\r
100 \r
101         Changes from V2.4.1\r
102 \r
103                 + Split serial.c into serial.c and serialISR.c.  serial.c can be \r
104                   compiled using ARM or THUMB modes.  serialISR.c must always be\r
105                   compiled in ARM mode.\r
106                 + Another small change to cSerialPutChar().\r
107 \r
108         Changed from V2.5.1\r
109 \r
110                 + In cSerialPutChar() an extra check is made to ensure the post to\r
111                   the queue was successful if then attempting to retrieve the posted\r
112                   character.\r
113 \r
114 */\r
115 \r
116 /* \r
117         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
118 \r
119         This file contains all the serial port components that can be compiled to\r
120         either ARM or THUMB mode.  Components that must be compiled to ARM mode are\r
121         contained in serialISR.c.\r
122 */\r
123 \r
124 /* Standard includes. */\r
125 #include <stdlib.h>\r
126 \r
127 /* Scheduler includes. */\r
128 #include "FreeRTOS.h"\r
129 #include "queue.h"\r
130 #include "task.h"\r
131 \r
132 /* Demo application includes. */\r
133 #include "serial.h"\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /* Constants to setup and access the UART. */\r
138 #define serDLAB                                                 ( ( unsigned char ) 0x80 )\r
139 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )\r
140 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )\r
141 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )\r
142 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )\r
143 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )\r
144 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )\r
145 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )\r
146 \r
147 /* Constants to setup and access the VIC. */\r
148 #define serUART0_VIC_CHANNEL                    ( ( unsigned long ) 0x0006 )\r
149 #define serUART0_VIC_CHANNEL_BIT                ( ( unsigned long ) 0x0040 )\r
150 #define serUART0_VIC_ENABLE                             ( ( unsigned long ) 0x0020 )\r
151 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
152 \r
153 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
154 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
155 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 /* Queues used to hold received characters, and characters waiting to be\r
160 transmitted. */\r
161 static QueueHandle_t xRxedChars; \r
162 static QueueHandle_t xCharsForTx; \r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /* Communication flag between the interrupt service routine and serial API. */\r
167 static volatile long *plTHREEmpty;\r
168 \r
169 /* \r
170  * The queues are created in serialISR.c as they are used from the ISR.\r
171  * Obtain references to the queues and THRE Empty flag. \r
172  */\r
173 extern void vSerialISRCreateQueues(     unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag );\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
178 {\r
179 unsigned long ulDivisor, ulWantedClock;\r
180 xComPortHandle xReturn = serHANDLE;\r
181 extern void ( vUART_ISR_Wrapper )( void );\r
182 \r
183         /* The queues are used in the serial ISR routine, so are created from\r
184         serialISR.c (which is always compiled to ARM mode. */\r
185         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );\r
186 \r
187         if( \r
188                 ( xRxedChars != serINVALID_QUEUE ) && \r
189                 ( xCharsForTx != serINVALID_QUEUE ) && \r
190                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
191           )\r
192         {\r
193                 portENTER_CRITICAL();\r
194                 {\r
195                         /* Setup the baud rate:  Calculate the divisor value. */\r
196                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
197                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
198 \r
199                         /* Set the DLAB bit so we can access the divisor. */\r
200                         UART0_LCR |= serDLAB;\r
201 \r
202                         /* Setup the divisor. */\r
203                         UART0_DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
204                         ulDivisor >>= 8;\r
205                         UART0_DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
206 \r
207                         /* Turn on the FIFO's and clear the buffers. */\r
208                         UART0_FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
209 \r
210                         /* Setup transmission format. */\r
211                         UART0_LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
212 \r
213                         /* Setup the VIC for the UART. */\r
214                         VICIntSelect &= ~( serUART0_VIC_CHANNEL_BIT );\r
215                         VICIntEnable |= serUART0_VIC_CHANNEL_BIT;\r
216                         VICVectAddr1 = ( long ) vUART_ISR_Wrapper;\r
217                         VICVectCntl1 = serUART0_VIC_CHANNEL | serUART0_VIC_ENABLE;\r
218 \r
219                         /* Enable UART0 interrupts. */\r
220                         UART0_IER |= serENABLE_INTERRUPTS;\r
221                 }\r
222                 portEXIT_CRITICAL();\r
223         }\r
224         else\r
225         {\r
226                 xReturn = ( xComPortHandle ) 0;\r
227         }\r
228 \r
229         return xReturn;\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
234 {\r
235         /* The port handle is not required as this driver only supports UART0. */\r
236         ( void ) pxPort;\r
237 \r
238         /* Get the next character from the buffer.  Return false if no characters\r
239         are available, or arrive before xBlockTime expires. */\r
240         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
241         {\r
242                 return pdTRUE;\r
243         }\r
244         else\r
245         {\r
246                 return pdFALSE;\r
247         }\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
252 {\r
253 signed char *pxNext;\r
254 \r
255         /* NOTE: This implementation does not handle the queue being full as no\r
256         block time is used! */\r
257 \r
258         /* The port handle is not required as this driver only supports UART0. */\r
259         ( void ) pxPort;\r
260         ( void ) usStringLength;\r
261 \r
262         /* Send each character in the string, one at a time. */\r
263         pxNext = ( signed char * ) pcString;\r
264         while( *pxNext )\r
265         {\r
266                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
267                 pxNext++;\r
268         }\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
273 {\r
274 signed portBASE_TYPE xReturn;\r
275 \r
276         /* This demo driver only supports one port so the parameter is not used. */\r
277         ( void ) pxPort;\r
278 \r
279         portENTER_CRITICAL();\r
280         {\r
281                 /* Is there space to write directly to the UART? */\r
282                 if( *plTHREEmpty == ( long ) pdTRUE )\r
283                 {\r
284                         /* We wrote the character directly to the UART, so was \r
285                         successful. */\r
286                         *plTHREEmpty = pdFALSE;\r
287                         UART0_THR = cOutChar;\r
288                         xReturn = pdPASS;\r
289                 }\r
290                 else \r
291                 {\r
292                         /* We cannot write directly to the UART, so queue the character.\r
293                         Block for a maximum of xBlockTime if there is no space in the\r
294                         queue. */\r
295                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
296 \r
297                         /* Depending on queue sizing and task prioritisation:  While we \r
298                         were blocked waiting to post interrupts were not disabled.  It is \r
299                         possible that the serial ISR has emptied the Tx queue, in which\r
300                         case we need to start the Tx off again. */\r
301                         if( ( *plTHREEmpty == ( long ) pdTRUE ) && ( xReturn == pdPASS ) )\r
302                         {\r
303                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
304                                 *plTHREEmpty = pdFALSE;\r
305                                 UART0_THR = cOutChar;\r
306                         }\r
307                 }\r
308         }\r
309         portEXIT_CRITICAL();\r
310 \r
311         return xReturn;\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 void vSerialClose( xComPortHandle xPort )\r
316 {\r
317         /* Not supported as not required by the demo application. */\r
318         ( void ) xPort;\r
319 }\r
320 /*-----------------------------------------------------------*/\r
321 \r
322 \r
323 \r
324 \r
325 \r
326         \r