2 * FreeRTOS Kernel V10.0.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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Basic interrupt driven serial port driver for uart1.
\r
32 /* Standard includes. */
\r
35 /* Kernel includes. */
\r
36 #include "FreeRTOS.h"
\r
40 /* Demo application includes. */
\r
41 #include <machine/uart.h>
\r
42 #include <machine/ic.h>
\r
44 /*-----------------------------------------------------------*/
\r
46 #define comBLOCK_RETRY_TIME 10
\r
47 /*-----------------------------------------------------------*/
\r
49 /* The interrupt handlers are naked functions that call C handlers. The C
\r
50 handlers are marked as noinline to ensure they work correctly when the
\r
52 void interrupt5_handler( void ) __attribute__((naked));
\r
53 static void prvTxHandler( void ) __attribute__((noinline));
\r
54 void interrupt6_handler( void ) __attribute__((naked));
\r
55 static void prvRxHandler( void ) __attribute__((noinline));
\r
57 /*-----------------------------------------------------------*/
\r
59 /* Queues used to hold received characters, and characters waiting to be
\r
61 static QueueHandle_t xRxedChars;
\r
62 static QueueHandle_t xCharsForTx;
\r
63 /*-----------------------------------------------------------*/
\r
65 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
67 /* Create the queues used to hold Rx and Tx characters. */
\r
68 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
69 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
71 if( ( xRxedChars ) && ( xCharsForTx ) )
\r
73 /* Set up interrupts */
\r
74 /* tx interrupt will be enabled when we need to send something */
\r
77 irq[IRQ_UART1_TX].ien = 1;
\r
78 irq[IRQ_UART1_RX].ien = 1;
\r
81 return ( xComPortHandle ) 0;
\r
83 /*-----------------------------------------------------------*/
\r
85 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
87 /* The port handle is not required as this driver only supports uart1. */
\r
90 /* Get the next character from the buffer. Return false if no characters
\r
91 are available, or arrive before xBlockTime expires. */
\r
92 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
101 /*-----------------------------------------------------------*/
\r
103 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
106 signed char *pChNext;
\r
108 /* Send each character in the string, one at a time. */
\r
109 pChNext = ( signed char * )pcString;
\r
110 for( i = 0; i < usStringLength; i++ )
\r
112 /* Block until character has been transmitted. */
\r
113 while( xSerialPutChar( pxPort, *pChNext, comBLOCK_RETRY_TIME ) != pdTRUE ); pChNext++;
\r
116 /*-----------------------------------------------------------*/
\r
118 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
122 /* Place the character in the queue of characters to be transmitted. */
\r
123 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
128 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
129 queue and send it. This does not need to be in a critical section as
\r
130 if the interrupt has already removed the character the next interrupt
\r
131 will simply turn off the Tx interrupt again. */
\r
132 uart1->tx_mask = 1;
\r
136 /*-----------------------------------------------------------*/
\r
138 void vSerialClose( xComPortHandle xPort )
\r
140 /* Not supported as not required by the demo application. */
\r
143 /*-----------------------------------------------------------*/
\r
145 /* UART Tx interrupt handler. */
\r
146 void interrupt5_handler( void )
\r
148 /* This is a naked function. */
\r
149 portSAVE_CONTEXT();
\r
151 portRESTORE_CONTEXT();
\r
153 /*-----------------------------------------------------------*/
\r
155 static void prvTxHandler( void )
\r
158 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
160 /* The interrupt was caused by the transmit fifo having space for at least one
\r
161 character. Are there any more characters to transmit? */
\r
162 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
164 /* A character was retrieved from the queue so can be sent to the uart now. */
\r
165 uart1->tx_data = cChar;
\r
169 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
170 uart1->tx_mask = 0;
\r
173 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
174 ensure that the unblocked task is the task that executes when the interrupt
\r
175 completes if the unblocked task has a priority higher than the interrupted
\r
177 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
179 /*-----------------------------------------------------------*/
\r
181 /* UART Rx interrupt. */
\r
182 void interrupt6_handler( void )
\r
184 portSAVE_CONTEXT();
\r
186 portRESTORE_CONTEXT();
\r
188 /*-----------------------------------------------------------*/
\r
190 static void prvRxHandler( void )
\r
193 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
195 /* The interrupt was caused by the receiver getting data. */
\r
196 cChar = uart1->rx_data;
\r
198 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
200 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
201 ensure that the unblocked task is the task that executes when the interrupt
\r
202 completes if the unblocked task has a priority higher than the interrupted
\r
204 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
206 /*-----------------------------------------------------------*/
\r