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