2 * FreeRTOS Kernel V10.1.0
\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
28 /* Basic interrupt driven serial port driver for uart1.
\r
31 /* Standard includes. */
\r
34 /* Kernel includes. */
\r
35 #include "FreeRTOS.h"
\r
39 /* Demo application includes. */
\r
40 #include <machine/uart.h>
\r
41 #include <machine/ic.h>
\r
43 /*-----------------------------------------------------------*/
\r
45 #define comBLOCK_RETRY_TIME 10
\r
46 /*-----------------------------------------------------------*/
\r
48 /* The interrupt handlers are naked functions that call C handlers. The C
\r
49 handlers are marked as noinline to ensure they work correctly when the
\r
51 void interrupt5_handler( void ) __attribute__((naked));
\r
52 static void prvTxHandler( void ) __attribute__((noinline));
\r
53 void interrupt6_handler( void ) __attribute__((naked));
\r
54 static void prvRxHandler( void ) __attribute__((noinline));
\r
56 /*-----------------------------------------------------------*/
\r
58 /* Queues used to hold received characters, and characters waiting to be
\r
60 static QueueHandle_t xRxedChars;
\r
61 static QueueHandle_t xCharsForTx;
\r
62 /*-----------------------------------------------------------*/
\r
64 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
66 /* Create the queues used to hold Rx and Tx characters. */
\r
67 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
68 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
70 if( ( xRxedChars ) && ( xCharsForTx ) )
\r
72 /* Set up interrupts */
\r
73 /* tx interrupt will be enabled when we need to send something */
\r
76 irq[IRQ_UART1_TX].ien = 1;
\r
77 irq[IRQ_UART1_RX].ien = 1;
\r
80 return ( xComPortHandle ) 0;
\r
82 /*-----------------------------------------------------------*/
\r
84 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
86 /* The port handle is not required as this driver only supports uart1. */
\r
89 /* Get the next character from the buffer. Return false if no characters
\r
90 are available, or arrive before xBlockTime expires. */
\r
91 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
100 /*-----------------------------------------------------------*/
\r
102 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
105 signed char *pChNext;
\r
107 /* Send each character in the string, one at a time. */
\r
108 pChNext = ( signed char * )pcString;
\r
109 for( i = 0; i < usStringLength; i++ )
\r
111 /* Block until character has been transmitted. */
\r
112 while( xSerialPutChar( pxPort, *pChNext, comBLOCK_RETRY_TIME ) != pdTRUE ); pChNext++;
\r
115 /*-----------------------------------------------------------*/
\r
117 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
121 /* Place the character in the queue of characters to be transmitted. */
\r
122 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
127 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
128 queue and send it. This does not need to be in a critical section as
\r
129 if the interrupt has already removed the character the next interrupt
\r
130 will simply turn off the Tx interrupt again. */
\r
131 uart1->tx_mask = 1;
\r
135 /*-----------------------------------------------------------*/
\r
137 void vSerialClose( xComPortHandle xPort )
\r
139 /* Not supported as not required by the demo application. */
\r
142 /*-----------------------------------------------------------*/
\r
144 /* UART Tx interrupt handler. */
\r
145 void interrupt5_handler( void )
\r
147 /* This is a naked function. */
\r
148 portSAVE_CONTEXT();
\r
150 portRESTORE_CONTEXT();
\r
152 /*-----------------------------------------------------------*/
\r
154 static void prvTxHandler( void )
\r
157 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
159 /* The interrupt was caused by the transmit fifo having space for at least one
\r
160 character. Are there any more characters to transmit? */
\r
161 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
163 /* A character was retrieved from the queue so can be sent to the uart now. */
\r
164 uart1->tx_data = cChar;
\r
168 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
169 uart1->tx_mask = 0;
\r
172 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
173 ensure that the unblocked task is the task that executes when the interrupt
\r
174 completes if the unblocked task has a priority higher than the interrupted
\r
176 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
178 /*-----------------------------------------------------------*/
\r
180 /* UART Rx interrupt. */
\r
181 void interrupt6_handler( void )
\r
183 portSAVE_CONTEXT();
\r
185 portRESTORE_CONTEXT();
\r
187 /*-----------------------------------------------------------*/
\r
189 static void prvRxHandler( void )
\r
192 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
194 /* The interrupt was caused by the receiver getting data. */
\r
195 cChar = uart1->rx_data;
\r
197 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
199 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
200 ensure that the unblocked task is the task that executes when the interrupt
\r
201 completes if the unblocked task has a priority higher than the interrupted
\r
203 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
205 /*-----------------------------------------------------------*/
\r