]> begriffs open source - freertos/blob - Demo/ARM7_LPC2129_Keil_RVDS/serial/serial.c
Update to V6.1.1
[freertos] / Demo / ARM7_LPC2129_Keil_RVDS / serial / serial.c
1 /*\r
2     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 \r
55 /* \r
56         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
57 \r
58         Note this driver is used to test the FreeRTOS port.  It is NOT intended to\r
59         be an example of an efficient implementation!\r
60 */\r
61 \r
62 /* Standard includes. */\r
63 #include <stdlib.h>\r
64 \r
65 /* Scheduler includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "queue.h"\r
68 #include "task.h"\r
69 \r
70 /* Demo application includes. */\r
71 #include "serial.h"\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* Constants to setup and access the UART. */\r
76 #define serDLAB                                                 ( ( unsigned char ) 0x80 )\r
77 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )\r
78 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )\r
79 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )\r
80 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )\r
81 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )\r
82 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )\r
83 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )\r
84 \r
85 /* Constants to setup and access the VIC. */\r
86 #define serU1VIC_CHANNEL                                ( ( unsigned long ) 0x0007 )\r
87 #define serU1VIC_CHANNEL_BIT                    ( ( unsigned long ) 0x0080 )\r
88 #define serU1VIC_ENABLE                                 ( ( unsigned long ) 0x0020 )\r
89 \r
90 /* Misc. */\r
91 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
92 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
93 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
94 \r
95 /* Constant to access the VIC. */\r
96 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
97 \r
98 /* Constants to determine the ISR source. */\r
99 #define serSOURCE_THRE                                  ( ( unsigned char ) 0x02 )\r
100 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned char ) 0x0c )\r
101 #define serSOURCE_ERROR                                 ( ( unsigned char ) 0x06 )\r
102 #define serSOURCE_RX                                    ( ( unsigned char ) 0x04 )\r
103 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned char ) 0x0f )\r
104 #define serINTERRUPT_IS_PENDING                 ( ( unsigned char ) 0x01 )\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /*\r
109  * The asm wrapper for the interrupt service routine.\r
110  */\r
111 extern void vUART_ISREntry( void );\r
112 \r
113 /* \r
114  * The C function called from the asm wrapper. \r
115  */\r
116 void vUART_ISRHandler( void );\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 /* Queues used to hold received characters, and characters waiting to be\r
121 transmitted. */\r
122 static xQueueHandle xRxedChars; \r
123 static xQueueHandle xCharsForTx; \r
124 \r
125 /* Communication flag between the interrupt service routine and serial API. */\r
126 static volatile long lTHREEmpty;\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
131 {\r
132 unsigned long ulDivisor, ulWantedClock;\r
133 xComPortHandle xReturn = serHANDLE;\r
134 \r
135         /* Create the queues used to hold Rx and Tx characters. */\r
136         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
137         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
138 \r
139         /* Initialise the THRE empty flag. */\r
140         lTHREEmpty = pdTRUE;\r
141 \r
142         if( \r
143                 ( xRxedChars != serINVALID_QUEUE ) && \r
144                 ( xCharsForTx != serINVALID_QUEUE ) && \r
145                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
146           )\r
147         {\r
148                 portENTER_CRITICAL()\r
149                 {\r
150                         /* Setup the baud rate:  Calculate the divisor value. */\r
151                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
152                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
153 \r
154                         /* Set the DLAB bit so we can access the divisor. */\r
155                         U1LCR |= serDLAB;\r
156 \r
157                         /* Setup the divisor. */\r
158                         U1DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
159                         ulDivisor >>= 8;\r
160                         U1DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
161 \r
162                         /* Turn on the FIFO's and clear the buffers. */\r
163                         U1FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
164 \r
165                         /* Setup transmission format. */\r
166                         U1LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
167 \r
168                         /* Setup the VIC for the UART. */\r
169                         VICIntSelect &= ~( serU1VIC_CHANNEL_BIT );\r
170                         VICIntEnable |= serU1VIC_CHANNEL_BIT;\r
171                         VICVectAddr1 = ( unsigned long ) vUART_ISREntry;\r
172                         VICVectCntl1 = serU1VIC_CHANNEL | serU1VIC_ENABLE;\r
173 \r
174                         /* Enable UART0 interrupts. */\r
175                         U1IER |= serENABLE_INTERRUPTS;\r
176                 }\r
177                 portEXIT_CRITICAL();\r
178         }\r
179         else\r
180         {\r
181                 xReturn = ( xComPortHandle ) 0;\r
182         }\r
183 \r
184         return xReturn;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
189 {\r
190         /* The port handle is not required as this driver only supports UART0. */\r
191         ( void ) pxPort;\r
192 \r
193         /* Get the next character from the buffer.  Return false if no characters\r
194         are available, or arrive before xBlockTime expires. */\r
195         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
196         {\r
197                 return pdTRUE;\r
198         }\r
199         else\r
200         {\r
201                 return pdFALSE;\r
202         }\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
207 {\r
208 signed char *pxNext;\r
209 \r
210         /* NOTE: This implementation does not handle the queue being full as no\r
211         block time is used! */\r
212 \r
213         /* The port handle is not required as this driver only supports UART0. */\r
214         ( void ) pxPort;\r
215         ( void ) usStringLength;\r
216 \r
217         /* Send each character in the string, one at a time. */\r
218         pxNext = ( signed char * ) pcString;\r
219         while( *pxNext )\r
220         {\r
221                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
222                 pxNext++;\r
223         }\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
228 {\r
229 signed portBASE_TYPE xReturn;\r
230 \r
231         /* The port handle is not required as this driver only supports UART0. */\r
232         ( void ) pxPort;\r
233 \r
234         portENTER_CRITICAL();\r
235         {\r
236                 /* Is there space to write directly to the UART? */\r
237                 if( lTHREEmpty == ( long ) pdTRUE )\r
238                 {\r
239                         /* We wrote the character directly to the UART, so was \r
240                         successful. */\r
241                         lTHREEmpty = pdFALSE;\r
242                         U1THR = cOutChar;\r
243                         xReturn = pdPASS;\r
244                 }\r
245                 else \r
246                 {\r
247                         /* We cannot write directly to the UART, so queue the character.\r
248                         Block for a maximum of xBlockTime if there is no space in the\r
249                         queue.  It is ok to block within a critical section as each\r
250                         task has it's own critical section management. */\r
251                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
252 \r
253                         /* Depending on queue sizing and task prioritisation:  While we \r
254                         were blocked waiting to post interrupts were not disabled.  It is \r
255                         possible that the serial ISR has emptied the Tx queue, in which\r
256                         case we need to start the Tx off again. */\r
257                         if( lTHREEmpty == ( long ) pdTRUE )\r
258                         {\r
259                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
260                                 lTHREEmpty = pdFALSE;\r
261                                 U1THR = cOutChar;\r
262                         }\r
263                 }\r
264         }\r
265         portEXIT_CRITICAL();\r
266 \r
267         return xReturn;\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 void vUART_ISRHandler( void )\r
272 {\r
273 signed char cChar;\r
274 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
275 unsigned char ucInterrupt;\r
276 \r
277         ucInterrupt = U1IIR;\r
278 \r
279         /* The interrupt pending bit is active low. */\r
280         while( ( ucInterrupt & serINTERRUPT_IS_PENDING ) == 0 )\r
281         {\r
282                 /* What caused the interrupt? */\r
283                 switch( ucInterrupt & serINTERRUPT_SOURCE_MASK )\r
284                 {\r
285                         case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
286                                                                         cChar = U1LSR;\r
287                                                                         break;\r
288         \r
289                         case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
290                                                                         character in the Tx queue, send it now. */\r
291                                                                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
292                                                                         {\r
293                                                                                 U1THR = cChar;\r
294                                                                         }\r
295                                                                         else\r
296                                                                         {\r
297                                                                                 /* There are no further characters \r
298                                                                                 queued to send so we can indicate \r
299                                                                                 that the THRE is available. */\r
300                                                                                 lTHREEmpty = pdTRUE;\r
301                                                                         }\r
302                                                                         break;\r
303         \r
304                         case serSOURCE_RX_TIMEOUT :\r
305                         case serSOURCE_RX       :       /* A character was received.  Place it in \r
306                                                                         the queue of received characters. */\r
307                                                                         cChar = U1RBR;\r
308                                                                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
309                                                                         break;\r
310         \r
311                         default                         :       /* There is nothing to do, leave the ISR. */\r
312                                                                         break;\r
313                 }\r
314 \r
315                 ucInterrupt = U1IIR;\r
316         }\r
317 \r
318         /* Clear the ISR in the VIC. */\r
319         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
320 \r
321         /* Exit the ISR.  If a task was woken by either a character being received\r
322         or transmitted then a context switch will occur. */\r
323         portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 \r
328 \r
329 \r
330 \r
331 \r
332         \r