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