2 * FreeRTOS Kernel V10.0.1
\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
30 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
32 This file contains all the serial port components that must be compiled
\r
33 to ARM mode. The components that can be compiled to either ARM or THUMB
\r
34 mode are contained in serial.c.
\r
38 /* Standard includes. */
\r
41 /* Scheduler includes. */
\r
42 #include "FreeRTOS.h"
\r
46 /* Demo application includes. */
\r
49 /*-----------------------------------------------------------*/
\r
51 /* Constant to access the VIC. */
\r
52 #define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
\r
54 /* Constants to determine the ISR source. */
\r
55 #define serSOURCE_THRE ( ( unsigned char ) 0x02 )
\r
56 #define serSOURCE_RX_TIMEOUT ( ( unsigned char ) 0x0c )
\r
57 #define serSOURCE_ERROR ( ( unsigned char ) 0x06 )
\r
58 #define serSOURCE_RX ( ( unsigned char ) 0x04 )
\r
59 #define serINTERRUPT_SOURCE_MASK ( ( unsigned char ) 0x0f )
\r
61 /* Queues used to hold received characters, and characters waiting to be
\r
63 static QueueHandle_t xRxedChars;
\r
64 static QueueHandle_t xCharsForTx;
\r
65 static volatile long lTHREEmpty;
\r
67 /*-----------------------------------------------------------*/
\r
70 * The queues are created in serialISR.c as they are used from the ISR.
\r
71 * Obtain references to the queues and THRE Empty flag.
\r
73 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag );
\r
75 /* UART0 interrupt service routine entry point. */
\r
76 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));
\r
78 /* UART0 interrupt service routine handler. */
\r
79 void vUART_ISR_Handler( void ) __attribute__ ((noinline));
\r
81 /*-----------------------------------------------------------*/
\r
82 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars,
\r
83 QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag )
\r
85 /* Create the queues used to hold Rx and Tx characters. */
\r
86 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
87 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
89 /* Pass back a reference to the queues so the serial API file can
\r
90 post/receive characters. */
\r
91 *pxRxedChars = xRxedChars;
\r
92 *pxCharsForTx = xCharsForTx;
\r
94 /* Initialise the THRE empty flag - and pass back a reference. */
\r
95 lTHREEmpty = ( long ) pdTRUE;
\r
96 *pplTHREEmptyFlag = &lTHREEmpty;
\r
98 /*-----------------------------------------------------------*/
\r
100 void vUART_ISR_Wrapper( void )
\r
102 /* Save the context of the interrupted task. */
\r
103 portSAVE_CONTEXT();
\r
105 /* Call the handler. This must be a separate function from the wrapper
\r
106 to ensure the correct stack frame is set up. */
\r
107 __asm volatile ("bl vUART_ISR_Handler");
\r
109 /* Restore the context of whichever task is going to run next. */
\r
110 portRESTORE_CONTEXT();
\r
112 /*-----------------------------------------------------------*/
\r
114 void vUART_ISR_Handler( void )
\r
117 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
119 /* What caused the interrupt? */
\r
120 switch( UART0_IIR & serINTERRUPT_SOURCE_MASK )
\r
122 case serSOURCE_ERROR : /* Not handling this, but clear the interrupt. */
\r
126 case serSOURCE_THRE : /* The THRE is empty. If there is another
\r
127 character in the Tx queue, send it now. */
\r
128 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
134 /* There are no further characters
\r
135 queued to send so we can indicate
\r
136 that the THRE is available. */
\r
137 lTHREEmpty = pdTRUE;
\r
141 case serSOURCE_RX_TIMEOUT :
\r
142 case serSOURCE_RX : /* A character was received. Place it in
\r
143 the queue of received characters. */
\r
145 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
148 default : /* There is nothing to do, leave the ISR. */
\r
152 if( xHigherPriorityTaskWoken )
\r
154 portYIELD_FROM_ISR();
\r
157 /* Clear the ISR in the VIC. */
\r
158 VICVectAddr = serCLEAR_VIC_INTERRUPT;
\r