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