]> git.sur5r.net Git - freertos/blob - Demo/msp430_IAR/serial/serial.c
Prepare for V5.3.1 release.
[freertos] / Demo / msp430_IAR / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
54  *\r
55  * This file only supports UART 1\r
56  */\r
57 \r
58 /* Standard includes. */\r
59 #include <stdlib.h>\r
60 \r
61 /* Scheduler includes. */\r
62 #include "FreeRTOS.h"\r
63 #include "queue.h"\r
64 #include "task.h"\r
65 \r
66 /* Demo application includes. */\r
67 #include "serial.h"\r
68 \r
69 /* Constants required to setup the hardware. */\r
70 #define serTX_AND_RX                    ( ( unsigned portCHAR ) 0x03 )\r
71 \r
72 /* Misc. constants. */\r
73 #define serNO_BLOCK                             ( ( portTickType ) 0 )\r
74 \r
75 /* Enable the UART Tx interrupt. */\r
76 #define vInterruptOn() IFG2 |= UTXIFG1\r
77 \r
78 /* The queue used to hold received characters. */\r
79 static xQueueHandle xRxedChars;\r
80 \r
81 /* The queue used to hold characters waiting transmission. */\r
82 static xQueueHandle xCharsForTx;\r
83 \r
84 static volatile portSHORT sTHREEmpty;\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
89 {\r
90 unsigned portLONG ulBaudRateCount;\r
91 \r
92         /* Initialise the hardware. */\r
93 \r
94         /* Generate the baud rate constants for the wanted baud rate. */\r
95         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
96 \r
97         portENTER_CRITICAL();\r
98         {\r
99                 /* Create the queues used by the com test task. */\r
100                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
101                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
102 \r
103                 /* Reset UART. */\r
104                 UCTL1 |= SWRST;\r
105 \r
106                 /* Set pin function. */\r
107                 P4SEL |= serTX_AND_RX;\r
108 \r
109                 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.\r
110                 LOOPBACK MODE!*/\r
111                 U1CTL |= CHAR + LISTEN;\r
112                 U1TCTL |= SSEL1;\r
113 \r
114                 /* Setup baud rate low byte. */\r
115                 U1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );\r
116 \r
117                 /* Setup baud rate high byte. */\r
118                 ulBaudRateCount >>= 8UL;\r
119                 U1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );\r
120 \r
121                 /* Enable ports. */\r
122                 ME2 |= UTXE1 + URXE1;\r
123 \r
124                 /* Set. */\r
125                 UCTL1 &= ~SWRST;\r
126 \r
127                 /* Nothing in the buffer yet. */\r
128                 sTHREEmpty = pdTRUE;\r
129 \r
130                 /* Enable interrupts. */\r
131                 IE2 |= URXIE1 + UTXIE1;\r
132         }\r
133         portEXIT_CRITICAL();\r
134         \r
135         /* Unlike other ports, this serial code does not allow for more than one\r
136         com port.  We therefore don't return a pointer to a port structure and can\r
137         instead just return NULL. */\r
138         return NULL;\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
143 {\r
144         /* Get the next character from the buffer.  Return false if no characters\r
145         are available, or arrive before xBlockTime expires. */\r
146         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
147         {\r
148                 return pdTRUE;\r
149         }\r
150         else\r
151         {\r
152                 return pdFALSE;\r
153         }\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
158 {\r
159 signed portBASE_TYPE xReturn;\r
160 \r
161         /* Transmit a character. */\r
162 \r
163         portENTER_CRITICAL();\r
164         {\r
165                 if( sTHREEmpty == pdTRUE )\r
166                 {\r
167                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that\r
168                         there are no characters queued to be transmitted - so we can\r
169                         write the character directly to the shift Tx register. */\r
170                         sTHREEmpty = pdFALSE;\r
171                         U1TXBUF = cOutChar;\r
172                         xReturn = pdPASS;\r
173                 }\r
174                 else\r
175                 {\r
176                         /* sTHREEmpty is false, so there are still characters waiting to be\r
177                         transmitted.  We have to queue this character so it gets\r
178                         transmitted     in turn. */\r
179 \r
180                         /* Return false if after the block time there is no room on the Tx\r
181                         queue.  It is ok to block inside a critical section as each task\r
182                         maintains it's own critical section status. */\r
183                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
184 \r
185                         /* Depending on queue sizing and task prioritisation:  While we\r
186                         were blocked waiting to post on the queue interrupts were not\r
187                         disabled.  It is possible that the serial ISR has emptied the\r
188                         Tx queue, in which case we need to start the Tx off again\r
189                         writing directly to the Tx register. */\r
190                         if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )\r
191                         {\r
192                                 /* Get back the character we just posted. */\r
193                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
194                                 sTHREEmpty = pdFALSE;\r
195                                 U1TXBUF = cOutChar;\r
196                         }\r
197                 }\r
198         }\r
199         portEXIT_CRITICAL();\r
200 \r
201         return pdPASS;\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 #if configINTERRUPT_EXAMPLE_METHOD == 1\r
206 \r
207         /*\r
208          * UART RX interrupt service routine.\r
209          */\r
210         #pragma vector=UART1RX_VECTOR\r
211         __interrupt void vRxISR( void )\r
212         {\r
213         signed portCHAR cChar;\r
214         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
215         \r
216                 /* Get the character from the UART and post it on the queue of Rxed\r
217                 characters. */\r
218                 cChar = U1RXBUF;\r
219         \r
220                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
221 \r
222                 if( xHigherPriorityTaskWoken )\r
223                 {\r
224                         /*If the post causes a task to wake force a context switch\r
225                         as the woken task may have a higher priority than the task we have\r
226                         interrupted. */\r
227                         taskYIELD();\r
228                 }\r
229 \r
230         /* Make sure any low power mode bits are clear before leaving the ISR. */\r
231         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
232         }\r
233         /*-----------------------------------------------------------*/\r
234         \r
235         /*\r
236          * UART Tx interrupt service routine.\r
237          */\r
238         #pragma vector=UART1TX_VECTOR\r
239         __interrupt void vTxISR( void )\r
240         {\r
241         signed portCHAR cChar;\r
242         portBASE_TYPE xTaskWoken = pdFALSE;\r
243         \r
244                 /* The previous character has been transmitted.  See if there are any\r
245                 further characters waiting transmission. */\r
246         \r
247                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
248                 {\r
249                         /* There was another character queued - transmit it now. */\r
250                         U1TXBUF = cChar;\r
251                 }\r
252                 else\r
253                 {\r
254                         /* There were no other characters to transmit. */\r
255                         sTHREEmpty = pdTRUE;\r
256                 }\r
257 \r
258         /* Make sure any low power mode bits are clear before leaving the ISR. */\r
259         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
260         }\r
261     /*-----------------------------------------------------------*/\r
262 \r
263 #elif configINTERRUPT_EXAMPLE_METHOD == 2\r
264 \r
265     /* This is a standard C function as an assembly file wrapper is used as an\r
266     interrupt entry point. */\r
267         void vRxISR( void )\r
268         {\r
269         signed portCHAR cChar;\r
270         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
271         \r
272                 /* Get the character from the UART and post it on the queue of Rxed\r
273                 characters. */\r
274                 cChar = U1RXBUF;\r
275         \r
276                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
277 \r
278         /*If the post causes a task to wake force a context switch\r
279         as the woken task may have a higher priority than the task we have\r
280         interrupted. */\r
281         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
282         }\r
283         /*-----------------------------------------------------------*/\r
284         \r
285     /* This is a standard C function as an assembly file wrapper is used as an\r
286     interrupt entry point. */\r
287         void vTxISR( void )\r
288         {\r
289         signed portCHAR cChar;\r
290         portBASE_TYPE xTaskWoken = pdFALSE;\r
291         \r
292                 /* The previous character has been transmitted.  See if there are any\r
293                 further characters waiting transmission. */\r
294         \r
295                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
296                 {\r
297                         /* There was another character queued - transmit it now. */\r
298                         U1TXBUF = cChar;\r
299                 }\r
300                 else\r
301                 {\r
302                         /* There were no other characters to transmit. */\r
303                         sTHREEmpty = pdTRUE;\r
304                 }\r
305         }\r
306 \r
307 #endif /* configINTERRUPT_EXAMPLE_METHOD */\r
308 /*-----------------------------------------------------------*/\r