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