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