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