SAMV71 Xplained Ultra Software Package 1.3

uart.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2011, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /**
00031  * \file
00032  *
00033  * Implementation of UART (Universal Asynchronous Receiver Transmitter)
00034  * controller.
00035  *
00036  */
00037 /*------------------------------------------------------------------------------
00038  *         Headers
00039  *----------------------------------------------------------------------------*/
00040 #include "chip.h"
00041 
00042 #include <assert.h>
00043 #include <string.h>
00044 
00045 /*------------------------------------------------------------------------------
00046  *         Exported functions
00047  *----------------------------------------------------------------------------*/
00048 
00049 /**
00050  * \brief Configures an UART peripheral with the specified parameters.
00051  *
00052  *
00053  *  \param uart  Pointer to the UART peripheral to configure.
00054  *  \param mode  Desired value for the UART mode register (see the datasheet).
00055  *  \param baudrate  Baudrate at which the UART should operate (in Hz).
00056  *  \param masterClock  Frequency of the system master clock (in Hz).
00057  */
00058 void UART_Configure(Uart *uart,
00059         uint32_t mode,
00060         uint32_t baudrate,
00061         uint32_t masterClock)
00062 {
00063     /* Reset and disable receiver & transmitter*/
00064     uart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX
00065         | UART_CR_RXDIS | UART_CR_TXDIS | UART_CR_RSTSTA;
00066 
00067     uart->UART_IDR = 0xFFFFFFFF;
00068 
00069     /* Configure mode*/
00070     uart->UART_MR = mode;
00071 
00072     /* Configure baudrate*/
00073     uart->UART_BRGR = (masterClock / baudrate) / 16;
00074 
00075     uart->UART_CR = UART_CR_TXEN | UART_CR_RXEN;
00076 
00077 }
00078 /**
00079  * \brief Enables or disables the transmitter of an UART peripheral.
00080  *
00081  *
00082  * \param uart  Pointer to an UART peripheral
00083  * \param enabled  If true, the transmitter is enabled; otherwise it is
00084  *  disabled.
00085  */
00086 void UART_SetTransmitterEnabled(Uart *uart, uint8_t enabled)
00087 {
00088     if (enabled) {
00089         uart->UART_CR = UART_CR_TXEN;
00090     } else {
00091         uart->UART_CR = UART_CR_TXDIS;
00092     }
00093 }
00094 
00095 /**
00096  * \brief Enables or disables the receiver of an UART peripheral
00097  *
00098  *
00099  * \param uart  Pointer to an UART peripheral
00100  * \param enabled  If true, the receiver is enabled; otherwise it is disabled.
00101  */
00102 void UART_SetReceiverEnabled(Uart *uart, uint8_t enabled)
00103 {
00104     if (enabled) {
00105         uart->UART_CR = UART_CR_RXEN;
00106     } else {
00107         uart->UART_CR = UART_CR_RXDIS;
00108     }
00109 }
00110 
00111 /**
00112  * \brief   Return 1 if a character can be read in UART
00113  * \param uart  Pointer to an UART peripheral.
00114  */
00115 uint32_t UART_IsRxReady(Uart *uart)
00116 {
00117     return (uart->UART_SR & UART_SR_RXRDY);
00118 }
00119 
00120 /**
00121  * \brief  Reads and returns a character from the UART.
00122  *
00123  * \note This function is synchronous (i.e. uses polling).
00124  * \param uart  Pointer to an UART peripheral.
00125  * \return Character received.
00126  */
00127 uint8_t UART_GetChar(Uart *uart)
00128 {
00129     while (!UART_IsRxReady(uart));
00130     return uart->UART_RHR;
00131 }
00132 
00133 /**
00134  * \brief   Return 1 if a character can be send to UART
00135  * \param uart  Pointer to an UART peripheral.
00136  */
00137 uint32_t UART_IsTxReady(Uart *uart)
00138 {
00139     return (uart->UART_SR & UART_SR_TXRDY);
00140 }
00141 
00142 /**
00143  * \brief   Return 1 if a character can be send to UART
00144  * \param uart  Pointer to an UART peripheral.
00145  */
00146 static uint32_t UART_IsTxSent(Uart *uart)
00147 {
00148     return (uart->UART_SR & UART_SR_TXEMPTY);
00149 }
00150 
00151 /**
00152  * \brief  Sends one packet of data through the specified UART peripheral. This
00153  * function operates synchronously, so it only returns when the data has been
00154  * actually sent.
00155  *
00156  * \param uart  Pointer to an UART peripheral.
00157  * \param c  Character to send
00158  */
00159 void UART_PutChar( Uart *uart, uint8_t c)
00160 {
00161     /* Wait for the transmitter to be ready*/
00162     while (!UART_IsRxReady(uart) && !UART_IsTxSent(uart));
00163 
00164     /* Send character*/
00165     uart->UART_THR = c;
00166 
00167     /* Wait for the transfer to complete*/
00168     while (!UART_IsTxSent(uart));
00169 }
00170 
00171 /**
00172  * \brief   Get present status
00173  * \param uart  Pointer to an UART peripheral.
00174  */
00175 uint32_t UART_GetStatus(Uart *uart)
00176 {
00177     return uart->UART_SR;
00178 }
00179 
00180 /**
00181  * \brief   Enable interrupt
00182  * \param uart  Pointer to an UART peripheral.
00183  * \param mode  Interrupt mode.
00184  */
00185 void UART_EnableIt(Uart *uart,uint32_t mode)
00186 {
00187     uart->UART_IER = mode;
00188 }
00189 
00190 /**
00191  * \brief   Disable interrupt
00192  * \param uart  Pointer to an UART peripheral.
00193  * \param mode  Interrupt mode.
00194  */
00195 void UART_DisableIt(Uart *uart,uint32_t mode)
00196 {
00197     uart->UART_IDR = mode;
00198 }
00199 
00200 /**
00201  * \brief   Return interrupt mask
00202  * \param uart  Pointer to an UART peripheral.
00203  */
00204 uint32_t UART_GetItMask(Uart *uart)
00205 {
00206     return uart->UART_IMR;
00207 }
00208 
00209 void UART_SendBuffer(Uart *uart, uint8_t *pBuffer, uint32_t BuffLen)
00210 {
00211     uint8_t *pData = pBuffer;
00212     uint32_t Len =0;
00213 
00214     for(Len =0; Len<BuffLen; Len++ ) {
00215         UART_PutChar(uart, *pData);
00216         pData++;
00217     }
00218 }
00219 
00220 void UART_ReceiveBuffer(Uart *uart, uint8_t *pBuffer, uint32_t BuffLen)
00221 {
00222     uint32_t Len =0;
00223 
00224     for(Len =0; Len<BuffLen; Len++ ) {
00225         *pBuffer = UART_GetChar(uart);
00226         pBuffer++;
00227     }
00228 }
00229 
00230 void UART_CompareConfig(Uart *uart, uint8_t Val1, uint8_t Val2)
00231 {
00232     uart->UART_CMPR = (UART_CMPR_VAL1(Val1) | UART_CMPR_VAL2(Val2));
00233 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines