]> begriffs open source - freertos/blob - 20080217/Demo/ARM7_LPC2129_Keil/serial/serial.c
Prepare Fujitsu ports for release.
[freertos] / 20080217 / Demo / ARM7_LPC2129_Keil / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 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\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27 \r
28         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 \r
44 /* \r
45         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
46 \r
47         This file contains all the serial port components that can be compiled to\r
48         either ARM or THUMB mode.  Components that must be compiled to ARM mode are\r
49         contained in serialISR.c.\r
50 */\r
51 \r
52 /* Standard includes. */\r
53 #include <stdlib.h>\r
54 \r
55 /* Scheduler includes. */\r
56 #include "FreeRTOS.h"\r
57 #include "queue.h"\r
58 #include "task.h"\r
59 \r
60 /* Demo application includes. */\r
61 #include "serial.h"\r
62 \r
63 /*-----------------------------------------------------------*/\r
64 \r
65 /* Constants to setup and access the UART. */\r
66 #define serDLAB                                                 ( ( unsigned portCHAR ) 0x80 )\r
67 #define serENABLE_INTERRUPTS                    ( ( unsigned portCHAR ) 0x03 )\r
68 #define serNO_PARITY                                    ( ( unsigned portCHAR ) 0x00 )\r
69 #define ser1_STOP_BIT                                   ( ( unsigned portCHAR ) 0x00 )\r
70 #define ser8_BIT_CHARS                                  ( ( unsigned portCHAR ) 0x03 )\r
71 #define serFIFO_ON                                              ( ( unsigned portCHAR ) 0x01 )\r
72 #define serCLEAR_FIFO                                   ( ( unsigned portCHAR ) 0x06 )\r
73 #define serWANTED_CLOCK_SCALING                 ( ( unsigned portLONG ) 16 )\r
74 \r
75 /* Constants to setup and access the VIC. */\r
76 #define serU0VIC_CHANNEL                                ( ( unsigned portLONG ) 0x0006 )\r
77 #define serU0VIC_CHANNEL_BIT                    ( ( unsigned portLONG ) 0x0040 )\r
78 #define serU0VIC_ENABLE                                 ( ( unsigned portLONG ) 0x0020 )\r
79 \r
80 /* Misc. */\r
81 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
82 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
83 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /* Queues used to hold received characters, and characters waiting to be\r
88 transmitted. */\r
89 static xQueueHandle xRxedChars; \r
90 static xQueueHandle xCharsForTx; \r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* Communication flag between the interrupt service routine and serial API. */\r
95 static volatile portLONG *plTHREEmpty;\r
96 \r
97 /* \r
98  * The queues are created in serialISR.c as they are used from the ISR.\r
99  * Obtain references to the queues and THRE Empty flag. \r
100  */\r
101 extern void vSerialISRCreateQueues(     unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx, portLONG volatile **pplTHREEmptyFlag );\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
106 {\r
107 unsigned portLONG ulDivisor, ulWantedClock;\r
108 xComPortHandle xReturn = serHANDLE;\r
109 \r
110         /* The queues are used in the serial ISR routine, so are created from\r
111         serialISR.c (which is always compiled to ARM mode). */\r
112         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );\r
113 \r
114         if( \r
115                 ( xRxedChars != serINVALID_QUEUE ) && \r
116                 ( xCharsForTx != serINVALID_QUEUE ) && \r
117                 ( ulWantedBaud != ( unsigned portLONG ) 0 ) \r
118           )\r
119         {\r
120                 portENTER_CRITICAL();\r
121                 {\r
122                         /* The reference to the ISR function is required to load into the \r
123                         interrupt controller.  The prototype is slightly different \r
124                         depending on whether in ARM or THUMB mode. */\r
125                         #ifdef KEIL_THUMB_INTERWORK\r
126                                 extern void ( vUART_ISR )( void ) __arm __task;\r
127                         #else\r
128                                 extern void ( vUART_ISR )( void ) __task;\r
129                         #endif\r
130 \r
131                         /* Setup the baud rate:  Calculate the divisor value. */\r
132                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
133                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
134 \r
135                         /* Set the DLAB bit so we can access the divisor. */\r
136                         U0LCR |= serDLAB;\r
137 \r
138                         /* Setup the divisor. */\r
139                         U0DLL = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );\r
140                         ulDivisor >>= 8;\r
141                         U0DLM = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );\r
142 \r
143                         /* Turn on the FIFO's and clear the buffers. */\r
144                         U0FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
145 \r
146                         /* Setup transmission format. */\r
147                         U0LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
148 \r
149                         /* Setup the VIC for the UART. */\r
150                         VICIntSelect &= ~( serU0VIC_CHANNEL_BIT );\r
151                         VICIntEnable |= serU0VIC_CHANNEL_BIT;\r
152                         VICVectAddr1 = ( unsigned portLONG ) vUART_ISR;\r
153                         VICVectCntl1 = serU0VIC_CHANNEL | serU0VIC_ENABLE;\r
154 \r
155                         /* Enable UART0 interrupts. */\r
156                         U0IER |= serENABLE_INTERRUPTS;\r
157                 }\r
158                 portEXIT_CRITICAL();\r
159         }\r
160         else\r
161         {\r
162                 xReturn = ( xComPortHandle ) 0;\r
163         }\r
164 \r
165         return xReturn;\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
170 {\r
171         /* The port handle is not required as this driver only supports UART0. */\r
172         ( void ) pxPort;\r
173 \r
174         /* Get the next character from the buffer.  Return false if no characters\r
175         are available, or arrive before xBlockTime expires. */\r
176         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
177         {\r
178                 return pdTRUE;\r
179         }\r
180         else\r
181         {\r
182                 return pdFALSE;\r
183         }\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )\r
188 {\r
189 signed portCHAR *pxNext;\r
190 \r
191         /* NOTE: This implementation does not handle the queue being full as no\r
192         block time is used! */\r
193 \r
194         /* The port handle is not required as this driver only supports UART0. */\r
195         ( void ) pxPort;\r
196         ( void ) usStringLength;\r
197 \r
198         /* Send each character in the string, one at a time. */\r
199         pxNext = ( signed portCHAR * ) pcString;\r
200         while( *pxNext )\r
201         {\r
202                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
203                 pxNext++;\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
209 {\r
210 signed portBASE_TYPE xReturn;\r
211 \r
212         /* The port handle is not required as this driver only supports UART0. */\r
213         ( void ) pxPort;\r
214 \r
215         portENTER_CRITICAL();\r
216         {\r
217                 /* Is there space to write directly to the UART? */\r
218                 if( *plTHREEmpty == ( portLONG ) pdTRUE )\r
219                 {\r
220                         /* We wrote the character directly to the UART, so was \r
221                         successful. */\r
222                         *plTHREEmpty = pdFALSE;\r
223                         U0THR = cOutChar;\r
224                         xReturn = pdPASS;\r
225                 }\r
226                 else \r
227                 {\r
228                         /* We cannot write directly to the UART, so queue the character.\r
229                         Block for a maximum of xBlockTime if there is no space in the\r
230                         queue.  It is ok to block within a critical section as each\r
231                         task has it's own critical section management. */\r
232                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
233 \r
234                         /* Depending on queue sizing and task prioritisation:  While we \r
235                         were blocked waiting to post interrupts were not disabled.  It is \r
236                         possible that the serial ISR has emptied the Tx queue, in which\r
237                         case we need to start the Tx off again. */\r
238                         if( *plTHREEmpty == ( portLONG ) pdTRUE )\r
239                         {\r
240                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
241                                 *plTHREEmpty = pdFALSE;\r
242                                 U0THR = cOutChar;\r
243                         }\r
244                 }\r
245         }\r
246         portEXIT_CRITICAL();\r
247 \r
248         return xReturn;\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 \r
253 \r
254 \r
255 \r
256 \r
257         \r