]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2129_Keil_RVDS/serial/serial.c
Update MSP432 projects to use updated driver library files.
[freertos] / FreeRTOS / Demo / ARM7_LPC2129_Keil_RVDS / serial / serial.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /* \r
72         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
73 \r
74         Note this driver is used to test the FreeRTOS port.  It is NOT intended to\r
75         be an example of an efficient implementation!\r
76 */\r
77 \r
78 /* Standard includes. */\r
79 #include <stdlib.h>\r
80 \r
81 /* Scheduler includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "queue.h"\r
84 #include "task.h"\r
85 \r
86 /* Demo application includes. */\r
87 #include "serial.h"\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 /* Constants to setup and access the UART. */\r
92 #define serDLAB                                                 ( ( unsigned char ) 0x80 )\r
93 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )\r
94 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )\r
95 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )\r
96 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )\r
97 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )\r
98 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )\r
99 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )\r
100 \r
101 /* Constants to setup and access the VIC. */\r
102 #define serU1VIC_CHANNEL                                ( ( unsigned long ) 0x0007 )\r
103 #define serU1VIC_CHANNEL_BIT                    ( ( unsigned long ) 0x0080 )\r
104 #define serU1VIC_ENABLE                                 ( ( unsigned long ) 0x0020 )\r
105 \r
106 /* Misc. */\r
107 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
108 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
109 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
110 \r
111 /* Constant to access the VIC. */\r
112 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
113 \r
114 /* Constants to determine the ISR source. */\r
115 #define serSOURCE_THRE                                  ( ( unsigned char ) 0x02 )\r
116 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned char ) 0x0c )\r
117 #define serSOURCE_ERROR                                 ( ( unsigned char ) 0x06 )\r
118 #define serSOURCE_RX                                    ( ( unsigned char ) 0x04 )\r
119 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned char ) 0x0f )\r
120 #define serINTERRUPT_IS_PENDING                 ( ( unsigned char ) 0x01 )\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * The asm wrapper for the interrupt service routine.\r
126  */\r
127 extern void vUART_ISREntry( void );\r
128 \r
129 /* \r
130  * The C function called from the asm wrapper. \r
131  */\r
132 void vUART_ISRHandler( void );\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /* Queues used to hold received characters, and characters waiting to be\r
137 transmitted. */\r
138 static QueueHandle_t xRxedChars; \r
139 static QueueHandle_t xCharsForTx; \r
140 \r
141 /* Communication flag between the interrupt service routine and serial API. */\r
142 static volatile long lTHREEmpty;\r
143 \r
144 /*-----------------------------------------------------------*/\r
145 \r
146 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
147 {\r
148 unsigned long ulDivisor, ulWantedClock;\r
149 xComPortHandle xReturn = serHANDLE;\r
150 \r
151         /* Create the queues used to hold Rx and Tx characters. */\r
152         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
153         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
154 \r
155         /* Initialise the THRE empty flag. */\r
156         lTHREEmpty = pdTRUE;\r
157 \r
158         if( \r
159                 ( xRxedChars != serINVALID_QUEUE ) && \r
160                 ( xCharsForTx != serINVALID_QUEUE ) && \r
161                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
162           )\r
163         {\r
164                 portENTER_CRITICAL()\r
165                 {\r
166                         /* Setup the baud rate:  Calculate the divisor value. */\r
167                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;\r
168                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;\r
169 \r
170                         /* Set the DLAB bit so we can access the divisor. */\r
171                         U1LCR |= serDLAB;\r
172 \r
173                         /* Setup the divisor. */\r
174                         U1DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
175                         ulDivisor >>= 8;\r
176                         U1DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );\r
177 \r
178                         /* Turn on the FIFO's and clear the buffers. */\r
179                         U1FCR = ( serFIFO_ON | serCLEAR_FIFO );\r
180 \r
181                         /* Setup transmission format. */\r
182                         U1LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;\r
183 \r
184                         /* Setup the VIC for the UART. */\r
185                         VICIntSelect &= ~( serU1VIC_CHANNEL_BIT );\r
186                         VICIntEnable |= serU1VIC_CHANNEL_BIT;\r
187                         VICVectAddr1 = ( unsigned long ) vUART_ISREntry;\r
188                         VICVectCntl1 = serU1VIC_CHANNEL | serU1VIC_ENABLE;\r
189 \r
190                         /* Enable UART0 interrupts. */\r
191                         U1IER |= serENABLE_INTERRUPTS;\r
192                 }\r
193                 portEXIT_CRITICAL();\r
194         }\r
195         else\r
196         {\r
197                 xReturn = ( xComPortHandle ) 0;\r
198         }\r
199 \r
200         return xReturn;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
205 {\r
206         /* The port handle is not required as this driver only supports UART0. */\r
207         ( void ) pxPort;\r
208 \r
209         /* Get the next character from the buffer.  Return false if no characters\r
210         are available, or arrive before xBlockTime expires. */\r
211         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
212         {\r
213                 return pdTRUE;\r
214         }\r
215         else\r
216         {\r
217                 return pdFALSE;\r
218         }\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
223 {\r
224 signed char *pxNext;\r
225 \r
226         /* NOTE: This implementation does not handle the queue being full as no\r
227         block time is used! */\r
228 \r
229         /* The port handle is not required as this driver only supports UART0. */\r
230         ( void ) pxPort;\r
231         ( void ) usStringLength;\r
232 \r
233         /* Send each character in the string, one at a time. */\r
234         pxNext = ( signed char * ) pcString;\r
235         while( *pxNext )\r
236         {\r
237                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
238                 pxNext++;\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
244 {\r
245 signed portBASE_TYPE xReturn;\r
246 \r
247         /* The port handle is not required as this driver only supports UART0. */\r
248         ( void ) pxPort;\r
249 \r
250         portENTER_CRITICAL();\r
251         {\r
252                 /* Is there space to write directly to the UART? */\r
253                 if( lTHREEmpty == ( long ) pdTRUE )\r
254                 {\r
255                         /* We wrote the character directly to the UART, so was \r
256                         successful. */\r
257                         lTHREEmpty = pdFALSE;\r
258                         U1THR = cOutChar;\r
259                         xReturn = pdPASS;\r
260                 }\r
261                 else \r
262                 {\r
263                         /* We cannot write directly to the UART, so queue the character.\r
264                         Block for a maximum of xBlockTime if there is no space in the\r
265                         queue.  It is ok to block within a critical section as each\r
266                         task has it's own critical section management. */\r
267                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
268 \r
269                         /* Depending on queue sizing and task prioritisation:  While we \r
270                         were blocked waiting to post interrupts were not disabled.  It is \r
271                         possible that the serial ISR has emptied the Tx queue, in which\r
272                         case we need to start the Tx off again. */\r
273                         if( lTHREEmpty == ( long ) pdTRUE )\r
274                         {\r
275                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
276                                 lTHREEmpty = pdFALSE;\r
277                                 U1THR = cOutChar;\r
278                         }\r
279                 }\r
280         }\r
281         portEXIT_CRITICAL();\r
282 \r
283         return xReturn;\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 void vUART_ISRHandler( void )\r
288 {\r
289 signed char cChar;\r
290 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
291 unsigned char ucInterrupt;\r
292 \r
293         ucInterrupt = U1IIR;\r
294 \r
295         /* The interrupt pending bit is active low. */\r
296         while( ( ucInterrupt & serINTERRUPT_IS_PENDING ) == 0 )\r
297         {\r
298                 /* What caused the interrupt? */\r
299                 switch( ucInterrupt & serINTERRUPT_SOURCE_MASK )\r
300                 {\r
301                         case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
302                                                                         cChar = U1LSR;\r
303                                                                         break;\r
304         \r
305                         case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
306                                                                         character in the Tx queue, send it now. */\r
307                                                                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
308                                                                         {\r
309                                                                                 U1THR = cChar;\r
310                                                                         }\r
311                                                                         else\r
312                                                                         {\r
313                                                                                 /* There are no further characters \r
314                                                                                 queued to send so we can indicate \r
315                                                                                 that the THRE is available. */\r
316                                                                                 lTHREEmpty = pdTRUE;\r
317                                                                         }\r
318                                                                         break;\r
319         \r
320                         case serSOURCE_RX_TIMEOUT :\r
321                         case serSOURCE_RX       :       /* A character was received.  Place it in \r
322                                                                         the queue of received characters. */\r
323                                                                         cChar = U1RBR;\r
324                                                                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
325                                                                         break;\r
326         \r
327                         default                         :       /* There is nothing to do, leave the ISR. */\r
328                                                                         break;\r
329                 }\r
330 \r
331                 ucInterrupt = U1IIR;\r
332         }\r
333 \r
334         /* Clear the ISR in the VIC. */\r
335         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
336 \r
337         /* Exit the ISR.  If a task was woken by either a character being received\r
338         or transmitted then a context switch will occur. */\r
339         portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 \r
344 \r
345 \r
346 \r
347 \r
348         \r