1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2008, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
30 //------------------------------------------------------------------------------
\r
32 //------------------------------------------------------------------------------
\r
35 #include <utility/trace.h>
\r
37 //------------------------------------------------------------------------------
\r
38 // Exported functions
\r
39 //------------------------------------------------------------------------------
\r
40 //------------------------------------------------------------------------------
\r
41 /// Configures an USART peripheral with the specified parameters.
\r
42 /// \param usart Pointer to the USART peripheral to configure.
\r
43 /// \param mode Desired value for the USART mode register (see the datasheet).
\r
44 /// \param baudrate Baudrate at which the USART should operate (in Hz).
\r
45 /// \param masterClock Frequency of the system master clock (in Hz).
\r
46 //------------------------------------------------------------------------------
\r
47 void USART_Configure(AT91S_USART *usart,
\r
49 unsigned int baudrate,
\r
50 unsigned int masterClock)
\r
52 // Reset and disable receiver & transmitter
\r
53 usart->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX
\r
54 | AT91C_US_RXDIS | AT91C_US_TXDIS;
\r
57 usart->US_MR = mode;
\r
59 // Configure baudrate
\r
60 // Asynchronous, no oversampling
\r
61 if (((mode & AT91C_US_SYNC) == 0)
\r
62 && ((mode & AT91C_US_OVER) == 0)) {
\r
64 usart->US_BRGR = (masterClock / baudrate) / 16;
\r
69 //------------------------------------------------------------------------------
\r
70 /// Enables or disables the transmitter of an USART peripheral.
\r
71 /// \param usart Pointer to an USART peripheral
\r
72 /// \param enabled If true, the transmitter is enabled; otherwise it is
\r
74 //------------------------------------------------------------------------------
\r
75 void USART_SetTransmitterEnabled(AT91S_USART *usart,
\r
76 unsigned char enabled)
\r
80 usart->US_CR = AT91C_US_TXEN;
\r
84 usart->US_CR = AT91C_US_TXDIS;
\r
88 //------------------------------------------------------------------------------
\r
89 /// Enables or disables the receiver of an USART peripheral
\r
90 /// \param usart Pointer to an USART peripheral
\r
91 /// \param enabled If true, the receiver is enabled; otherwise it is disabled.
\r
92 //------------------------------------------------------------------------------
\r
93 void USART_SetReceiverEnabled(AT91S_USART *usart,
\r
94 unsigned char enabled)
\r
98 usart->US_CR = AT91C_US_RXEN;
\r
102 usart->US_CR = AT91C_US_RXDIS;
\r
106 //------------------------------------------------------------------------------
\r
107 /// Sends one packet of data through the specified USART peripheral. This
\r
108 /// function operates synchronously, so it only returns when the data has been
\r
110 /// \param usart Pointer to an USART peripheral.
\r
111 /// \param data Data to send including 9nth bit and sync field if necessary (in
\r
112 /// the same format as the US_THR register in the datasheet).
\r
113 /// \param timeOut Time out value (0 = no timeout).
\r
114 //------------------------------------------------------------------------------
\r
116 AT91S_USART *usart,
\r
117 unsigned short data,
\r
118 volatile unsigned int timeOut)
\r
120 if (timeOut == 0) {
\r
122 while ((usart->US_CSR & AT91C_US_TXEMPTY) == 0);
\r
126 while ((usart->US_CSR & AT91C_US_TXEMPTY) == 0) {
\r
128 if (timeOut == 0) {
\r
130 trace_LOG(trace_ERROR, "-E- USART_Write: Timed out.\n\r");
\r
137 usart->US_THR = data;
\r
140 //------------------------------------------------------------------------------
\r
141 /// Sends the contents of a data buffer through the specified USART peripheral.
\r
142 /// This function returns immediately (1 if the buffer has been queued, 0
\r
143 /// otherwise); poll the ENDTX and TXBUFE bits of the USART status register
\r
144 /// to check for the transfer completion.
\r
145 /// \param usart Pointer to an USART peripheral.
\r
146 /// \param buffer Pointer to the data buffer to send.
\r
147 /// \param size Size of the data buffer (in bytes).
\r
148 //------------------------------------------------------------------------------
\r
149 unsigned char USART_WriteBuffer(
\r
150 AT91S_USART *usart,
\r
154 // Check if the first PDC bank is free
\r
155 if ((usart->US_TCR == 0) && (usart->US_TNCR == 0)) {
\r
157 usart->US_TPR = (unsigned int) buffer;
\r
158 usart->US_TCR = size;
\r
159 usart->US_PTCR = AT91C_PDC_TXTEN;
\r
163 // Check if the second PDC bank is free
\r
164 else if (usart->US_TNCR == 0) {
\r
166 usart->US_TNPR = (unsigned int) buffer;
\r
167 usart->US_TNCR = size;
\r
177 //------------------------------------------------------------------------------
\r
178 /// Reads and return a packet of data on the specified USART peripheral. This
\r
179 /// function operates asynchronously, so it waits until some data has been
\r
181 /// \param usart Pointer to an USART peripheral.
\r
182 /// \param timeOut Time out value (0 -> no timeout).
\r
183 //------------------------------------------------------------------------------
\r
184 unsigned short USART_Read(
\r
185 AT91S_USART *usart,
\r
186 volatile unsigned int timeOut)
\r
188 if (timeOut == 0) {
\r
190 while ((usart->US_CSR & AT91C_US_RXRDY) == 0);
\r
194 while ((usart->US_CSR & AT91C_US_RXRDY) == 0) {
\r
196 if (timeOut == 0) {
\r
198 trace_LOG(trace_ERROR, "-E- USART_Read: Timed out.\n\r");
\r
205 return usart->US_RHR;
\r
208 //------------------------------------------------------------------------------
\r
209 /// Reads data from an USART peripheral, filling the provided buffer until it
\r
210 /// becomes full. This function returns immediately with 1 if the buffer has
\r
211 /// been queued for transmission; otherwise 0.
\r
212 /// \param usart Pointer to an USART peripheral.
\r
213 /// \param buffer Pointer to the buffer where the received data will be stored.
\r
214 /// \param size Size of the data buffer (in bytes).
\r
215 //------------------------------------------------------------------------------
\r
216 unsigned char USART_ReadBuffer(AT91S_USART *usart,
\r
220 // Check if the first PDC bank is free
\r
221 if ((usart->US_RCR == 0) && (usart->US_RNCR == 0)) {
\r
223 usart->US_RPR = (unsigned int) buffer;
\r
224 usart->US_RCR = size;
\r
225 usart->US_PTCR = AT91C_PDC_RXTEN;
\r
229 // Check if the second PDC bank is free
\r
230 else if (usart->US_RNCR == 0) {
\r
232 usart->US_RNPR = (unsigned int) buffer;
\r
233 usart->US_RNCR = size;
\r
243 //------------------------------------------------------------------------------
\r
244 /// Returns 1 if some data has been received and can be read from an USART;
\r
245 /// otherwise returns 0.
\r
246 /// \param usart Pointer to an AT91S_USART instance.
\r
247 //------------------------------------------------------------------------------
\r
248 unsigned char USART_IsDataAvailable(AT91S_USART *usart)
\r
250 if ((usart->US_CSR & AT91C_US_RXRDY) != 0) {
\r