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