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