]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_GCC/serial/serial.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / msp430_GCC / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 #include <signal.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 /* Interrupt service routines. */\r
64 interrupt (UART1RX_VECTOR) wakeup vRxISR( void );\r
65 interrupt (UART1TX_VECTOR) wakeup vTxISR( void );\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
70 {\r
71 unsigned long 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 char ) );\r
82                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\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 char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
97 \r
98                 /* Setup baud rate high byte. */\r
99                 ulBaudRateCount >>= 8UL;\r
100                 U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 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 char *pcRxedChar, TickType_t 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 char cOutChar, TickType_t 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 /*\r
187  * UART RX interrupt service routine.\r
188  */\r
189 interrupt (UART1RX_VECTOR) wakeup vRxISR( void )\r
190 {\r
191 signed char cChar;\r
192 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
193 \r
194         /* Get the character from the UART and post it on the queue of Rxed \r
195         characters. */\r
196         cChar = U1RXBUF;\r
197 \r
198         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
199 \r
200         if( xHigherPriorityTaskWoken )\r
201         {\r
202                 /*If the post causes a task to wake force a context switch \r
203                 as the woken task may have a higher priority than the task we have \r
204                 interrupted. */\r
205                 taskYIELD();\r
206         }\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 /*\r
211  * UART Tx interrupt service routine.\r
212  */\r
213 interrupt (UART1TX_VECTOR) wakeup vTxISR( void )\r
214 {\r
215 signed char cChar;\r
216 portBASE_TYPE xTaskWoken = pdFALSE;\r
217 \r
218         /* The previous character has been transmitted.  See if there are any\r
219         further characters waiting transmission. */\r
220 \r
221         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
222         {\r
223                 /* There was another character queued - transmit it now. */\r
224                 U1TXBUF = cChar;\r
225         }\r
226         else\r
227         {\r
228                 /* There were no other characters to transmit. */\r
229                 sTHREEmpty = pdTRUE;\r
230         }\r
231 }\r
232 \r