]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/Full_Demo/serial.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / Full_Demo / 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         Note1:  This driver is used specifically to provide an interface to the\r
32         FreeRTOS+CLI command interpreter.  It is *not* intended to be a generic\r
33         serial port driver.  Nor is it intended to be used as an example of an\r
34         efficient implementation.  In particular, a queue is used to buffer\r
35         received characters, which is fine in this case as key presses arrive\r
36         slowly, but a DMA and/or RAM buffer should be used in place of the queue in\r
37         applications that expect higher throughput.\r
38 \r
39         Note2:  This driver does not attempt to handle UART errors.\r
40 */\r
41 \r
42 /* Scheduler includes. */\r
43 #include "FreeRTOS.h"\r
44 #include "task.h"\r
45 #include "queue.h"\r
46 #include "semphr.h"\r
47 \r
48 /* Demo application includes. */\r
49 #include "serial.h"\r
50 \r
51 /*-----------------------------------------------------------*/\r
52 \r
53 /*\r
54  * The UART interrupt handler.\r
55  */\r
56 void vUART_Handler( void );\r
57 \r
58 /*-----------------------------------------------------------*/\r
59 \r
60 /* The queue into which received key presses are placed.  NOTE THE COMMENTS AT\r
61 THE TOP OF THIS FILE REGARDING THE USE OF QUEUES FOR THIS PURPOSE. */\r
62 static QueueHandle_t xRxQueue = NULL;\r
63 \r
64 /* Variables used in the Tx interrupt to send a string. */\r
65 static volatile const signed char *pcStringStart = NULL, *pcStringEnd = NULL;\r
66 static volatile TaskHandle_t xTransmittingTask = NULL;\r
67 \r
68 static EUSCI_A_Type * const pxUARTA0 = ( EUSCI_A_Type * ) EUSCI_A0_BASE;\r
69 \r
70 /* UART Configuration for 19200 baud.  Value generated using the tool provided\r
71 on the following page:\r
72 http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html\r
73  */\r
74 const eUSCI_UART_Config xUARTConfig =\r
75 {\r
76         EUSCI_A_UART_CLOCKSOURCE_SMCLK, /* SMCLK Clock Source. */\r
77         156,                                                    /* BRDIV */\r
78         4,                                                              /* UCxBRF */\r
79         0,                                                              /* UCxBRS */\r
80         EUSCI_A_UART_NO_PARITY,                 /* No Parity. */\r
81         EUSCI_A_UART_LSB_FIRST,                 /* MSB First. */\r
82         EUSCI_A_UART_ONE_STOP_BIT,              /* One stop bit. */\r
83         EUSCI_A_UART_MODE,                              /* UART mode. */\r
84         EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION /* Low Frequency Mode. */\r
85 };\r
86 \r
87 /*\r
88  * See the serial2.h header file.\r
89  */\r
90 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned long uxQueueLength )\r
91 {\r
92         /* Create the queue used to hold received characters.  NOTE THE COMMENTS AT\r
93         THE TOP OF THIS FILE REGARDING THE USE OF QUEUES FOR THIS PURPSOE. */\r
94         xRxQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
95         configASSERT( xRxQueue );\r
96 \r
97         /* Use the library functions to initialise and enable the UART. */\r
98         MAP_UART_initModule( EUSCI_A0_BASE, &xUARTConfig );\r
99         MAP_UART_enableModule( EUSCI_A0_BASE );\r
100         MAP_UART_clearInterruptFlag( EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT | EUSCI_A_UART_TRANSMIT_INTERRUPT );\r
101         MAP_UART_enableInterrupt( EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT );\r
102 \r
103         /* The interrupt handler uses the FreeRTOS API function so its priority must\r
104         be at or below the configured maximum system call interrupt priority.\r
105         configKERNEL_INTERRUPT_PRIORITY is the priority used by the RTOS tick and\r
106         (should) always be set to the minimum priority. */\r
107         MAP_Interrupt_setPriority( INT_EUSCIA0, configKERNEL_INTERRUPT_PRIORITY );\r
108         MAP_Interrupt_enableInterrupt( INT_EUSCIA0 );\r
109 \r
110         /* Only one UART is supported so the handle is not used. */\r
111         return ( xComPortHandle ) 0;\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 BaseType_t xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
116 {\r
117 BaseType_t xReturn;\r
118 \r
119         /* Only a single port is supported. */\r
120         ( void ) pxPort;\r
121 \r
122         /* Obtain a received character from the queue - entering the Blocked state\r
123         (so not consuming any processing time) to wait for a character if one is not\r
124         already available. */\r
125         xReturn = xQueueReceive( xRxQueue, pcRxedChar, xBlockTime );\r
126         return xReturn;\r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
131 {\r
132 const TickType_t xMaxWaitTime = pdMS_TO_TICKS( 20UL * ( uint32_t ) usStringLength );\r
133 \r
134         /* Only a single port is supported. */\r
135         ( void ) pxPort;\r
136 \r
137         /* Note there is no mutual exclusion at the driver level.  If more than one\r
138         task is using the serial port then mutual exclusion should be provided where\r
139         this function is called. */\r
140 \r
141         /* Ensure notifications are not already waiting. */\r
142         ( void ) ulTaskNotifyTake( pdTRUE, 0 );\r
143 \r
144         /* Remember which task is sending the byte. */\r
145         xTransmittingTask = xTaskGetCurrentTaskHandle();\r
146 \r
147         /* Mark the start and end of the data being sent. */\r
148         pcStringStart = pcString;\r
149         pcStringEnd = pcStringStart + usStringLength;\r
150 \r
151         /* Start to send the first byte. */\r
152         pxUARTA0->TXBUF = ( uint_fast8_t ) *pcString;\r
153 \r
154         /* Enable the interrupt then wait for the byte to be sent.  The interrupt\r
155         will be disabled again in the ISR. */\r
156         MAP_UART_enableInterrupt( EUSCI_A0_BASE, EUSCI_A_UART_TRANSMIT_INTERRUPT );\r
157         ulTaskNotifyTake( pdTRUE, xMaxWaitTime );\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
162 {\r
163 const TickType_t xMaxWaitTime = pdMS_TO_TICKS( 20UL );\r
164 \r
165         /* Only a single port is supported. */\r
166         ( void ) pxPort;\r
167 \r
168         /* Note there is no mutual exclusion at the driver level.  If more than one\r
169         task is using the serial port then mutual exclusion should be provided where\r
170         this function is called. */\r
171 \r
172         /* Ensure notifications are not already waiting. */\r
173         ( void ) ulTaskNotifyTake( pdTRUE, 0 );\r
174 \r
175         /* Remember which task is sending the byte. */\r
176         xTransmittingTask = xTaskGetCurrentTaskHandle();\r
177 \r
178         /* Mark the start and end of the data being sent - in this case just a\r
179         single byte. */\r
180         pcStringStart = &cOutChar;\r
181         pcStringEnd = pcStringStart + sizeof( cOutChar );\r
182 \r
183         /* Start to send the byte. */\r
184         pxUARTA0->TXBUF = ( uint_fast8_t ) cOutChar;\r
185 \r
186         /* Enable the interrupt then wait for the byte to be sent.  The interrupt\r
187         will be disabled again in the ISR. */\r
188         MAP_UART_enableInterrupt( EUSCI_A0_BASE, EUSCI_A_UART_TRANSMIT_INTERRUPT );\r
189         ulTaskNotifyTake( pdTRUE, xMaxWaitTime );\r
190 \r
191         return pdPASS;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vSerialClose(xComPortHandle xPort)\r
196 {\r
197         /* Not supported as not required by the demo application. */\r
198         ( void ) xPort;\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 void vUART_Handler( void )\r
203 {\r
204 uint8_t ucChar;\r
205 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
206 uint_fast8_t xInterruptStatus;\r
207 \r
208         xInterruptStatus = MAP_UART_getEnabledInterruptStatus( EUSCI_A0_BASE );\r
209 \r
210         if( ( xInterruptStatus & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG ) != 0x00 )\r
211         {\r
212                 /* Obtain the character. */\r
213                 ucChar = MAP_UART_receiveData( EUSCI_A0_BASE );\r
214 \r
215                 /* Send the character to the queue.  Note the comments at the top of this\r
216                 file with regards to the inefficiency of this method for anything other than\r
217                 very low bandwidth communications.\r
218 \r
219                 If writing to the queue unblocks a task, and the unblocked task has a\r
220                 priority above the currently running task (the task that this interrupt\r
221                 interrupted), then xHigherPriorityTaskWoken will be set to pdTRUE inside the\r
222                 xQueueSendFromISR() function.  xHigherPriorityTaskWoken is then passed to\r
223                 portYIELD_FROM_ISR() at the end of this interrupt handler to request a\r
224                 context switch so the interrupt returns directly to the (higher priority)\r
225                 unblocked task. */\r
226                 xQueueSendFromISR( xRxQueue, &ucChar, &xHigherPriorityTaskWoken );\r
227         }\r
228 \r
229         if( ( xInterruptStatus & EUSCI_A_UART_TRANSMIT_INTERRUPT_FLAG ) != 0x00 )\r
230         {\r
231                 /* Are there more characters to transmit? */\r
232                 pcStringStart++;\r
233                 if( ( uint32_t ) pcStringStart < ( uint32_t ) pcStringEnd )\r
234                 {\r
235                         /* This is probably quite a heavy wait function just for writing to\r
236                         the Tx register.  An optimised design would probably replace this\r
237                         with a simple register write. */\r
238                         pxUARTA0->TXBUF = ( uint_fast8_t ) *pcStringStart;\r
239                 }\r
240                 else\r
241                 {\r
242                         /* No more characters to send.  Disable the interrupt and notify the\r
243                         task, if the task is waiting. */\r
244                         MAP_UART_disableInterrupt( EUSCI_A0_BASE, EUSCI_A_UART_TRANSMIT_INTERRUPT );\r
245                         if( xTransmittingTask != NULL )\r
246                         {\r
247                                 vTaskNotifyGiveFromISR( xTransmittingTask, &xHigherPriorityTaskWoken );\r
248                                 xTransmittingTask = NULL;\r
249                         }\r
250                 }\r
251         }\r
252 \r
253 \r
254         /* portYIELD_FROM_ISR() will request a context switch if executing this\r
255         interrupt handler caused a task to leave the blocked state, and the task\r
256         that left the blocked state has a higher priority than the currently running\r
257         task (the task this interrupt interrupted).  See the comment above the calls\r
258         to xSemaphoreGiveFromISR() and xQueueSendFromISR() within this function. */\r
259         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
260 }\r
261 \r
262 \r
263 \r
264 \r
265 \r