2 FreeRTOS V9.0.1 - Copyright (C) 2017 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
71 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0.
\r
73 This file contains all the serial port components that can be compiled to
\r
74 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
75 contained in serialISR.c.
\r
78 /* Standard includes. */
\r
81 /* Scheduler includes. */
\r
82 #include "FreeRTOS.h"
\r
86 /* Demo application includes. */
\r
88 #include "AT91R40008.h"
\r
93 /*-----------------------------------------------------------*/
\r
95 /* Constants to setup and access the UART. */
\r
96 #define portUSART0_AIC_CHANNEL ( ( unsigned long ) 2 )
\r
98 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
99 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
100 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
102 /*-----------------------------------------------------------*/
\r
104 /* Queues used to hold received characters, and characters waiting to be
\r
106 static QueueHandle_t xRxedChars;
\r
107 static QueueHandle_t xCharsForTx;
\r
109 /*-----------------------------------------------------------*/
\r
112 * The queues are created in serialISR.c as they are used from the ISR.
\r
113 * Obtain references to the queues and THRE Empty flag.
\r
115 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx );
\r
117 /*-----------------------------------------------------------*/
\r
119 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
121 unsigned long ulSpeed;
\r
122 unsigned long ulCD;
\r
123 xComPortHandle xReturn = serHANDLE;
\r
124 extern void ( vUART_ISR_Wrapper )( void );
\r
126 /* The queues are used in the serial ISR routine, so are created from
\r
127 serialISR.c (which is always compiled to ARM mode. */
\r
128 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );
\r
131 ( xRxedChars != serINVALID_QUEUE ) &&
\r
132 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
133 ( ulWantedBaud != ( unsigned long ) 0 )
\r
136 portENTER_CRITICAL();
\r
138 /* Enable clock to USART0... */
\r
139 AT91C_BASE_PS->PS_PCER = AT91C_PS_US0;
\r
141 /* Disable all USART0 interrupt sources to begin... */
\r
142 AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;
\r
144 /* Reset various status bits (just in case)... */
\r
145 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
147 AT91C_BASE_PIO->PIO_PDR = TXD0 | RXD0; /* Enable RXD and TXD pins */
\r
148 AT91C_BASE_US0->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS;
\r
150 /* Clear Transmit and Receive Counters */
\r
151 AT91C_BASE_US0->US_RCR = 0;
\r
152 AT91C_BASE_US0->US_TCR = 0;
\r
154 /* Input clock to baud rate generator is MCK */
\r
155 ulSpeed = configCPU_CLOCK_HZ * 10;
\r
156 ulSpeed = ulSpeed / 16;
\r
157 ulSpeed = ulSpeed / ulWantedBaud;
\r
159 /* compute the error */
\r
160 ulCD = ulSpeed / 10;
\r
161 if ((ulSpeed - (ulCD * 10)) >= 5)
\r
164 /* Define the baud rate divisor register */
\r
165 AT91C_BASE_US0->US_BRGR = ulCD;
\r
167 /* Define the USART mode */
\r
168 AT91C_BASE_US0->US_MR = US_CLKS_MCK | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1 | US_CHMODE_NORMAL;
\r
170 /* Write the Timeguard Register */
\r
171 AT91C_BASE_US0->US_TTGR = 0;
\r
173 /* Setup the interrupt for USART0.
\r
175 Store interrupt handler function address in USART0 vector register... */
\r
176 AT91C_BASE_AIC->AIC_SVR[ portUSART0_AIC_CHANNEL ] = (unsigned long)vUART_ISR_Wrapper;
\r
178 /* USART0 interrupt level-sensitive, priority 1... */
\r
179 AT91C_BASE_AIC->AIC_SMR[ portUSART0_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 1;
\r
181 /* Clear some pending USART0 interrupts (just in case)... */
\r
182 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
184 /* Enable USART0 interrupt sources (but not Tx for now)... */
\r
185 AT91C_BASE_US0->US_IER = US_RXRDY;
\r
187 /* Enable USART0 interrupts in the AIC... */
\r
188 AT91C_BASE_AIC->AIC_IECR = ( 1 << portUSART0_AIC_CHANNEL );
\r
190 /* Enable receiver and transmitter... */
\r
191 AT91C_BASE_US0->US_CR = US_RXEN | US_TXEN;
\r
193 portEXIT_CRITICAL();
\r
197 xReturn = ( xComPortHandle ) 0;
\r
202 /*-----------------------------------------------------------*/
\r
204 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
206 /* The port handle is not required as this driver only supports UART0. */
\r
209 /* Get the next character from the buffer. Return false if no characters
\r
210 are available, or arrive before xBlockTime expires. */
\r
211 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
220 /*-----------------------------------------------------------*/
\r
222 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
224 signed char *pxNext;
\r
226 /* NOTE: This implementation does not handle the queue being full as no
\r
227 block time is used! */
\r
229 /* The port handle is not required as this driver only supports UART0. */
\r
231 ( void ) usStringLength;
\r
233 /* Send each character in the string, one at a time. */
\r
234 pxNext = ( signed char * ) pcString;
\r
237 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
241 /*-----------------------------------------------------------*/
\r
243 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
247 /* Place the character in the queue of characters to be transmitted. */
\r
248 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
253 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
254 queue and send it. This does not need to be in a critical section as
\r
255 if the interrupt has already removed the character the next interrupt
\r
256 will simply turn off the Tx interrupt again. */
\r
257 AT91C_BASE_US0->US_IER = US_TXRDY;
\r
261 /*-----------------------------------------------------------*/
\r
263 void vSerialClose( xComPortHandle xPort )
\r
265 /* Not supported as not required by the demo application. */
\r
268 /*-----------------------------------------------------------*/
\r