]> begriffs open source - freertos/blob - Demo/msp430_CrossWorks/serial/serial.c
Update to V4.7.1
[freertos] / Demo / msp430_CrossWorks / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 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         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 \r
44 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.   \r
45  * \r
46  * This file only supports UART 1\r
47  */\r
48 \r
49 /* Standard includes. */\r
50 #include <stdlib.h>\r
51 \r
52 /* Scheduler includes. */\r
53 #include "FreeRTOS.h"\r
54 #include "queue.h"\r
55 #include "task.h"\r
56 \r
57 /* Demo application includes. */\r
58 #include "serial.h"\r
59 \r
60 /* Constants required to setup the hardware. */\r
61 #define serTX_AND_RX                    ( ( unsigned portCHAR ) 0x03 )\r
62 \r
63 /* Misc. constants. */\r
64 #define serNO_BLOCK                             ( ( portTickType ) 0 )\r
65 \r
66 /* Enable the UART Tx interrupt. */\r
67 #define vInterruptOn() IFG2 |= UTXIFG1\r
68 \r
69 /* The queue used to hold received characters. */\r
70 static xQueueHandle xRxedChars; \r
71 \r
72 /* The queue used to hold characters waiting transmission. */\r
73 static xQueueHandle xCharsForTx; \r
74 \r
75 static volatile portSHORT sTHREEmpty;\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
80 {\r
81 unsigned portLONG ulBaudRateCount;\r
82 \r
83         /* Initialise the hardware. */\r
84 \r
85         /* Generate the baud rate constants for the wanted baud rate. */\r
86         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
87 \r
88         portENTER_CRITICAL();\r
89         {\r
90                 /* Create the queues used by the com test task. */\r
91                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
92                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
93 \r
94                 /* Reset UART. */\r
95                 UCTL1 |= SWRST;\r
96 \r
97                 /* Set pin function. */\r
98                 P4SEL |= serTX_AND_RX;\r
99 \r
100                 /* All other bits remain at zero for n, 8, 1 interrupt driven operation. \r
101                 LOOPBACK MODE!*/\r
102                 U1CTL |= CHAR + LISTEN;\r
103                 U1TCTL |= SSEL1;\r
104 \r
105                 /* Setup baud rate low byte. */\r
106                 U1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );\r
107 \r
108                 /* Setup baud rate high byte. */\r
109                 ulBaudRateCount >>= 8UL;\r
110                 U1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );\r
111 \r
112                 /* Enable ports. */\r
113                 ME2 |= UTXE1 + URXE1;\r
114 \r
115                 /* Set. */\r
116                 UCTL1 &= ~SWRST;\r
117 \r
118                 /* Nothing in the buffer yet. */\r
119                 sTHREEmpty = pdTRUE;\r
120 \r
121                 /* Enable interrupts. */\r
122                 IE2 |= URXIE1 + UTXIE1;\r
123         }\r
124         portEXIT_CRITICAL();\r
125         \r
126         /* Unlike other ports, this serial code does not allow for more than one\r
127         com port.  We therefore don't return a pointer to a port structure and can\r
128         instead just return NULL. */\r
129         return NULL;\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
134 {\r
135         /* Get the next character from the buffer.  Return false if no characters\r
136         are available, or arrive before xBlockTime expires. */\r
137         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
138         {\r
139                 return pdTRUE;\r
140         }\r
141         else\r
142         {\r
143                 return pdFALSE;\r
144         }\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
149 {\r
150 signed portBASE_TYPE xReturn;\r
151 \r
152         /* Transmit a character. */\r
153 \r
154         portENTER_CRITICAL();\r
155         {\r
156                 if( sTHREEmpty == pdTRUE )\r
157                 {\r
158                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that \r
159                         there are no characters queued to be transmitted - so we can\r
160                         write the character directly to the shift Tx register. */\r
161                         sTHREEmpty = pdFALSE;\r
162                         U1TXBUF = cOutChar;\r
163                         xReturn = pdPASS;\r
164                 }\r
165                 else\r
166                 {\r
167                         /* sTHREEmpty is false, so there are still characters waiting to be\r
168                         transmitted.  We have to queue this character so it gets \r
169                         transmitted     in turn. */\r
170 \r
171                         /* Return false if after the block time there is no room on the Tx \r
172                         queue.  It is ok to block inside a critical section as each task\r
173                         maintains it's own critical section status. */\r
174                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
175 \r
176                         /* Depending on queue sizing and task prioritisation:  While we \r
177                         were blocked waiting to post on the queue interrupts were not \r
178                         disabled.  It is possible that the serial ISR has emptied the \r
179                         Tx queue, in which case we need to start the Tx off again\r
180                         writing directly to the Tx register. */\r
181                         if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )\r
182                         {\r
183                                 /* Get back the character we just posted. */\r
184                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
185                                 sTHREEmpty = pdFALSE;\r
186                                 U1TXBUF = cOutChar;\r
187                         }\r
188                 }\r
189         }\r
190         portEXIT_CRITICAL();\r
191 \r
192         return pdPASS;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 #ifdef MSP_ROWLEY_RB_PORT\r
197 \r
198 /* Serial interrupt service routines for the RB port. */\r
199 \r
200         /*\r
201          * UART RX interrupt service routine.\r
202          */\r
203         void vRxISR( void ) __interrupt[ UART1RX_VECTOR ]\r
204         {\r
205         signed portCHAR cChar;\r
206         \r
207                 /* Get the character from the UART and post it on the queue of Rxed \r
208                 characters. */\r
209                 cChar = U1RXBUF;\r
210         \r
211                 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )\r
212                 {\r
213                         /*If the post causes a task to wake force a context switch \r
214                         as the woken task may have a higher priority than the task we have \r
215                         interrupted. */\r
216                         taskYIELD();\r
217                 }\r
218         }\r
219         /*-----------------------------------------------------------*/\r
220         \r
221         /*\r
222          * UART Tx interrupt service routine.\r
223          */\r
224         void vTxISR( void ) __interrupt[ UART1TX_VECTOR ]\r
225         {\r
226         signed portCHAR cChar;\r
227         portBASE_TYPE xTaskWoken;\r
228         \r
229                 /* The previous character has been transmitted.  See if there are any\r
230                 further characters waiting transmission. */\r
231         \r
232                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
233                 {\r
234                         /* There was another character queued - transmit it now. */\r
235                         U1TXBUF = cChar;\r
236                 }\r
237                 else\r
238                 {\r
239                         /* There were no other characters to transmit. */\r
240                         sTHREEmpty = pdTRUE;\r
241                 }\r
242         }\r
243 \r
244 #endif\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 #ifdef MSP_ROWLEY_MP_PORT\r
248 \r
249 /* Serial port interrupts for the alternative port code. */\r
250 \r
251         void ISRCom1Rx( void )\r
252         {\r
253         signed portCHAR cChar;\r
254         \r
255                 /* Get the character from the UART and post it on the queue of Rxed \r
256                 characters. */\r
257                 cChar = U1RXBUF;\r
258         \r
259                 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )\r
260                 {\r
261                         /*If the post causes a task to wake force a context switch \r
262                         as the woken task may have a higher priority than the task we have \r
263                         interrupted. */\r
264                         portEXIT_SWITCHING_ISR( pdTRUE );\r
265                 }\r
266         }\r
267         /*-----------------------------------------------------------*/\r
268         \r
269         void ISRCom1Tx( void )\r
270         {\r
271         signed portCHAR cChar;\r
272         portBASE_TYPE xTaskWoken;\r
273         \r
274                 /* The previous character has been transmitted.  See if there are any\r
275                 further characters waiting transmission. */\r
276         \r
277                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
278                 {\r
279                         /* There was another character queued - transmit it now. */\r
280                         U1TXBUF = cChar;\r
281                 }\r
282                 else\r
283                 {\r
284                         /* There were no other characters to transmit. */\r
285                         sTHREEmpty = pdTRUE;\r
286                 }\r
287         }\r
288 \r
289 #endif\r
290 /*-----------------------------------------------------------*/\r