2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0.
\r
74 This file contains all the serial port components that must be compiled
\r
75 to ARM mode. The components that can be compiled to either ARM or THUMB
\r
76 mode are contained in serial.c.
\r
80 /* Standard includes. */
\r
83 /* Scheduler includes. */
\r
84 #include "FreeRTOS.h"
\r
88 /* Demo application includes. */
\r
90 #include "AT91R40008.h"
\r
93 /*-----------------------------------------------------------*/
\r
95 /* Constant to access the AIC. */
\r
96 #define serCLEAR_AIC_INTERRUPT ( ( unsigned long ) 0 )
\r
98 /* Constants to determine the ISR source. */
\r
99 #define serSOURCE_THRE ( ( unsigned char ) 0x02 )
\r
100 #define serSOURCE_RX_TIMEOUT ( ( unsigned char ) 0x0c )
\r
101 #define serSOURCE_ERROR ( ( unsigned char ) 0x06 )
\r
102 #define serSOURCE_RX ( ( unsigned char ) 0x04 )
\r
103 #define serINTERRUPT_SOURCE_MASK ( ( unsigned long ) (US_RXRDY | US_TXRDY | US_RXBRK | US_OVRE | US_FRAME | US_PARE) )
\r
105 /* Queues used to hold received characters, and characters waiting to be
\r
107 static QueueHandle_t xRxedChars;
\r
108 static QueueHandle_t xCharsForTx;
\r
110 /*-----------------------------------------------------------*/
\r
112 /* UART0 interrupt service routine. This can cause a context switch so MUST
\r
113 be declared "naked". */
\r
114 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));
\r
116 /* The ISR function that actually performs the work. This must be separate
\r
117 from the wrapper to ensure the correct stack frame is set up. */
\r
118 void vUART_ISR_Handler( void ) __attribute__ ((noinline));
\r
120 /*-----------------------------------------------------------*/
\r
121 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx )
\r
123 /* Create the queues used to hold Rx and Tx characters. */
\r
124 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
125 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
127 /* Pass back a reference to the queues so the serial API file can
\r
128 post/receive characters. */
\r
129 *pxRxedChars = xRxedChars;
\r
130 *pxCharsForTx = xCharsForTx;
\r
132 /*-----------------------------------------------------------*/
\r
134 void vUART_ISR_Wrapper( void )
\r
136 /* Save the context of the interrupted task. */
\r
137 portSAVE_CONTEXT();
\r
139 /* Call the handler. This must be a separate function to ensure the
\r
140 stack frame is correctly set up. */
\r
141 __asm volatile( "bl vUART_ISR_Handler" );
\r
143 /* Restore the context of whichever task will run next. */
\r
144 portRESTORE_CONTEXT();
\r
146 /*-----------------------------------------------------------*/
\r
148 void vUART_ISR_Handler( void )
\r
150 /* Now we can declare the local variables. These must be static. */
\r
152 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
153 unsigned long ulStatus;
\r
155 /* What caused the interrupt? */
\r
156 ulStatus = AT91C_BASE_US0->US_CSR & AT91C_BASE_US0->US_IMR;
\r
158 if (ulStatus & US_TXRDY)
\r
160 /* The interrupt was caused by the THR becoming empty. Are there any
\r
161 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
\r
166 AT91C_BASE_US0->US_THR = cChar;
\r
170 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
171 AT91C_BASE_US0->US_IDR = US_TXRDY;
\r
175 if (ulStatus & US_RXRDY)
\r
177 /* The interrupt was caused by the receiver getting data. */
\r
178 cChar = AT91C_BASE_US0->US_RHR;
\r
180 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken);
\r
183 /* Acknowledge the interrupt at AIC level... */
\r
184 AT91C_BASE_AIC->AIC_EOICR = serCLEAR_AIC_INTERRUPT;
\r
186 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
187 ensure that the unblocked task is the task that executes when the interrupt
\r
188 completes if the unblocked task has a priority higher than the interrupted
\r
190 if( xHigherPriorityTaskWoken )
\r
192 portYIELD_FROM_ISR();
\r
195 /*-----------------------------------------------------------*/
\r