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
29 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0.
\r
31 This file contains all the serial port components that can be compiled to
\r
32 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
33 contained in serialISR.c.
\r
36 /* Standard includes. */
\r
39 /* Scheduler includes. */
\r
40 #include "FreeRTOS.h"
\r
44 /* Demo application includes. */
\r
46 #include "AT91R40008.h"
\r
51 /*-----------------------------------------------------------*/
\r
53 /* Constants to setup and access the UART. */
\r
54 #define portUSART0_AIC_CHANNEL ( ( unsigned long ) 2 )
\r
56 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
57 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
58 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
60 /*-----------------------------------------------------------*/
\r
62 /* Queues used to hold received characters, and characters waiting to be
\r
64 static QueueHandle_t xRxedChars;
\r
65 static QueueHandle_t xCharsForTx;
\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 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx );
\r
75 /*-----------------------------------------------------------*/
\r
77 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
79 unsigned long ulSpeed;
\r
81 xComPortHandle xReturn = serHANDLE;
\r
82 extern void ( vUART_ISR_Wrapper )( void );
\r
84 /* The queues are used in the serial ISR routine, so are created from
\r
85 serialISR.c (which is always compiled to ARM mode. */
\r
86 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );
\r
89 ( xRxedChars != serINVALID_QUEUE ) &&
\r
90 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
91 ( ulWantedBaud != ( unsigned long ) 0 )
\r
94 portENTER_CRITICAL();
\r
96 /* Enable clock to USART0... */
\r
97 AT91C_BASE_PS->PS_PCER = AT91C_PS_US0;
\r
99 /* Disable all USART0 interrupt sources to begin... */
\r
100 AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;
\r
102 /* Reset various status bits (just in case)... */
\r
103 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
105 AT91C_BASE_PIO->PIO_PDR = TXD0 | RXD0; /* Enable RXD and TXD pins */
\r
106 AT91C_BASE_US0->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS;
\r
108 /* Clear Transmit and Receive Counters */
\r
109 AT91C_BASE_US0->US_RCR = 0;
\r
110 AT91C_BASE_US0->US_TCR = 0;
\r
112 /* Input clock to baud rate generator is MCK */
\r
113 ulSpeed = configCPU_CLOCK_HZ * 10;
\r
114 ulSpeed = ulSpeed / 16;
\r
115 ulSpeed = ulSpeed / ulWantedBaud;
\r
117 /* compute the error */
\r
118 ulCD = ulSpeed / 10;
\r
119 if ((ulSpeed - (ulCD * 10)) >= 5)
\r
122 /* Define the baud rate divisor register */
\r
123 AT91C_BASE_US0->US_BRGR = ulCD;
\r
125 /* Define the USART mode */
\r
126 AT91C_BASE_US0->US_MR = US_CLKS_MCK | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1 | US_CHMODE_NORMAL;
\r
128 /* Write the Timeguard Register */
\r
129 AT91C_BASE_US0->US_TTGR = 0;
\r
131 /* Setup the interrupt for USART0.
\r
133 Store interrupt handler function address in USART0 vector register... */
\r
134 AT91C_BASE_AIC->AIC_SVR[ portUSART0_AIC_CHANNEL ] = (unsigned long)vUART_ISR_Wrapper;
\r
136 /* USART0 interrupt level-sensitive, priority 1... */
\r
137 AT91C_BASE_AIC->AIC_SMR[ portUSART0_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 1;
\r
139 /* Clear some pending USART0 interrupts (just in case)... */
\r
140 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
142 /* Enable USART0 interrupt sources (but not Tx for now)... */
\r
143 AT91C_BASE_US0->US_IER = US_RXRDY;
\r
145 /* Enable USART0 interrupts in the AIC... */
\r
146 AT91C_BASE_AIC->AIC_IECR = ( 1 << portUSART0_AIC_CHANNEL );
\r
148 /* Enable receiver and transmitter... */
\r
149 AT91C_BASE_US0->US_CR = US_RXEN | US_TXEN;
\r
151 portEXIT_CRITICAL();
\r
155 xReturn = ( xComPortHandle ) 0;
\r
160 /*-----------------------------------------------------------*/
\r
162 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
164 /* The port handle is not required as this driver only supports UART0. */
\r
167 /* Get the next character from the buffer. Return false if no characters
\r
168 are available, or arrive before xBlockTime expires. */
\r
169 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
178 /*-----------------------------------------------------------*/
\r
180 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
182 signed char *pxNext;
\r
184 /* NOTE: This implementation does not handle the queue being full as no
\r
185 block time is used! */
\r
187 /* The port handle is not required as this driver only supports UART0. */
\r
189 ( void ) usStringLength;
\r
191 /* Send each character in the string, one at a time. */
\r
192 pxNext = ( signed char * ) pcString;
\r
195 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
199 /*-----------------------------------------------------------*/
\r
201 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
205 /* Place the character in the queue of characters to be transmitted. */
\r
206 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
211 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
212 queue and send it. This does not need to be in a critical section as
\r
213 if the interrupt has already removed the character the next interrupt
\r
214 will simply turn off the Tx interrupt again. */
\r
215 AT91C_BASE_US0->US_IER = US_TXRDY;
\r
219 /*-----------------------------------------------------------*/
\r
221 void vSerialClose( xComPortHandle xPort )
\r
223 /* Not supported as not required by the demo application. */
\r
226 /*-----------------------------------------------------------*/
\r