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