]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2129_IAR/serial/serial.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / ARM7_LPC2129_IAR / 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 /*\r
31         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.\r
32 */\r
33 \r
34 /* Standard includes. */\r
35 #include <stdlib.h>\r
36 \r
37 /* Scheduler includes. */\r
38 #include "FreeRTOS.h"\r
39 #include "queue.h"\r
40 #include "task.h"\r
41 \r
42 /* Demo application includes. */\r
43 #include "serial.h"\r
44 \r
45 /*-----------------------------------------------------------*/\r
46 \r
47 /* Constants to setup and access the UART. */\r
48 #define serDLAB                                                 ( ( unsigned char ) 0x80 )\r
49 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )\r
50 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )\r
51 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )\r
52 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )\r
53 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )\r
54 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )\r
55 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )\r
56 \r
57 /* Constants to setup and access the VIC. */\r
58 #define serU0VIC_CHANNEL                                ( ( unsigned long ) 0x0006 )\r
59 #define serU0VIC_CHANNEL_BIT                    ( ( unsigned long ) 0x0040 )\r
60 #define serU0VIC_ENABLE                                 ( ( unsigned long ) 0x0020 )\r
61 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
62 \r
63 /* Constants to determine the ISR source. */\r
64 #define serSOURCE_THRE                                  ( ( unsigned char ) 0x02 )\r
65 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned char ) 0x0c )\r
66 #define serSOURCE_ERROR                                 ( ( unsigned char ) 0x06 )\r
67 #define serSOURCE_RX                                    ( ( unsigned char ) 0x04 )\r
68 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned char ) 0x0f )\r
69 \r
70 /* Misc. */\r
71 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
72 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
73 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /* Queues used to hold received characters, and characters waiting to be\r
78 transmitted. */\r
79 static QueueHandle_t xRxedChars;\r
80 static QueueHandle_t xCharsForTx;\r
81 static volatile long lTHREEmpty = pdFALSE;\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /* The ISR.  Note that this is called by a wrapper written in the file\r
86 SerialISR.s79.  See the WEB documentation for this port for further\r
87 information. */\r
88 __arm void vSerialISR( void );\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
93 {\r
94 unsigned long ulDivisor, ulWantedClock;\r
95 xComPortHandle xReturn = serHANDLE;\r
96 extern void ( vSerialISREntry) ( void );\r
97 \r
98         /* Create the queues used to hold Rx and Tx characters. */\r
99         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
100         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
101 \r
102         /* Initialise the THRE empty flag. */\r
103         lTHREEmpty = pdTRUE;\r
104 \r
105         if(\r
106                 ( xRxedChars != serINVALID_QUEUE ) &&\r
107                 ( xCharsForTx != serINVALID_QUEUE ) &&\r
108                 ( ulWantedBaud != ( unsigned long ) 0 )\r
109           )\r
110         {\r
111                 portENTER_CRITICAL();\r
112                 {\r
113                         /* Setup the baud rate:  Calculate the divisor value. */\r
114                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
115                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
116 \r
117                         /* Set the DLAB bit so we can access the divisor. */\r
118                         U0LCR |= serDLAB;\r
119 \r
120                         /* Setup the divisor. */\r
121                         U0DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
122                         ulDivisor >>= 8;\r
123                         U0DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
124 \r
125                         /* Turn on the FIFO's and clear the buffers. */\r
126                         U0FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
127 \r
128                         /* Setup transmission format. */\r
129                         U0LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
130 \r
131                         /* Setup the VIC for the UART. */\r
132                         VICIntSelect &= ~( serU0VIC_CHANNEL_BIT );\r
133                         VICIntEnable |= serU0VIC_CHANNEL_BIT;\r
134                         VICVectAddr1 = ( unsigned long ) vSerialISREntry;\r
135                         VICVectCntl1 = serU0VIC_CHANNEL | serU0VIC_ENABLE;\r
136 \r
137                         /* Enable UART0 interrupts. */\r
138                         U0IER |= serENABLE_INTERRUPTS;\r
139                 }\r
140                 portEXIT_CRITICAL();\r
141 \r
142                 xReturn = ( xComPortHandle ) 1;\r
143         }\r
144         else\r
145         {\r
146                 xReturn = ( xComPortHandle ) 0;\r
147         }\r
148 \r
149         return xReturn;\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
154 {\r
155         /* The port handle is not required as this driver only supports UART0. */\r
156         ( void ) pxPort;\r
157 \r
158         /* Get the next character from the buffer.  Return false if no characters\r
159         are available, or arrive before xBlockTime expires. */\r
160         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
161         {\r
162                 return pdTRUE;\r
163         }\r
164         else\r
165         {\r
166                 return pdFALSE;\r
167         }\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
172 {\r
173 signed char *pxNext;\r
174 \r
175         /* NOTE: This implementation does not handle the queue being full as no\r
176         block time is used! */\r
177 \r
178         /* The port handle is not required as this driver only supports UART0. */\r
179         ( void ) pxPort;\r
180         ( void ) usStringLength;\r
181 \r
182         /* Send each character in the string, one at a time. */\r
183         pxNext = ( signed char * ) pcString;\r
184         while( *pxNext )\r
185         {\r
186                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
187                 pxNext++;\r
188         }\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
193 {\r
194 signed portBASE_TYPE xReturn;\r
195 \r
196         /* The port handle is not required as this driver only supports UART0. */\r
197         ( void ) pxPort;\r
198 \r
199         portENTER_CRITICAL();\r
200         {\r
201                 /* Is there space to write directly to the UART? */\r
202                 if( lTHREEmpty == ( long ) pdTRUE )\r
203                 {\r
204                         /* We wrote the character directly to the UART, so was\r
205                         successful. */\r
206                         lTHREEmpty = pdFALSE;\r
207                         U0THR = cOutChar;\r
208                         xReturn = pdPASS;\r
209                 }\r
210                 else\r
211                 {\r
212                         /* We cannot write directly to the UART, so queue the character.\r
213                         Block for a maximum of xBlockTime if there is no space in the\r
214                         queue.  It is ok to block within a critical section as each\r
215                         task has it's own critical section management. */\r
216                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
217 \r
218                         /* Depending on queue sizing and task prioritisation:  While we\r
219                         were blocked waiting to post interrupts were not disabled.  It is\r
220                         possible that the serial ISR has emptied the Tx queue, in which\r
221                         case we need to start the Tx off again. */\r
222                         if( lTHREEmpty == ( long ) pdTRUE )\r
223                         {\r
224                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
225                                 lTHREEmpty = pdFALSE;\r
226                                 U0THR = cOutChar;\r
227                         }\r
228                 }\r
229         }\r
230         portEXIT_CRITICAL();\r
231 \r
232         return xReturn;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 __arm void vSerialISR( void )\r
237 {\r
238 signed char cChar;\r
239 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
240 \r
241         /* What caused the interrupt? */\r
242         switch( U0IIR & serINTERRUPT_SOURCE_MASK )\r
243         {\r
244                 case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
245                                                                 cChar = U0LSR;\r
246                                                                 break;\r
247 \r
248                 case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
249                                                                 character in the Tx queue, send it now. */\r
250                                                                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
251                                                                 {\r
252                                                                         U0THR = cChar;\r
253                                                                 }\r
254                                                                 else\r
255                                                                 {\r
256                                                                         /* There are no further characters\r
257                                                                         queued to send so we can indicate\r
258                                                                         that the THRE is available. */\r
259                                                                         lTHREEmpty = pdTRUE;\r
260                                                                 }\r
261                                                                 break;\r
262 \r
263                 case serSOURCE_RX_TIMEOUT :\r
264                 case serSOURCE_RX       :       /* A character was received.  Place it in\r
265                                                                 the queue of received characters. */\r
266                                                                 cChar = U0RBR;\r
267                                                                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
268                                                                 break;\r
269 \r
270                 default                         :       /* There is nothing to do, leave the ISR. */\r
271                                                                 break;\r
272         }\r
273 \r
274         /* Exit the ISR.  If a task was woken by either a character being received\r
275         or transmitted then a context switch will occur. */\r
276         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
277 \r
278         /* Clear the ISR in the VIC. */\r
279         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
280 }\r
281 /*-----------------------------------------------------------*/\r