2 * FreeRTOS Kernel V10.0.1
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\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
39 Note2: This driver does not attempt to handle UART errors.
\r
42 /* Scheduler includes. */
\r
43 #include "FreeRTOS.h"
\r
48 /* Demo application includes. */
\r
51 /*-----------------------------------------------------------*/
\r
54 * The UART interrupt handler.
\r
56 void vUART_Handler( void );
\r
58 /*-----------------------------------------------------------*/
\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
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
68 static EUSCI_A_Type * const pxUARTA0 = ( EUSCI_A_Type * ) EUSCI_A0_BASE;
\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
74 const eUSCI_UART_Config xUARTConfig =
\r
76 EUSCI_A_UART_CLOCKSOURCE_SMCLK, /* SMCLK Clock Source. */
\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
88 * See the serial2.h header file.
\r
90 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned long uxQueueLength )
\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
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
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
110 /* Only one UART is supported so the handle is not used. */
\r
111 return ( xComPortHandle ) 0;
\r
113 /*-----------------------------------------------------------*/
\r
115 BaseType_t xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
117 BaseType_t xReturn;
\r
119 /* Only a single port is supported. */
\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
128 /*-----------------------------------------------------------*/
\r
130 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
132 const TickType_t xMaxWaitTime = pdMS_TO_TICKS( 20UL * ( uint32_t ) usStringLength );
\r
134 /* Only a single port is supported. */
\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
141 /* Ensure notifications are not already waiting. */
\r
142 ( void ) ulTaskNotifyTake( pdTRUE, 0 );
\r
144 /* Remember which task is sending the byte. */
\r
145 xTransmittingTask = xTaskGetCurrentTaskHandle();
\r
147 /* Mark the start and end of the data being sent. */
\r
148 pcStringStart = pcString;
\r
149 pcStringEnd = pcStringStart + usStringLength;
\r
151 /* Start to send the first byte. */
\r
152 pxUARTA0->TXBUF = ( uint_fast8_t ) *pcString;
\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
159 /*-----------------------------------------------------------*/
\r
161 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
163 const TickType_t xMaxWaitTime = pdMS_TO_TICKS( 20UL );
\r
165 /* Only a single port is supported. */
\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
172 /* Ensure notifications are not already waiting. */
\r
173 ( void ) ulTaskNotifyTake( pdTRUE, 0 );
\r
175 /* Remember which task is sending the byte. */
\r
176 xTransmittingTask = xTaskGetCurrentTaskHandle();
\r
178 /* Mark the start and end of the data being sent - in this case just a
\r
180 pcStringStart = &cOutChar;
\r
181 pcStringEnd = pcStringStart + sizeof( cOutChar );
\r
183 /* Start to send the byte. */
\r
184 pxUARTA0->TXBUF = ( uint_fast8_t ) cOutChar;
\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
193 /*-----------------------------------------------------------*/
\r
195 void vSerialClose(xComPortHandle xPort)
\r
197 /* Not supported as not required by the demo application. */
\r
200 /*-----------------------------------------------------------*/
\r
202 void vUART_Handler( void )
\r
205 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
206 uint_fast8_t xInterruptStatus;
\r
208 xInterruptStatus = MAP_UART_getEnabledInterruptStatus( EUSCI_A0_BASE );
\r
210 if( ( xInterruptStatus & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG ) != 0x00 )
\r
212 /* Obtain the character. */
\r
213 ucChar = MAP_UART_receiveData( EUSCI_A0_BASE );
\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
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
226 xQueueSendFromISR( xRxQueue, &ucChar, &xHigherPriorityTaskWoken );
\r
229 if( ( xInterruptStatus & EUSCI_A_UART_TRANSMIT_INTERRUPT_FLAG ) != 0x00 )
\r
231 /* Are there more characters to transmit? */
\r
233 if( ( uint32_t ) pcStringStart < ( uint32_t ) pcStringEnd )
\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
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
247 vTaskNotifyGiveFromISR( xTransmittingTask, &xHigherPriorityTaskWoken );
\r
248 xTransmittingTask = NULL;
\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