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