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
30 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
33 /* Standard includes. */
\r
36 /* Scheduler includes. */
\r
37 #include "FreeRTOS.h"
\r
40 /* Demo application includes. */
\r
43 /*-----------------------------------------------------------*/
\r
45 /* Location of the COM0 registers. */
\r
46 #define serCOM0 ( ( AT91PS_USART ) AT91C_BASE_US0 )
\r
48 /* Interrupt control macros. */
\r
49 #define serINTERRUPT_LEVEL ( 5 )
\r
50 #define vInterruptOn() AT91F_US_EnableIt( serCOM0, AT91C_US_TXRDY | AT91C_US_RXRDY )
\r
51 #define vInterruptOff() AT91F_US_DisableIt( serCOM0, AT91C_US_TXRDY )
\r
53 /* Misc constants. */
\r
54 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
55 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
56 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
57 #define serNO_TIMEGUARD ( ( unsigned long ) 0 )
\r
58 #define serNO_PERIPHERAL_B_SETUP ( ( unsigned long ) 0 )
\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
66 /*-----------------------------------------------------------*/
\r
68 /* Interrupt entry point written in the assembler file serialISR.s79. */
\r
69 extern void vSerialISREntry( void );
\r
71 /* The interrupt service routine - called from the assembly entry point. */
\r
72 __arm void vSerialISR( void );
\r
74 /*-----------------------------------------------------------*/
\r
77 * See the serial2.h header file.
\r
79 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
81 xComPortHandle xReturn = serHANDLE;
\r
82 extern void ( vUART_ISR )( void );
\r
84 /* Create the queues used to hold Rx and Tx characters. */
\r
85 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
86 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
88 /* If the queues were created correctly then setup the serial port
\r
90 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
92 portENTER_CRITICAL();
\r
94 /* Enable the USART clock. */
\r
95 AT91F_PMC_EnablePeriphClock( AT91C_BASE_PMC, 1 << AT91C_ID_US0 );
\r
97 AT91F_PIO_CfgPeriph( AT91C_BASE_PIOA, ( ( unsigned long ) AT91C_PA5_RXD0 ) | ( ( unsigned long ) AT91C_PA6_TXD0 ), serNO_PERIPHERAL_B_SETUP );
\r
99 /* Set the required protocol. */
\r
100 AT91F_US_Configure( serCOM0, configCPU_CLOCK_HZ, AT91C_US_ASYNC_MODE, ulWantedBaud, serNO_TIMEGUARD );
\r
102 /* Enable Rx and Tx. */
\r
103 serCOM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
\r
105 /* Enable the Rx interrupts. The Tx interrupts are not enabled
\r
106 until there are characters to be transmitted. */
\r
107 AT91F_US_EnableIt( serCOM0, AT91C_US_RXRDY );
\r
109 /* Enable the interrupts in the AIC. */
\r
110 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_US0, serINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, ( void (*)( void ) ) vSerialISREntry );
\r
111 AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_US0 );
\r
113 portEXIT_CRITICAL();
\r
117 xReturn = ( xComPortHandle ) 0;
\r
120 /* This demo file only supports a single port but we have to return
\r
121 something to comply with the standard demo header file. */
\r
124 /*-----------------------------------------------------------*/
\r
126 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
128 /* The port handle is not required as this driver only supports one port. */
\r
131 /* Get the next character from the buffer. Return false if no characters
\r
132 are available, or arrive before xBlockTime expires. */
\r
133 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
142 /*-----------------------------------------------------------*/
\r
144 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
146 signed char *pxNext;
\r
148 /* A couple of parameters that this port does not use. */
\r
149 ( void ) usStringLength;
\r
152 /* NOTE: This implementation does not handle the queue being full as no
\r
153 block time is used! */
\r
155 /* The port handle is not required as this driver only supports UART0. */
\r
158 /* Send each character in the string, one at a time. */
\r
159 pxNext = ( signed char * ) pcString;
\r
162 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
166 /*-----------------------------------------------------------*/
\r
168 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
170 /* Place the character in the queue of characters to be transmitted. */
\r
171 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
176 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
177 queue and send it. This does not need to be in a critical section as
\r
178 if the interrupt has already removed the character the next interrupt
\r
179 will simply turn off the Tx interrupt again. */
\r
184 /*-----------------------------------------------------------*/
\r
186 void vSerialClose( xComPortHandle xPort )
\r
188 /* Not supported as not required by the demo application. */
\r
190 /*-----------------------------------------------------------*/
\r
192 /* Serial port ISR. This can cause a context switch so is not defined as a
\r
193 standard ISR using the __irq keyword. Instead a wrapper function is defined
\r
194 within serialISR.s79 which in turn calls this function. See the port
\r
195 documentation on the FreeRTOS.org website for more information. */
\r
196 __arm void vSerialISR( void )
\r
198 unsigned long ulStatus;
\r
200 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
202 /* What caused the interrupt? */
\r
203 ulStatus = serCOM0->US_CSR &= serCOM0->US_IMR;
\r
205 if( ulStatus & AT91C_US_TXRDY )
\r
207 /* The interrupt was caused by the THR becoming empty. Are there any
\r
208 more characters to transmit? */
\r
209 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
211 /* A character was retrieved from the queue so can be sent to the
\r
213 serCOM0->US_THR = cChar;
\r
217 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
222 if( ulStatus & AT91C_US_RXRDY )
\r
224 /* The interrupt was caused by a character being received. Grab the
\r
225 character from the RHR and place it in the queue or received
\r
227 cChar = serCOM0->US_RHR;
\r
228 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
231 /* If a task was woken by either a character being received or a character
\r
232 being transmitted then we may need to switch to another task. */
\r
233 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
235 /* End the interrupt in the AIC. */
\r
236 AT91C_BASE_AIC->AIC_EOICR = 0;
\r