]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2129_Keil_RVDS/serial/serial.c
d00bfb64faf132204e68a0c6bcdec2ed3bf11fcc
[freertos] / FreeRTOS / Demo / ARM7_LPC2129_Keil_RVDS / serial / serial.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 \r
66 /* \r
67         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
68 \r
69         Note this driver is used to test the FreeRTOS port.  It is NOT intended to\r
70         be an example of an efficient implementation!\r
71 */\r
72 \r
73 /* Standard includes. */\r
74 #include <stdlib.h>\r
75 \r
76 /* Scheduler includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "queue.h"\r
79 #include "task.h"\r
80 \r
81 /* Demo application includes. */\r
82 #include "serial.h"\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /* Constants to setup and access the UART. */\r
87 #define serDLAB                                                 ( ( unsigned char ) 0x80 )\r
88 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )\r
89 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )\r
90 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )\r
91 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )\r
92 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )\r
93 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )\r
94 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )\r
95 \r
96 /* Constants to setup and access the VIC. */\r
97 #define serU1VIC_CHANNEL                                ( ( unsigned long ) 0x0007 )\r
98 #define serU1VIC_CHANNEL_BIT                    ( ( unsigned long ) 0x0080 )\r
99 #define serU1VIC_ENABLE                                 ( ( unsigned long ) 0x0020 )\r
100 \r
101 /* Misc. */\r
102 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
103 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
104 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
105 \r
106 /* Constant to access the VIC. */\r
107 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
108 \r
109 /* Constants to determine the ISR source. */\r
110 #define serSOURCE_THRE                                  ( ( unsigned char ) 0x02 )\r
111 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned char ) 0x0c )\r
112 #define serSOURCE_ERROR                                 ( ( unsigned char ) 0x06 )\r
113 #define serSOURCE_RX                                    ( ( unsigned char ) 0x04 )\r
114 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned char ) 0x0f )\r
115 #define serINTERRUPT_IS_PENDING                 ( ( unsigned char ) 0x01 )\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /*\r
120  * The asm wrapper for the interrupt service routine.\r
121  */\r
122 extern void vUART_ISREntry( void );\r
123 \r
124 /* \r
125  * The C function called from the asm wrapper. \r
126  */\r
127 void vUART_ISRHandler( void );\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /* Queues used to hold received characters, and characters waiting to be\r
132 transmitted. */\r
133 static xQueueHandle xRxedChars; \r
134 static xQueueHandle xCharsForTx; \r
135 \r
136 /* Communication flag between the interrupt service routine and serial API. */\r
137 static volatile long lTHREEmpty;\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
142 {\r
143 unsigned long ulDivisor, ulWantedClock;\r
144 xComPortHandle xReturn = serHANDLE;\r
145 \r
146         /* Create the queues used to hold Rx and Tx characters. */\r
147         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
148         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
149 \r
150         /* Initialise the THRE empty flag. */\r
151         lTHREEmpty = pdTRUE;\r
152 \r
153         if( \r
154                 ( xRxedChars != serINVALID_QUEUE ) && \r
155                 ( xCharsForTx != serINVALID_QUEUE ) && \r
156                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
157           )\r
158         {\r
159                 portENTER_CRITICAL()\r
160                 {\r
161                         /* Setup the baud rate:  Calculate the divisor value. */\r
162                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
163                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
164 \r
165                         /* Set the DLAB bit so we can access the divisor. */\r
166                         U1LCR |= serDLAB;\r
167 \r
168                         /* Setup the divisor. */\r
169                         U1DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
170                         ulDivisor >>= 8;\r
171                         U1DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
172 \r
173                         /* Turn on the FIFO's and clear the buffers. */\r
174                         U1FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
175 \r
176                         /* Setup transmission format. */\r
177                         U1LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
178 \r
179                         /* Setup the VIC for the UART. */\r
180                         VICIntSelect &= ~( serU1VIC_CHANNEL_BIT );\r
181                         VICIntEnable |= serU1VIC_CHANNEL_BIT;\r
182                         VICVectAddr1 = ( unsigned long ) vUART_ISREntry;\r
183                         VICVectCntl1 = serU1VIC_CHANNEL | serU1VIC_ENABLE;\r
184 \r
185                         /* Enable UART0 interrupts. */\r
186                         U1IER |= serENABLE_INTERRUPTS;\r
187                 }\r
188                 portEXIT_CRITICAL();\r
189         }\r
190         else\r
191         {\r
192                 xReturn = ( xComPortHandle ) 0;\r
193         }\r
194 \r
195         return xReturn;\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
200 {\r
201         /* The port handle is not required as this driver only supports UART0. */\r
202         ( void ) pxPort;\r
203 \r
204         /* Get the next character from the buffer.  Return false if no characters\r
205         are available, or arrive before xBlockTime expires. */\r
206         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
207         {\r
208                 return pdTRUE;\r
209         }\r
210         else\r
211         {\r
212                 return pdFALSE;\r
213         }\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
218 {\r
219 signed char *pxNext;\r
220 \r
221         /* NOTE: This implementation does not handle the queue being full as no\r
222         block time is used! */\r
223 \r
224         /* The port handle is not required as this driver only supports UART0. */\r
225         ( void ) pxPort;\r
226         ( void ) usStringLength;\r
227 \r
228         /* Send each character in the string, one at a time. */\r
229         pxNext = ( signed char * ) pcString;\r
230         while( *pxNext )\r
231         {\r
232                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
233                 pxNext++;\r
234         }\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
239 {\r
240 signed portBASE_TYPE xReturn;\r
241 \r
242         /* The port handle is not required as this driver only supports UART0. */\r
243         ( void ) pxPort;\r
244 \r
245         portENTER_CRITICAL();\r
246         {\r
247                 /* Is there space to write directly to the UART? */\r
248                 if( lTHREEmpty == ( long ) pdTRUE )\r
249                 {\r
250                         /* We wrote the character directly to the UART, so was \r
251                         successful. */\r
252                         lTHREEmpty = pdFALSE;\r
253                         U1THR = cOutChar;\r
254                         xReturn = pdPASS;\r
255                 }\r
256                 else \r
257                 {\r
258                         /* We cannot write directly to the UART, so queue the character.\r
259                         Block for a maximum of xBlockTime if there is no space in the\r
260                         queue.  It is ok to block within a critical section as each\r
261                         task has it's own critical section management. */\r
262                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
263 \r
264                         /* Depending on queue sizing and task prioritisation:  While we \r
265                         were blocked waiting to post interrupts were not disabled.  It is \r
266                         possible that the serial ISR has emptied the Tx queue, in which\r
267                         case we need to start the Tx off again. */\r
268                         if( lTHREEmpty == ( long ) pdTRUE )\r
269                         {\r
270                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
271                                 lTHREEmpty = pdFALSE;\r
272                                 U1THR = cOutChar;\r
273                         }\r
274                 }\r
275         }\r
276         portEXIT_CRITICAL();\r
277 \r
278         return xReturn;\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 void vUART_ISRHandler( void )\r
283 {\r
284 signed char cChar;\r
285 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
286 unsigned char ucInterrupt;\r
287 \r
288         ucInterrupt = U1IIR;\r
289 \r
290         /* The interrupt pending bit is active low. */\r
291         while( ( ucInterrupt & serINTERRUPT_IS_PENDING ) == 0 )\r
292         {\r
293                 /* What caused the interrupt? */\r
294                 switch( ucInterrupt & serINTERRUPT_SOURCE_MASK )\r
295                 {\r
296                         case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
297                                                                         cChar = U1LSR;\r
298                                                                         break;\r
299         \r
300                         case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
301                                                                         character in the Tx queue, send it now. */\r
302                                                                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
303                                                                         {\r
304                                                                                 U1THR = cChar;\r
305                                                                         }\r
306                                                                         else\r
307                                                                         {\r
308                                                                                 /* There are no further characters \r
309                                                                                 queued to send so we can indicate \r
310                                                                                 that the THRE is available. */\r
311                                                                                 lTHREEmpty = pdTRUE;\r
312                                                                         }\r
313                                                                         break;\r
314         \r
315                         case serSOURCE_RX_TIMEOUT :\r
316                         case serSOURCE_RX       :       /* A character was received.  Place it in \r
317                                                                         the queue of received characters. */\r
318                                                                         cChar = U1RBR;\r
319                                                                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
320                                                                         break;\r
321         \r
322                         default                         :       /* There is nothing to do, leave the ISR. */\r
323                                                                         break;\r
324                 }\r
325 \r
326                 ucInterrupt = U1IIR;\r
327         }\r
328 \r
329         /* Clear the ISR in the VIC. */\r
330         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
331 \r
332         /* Exit the ISR.  If a task was woken by either a character being received\r
333         or transmitted then a context switch will occur. */\r
334         portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 \r
339 \r
340 \r
341 \r
342 \r
343         \r