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