]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2129_IAR/serial/serial.c
Update version number ready to release the FAT file system demo.
[freertos] / FreeRTOS / Demo / ARM7_LPC2129_IAR / serial / serial.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 \r
76 /*\r
77         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.\r
78 */\r
79 \r
80 /* Standard includes. */\r
81 #include <stdlib.h>\r
82 \r
83 /* Scheduler includes. */\r
84 #include "FreeRTOS.h"\r
85 #include "queue.h"\r
86 #include "task.h"\r
87 \r
88 /* Demo application includes. */\r
89 #include "serial.h"\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* Constants to setup and access the UART. */\r
94 #define serDLAB                                                 ( ( unsigned char ) 0x80 )\r
95 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )\r
96 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )\r
97 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )\r
98 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )\r
99 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )\r
100 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )\r
101 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )\r
102 \r
103 /* Constants to setup and access the VIC. */\r
104 #define serU0VIC_CHANNEL                                ( ( unsigned long ) 0x0006 )\r
105 #define serU0VIC_CHANNEL_BIT                    ( ( unsigned long ) 0x0040 )\r
106 #define serU0VIC_ENABLE                                 ( ( unsigned long ) 0x0020 )\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 \r
116 /* Misc. */\r
117 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
118 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
119 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /* Queues used to hold received characters, and characters waiting to be\r
124 transmitted. */\r
125 static xQueueHandle xRxedChars;\r
126 static xQueueHandle xCharsForTx;\r
127 static volatile long lTHREEmpty = pdFALSE;\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /* The ISR.  Note that this is called by a wrapper written in the file\r
132 SerialISR.s79.  See the WEB documentation for this port for further\r
133 information. */\r
134 __arm void vSerialISR( void );\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
139 {\r
140 unsigned long ulDivisor, ulWantedClock;\r
141 xComPortHandle xReturn = serHANDLE;\r
142 extern void ( vSerialISREntry) ( void );\r
143 \r
144         /* Create the queues used to hold Rx and Tx characters. */\r
145         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
146         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
147 \r
148         /* Initialise the THRE empty flag. */\r
149         lTHREEmpty = pdTRUE;\r
150 \r
151         if(\r
152                 ( xRxedChars != serINVALID_QUEUE ) &&\r
153                 ( xCharsForTx != serINVALID_QUEUE ) &&\r
154                 ( ulWantedBaud != ( unsigned long ) 0 )\r
155           )\r
156         {\r
157                 portENTER_CRITICAL();\r
158                 {\r
159                         /* Setup the baud rate:  Calculate the divisor value. */\r
160                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
161                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
162 \r
163                         /* Set the DLAB bit so we can access the divisor. */\r
164                         U0LCR |= serDLAB;\r
165 \r
166                         /* Setup the divisor. */\r
167                         U0DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
168                         ulDivisor >>= 8;\r
169                         U0DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
170 \r
171                         /* Turn on the FIFO's and clear the buffers. */\r
172                         U0FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
173 \r
174                         /* Setup transmission format. */\r
175                         U0LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
176 \r
177                         /* Setup the VIC for the UART. */\r
178                         VICIntSelect &= ~( serU0VIC_CHANNEL_BIT );\r
179                         VICIntEnable |= serU0VIC_CHANNEL_BIT;\r
180                         VICVectAddr1 = ( unsigned long ) vSerialISREntry;\r
181                         VICVectCntl1 = serU0VIC_CHANNEL | serU0VIC_ENABLE;\r
182 \r
183                         /* Enable UART0 interrupts. */\r
184                         U0IER |= serENABLE_INTERRUPTS;\r
185                 }\r
186                 portEXIT_CRITICAL();\r
187 \r
188                 xReturn = ( xComPortHandle ) 1;\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                         U0THR = 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                                 U0THR = 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 __arm void vSerialISR( void )\r
283 {\r
284 signed char cChar;\r
285 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
286 \r
287         /* What caused the interrupt? */\r
288         switch( U0IIR & serINTERRUPT_SOURCE_MASK )\r
289         {\r
290                 case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
291                                                                 cChar = U0LSR;\r
292                                                                 break;\r
293 \r
294                 case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
295                                                                 character in the Tx queue, send it now. */\r
296                                                                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
297                                                                 {\r
298                                                                         U0THR = cChar;\r
299                                                                 }\r
300                                                                 else\r
301                                                                 {\r
302                                                                         /* There are no further characters\r
303                                                                         queued to send so we can indicate\r
304                                                                         that the THRE is available. */\r
305                                                                         lTHREEmpty = pdTRUE;\r
306                                                                 }\r
307                                                                 break;\r
308 \r
309                 case serSOURCE_RX_TIMEOUT :\r
310                 case serSOURCE_RX       :       /* A character was received.  Place it in\r
311                                                                 the queue of received characters. */\r
312                                                                 cChar = U0RBR;\r
313                                                                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
314                                                                 break;\r
315 \r
316                 default                         :       /* There is nothing to do, leave the ISR. */\r
317                                                                 break;\r
318         }\r
319 \r
320         /* Exit the ISR.  If a task was woken by either a character being received\r
321         or transmitted then a context switch will occur. */\r
322         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
323 \r
324         /* Clear the ISR in the VIC. */\r
325         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
326 }\r
327 /*-----------------------------------------------------------*/\r