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