2 FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0.
\r
57 This file contains all the serial port components that can be compiled to
\r
58 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
59 contained in serialISR.c.
\r
62 /* Standard includes. */
\r
65 /* Scheduler includes. */
\r
66 #include "FreeRTOS.h"
\r
70 /* Demo application includes. */
\r
72 #include "AT91R40008.h"
\r
77 /*-----------------------------------------------------------*/
\r
79 /* Constants to setup and access the UART. */
\r
80 #define portUSART0_AIC_CHANNEL ( ( unsigned long ) 2 )
\r
82 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
83 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
84 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
86 /*-----------------------------------------------------------*/
\r
88 /* Queues used to hold received characters, and characters waiting to be
\r
90 static xQueueHandle xRxedChars;
\r
91 static xQueueHandle xCharsForTx;
\r
93 /*-----------------------------------------------------------*/
\r
96 * The queues are created in serialISR.c as they are used from the ISR.
\r
97 * Obtain references to the queues and THRE Empty flag.
\r
99 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx );
\r
101 /*-----------------------------------------------------------*/
\r
103 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
105 unsigned long ulSpeed;
\r
106 unsigned long ulCD;
\r
107 xComPortHandle xReturn = serHANDLE;
\r
108 extern void ( vUART_ISR_Wrapper )( void );
\r
110 /* The queues are used in the serial ISR routine, so are created from
\r
111 serialISR.c (which is always compiled to ARM mode. */
\r
112 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );
\r
115 ( xRxedChars != serINVALID_QUEUE ) &&
\r
116 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
117 ( ulWantedBaud != ( unsigned long ) 0 )
\r
120 portENTER_CRITICAL();
\r
122 /* Enable clock to USART0... */
\r
123 AT91C_BASE_PS->PS_PCER = AT91C_PS_US0;
\r
125 /* Disable all USART0 interrupt sources to begin... */
\r
126 AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;
\r
128 /* Reset various status bits (just in case)... */
\r
129 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
131 AT91C_BASE_PIO->PIO_PDR = TXD0 | RXD0; /* Enable RXD and TXD pins */
\r
132 AT91C_BASE_US0->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS;
\r
134 /* Clear Transmit and Receive Counters */
\r
135 AT91C_BASE_US0->US_RCR = 0;
\r
136 AT91C_BASE_US0->US_TCR = 0;
\r
138 /* Input clock to baud rate generator is MCK */
\r
139 ulSpeed = configCPU_CLOCK_HZ * 10;
\r
140 ulSpeed = ulSpeed / 16;
\r
141 ulSpeed = ulSpeed / ulWantedBaud;
\r
143 /* compute the error */
\r
144 ulCD = ulSpeed / 10;
\r
145 if ((ulSpeed - (ulCD * 10)) >= 5)
\r
148 /* Define the baud rate divisor register */
\r
149 AT91C_BASE_US0->US_BRGR = ulCD;
\r
151 /* Define the USART mode */
\r
152 AT91C_BASE_US0->US_MR = US_CLKS_MCK | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1 | US_CHMODE_NORMAL;
\r
154 /* Write the Timeguard Register */
\r
155 AT91C_BASE_US0->US_TTGR = 0;
\r
157 /* Setup the interrupt for USART0.
\r
159 Store interrupt handler function address in USART0 vector register... */
\r
160 AT91C_BASE_AIC->AIC_SVR[ portUSART0_AIC_CHANNEL ] = (unsigned long)vUART_ISR_Wrapper;
\r
162 /* USART0 interrupt level-sensitive, priority 1... */
\r
163 AT91C_BASE_AIC->AIC_SMR[ portUSART0_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 1;
\r
165 /* Clear some pending USART0 interrupts (just in case)... */
\r
166 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
168 /* Enable USART0 interrupt sources (but not Tx for now)... */
\r
169 AT91C_BASE_US0->US_IER = US_RXRDY;
\r
171 /* Enable USART0 interrupts in the AIC... */
\r
172 AT91C_BASE_AIC->AIC_IECR = ( 1 << portUSART0_AIC_CHANNEL );
\r
174 /* Enable receiver and transmitter... */
\r
175 AT91C_BASE_US0->US_CR = US_RXEN | US_TXEN;
\r
177 portEXIT_CRITICAL();
\r
181 xReturn = ( xComPortHandle ) 0;
\r
186 /*-----------------------------------------------------------*/
\r
188 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
190 /* The port handle is not required as this driver only supports UART0. */
\r
193 /* Get the next character from the buffer. Return false if no characters
\r
194 are available, or arrive before xBlockTime expires. */
\r
195 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
204 /*-----------------------------------------------------------*/
\r
206 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
208 signed char *pxNext;
\r
210 /* NOTE: This implementation does not handle the queue being full as no
\r
211 block time is used! */
\r
213 /* The port handle is not required as this driver only supports UART0. */
\r
215 ( void ) usStringLength;
\r
217 /* Send each character in the string, one at a time. */
\r
218 pxNext = ( signed char * ) pcString;
\r
221 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
225 /*-----------------------------------------------------------*/
\r
227 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
231 /* Place the character in the queue of characters to be transmitted. */
\r
232 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
237 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
238 queue and send it. This does not need to be in a critical section as
\r
239 if the interrupt has already removed the character the next interrupt
\r
240 will simply turn off the Tx interrupt again. */
\r
241 AT91C_BASE_US0->US_IER = US_TXRDY;
\r
245 /*-----------------------------------------------------------*/
\r
247 void vSerialClose( xComPortHandle xPort )
\r
249 /* Not supported as not required by the demo application. */
\r
252 /*-----------------------------------------------------------*/
\r