]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2106_GCC/serial/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / ARM7_LPC2106_GCC / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30         Changes from V2.4.0\r
31 \r
32                 + Made serial ISR handling more complete and robust.\r
33 \r
34         Changes from V2.4.1\r
35 \r
36                 + Split serial.c into serial.c and serialISR.c.  serial.c can be \r
37                   compiled using ARM or THUMB modes.  serialISR.c must always be\r
38                   compiled in ARM mode.\r
39                 + Another small change to cSerialPutChar().\r
40 \r
41         Changed from V2.5.1\r
42 \r
43                 + In cSerialPutChar() an extra check is made to ensure the post to\r
44                   the queue was successful if then attempting to retrieve the posted\r
45                   character.\r
46 \r
47 */\r
48 \r
49 /* \r
50         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
51 \r
52         This file contains all the serial port components that can be compiled to\r
53         either ARM or THUMB mode.  Components that must be compiled to ARM mode are\r
54         contained in serialISR.c.\r
55 */\r
56 \r
57 /* Standard includes. */\r
58 #include <stdlib.h>\r
59 \r
60 /* Scheduler includes. */\r
61 #include "FreeRTOS.h"\r
62 #include "queue.h"\r
63 #include "task.h"\r
64 \r
65 /* Demo application includes. */\r
66 #include "serial.h"\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /* Constants to setup and access the UART. */\r
71 #define serDLAB                                                 ( ( unsigned char ) 0x80 )\r
72 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )\r
73 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )\r
74 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )\r
75 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )\r
76 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )\r
77 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )\r
78 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )\r
79 \r
80 /* Constants to setup and access the VIC. */\r
81 #define serUART0_VIC_CHANNEL                    ( ( unsigned long ) 0x0006 )\r
82 #define serUART0_VIC_CHANNEL_BIT                ( ( unsigned long ) 0x0040 )\r
83 #define serUART0_VIC_ENABLE                             ( ( unsigned long ) 0x0020 )\r
84 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
85 \r
86 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
87 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
88 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /* Queues used to hold received characters, and characters waiting to be\r
93 transmitted. */\r
94 static QueueHandle_t xRxedChars; \r
95 static QueueHandle_t xCharsForTx; \r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* Communication flag between the interrupt service routine and serial API. */\r
100 static volatile long *plTHREEmpty;\r
101 \r
102 /* \r
103  * The queues are created in serialISR.c as they are used from the ISR.\r
104  * Obtain references to the queues and THRE Empty flag. \r
105  */\r
106 extern void vSerialISRCreateQueues(     unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag );\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
111 {\r
112 unsigned long ulDivisor, ulWantedClock;\r
113 xComPortHandle xReturn = serHANDLE;\r
114 extern void ( vUART_ISR_Wrapper )( void );\r
115 \r
116         /* The queues are used in the serial ISR routine, so are created from\r
117         serialISR.c (which is always compiled to ARM mode. */\r
118         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );\r
119 \r
120         if( \r
121                 ( xRxedChars != serINVALID_QUEUE ) && \r
122                 ( xCharsForTx != serINVALID_QUEUE ) && \r
123                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
124           )\r
125         {\r
126                 portENTER_CRITICAL();\r
127                 {\r
128                         /* Setup the baud rate:  Calculate the divisor value. */\r
129                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
130                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
131 \r
132                         /* Set the DLAB bit so we can access the divisor. */\r
133                         UART0_LCR |= serDLAB;\r
134 \r
135                         /* Setup the divisor. */\r
136                         UART0_DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
137                         ulDivisor >>= 8;\r
138                         UART0_DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
139 \r
140                         /* Turn on the FIFO's and clear the buffers. */\r
141                         UART0_FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
142 \r
143                         /* Setup transmission format. */\r
144                         UART0_LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
145 \r
146                         /* Setup the VIC for the UART. */\r
147                         VICIntSelect &= ~( serUART0_VIC_CHANNEL_BIT );\r
148                         VICIntEnable |= serUART0_VIC_CHANNEL_BIT;\r
149                         VICVectAddr1 = ( long ) vUART_ISR_Wrapper;\r
150                         VICVectCntl1 = serUART0_VIC_CHANNEL | serUART0_VIC_ENABLE;\r
151 \r
152                         /* Enable UART0 interrupts. */\r
153                         UART0_IER |= serENABLE_INTERRUPTS;\r
154                 }\r
155                 portEXIT_CRITICAL();\r
156         }\r
157         else\r
158         {\r
159                 xReturn = ( xComPortHandle ) 0;\r
160         }\r
161 \r
162         return xReturn;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
167 {\r
168         /* The port handle is not required as this driver only supports UART0. */\r
169         ( void ) pxPort;\r
170 \r
171         /* Get the next character from the buffer.  Return false if no characters\r
172         are available, or arrive before xBlockTime expires. */\r
173         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
174         {\r
175                 return pdTRUE;\r
176         }\r
177         else\r
178         {\r
179                 return pdFALSE;\r
180         }\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
185 {\r
186 signed char *pxNext;\r
187 \r
188         /* NOTE: This implementation does not handle the queue being full as no\r
189         block time is used! */\r
190 \r
191         /* The port handle is not required as this driver only supports UART0. */\r
192         ( void ) pxPort;\r
193         ( void ) usStringLength;\r
194 \r
195         /* Send each character in the string, one at a time. */\r
196         pxNext = ( signed char * ) pcString;\r
197         while( *pxNext )\r
198         {\r
199                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
200                 pxNext++;\r
201         }\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
206 {\r
207 signed portBASE_TYPE xReturn;\r
208 \r
209         /* This demo driver only supports one port so the parameter is not used. */\r
210         ( void ) pxPort;\r
211 \r
212         portENTER_CRITICAL();\r
213         {\r
214                 /* Is there space to write directly to the UART? */\r
215                 if( *plTHREEmpty == ( long ) pdTRUE )\r
216                 {\r
217                         /* We wrote the character directly to the UART, so was \r
218                         successful. */\r
219                         *plTHREEmpty = pdFALSE;\r
220                         UART0_THR = cOutChar;\r
221                         xReturn = pdPASS;\r
222                 }\r
223                 else \r
224                 {\r
225                         /* We cannot write directly to the UART, so queue the character.\r
226                         Block for a maximum of xBlockTime if there is no space in the\r
227                         queue. */\r
228                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
229 \r
230                         /* Depending on queue sizing and task prioritisation:  While we \r
231                         were blocked waiting to post interrupts were not disabled.  It is \r
232                         possible that the serial ISR has emptied the Tx queue, in which\r
233                         case we need to start the Tx off again. */\r
234                         if( ( *plTHREEmpty == ( long ) pdTRUE ) && ( xReturn == pdPASS ) )\r
235                         {\r
236                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
237                                 *plTHREEmpty = pdFALSE;\r
238                                 UART0_THR = cOutChar;\r
239                         }\r
240                 }\r
241         }\r
242         portEXIT_CRITICAL();\r
243 \r
244         return xReturn;\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 void vSerialClose( xComPortHandle xPort )\r
249 {\r
250         /* Not supported as not required by the demo application. */\r
251         ( void ) xPort;\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 \r
256 \r
257 \r
258 \r
259         \r