]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_CrossWorks/serial/serial.c
Update version number ready for release.
[freertos] / FreeRTOS / Demo / msp430_CrossWorks / serial / serial.c
1 /*\r
2     FreeRTOS V8.0.1 - 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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                             ( ( TickType_t ) 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 QueueHandle_t xRxedChars; \r
94 \r
95 /* The queue used to hold characters waiting transmission. */\r
96 static QueueHandle_t 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, TickType_t 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, TickType_t 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         void vRxISR( void ) __interrupt[ UART1RX_VECTOR ]\r
225         {\r
226         signed char 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         void vTxISR( void ) __interrupt[ UART1TX_VECTOR ]\r
252         {\r
253         signed char cChar;\r
254         portBASE_TYPE xTaskWoken = pdFALSE;\r
255         \r
256                 /* The previous character has been transmitted.  See if there are any\r
257                 further characters waiting transmission. */\r
258         \r
259                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
260                 {\r
261                         /* There was another character queued - transmit it now. */\r
262                         U1TXBUF = cChar;\r
263                 }\r
264                 else\r
265                 {\r
266                         /* There were no other characters to transmit. */\r
267                         sTHREEmpty = pdTRUE;\r
268                 }\r
269 \r
270         /* Make sure any low power mode bits are clear before leaving the ISR. */\r
271         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
272         }\r
273     /*-----------------------------------------------------------*/\r
274 \r
275 #elif configINTERRUPT_EXAMPLE_METHOD == 2\r
276 \r
277     /* This is a standard C function as an assembly file wrapper is used as an\r
278     interrupt entry point. */\r
279         void vRxISR( void )\r
280         {\r
281         signed char cChar;\r
282         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
283         \r
284                 /* Get the character from the UART and post it on the queue of Rxed \r
285                 characters. */\r
286                 cChar = U1RXBUF;\r
287         \r
288                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
289 \r
290         /*If the post causes a task to wake force a context switch \r
291         as the woken task may have a higher priority than the task we have \r
292         interrupted. */\r
293         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
294         }\r
295         /*-----------------------------------------------------------*/\r
296         \r
297     /* This is a standard C function as an assembly file wrapper is used as an\r
298     interrupt entry point. */\r
299         void vTxISR( void )\r
300         {\r
301         signed char cChar;\r
302         portBASE_TYPE xTaskWoken = pdFALSE;\r
303         \r
304                 /* The previous character has been transmitted.  See if there are any\r
305                 further characters waiting transmission. */\r
306         \r
307                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
308                 {\r
309                         /* There was another character queued - transmit it now. */\r
310                         U1TXBUF = cChar;\r
311                 }\r
312                 else\r
313                 {\r
314                         /* There were no other characters to transmit. */\r
315                         sTHREEmpty = pdTRUE;\r
316                 }\r
317         }\r
318 \r
319 #endif /* configINTERRUPT_EXAMPLE_METHOD */\r
320 /*-----------------------------------------------------------*/\r