SAMV71 Xplained Ultra Software Package 1.5

uart.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
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  * \brief Enables or disables the receiver of an UART peripheral
00096  *
00097  *
00098  * \param uart  Pointer to an UART peripheral
00099  * \param enabled  If true, the receiver is enabled; otherwise it is disabled.
00100  */
00101 void UART_SetReceiverEnabled(Uart *uart, uint8_t enabled)
00102 {
00103     if (enabled)
00104         uart->UART_CR = UART_CR_RXEN;
00105     else
00106         uart->UART_CR = UART_CR_RXDIS;
00107 }
00108 
00109 /**
00110  * \brief   Return 1 if a character can be read in UART
00111  * \param uart  Pointer to an UART peripheral.
00112  */
00113 uint32_t UART_IsRxReady(Uart *uart)
00114 {
00115     return (uart->UART_SR & UART_SR_RXRDY);
00116 }
00117 
00118 /**
00119  * \brief  Reads and returns a character from the UART.
00120  *
00121  * \note This function is synchronous (i.e. uses polling).
00122  * \param uart  Pointer to an UART peripheral.
00123  * \return Character received.
00124  */
00125 uint8_t UART_GetChar(Uart *uart)
00126 {
00127     while (!UART_IsRxReady(uart));
00128 
00129     return uart->UART_RHR;
00130 }
00131 
00132 /**
00133  * \brief   Return 1 if a character can be send to UART
00134  * \param uart  Pointer to an UART peripheral.
00135  */
00136 uint32_t UART_IsTxReady(Uart *uart)
00137 {
00138     return (uart->UART_SR & UART_SR_TXRDY);
00139 }
00140 
00141 /**
00142  * \brief   Return 1 if a character can be send to UART
00143  * \param uart  Pointer to an UART peripheral.
00144  */
00145 static uint32_t UART_IsTxSent(Uart *uart)
00146 {
00147     return (uart->UART_SR & UART_SR_TXEMPTY);
00148 }
00149 
00150 /**
00151  * \brief  Sends one packet of data through the specified UART peripheral. This
00152  * function operates synchronously, so it only returns when the data has been
00153  * actually sent.
00154  *
00155  * \param uart  Pointer to an UART peripheral.
00156  * \param c  Character to send
00157  */
00158 void UART_PutChar(Uart *uart, uint8_t c)
00159 {
00160     /* Wait for the transmitter to be ready*/
00161     while (!UART_IsRxReady(uart) && !UART_IsTxSent(uart));
00162 
00163     /* Send character*/
00164     uart->UART_THR = c;
00165 
00166     /* Wait for the transfer to complete*/
00167     while (!UART_IsTxSent(uart));
00168 }
00169 
00170 /**
00171  * \brief   Get present status
00172  * \param uart  Pointer to an UART peripheral.
00173  */
00174 uint32_t UART_GetStatus(Uart *uart)
00175 {
00176     return uart->UART_SR;
00177 }
00178 
00179 /**
00180  * \brief   Enable interrupt
00181  * \param uart  Pointer to an UART peripheral.
00182  * \param mode  Interrupt mode.
00183  */
00184 void UART_EnableIt(Uart *uart, uint32_t mode)
00185 {
00186     uart->UART_IER = mode;
00187 }
00188 
00189 /**
00190  * \brief   Disable interrupt
00191  * \param uart  Pointer to an UART peripheral.
00192  * \param mode  Interrupt mode.
00193  */
00194 void UART_DisableIt(Uart *uart, uint32_t mode)
00195 {
00196     uart->UART_IDR = mode;
00197 }
00198 
00199 /**
00200  * \brief   Return interrupt mask
00201  * \param uart  Pointer to an UART peripheral.
00202  */
00203 uint32_t UART_GetItMask(Uart *uart)
00204 {
00205     return uart->UART_IMR;
00206 }
00207 
00208 void UART_SendBuffer(Uart *uart, uint8_t *pBuffer, uint32_t BuffLen)
00209 {
00210     uint8_t *pData = pBuffer;
00211     uint32_t Len = 0;
00212 
00213     for (Len = 0; Len < BuffLen; Len++) {
00214         UART_PutChar(uart, *pData);
00215         pData++;
00216     }
00217 }
00218 
00219 void UART_ReceiveBuffer(Uart *uart, uint8_t *pBuffer, uint32_t BuffLen)
00220 {
00221     uint32_t Len = 0;
00222 
00223     for (Len = 0; Len < BuffLen; Len++) {
00224         *pBuffer = UART_GetChar(uart);
00225         pBuffer++;
00226     }
00227 }
00228 
00229 void UART_CompareConfig(Uart *uart, uint8_t Val1, uint8_t Val2)
00230 {
00231     uart->UART_CMPR = (UART_CMPR_VAL1(Val1) | UART_CMPR_VAL2(Val2));
00232 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines