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