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