1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2014, 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
34 * Implementation of UART (Universal Asynchronous Receiver Transmitter)
\r
38 /*------------------------------------------------------------------------------
\r
40 *------------------------------------------------------------------------------*/
\r
46 /*----------------------------------------------------------------------------
\r
48 *----------------------------------------------------------------------------*/
\r
51 /*------------------------------------------------------------------------------
\r
52 * Exported functions
\r
53 *------------------------------------------------------------------------------*/
\r
56 * \brief Configures an UART peripheral with the specified parameters.
\r
59 * \param uart Pointer to the UART peripheral to configure.
\r
60 * \param mode Desired value for the UART mode register (see the datasheet).
\r
61 * \param baudrate Baudrate at which the UART should operate (in Hz).
\r
62 * \param masterClock Frequency of the system master clock (in Hz).
\r
64 void UART_Configure(Uart *uart,
\r
67 uint32_t masterClock)
\r
69 /* Reset and disable receiver & transmitter*/
\r
70 uart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX
\r
71 | UART_CR_RXDIS | UART_CR_TXDIS | UART_CR_RSTSTA;
\r
73 uart->UART_IDR = 0xFFFFFFFF;
\r
76 uart->UART_MR = mode;
\r
78 /* Configure baudrate*/
\r
79 uart->UART_BRGR = (masterClock / baudrate) / 16;
\r
81 uart->UART_CR = UART_CR_TXEN | UART_CR_RXEN;
\r
85 * \brief Enables or disables the transmitter of an UART peripheral.
\r
88 * \param uart Pointer to an UART peripheral
\r
89 * \param enabled If true, the transmitter is enabled; otherwise it is
\r
92 void UART_SetTransmitterEnabled(Uart *uart, uint8_t enabled)
\r
96 uart->UART_CR = UART_CR_TXEN;
\r
100 uart->UART_CR = UART_CR_TXDIS;
\r
105 * \brief Enables or disables the receiver of an UART peripheral
\r
108 * \param uart Pointer to an UART peripheral
\r
109 * \param enabled If true, the receiver is enabled; otherwise it is disabled.
\r
111 void UART_SetReceiverEnabled(Uart *uart, uint8_t enabled)
\r
115 uart->UART_CR = UART_CR_RXEN;
\r
119 uart->UART_CR = UART_CR_RXDIS;
\r
125 * \brief Return 1 if a character can be read in UART
\r
126 * \param uart Pointer to an UART peripheral.
\r
128 uint32_t UART_IsRxReady(Uart *uart)
\r
130 return (uart->UART_SR & UART_SR_RXRDY);
\r
135 * \brief Reads and returns a character from the UART.
\r
137 * \note This function is synchronous (i.e. uses polling).
\r
138 * \param uart Pointer to an UART peripheral.
\r
139 * \return Character received.
\r
141 uint8_t UART_GetChar(Uart *uart)
\r
143 while (!UART_IsRxReady(uart));
\r
144 return uart->UART_RHR;
\r
149 * \brief Return 1 if a character can be send to UART
\r
150 * \param uart Pointer to an UART peripheral.
\r
152 uint32_t UART_IsTxReady(Uart *uart)
\r
154 return (uart->UART_SR & UART_SR_TXRDY);
\r
159 * \brief Return 1 if a character can be send to UART
\r
160 * \param uart Pointer to an UART peripheral.
\r
162 static uint32_t UART_IsTxSent(Uart *uart)
\r
164 return (uart->UART_SR & UART_SR_TXEMPTY);
\r
169 * \brief Sends one packet of data through the specified UART peripheral. This
\r
170 * function operates synchronously, so it only returns when the data has been
\r
173 * \param uart Pointer to an UART peripheral.
\r
174 * \param c Character to send
\r
176 void UART_PutChar( Uart *uart, uint8_t c)
\r
178 /* Wait for the transmitter to be ready*/
\r
179 while (!UART_IsRxReady(uart) && !UART_IsTxSent(uart));
\r
181 /* Send character*/
\r
182 uart->UART_THR = c;
\r
184 /* Wait for the transfer to complete*/
\r
185 while (!UART_IsTxSent(uart));
\r
191 * \brief Get present status
\r
192 * \param uart Pointer to an UART peripheral.
\r
194 uint32_t UART_GetStatus(Uart *uart)
\r
196 return uart->UART_SR;
\r
200 * \brief Enable interrupt
\r
201 * \param uart Pointer to an UART peripheral.
\r
202 * \param mode Interrupt mode.
\r
204 void UART_EnableIt(Uart *uart,uint32_t mode)
\r
206 uart->UART_IER = mode;
\r
210 * \brief Disable interrupt
\r
211 * \param uart Pointer to an UART peripheral.
\r
212 * \param mode Interrupt mode.
\r
214 void UART_DisableIt(Uart *uart,uint32_t mode)
\r
216 uart->UART_IDR = mode;
\r
220 * \brief Return interrupt mask
\r
221 * \param uart Pointer to an UART peripheral.
\r
223 uint32_t UART_GetItMask(Uart *uart)
\r
225 return uart->UART_IMR;
\r
228 void UART_SendBuffer(Uart *uart, uint8_t *pBuffer, uint32_t BuffLen)
\r
230 uint8_t *pData = pBuffer;
\r
233 for(Len =0; Len<BuffLen; Len++ )
\r
235 UART_PutChar(uart, *pData);
\r
240 void UART_ReceiveBuffer(Uart *uart, uint8_t *pBuffer, uint32_t BuffLen)
\r
244 for(Len =0; Len<BuffLen; Len++ )
\r
246 *pBuffer = UART_GetChar(uart);
\r
252 void UART_CompareConfig(Uart *uart, uint8_t Val1, uint8_t Val2)
\r
255 uart->UART_CMPR = (UART_CMPR_VAL1(Val1) | UART_CMPR_VAL2(Val2));
\r