1*1b2596b5SMatthias Ringwald /** 2*1b2596b5SMatthias Ringwald * \file 3*1b2596b5SMatthias Ringwald * 4*1b2596b5SMatthias Ringwald * \brief Universal Asynchronous Receiver Transceiver (UART) driver for SAM. 5*1b2596b5SMatthias Ringwald * 6*1b2596b5SMatthias Ringwald * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved. 7*1b2596b5SMatthias Ringwald * 8*1b2596b5SMatthias Ringwald * \asf_license_start 9*1b2596b5SMatthias Ringwald * 10*1b2596b5SMatthias Ringwald * \page License 11*1b2596b5SMatthias Ringwald * 12*1b2596b5SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 13*1b2596b5SMatthias Ringwald * modification, are permitted provided that the following conditions are met: 14*1b2596b5SMatthias Ringwald * 15*1b2596b5SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright notice, 16*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer. 17*1b2596b5SMatthias Ringwald * 18*1b2596b5SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright notice, 19*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer in the documentation 20*1b2596b5SMatthias Ringwald * and/or other materials provided with the distribution. 21*1b2596b5SMatthias Ringwald * 22*1b2596b5SMatthias Ringwald * 3. The name of Atmel may not be used to endorse or promote products derived 23*1b2596b5SMatthias Ringwald * from this software without specific prior written permission. 24*1b2596b5SMatthias Ringwald * 25*1b2596b5SMatthias Ringwald * 4. This software may only be redistributed and used in connection with an 26*1b2596b5SMatthias Ringwald * Atmel microcontroller product. 27*1b2596b5SMatthias Ringwald * 28*1b2596b5SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 29*1b2596b5SMatthias Ringwald * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 30*1b2596b5SMatthias Ringwald * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 31*1b2596b5SMatthias Ringwald * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 32*1b2596b5SMatthias Ringwald * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33*1b2596b5SMatthias Ringwald * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34*1b2596b5SMatthias Ringwald * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35*1b2596b5SMatthias Ringwald * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 36*1b2596b5SMatthias Ringwald * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 37*1b2596b5SMatthias Ringwald * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38*1b2596b5SMatthias Ringwald * POSSIBILITY OF SUCH DAMAGE. 39*1b2596b5SMatthias Ringwald * 40*1b2596b5SMatthias Ringwald * \asf_license_stop 41*1b2596b5SMatthias Ringwald * 42*1b2596b5SMatthias Ringwald */ 43*1b2596b5SMatthias Ringwald /* 44*1b2596b5SMatthias Ringwald * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> 45*1b2596b5SMatthias Ringwald */ 46*1b2596b5SMatthias Ringwald 47*1b2596b5SMatthias Ringwald #ifndef UART_H_INCLUDED 48*1b2596b5SMatthias Ringwald #define UART_H_INCLUDED 49*1b2596b5SMatthias Ringwald 50*1b2596b5SMatthias Ringwald #include "compiler.h" 51*1b2596b5SMatthias Ringwald 52*1b2596b5SMatthias Ringwald /// @cond 0 53*1b2596b5SMatthias Ringwald /**INDENT-OFF**/ 54*1b2596b5SMatthias Ringwald #ifdef __cplusplus 55*1b2596b5SMatthias Ringwald extern "C" { 56*1b2596b5SMatthias Ringwald #endif 57*1b2596b5SMatthias Ringwald /**INDENT-ON**/ 58*1b2596b5SMatthias Ringwald /// @endcond 59*1b2596b5SMatthias Ringwald 60*1b2596b5SMatthias Ringwald /* UART internal div factor for sampling */ 61*1b2596b5SMatthias Ringwald #define UART_MCK_DIV 16 62*1b2596b5SMatthias Ringwald /* Div factor to get the maximum baud rate */ 63*1b2596b5SMatthias Ringwald #define UART_MCK_DIV_MIN_FACTOR 1 64*1b2596b5SMatthias Ringwald /* Div factor to get the minimum baud rate */ 65*1b2596b5SMatthias Ringwald #define UART_MCK_DIV_MAX_FACTOR 65535 66*1b2596b5SMatthias Ringwald 67*1b2596b5SMatthias Ringwald /*! \brief Option list for UART peripheral initialization */ 68*1b2596b5SMatthias Ringwald typedef struct sam_uart_opt { 69*1b2596b5SMatthias Ringwald /** MCK for UART */ 70*1b2596b5SMatthias Ringwald uint32_t ul_mck; 71*1b2596b5SMatthias Ringwald /** Expected baud rate */ 72*1b2596b5SMatthias Ringwald uint32_t ul_baudrate; 73*1b2596b5SMatthias Ringwald /** Initialize value for UART mode register */ 74*1b2596b5SMatthias Ringwald uint32_t ul_mode; 75*1b2596b5SMatthias Ringwald } sam_uart_opt_t; 76*1b2596b5SMatthias Ringwald 77*1b2596b5SMatthias Ringwald uint32_t uart_init(Uart *p_uart, const sam_uart_opt_t *p_uart_opt); 78*1b2596b5SMatthias Ringwald void uart_enable_tx(Uart *p_uart); 79*1b2596b5SMatthias Ringwald void uart_disable_tx(Uart *p_uart); 80*1b2596b5SMatthias Ringwald void uart_reset_tx(Uart *p_uart); 81*1b2596b5SMatthias Ringwald void uart_enable_rx(Uart *p_uart); 82*1b2596b5SMatthias Ringwald void uart_disable_rx(Uart *p_uart); 83*1b2596b5SMatthias Ringwald void uart_reset_rx(Uart *p_uart); 84*1b2596b5SMatthias Ringwald void uart_enable(Uart *p_uart); 85*1b2596b5SMatthias Ringwald void uart_disable(Uart *p_uart); 86*1b2596b5SMatthias Ringwald void uart_reset(Uart *p_uart); 87*1b2596b5SMatthias Ringwald void uart_enable_interrupt(Uart *p_uart, uint32_t ul_sources); 88*1b2596b5SMatthias Ringwald void uart_disable_interrupt(Uart *p_uart, uint32_t ul_sources); 89*1b2596b5SMatthias Ringwald uint32_t uart_get_interrupt_mask(Uart *p_uart); 90*1b2596b5SMatthias Ringwald uint32_t uart_get_status(Uart *p_uart); 91*1b2596b5SMatthias Ringwald void uart_reset_status(Uart *p_uart); 92*1b2596b5SMatthias Ringwald uint32_t uart_is_tx_ready(Uart *p_uart); 93*1b2596b5SMatthias Ringwald uint32_t uart_is_tx_empty(Uart *p_uart); 94*1b2596b5SMatthias Ringwald uint32_t uart_is_rx_ready(Uart *p_uart); 95*1b2596b5SMatthias Ringwald uint32_t uart_is_tx_buf_empty(Uart *p_uart); 96*1b2596b5SMatthias Ringwald void uart_set_clock_divisor(Uart *p_uart, uint16_t us_divisor); 97*1b2596b5SMatthias Ringwald uint32_t uart_write(Uart *p_uart, const uint8_t uc_data); 98*1b2596b5SMatthias Ringwald uint32_t uart_read(Uart *p_uart, uint8_t *puc_data); 99*1b2596b5SMatthias Ringwald #if (!SAMV71 && !SAMV70 && !SAME70 && !SAMS70) 100*1b2596b5SMatthias Ringwald uint32_t uart_is_rx_buf_end(Uart *p_uart); 101*1b2596b5SMatthias Ringwald uint32_t uart_is_tx_buf_end(Uart *p_uart); 102*1b2596b5SMatthias Ringwald uint32_t uart_is_rx_buf_full(Uart *p_uart); 103*1b2596b5SMatthias Ringwald Pdc *uart_get_pdc_base(Uart *p_uart); 104*1b2596b5SMatthias Ringwald #endif 105*1b2596b5SMatthias Ringwald #if (SAMG53 || SAMG54 || SAMV71 || SAMV70 || SAME70 || SAMS70) 106*1b2596b5SMatthias Ringwald void uart_set_sleepwalking(Uart *p_uart, uint8_t ul_low_value, 107*1b2596b5SMatthias Ringwald bool cmpmode, bool cmppar, uint8_t ul_high_value); 108*1b2596b5SMatthias Ringwald void uart_set_write_protection(Uart *p_uart, bool flag); 109*1b2596b5SMatthias Ringwald #endif 110*1b2596b5SMatthias Ringwald 111*1b2596b5SMatthias Ringwald #if (SAM4C || SAM4CP || SAM4CM) 112*1b2596b5SMatthias Ringwald enum uart_optical_duty_cycle { 113*1b2596b5SMatthias Ringwald UART_MOD_CLK_DUTY_50_00 = UART_MR_OPT_DUTY_DUTY_50, 114*1b2596b5SMatthias Ringwald UART_MOD_CLK_DUTY_43_75 = UART_MR_OPT_DUTY_DUTY_43P75, 115*1b2596b5SMatthias Ringwald UART_MOD_CLK_DUTY_37_50 = UART_MR_OPT_DUTY_DUTY_37P5, 116*1b2596b5SMatthias Ringwald UART_MOD_CLK_DUTY_31_25 = UART_MR_OPT_DUTY_DUTY_31P25, 117*1b2596b5SMatthias Ringwald UART_MOD_CLK_DUTY_25_00 = UART_MR_OPT_DUTY_DUTY_25, 118*1b2596b5SMatthias Ringwald UART_MOD_CLK_DUTY_18_75 = UART_MR_OPT_DUTY_DUTY_18P75, 119*1b2596b5SMatthias Ringwald UART_MOD_CLK_DUTY_12_50 = UART_MR_OPT_DUTY_DUTY_12P5, 120*1b2596b5SMatthias Ringwald UART_MOD_CLK_DUTY_06_25 = UART_MR_OPT_DUTY_DUTY_6P25, 121*1b2596b5SMatthias Ringwald }; 122*1b2596b5SMatthias Ringwald 123*1b2596b5SMatthias Ringwald enum uart_optical_cmp_threshold { 124*1b2596b5SMatthias Ringwald UART_RX_CMP_THRESHOLD_VDDIO_DIV_10_0 = UART_MR_OPT_CMPTH_VDDIO_DIV10, 125*1b2596b5SMatthias Ringwald UART_RX_CMP_THRESHOLD_VDDIO_DIV_5_0 = UART_MR_OPT_CMPTH_VDDIO_DIV5, 126*1b2596b5SMatthias Ringwald UART_RX_CMP_THRESHOLD_VDDIO_DIV_3_3 = UART_MR_OPT_CMPTH_VDDIO_DIV3P3, 127*1b2596b5SMatthias Ringwald UART_RX_CMP_THRESHOLD_VDDIO_DIV_2_5 = UART_MR_OPT_CMPTH_VDDIO_DIV2P5, 128*1b2596b5SMatthias Ringwald UART_RX_CMP_THRESHOLD_VDDIO_DIV_2_0 = UART_MR_OPT_CMPTH_VDDIO_DIV2, 129*1b2596b5SMatthias Ringwald }; 130*1b2596b5SMatthias Ringwald 131*1b2596b5SMatthias Ringwald struct uart_config_optical { 132*1b2596b5SMatthias Ringwald /* UART Receive Data Inverted */ 133*1b2596b5SMatthias Ringwald bool rx_inverted; 134*1b2596b5SMatthias Ringwald /* UART Modulated Data Inverted */ 135*1b2596b5SMatthias Ringwald bool tx_inverted; 136*1b2596b5SMatthias Ringwald /* UART Receiver Digital Filter */ 137*1b2596b5SMatthias Ringwald bool rx_filter; 138*1b2596b5SMatthias Ringwald /* Optical Link Clock Divider */ 139*1b2596b5SMatthias Ringwald uint8_t clk_div; 140*1b2596b5SMatthias Ringwald /* Optical Link Modulation Clock Duty Cycle */ 141*1b2596b5SMatthias Ringwald enum uart_optical_duty_cycle duty; 142*1b2596b5SMatthias Ringwald /* Receive Path Comparator Threshold */ 143*1b2596b5SMatthias Ringwald enum uart_optical_cmp_threshold threshold; 144*1b2596b5SMatthias Ringwald }; 145*1b2596b5SMatthias Ringwald 146*1b2596b5SMatthias Ringwald void uart_enable_optical_interface(Uart *p_uart); 147*1b2596b5SMatthias Ringwald void uart_disable_optical_interface(Uart *p_uart); 148*1b2596b5SMatthias Ringwald void uart_config_optical_interface(Uart *p_uart, 149*1b2596b5SMatthias Ringwald struct uart_config_optical *cfg); 150*1b2596b5SMatthias Ringwald #endif 151*1b2596b5SMatthias Ringwald 152*1b2596b5SMatthias Ringwald /// @cond 0 153*1b2596b5SMatthias Ringwald /**INDENT-OFF**/ 154*1b2596b5SMatthias Ringwald #ifdef __cplusplus 155*1b2596b5SMatthias Ringwald } 156*1b2596b5SMatthias Ringwald #endif 157*1b2596b5SMatthias Ringwald /**INDENT-ON**/ 158*1b2596b5SMatthias Ringwald /// @endcond 159*1b2596b5SMatthias Ringwald 160*1b2596b5SMatthias Ringwald #endif /* UART_H_INCLUDED */ 161