2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0.
\r
68 This file contains all the serial port components that can be compiled to
\r
69 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
70 contained in serialISR.c.
\r
73 /* Standard includes. */
\r
76 /* Scheduler includes. */
\r
77 #include "FreeRTOS.h"
\r
81 /* Demo application includes. */
\r
83 #include "AT91R40008.h"
\r
88 /*-----------------------------------------------------------*/
\r
90 /* Constants to setup and access the UART. */
\r
91 #define portUSART0_AIC_CHANNEL ( ( unsigned long ) 2 )
\r
93 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
94 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
95 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
97 /*-----------------------------------------------------------*/
\r
99 /* Queues used to hold received characters, and characters waiting to be
\r
101 static xQueueHandle xRxedChars;
\r
102 static xQueueHandle xCharsForTx;
\r
104 /*-----------------------------------------------------------*/
\r
107 * The queues are created in serialISR.c as they are used from the ISR.
\r
108 * Obtain references to the queues and THRE Empty flag.
\r
110 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx );
\r
112 /*-----------------------------------------------------------*/
\r
114 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
116 unsigned long ulSpeed;
\r
117 unsigned long ulCD;
\r
118 xComPortHandle xReturn = serHANDLE;
\r
119 extern void ( vUART_ISR_Wrapper )( void );
\r
121 /* The queues are used in the serial ISR routine, so are created from
\r
122 serialISR.c (which is always compiled to ARM mode. */
\r
123 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );
\r
126 ( xRxedChars != serINVALID_QUEUE ) &&
\r
127 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
128 ( ulWantedBaud != ( unsigned long ) 0 )
\r
131 portENTER_CRITICAL();
\r
133 /* Enable clock to USART0... */
\r
134 AT91C_BASE_PS->PS_PCER = AT91C_PS_US0;
\r
136 /* Disable all USART0 interrupt sources to begin... */
\r
137 AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;
\r
139 /* Reset various status bits (just in case)... */
\r
140 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
142 AT91C_BASE_PIO->PIO_PDR = TXD0 | RXD0; /* Enable RXD and TXD pins */
\r
143 AT91C_BASE_US0->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS;
\r
145 /* Clear Transmit and Receive Counters */
\r
146 AT91C_BASE_US0->US_RCR = 0;
\r
147 AT91C_BASE_US0->US_TCR = 0;
\r
149 /* Input clock to baud rate generator is MCK */
\r
150 ulSpeed = configCPU_CLOCK_HZ * 10;
\r
151 ulSpeed = ulSpeed / 16;
\r
152 ulSpeed = ulSpeed / ulWantedBaud;
\r
154 /* compute the error */
\r
155 ulCD = ulSpeed / 10;
\r
156 if ((ulSpeed - (ulCD * 10)) >= 5)
\r
159 /* Define the baud rate divisor register */
\r
160 AT91C_BASE_US0->US_BRGR = ulCD;
\r
162 /* Define the USART mode */
\r
163 AT91C_BASE_US0->US_MR = US_CLKS_MCK | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1 | US_CHMODE_NORMAL;
\r
165 /* Write the Timeguard Register */
\r
166 AT91C_BASE_US0->US_TTGR = 0;
\r
168 /* Setup the interrupt for USART0.
\r
170 Store interrupt handler function address in USART0 vector register... */
\r
171 AT91C_BASE_AIC->AIC_SVR[ portUSART0_AIC_CHANNEL ] = (unsigned long)vUART_ISR_Wrapper;
\r
173 /* USART0 interrupt level-sensitive, priority 1... */
\r
174 AT91C_BASE_AIC->AIC_SMR[ portUSART0_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 1;
\r
176 /* Clear some pending USART0 interrupts (just in case)... */
\r
177 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
179 /* Enable USART0 interrupt sources (but not Tx for now)... */
\r
180 AT91C_BASE_US0->US_IER = US_RXRDY;
\r
182 /* Enable USART0 interrupts in the AIC... */
\r
183 AT91C_BASE_AIC->AIC_IECR = ( 1 << portUSART0_AIC_CHANNEL );
\r
185 /* Enable receiver and transmitter... */
\r
186 AT91C_BASE_US0->US_CR = US_RXEN | US_TXEN;
\r
188 portEXIT_CRITICAL();
\r
192 xReturn = ( xComPortHandle ) 0;
\r
197 /*-----------------------------------------------------------*/
\r
199 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
201 /* The port handle is not required as this driver only supports UART0. */
\r
204 /* Get the next character from the buffer. Return false if no characters
\r
205 are available, or arrive before xBlockTime expires. */
\r
206 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
215 /*-----------------------------------------------------------*/
\r
217 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
219 signed char *pxNext;
\r
221 /* NOTE: This implementation does not handle the queue being full as no
\r
222 block time is used! */
\r
224 /* The port handle is not required as this driver only supports UART0. */
\r
226 ( void ) usStringLength;
\r
228 /* Send each character in the string, one at a time. */
\r
229 pxNext = ( signed char * ) pcString;
\r
232 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
236 /*-----------------------------------------------------------*/
\r
238 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
242 /* Place the character in the queue of characters to be transmitted. */
\r
243 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
248 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
249 queue and send it. This does not need to be in a critical section as
\r
250 if the interrupt has already removed the character the next interrupt
\r
251 will simply turn off the Tx interrupt again. */
\r
252 AT91C_BASE_US0->US_IER = US_TXRDY;
\r
256 /*-----------------------------------------------------------*/
\r
258 void vSerialClose( xComPortHandle xPort )
\r
260 /* Not supported as not required by the demo application. */
\r
263 /*-----------------------------------------------------------*/
\r