]> git.sur5r.net Git - freertos/blob - Demo/ARM7_LPC2106_GCC/serial/serial.c
Update to V5.4.1
[freertos] / Demo / ARM7_LPC2106_GCC / serial / serial.c
1 /*\r
2         FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         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 without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /*\r
49         Changes from V2.4.0\r
50 \r
51                 + Made serial ISR handling more complete and robust.\r
52 \r
53         Changes from V2.4.1\r
54 \r
55                 + Split serial.c into serial.c and serialISR.c.  serial.c can be \r
56                   compiled using ARM or THUMB modes.  serialISR.c must always be\r
57                   compiled in ARM mode.\r
58                 + Another small change to cSerialPutChar().\r
59 \r
60         Changed from V2.5.1\r
61 \r
62                 + In cSerialPutChar() an extra check is made to ensure the post to\r
63                   the queue was successful if then attempting to retrieve the posted\r
64                   character.\r
65 \r
66 */\r
67 \r
68 /* \r
69         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
70 \r
71         This file contains all the serial port components that can be compiled to\r
72         either ARM or THUMB mode.  Components that must be compiled to ARM mode are\r
73         contained in serialISR.c.\r
74 */\r
75 \r
76 /* Standard includes. */\r
77 #include <stdlib.h>\r
78 \r
79 /* Scheduler includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "queue.h"\r
82 #include "task.h"\r
83 \r
84 /* Demo application includes. */\r
85 #include "serial.h"\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 /* Constants to setup and access the UART. */\r
90 #define serDLAB                                                 ( ( unsigned portCHAR ) 0x80 )\r
91 #define serENABLE_INTERRUPTS                    ( ( unsigned portCHAR ) 0x03 )\r
92 #define serNO_PARITY                                    ( ( unsigned portCHAR ) 0x00 )\r
93 #define ser1_STOP_BIT                                   ( ( unsigned portCHAR ) 0x00 )\r
94 #define ser8_BIT_CHARS                                  ( ( unsigned portCHAR ) 0x03 )\r
95 #define serFIFO_ON                                              ( ( unsigned portCHAR ) 0x01 )\r
96 #define serCLEAR_FIFO                                   ( ( unsigned portCHAR ) 0x06 )\r
97 #define serWANTED_CLOCK_SCALING                 ( ( unsigned portLONG ) 16 )\r
98 \r
99 /* Constants to setup and access the VIC. */\r
100 #define serUART0_VIC_CHANNEL                    ( ( unsigned portLONG ) 0x0006 )\r
101 #define serUART0_VIC_CHANNEL_BIT                ( ( unsigned portLONG ) 0x0040 )\r
102 #define serUART0_VIC_ENABLE                             ( ( unsigned portLONG ) 0x0020 )\r
103 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned portLONG ) 0 )\r
104 \r
105 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
106 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
107 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* Queues used to hold received characters, and characters waiting to be\r
112 transmitted. */\r
113 static xQueueHandle xRxedChars; \r
114 static xQueueHandle xCharsForTx; \r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /* Communication flag between the interrupt service routine and serial API. */\r
119 static volatile portLONG *plTHREEmpty;\r
120 \r
121 /* \r
122  * The queues are created in serialISR.c as they are used from the ISR.\r
123  * Obtain references to the queues and THRE Empty flag. \r
124  */\r
125 extern void vSerialISRCreateQueues(     unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx, portLONG volatile **pplTHREEmptyFlag );\r
126 \r
127 /*-----------------------------------------------------------*/\r
128 \r
129 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
130 {\r
131 unsigned portLONG ulDivisor, ulWantedClock;\r
132 xComPortHandle xReturn = serHANDLE;\r
133 extern void ( vUART_ISR_Wrapper )( void );\r
134 \r
135         /* The queues are used in the serial ISR routine, so are created from\r
136         serialISR.c (which is always compiled to ARM mode. */\r
137         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );\r
138 \r
139         if( \r
140                 ( xRxedChars != serINVALID_QUEUE ) && \r
141                 ( xCharsForTx != serINVALID_QUEUE ) && \r
142                 ( ulWantedBaud != ( unsigned portLONG ) 0 ) \r
143           )\r
144         {\r
145                 portENTER_CRITICAL();\r
146                 {\r
147                         /* Setup the baud rate:  Calculate the divisor value. */\r
148                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
149                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
150 \r
151                         /* Set the DLAB bit so we can access the divisor. */\r
152                         UART0_LCR |= serDLAB;\r
153 \r
154                         /* Setup the divisor. */\r
155                         UART0_DLL = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );\r
156                         ulDivisor >>= 8;\r
157                         UART0_DLM = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );\r
158 \r
159                         /* Turn on the FIFO's and clear the buffers. */\r
160                         UART0_FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
161 \r
162                         /* Setup transmission format. */\r
163                         UART0_LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
164 \r
165                         /* Setup the VIC for the UART. */\r
166                         VICIntSelect &= ~( serUART0_VIC_CHANNEL_BIT );\r
167                         VICIntEnable |= serUART0_VIC_CHANNEL_BIT;\r
168                         VICVectAddr1 = ( portLONG ) vUART_ISR_Wrapper;\r
169                         VICVectCntl1 = serUART0_VIC_CHANNEL | serUART0_VIC_ENABLE;\r
170 \r
171                         /* Enable UART0 interrupts. */\r
172                         UART0_IER |= serENABLE_INTERRUPTS;\r
173                 }\r
174                 portEXIT_CRITICAL();\r
175         }\r
176         else\r
177         {\r
178                 xReturn = ( xComPortHandle ) 0;\r
179         }\r
180 \r
181         return xReturn;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
186 {\r
187         /* The port handle is not required as this driver only supports UART0. */\r
188         ( void ) pxPort;\r
189 \r
190         /* Get the next character from the buffer.  Return false if no characters\r
191         are available, or arrive before xBlockTime expires. */\r
192         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
193         {\r
194                 return pdTRUE;\r
195         }\r
196         else\r
197         {\r
198                 return pdFALSE;\r
199         }\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )\r
204 {\r
205 signed portCHAR *pxNext;\r
206 \r
207         /* NOTE: This implementation does not handle the queue being full as no\r
208         block time is used! */\r
209 \r
210         /* The port handle is not required as this driver only supports UART0. */\r
211         ( void ) pxPort;\r
212         ( void ) usStringLength;\r
213 \r
214         /* Send each character in the string, one at a time. */\r
215         pxNext = ( signed portCHAR * ) pcString;\r
216         while( *pxNext )\r
217         {\r
218                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
219                 pxNext++;\r
220         }\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
225 {\r
226 signed portBASE_TYPE xReturn;\r
227 \r
228         /* This demo driver only supports one port so the parameter is not used. */\r
229         ( void ) pxPort;\r
230 \r
231         portENTER_CRITICAL();\r
232         {\r
233                 /* Is there space to write directly to the UART? */\r
234                 if( *plTHREEmpty == ( portLONG ) pdTRUE )\r
235                 {\r
236                         /* We wrote the character directly to the UART, so was \r
237                         successful. */\r
238                         *plTHREEmpty = pdFALSE;\r
239                         UART0_THR = cOutChar;\r
240                         xReturn = pdPASS;\r
241                 }\r
242                 else \r
243                 {\r
244                         /* We cannot write directly to the UART, so queue the character.\r
245                         Block for a maximum of xBlockTime if there is no space in the\r
246                         queue. */\r
247                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
248 \r
249                         /* Depending on queue sizing and task prioritisation:  While we \r
250                         were blocked waiting to post interrupts were not disabled.  It is \r
251                         possible that the serial ISR has emptied the Tx queue, in which\r
252                         case we need to start the Tx off again. */\r
253                         if( ( *plTHREEmpty == ( portLONG ) pdTRUE ) && ( xReturn == pdPASS ) )\r
254                         {\r
255                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
256                                 *plTHREEmpty = pdFALSE;\r
257                                 UART0_THR = cOutChar;\r
258                         }\r
259                 }\r
260         }\r
261         portEXIT_CRITICAL();\r
262 \r
263         return xReturn;\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 void vSerialClose( xComPortHandle xPort )\r
268 {\r
269         /* Not supported as not required by the demo application. */\r
270         ( void ) xPort;\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 \r
275 \r
276 \r
277 \r
278         \r