]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_GCC/serial/serial.c
331acdca19f48ef0949e8adcc9dd8d80cf9d4b1b
[freertos] / FreeRTOS / Demo / msp430_GCC / 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 #include <signal.h>\r
105 \r
106 /* Scheduler includes. */\r
107 #include "FreeRTOS.h"\r
108 #include "queue.h"\r
109 #include "task.h"\r
110 \r
111 /* Demo application includes. */\r
112 #include "serial.h"\r
113 \r
114 /* Constants required to setup the hardware. */\r
115 #define serTX_AND_RX                    ( ( unsigned char ) 0x03 )\r
116 \r
117 /* Misc. constants. */\r
118 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
119 \r
120 /* Enable the UART Tx interrupt. */\r
121 #define vInterruptOn() IFG2 |= UTXIFG1\r
122 \r
123 /* The queue used to hold received characters. */\r
124 static QueueHandle_t xRxedChars; \r
125 \r
126 /* The queue used to hold characters waiting transmission. */\r
127 static QueueHandle_t xCharsForTx; \r
128 \r
129 static volatile short sTHREEmpty;\r
130 \r
131 /* Interrupt service routines. */\r
132 interrupt (UART1RX_VECTOR) wakeup vRxISR( void );\r
133 interrupt (UART1TX_VECTOR) wakeup vTxISR( void );\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
138 {\r
139 unsigned long ulBaudRateCount;\r
140 \r
141         /* Initialise the hardware. */\r
142 \r
143         /* Generate the baud rate constants for the wanted baud rate. */\r
144         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
145 \r
146         portENTER_CRITICAL();\r
147         {\r
148                 /* Create the queues used by the com test task. */\r
149                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
150                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
151 \r
152                 /* Reset UART. */\r
153                 UCTL1 |= SWRST;\r
154 \r
155                 /* Set pin function. */\r
156                 P4SEL |= serTX_AND_RX;\r
157 \r
158                 /* All other bits remain at zero for n, 8, 1 interrupt driven operation. \r
159                 LOOPBACK MODE!*/\r
160                 U1CTL |= CHAR + LISTEN;\r
161                 U1TCTL |= SSEL1;\r
162 \r
163                 /* Setup baud rate low byte. */\r
164                 U1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
165 \r
166                 /* Setup baud rate high byte. */\r
167                 ulBaudRateCount >>= 8UL;\r
168                 U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
169 \r
170                 /* Enable ports. */\r
171                 ME2 |= UTXE1 + URXE1;\r
172 \r
173                 /* Set. */\r
174                 UCTL1 &= ~SWRST;\r
175 \r
176                 /* Nothing in the buffer yet. */\r
177                 sTHREEmpty = pdTRUE;\r
178 \r
179                 /* Enable interrupts. */\r
180                 IE2 |= URXIE1 + UTXIE1;\r
181         }\r
182         portEXIT_CRITICAL();\r
183         \r
184         /* Unlike other ports, this serial code does not allow for more than one\r
185         com port.  We therefore don't return a pointer to a port structure and can\r
186         instead just return NULL. */\r
187         return NULL;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
192 {\r
193         /* Get the next character from the buffer.  Return false if no characters\r
194         are available, or arrive before xBlockTime expires. */\r
195         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
196         {\r
197                 return pdTRUE;\r
198         }\r
199         else\r
200         {\r
201                 return pdFALSE;\r
202         }\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
207 {\r
208 signed portBASE_TYPE xReturn;\r
209 \r
210         /* Transmit a character. */\r
211 \r
212         portENTER_CRITICAL();\r
213         {\r
214                 if( sTHREEmpty == pdTRUE )\r
215                 {\r
216                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that \r
217                         there are no characters queued to be transmitted - so we can\r
218                         write the character directly to the shift Tx register. */\r
219                         sTHREEmpty = pdFALSE;\r
220                         U1TXBUF = cOutChar;\r
221                         xReturn = pdPASS;\r
222                 }\r
223                 else\r
224                 {\r
225                         /* sTHREEmpty is false, so there are still characters waiting to be\r
226                         transmitted.  We have to queue this character so it gets \r
227                         transmitted     in turn. */\r
228 \r
229                         /* Return false if after the block time there is no room on the Tx \r
230                         queue.  It is ok to block inside a critical section as each task\r
231                         maintains it's own critical section status. */\r
232                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
233 \r
234                         /* Depending on queue sizing and task prioritisation:  While we \r
235                         were blocked waiting to post on the queue interrupts were not \r
236                         disabled.  It is possible that the serial ISR has emptied the \r
237                         Tx queue, in which case we need to start the Tx off again\r
238                         writing directly to the Tx register. */\r
239                         if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )\r
240                         {\r
241                                 /* Get back the character we just posted. */\r
242                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
243                                 sTHREEmpty = pdFALSE;\r
244                                 U1TXBUF = cOutChar;\r
245                         }\r
246                 }\r
247         }\r
248         portEXIT_CRITICAL();\r
249 \r
250         return pdPASS;\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 /*\r
255  * UART RX interrupt service routine.\r
256  */\r
257 interrupt (UART1RX_VECTOR) wakeup vRxISR( void )\r
258 {\r
259 signed char cChar;\r
260 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
261 \r
262         /* Get the character from the UART and post it on the queue of Rxed \r
263         characters. */\r
264         cChar = U1RXBUF;\r
265 \r
266         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
267 \r
268         if( xHigherPriorityTaskWoken )\r
269         {\r
270                 /*If the post causes a task to wake force a context switch \r
271                 as the woken task may have a higher priority than the task we have \r
272                 interrupted. */\r
273                 taskYIELD();\r
274         }\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 /*\r
279  * UART Tx interrupt service routine.\r
280  */\r
281 interrupt (UART1TX_VECTOR) wakeup vTxISR( void )\r
282 {\r
283 signed char cChar;\r
284 portBASE_TYPE xTaskWoken = pdFALSE;\r
285 \r
286         /* The previous character has been transmitted.  See if there are any\r
287         further characters waiting transmission. */\r
288 \r
289         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
290         {\r
291                 /* There was another character queued - transmit it now. */\r
292                 U1TXBUF = cChar;\r
293         }\r
294         else\r
295         {\r
296                 /* There were no other characters to transmit. */\r
297                 sTHREEmpty = pdTRUE;\r
298         }\r
299 }\r
300 \r