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