]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2106_GCC/serial/serial.c
d99c73418d56c1baffbd63ced7a68bef0ccd75c2
[freertos] / FreeRTOS / Demo / ARM7_LPC2106_GCC / serial / serial.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\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
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \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
65 */\r
66 \r
67 /*\r
68         Changes from V2.4.0\r
69 \r
70                 + Made serial ISR handling more complete and robust.\r
71 \r
72         Changes from V2.4.1\r
73 \r
74                 + Split serial.c into serial.c and serialISR.c.  serial.c can be \r
75                   compiled using ARM or THUMB modes.  serialISR.c must always be\r
76                   compiled in ARM mode.\r
77                 + Another small change to cSerialPutChar().\r
78 \r
79         Changed from V2.5.1\r
80 \r
81                 + In cSerialPutChar() an extra check is made to ensure the post to\r
82                   the queue was successful if then attempting to retrieve the posted\r
83                   character.\r
84 \r
85 */\r
86 \r
87 /* \r
88         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
89 \r
90         This file contains all the serial port components that can be compiled to\r
91         either ARM or THUMB mode.  Components that must be compiled to ARM mode are\r
92         contained in serialISR.c.\r
93 */\r
94 \r
95 /* Standard includes. */\r
96 #include <stdlib.h>\r
97 \r
98 /* Scheduler includes. */\r
99 #include "FreeRTOS.h"\r
100 #include "queue.h"\r
101 #include "task.h"\r
102 \r
103 /* Demo application includes. */\r
104 #include "serial.h"\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /* Constants to setup and access the UART. */\r
109 #define serDLAB                                                 ( ( unsigned char ) 0x80 )\r
110 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )\r
111 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )\r
112 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )\r
113 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )\r
114 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )\r
115 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )\r
116 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )\r
117 \r
118 /* Constants to setup and access the VIC. */\r
119 #define serUART0_VIC_CHANNEL                    ( ( unsigned long ) 0x0006 )\r
120 #define serUART0_VIC_CHANNEL_BIT                ( ( unsigned long ) 0x0040 )\r
121 #define serUART0_VIC_ENABLE                             ( ( unsigned long ) 0x0020 )\r
122 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
123 \r
124 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
125 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
126 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /* Queues used to hold received characters, and characters waiting to be\r
131 transmitted. */\r
132 static xQueueHandle xRxedChars; \r
133 static xQueueHandle xCharsForTx; \r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /* Communication flag between the interrupt service routine and serial API. */\r
138 static volatile long *plTHREEmpty;\r
139 \r
140 /* \r
141  * The queues are created in serialISR.c as they are used from the ISR.\r
142  * Obtain references to the queues and THRE Empty flag. \r
143  */\r
144 extern void vSerialISRCreateQueues(     unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx, long volatile **pplTHREEmptyFlag );\r
145 \r
146 /*-----------------------------------------------------------*/\r
147 \r
148 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
149 {\r
150 unsigned long ulDivisor, ulWantedClock;\r
151 xComPortHandle xReturn = serHANDLE;\r
152 extern void ( vUART_ISR_Wrapper )( void );\r
153 \r
154         /* The queues are used in the serial ISR routine, so are created from\r
155         serialISR.c (which is always compiled to ARM mode. */\r
156         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );\r
157 \r
158         if( \r
159                 ( xRxedChars != serINVALID_QUEUE ) && \r
160                 ( xCharsForTx != serINVALID_QUEUE ) && \r
161                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
162           )\r
163         {\r
164                 portENTER_CRITICAL();\r
165                 {\r
166                         /* Setup the baud rate:  Calculate the divisor value. */\r
167                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
168                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
169 \r
170                         /* Set the DLAB bit so we can access the divisor. */\r
171                         UART0_LCR |= serDLAB;\r
172 \r
173                         /* Setup the divisor. */\r
174                         UART0_DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
175                         ulDivisor >>= 8;\r
176                         UART0_DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
177 \r
178                         /* Turn on the FIFO's and clear the buffers. */\r
179                         UART0_FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
180 \r
181                         /* Setup transmission format. */\r
182                         UART0_LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
183 \r
184                         /* Setup the VIC for the UART. */\r
185                         VICIntSelect &= ~( serUART0_VIC_CHANNEL_BIT );\r
186                         VICIntEnable |= serUART0_VIC_CHANNEL_BIT;\r
187                         VICVectAddr1 = ( long ) vUART_ISR_Wrapper;\r
188                         VICVectCntl1 = serUART0_VIC_CHANNEL | serUART0_VIC_ENABLE;\r
189 \r
190                         /* Enable UART0 interrupts. */\r
191                         UART0_IER |= serENABLE_INTERRUPTS;\r
192                 }\r
193                 portEXIT_CRITICAL();\r
194         }\r
195         else\r
196         {\r
197                 xReturn = ( xComPortHandle ) 0;\r
198         }\r
199 \r
200         return xReturn;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
205 {\r
206         /* The port handle is not required as this driver only supports UART0. */\r
207         ( void ) pxPort;\r
208 \r
209         /* Get the next character from the buffer.  Return false if no characters\r
210         are available, or arrive before xBlockTime expires. */\r
211         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
212         {\r
213                 return pdTRUE;\r
214         }\r
215         else\r
216         {\r
217                 return pdFALSE;\r
218         }\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
223 {\r
224 signed char *pxNext;\r
225 \r
226         /* NOTE: This implementation does not handle the queue being full as no\r
227         block time is used! */\r
228 \r
229         /* The port handle is not required as this driver only supports UART0. */\r
230         ( void ) pxPort;\r
231         ( void ) usStringLength;\r
232 \r
233         /* Send each character in the string, one at a time. */\r
234         pxNext = ( signed char * ) pcString;\r
235         while( *pxNext )\r
236         {\r
237                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
238                 pxNext++;\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
244 {\r
245 signed portBASE_TYPE xReturn;\r
246 \r
247         /* This demo driver only supports one port so the parameter is not used. */\r
248         ( void ) pxPort;\r
249 \r
250         portENTER_CRITICAL();\r
251         {\r
252                 /* Is there space to write directly to the UART? */\r
253                 if( *plTHREEmpty == ( long ) pdTRUE )\r
254                 {\r
255                         /* We wrote the character directly to the UART, so was \r
256                         successful. */\r
257                         *plTHREEmpty = pdFALSE;\r
258                         UART0_THR = cOutChar;\r
259                         xReturn = pdPASS;\r
260                 }\r
261                 else \r
262                 {\r
263                         /* We cannot write directly to the UART, so queue the character.\r
264                         Block for a maximum of xBlockTime if there is no space in the\r
265                         queue. */\r
266                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
267 \r
268                         /* Depending on queue sizing and task prioritisation:  While we \r
269                         were blocked waiting to post interrupts were not disabled.  It is \r
270                         possible that the serial ISR has emptied the Tx queue, in which\r
271                         case we need to start the Tx off again. */\r
272                         if( ( *plTHREEmpty == ( long ) pdTRUE ) && ( xReturn == pdPASS ) )\r
273                         {\r
274                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
275                                 *plTHREEmpty = pdFALSE;\r
276                                 UART0_THR = cOutChar;\r
277                         }\r
278                 }\r
279         }\r
280         portEXIT_CRITICAL();\r
281 \r
282         return xReturn;\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 void vSerialClose( xComPortHandle xPort )\r
287 {\r
288         /* Not supported as not required by the demo application. */\r
289         ( void ) xPort;\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 \r
294 \r
295 \r
296 \r
297         \r