1*1b2596b5SMatthias Ringwald /** 2*1b2596b5SMatthias Ringwald * \file 3*1b2596b5SMatthias Ringwald * 4*1b2596b5SMatthias Ringwald * \brief Universal Synchronous Asynchronous Receiver Transmitter (USART) driver 5*1b2596b5SMatthias Ringwald * for SAM. 6*1b2596b5SMatthias Ringwald * 7*1b2596b5SMatthias Ringwald * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved. 8*1b2596b5SMatthias Ringwald * 9*1b2596b5SMatthias Ringwald * \asf_license_start 10*1b2596b5SMatthias Ringwald * 11*1b2596b5SMatthias Ringwald * \page License 12*1b2596b5SMatthias Ringwald * 13*1b2596b5SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 14*1b2596b5SMatthias Ringwald * modification, are permitted provided that the following conditions are met: 15*1b2596b5SMatthias Ringwald * 16*1b2596b5SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright notice, 17*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer. 18*1b2596b5SMatthias Ringwald * 19*1b2596b5SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright notice, 20*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer in the documentation 21*1b2596b5SMatthias Ringwald * and/or other materials provided with the distribution. 22*1b2596b5SMatthias Ringwald * 23*1b2596b5SMatthias Ringwald * 3. The name of Atmel may not be used to endorse or promote products derived 24*1b2596b5SMatthias Ringwald * from this software without specific prior written permission. 25*1b2596b5SMatthias Ringwald * 26*1b2596b5SMatthias Ringwald * 4. This software may only be redistributed and used in connection with an 27*1b2596b5SMatthias Ringwald * Atmel microcontroller product. 28*1b2596b5SMatthias Ringwald * 29*1b2596b5SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 30*1b2596b5SMatthias Ringwald * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 31*1b2596b5SMatthias Ringwald * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 32*1b2596b5SMatthias Ringwald * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 33*1b2596b5SMatthias Ringwald * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34*1b2596b5SMatthias Ringwald * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35*1b2596b5SMatthias Ringwald * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36*1b2596b5SMatthias Ringwald * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37*1b2596b5SMatthias Ringwald * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 38*1b2596b5SMatthias Ringwald * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39*1b2596b5SMatthias Ringwald * POSSIBILITY OF SUCH DAMAGE. 40*1b2596b5SMatthias Ringwald * 41*1b2596b5SMatthias Ringwald * \asf_license_stop 42*1b2596b5SMatthias Ringwald * 43*1b2596b5SMatthias Ringwald */ 44*1b2596b5SMatthias Ringwald /* 45*1b2596b5SMatthias Ringwald * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> 46*1b2596b5SMatthias Ringwald */ 47*1b2596b5SMatthias Ringwald 48*1b2596b5SMatthias Ringwald #ifndef USART_H_INCLUDED 49*1b2596b5SMatthias Ringwald #define USART_H_INCLUDED 50*1b2596b5SMatthias Ringwald 51*1b2596b5SMatthias Ringwald #include "compiler.h" 52*1b2596b5SMatthias Ringwald 53*1b2596b5SMatthias Ringwald /** 54*1b2596b5SMatthias Ringwald * \defgroup group_sam_drivers_usart Universal Synchronous Asynchronous Receiver 55*1b2596b5SMatthias Ringwald * Transmitter (USART). 56*1b2596b5SMatthias Ringwald * 57*1b2596b5SMatthias Ringwald * See \ref sam_usart_quickstart. 58*1b2596b5SMatthias Ringwald * 59*1b2596b5SMatthias Ringwald * This is a low-level driver implementation for the SAM Universal 60*1b2596b5SMatthias Ringwald * Synchronous/Asynchronous Receiver/Transmitter. 61*1b2596b5SMatthias Ringwald * 62*1b2596b5SMatthias Ringwald * @{ 63*1b2596b5SMatthias Ringwald */ 64*1b2596b5SMatthias Ringwald 65*1b2596b5SMatthias Ringwald /// @cond 0 66*1b2596b5SMatthias Ringwald /**INDENT-OFF**/ 67*1b2596b5SMatthias Ringwald #ifdef __cplusplus 68*1b2596b5SMatthias Ringwald extern "C" { 69*1b2596b5SMatthias Ringwald #endif 70*1b2596b5SMatthias Ringwald /**INDENT-ON**/ 71*1b2596b5SMatthias Ringwald /// @endcond 72*1b2596b5SMatthias Ringwald 73*1b2596b5SMatthias Ringwald /** Clock phase. */ 74*1b2596b5SMatthias Ringwald #define SPI_CPHA (1 << 0) 75*1b2596b5SMatthias Ringwald 76*1b2596b5SMatthias Ringwald /** Clock polarity. */ 77*1b2596b5SMatthias Ringwald #define SPI_CPOL (1 << 1) 78*1b2596b5SMatthias Ringwald 79*1b2596b5SMatthias Ringwald /** SPI mode definition. */ 80*1b2596b5SMatthias Ringwald #define SPI_MODE_0 0 81*1b2596b5SMatthias Ringwald #define SPI_MODE_1 (SPI_CPHA) 82*1b2596b5SMatthias Ringwald #define SPI_MODE_2 (SPI_CPOL) 83*1b2596b5SMatthias Ringwald #define SPI_MODE_3 (SPI_CPOL | SPI_CPHA) 84*1b2596b5SMatthias Ringwald 85*1b2596b5SMatthias Ringwald /**micro definition for LIN mode of SAMV71*/ 86*1b2596b5SMatthias Ringwald #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 87*1b2596b5SMatthias Ringwald #define US_MR_USART_MODE_LIN_MASTER 0x0A 88*1b2596b5SMatthias Ringwald #define US_MR_USART_MODE_LIN_SLAVE 0x0B 89*1b2596b5SMatthias Ringwald #endif 90*1b2596b5SMatthias Ringwald /* Input parameters when initializing RS232 and similar modes. */ 91*1b2596b5SMatthias Ringwald typedef struct { 92*1b2596b5SMatthias Ringwald /* Set baud rate of the USART (unused in slave modes). */ 93*1b2596b5SMatthias Ringwald uint32_t baudrate; 94*1b2596b5SMatthias Ringwald 95*1b2596b5SMatthias Ringwald /* 96*1b2596b5SMatthias Ringwald * Number of bits, which should be one of the following: US_MR_CHRL_5_BIT, 97*1b2596b5SMatthias Ringwald * US_MR_CHRL_6_BIT, US_MR_CHRL_7_BIT, US_MR_CHRL_8_BIT or 98*1b2596b5SMatthias Ringwald * US_MR_MODE9. 99*1b2596b5SMatthias Ringwald */ 100*1b2596b5SMatthias Ringwald uint32_t char_length; 101*1b2596b5SMatthias Ringwald 102*1b2596b5SMatthias Ringwald /* 103*1b2596b5SMatthias Ringwald * Parity type, which should be one of the following: US_MR_PAR_EVEN, 104*1b2596b5SMatthias Ringwald * US_MR_PAR_ODD, US_MR_PAR_SPACE, US_MR_PAR_MARK, US_MR_PAR_NO 105*1b2596b5SMatthias Ringwald * or US_MR_PAR_MULTIDROP. 106*1b2596b5SMatthias Ringwald */ 107*1b2596b5SMatthias Ringwald uint32_t parity_type; 108*1b2596b5SMatthias Ringwald 109*1b2596b5SMatthias Ringwald /* 110*1b2596b5SMatthias Ringwald * Number of stop bits between two characters: US_MR_NBSTOP_1_BIT, 111*1b2596b5SMatthias Ringwald * US_MR_NBSTOP_1_5_BIT, US_MR_NBSTOP_2_BIT. 112*1b2596b5SMatthias Ringwald * \note US_MR_NBSTOP_1_5_BIT is supported in asynchronous modes only. 113*1b2596b5SMatthias Ringwald */ 114*1b2596b5SMatthias Ringwald uint32_t stop_bits; 115*1b2596b5SMatthias Ringwald 116*1b2596b5SMatthias Ringwald /* 117*1b2596b5SMatthias Ringwald * Run the channel in test mode, which should be one of following: 118*1b2596b5SMatthias Ringwald * US_MR_CHMODE_NORMAL, US_MR_CHMODE_AUTOMATIC, 119*1b2596b5SMatthias Ringwald * US_MR_CHMODE_LOCAL_LOOPBACK, US_MR_CHMODE_REMOTE_LOOPBACK. 120*1b2596b5SMatthias Ringwald */ 121*1b2596b5SMatthias Ringwald uint32_t channel_mode; 122*1b2596b5SMatthias Ringwald 123*1b2596b5SMatthias Ringwald /* Filter of IrDA mode, useless in other modes. */ 124*1b2596b5SMatthias Ringwald uint32_t irda_filter; 125*1b2596b5SMatthias Ringwald } sam_usart_opt_t; 126*1b2596b5SMatthias Ringwald 127*1b2596b5SMatthias Ringwald /* Input parameters when initializing ISO7816 mode. */ 128*1b2596b5SMatthias Ringwald typedef struct { 129*1b2596b5SMatthias Ringwald /* Set the frequency of the ISO7816 clock. */ 130*1b2596b5SMatthias Ringwald uint32_t iso7816_hz; 131*1b2596b5SMatthias Ringwald 132*1b2596b5SMatthias Ringwald /* 133*1b2596b5SMatthias Ringwald * The number of ISO7816 clock ticks in every bit period (1 to 2047, 134*1b2596b5SMatthias Ringwald * 0 = disable clock). Baudrate rate = iso7816_hz / fidi_ratio. 135*1b2596b5SMatthias Ringwald */ 136*1b2596b5SMatthias Ringwald uint32_t fidi_ratio; 137*1b2596b5SMatthias Ringwald 138*1b2596b5SMatthias Ringwald /* 139*1b2596b5SMatthias Ringwald * How to calculate the parity bit: US_MR_PAR_EVEN for normal mode or 140*1b2596b5SMatthias Ringwald * US_MR_PAR_ODD for inverse mode. 141*1b2596b5SMatthias Ringwald */ 142*1b2596b5SMatthias Ringwald uint32_t parity_type; 143*1b2596b5SMatthias Ringwald 144*1b2596b5SMatthias Ringwald /* 145*1b2596b5SMatthias Ringwald * Inhibit Non Acknowledge: 146*1b2596b5SMatthias Ringwald * - 0: the NACK is generated; 147*1b2596b5SMatthias Ringwald * - 1: the NACK is not generated. 148*1b2596b5SMatthias Ringwald * 149*1b2596b5SMatthias Ringwald * \note This bit will be used only in ISO7816 mode, protocol T = 0 150*1b2596b5SMatthias Ringwald * receiver. 151*1b2596b5SMatthias Ringwald */ 152*1b2596b5SMatthias Ringwald uint32_t inhibit_nack; 153*1b2596b5SMatthias Ringwald 154*1b2596b5SMatthias Ringwald /* 155*1b2596b5SMatthias Ringwald * Disable successive NACKs. 156*1b2596b5SMatthias Ringwald * - 0: NACK is sent on the ISO line as soon as a parity error occurs 157*1b2596b5SMatthias Ringwald * in the received character. Successive parity errors are counted up to 158*1b2596b5SMatthias Ringwald * the value in the max_iterations field. These parity errors generate 159*1b2596b5SMatthias Ringwald * a NACK on the ISO line. As soon as this value is reached, no additional 160*1b2596b5SMatthias Ringwald * NACK is sent on the ISO line. The ITERATION flag is asserted. 161*1b2596b5SMatthias Ringwald */ 162*1b2596b5SMatthias Ringwald uint32_t dis_suc_nack; 163*1b2596b5SMatthias Ringwald 164*1b2596b5SMatthias Ringwald /* Max number of repetitions (0 to 7). */ 165*1b2596b5SMatthias Ringwald uint32_t max_iterations; 166*1b2596b5SMatthias Ringwald 167*1b2596b5SMatthias Ringwald /* 168*1b2596b5SMatthias Ringwald * Bit order in transmitted characters: 169*1b2596b5SMatthias Ringwald * - 0: LSB first; 170*1b2596b5SMatthias Ringwald * - 1: MSB first. 171*1b2596b5SMatthias Ringwald */ 172*1b2596b5SMatthias Ringwald uint32_t bit_order; 173*1b2596b5SMatthias Ringwald 174*1b2596b5SMatthias Ringwald /* 175*1b2596b5SMatthias Ringwald * Which protocol is used: 176*1b2596b5SMatthias Ringwald * - 0: T = 0; 177*1b2596b5SMatthias Ringwald * - 1: T = 1. 178*1b2596b5SMatthias Ringwald */ 179*1b2596b5SMatthias Ringwald uint32_t protocol_type; 180*1b2596b5SMatthias Ringwald } usart_iso7816_opt_t; 181*1b2596b5SMatthias Ringwald 182*1b2596b5SMatthias Ringwald /* Input parameters when initializing SPI mode. */ 183*1b2596b5SMatthias Ringwald typedef struct { 184*1b2596b5SMatthias Ringwald /* Set the frequency of the SPI clock (unused in slave mode). */ 185*1b2596b5SMatthias Ringwald uint32_t baudrate; 186*1b2596b5SMatthias Ringwald 187*1b2596b5SMatthias Ringwald /* 188*1b2596b5SMatthias Ringwald * Number of bits, which should be one of the following: US_MR_CHRL_5_BIT, 189*1b2596b5SMatthias Ringwald * US_MR_CHRL_6_BIT, US_MR_CHRL_7_BIT, US_MR_CHRL_8_BIT or 190*1b2596b5SMatthias Ringwald * US_MR_MODE9. 191*1b2596b5SMatthias Ringwald */ 192*1b2596b5SMatthias Ringwald uint32_t char_length; 193*1b2596b5SMatthias Ringwald 194*1b2596b5SMatthias Ringwald /* 195*1b2596b5SMatthias Ringwald * Which SPI mode to use, which should be one of the following: 196*1b2596b5SMatthias Ringwald * SPI_MODE_0, SPI_MODE_1, SPI_MODE_2, SPI_MODE_3. 197*1b2596b5SMatthias Ringwald */ 198*1b2596b5SMatthias Ringwald uint32_t spi_mode; 199*1b2596b5SMatthias Ringwald 200*1b2596b5SMatthias Ringwald /* 201*1b2596b5SMatthias Ringwald * Run the channel in test mode, which should be one of following: 202*1b2596b5SMatthias Ringwald * US_MR_CHMODE_NORMAL, US_MR_CHMODE_AUTOMATIC, 203*1b2596b5SMatthias Ringwald * US_MR_CHMODE_LOCAL_LOOPBACK, US_MR_CHMODE_REMOTE_LOOPBACK. 204*1b2596b5SMatthias Ringwald */ 205*1b2596b5SMatthias Ringwald uint32_t channel_mode; 206*1b2596b5SMatthias Ringwald } usart_spi_opt_t; 207*1b2596b5SMatthias Ringwald 208*1b2596b5SMatthias Ringwald void usart_reset(Usart *p_usart); 209*1b2596b5SMatthias Ringwald uint32_t usart_set_async_baudrate(Usart *p_usart, 210*1b2596b5SMatthias Ringwald uint32_t baudrate, uint32_t ul_mck); 211*1b2596b5SMatthias Ringwald uint32_t usart_init_rs232(Usart *p_usart, 212*1b2596b5SMatthias Ringwald const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck); 213*1b2596b5SMatthias Ringwald uint32_t usart_init_hw_handshaking(Usart *p_usart, 214*1b2596b5SMatthias Ringwald const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck); 215*1b2596b5SMatthias Ringwald #if (SAM3S || SAM4S || SAM3U || SAM4L || SAM4E) 216*1b2596b5SMatthias Ringwald uint32_t usart_init_modem(Usart *p_usart, 217*1b2596b5SMatthias Ringwald const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck); 218*1b2596b5SMatthias Ringwald #endif 219*1b2596b5SMatthias Ringwald uint32_t usart_init_sync_master(Usart *p_usart, 220*1b2596b5SMatthias Ringwald const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck); 221*1b2596b5SMatthias Ringwald uint32_t usart_init_sync_slave(Usart *p_usart, 222*1b2596b5SMatthias Ringwald const sam_usart_opt_t *p_usart_opt); 223*1b2596b5SMatthias Ringwald uint32_t usart_init_rs485(Usart *p_usart, 224*1b2596b5SMatthias Ringwald const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck); 225*1b2596b5SMatthias Ringwald #if (!SAMG55 && !SAMV71 && !SAMV70 && !SAME70 && !SAMS70) 226*1b2596b5SMatthias Ringwald uint32_t usart_init_irda(Usart *p_usart, 227*1b2596b5SMatthias Ringwald const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck); 228*1b2596b5SMatthias Ringwald #endif 229*1b2596b5SMatthias Ringwald #if (!SAMV71 && !SAMV70 && !SAME70 && !SAMS70) 230*1b2596b5SMatthias Ringwald uint32_t usart_init_iso7816(Usart *p_usart, 231*1b2596b5SMatthias Ringwald const usart_iso7816_opt_t *p_usart_opt, uint32_t ul_mck); 232*1b2596b5SMatthias Ringwald void usart_reset_iterations(Usart *p_usart); 233*1b2596b5SMatthias Ringwald void usart_reset_nack(Usart *p_usart); 234*1b2596b5SMatthias Ringwald uint32_t usart_is_rx_buf_end(Usart *p_usart); 235*1b2596b5SMatthias Ringwald uint32_t usart_is_tx_buf_end(Usart *p_usart); 236*1b2596b5SMatthias Ringwald uint32_t usart_is_rx_buf_full(Usart *p_usart); 237*1b2596b5SMatthias Ringwald uint32_t usart_is_tx_buf_empty(Usart *p_usart); 238*1b2596b5SMatthias Ringwald uint8_t usart_get_error_number(Usart *p_usart); 239*1b2596b5SMatthias Ringwald #endif 240*1b2596b5SMatthias Ringwald uint32_t usart_init_spi_master(Usart *p_usart, 241*1b2596b5SMatthias Ringwald const usart_spi_opt_t *p_usart_opt, uint32_t ul_mck); 242*1b2596b5SMatthias Ringwald uint32_t usart_init_spi_slave(Usart *p_usart, 243*1b2596b5SMatthias Ringwald const usart_spi_opt_t *p_usart_opt); 244*1b2596b5SMatthias Ringwald #if (SAM3XA || SAM4L || SAMG55 || SAMV71 || SAMV70 || SAME70 || SAMS70) 245*1b2596b5SMatthias Ringwald uint32_t usart_init_lin_master(Usart *p_usart, uint32_t ul_baudrate, 246*1b2596b5SMatthias Ringwald uint32_t ul_mck); 247*1b2596b5SMatthias Ringwald uint32_t usart_init_lin_slave(Usart *p_usart, uint32_t ul_baudrate, 248*1b2596b5SMatthias Ringwald uint32_t ul_mck); 249*1b2596b5SMatthias Ringwald void usart_lin_abort_tx(Usart *p_usart); 250*1b2596b5SMatthias Ringwald void usart_lin_send_wakeup_signal(Usart *p_usart); 251*1b2596b5SMatthias Ringwald void usart_lin_set_node_action(Usart *p_usart, uint8_t uc_action); 252*1b2596b5SMatthias Ringwald void usart_lin_disable_parity(Usart *p_usart); 253*1b2596b5SMatthias Ringwald void usart_lin_enable_parity(Usart *p_usart); 254*1b2596b5SMatthias Ringwald void usart_lin_disable_checksum(Usart *p_usart); 255*1b2596b5SMatthias Ringwald void usart_lin_enable_checksum(Usart *p_usart); 256*1b2596b5SMatthias Ringwald void usart_lin_set_checksum_type(Usart *p_usart, uint8_t uc_type); 257*1b2596b5SMatthias Ringwald void usart_lin_set_data_len_mode(Usart *p_usart, uint8_t uc_mode); 258*1b2596b5SMatthias Ringwald void usart_lin_disable_frame_slot(Usart *p_usart); 259*1b2596b5SMatthias Ringwald void usart_lin_enable_frame_slot(Usart *p_usart); 260*1b2596b5SMatthias Ringwald void usart_lin_set_wakeup_signal_type(Usart *p_usart, uint8_t uc_type); 261*1b2596b5SMatthias Ringwald void usart_lin_set_response_data_len(Usart *p_usart, uint8_t uc_len); 262*1b2596b5SMatthias Ringwald void usart_lin_disable_pdc_mode(Usart *p_usart); 263*1b2596b5SMatthias Ringwald void usart_lin_enable_pdc_mode(Usart *p_usart); 264*1b2596b5SMatthias Ringwald void usart_lin_set_tx_identifier(Usart *p_usart, uint8_t uc_id); 265*1b2596b5SMatthias Ringwald uint8_t usart_lin_read_identifier(Usart *p_usart); 266*1b2596b5SMatthias Ringwald uint8_t usart_lin_get_data_length(Usart *usart); 267*1b2596b5SMatthias Ringwald #endif 268*1b2596b5SMatthias Ringwald #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 269*1b2596b5SMatthias Ringwald uint8_t usart_lin_identifier_send_complete(Usart *usart); 270*1b2596b5SMatthias Ringwald uint8_t usart_lin_identifier_reception_complete(Usart *usart); 271*1b2596b5SMatthias Ringwald uint8_t usart_lin_tx_complete(Usart *usart); 272*1b2596b5SMatthias Ringwald uint32_t usart_init_lon(Usart *p_usart, uint32_t ul_baudrate, uint32_t ul_mck); 273*1b2596b5SMatthias Ringwald void usart_lon_set_comm_type(Usart *p_usart, uint8_t uc_type); 274*1b2596b5SMatthias Ringwald void usart_lon_disable_coll_detection(Usart *p_usart); 275*1b2596b5SMatthias Ringwald void usart_lon_enable_coll_detection(Usart *p_usart); 276*1b2596b5SMatthias Ringwald void usart_lon_set_tcol(Usart *p_usart, uint8_t uc_type); 277*1b2596b5SMatthias Ringwald void usart_lon_set_cdtail(Usart *p_usart, uint8_t uc_type); 278*1b2596b5SMatthias Ringwald void usart_lon_set_dmam(Usart *p_usart, uint8_t uc_type); 279*1b2596b5SMatthias Ringwald void usart_lon_set_beta1_tx_len(Usart *p_usart, uint32_t ul_len); 280*1b2596b5SMatthias Ringwald void usart_lon_set_beta1_rx_len(Usart *p_usart, uint32_t ul_len); 281*1b2596b5SMatthias Ringwald void usart_lon_set_priority(Usart *p_usart, uint8_t uc_psnb, uint8_t uc_nps); 282*1b2596b5SMatthias Ringwald void usart_lon_set_tx_idt(Usart *p_usart, uint32_t ul_time); 283*1b2596b5SMatthias Ringwald void usart_lon_set_rx_idt(Usart *p_usart, uint32_t ul_time); 284*1b2596b5SMatthias Ringwald void usart_lon_set_pre_len(Usart *p_usart, uint32_t ul_len); 285*1b2596b5SMatthias Ringwald void usart_lon_set_data_len(Usart *p_usart, uint8_t uc_len); 286*1b2596b5SMatthias Ringwald void usart_lon_set_l2hdr(Usart *p_usart, uint8_t uc_bli, uint8_t uc_altp, uint8_t uc_pb); 287*1b2596b5SMatthias Ringwald uint32_t usart_lon_is_tx_end(Usart *p_usart); 288*1b2596b5SMatthias Ringwald uint32_t usart_lon_is_rx_end(Usart *p_usart); 289*1b2596b5SMatthias Ringwald #endif 290*1b2596b5SMatthias Ringwald void usart_enable_tx(Usart *p_usart); 291*1b2596b5SMatthias Ringwald void usart_disable_tx(Usart *p_usart); 292*1b2596b5SMatthias Ringwald void usart_reset_tx(Usart *p_usart); 293*1b2596b5SMatthias Ringwald void usart_set_tx_timeguard(Usart *p_usart, uint32_t timeguard); 294*1b2596b5SMatthias Ringwald void usart_enable_rx(Usart *p_usart); 295*1b2596b5SMatthias Ringwald void usart_disable_rx(Usart *p_usart); 296*1b2596b5SMatthias Ringwald void usart_reset_rx(Usart *p_usart); 297*1b2596b5SMatthias Ringwald void usart_set_rx_timeout(Usart *p_usart, uint32_t timeout); 298*1b2596b5SMatthias Ringwald void usart_enable_interrupt(Usart *p_usart, uint32_t ul_sources); 299*1b2596b5SMatthias Ringwald void usart_disable_interrupt(Usart *p_usart, uint32_t ul_sources); 300*1b2596b5SMatthias Ringwald uint32_t usart_get_interrupt_mask(Usart *p_usart); 301*1b2596b5SMatthias Ringwald uint32_t usart_get_status(Usart *p_usart); 302*1b2596b5SMatthias Ringwald void usart_reset_status(Usart *p_usart); 303*1b2596b5SMatthias Ringwald void usart_start_tx_break(Usart *p_usart); 304*1b2596b5SMatthias Ringwald void usart_stop_tx_break(Usart *p_usart); 305*1b2596b5SMatthias Ringwald void usart_start_rx_timeout(Usart *p_usart); 306*1b2596b5SMatthias Ringwald uint32_t usart_send_address(Usart *p_usart, uint32_t ul_addr); 307*1b2596b5SMatthias Ringwald void usart_restart_rx_timeout(Usart *p_usart); 308*1b2596b5SMatthias Ringwald #if (SAM3S || SAM4S || SAM3U || SAM4L || SAM4E) 309*1b2596b5SMatthias Ringwald void usart_drive_DTR_pin_low(Usart *p_usart); 310*1b2596b5SMatthias Ringwald void usart_drive_DTR_pin_high(Usart *p_usart); 311*1b2596b5SMatthias Ringwald #endif 312*1b2596b5SMatthias Ringwald void usart_drive_RTS_pin_low(Usart *p_usart); 313*1b2596b5SMatthias Ringwald void usart_drive_RTS_pin_high(Usart *p_usart); 314*1b2596b5SMatthias Ringwald void usart_spi_force_chip_select(Usart *p_usart); 315*1b2596b5SMatthias Ringwald void usart_spi_release_chip_select(Usart *p_usart); 316*1b2596b5SMatthias Ringwald uint32_t usart_is_tx_ready(Usart *p_usart); 317*1b2596b5SMatthias Ringwald uint32_t usart_is_tx_empty(Usart *p_usart); 318*1b2596b5SMatthias Ringwald uint32_t usart_is_rx_ready(Usart *p_usart); 319*1b2596b5SMatthias Ringwald uint32_t usart_write(Usart *p_usart, uint32_t c); 320*1b2596b5SMatthias Ringwald uint32_t usart_putchar(Usart *p_usart, uint32_t c); 321*1b2596b5SMatthias Ringwald void usart_write_line(Usart *p_usart, const char *string); 322*1b2596b5SMatthias Ringwald uint32_t usart_read(Usart *p_usart, uint32_t *c); 323*1b2596b5SMatthias Ringwald uint32_t usart_getchar(Usart *p_usart, uint32_t *c); 324*1b2596b5SMatthias Ringwald #if (SAM3XA || SAM3U) 325*1b2596b5SMatthias Ringwald uint32_t *usart_get_tx_access(Usart *p_usart); 326*1b2596b5SMatthias Ringwald uint32_t *usart_get_rx_access(Usart *p_usart); 327*1b2596b5SMatthias Ringwald #endif 328*1b2596b5SMatthias Ringwald #if (!SAM4L && !SAMV71 && !SAMV70 && !SAME70 && !SAMS70) 329*1b2596b5SMatthias Ringwald Pdc *usart_get_pdc_base(Usart *p_usart); 330*1b2596b5SMatthias Ringwald #endif 331*1b2596b5SMatthias Ringwald void usart_enable_writeprotect(Usart *p_usart); 332*1b2596b5SMatthias Ringwald void usart_disable_writeprotect(Usart *p_usart); 333*1b2596b5SMatthias Ringwald uint32_t usart_get_writeprotect_status(Usart *p_usart); 334*1b2596b5SMatthias Ringwald #if (SAM3S || SAM4S || SAM3U || SAM3XA || SAM4L || SAM4E || SAM4C || SAM4CP || SAM4CM || SAMV70 || SAMV71 || SAMS70 || SAME70) 335*1b2596b5SMatthias Ringwald void usart_man_set_tx_pre_len(Usart *p_usart, uint8_t uc_len); 336*1b2596b5SMatthias Ringwald void usart_man_set_tx_pre_pattern(Usart *p_usart, uint8_t uc_pattern); 337*1b2596b5SMatthias Ringwald void usart_man_set_tx_polarity(Usart *p_usart, uint8_t uc_polarity); 338*1b2596b5SMatthias Ringwald void usart_man_set_rx_pre_len(Usart *p_usart, uint8_t uc_len); 339*1b2596b5SMatthias Ringwald void usart_man_set_rx_pre_pattern(Usart *p_usart, uint8_t uc_pattern); 340*1b2596b5SMatthias Ringwald void usart_man_set_rx_polarity(Usart *p_usart, uint8_t uc_polarity); 341*1b2596b5SMatthias Ringwald void usart_man_enable_drift_compensation(Usart *p_usart); 342*1b2596b5SMatthias Ringwald void usart_man_disable_drift_compensation(Usart *p_usart); 343*1b2596b5SMatthias Ringwald #endif 344*1b2596b5SMatthias Ringwald 345*1b2596b5SMatthias Ringwald #if SAM4L 346*1b2596b5SMatthias Ringwald uint32_t usart_get_version(Usart *p_usart); 347*1b2596b5SMatthias Ringwald #endif 348*1b2596b5SMatthias Ringwald 349*1b2596b5SMatthias Ringwald #if SAMG55 350*1b2596b5SMatthias Ringwald void usart_set_sleepwalking(Usart *p_uart, uint8_t ul_low_value, 351*1b2596b5SMatthias Ringwald bool cmpmode, bool cmppar, uint8_t ul_high_value); 352*1b2596b5SMatthias Ringwald #endif 353*1b2596b5SMatthias Ringwald 354*1b2596b5SMatthias Ringwald /// @cond 0 355*1b2596b5SMatthias Ringwald /**INDENT-OFF**/ 356*1b2596b5SMatthias Ringwald #ifdef __cplusplus 357*1b2596b5SMatthias Ringwald } 358*1b2596b5SMatthias Ringwald #endif 359*1b2596b5SMatthias Ringwald /**INDENT-ON**/ 360*1b2596b5SMatthias Ringwald /// @endcond 361*1b2596b5SMatthias Ringwald 362*1b2596b5SMatthias Ringwald //! @} 363*1b2596b5SMatthias Ringwald 364*1b2596b5SMatthias Ringwald /** 365*1b2596b5SMatthias Ringwald * \page sam_usart_quickstart Quick start guide for the SAM USART module 366*1b2596b5SMatthias Ringwald * 367*1b2596b5SMatthias Ringwald * This is the quick start guide for the \ref group_sam_drivers_usart 368*1b2596b5SMatthias Ringwald * "USART module", with step-by-step instructions on how to configure and 369*1b2596b5SMatthias Ringwald * use the driver in a selection of use cases. 370*1b2596b5SMatthias Ringwald * 371*1b2596b5SMatthias Ringwald * The use cases contain several code fragments. The code fragments in the 372*1b2596b5SMatthias Ringwald * steps for setup can be copied into a custom initialization function, while 373*1b2596b5SMatthias Ringwald * the steps for usage can be copied into, e.g., the main application function. 374*1b2596b5SMatthias Ringwald * 375*1b2596b5SMatthias Ringwald * \note Some SAM devices contain both USART and UART modules, with the latter 376*1b2596b5SMatthias Ringwald * being a subset in functionality of the former but physically separate 377*1b2596b5SMatthias Ringwald * peripherals. UART modules are compatible with the USART driver, but 378*1b2596b5SMatthias Ringwald * only for the functions and modes supported by the base UART driver. 379*1b2596b5SMatthias Ringwald * 380*1b2596b5SMatthias Ringwald * \section usart_use_cases USART use cases 381*1b2596b5SMatthias Ringwald * - \ref usart_basic_use_case 382*1b2596b5SMatthias Ringwald * - \subpage usart_use_case_1 383*1b2596b5SMatthias Ringwald * - \subpage usart_use_case_2 384*1b2596b5SMatthias Ringwald * 385*1b2596b5SMatthias Ringwald * \note The USART pins configuration are not included here. Please refer 386*1b2596b5SMatthias Ringwald * the related code in board_init() function. 387*1b2596b5SMatthias Ringwald * 388*1b2596b5SMatthias Ringwald * \section usart_basic_use_case Basic use case - transmit a character 389*1b2596b5SMatthias Ringwald * In this use case, the USART module is configured for: 390*1b2596b5SMatthias Ringwald * - Using USART0 391*1b2596b5SMatthias Ringwald * - Baudrate: 9600 392*1b2596b5SMatthias Ringwald * - Character length: 8 bit 393*1b2596b5SMatthias Ringwald * - Parity mode: Disabled 394*1b2596b5SMatthias Ringwald * - Stop bit: None 395*1b2596b5SMatthias Ringwald * - RS232 mode 396*1b2596b5SMatthias Ringwald * 397*1b2596b5SMatthias Ringwald * \section usart_basic_use_case_setup Setup steps 398*1b2596b5SMatthias Ringwald * 399*1b2596b5SMatthias Ringwald * \subsection usart_basic_use_case_setup_prereq Prerequisites 400*1b2596b5SMatthias Ringwald * -# \ref sysclk_group "System Clock Management (sysclock)" 401*1b2596b5SMatthias Ringwald * -# \ref ioport_group "Common IOPORT API (ioport)" 402*1b2596b5SMatthias Ringwald * 403*1b2596b5SMatthias Ringwald * \subsection usart_basic_use_case_setup_code Example code 404*1b2596b5SMatthias Ringwald * The following configuration must be added to the project (typically to a 405*1b2596b5SMatthias Ringwald * conf_usart.h file, but it can also be added to your main application file.) 406*1b2596b5SMatthias Ringwald * \code 407*1b2596b5SMatthias Ringwald #define USART_SERIAL USART0 408*1b2596b5SMatthias Ringwald #define USART_SERIAL_ID ID_USART0 //USART0 for sam4l 409*1b2596b5SMatthias Ringwald #define USART_SERIAL_BAUDRATE 9600 410*1b2596b5SMatthias Ringwald #define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT 411*1b2596b5SMatthias Ringwald #define USART_SERIAL_PARITY US_MR_PAR_NO 412*1b2596b5SMatthias Ringwald #define USART_SERIAL_STOP_BIT US_MR_NBSTOP_1_BIT 413*1b2596b5SMatthias Ringwald \endcode 414*1b2596b5SMatthias Ringwald * 415*1b2596b5SMatthias Ringwald * Add to application initialization: 416*1b2596b5SMatthias Ringwald * \code 417*1b2596b5SMatthias Ringwald sysclk_init(); 418*1b2596b5SMatthias Ringwald 419*1b2596b5SMatthias Ringwald board_init(); 420*1b2596b5SMatthias Ringwald 421*1b2596b5SMatthias Ringwald const sam_usart_opt_t usart_console_settings = { 422*1b2596b5SMatthias Ringwald USART_SERIAL_BAUDRATE, 423*1b2596b5SMatthias Ringwald USART_SERIAL_CHAR_LENGTH, 424*1b2596b5SMatthias Ringwald USART_SERIAL_PARITY, 425*1b2596b5SMatthias Ringwald USART_SERIAL_STOP_BIT, 426*1b2596b5SMatthias Ringwald US_MR_CHMODE_NORMAL 427*1b2596b5SMatthias Ringwald }; 428*1b2596b5SMatthias Ringwald #if SAM4L 429*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL); 430*1b2596b5SMatthias Ringwald #else 431*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL_ID); 432*1b2596b5SMatthias Ringwald #endif 433*1b2596b5SMatthias Ringwald usart_init_rs232(USART_SERIAL, &usart_console_settings, 434*1b2596b5SMatthias Ringwald sysclk_get_main_hz()); 435*1b2596b5SMatthias Ringwald usart_enable_tx(USART_SERIAL); 436*1b2596b5SMatthias Ringwald usart_enable_rx(USART_SERIAL); 437*1b2596b5SMatthias Ringwald \endcode 438*1b2596b5SMatthias Ringwald * 439*1b2596b5SMatthias Ringwald * \subsection usart_basic_use_case_setup_flow Workflow 440*1b2596b5SMatthias Ringwald * -# Initialize system clock: 441*1b2596b5SMatthias Ringwald * \code 442*1b2596b5SMatthias Ringwald sysclk_init(); 443*1b2596b5SMatthias Ringwald \endcode 444*1b2596b5SMatthias Ringwald * -# Configure the USART Tx and Rx pins by call the board init function: 445*1b2596b5SMatthias Ringwald * \code 446*1b2596b5SMatthias Ringwald board_init(); 447*1b2596b5SMatthias Ringwald \endcode 448*1b2596b5SMatthias Ringwald * \note Set the following define in conf_board.h file to enable COM port,it will be used in 449*1b2596b5SMatthias Ringwald * board_init() function to set up IOPorts for the USART pins. 450*1b2596b5SMatthias Ringwald * For SAM4L: 451*1b2596b5SMatthias Ringwald * \code 452*1b2596b5SMatthias Ringwald #define CONF_BOARD_COM_PORT 453*1b2596b5SMatthias Ringwald \endcode 454*1b2596b5SMatthias Ringwald * For other SAM devices: 455*1b2596b5SMatthias Ringwald * \code 456*1b2596b5SMatthias Ringwald #define CONF_BOARD_UART_CONSOLE 457*1b2596b5SMatthias Ringwald \endcode 458*1b2596b5SMatthias Ringwald * -# Create USART options struct: 459*1b2596b5SMatthias Ringwald * \code 460*1b2596b5SMatthias Ringwald const sam_usart_opt_t usart_console_settings = { 461*1b2596b5SMatthias Ringwald USART_SERIAL_BAUDRATE, 462*1b2596b5SMatthias Ringwald USART_SERIAL_CHAR_LENGTH, 463*1b2596b5SMatthias Ringwald USART_SERIAL_PARITY, 464*1b2596b5SMatthias Ringwald USART_SERIAL_STOP_BIT, 465*1b2596b5SMatthias Ringwald US_MR_CHMODE_NORMAL 466*1b2596b5SMatthias Ringwald }; 467*1b2596b5SMatthias Ringwald \endcode 468*1b2596b5SMatthias Ringwald * -# Enable the clock to the USART module: 469*1b2596b5SMatthias Ringwald * \code 470*1b2596b5SMatthias Ringwald #if SAM4L 471*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL); 472*1b2596b5SMatthias Ringwald #else 473*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL_ID); 474*1b2596b5SMatthias Ringwald #endif 475*1b2596b5SMatthias Ringwald \endcode 476*1b2596b5SMatthias Ringwald * -# Initialize the USART module in RS232 mode: 477*1b2596b5SMatthias Ringwald * \code 478*1b2596b5SMatthias Ringwald usart_init_rs232(USART_SERIAL, &usart_console_settings, 479*1b2596b5SMatthias Ringwald sysclk_get_main_hz()); 480*1b2596b5SMatthias Ringwald \endcode 481*1b2596b5SMatthias Ringwald * -# Enable the Rx and Tx modes of the USART module: 482*1b2596b5SMatthias Ringwald * \code 483*1b2596b5SMatthias Ringwald usart_enable_tx(USART_SERIAL); 484*1b2596b5SMatthias Ringwald usart_enable_rx(USART_SERIAL); 485*1b2596b5SMatthias Ringwald \endcode 486*1b2596b5SMatthias Ringwald * 487*1b2596b5SMatthias Ringwald * \section usart_basic_use_case_usage Usage steps 488*1b2596b5SMatthias Ringwald * 489*1b2596b5SMatthias Ringwald * \subsection usart_basic_use_case_usage_code Example code 490*1b2596b5SMatthias Ringwald * Add to application C-file: 491*1b2596b5SMatthias Ringwald * \code 492*1b2596b5SMatthias Ringwald usart_putchar(USART_SERIAL, 'a'); 493*1b2596b5SMatthias Ringwald \endcode 494*1b2596b5SMatthias Ringwald * 495*1b2596b5SMatthias Ringwald * \subsection usart_basic_use_case_usage_flow Workflow 496*1b2596b5SMatthias Ringwald * -# Send an 'a' character via USART 497*1b2596b5SMatthias Ringwald * \code usart_putchar(USART_SERIAL, 'a'); \endcode 498*1b2596b5SMatthias Ringwald */ 499*1b2596b5SMatthias Ringwald 500*1b2596b5SMatthias Ringwald /** 501*1b2596b5SMatthias Ringwald * \page usart_use_case_1 USART receive character and echo back 502*1b2596b5SMatthias Ringwald * 503*1b2596b5SMatthias Ringwald * In this use case, the USART module is configured for: 504*1b2596b5SMatthias Ringwald * - Using USART0 505*1b2596b5SMatthias Ringwald * - Baudrate: 9600 506*1b2596b5SMatthias Ringwald * - Character length: 8 bit 507*1b2596b5SMatthias Ringwald * - Parity mode: Disabled 508*1b2596b5SMatthias Ringwald * - Stop bit: None 509*1b2596b5SMatthias Ringwald * - RS232 mode 510*1b2596b5SMatthias Ringwald * 511*1b2596b5SMatthias Ringwald * The use case waits for a received character on the configured USART and 512*1b2596b5SMatthias Ringwald * echoes the character back to the same USART. 513*1b2596b5SMatthias Ringwald * 514*1b2596b5SMatthias Ringwald * \section usart_use_case_1_setup Setup steps 515*1b2596b5SMatthias Ringwald * 516*1b2596b5SMatthias Ringwald * \subsection usart_use_case_1_setup_prereq Prerequisites 517*1b2596b5SMatthias Ringwald * -# \ref sysclk_group "System Clock Management (sysclock)" 518*1b2596b5SMatthias Ringwald * -# \ref ioport_group "Common IOPORT API (ioport)" 519*1b2596b5SMatthias Ringwald * 520*1b2596b5SMatthias Ringwald * \subsection usart_use_case_1_setup_code Example code 521*1b2596b5SMatthias Ringwald * The following configuration must be added to the project (typically to a 522*1b2596b5SMatthias Ringwald * conf_usart.h file, but it can also be added to your main application file.): 523*1b2596b5SMatthias Ringwald * \code 524*1b2596b5SMatthias Ringwald #define USART_SERIAL USART0 525*1b2596b5SMatthias Ringwald #define USART_SERIAL_ID ID_USART0 //USART0 for sam4l 526*1b2596b5SMatthias Ringwald #define USART_SERIAL_BAUDRATE 9600 527*1b2596b5SMatthias Ringwald #define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT 528*1b2596b5SMatthias Ringwald #define USART_SERIAL_PARITY US_MR_PAR_NO 529*1b2596b5SMatthias Ringwald #define USART_SERIAL_STOP_BIT US_MR_NBSTOP_1_BIT 530*1b2596b5SMatthias Ringwald \endcode 531*1b2596b5SMatthias Ringwald * 532*1b2596b5SMatthias Ringwald * A variable for the received byte must be added: 533*1b2596b5SMatthias Ringwald * \code 534*1b2596b5SMatthias Ringwald uint32_t received_byte; 535*1b2596b5SMatthias Ringwald \endcode 536*1b2596b5SMatthias Ringwald * 537*1b2596b5SMatthias Ringwald * Add to application initialization: 538*1b2596b5SMatthias Ringwald * \code 539*1b2596b5SMatthias Ringwald sysclk_init(); 540*1b2596b5SMatthias Ringwald 541*1b2596b5SMatthias Ringwald board_init(); 542*1b2596b5SMatthias Ringwald 543*1b2596b5SMatthias Ringwald const sam_usart_opt_t usart_console_settings = { 544*1b2596b5SMatthias Ringwald USART_SERIAL_BAUDRATE, 545*1b2596b5SMatthias Ringwald USART_SERIAL_CHAR_LENGTH, 546*1b2596b5SMatthias Ringwald USART_SERIAL_PARITY, 547*1b2596b5SMatthias Ringwald USART_SERIAL_STOP_BIT, 548*1b2596b5SMatthias Ringwald US_MR_CHMODE_NORMAL 549*1b2596b5SMatthias Ringwald }; 550*1b2596b5SMatthias Ringwald 551*1b2596b5SMatthias Ringwald #if SAM4L 552*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL); 553*1b2596b5SMatthias Ringwald #else 554*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL_ID); 555*1b2596b5SMatthias Ringwald #endif 556*1b2596b5SMatthias Ringwald 557*1b2596b5SMatthias Ringwald usart_init_rs232(USART_SERIAL, &usart_console_settings, 558*1b2596b5SMatthias Ringwald sysclk_get_main_hz()); 559*1b2596b5SMatthias Ringwald usart_enable_tx(USART_SERIAL); 560*1b2596b5SMatthias Ringwald usart_enable_rx(USART_SERIAL); 561*1b2596b5SMatthias Ringwald \endcode 562*1b2596b5SMatthias Ringwald * 563*1b2596b5SMatthias Ringwald * \subsection usart_use_case_1_setup_flow Workflow 564*1b2596b5SMatthias Ringwald * -# Initialize system clock: 565*1b2596b5SMatthias Ringwald * \code 566*1b2596b5SMatthias Ringwald sysclk_init(); 567*1b2596b5SMatthias Ringwald \endcode 568*1b2596b5SMatthias Ringwald * -# Configure the USART Tx and Rx pins by call the board init function: 569*1b2596b5SMatthias Ringwald * \code 570*1b2596b5SMatthias Ringwald board_init(); 571*1b2596b5SMatthias Ringwald \endcode 572*1b2596b5SMatthias Ringwald * \note Set the following define in conf_board.h file to enable COM port,it will be used in 573*1b2596b5SMatthias Ringwald * board_init() function to set up IOPorts for the USART pins. 574*1b2596b5SMatthias Ringwald * For SAM4L: 575*1b2596b5SMatthias Ringwald * \code 576*1b2596b5SMatthias Ringwald #define CONF_BOARD_COM_PORT 577*1b2596b5SMatthias Ringwald \endcode 578*1b2596b5SMatthias Ringwald * For other SAM devices: 579*1b2596b5SMatthias Ringwald * \code 580*1b2596b5SMatthias Ringwald #define CONF_BOARD_UART_CONSOLE 581*1b2596b5SMatthias Ringwald \endcode 582*1b2596b5SMatthias Ringwald * -# Create USART options struct: 583*1b2596b5SMatthias Ringwald * \code 584*1b2596b5SMatthias Ringwald const sam_usart_opt_t usart_console_settings = { 585*1b2596b5SMatthias Ringwald USART_SERIAL_BAUDRATE, 586*1b2596b5SMatthias Ringwald USART_SERIAL_CHAR_LENGTH, 587*1b2596b5SMatthias Ringwald USART_SERIAL_PARITY, 588*1b2596b5SMatthias Ringwald USART_SERIAL_STOP_BIT, 589*1b2596b5SMatthias Ringwald US_MR_CHMODE_NORMAL 590*1b2596b5SMatthias Ringwald }; 591*1b2596b5SMatthias Ringwald \endcode 592*1b2596b5SMatthias Ringwald * -# Enable the clock to the USART module: 593*1b2596b5SMatthias Ringwald * \code 594*1b2596b5SMatthias Ringwald #if SAM4L 595*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL); 596*1b2596b5SMatthias Ringwald #else 597*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL_ID); 598*1b2596b5SMatthias Ringwald #endif 599*1b2596b5SMatthias Ringwald \endcode 600*1b2596b5SMatthias Ringwald * -# Initialize the USART module in RS232 mode: 601*1b2596b5SMatthias Ringwald * \code 602*1b2596b5SMatthias Ringwald usart_init_rs232(USART_SERIAL, &usart_console_settings, 603*1b2596b5SMatthias Ringwald sysclk_get_main_hz()); 604*1b2596b5SMatthias Ringwald \endcode 605*1b2596b5SMatthias Ringwald * -# Enable the Rx and Tx modes of the USART module: 606*1b2596b5SMatthias Ringwald * \code 607*1b2596b5SMatthias Ringwald usart_enable_tx(USART_SERIAL); 608*1b2596b5SMatthias Ringwald usart_enable_rx(USART_SERIAL); 609*1b2596b5SMatthias Ringwald \endcode 610*1b2596b5SMatthias Ringwald * 611*1b2596b5SMatthias Ringwald * \section usart_use_case_1_usage Usage steps 612*1b2596b5SMatthias Ringwald * 613*1b2596b5SMatthias Ringwald * \subsection usart_use_case_1_usage_code Example code 614*1b2596b5SMatthias Ringwald * Add to, e.g., main loop in application C-file: 615*1b2596b5SMatthias Ringwald * \code 616*1b2596b5SMatthias Ringwald received_byte = usart_getchar(USART_SERIAL); 617*1b2596b5SMatthias Ringwald usart_putchar(USART_SERIAL, received_byte); 618*1b2596b5SMatthias Ringwald \endcode 619*1b2596b5SMatthias Ringwald * 620*1b2596b5SMatthias Ringwald * \subsection usart_use_case_1_usage_flow Workflow 621*1b2596b5SMatthias Ringwald * -# Wait for reception of a character: 622*1b2596b5SMatthias Ringwald * \code usart_getchar(USART_SERIAL, &received_byte); \endcode 623*1b2596b5SMatthias Ringwald * -# Echo the character back: 624*1b2596b5SMatthias Ringwald * \code usart_putchar(USART_SERIAL, received_byte); \endcode 625*1b2596b5SMatthias Ringwald */ 626*1b2596b5SMatthias Ringwald 627*1b2596b5SMatthias Ringwald /** 628*1b2596b5SMatthias Ringwald * \page usart_use_case_2 USART receive character and echo back via interrupts 629*1b2596b5SMatthias Ringwald * 630*1b2596b5SMatthias Ringwald * In this use case, the USART module is configured for: 631*1b2596b5SMatthias Ringwald * - Using USART0 632*1b2596b5SMatthias Ringwald * - Baudrate: 9600 633*1b2596b5SMatthias Ringwald * - Character length: 8 bit 634*1b2596b5SMatthias Ringwald * - Parity mode: Disabled 635*1b2596b5SMatthias Ringwald * - Stop bit: None 636*1b2596b5SMatthias Ringwald * - RS232 mode 637*1b2596b5SMatthias Ringwald * 638*1b2596b5SMatthias Ringwald * The use case waits for a received character on the configured USART and 639*1b2596b5SMatthias Ringwald * echoes the character back to the same USART. The character reception is 640*1b2596b5SMatthias Ringwald * performed via an interrupt handler, rather than the polling method used 641*1b2596b5SMatthias Ringwald * in \ref usart_use_case_1. 642*1b2596b5SMatthias Ringwald * 643*1b2596b5SMatthias Ringwald * \section usart_use_case_2_setup Setup steps 644*1b2596b5SMatthias Ringwald * 645*1b2596b5SMatthias Ringwald * \subsection usart_use_case_2_setup_prereq Prerequisites 646*1b2596b5SMatthias Ringwald * -# \ref sysclk_group "System Clock Management (sysclock)" 647*1b2596b5SMatthias Ringwald * -# \ref pio_group "Parallel Input/Output Controller (pio)" 648*1b2596b5SMatthias Ringwald * -# \ref pmc_group "Power Management Controller (pmc)" 649*1b2596b5SMatthias Ringwald * 650*1b2596b5SMatthias Ringwald * \subsection usart_use_case_2_setup_code Example code 651*1b2596b5SMatthias Ringwald * The following configuration must be added to the project (typically to a 652*1b2596b5SMatthias Ringwald * conf_usart.h file, but it can also be added to your main application file.): 653*1b2596b5SMatthias Ringwald * \code 654*1b2596b5SMatthias Ringwald #define USART_SERIAL USART0 655*1b2596b5SMatthias Ringwald #define USART_SERIAL_ID ID_USART0 //USART0 for sam4l 656*1b2596b5SMatthias Ringwald #define USART_SERIAL_ISR_HANDLER USART0_Handler 657*1b2596b5SMatthias Ringwald #define USART_SERIAL_BAUDRATE 9600 658*1b2596b5SMatthias Ringwald #define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT 659*1b2596b5SMatthias Ringwald #define USART_SERIAL_PARITY US_MR_PAR_NO 660*1b2596b5SMatthias Ringwald #define USART_SERIAL_STOP_BIT US_MR_NBSTOP_1_BIT 661*1b2596b5SMatthias Ringwald \endcode 662*1b2596b5SMatthias Ringwald * 663*1b2596b5SMatthias Ringwald * A variable for the received byte must be added: 664*1b2596b5SMatthias Ringwald * \code 665*1b2596b5SMatthias Ringwald uint32_t received_byte; 666*1b2596b5SMatthias Ringwald \endcode 667*1b2596b5SMatthias Ringwald * 668*1b2596b5SMatthias Ringwald * Add to application initialization: 669*1b2596b5SMatthias Ringwald * \code 670*1b2596b5SMatthias Ringwald sysclk_init(); 671*1b2596b5SMatthias Ringwald 672*1b2596b5SMatthias Ringwald board_init(); 673*1b2596b5SMatthias Ringwald 674*1b2596b5SMatthias Ringwald const sam_usart_opt_t usart_console_settings = { 675*1b2596b5SMatthias Ringwald USART_SERIAL_BAUDRATE, 676*1b2596b5SMatthias Ringwald USART_SERIAL_CHAR_LENGTH, 677*1b2596b5SMatthias Ringwald USART_SERIAL_PARITY, 678*1b2596b5SMatthias Ringwald USART_SERIAL_STOP_BIT, 679*1b2596b5SMatthias Ringwald US_MR_CHMODE_NORMAL 680*1b2596b5SMatthias Ringwald }; 681*1b2596b5SMatthias Ringwald 682*1b2596b5SMatthias Ringwald #if SAM4L 683*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL); 684*1b2596b5SMatthias Ringwald #else 685*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL_ID); 686*1b2596b5SMatthias Ringwald #endif 687*1b2596b5SMatthias Ringwald 688*1b2596b5SMatthias Ringwald usart_init_rs232(USART_SERIAL, &usart_console_settings, 689*1b2596b5SMatthias Ringwald sysclk_get_main_hz()); 690*1b2596b5SMatthias Ringwald usart_enable_tx(USART_SERIAL); 691*1b2596b5SMatthias Ringwald usart_enable_rx(USART_SERIAL); 692*1b2596b5SMatthias Ringwald 693*1b2596b5SMatthias Ringwald usart_enable_interrupt(USART_SERIAL, US_IER_RXRDY); 694*1b2596b5SMatthias Ringwald NVIC_EnableIRQ(USART_SERIAL_IRQ); 695*1b2596b5SMatthias Ringwald \endcode 696*1b2596b5SMatthias Ringwald * 697*1b2596b5SMatthias Ringwald * \subsection usart_use_case_2_setup_flow Workflow 698*1b2596b5SMatthias Ringwald * -# Initialize system clock: 699*1b2596b5SMatthias Ringwald * \code 700*1b2596b5SMatthias Ringwald sysclk_init(); 701*1b2596b5SMatthias Ringwald \endcode 702*1b2596b5SMatthias Ringwald * -# Configure the USART Tx and Rx pins by call the board init function: 703*1b2596b5SMatthias Ringwald * \code 704*1b2596b5SMatthias Ringwald board_init(); 705*1b2596b5SMatthias Ringwald \endcode 706*1b2596b5SMatthias Ringwald * \note Set the following define in conf_board.h file to enable COM port,it will be used in 707*1b2596b5SMatthias Ringwald * board_init() function to set up IOPorts for the USART pins. 708*1b2596b5SMatthias Ringwald * For SAM4L: 709*1b2596b5SMatthias Ringwald * \code 710*1b2596b5SMatthias Ringwald #define CONF_BOARD_COM_PORT 711*1b2596b5SMatthias Ringwald \endcode 712*1b2596b5SMatthias Ringwald * For other SAM devices: 713*1b2596b5SMatthias Ringwald * \code 714*1b2596b5SMatthias Ringwald #define CONF_BOARD_UART_CONSOLE 715*1b2596b5SMatthias Ringwald \endcode 716*1b2596b5SMatthias Ringwald * -# Create USART options struct: 717*1b2596b5SMatthias Ringwald * \code 718*1b2596b5SMatthias Ringwald const sam_usart_opt_t usart_console_settings = { 719*1b2596b5SMatthias Ringwald USART_SERIAL_BAUDRATE, 720*1b2596b5SMatthias Ringwald USART_SERIAL_CHAR_LENGTH, 721*1b2596b5SMatthias Ringwald USART_SERIAL_PARITY, 722*1b2596b5SMatthias Ringwald USART_SERIAL_STOP_BIT, 723*1b2596b5SMatthias Ringwald US_MR_CHMODE_NORMAL 724*1b2596b5SMatthias Ringwald }; 725*1b2596b5SMatthias Ringwald \endcode 726*1b2596b5SMatthias Ringwald * -# Enable the clock to the USART module: 727*1b2596b5SMatthias Ringwald * \code 728*1b2596b5SMatthias Ringwald #if SAM4L 729*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL); 730*1b2596b5SMatthias Ringwald #else 731*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(USART_SERIAL_ID); 732*1b2596b5SMatthias Ringwald #endif 733*1b2596b5SMatthias Ringwald \endcode 734*1b2596b5SMatthias Ringwald * -# Initialize the USART module in RS232 mode: 735*1b2596b5SMatthias Ringwald * \code 736*1b2596b5SMatthias Ringwald usart_init_rs232(USART_SERIAL, &usart_console_settings, 737*1b2596b5SMatthias Ringwald sysclk_get_main_hz()); 738*1b2596b5SMatthias Ringwald \endcode 739*1b2596b5SMatthias Ringwald * -# Enable the Rx and Tx modes of the USART module: 740*1b2596b5SMatthias Ringwald * \code 741*1b2596b5SMatthias Ringwald usart_enable_tx(USART_SERIAL); 742*1b2596b5SMatthias Ringwald usart_enable_rx(USART_SERIAL); 743*1b2596b5SMatthias Ringwald \endcode 744*1b2596b5SMatthias Ringwald * -# Enable the USART character reception interrupt, and general interrupts 745*1b2596b5SMatthias Ringwald * for the USART module. 746*1b2596b5SMatthias Ringwald * \code 747*1b2596b5SMatthias Ringwald usart_enable_interrupt(USART_SERIAL, US_IER_RXRDY); 748*1b2596b5SMatthias Ringwald NVIC_EnableIRQ(USART_SERIAL_IRQ); 749*1b2596b5SMatthias Ringwald \endcode 750*1b2596b5SMatthias Ringwald * \section usart_use_case_2_usage Usage steps 751*1b2596b5SMatthias Ringwald * 752*1b2596b5SMatthias Ringwald * \subsection usart_use_case_2_usage_code Example code 753*1b2596b5SMatthias Ringwald * Add to your main application C-file the USART interrupt handler: 754*1b2596b5SMatthias Ringwald * \code 755*1b2596b5SMatthias Ringwald void USART_SERIAL_ISR_HANDLER(void) 756*1b2596b5SMatthias Ringwald { 757*1b2596b5SMatthias Ringwald uint32_t dw_status = usart_get_status(USART_SERIAL); 758*1b2596b5SMatthias Ringwald 759*1b2596b5SMatthias Ringwald if (dw_status & US_CSR_RXRDY) { 760*1b2596b5SMatthias Ringwald uint32_t received_byte; 761*1b2596b5SMatthias Ringwald 762*1b2596b5SMatthias Ringwald usart_read(USART_SERIAL, &received_byte); 763*1b2596b5SMatthias Ringwald usart_write(USART_SERIAL, received_byte); 764*1b2596b5SMatthias Ringwald } 765*1b2596b5SMatthias Ringwald } 766*1b2596b5SMatthias Ringwald \endcode 767*1b2596b5SMatthias Ringwald * 768*1b2596b5SMatthias Ringwald * \subsection usart_use_case_2_usage_flow Workflow 769*1b2596b5SMatthias Ringwald * -# When the USART ISR fires, retrieve the USART module interrupt flags: 770*1b2596b5SMatthias Ringwald * \code uint32_t dw_status = usart_get_status(USART_SERIAL); \endcode 771*1b2596b5SMatthias Ringwald * -# Check if the USART Receive Character interrupt has fired: 772*1b2596b5SMatthias Ringwald * \code if (dw_status & US_CSR_RXRDY) \endcode 773*1b2596b5SMatthias Ringwald * -# If a character has been received, fetch it into a temporary variable: 774*1b2596b5SMatthias Ringwald * \code usart_read(USART_SERIAL, &received_byte); \endcode 775*1b2596b5SMatthias Ringwald * -# Echo the character back: 776*1b2596b5SMatthias Ringwald * \code usart_write(USART_SERIAL, received_byte); \endcode 777*1b2596b5SMatthias Ringwald */ 778*1b2596b5SMatthias Ringwald 779*1b2596b5SMatthias Ringwald #endif /* USART_H_INCLUDED */ 780