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