]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2129_Keil_RVDS/serial/serial.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / ARM7_LPC2129_Keil_RVDS / 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 /* \r
69         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
70 \r
71         Note this driver is used to test the FreeRTOS port.  It is NOT intended to\r
72         be an example of an efficient implementation!\r
73 */\r
74 \r
75 /* Standard includes. */\r
76 #include <stdlib.h>\r
77 \r
78 /* Scheduler includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "queue.h"\r
81 #include "task.h"\r
82 \r
83 /* Demo application includes. */\r
84 #include "serial.h"\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* Constants to setup and access the UART. */\r
89 #define serDLAB                                                 ( ( unsigned char ) 0x80 )\r
90 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )\r
91 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )\r
92 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )\r
93 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )\r
94 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )\r
95 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )\r
96 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )\r
97 \r
98 /* Constants to setup and access the VIC. */\r
99 #define serU1VIC_CHANNEL                                ( ( unsigned long ) 0x0007 )\r
100 #define serU1VIC_CHANNEL_BIT                    ( ( unsigned long ) 0x0080 )\r
101 #define serU1VIC_ENABLE                                 ( ( unsigned long ) 0x0020 )\r
102 \r
103 /* Misc. */\r
104 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
105 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
106 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
107 \r
108 /* Constant to access the VIC. */\r
109 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
110 \r
111 /* Constants to determine the ISR source. */\r
112 #define serSOURCE_THRE                                  ( ( unsigned char ) 0x02 )\r
113 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned char ) 0x0c )\r
114 #define serSOURCE_ERROR                                 ( ( unsigned char ) 0x06 )\r
115 #define serSOURCE_RX                                    ( ( unsigned char ) 0x04 )\r
116 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned char ) 0x0f )\r
117 #define serINTERRUPT_IS_PENDING                 ( ( unsigned char ) 0x01 )\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /*\r
122  * The asm wrapper for the interrupt service routine.\r
123  */\r
124 extern void vUART_ISREntry( void );\r
125 \r
126 /* \r
127  * The C function called from the asm wrapper. \r
128  */\r
129 void vUART_ISRHandler( void );\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 xQueueHandle xRxedChars; \r
136 static xQueueHandle xCharsForTx; \r
137 \r
138 /* Communication flag between the interrupt service routine and serial API. */\r
139 static volatile long lTHREEmpty;\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
144 {\r
145 unsigned long ulDivisor, ulWantedClock;\r
146 xComPortHandle xReturn = serHANDLE;\r
147 \r
148         /* Create the queues used to hold Rx and Tx characters. */\r
149         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
150         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
151 \r
152         /* Initialise the THRE empty flag. */\r
153         lTHREEmpty = pdTRUE;\r
154 \r
155         if( \r
156                 ( xRxedChars != serINVALID_QUEUE ) && \r
157                 ( xCharsForTx != serINVALID_QUEUE ) && \r
158                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
159           )\r
160         {\r
161                 portENTER_CRITICAL()\r
162                 {\r
163                         /* Setup the baud rate:  Calculate the divisor value. */\r
164                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
165                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
166 \r
167                         /* Set the DLAB bit so we can access the divisor. */\r
168                         U1LCR |= serDLAB;\r
169 \r
170                         /* Setup the divisor. */\r
171                         U1DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
172                         ulDivisor >>= 8;\r
173                         U1DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
174 \r
175                         /* Turn on the FIFO's and clear the buffers. */\r
176                         U1FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
177 \r
178                         /* Setup transmission format. */\r
179                         U1LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
180 \r
181                         /* Setup the VIC for the UART. */\r
182                         VICIntSelect &= ~( serU1VIC_CHANNEL_BIT );\r
183                         VICIntEnable |= serU1VIC_CHANNEL_BIT;\r
184                         VICVectAddr1 = ( unsigned long ) vUART_ISREntry;\r
185                         VICVectCntl1 = serU1VIC_CHANNEL | serU1VIC_ENABLE;\r
186 \r
187                         /* Enable UART0 interrupts. */\r
188                         U1IER |= serENABLE_INTERRUPTS;\r
189                 }\r
190                 portEXIT_CRITICAL();\r
191         }\r
192         else\r
193         {\r
194                 xReturn = ( xComPortHandle ) 0;\r
195         }\r
196 \r
197         return xReturn;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
202 {\r
203         /* The port handle is not required as this driver only supports UART0. */\r
204         ( void ) pxPort;\r
205 \r
206         /* Get the next character from the buffer.  Return false if no characters\r
207         are available, or arrive before xBlockTime expires. */\r
208         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
209         {\r
210                 return pdTRUE;\r
211         }\r
212         else\r
213         {\r
214                 return pdFALSE;\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
220 {\r
221 signed char *pxNext;\r
222 \r
223         /* NOTE: This implementation does not handle the queue being full as no\r
224         block time is used! */\r
225 \r
226         /* The port handle is not required as this driver only supports UART0. */\r
227         ( void ) pxPort;\r
228         ( void ) usStringLength;\r
229 \r
230         /* Send each character in the string, one at a time. */\r
231         pxNext = ( signed char * ) pcString;\r
232         while( *pxNext )\r
233         {\r
234                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
235                 pxNext++;\r
236         }\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
241 {\r
242 signed portBASE_TYPE xReturn;\r
243 \r
244         /* The port handle is not required as this driver only supports UART0. */\r
245         ( void ) pxPort;\r
246 \r
247         portENTER_CRITICAL();\r
248         {\r
249                 /* Is there space to write directly to the UART? */\r
250                 if( lTHREEmpty == ( long ) pdTRUE )\r
251                 {\r
252                         /* We wrote the character directly to the UART, so was \r
253                         successful. */\r
254                         lTHREEmpty = pdFALSE;\r
255                         U1THR = cOutChar;\r
256                         xReturn = pdPASS;\r
257                 }\r
258                 else \r
259                 {\r
260                         /* We cannot write directly to the UART, so queue the character.\r
261                         Block for a maximum of xBlockTime if there is no space in the\r
262                         queue.  It is ok to block within a critical section as each\r
263                         task has it's own critical section management. */\r
264                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
265 \r
266                         /* Depending on queue sizing and task prioritisation:  While we \r
267                         were blocked waiting to post interrupts were not disabled.  It is \r
268                         possible that the serial ISR has emptied the Tx queue, in which\r
269                         case we need to start the Tx off again. */\r
270                         if( lTHREEmpty == ( long ) pdTRUE )\r
271                         {\r
272                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
273                                 lTHREEmpty = pdFALSE;\r
274                                 U1THR = cOutChar;\r
275                         }\r
276                 }\r
277         }\r
278         portEXIT_CRITICAL();\r
279 \r
280         return xReturn;\r
281 }\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 void vUART_ISRHandler( void )\r
285 {\r
286 signed char cChar;\r
287 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
288 unsigned char ucInterrupt;\r
289 \r
290         ucInterrupt = U1IIR;\r
291 \r
292         /* The interrupt pending bit is active low. */\r
293         while( ( ucInterrupt & serINTERRUPT_IS_PENDING ) == 0 )\r
294         {\r
295                 /* What caused the interrupt? */\r
296                 switch( ucInterrupt & serINTERRUPT_SOURCE_MASK )\r
297                 {\r
298                         case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
299                                                                         cChar = U1LSR;\r
300                                                                         break;\r
301         \r
302                         case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
303                                                                         character in the Tx queue, send it now. */\r
304                                                                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
305                                                                         {\r
306                                                                                 U1THR = cChar;\r
307                                                                         }\r
308                                                                         else\r
309                                                                         {\r
310                                                                                 /* There are no further characters \r
311                                                                                 queued to send so we can indicate \r
312                                                                                 that the THRE is available. */\r
313                                                                                 lTHREEmpty = pdTRUE;\r
314                                                                         }\r
315                                                                         break;\r
316         \r
317                         case serSOURCE_RX_TIMEOUT :\r
318                         case serSOURCE_RX       :       /* A character was received.  Place it in \r
319                                                                         the queue of received characters. */\r
320                                                                         cChar = U1RBR;\r
321                                                                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
322                                                                         break;\r
323         \r
324                         default                         :       /* There is nothing to do, leave the ISR. */\r
325                                                                         break;\r
326                 }\r
327 \r
328                 ucInterrupt = U1IIR;\r
329         }\r
330 \r
331         /* Clear the ISR in the VIC. */\r
332         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
333 \r
334         /* Exit the ISR.  If a task was woken by either a character being received\r
335         or transmitted then a context switch will occur. */\r
336         portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 \r
341 \r
342 \r
343 \r
344 \r
345         \r