2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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, uses loopback
\r
32 * mode, and is used to test interrupts that use the FreeRTOS API as part of
\r
33 * a wider test suite. Nor is it intended to show an efficient implementation
\r
34 * of a UART interrupt service routine as queues are used to pass individual
\r
35 * characters one at a time!
\r
38 /* Standard includes. */
\r
41 /* Scheduler includes. */
\r
42 #include "FreeRTOS.h"
\r
46 /* Demo application includes. */
\r
49 /* Misc. constants. */
\r
50 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
52 /* The queue used to hold received characters. */
\r
53 static QueueHandle_t xRxedChars;
\r
55 /* The queue used to hold characters waiting transmission. */
\r
56 static QueueHandle_t xCharsForTx;
\r
58 /*-----------------------------------------------------------*/
\r
60 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
62 unsigned long ulBaudRateCount;
\r
64 /* Initialise the hardware. */
\r
66 /* Generate the baud rate constants for the wanted baud rate. */
\r
67 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
69 portENTER_CRITICAL();
\r
71 /* Create the queues used by the com test task. */
\r
72 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
73 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
76 UCA1CTL1 |= UCSWRST;
\r
79 UCA1CTL1 = UCSSEL0 | UCSSEL1;
\r
81 /* Setup baud rate low byte. */
\r
82 UCA1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
84 /* Setup baud rate high byte. */
\r
85 ulBaudRateCount >>= 8UL;
\r
86 UCA1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
88 /* UCLISTEN sets loopback mode! */
\r
89 UCA1STAT = UCLISTEN;
\r
91 /* Enable interrupts. */
\r
94 /* Take out of reset. */
\r
95 UCA1CTL1 &= ~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 signed portBASE_TYPE 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 /* The implementation of this interrupt is provided to demonstrate the use
\r
135 of queues from inside an interrupt service routine. It is *not* intended to
\r
136 be an efficient interrupt implementation. A real application should make use
\r
137 of the DMA. Or, as a minimum, transmission and reception could use a simple
\r
138 RAM ring buffer, and synchronise with a task using a semaphore when a complete
\r
139 message has been received or transmitted. */
\r
140 #pragma vector=USCI_A1_VECTOR
\r
141 interrupt void prvUSCI_A1_ISR( void )
\r
144 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
146 if( ( UCA1IFG & UCRXIFG ) != 0 )
\r
148 /* Get the character from the UART and post it on the queue of Rxed
\r
151 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
153 else if( ( UCA1IFG & UCTXIFG ) != 0 )
\r
155 /* The previous character has been transmitted. See if there are any
\r
156 further characters waiting transmission. */
\r
157 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
159 /* There was another character queued - transmit it now. */
\r
164 /* There were no other characters to transmit - disable the Tx
\r
170 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
172 /* If writing to a queue caused a task to unblock, and the unblocked task
\r
173 has a priority equal to or above the task that this interrupt interrupted,
\r
174 then lHigherPriorityTaskWoken will have been set to pdTRUE internally within
\r
175 xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this
\r
176 interrupt returns directly to the higher priority unblocked task.
\r
178 THIS MUST BE THE LAST THING DONE IN THE ISR. */
\r
179 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r