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
30 * BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
32 * This file only supports UART 2
\r
35 /* Standard includes. */
\r
38 /* Scheduler includes. */
\r
39 #include "FreeRTOS.h"
\r
43 /* Demo application includes. */
\r
46 /* The queue used to hold received characters. */
\r
47 static QueueHandle_t xRxedChars;
\r
49 /* The queue used to hold characters waiting transmission. */
\r
50 static QueueHandle_t xCharsForTx;
\r
52 static volatile short sTHREEmpty;
\r
54 /*-----------------------------------------------------------*/
\r
56 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
58 portENTER_CRITICAL();
\r
60 /* Create the queues used by the com test task. */
\r
61 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
62 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
64 /* Initialize UART asynchronous mode */
\r
65 BGR02 = configPER_CLOCK_HZ / ulWantedBaud;
\r
67 SCR02 = 0x17; /* 8N1 */
\r
68 SMR02 = 0x0d; /* enable SOT3, Reset, normal mode */
\r
69 SSR02 = 0x02; /* LSB first, enable receive interrupts */
\r
71 PFR20_D0 = 1; /* enable UART */
\r
72 PFR20_D1 = 1; /* enable UART */
\r
74 EPFR20_D1 = 0; /* enable UART */
\r
76 portEXIT_CRITICAL();
\r
78 /* Unlike other ports, this serial code does not allow for more than one
\r
79 com port. We therefore don't return a pointer to a port structure and can
\r
80 instead just return NULL. */
\r
83 /*-----------------------------------------------------------*/
\r
85 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
87 /* Get the next character from the buffer. Return false if no characters
\r
88 are available, or arrive before xBlockTime expires. */
\r
89 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
98 /*-----------------------------------------------------------*/
\r
100 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
102 signed portBASE_TYPE xReturn;
\r
104 /* Transmit a character. */
\r
105 portENTER_CRITICAL();
\r
107 if( sTHREEmpty == pdTRUE )
\r
109 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
110 there are no characters queued to be transmitted - so we can
\r
111 write the character directly to the shift Tx register. */
\r
112 sTHREEmpty = pdFALSE;
\r
118 /* sTHREEmpty is false, so there are still characters waiting to be
\r
119 transmitted. We have to queue this character so it gets
\r
120 transmitted in turn. */
\r
122 /* Return false if after the block time there is no room on the Tx
\r
123 queue. It is ok to block inside a critical section as each task
\r
124 maintains it's own critical section status. */
\r
125 if (xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdTRUE)
\r
135 if (pdPASS == xReturn)
\r
137 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
138 queue and send it. This does not need to be in a critical section as
\r
139 if the interrupt has already removed the character the next interrupt
\r
140 will simply turn off the Tx interrupt again. */
\r
145 portEXIT_CRITICAL();
\r
149 /*-----------------------------------------------------------*/
\r
152 * UART RX interrupt service routine.
\r
154 __interrupt void UART2_RxISR (void)
\r
157 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
159 /* Get the character from the UART and post it on the queue of Rxed
\r
163 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
165 if( xHigherPriorityTaskWoken )
\r
167 /*If the post causes a task to wake force a context switch
\r
168 as the woken task may have a higher priority than the task we have
\r
170 portYIELD_FROM_ISR();
\r
174 /*-----------------------------------------------------------*/
\r
177 * UART Tx interrupt service routine.
\r
179 __interrupt void UART2_TxISR (void)
\r
182 signed portBASE_TYPE xTaskWoken = pdFALSE;
\r
184 /* The previous character has been transmitted. See if there are any
\r
185 further characters waiting transmission. */
\r
186 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
188 /* There was another character queued - transmit it now. */
\r
193 /* There were no other characters to transmit. */
\r
194 sTHREEmpty = pdTRUE;
\r
196 /* Disable transmit interrupts */
\r