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