2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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 * This is not a proper UART driver. It only supports one port, and is not
\r
32 * intended to show an efficient implementation as queues are used to pass
\r
33 * individual characters one at a time! This is ok for a slow interface, such
\r
34 * as a command line interface (which this driver is used for), but is too
\r
35 * inefficient for higher bandwidth applications.
\r
38 /* Standard includes. */
\r
41 /* Scheduler includes. */
\r
42 #include "FreeRTOS.h"
\r
46 /* Demo application includes. */
\r
50 #include "driverlib.h"
\r
52 /* Misc. constants. */
\r
53 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
55 /* The queue used to hold received characters. */
\r
56 static QueueHandle_t xRxedChars;
\r
58 /* The queue used to hold characters waiting transmission. */
\r
59 static QueueHandle_t xCharsForTx;
\r
61 /*-----------------------------------------------------------*/
\r
63 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
65 unsigned long ulBaudRateCount;
\r
67 /* Initialise the hardware. */
\r
69 /* Generate the baud rate constants for the wanted baud rate. */
\r
70 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
72 portENTER_CRITICAL();
\r
74 /* Create the queues used by the com test task. */
\r
75 xRxedChars = xQueueCreate( uxQueueLength, ( UBaseType_t ) sizeof( signed char ) );
\r
76 xCharsForTx = xQueueCreate( uxQueueLength, ( UBaseType_t ) sizeof( signed char ) );
\r
79 UCA0CTL1 |= UCSWRST;
\r
82 UCA0CTL1 = UCSSEL0 | UCSSEL1;
\r
84 /* Setup baud rate low byte. */
\r
85 UCA0BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
87 /* Setup baud rate high byte. */
\r
88 ulBaudRateCount >>= 8UL;
\r
89 UCA0BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
91 /* Enable interrupts. */
\r
94 /* Take out of reset. */
\r
95 UCA0CTL1 &= ~UCSWRST;
\r
97 portEXIT_CRITICAL();
\r
99 /* Note the comments at the top of this file about this not being a generic
\r
103 /*-----------------------------------------------------------*/
\r
105 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
107 /* Get the next character from the buffer. Return false if no characters
\r
108 are available, or arrive before xBlockTime expires. */
\r
109 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
118 /*-----------------------------------------------------------*/
\r
120 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
122 BaseType_t xReturn;
\r
124 /* Send the next character to the queue of characters waiting transmission,
\r
125 then enable the UART Tx interrupt, just in case UART transmission has already
\r
126 completed and switched itself off. */
\r
127 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
132 /*-----------------------------------------------------------*/
\r
134 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
136 UBaseType_t uxChar;
\r
137 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 100 );
\r
139 /* The driver only supports one port so the pxPort parameter is not used. */
\r
142 for( uxChar = 0; uxChar < usStringLength; uxChar++ )
\r
144 if( xQueueSend( xCharsForTx, &( pcString[ uxChar ] ), xMaxBlockTime ) == pdFALSE )
\r
154 /*-----------------------------------------------------------*/
\r
156 /* The implementation of this interrupt is provided to demonstrate the use
\r
157 of queues from inside an interrupt service routine. It is *not* intended to
\r
158 be an efficient interrupt implementation. A real application should make use
\r
159 of the DMA. Or, as a minimum, transmission and reception could use a simple
\r
160 RAM ring buffer, and synchronise with a task using a semaphore when a complete
\r
161 message has been received or transmitted. */
\r
162 #pragma vector=USCI_A0_VECTOR
\r
163 __interrupt void prvUSCI_A0_ISR( void )
\r
166 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
168 while( ( UCA0IFG & UCRXIFG ) != 0 )
\r
170 /* Get the character from the UART and post it on the queue of Rxed
\r
173 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
176 /* If there is a Tx interrupt pending and the tx interrupts are enabled. */
\r
177 if( ( UCA0IFG & UCTXIFG ) != 0 )
\r
179 /* The previous character has been transmitted. See if there are any
\r
180 further characters waiting transmission. */
\r
181 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
183 /* There was another character queued - transmit it now. */
\r
188 /* There were no other characters to transmit - disable the Tx
\r
194 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
196 /* If writing to a queue caused a task to unblock, and the unblocked task
\r
197 has a priority equal to or above the task that this interrupt interrupted,
\r
198 then lHigherPriorityTaskWoken will have been set to pdTRUE internally within
\r
199 xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this
\r
200 interrupt returns directly to the higher priority unblocked task.
\r
202 THIS MUST BE THE LAST THING DONE IN THE ISR. */
\r
203 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r