2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0.
\r
69 This file contains all the serial port components that can be compiled to
\r
70 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
71 contained in serialISR.c.
\r
74 /* Standard includes. */
\r
77 /* Scheduler includes. */
\r
78 #include "FreeRTOS.h"
\r
82 /* Demo application includes. */
\r
84 #include "AT91R40008.h"
\r
89 /*-----------------------------------------------------------*/
\r
91 /* Constants to setup and access the UART. */
\r
92 #define portUSART0_AIC_CHANNEL ( ( unsigned long ) 2 )
\r
94 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
95 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
96 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
98 /*-----------------------------------------------------------*/
\r
100 /* Queues used to hold received characters, and characters waiting to be
\r
102 static QueueHandle_t xRxedChars;
\r
103 static QueueHandle_t xCharsForTx;
\r
105 /*-----------------------------------------------------------*/
\r
108 * The queues are created in serialISR.c as they are used from the ISR.
\r
109 * Obtain references to the queues and THRE Empty flag.
\r
111 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx );
\r
113 /*-----------------------------------------------------------*/
\r
115 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
117 unsigned long ulSpeed;
\r
118 unsigned long ulCD;
\r
119 xComPortHandle xReturn = serHANDLE;
\r
120 extern void ( vUART_ISR_Wrapper )( void );
\r
122 /* The queues are used in the serial ISR routine, so are created from
\r
123 serialISR.c (which is always compiled to ARM mode. */
\r
124 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );
\r
127 ( xRxedChars != serINVALID_QUEUE ) &&
\r
128 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
129 ( ulWantedBaud != ( unsigned long ) 0 )
\r
132 portENTER_CRITICAL();
\r
134 /* Enable clock to USART0... */
\r
135 AT91C_BASE_PS->PS_PCER = AT91C_PS_US0;
\r
137 /* Disable all USART0 interrupt sources to begin... */
\r
138 AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;
\r
140 /* Reset various status bits (just in case)... */
\r
141 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
143 AT91C_BASE_PIO->PIO_PDR = TXD0 | RXD0; /* Enable RXD and TXD pins */
\r
144 AT91C_BASE_US0->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS;
\r
146 /* Clear Transmit and Receive Counters */
\r
147 AT91C_BASE_US0->US_RCR = 0;
\r
148 AT91C_BASE_US0->US_TCR = 0;
\r
150 /* Input clock to baud rate generator is MCK */
\r
151 ulSpeed = configCPU_CLOCK_HZ * 10;
\r
152 ulSpeed = ulSpeed / 16;
\r
153 ulSpeed = ulSpeed / ulWantedBaud;
\r
155 /* compute the error */
\r
156 ulCD = ulSpeed / 10;
\r
157 if ((ulSpeed - (ulCD * 10)) >= 5)
\r
160 /* Define the baud rate divisor register */
\r
161 AT91C_BASE_US0->US_BRGR = ulCD;
\r
163 /* Define the USART mode */
\r
164 AT91C_BASE_US0->US_MR = US_CLKS_MCK | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1 | US_CHMODE_NORMAL;
\r
166 /* Write the Timeguard Register */
\r
167 AT91C_BASE_US0->US_TTGR = 0;
\r
169 /* Setup the interrupt for USART0.
\r
171 Store interrupt handler function address in USART0 vector register... */
\r
172 AT91C_BASE_AIC->AIC_SVR[ portUSART0_AIC_CHANNEL ] = (unsigned long)vUART_ISR_Wrapper;
\r
174 /* USART0 interrupt level-sensitive, priority 1... */
\r
175 AT91C_BASE_AIC->AIC_SMR[ portUSART0_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 1;
\r
177 /* Clear some pending USART0 interrupts (just in case)... */
\r
178 AT91C_BASE_US0->US_CR = US_RSTSTA;
\r
180 /* Enable USART0 interrupt sources (but not Tx for now)... */
\r
181 AT91C_BASE_US0->US_IER = US_RXRDY;
\r
183 /* Enable USART0 interrupts in the AIC... */
\r
184 AT91C_BASE_AIC->AIC_IECR = ( 1 << portUSART0_AIC_CHANNEL );
\r
186 /* Enable receiver and transmitter... */
\r
187 AT91C_BASE_US0->US_CR = US_RXEN | US_TXEN;
\r
189 portEXIT_CRITICAL();
\r
193 xReturn = ( xComPortHandle ) 0;
\r
198 /*-----------------------------------------------------------*/
\r
200 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
202 /* The port handle is not required as this driver only supports UART0. */
\r
205 /* Get the next character from the buffer. Return false if no characters
\r
206 are available, or arrive before xBlockTime expires. */
\r
207 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
216 /*-----------------------------------------------------------*/
\r
218 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
220 signed char *pxNext;
\r
222 /* NOTE: This implementation does not handle the queue being full as no
\r
223 block time is used! */
\r
225 /* The port handle is not required as this driver only supports UART0. */
\r
227 ( void ) usStringLength;
\r
229 /* Send each character in the string, one at a time. */
\r
230 pxNext = ( signed char * ) pcString;
\r
233 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
237 /*-----------------------------------------------------------*/
\r
239 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
243 /* Place the character in the queue of characters to be transmitted. */
\r
244 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
249 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
250 queue and send it. This does not need to be in a critical section as
\r
251 if the interrupt has already removed the character the next interrupt
\r
252 will simply turn off the Tx interrupt again. */
\r
253 AT91C_BASE_US0->US_IER = US_TXRDY;
\r
257 /*-----------------------------------------------------------*/
\r
259 void vSerialClose( xComPortHandle xPort )
\r
261 /* Not supported as not required by the demo application. */
\r
264 /*-----------------------------------------------------------*/
\r