]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_CrossWorks/serial/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / msp430_CrossWorks / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.   \r
31  * \r
32  * This file only supports UART 1\r
33  */\r
34 \r
35 /* Standard includes. */\r
36 #include <stdlib.h>\r
37 \r
38 /* Scheduler includes. */\r
39 #include "FreeRTOS.h"\r
40 #include "queue.h"\r
41 #include "task.h"\r
42 \r
43 /* Demo application includes. */\r
44 #include "serial.h"\r
45 \r
46 /* Constants required to setup the hardware. */\r
47 #define serTX_AND_RX                    ( ( unsigned char ) 0x03 )\r
48 \r
49 /* Misc. constants. */\r
50 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
51 \r
52 /* Enable the UART Tx interrupt. */\r
53 #define vInterruptOn() IFG2 |= UTXIFG1\r
54 \r
55 /* The queue used to hold received characters. */\r
56 static QueueHandle_t xRxedChars; \r
57 \r
58 /* The queue used to hold characters waiting transmission. */\r
59 static QueueHandle_t xCharsForTx; \r
60 \r
61 static volatile short sTHREEmpty;\r
62 \r
63 /*-----------------------------------------------------------*/\r
64 \r
65 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
66 {\r
67 unsigned long ulBaudRateCount;\r
68 \r
69         /* Initialise the hardware. */\r
70 \r
71         /* Generate the baud rate constants for the wanted baud rate. */\r
72         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
73 \r
74         portENTER_CRITICAL();\r
75         {\r
76                 /* Create the queues used by the com test task. */\r
77                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
78                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
79 \r
80                 /* Reset UART. */\r
81                 UCTL1 |= SWRST;\r
82 \r
83                 /* Set pin function. */\r
84                 P4SEL |= serTX_AND_RX;\r
85 \r
86                 /* All other bits remain at zero for n, 8, 1 interrupt driven operation. \r
87                 LOOPBACK MODE!*/\r
88                 U1CTL |= CHAR + LISTEN;\r
89                 U1TCTL |= SSEL1;\r
90 \r
91                 /* Setup baud rate low byte. */\r
92                 U1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
93 \r
94                 /* Setup baud rate high byte. */\r
95                 ulBaudRateCount >>= 8UL;\r
96                 U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
97 \r
98                 /* Enable ports. */\r
99                 ME2 |= UTXE1 + URXE1;\r
100 \r
101                 /* Set. */\r
102                 UCTL1 &= ~SWRST;\r
103 \r
104                 /* Nothing in the buffer yet. */\r
105                 sTHREEmpty = pdTRUE;\r
106 \r
107                 /* Enable interrupts. */\r
108                 IE2 |= URXIE1 + UTXIE1;\r
109         }\r
110         portEXIT_CRITICAL();\r
111         \r
112         /* Unlike other ports, this serial code does not allow for more than one\r
113         com port.  We therefore don't return a pointer to a port structure and can\r
114         instead just return NULL. */\r
115         return NULL;\r
116 }\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
120 {\r
121         /* Get the next character from the buffer.  Return false if no characters\r
122         are available, or arrive before xBlockTime expires. */\r
123         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
124         {\r
125                 return pdTRUE;\r
126         }\r
127         else\r
128         {\r
129                 return pdFALSE;\r
130         }\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
135 {\r
136 signed portBASE_TYPE xReturn;\r
137 \r
138         /* Transmit a character. */\r
139 \r
140         portENTER_CRITICAL();\r
141         {\r
142                 if( sTHREEmpty == pdTRUE )\r
143                 {\r
144                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that \r
145                         there are no characters queued to be transmitted - so we can\r
146                         write the character directly to the shift Tx register. */\r
147                         sTHREEmpty = pdFALSE;\r
148                         U1TXBUF = cOutChar;\r
149                         xReturn = pdPASS;\r
150                 }\r
151                 else\r
152                 {\r
153                         /* sTHREEmpty is false, so there are still characters waiting to be\r
154                         transmitted.  We have to queue this character so it gets \r
155                         transmitted     in turn. */\r
156 \r
157                         /* Return false if after the block time there is no room on the Tx \r
158                         queue.  It is ok to block inside a critical section as each task\r
159                         maintains it's own critical section status. */\r
160                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
161 \r
162                         /* Depending on queue sizing and task prioritisation:  While we \r
163                         were blocked waiting to post on the queue interrupts were not \r
164                         disabled.  It is possible that the serial ISR has emptied the \r
165                         Tx queue, in which case we need to start the Tx off again\r
166                         writing directly to the Tx register. */\r
167                         if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )\r
168                         {\r
169                                 /* Get back the character we just posted. */\r
170                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );\r
171                                 sTHREEmpty = pdFALSE;\r
172                                 U1TXBUF = cOutChar;\r
173                         }\r
174                 }\r
175         }\r
176         portEXIT_CRITICAL();\r
177 \r
178         return pdPASS;\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 #if configINTERRUPT_EXAMPLE_METHOD == 1\r
183 \r
184         /*\r
185          * UART RX interrupt service routine.\r
186          */\r
187         void vRxISR( void ) __interrupt[ UART1RX_VECTOR ]\r
188         {\r
189         signed char cChar;\r
190         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
191         \r
192                 /* Get the character from the UART and post it on the queue of Rxed \r
193                 characters. */\r
194                 cChar = U1RXBUF;\r
195         \r
196                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
197 \r
198                 if( xHigherPriorityTaskWoken )\r
199                 {\r
200                         /*If the post causes a task to wake force a context switch \r
201                         as the woken task may have a higher priority than the task we have \r
202                         interrupted. */\r
203                         taskYIELD();\r
204                 }\r
205 \r
206         /* Make sure any low power mode bits are clear before leaving the ISR. */\r
207         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\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 char cChar;\r
217         portBASE_TYPE xTaskWoken = pdFALSE;\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         /* Make sure any low power mode bits are clear before leaving the ISR. */\r
234         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
235         }\r
236     /*-----------------------------------------------------------*/\r
237 \r
238 #elif configINTERRUPT_EXAMPLE_METHOD == 2\r
239 \r
240     /* This is a standard C function as an assembly file wrapper is used as an\r
241     interrupt entry point. */\r
242         void vRxISR( void )\r
243         {\r
244         signed char cChar;\r
245         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
246         \r
247                 /* Get the character from the UART and post it on the queue of Rxed \r
248                 characters. */\r
249                 cChar = U1RXBUF;\r
250         \r
251                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
252 \r
253         /*If the post causes a task to wake force a context switch \r
254         as the woken task may have a higher priority than the task we have \r
255         interrupted. */\r
256         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
257         }\r
258         /*-----------------------------------------------------------*/\r
259         \r
260     /* This is a standard C function as an assembly file wrapper is used as an\r
261     interrupt entry point. */\r
262         void vTxISR( void )\r
263         {\r
264         signed char cChar;\r
265         portBASE_TYPE xTaskWoken = pdFALSE;\r
266         \r
267                 /* The previous character has been transmitted.  See if there are any\r
268                 further characters waiting transmission. */\r
269         \r
270                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
271                 {\r
272                         /* There was another character queued - transmit it now. */\r
273                         U1TXBUF = cChar;\r
274                 }\r
275                 else\r
276                 {\r
277                         /* There were no other characters to transmit. */\r
278                         sTHREEmpty = pdTRUE;\r
279                 }\r
280         }\r
281 \r
282 #endif /* configINTERRUPT_EXAMPLE_METHOD */\r
283 /*-----------------------------------------------------------*/\r