]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_CrossWorks/serial/serial.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / msp430_CrossWorks / serial / serial.c
1 /*\r
2     FreeRTOS V7.5.0 - 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 char ) 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 short sTHREEmpty;\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
102 {\r
103 unsigned long 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 char ) );\r
114                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\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 char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
129 \r
130                 /* Setup baud rate high byte. */\r
131                 ulBaudRateCount >>= 8UL;\r
132                 U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 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 char *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 char 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         void vRxISR( void ) __interrupt[ UART1RX_VECTOR ]\r
224         {\r
225         signed char cChar;\r
226         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
227         \r
228                 /* Get the character from the UART and post it on the queue of Rxed \r
229                 characters. */\r
230                 cChar = U1RXBUF;\r
231         \r
232                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
233 \r
234                 if( xHigherPriorityTaskWoken )\r
235                 {\r
236                         /*If the post causes a task to wake force a context switch \r
237                         as the woken task may have a higher priority than the task we have \r
238                         interrupted. */\r
239                         taskYIELD();\r
240                 }\r
241 \r
242         /* Make sure any low power mode bits are clear before leaving the ISR. */\r
243         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
244         }\r
245         /*-----------------------------------------------------------*/\r
246         \r
247         /*\r
248          * UART Tx interrupt service routine.\r
249          */\r
250         void vTxISR( void ) __interrupt[ UART1TX_VECTOR ]\r
251         {\r
252         signed char cChar;\r
253         portBASE_TYPE xTaskWoken = pdFALSE;\r
254         \r
255                 /* The previous character has been transmitted.  See if there are any\r
256                 further characters waiting transmission. */\r
257         \r
258                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
259                 {\r
260                         /* There was another character queued - transmit it now. */\r
261                         U1TXBUF = cChar;\r
262                 }\r
263                 else\r
264                 {\r
265                         /* There were no other characters to transmit. */\r
266                         sTHREEmpty = pdTRUE;\r
267                 }\r
268 \r
269         /* Make sure any low power mode bits are clear before leaving the ISR. */\r
270         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
271         }\r
272     /*-----------------------------------------------------------*/\r
273 \r
274 #elif configINTERRUPT_EXAMPLE_METHOD == 2\r
275 \r
276     /* This is a standard C function as an assembly file wrapper is used as an\r
277     interrupt entry point. */\r
278         void vRxISR( void )\r
279         {\r
280         signed char cChar;\r
281         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
282         \r
283                 /* Get the character from the UART and post it on the queue of Rxed \r
284                 characters. */\r
285                 cChar = U1RXBUF;\r
286         \r
287                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
288 \r
289         /*If the post causes a task to wake force a context switch \r
290         as the woken task may have a higher priority than the task we have \r
291         interrupted. */\r
292         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
293         }\r
294         /*-----------------------------------------------------------*/\r
295         \r
296     /* This is a standard C function as an assembly file wrapper is used as an\r
297     interrupt entry point. */\r
298         void vTxISR( void )\r
299         {\r
300         signed char cChar;\r
301         portBASE_TYPE xTaskWoken = pdFALSE;\r
302         \r
303                 /* The previous character has been transmitted.  See if there are any\r
304                 further characters waiting transmission. */\r
305         \r
306                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
307                 {\r
308                         /* There was another character queued - transmit it now. */\r
309                         U1TXBUF = cChar;\r
310                 }\r
311                 else\r
312                 {\r
313                         /* There were no other characters to transmit. */\r
314                         sTHREEmpty = pdTRUE;\r
315                 }\r
316         }\r
317 \r
318 #endif /* configINTERRUPT_EXAMPLE_METHOD */\r
319 /*-----------------------------------------------------------*/\r