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