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