]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_IAR/serial/serial.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / msp430_IAR / serial / serial.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 \r
66 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
67  *\r
68  * This file only supports UART 1\r
69  */\r
70 \r
71 /* Standard includes. */\r
72 #include <stdlib.h>\r
73 \r
74 /* Scheduler includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "queue.h"\r
77 #include "task.h"\r
78 \r
79 /* Demo application includes. */\r
80 #include "serial.h"\r
81 \r
82 /* Constants required to setup the hardware. */\r
83 #define serTX_AND_RX                    ( ( unsigned portCHAR ) 0x03 )\r
84 \r
85 /* Misc. constants. */\r
86 #define serNO_BLOCK                             ( ( portTickType ) 0 )\r
87 \r
88 /* Enable the UART Tx interrupt. */\r
89 #define vInterruptOn() IFG2 |= UTXIFG1\r
90 \r
91 /* The queue used to hold received characters. */\r
92 static xQueueHandle xRxedChars;\r
93 \r
94 /* The queue used to hold characters waiting transmission. */\r
95 static xQueueHandle xCharsForTx;\r
96 \r
97 static volatile portSHORT sTHREEmpty;\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
102 {\r
103 unsigned portLONG ulBaudRateCount;\r
104 \r
105         /* Initialise the hardware. */\r
106 \r
107         /* Generate the baud rate constants for the wanted baud rate. */\r
108         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
109 \r
110         portENTER_CRITICAL();\r
111         {\r
112                 /* Create the queues used by the com test task. */\r
113                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
114                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
115 \r
116                 /* Reset UART. */\r
117                 UCTL1 |= SWRST;\r
118 \r
119                 /* Set pin function. */\r
120                 P4SEL |= serTX_AND_RX;\r
121 \r
122                 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.\r
123                 LOOPBACK MODE!*/\r
124                 U1CTL |= CHAR + LISTEN;\r
125                 U1TCTL |= SSEL1;\r
126 \r
127                 /* Setup baud rate low byte. */\r
128                 U1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );\r
129 \r
130                 /* Setup baud rate high byte. */\r
131                 ulBaudRateCount >>= 8UL;\r
132                 U1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );\r
133 \r
134                 /* Enable ports. */\r
135                 ME2 |= UTXE1 + URXE1;\r
136 \r
137                 /* Set. */\r
138                 UCTL1 &= ~SWRST;\r
139 \r
140                 /* Nothing in the buffer yet. */\r
141                 sTHREEmpty = pdTRUE;\r
142 \r
143                 /* Enable interrupts. */\r
144                 IE2 |= URXIE1 + UTXIE1;\r
145         }\r
146         portEXIT_CRITICAL();\r
147         \r
148         /* Unlike other ports, this serial code does not allow for more than one\r
149         com port.  We therefore don't return a pointer to a port structure and can\r
150         instead just return NULL. */\r
151         return NULL;\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\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 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
171 {\r
172 signed portBASE_TYPE xReturn;\r
173 \r
174         /* Transmit a character. */\r
175 \r
176         portENTER_CRITICAL();\r
177         {\r
178                 if( sTHREEmpty == pdTRUE )\r
179                 {\r
180                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that\r
181                         there are no characters queued to be transmitted - so we can\r
182                         write the character directly to the shift Tx register. */\r
183                         sTHREEmpty = pdFALSE;\r
184                         U1TXBUF = cOutChar;\r
185                         xReturn = pdPASS;\r
186                 }\r
187                 else\r
188                 {\r
189                         /* sTHREEmpty is false, so there are still characters waiting to be\r
190                         transmitted.  We have to queue this character so it gets\r
191                         transmitted     in turn. */\r
192 \r
193                         /* Return false if after the block time there is no room on the Tx\r
194                         queue.  It is ok to block inside a critical section as each task\r
195                         maintains it's own critical section status. */\r
196                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
197 \r
198                         /* Depending on queue sizing and task prioritisation:  While we\r
199                         were blocked waiting to post on the queue interrupts were not\r
200                         disabled.  It is possible that the serial ISR has emptied the\r
201                         Tx queue, in which case we need to start the Tx off again\r
202                         writing directly to the Tx register. */\r
203                         if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )\r
204                         {\r
205                                 /* Get back the character we just posted. */\r
206                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
207                                 sTHREEmpty = pdFALSE;\r
208                                 U1TXBUF = cOutChar;\r
209                         }\r
210                 }\r
211         }\r
212         portEXIT_CRITICAL();\r
213 \r
214         return pdPASS;\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 #if configINTERRUPT_EXAMPLE_METHOD == 1\r
219 \r
220         /*\r
221          * UART RX interrupt service routine.\r
222          */\r
223         #pragma vector=UART1RX_VECTOR\r
224         __interrupt void vRxISR( void )\r
225         {\r
226         signed portCHAR cChar;\r
227         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
228         \r
229                 /* Get the character from the UART and post it on the queue of Rxed\r
230                 characters. */\r
231                 cChar = U1RXBUF;\r
232         \r
233                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
234 \r
235                 if( xHigherPriorityTaskWoken )\r
236                 {\r
237                         /*If the post causes a task to wake force a context switch\r
238                         as the woken task may have a higher priority than the task we have\r
239                         interrupted. */\r
240                         taskYIELD();\r
241                 }\r
242 \r
243         /* Make sure any low power mode bits are clear before leaving the ISR. */\r
244         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
245         }\r
246         /*-----------------------------------------------------------*/\r
247         \r
248         /*\r
249          * UART Tx interrupt service routine.\r
250          */\r
251         #pragma vector=UART1TX_VECTOR\r
252         __interrupt void vTxISR( void )\r
253         {\r
254         signed portCHAR cChar;\r
255         portBASE_TYPE xTaskWoken = pdFALSE;\r
256         \r
257                 /* The previous character has been transmitted.  See if there are any\r
258                 further characters waiting transmission. */\r
259         \r
260                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
261                 {\r
262                         /* There was another character queued - transmit it now. */\r
263                         U1TXBUF = cChar;\r
264                 }\r
265                 else\r
266                 {\r
267                         /* There were no other characters to transmit. */\r
268                         sTHREEmpty = pdTRUE;\r
269                 }\r
270 \r
271         /* Make sure any low power mode bits are clear before leaving the ISR. */\r
272         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
273         }\r
274     /*-----------------------------------------------------------*/\r
275 \r
276 #elif configINTERRUPT_EXAMPLE_METHOD == 2\r
277 \r
278     /* This is a standard C function as an assembly file wrapper is used as an\r
279     interrupt entry point. */\r
280         void vRxISR( void )\r
281         {\r
282         signed portCHAR cChar;\r
283         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
284         \r
285                 /* Get the character from the UART and post it on the queue of Rxed\r
286                 characters. */\r
287                 cChar = U1RXBUF;\r
288         \r
289                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
290 \r
291         /*If the post causes a task to wake force a context switch\r
292         as the woken task may have a higher priority than the task we have\r
293         interrupted. */\r
294         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
295         }\r
296         /*-----------------------------------------------------------*/\r
297         \r
298     /* This is a standard C function as an assembly file wrapper is used as an\r
299     interrupt entry point. */\r
300         void vTxISR( void )\r
301         {\r
302         signed portCHAR cChar;\r
303         portBASE_TYPE xTaskWoken = pdFALSE;\r
304         \r
305                 /* The previous character has been transmitted.  See if there are any\r
306                 further characters waiting transmission. */\r
307         \r
308                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
309                 {\r
310                         /* There was another character queued - transmit it now. */\r
311                         U1TXBUF = cChar;\r
312                 }\r
313                 else\r
314                 {\r
315                         /* There were no other characters to transmit. */\r
316                         sTHREEmpty = pdTRUE;\r
317                 }\r
318         }\r
319 \r
320 #endif /* configINTERRUPT_EXAMPLE_METHOD */\r
321 /*-----------------------------------------------------------*/\r