]> git.sur5r.net Git - freertos/blob - Demo/msp430_GCC/serial/serial.c
Ready for V5.2.0 release.
[freertos] / Demo / msp430_GCC / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.2.0 - 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 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\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 #include <signal.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 /* Interrupt service routines. */\r
88 interrupt (UART1RX_VECTOR) wakeup vRxISR( void );\r
89 interrupt (UART1TX_VECTOR) wakeup vTxISR( void );\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
94 {\r
95 unsigned portLONG ulBaudRateCount;\r
96 \r
97         /* Initialise the hardware. */\r
98 \r
99         /* Generate the baud rate constants for the wanted baud rate. */\r
100         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
101 \r
102         portENTER_CRITICAL();\r
103         {\r
104                 /* Create the queues used by the com test task. */\r
105                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
106                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
107 \r
108                 /* Reset UART. */\r
109                 UCTL1 |= SWRST;\r
110 \r
111                 /* Set pin function. */\r
112                 P4SEL |= serTX_AND_RX;\r
113 \r
114                 /* All other bits remain at zero for n, 8, 1 interrupt driven operation. \r
115                 LOOPBACK MODE!*/\r
116                 U1CTL |= CHAR + LISTEN;\r
117                 U1TCTL |= SSEL1;\r
118 \r
119                 /* Setup baud rate low byte. */\r
120                 U1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );\r
121 \r
122                 /* Setup baud rate high byte. */\r
123                 ulBaudRateCount >>= 8UL;\r
124                 U1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );\r
125 \r
126                 /* Enable ports. */\r
127                 ME2 |= UTXE1 + URXE1;\r
128 \r
129                 /* Set. */\r
130                 UCTL1 &= ~SWRST;\r
131 \r
132                 /* Nothing in the buffer yet. */\r
133                 sTHREEmpty = pdTRUE;\r
134 \r
135                 /* Enable interrupts. */\r
136                 IE2 |= URXIE1 + UTXIE1;\r
137         }\r
138         portEXIT_CRITICAL();\r
139         \r
140         /* Unlike other ports, this serial code does not allow for more than one\r
141         com port.  We therefore don't return a pointer to a port structure and can\r
142         instead just return NULL. */\r
143         return NULL;\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
148 {\r
149         /* Get the next character from the buffer.  Return false if no characters\r
150         are available, or arrive before xBlockTime expires. */\r
151         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
152         {\r
153                 return pdTRUE;\r
154         }\r
155         else\r
156         {\r
157                 return pdFALSE;\r
158         }\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
163 {\r
164 signed portBASE_TYPE xReturn;\r
165 \r
166         /* Transmit a character. */\r
167 \r
168         portENTER_CRITICAL();\r
169         {\r
170                 if( sTHREEmpty == pdTRUE )\r
171                 {\r
172                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that \r
173                         there are no characters queued to be transmitted - so we can\r
174                         write the character directly to the shift Tx register. */\r
175                         sTHREEmpty = pdFALSE;\r
176                         U1TXBUF = cOutChar;\r
177                         xReturn = pdPASS;\r
178                 }\r
179                 else\r
180                 {\r
181                         /* sTHREEmpty is false, so there are still characters waiting to be\r
182                         transmitted.  We have to queue this character so it gets \r
183                         transmitted     in turn. */\r
184 \r
185                         /* Return false if after the block time there is no room on the Tx \r
186                         queue.  It is ok to block inside a critical section as each task\r
187                         maintains it's own critical section status. */\r
188                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
189 \r
190                         /* Depending on queue sizing and task prioritisation:  While we \r
191                         were blocked waiting to post on the queue interrupts were not \r
192                         disabled.  It is possible that the serial ISR has emptied the \r
193                         Tx queue, in which case we need to start the Tx off again\r
194                         writing directly to the Tx register. */\r
195                         if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )\r
196                         {\r
197                                 /* Get back the character we just posted. */\r
198                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
199                                 sTHREEmpty = pdFALSE;\r
200                                 U1TXBUF = cOutChar;\r
201                         }\r
202                 }\r
203         }\r
204         portEXIT_CRITICAL();\r
205 \r
206         return pdPASS;\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 /*\r
211  * UART RX interrupt service routine.\r
212  */\r
213 interrupt (UART1RX_VECTOR) wakeup vRxISR( void )\r
214 {\r
215 signed portCHAR cChar;\r
216 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
217 \r
218         /* Get the character from the UART and post it on the queue of Rxed \r
219         characters. */\r
220         cChar = U1RXBUF;\r
221 \r
222         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
223 \r
224         if( xHigherPriorityTaskWoken )\r
225         {\r
226                 /*If the post causes a task to wake force a context switch \r
227                 as the woken task may have a higher priority than the task we have \r
228                 interrupted. */\r
229                 taskYIELD();\r
230         }\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 /*\r
235  * UART Tx interrupt service routine.\r
236  */\r
237 interrupt (UART1TX_VECTOR) wakeup vTxISR( void )\r
238 {\r
239 signed portCHAR cChar;\r
240 portBASE_TYPE xTaskWoken = pdFALSE;\r
241 \r
242         /* The previous character has been transmitted.  See if there are any\r
243         further characters waiting transmission. */\r
244 \r
245         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
246         {\r
247                 /* There was another character queued - transmit it now. */\r
248                 U1TXBUF = cChar;\r
249         }\r
250         else\r
251         {\r
252                 /* There were no other characters to transmit. */\r
253                 sTHREEmpty = pdTRUE;\r
254         }\r
255 }\r
256 \r