mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Child:
592:a274ee790e56
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

Full URL: https://github.com/mbedmicro/mbed/commit/d5b4d2ab9c47edb4dc5776e7177b0c2263459081/

Initial version of drivers for SAMR21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 579:53297373a894 1 /**
mbed_official 579:53297373a894 2 *
mbed_official 579:53297373a894 3 * \file
mbed_official 579:53297373a894 4 *
mbed_official 579:53297373a894 5 * \brief SAM SERCOM USART Driver
mbed_official 579:53297373a894 6 *
mbed_official 579:53297373a894 7 * Copyright (C) 2012-2014 Atmel Corporation. All rights reserved.
mbed_official 579:53297373a894 8 *
mbed_official 579:53297373a894 9 * \asf_license_start
mbed_official 579:53297373a894 10 *
mbed_official 579:53297373a894 11 * \page License
mbed_official 579:53297373a894 12 *
mbed_official 579:53297373a894 13 * Redistribution and use in source and binary forms, with or without
mbed_official 579:53297373a894 14 * modification, are permitted provided that the following conditions are met:
mbed_official 579:53297373a894 15 *
mbed_official 579:53297373a894 16 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 579:53297373a894 17 * this list of conditions and the following disclaimer.
mbed_official 579:53297373a894 18 *
mbed_official 579:53297373a894 19 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 579:53297373a894 20 * this list of conditions and the following disclaimer in the documentation
mbed_official 579:53297373a894 21 * and/or other materials provided with the distribution.
mbed_official 579:53297373a894 22 *
mbed_official 579:53297373a894 23 * 3. The name of Atmel may not be used to endorse or promote products derived
mbed_official 579:53297373a894 24 * from this software without specific prior written permission.
mbed_official 579:53297373a894 25 *
mbed_official 579:53297373a894 26 * 4. This software may only be redistributed and used in connection with an
mbed_official 579:53297373a894 27 * Atmel microcontroller product.
mbed_official 579:53297373a894 28 *
mbed_official 579:53297373a894 29 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
mbed_official 579:53297373a894 30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mbed_official 579:53297373a894 31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
mbed_official 579:53297373a894 32 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
mbed_official 579:53297373a894 33 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 579:53297373a894 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
mbed_official 579:53297373a894 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
mbed_official 579:53297373a894 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
mbed_official 579:53297373a894 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
mbed_official 579:53297373a894 38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 579:53297373a894 39 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 579:53297373a894 40 *
mbed_official 579:53297373a894 41 * \asf_license_stop
mbed_official 579:53297373a894 42 *
mbed_official 579:53297373a894 43 */
mbed_official 579:53297373a894 44 /**
mbed_official 579:53297373a894 45 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
mbed_official 579:53297373a894 46 */
mbed_official 579:53297373a894 47 #ifndef USART_H_INCLUDED
mbed_official 579:53297373a894 48 #define USART_H_INCLUDED
mbed_official 579:53297373a894 49
mbed_official 579:53297373a894 50 /**
mbed_official 579:53297373a894 51 * \defgroup asfdoc_sam0_sercom_usart_group SAM Serial USART Driver (SERCOM USART)
mbed_official 579:53297373a894 52 *
mbed_official 579:53297373a894 53 * This driver for Atmel庐 | SMART SAM devices provides an interface for the configuration
mbed_official 579:53297373a894 54 * and management of the SERCOM module in its USART mode to transfer or receive
mbed_official 579:53297373a894 55 * USART data frames. The following driver API modes are covered by this
mbed_official 579:53297373a894 56 * manual:
mbed_official 579:53297373a894 57 *
mbed_official 579:53297373a894 58 * - Polled APIs
mbed_official 579:53297373a894 59 * \if USART_CALLBACK_MODE
mbed_official 579:53297373a894 60 * - Callback APIs
mbed_official 579:53297373a894 61 * \endif
mbed_official 579:53297373a894 62 *
mbed_official 579:53297373a894 63 * The following peripherals are used by this module:
mbed_official 579:53297373a894 64 * - SERCOM (Serial Communication Interface)
mbed_official 579:53297373a894 65 *
mbed_official 579:53297373a894 66 * The following devices can use this module:
mbed_official 579:53297373a894 67 * - Atmel | SMART SAM D20/D21
mbed_official 579:53297373a894 68 * - Atmel | SMART SAM R21
mbed_official 579:53297373a894 69 * - Atmel | SMART SAM D10/D11
mbed_official 579:53297373a894 70 * - Atmel | SMART SAM L21
mbed_official 579:53297373a894 71 *
mbed_official 579:53297373a894 72 * The outline of this documentation is as follows:
mbed_official 579:53297373a894 73 * - \ref asfdoc_sam0_sercom_usart_prerequisites
mbed_official 579:53297373a894 74 * - \ref asfdoc_sam0_sercom_usart_overview
mbed_official 579:53297373a894 75 * - \ref asfdoc_sam0_sercom_usart_special_considerations
mbed_official 579:53297373a894 76 * - \ref asfdoc_sam0_sercom_usart_extra_info
mbed_official 579:53297373a894 77 * - \ref asfdoc_sam0_sercom_usart_examples
mbed_official 579:53297373a894 78 * - \ref asfdoc_sam0_sercom_usart_api_overview
mbed_official 579:53297373a894 79 *
mbed_official 579:53297373a894 80 * \section asfdoc_sam0_sercom_usart_prerequisites Prerequisites
mbed_official 579:53297373a894 81 *
mbed_official 579:53297373a894 82 * To use the USART you need to have a GCLK generator enabled and running
mbed_official 579:53297373a894 83 * that can be used as the SERCOM clock source. This can either be configured
mbed_official 579:53297373a894 84 * in conf_clocks.h or by using the system clock driver.
mbed_official 579:53297373a894 85 *
mbed_official 579:53297373a894 86 * \section asfdoc_sam0_sercom_usart_overview Module Overview
mbed_official 579:53297373a894 87 *
mbed_official 579:53297373a894 88 * This driver will use one (or more) SERCOM interfaces on the system
mbed_official 579:53297373a894 89 * and configure it to run as a USART interface in either synchronous
mbed_official 579:53297373a894 90 * or asynchronous mode.
mbed_official 579:53297373a894 91 *
mbed_official 579:53297373a894 92 * \subsection asfdoc_sam0_sercom_usart_features Driver Feature Macro Definition
mbed_official 579:53297373a894 93 * <table>
mbed_official 579:53297373a894 94 * <tr>
mbed_official 579:53297373a894 95 * <th>Driver Feature Macro</th>
mbed_official 579:53297373a894 96 * <th>Supported devices</th>
mbed_official 579:53297373a894 97 * </tr>
mbed_official 579:53297373a894 98 * <tr>
mbed_official 579:53297373a894 99 * <td>FEATURE_USART_SYNC_SCHEME_V2</td>
mbed_official 579:53297373a894 100 * <td>SAM D21/R21/D10/D11/L21</td>
mbed_official 579:53297373a894 101 * </tr>
mbed_official 579:53297373a894 102 * <tr>
mbed_official 579:53297373a894 103 * <td>FEATURE_USART_OVER_SAMPLE</td>
mbed_official 579:53297373a894 104 * <td>SAM D21/R21/D10/D11/L21</td>
mbed_official 579:53297373a894 105 * </tr>
mbed_official 579:53297373a894 106 * <tr>
mbed_official 579:53297373a894 107 * <td>FEATURE_USART_HARDWARE_FLOW_CONTROL</td>
mbed_official 579:53297373a894 108 * <td>SAM D21/R21/D10/D11/L21</td>
mbed_official 579:53297373a894 109 * </tr>
mbed_official 579:53297373a894 110 * <tr>
mbed_official 579:53297373a894 111 * <td>FEATURE_USART_IRDA</td>
mbed_official 579:53297373a894 112 * <td>SAM D21/R21/D10/D11/L21</td>
mbed_official 579:53297373a894 113 * </tr>
mbed_official 579:53297373a894 114 * <tr>
mbed_official 579:53297373a894 115 * <td>FEATURE_USART_LIN_SLAVE</td>
mbed_official 579:53297373a894 116 * <td>SAM D21/R21/D10/D11/L21</td>
mbed_official 579:53297373a894 117 * </tr>
mbed_official 579:53297373a894 118 * <tr>
mbed_official 579:53297373a894 119 * <td>FEATURE_USART_COLLISION_DECTION</td>
mbed_official 579:53297373a894 120 * <td>SAM D21/R21/D10/D11/L21</td>
mbed_official 579:53297373a894 121 * </tr>
mbed_official 579:53297373a894 122 * <tr>
mbed_official 579:53297373a894 123 * <td>FEATURE_USART_START_FRAME_DECTION</td>
mbed_official 579:53297373a894 124 * <td>SAM D21/R21/D10/D11/L21</td>
mbed_official 579:53297373a894 125 * </tr>
mbed_official 579:53297373a894 126 * <tr>
mbed_official 579:53297373a894 127 * <td>FEATURE_USART_IMMEDIATE_BUFFER_OVERFLOW_NOTIFICATION</td>
mbed_official 579:53297373a894 128 * <td>SAM D21/R21/D10/D11/L21</td>
mbed_official 579:53297373a894 129 * </tr>
mbed_official 579:53297373a894 130 * </table>
mbed_official 579:53297373a894 131 * \note The specific features are only available in the driver when the
mbed_official 579:53297373a894 132 * selected device supports those features.
mbed_official 579:53297373a894 133 *
mbed_official 579:53297373a894 134 * \subsection asfdoc_sam0_sercom_usart_overview_frame_format Frame Format
mbed_official 579:53297373a894 135 *
mbed_official 579:53297373a894 136 * Communication is based on frames, where the frame format can be customized
mbed_official 579:53297373a894 137 * to accommodate a wide range of standards. A frame consists of a start bit,
mbed_official 579:53297373a894 138 * a number of data bits, an optional parity bit for error detection as well
mbed_official 579:53297373a894 139 * as a configurable length stop bit(s) - see
mbed_official 579:53297373a894 140 * \ref asfdoc_sam0_sercom_usart_frame_diagram "the figure below".
mbed_official 579:53297373a894 141 * \ref asfdoc_sam0_sercom_usart_frame_params "The table below" shows the
mbed_official 579:53297373a894 142 * available parameters you can change in a frame.
mbed_official 579:53297373a894 143 *
mbed_official 579:53297373a894 144 * \anchor asfdoc_sam0_sercom_usart_frame_params
mbed_official 579:53297373a894 145 * <table>
mbed_official 579:53297373a894 146 * <caption>USART Frame Parameters</caption>
mbed_official 579:53297373a894 147 * <tr>
mbed_official 579:53297373a894 148 * <th>Parameter</th>
mbed_official 579:53297373a894 149 * <th>Options</th>
mbed_official 579:53297373a894 150 * </tr>
mbed_official 579:53297373a894 151 * <tr>
mbed_official 579:53297373a894 152 * <td>Start bit</td>
mbed_official 579:53297373a894 153 * <td>1</td>
mbed_official 579:53297373a894 154 * </tr>
mbed_official 579:53297373a894 155 * <tr>
mbed_official 579:53297373a894 156 * <td>Data bits</td>
mbed_official 579:53297373a894 157 * <td>5, 6, 7, 8, 9</td>
mbed_official 579:53297373a894 158 * </tr>
mbed_official 579:53297373a894 159 * <tr>
mbed_official 579:53297373a894 160 * <td>Parity bit</td>
mbed_official 579:53297373a894 161 * <td>None, Even, Odd</td>
mbed_official 579:53297373a894 162 * </tr>
mbed_official 579:53297373a894 163 * <tr>
mbed_official 579:53297373a894 164 * <td>Stop bits</td>
mbed_official 579:53297373a894 165 * <td>1, 2</td>
mbed_official 579:53297373a894 166 * </tr>
mbed_official 579:53297373a894 167 * </table>
mbed_official 579:53297373a894 168 *
mbed_official 579:53297373a894 169 * \anchor asfdoc_sam0_sercom_usart_frame_diagram
mbed_official 579:53297373a894 170 * \image html usart_frame.svg "USART Frame Overview" width=100%
mbed_official 579:53297373a894 171 *
mbed_official 579:53297373a894 172 * \subsection asfdoc_sam0_sercom_usart_overview_sync Synchronous Mode
mbed_official 579:53297373a894 173 *
mbed_official 579:53297373a894 174 * In synchronous mode a dedicated clock line is provided; either by the USART
mbed_official 579:53297373a894 175 * itself if in master mode, or by an external master if in slave mode.
mbed_official 579:53297373a894 176 * Maximum transmission speed is the same as the GCLK clocking the USART
mbed_official 579:53297373a894 177 * peripheral when in slave mode, and the GCLK divided by two if in
mbed_official 579:53297373a894 178 * master mode. In synchronous mode the interface needs three lines to
mbed_official 579:53297373a894 179 * communicate:
mbed_official 579:53297373a894 180 * - TX (Transmit pin)
mbed_official 579:53297373a894 181 * - RX (Receive pin)
mbed_official 579:53297373a894 182 * - XCK (Clock pin)
mbed_official 579:53297373a894 183 *
mbed_official 579:53297373a894 184 * \subsubsection asfdoc_sam0_sercom_usart_overview_sync_sampling Data Sampling
mbed_official 579:53297373a894 185 * In synchronous mode the data is sampled on either the rising or falling edge
mbed_official 579:53297373a894 186 * of the clock signal. This is configured by setting the clock polarity in the
mbed_official 579:53297373a894 187 * configuration struct.
mbed_official 579:53297373a894 188 *
mbed_official 579:53297373a894 189 * \subsection asfdoc_sam0_sercom_usart_overview_async Asynchronous Mode
mbed_official 579:53297373a894 190 *
mbed_official 579:53297373a894 191 * In asynchronous mode no dedicated clock line is used, and the communication
mbed_official 579:53297373a894 192 * is based on matching the clock speed on the transmitter and receiver. The
mbed_official 579:53297373a894 193 * clock is generated from the internal SERCOM baudrate generator, and the
mbed_official 579:53297373a894 194 * frames are synchronized by using the frame start bits. Maximum transmission
mbed_official 579:53297373a894 195 * speed is limited to the SERCOM GCLK divided by 16.
mbed_official 579:53297373a894 196 * In asynchronous mode the interface only needs two lines to communicate:
mbed_official 579:53297373a894 197 * - TX (Transmit pin)
mbed_official 579:53297373a894 198 * - RX (Receive pin)
mbed_official 579:53297373a894 199 *
mbed_official 579:53297373a894 200 * \subsubsection asfdoc_sam0_sercom_usart_overview_async_clock_matching Transmitter/receiver Clock Matching
mbed_official 579:53297373a894 201 *
mbed_official 579:53297373a894 202 * For successful transmit and receive using the asynchronous mode the receiver
mbed_official 579:53297373a894 203 * and transmitter clocks needs to be closely matched. When receiving a frame
mbed_official 579:53297373a894 204 * that does not match the selected baudrate closely enough the receiver will
mbed_official 579:53297373a894 205 * be unable to synchronize the frame(s), and garbage transmissions will
mbed_official 579:53297373a894 206 * result.
mbed_official 579:53297373a894 207 *
mbed_official 579:53297373a894 208 * \subsection asfdoc_sam0_sercom_usart_parity Parity
mbed_official 579:53297373a894 209 * Parity can be enabled to detect if a transmission was in error. This is done
mbed_official 579:53297373a894 210 * by counting the number of "1" bits in the frame. When using Even parity the
mbed_official 579:53297373a894 211 * parity bit will be set if the total number of "1"s in the frame are an even
mbed_official 579:53297373a894 212 * number. If using Odd parity the parity bit will be set if the total number
mbed_official 579:53297373a894 213 * of "1"s are Odd.
mbed_official 579:53297373a894 214 *
mbed_official 579:53297373a894 215 * When receiving a character the receiver will count the number of "1"s in the
mbed_official 579:53297373a894 216 * frame and give an error if the received frame and parity bit disagree.
mbed_official 579:53297373a894 217 *
mbed_official 579:53297373a894 218 * \subsection asfdoc_sam0_sercom_usart_overview_pin_configuration GPIO Configuration
mbed_official 579:53297373a894 219 *
mbed_official 579:53297373a894 220 * The SERCOM module has four internal pads; the RX pin can be placed freely on
mbed_official 579:53297373a894 221 * any one of the four pads, and the TX and XCK pins have two predefined
mbed_official 579:53297373a894 222 * positions that can be selected as a pair. The pads can then be routed to an
mbed_official 579:53297373a894 223 * external GPIO pin using the normal pin multiplexing scheme on the SAM.
mbed_official 579:53297373a894 224 *
mbed_official 579:53297373a894 225 * \section asfdoc_sam0_sercom_usart_special_considerations Special Considerations
mbed_official 579:53297373a894 226 *
mbed_official 579:53297373a894 227 * \if USART_CALLBACK_MODE
mbed_official 579:53297373a894 228 * Never execute large portions of code in the callbacks. These
mbed_official 579:53297373a894 229 * are run from the interrupt routine, and thus having long callbacks will
mbed_official 579:53297373a894 230 * keep the processor in the interrupt handler for an equally long time.
mbed_official 579:53297373a894 231 * A common way to handle this is to use global flags signaling the
mbed_official 579:53297373a894 232 * main application that an interrupt event has happened, and only do the
mbed_official 579:53297373a894 233 * minimal needed processing in the callback.
mbed_official 579:53297373a894 234 * \else
mbed_official 579:53297373a894 235 * No special considerations.
mbed_official 579:53297373a894 236 * \endif
mbed_official 579:53297373a894 237 *
mbed_official 579:53297373a894 238 * \section asfdoc_sam0_sercom_usart_extra_info Extra Information
mbed_official 579:53297373a894 239 *
mbed_official 579:53297373a894 240 * For extra information, see \ref asfdoc_sam0_sercom_usart_extra. This includes:
mbed_official 579:53297373a894 241 * - \ref asfdoc_sam0_sercom_usart_extra_acronyms
mbed_official 579:53297373a894 242 * - \ref asfdoc_sam0_sercom_usart_extra_dependencies
mbed_official 579:53297373a894 243 * - \ref asfdoc_sam0_sercom_usart_extra_errata
mbed_official 579:53297373a894 244 * - \ref asfdoc_sam0_sercom_usart_extra_history
mbed_official 579:53297373a894 245 *
mbed_official 579:53297373a894 246 * \section asfdoc_sam0_sercom_usart_examples Examples
mbed_official 579:53297373a894 247 *
mbed_official 579:53297373a894 248 * For a list of examples related to this driver, see
mbed_official 579:53297373a894 249 * \ref asfdoc_sam0_sercom_usart_exqsg.
mbed_official 579:53297373a894 250 *
mbed_official 579:53297373a894 251 * \section asfdoc_sam0_sercom_usart_api_overview API Overview
mbed_official 579:53297373a894 252 * @{
mbed_official 579:53297373a894 253 */
mbed_official 579:53297373a894 254
mbed_official 579:53297373a894 255 #include <compiler.h>
mbed_official 579:53297373a894 256 #include <sercom.h>
mbed_official 579:53297373a894 257 #include <pinmux.h>
mbed_official 579:53297373a894 258
mbed_official 579:53297373a894 259 #define USART_CALLBACK_MODE true
mbed_official 579:53297373a894 260
mbed_official 579:53297373a894 261 #if USART_CALLBACK_MODE == true
mbed_official 579:53297373a894 262 # include <sercom_interrupt.h>
mbed_official 579:53297373a894 263 #endif
mbed_official 579:53297373a894 264
mbed_official 579:53297373a894 265 #ifdef __cplusplus
mbed_official 579:53297373a894 266 extern "C" {
mbed_official 579:53297373a894 267 #endif
mbed_official 579:53297373a894 268
mbed_official 579:53297373a894 269 /**
mbed_official 579:53297373a894 270 * \name Driver Feature Definition
mbed_official 579:53297373a894 271 * Define SERCOM USART features set according to different device family.
mbed_official 579:53297373a894 272 * @{
mbed_official 579:53297373a894 273 */
mbed_official 579:53297373a894 274 #if (SAMD21) || (SAMR21) || (SAMD10) || (SAMD11) || (SAML21) || defined(__DOXYGEN__)
mbed_official 579:53297373a894 275 /** Usart sync scheme version 2. */
mbed_official 579:53297373a894 276 # define FEATURE_USART_SYNC_SCHEME_V2
mbed_official 579:53297373a894 277 /** Usart over sampling. */
mbed_official 579:53297373a894 278 # define FEATURE_USART_OVER_SAMPLE
mbed_official 579:53297373a894 279 /** Usart hardware control flow. */
mbed_official 579:53297373a894 280 # define FEATURE_USART_HARDWARE_FLOW_CONTROL
mbed_official 579:53297373a894 281 /** IrDA mode. */
mbed_official 579:53297373a894 282 # define FEATURE_USART_IRDA
mbed_official 579:53297373a894 283 /** LIN slave mode. */
mbed_official 579:53297373a894 284 # define FEATURE_USART_LIN_SLAVE
mbed_official 579:53297373a894 285 /** Usart collision detection. */
mbed_official 579:53297373a894 286 # define FEATURE_USART_COLLISION_DECTION
mbed_official 579:53297373a894 287 /** Usart start frame detection. */
mbed_official 579:53297373a894 288 # define FEATURE_USART_START_FRAME_DECTION
mbed_official 579:53297373a894 289 /** Usart start buffer overflow notification. */
mbed_official 579:53297373a894 290 # define FEATURE_USART_IMMEDIATE_BUFFER_OVERFLOW_NOTIFICATION
mbed_official 579:53297373a894 291 #endif
mbed_official 579:53297373a894 292 /*@}*/
mbed_official 579:53297373a894 293
mbed_official 579:53297373a894 294 #ifndef PINMUX_DEFAULT
mbed_official 579:53297373a894 295 /** Default pinmux. */
mbed_official 579:53297373a894 296 # define PINMUX_DEFAULT 0
mbed_official 579:53297373a894 297 #endif
mbed_official 579:53297373a894 298
mbed_official 579:53297373a894 299 #ifndef PINMUX_UNUSED
mbed_official 579:53297373a894 300 /** Unused pinmux. */
mbed_official 579:53297373a894 301 # define PINMUX_UNUSED 0xFFFFFFFF
mbed_official 579:53297373a894 302 #endif
mbed_official 579:53297373a894 303
mbed_official 579:53297373a894 304 #ifndef USART_TIMEOUT
mbed_official 579:53297373a894 305 /** USART timeout value. */
mbed_official 579:53297373a894 306 # define USART_TIMEOUT 0xFFFF
mbed_official 579:53297373a894 307 #endif
mbed_official 579:53297373a894 308
mbed_official 579:53297373a894 309 #if USART_CALLBACK_MODE == true
mbed_official 579:53297373a894 310 /**
mbed_official 579:53297373a894 311 * \brief USART Callback enum
mbed_official 579:53297373a894 312 *
mbed_official 579:53297373a894 313 * Callbacks for the Asynchronous USART driver.
mbed_official 579:53297373a894 314 */
mbed_official 579:53297373a894 315 enum usart_callback {
mbed_official 579:53297373a894 316 /** Callback for buffer transmitted. */
mbed_official 579:53297373a894 317 USART_CALLBACK_BUFFER_TRANSMITTED,
mbed_official 579:53297373a894 318 /** Callback for buffer received. */
mbed_official 579:53297373a894 319 USART_CALLBACK_BUFFER_RECEIVED,
mbed_official 579:53297373a894 320 /** Callback for error. */
mbed_official 579:53297373a894 321 USART_CALLBACK_ERROR,
mbed_official 579:53297373a894 322 #ifdef FEATURE_USART_LIN_SLAVE
mbed_official 579:53297373a894 323 /** Callback for break character is received. */
mbed_official 579:53297373a894 324 USART_CALLBACK_BREAK_RECEIVED,
mbed_official 579:53297373a894 325 #endif
mbed_official 579:53297373a894 326 #ifdef FEATURE_USART_HARDWARE_FLOW_CONTROL
mbed_official 579:53297373a894 327 /** Callback for a change is detected on the CTS pin. */
mbed_official 579:53297373a894 328 USART_CALLBACK_CTS_INPUT_CHANGE,
mbed_official 579:53297373a894 329 #endif
mbed_official 579:53297373a894 330 #ifdef FEATURE_USART_START_FRAME_DECTION
mbed_official 579:53297373a894 331 /** Callback for a start condition is detected on the RxD line. */
mbed_official 579:53297373a894 332 USART_CALLBACK_START_RECEIVED,
mbed_official 579:53297373a894 333 #endif
mbed_official 579:53297373a894 334 # if !defined(__DOXYGEN__)
mbed_official 579:53297373a894 335 /** Number of available callbacks. */
mbed_official 579:53297373a894 336 USART_CALLBACK_N,
mbed_official 579:53297373a894 337 # endif
mbed_official 579:53297373a894 338 };
mbed_official 579:53297373a894 339 #endif
mbed_official 579:53297373a894 340
mbed_official 579:53297373a894 341 /**
mbed_official 579:53297373a894 342 * \brief USART Data Order enum
mbed_official 579:53297373a894 343 *
mbed_official 579:53297373a894 344 * The data order decides which of MSB or LSB is shifted out first when data is
mbed_official 579:53297373a894 345 * transferred.
mbed_official 579:53297373a894 346 */
mbed_official 579:53297373a894 347 enum usart_dataorder {
mbed_official 579:53297373a894 348 /** The MSB will be shifted out first during transmission,
mbed_official 579:53297373a894 349 * and shifted in first during reception. */
mbed_official 579:53297373a894 350 USART_DATAORDER_MSB = 0,
mbed_official 579:53297373a894 351 /** The LSB will be shifted out first during transmission,
mbed_official 579:53297373a894 352 * and shifted in first during reception. */
mbed_official 579:53297373a894 353 USART_DATAORDER_LSB = SERCOM_USART_CTRLA_DORD,
mbed_official 579:53297373a894 354 };
mbed_official 579:53297373a894 355
mbed_official 579:53297373a894 356 /**
mbed_official 579:53297373a894 357 * \brief USART Transfer mode enum
mbed_official 579:53297373a894 358 *
mbed_official 579:53297373a894 359 * Select USART transfer mode.
mbed_official 579:53297373a894 360 */
mbed_official 579:53297373a894 361 enum usart_transfer_mode {
mbed_official 579:53297373a894 362 /** Transfer of data is done synchronously. */
mbed_official 579:53297373a894 363 USART_TRANSFER_SYNCHRONOUSLY = (SERCOM_USART_CTRLA_CMODE),
mbed_official 579:53297373a894 364 /** Transfer of data is done asynchronously. */
mbed_official 579:53297373a894 365 USART_TRANSFER_ASYNCHRONOUSLY = 0
mbed_official 579:53297373a894 366 };
mbed_official 579:53297373a894 367
mbed_official 579:53297373a894 368 /**
mbed_official 579:53297373a894 369 * \brief USART Parity enum
mbed_official 579:53297373a894 370 *
mbed_official 579:53297373a894 371 * Select parity USART parity mode.
mbed_official 579:53297373a894 372 */
mbed_official 579:53297373a894 373 enum usart_parity {
mbed_official 579:53297373a894 374 /** For odd parity checking, the parity bit will be set if number of
mbed_official 579:53297373a894 375 * ones being transferred is even. */
mbed_official 579:53297373a894 376 USART_PARITY_ODD = SERCOM_USART_CTRLB_PMODE,
mbed_official 579:53297373a894 377
mbed_official 579:53297373a894 378 /** For even parity checking, the parity bit will be set if number of
mbed_official 579:53297373a894 379 * ones being received is odd. */
mbed_official 579:53297373a894 380 USART_PARITY_EVEN = 0,
mbed_official 579:53297373a894 381
mbed_official 579:53297373a894 382 /** No parity checking will be executed, and there will be no parity bit
mbed_official 579:53297373a894 383 * in the received frame. */
mbed_official 579:53297373a894 384 USART_PARITY_NONE = 0xFF,
mbed_official 579:53297373a894 385 };
mbed_official 579:53297373a894 386
mbed_official 579:53297373a894 387 /**
mbed_official 579:53297373a894 388 * \brief USART signal MUX settings
mbed_official 579:53297373a894 389 *
mbed_official 579:53297373a894 390 * Set the functionality of the SERCOM pins.
mbed_official 579:53297373a894 391 *
mbed_official 579:53297373a894 392 * See \ref asfdoc_sam0_sercom_usart_mux_settings for a description of the
mbed_official 579:53297373a894 393 * various MUX setting options.
mbed_official 579:53297373a894 394 */
mbed_official 579:53297373a894 395 enum usart_signal_mux_settings {
mbed_official 579:53297373a894 396 #ifdef FEATURE_USART_HARDWARE_FLOW_CONTROL
mbed_official 579:53297373a894 397 /** MUX setting RX_0_TX_0_XCK_1. */
mbed_official 579:53297373a894 398 USART_RX_0_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO(0)),
mbed_official 579:53297373a894 399 /** MUX setting RX_0_TX_2_XCK_3. */
mbed_official 579:53297373a894 400 USART_RX_0_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO(1)),
mbed_official 579:53297373a894 401 /** MUX setting USART_RX_0_TX_0_RTS_2_CTS_3. */
mbed_official 579:53297373a894 402 USART_RX_0_TX_0_RTS_2_CTS_3 = (SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO(2)),
mbed_official 579:53297373a894 403 /** MUX setting RX_1_TX_0_XCK_1. */
mbed_official 579:53297373a894 404 USART_RX_1_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO(0)),
mbed_official 579:53297373a894 405 /** MUX setting RX_1_TX_2_XCK_3. */
mbed_official 579:53297373a894 406 USART_RX_1_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO(1)),
mbed_official 579:53297373a894 407 /** MUX setting USART_RX_1_TX_0_RTS_2_CTS_3. */
mbed_official 579:53297373a894 408 USART_RX_1_TX_0_RTS_2_CTS_3 = (SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO(2)),
mbed_official 579:53297373a894 409 /** MUX setting RX_2_TX_0_XCK_1. */
mbed_official 579:53297373a894 410 USART_RX_2_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(2) | SERCOM_USART_CTRLA_TXPO(0)),
mbed_official 579:53297373a894 411 /** MUX setting RX_2_TX_2_XCK_3. */
mbed_official 579:53297373a894 412 USART_RX_2_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(2) | SERCOM_USART_CTRLA_TXPO(1)),
mbed_official 579:53297373a894 413 /** MUX setting USART_RX_2_TX_0_RTS_2_CTS_3. */
mbed_official 579:53297373a894 414 USART_RX_2_TX_0_RTS_2_CTS_3 = (SERCOM_USART_CTRLA_RXPO(2) | SERCOM_USART_CTRLA_TXPO(2)),
mbed_official 579:53297373a894 415 /** MUX setting RX_3_TX_0_XCK_1. */
mbed_official 579:53297373a894 416 USART_RX_3_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(0)),
mbed_official 579:53297373a894 417 /** MUX setting RX_3_TX_2_XCK_3. */
mbed_official 579:53297373a894 418 USART_RX_3_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(1)),
mbed_official 579:53297373a894 419 /** MUX setting USART_RX_3_TX_0_RTS_2_CTS_3. */
mbed_official 579:53297373a894 420 USART_RX_3_TX_0_RTS_2_CTS_3 = (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(2)),
mbed_official 579:53297373a894 421 #else
mbed_official 579:53297373a894 422 /** MUX setting RX_0_TX_0_XCK_1. */
mbed_official 579:53297373a894 423 USART_RX_0_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(0)),
mbed_official 579:53297373a894 424 /** MUX setting RX_0_TX_2_XCK_3. */
mbed_official 579:53297373a894 425 USART_RX_0_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO),
mbed_official 579:53297373a894 426 /** MUX setting RX_1_TX_0_XCK_1. */
mbed_official 579:53297373a894 427 USART_RX_1_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(1)),
mbed_official 579:53297373a894 428 /** MUX setting RX_1_TX_2_XCK_3. */
mbed_official 579:53297373a894 429 USART_RX_1_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO),
mbed_official 579:53297373a894 430 /** MUX setting RX_2_TX_0_XCK_1. */
mbed_official 579:53297373a894 431 USART_RX_2_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(2)),
mbed_official 579:53297373a894 432 /** MUX setting RX_2_TX_2_XCK_3. */
mbed_official 579:53297373a894 433 USART_RX_2_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(2) | SERCOM_USART_CTRLA_TXPO),
mbed_official 579:53297373a894 434 /** MUX setting RX_3_TX_0_XCK_1. */
mbed_official 579:53297373a894 435 USART_RX_3_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(3)),
mbed_official 579:53297373a894 436 /** MUX setting RX_3_TX_2_XCK_3. */
mbed_official 579:53297373a894 437 USART_RX_3_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO),
mbed_official 579:53297373a894 438 #endif
mbed_official 579:53297373a894 439 };
mbed_official 579:53297373a894 440
mbed_official 579:53297373a894 441 /**
mbed_official 579:53297373a894 442 * \brief USART Stop Bits enum
mbed_official 579:53297373a894 443 *
mbed_official 579:53297373a894 444 * Number of stop bits for a frame.
mbed_official 579:53297373a894 445 */
mbed_official 579:53297373a894 446 enum usart_stopbits {
mbed_official 579:53297373a894 447 /** Each transferred frame contains one stop bit. */
mbed_official 579:53297373a894 448 USART_STOPBITS_1 = 0,
mbed_official 579:53297373a894 449 /** Each transferred frame contains two stop bits. */
mbed_official 579:53297373a894 450 USART_STOPBITS_2 = SERCOM_USART_CTRLB_SBMODE,
mbed_official 579:53297373a894 451 };
mbed_official 579:53297373a894 452
mbed_official 579:53297373a894 453 /**
mbed_official 579:53297373a894 454 * \brief USART Character Size
mbed_official 579:53297373a894 455 *
mbed_official 579:53297373a894 456 * Number of bits for the character sent in a frame.
mbed_official 579:53297373a894 457 */
mbed_official 579:53297373a894 458 enum usart_character_size {
mbed_official 579:53297373a894 459 /** The char being sent in a frame is five bits long. */
mbed_official 579:53297373a894 460 USART_CHARACTER_SIZE_5BIT = SERCOM_USART_CTRLB_CHSIZE(5),
mbed_official 579:53297373a894 461 /** The char being sent in a frame is six bits long. */
mbed_official 579:53297373a894 462 USART_CHARACTER_SIZE_6BIT = SERCOM_USART_CTRLB_CHSIZE(6),
mbed_official 579:53297373a894 463 /** The char being sent in a frame is seven bits long. */
mbed_official 579:53297373a894 464 USART_CHARACTER_SIZE_7BIT = SERCOM_USART_CTRLB_CHSIZE(7),
mbed_official 579:53297373a894 465 /** The char being sent in a frame is eight bits long. */
mbed_official 579:53297373a894 466 USART_CHARACTER_SIZE_8BIT = SERCOM_USART_CTRLB_CHSIZE(0),
mbed_official 579:53297373a894 467 /** The char being sent in a frame is nine bits long. */
mbed_official 579:53297373a894 468 USART_CHARACTER_SIZE_9BIT = SERCOM_USART_CTRLB_CHSIZE(1),
mbed_official 579:53297373a894 469 };
mbed_official 579:53297373a894 470
mbed_official 579:53297373a894 471 #ifdef FEATURE_USART_OVER_SAMPLE
mbed_official 579:53297373a894 472 /**
mbed_official 579:53297373a894 473 * \brief USART Sample Rate
mbed_official 579:53297373a894 474 *
mbed_official 579:53297373a894 475 * The value of sample rate and baudrate generation mode.
mbed_official 579:53297373a894 476 */
mbed_official 579:53297373a894 477 enum usart_sample_rate {
mbed_official 579:53297373a894 478 /** 16x over-sampling using arithmetic baudrate generation. */
mbed_official 579:53297373a894 479 USART_SAMPLE_RATE_16X_ARITHMETIC = SERCOM_USART_CTRLA_SAMPR(0),
mbed_official 579:53297373a894 480 /** 16x over-sampling using fractional baudrate generation. */
mbed_official 579:53297373a894 481 USART_SAMPLE_RATE_16X_FRACTIONAL = SERCOM_USART_CTRLA_SAMPR(1),
mbed_official 579:53297373a894 482 /** 8x over-sampling using arithmetic baudrate generation. */
mbed_official 579:53297373a894 483 USART_SAMPLE_RATE_8X_ARITHMETIC = SERCOM_USART_CTRLA_SAMPR(2),
mbed_official 579:53297373a894 484 /** 8x over-sampling using fractional baudrate generation. */
mbed_official 579:53297373a894 485 USART_SAMPLE_RATE_8X_FRACTIONAL = SERCOM_USART_CTRLA_SAMPR(3),
mbed_official 579:53297373a894 486 /** 3x over-sampling using arithmetic baudrate generation. */
mbed_official 579:53297373a894 487 USART_SAMPLE_RATE_3X_ARITHMETIC = SERCOM_USART_CTRLA_SAMPR(4),
mbed_official 579:53297373a894 488 };
mbed_official 579:53297373a894 489
mbed_official 579:53297373a894 490 /**
mbed_official 579:53297373a894 491 * \brief USART Sample Adjustment
mbed_official 579:53297373a894 492 *
mbed_official 579:53297373a894 493 * The value of sample number used for majority voting.
mbed_official 579:53297373a894 494 */
mbed_official 579:53297373a894 495 enum usart_sample_adjustment {
mbed_official 579:53297373a894 496 /** The first, middle and last sample number used for majority voting is 7-8-9. */
mbed_official 579:53297373a894 497 USART_SAMPLE_ADJUSTMENT_7_8_9 = SERCOM_USART_CTRLA_SAMPA(0),
mbed_official 579:53297373a894 498 /** The first, middle and last sample number used for majority voting is 9-10-11. */
mbed_official 579:53297373a894 499 USART_SAMPLE_ADJUSTMENT_9_10_11 = SERCOM_USART_CTRLA_SAMPA(1),
mbed_official 579:53297373a894 500 /** The first, middle and last sample number used for majority voting is 11-12-13. */
mbed_official 579:53297373a894 501 USART_SAMPLE_ADJUSTMENT_11_12_13 = SERCOM_USART_CTRLA_SAMPA(2),
mbed_official 579:53297373a894 502 /** The first, middle and last sample number used for majority voting is 13-14-15. */
mbed_official 579:53297373a894 503 USART_SAMPLE_ADJUSTMENT_13_14_15 = SERCOM_USART_CTRLA_SAMPA(3),
mbed_official 579:53297373a894 504 };
mbed_official 579:53297373a894 505 #endif
mbed_official 579:53297373a894 506
mbed_official 579:53297373a894 507 /**
mbed_official 579:53297373a894 508 * \brief USART Transceiver
mbed_official 579:53297373a894 509 *
mbed_official 579:53297373a894 510 * Select Receiver or Transmitter.
mbed_official 579:53297373a894 511 */
mbed_official 579:53297373a894 512 enum usart_transceiver_type {
mbed_official 579:53297373a894 513 /** The parameter is for the Receiver. */
mbed_official 579:53297373a894 514 USART_TRANSCEIVER_RX,
mbed_official 579:53297373a894 515 /** The parameter is for the Transmitter. */
mbed_official 579:53297373a894 516 USART_TRANSCEIVER_TX,
mbed_official 579:53297373a894 517 };
mbed_official 579:53297373a894 518
mbed_official 579:53297373a894 519 /**
mbed_official 579:53297373a894 520 * \brief USART configuration struct
mbed_official 579:53297373a894 521 *
mbed_official 579:53297373a894 522 * Configuration options for USART.
mbed_official 579:53297373a894 523 */
mbed_official 579:53297373a894 524 struct usart_config {
mbed_official 579:53297373a894 525 /** USART bit order (MSB or LSB first). */
mbed_official 579:53297373a894 526 enum usart_dataorder data_order;
mbed_official 579:53297373a894 527 /** USART in asynchronous or synchronous mode. */
mbed_official 579:53297373a894 528 enum usart_transfer_mode transfer_mode;
mbed_official 579:53297373a894 529 /** USART parity. */
mbed_official 579:53297373a894 530 enum usart_parity parity;
mbed_official 579:53297373a894 531 /** Number of stop bits. */
mbed_official 579:53297373a894 532 enum usart_stopbits stopbits;
mbed_official 579:53297373a894 533 /** USART character size. */
mbed_official 579:53297373a894 534 enum usart_character_size character_size;
mbed_official 579:53297373a894 535 /** USART pin out. */
mbed_official 579:53297373a894 536 enum usart_signal_mux_settings mux_setting;
mbed_official 579:53297373a894 537 #ifdef FEATURE_USART_OVER_SAMPLE
mbed_official 579:53297373a894 538 /** USART sample rate. */
mbed_official 579:53297373a894 539 enum usart_sample_rate sample_rate;
mbed_official 579:53297373a894 540 /** USART sample adjustment. */
mbed_official 579:53297373a894 541 enum usart_sample_adjustment sample_adjustment;
mbed_official 579:53297373a894 542 #endif
mbed_official 579:53297373a894 543 #ifdef FEATURE_USART_IMMEDIATE_BUFFER_OVERFLOW_NOTIFICATION
mbed_official 579:53297373a894 544 /** Controls when the buffer overflow status bit is asserted when a buffer overflow occurs.*/
mbed_official 579:53297373a894 545 bool immediate_buffer_overflow_notification;
mbed_official 579:53297373a894 546 #endif
mbed_official 579:53297373a894 547 #ifdef FEATURE_USART_IRDA
mbed_official 579:53297373a894 548 /** Enable IrDA encoding format. */
mbed_official 579:53297373a894 549 bool encoding_format_enable;
mbed_official 579:53297373a894 550 /** The minimum pulse length that is required for a pulse to be accepted by the IrDA receiver. */
mbed_official 579:53297373a894 551 uint8_t receive_pulse_length;
mbed_official 579:53297373a894 552 #endif
mbed_official 579:53297373a894 553 #ifdef FEATURE_USART_LIN_SLAVE
mbed_official 579:53297373a894 554 /** Enable LIN Slave Support. */
mbed_official 579:53297373a894 555 bool lin_slave_enable;
mbed_official 579:53297373a894 556 #endif
mbed_official 579:53297373a894 557 #ifdef FEATURE_USART_START_FRAME_DECTION
mbed_official 579:53297373a894 558 /** Enable start of frame dection. */
mbed_official 579:53297373a894 559 bool start_frame_detection_enable;
mbed_official 579:53297373a894 560 #endif
mbed_official 579:53297373a894 561 #ifdef FEATURE_USART_COLLISION_DECTION
mbed_official 579:53297373a894 562 /** Enable collision dection. */
mbed_official 579:53297373a894 563 bool collision_detection_enable;
mbed_official 579:53297373a894 564 #endif
mbed_official 579:53297373a894 565 /** USART baudrate. */
mbed_official 579:53297373a894 566 uint32_t baudrate;
mbed_official 579:53297373a894 567 /** Enable receiver. */
mbed_official 579:53297373a894 568 bool receiver_enable;
mbed_official 579:53297373a894 569 /** Enable transmitter. */
mbed_official 579:53297373a894 570 bool transmitter_enable;
mbed_official 579:53297373a894 571
mbed_official 579:53297373a894 572 /** USART Clock Polarity.
mbed_official 579:53297373a894 573 * If true, data changes on falling XCK edge and
mbed_official 579:53297373a894 574 * is sampled at rising edge.
mbed_official 579:53297373a894 575 * If false, data changes on rising XCK edge and
mbed_official 579:53297373a894 576 * is sampled at falling edge.
mbed_official 579:53297373a894 577 * */
mbed_official 579:53297373a894 578 bool clock_polarity_inverted;
mbed_official 579:53297373a894 579
mbed_official 579:53297373a894 580 /** States whether to use the external clock applied to the XCK pin.
mbed_official 579:53297373a894 581 * In synchronous mode the shift register will act directly on the XCK clock.
mbed_official 579:53297373a894 582 * In asynchronous mode the XCK will be the input to the USART hardware module.
mbed_official 579:53297373a894 583 */
mbed_official 579:53297373a894 584 bool use_external_clock;
mbed_official 579:53297373a894 585 /** External clock frequency in synchronous mode.
mbed_official 579:53297373a894 586 * This must be set if \c use_external_clock is true. */
mbed_official 579:53297373a894 587 uint32_t ext_clock_freq;
mbed_official 579:53297373a894 588 /** If true the USART will be kept running in Standby sleep mode. */
mbed_official 579:53297373a894 589 bool run_in_standby;
mbed_official 579:53297373a894 590 /** GCLK generator source. */
mbed_official 579:53297373a894 591 enum gclk_generator generator_source;
mbed_official 579:53297373a894 592 /** PAD0 pinmux. */
mbed_official 579:53297373a894 593 uint32_t pinmux_pad0;
mbed_official 579:53297373a894 594 /** PAD1 pinmux. */
mbed_official 579:53297373a894 595 uint32_t pinmux_pad1;
mbed_official 579:53297373a894 596 /** PAD2 pinmux. */
mbed_official 579:53297373a894 597 uint32_t pinmux_pad2;
mbed_official 579:53297373a894 598 /** PAD3 pinmux. */
mbed_official 579:53297373a894 599 uint32_t pinmux_pad3;
mbed_official 579:53297373a894 600 };
mbed_official 579:53297373a894 601
mbed_official 579:53297373a894 602 #if USART_CALLBACK_MODE == true
mbed_official 579:53297373a894 603 /**
mbed_official 579:53297373a894 604 * \brief USART module instance
mbed_official 579:53297373a894 605 *
mbed_official 579:53297373a894 606 * Forward Declaration for the device instance.
mbed_official 579:53297373a894 607 */
mbed_official 579:53297373a894 608 struct usart_module;
mbed_official 579:53297373a894 609
mbed_official 579:53297373a894 610 /**
mbed_official 579:53297373a894 611 * \brief USART callback type
mbed_official 579:53297373a894 612 *
mbed_official 579:53297373a894 613 * Type of the callback functions.
mbed_official 579:53297373a894 614 */
mbed_official 579:53297373a894 615 typedef void (*usart_callback_t)(const struct usart_module *const module);
mbed_official 579:53297373a894 616 #endif
mbed_official 579:53297373a894 617
mbed_official 579:53297373a894 618 /**
mbed_official 579:53297373a894 619 * \brief SERCOM USART driver software device instance structure.
mbed_official 579:53297373a894 620 *
mbed_official 579:53297373a894 621 * SERCOM USART driver software instance structure, used to retain software
mbed_official 579:53297373a894 622 * state information of an associated hardware module instance.
mbed_official 579:53297373a894 623 *
mbed_official 579:53297373a894 624 * \note The fields of this structure should not be altered by the user
mbed_official 579:53297373a894 625 * application; they are reserved for module-internal use only.
mbed_official 579:53297373a894 626 */
mbed_official 579:53297373a894 627 struct usart_module {
mbed_official 579:53297373a894 628 #if !defined(__DOXYGEN__)
mbed_official 579:53297373a894 629 /** Pointer to the hardware instance. */
mbed_official 579:53297373a894 630 Sercom *hw;
mbed_official 579:53297373a894 631 /** Module lock. */
mbed_official 579:53297373a894 632 volatile bool locked;
mbed_official 579:53297373a894 633 /** Character size of the data being transferred. */
mbed_official 579:53297373a894 634 enum usart_character_size character_size;
mbed_official 579:53297373a894 635 /** Receiver enabled. */
mbed_official 579:53297373a894 636 bool receiver_enabled;
mbed_official 579:53297373a894 637 /** Transmitter enabled. */
mbed_official 579:53297373a894 638 bool transmitter_enabled;
mbed_official 579:53297373a894 639 #ifdef FEATURE_USART_LIN_SLAVE
mbed_official 579:53297373a894 640 /** LIN Slave Support enabled. */
mbed_official 579:53297373a894 641 bool lin_slave_enabled;
mbed_official 579:53297373a894 642 #endif
mbed_official 579:53297373a894 643 #ifdef FEATURE_USART_START_FRAME_DECTION
mbed_official 579:53297373a894 644 /** Start of frame dection enabled. */
mbed_official 579:53297373a894 645 bool start_frame_detection_enabled;
mbed_official 579:53297373a894 646 #endif
mbed_official 579:53297373a894 647 # if USART_CALLBACK_MODE == true
mbed_official 579:53297373a894 648 /** Array to store callback function pointers in. */
mbed_official 579:53297373a894 649 usart_callback_t callback[USART_CALLBACK_N];
mbed_official 579:53297373a894 650 /** Buffer pointer to where the next received character will be put. */
mbed_official 579:53297373a894 651 volatile uint8_t *rx_buffer_ptr;
mbed_official 579:53297373a894 652
mbed_official 579:53297373a894 653 /** Buffer pointer to where the next character will be transmitted from.
mbed_official 579:53297373a894 654 **/
mbed_official 579:53297373a894 655 volatile uint8_t *tx_buffer_ptr;
mbed_official 579:53297373a894 656 /** Remaining characters to receive. */
mbed_official 579:53297373a894 657 volatile uint16_t remaining_rx_buffer_length;
mbed_official 579:53297373a894 658 /** Remaining characters to transmit. */
mbed_official 579:53297373a894 659 volatile uint16_t remaining_tx_buffer_length;
mbed_official 579:53297373a894 660 /** Bit mask for callbacks registered. */
mbed_official 579:53297373a894 661 uint8_t callback_reg_mask;
mbed_official 579:53297373a894 662 /** Bit mask for callbacks enabled. */
mbed_official 579:53297373a894 663 uint8_t callback_enable_mask;
mbed_official 579:53297373a894 664 /** Holds the status of the ongoing or last read operation. */
mbed_official 579:53297373a894 665 volatile enum status_code rx_status;
mbed_official 579:53297373a894 666 /** Holds the status of the ongoing or last write operation. */
mbed_official 579:53297373a894 667 volatile enum status_code tx_status;
mbed_official 579:53297373a894 668 # endif
mbed_official 579:53297373a894 669 #endif
mbed_official 579:53297373a894 670 };
mbed_official 579:53297373a894 671
mbed_official 579:53297373a894 672 /**
mbed_official 579:53297373a894 673 * \name Lock/Unlock
mbed_official 579:53297373a894 674 * @{
mbed_official 579:53297373a894 675 */
mbed_official 579:53297373a894 676
mbed_official 579:53297373a894 677 /**
mbed_official 579:53297373a894 678 * \brief Attempt to get lock on driver instance
mbed_official 579:53297373a894 679 *
mbed_official 579:53297373a894 680 * This function checks the instance's lock, which indicates whether or not it
mbed_official 579:53297373a894 681 * is currently in use, and sets the lock if it was not already set.
mbed_official 579:53297373a894 682 *
mbed_official 579:53297373a894 683 * The purpose of this is to enable exclusive access to driver instances, so
mbed_official 579:53297373a894 684 * that, e.g., transactions by different services will not interfere with each
mbed_official 579:53297373a894 685 * other.
mbed_official 579:53297373a894 686 *
mbed_official 579:53297373a894 687 * \param[in,out] module Pointer to the driver instance to lock
mbed_official 579:53297373a894 688 *
mbed_official 579:53297373a894 689 * \retval STATUS_OK If the module was locked
mbed_official 579:53297373a894 690 * \retval STATUS_BUSY If the module was already locked
mbed_official 579:53297373a894 691 */
mbed_official 579:53297373a894 692 static inline enum status_code usart_lock(
mbed_official 579:53297373a894 693 struct usart_module *const module)
mbed_official 579:53297373a894 694 {
mbed_official 579:53297373a894 695 enum status_code status;
mbed_official 579:53297373a894 696
mbed_official 579:53297373a894 697 system_interrupt_enter_critical_section();
mbed_official 579:53297373a894 698
mbed_official 579:53297373a894 699 if (module->locked) {
mbed_official 579:53297373a894 700 status = STATUS_BUSY;
mbed_official 579:53297373a894 701 } else {
mbed_official 579:53297373a894 702 module->locked = true;
mbed_official 579:53297373a894 703 status = STATUS_OK;
mbed_official 579:53297373a894 704 }
mbed_official 579:53297373a894 705
mbed_official 579:53297373a894 706 system_interrupt_leave_critical_section();
mbed_official 579:53297373a894 707
mbed_official 579:53297373a894 708 return status;
mbed_official 579:53297373a894 709 }
mbed_official 579:53297373a894 710
mbed_official 579:53297373a894 711 /**
mbed_official 579:53297373a894 712 * \brief Unlock driver instance
mbed_official 579:53297373a894 713 *
mbed_official 579:53297373a894 714 * This function clears the instance lock, indicating that it is available for
mbed_official 579:53297373a894 715 * use.
mbed_official 579:53297373a894 716 *
mbed_official 579:53297373a894 717 * \param[in,out] module Pointer to the driver instance to lock
mbed_official 579:53297373a894 718 *
mbed_official 579:53297373a894 719 */
mbed_official 579:53297373a894 720 static inline void usart_unlock(struct usart_module *const module)
mbed_official 579:53297373a894 721 {
mbed_official 579:53297373a894 722 module->locked = false;
mbed_official 579:53297373a894 723 }
mbed_official 579:53297373a894 724
mbed_official 579:53297373a894 725 /** @} */
mbed_official 579:53297373a894 726
mbed_official 579:53297373a894 727 /**
mbed_official 579:53297373a894 728 * \brief Check if peripheral is busy syncing registers across clock domains
mbed_official 579:53297373a894 729 *
mbed_official 579:53297373a894 730 * Return peripheral synchronization status. If doing a non-blocking
mbed_official 579:53297373a894 731 * implementation this function can be used to check the sync state and hold of
mbed_official 579:53297373a894 732 * any new actions until sync is complete. If this functions is not run; the
mbed_official 579:53297373a894 733 * functions will block until the sync has completed.
mbed_official 579:53297373a894 734 *
mbed_official 579:53297373a894 735 * \param[in] module Pointer to peripheral module
mbed_official 579:53297373a894 736 *
mbed_official 579:53297373a894 737 * \return Peripheral sync status.
mbed_official 579:53297373a894 738 *
mbed_official 579:53297373a894 739 * \retval true Peripheral is busy syncing
mbed_official 579:53297373a894 740 * \retval false Peripheral is not busy syncing and can be read/written without
mbed_official 579:53297373a894 741 * stalling the bus.
mbed_official 579:53297373a894 742 */
mbed_official 579:53297373a894 743 static inline bool usart_is_syncing(
mbed_official 579:53297373a894 744 const struct usart_module *const module)
mbed_official 579:53297373a894 745 {
mbed_official 579:53297373a894 746 /* Sanity check arguments */
mbed_official 579:53297373a894 747 Assert(module);
mbed_official 579:53297373a894 748 Assert(module->hw);
mbed_official 579:53297373a894 749
mbed_official 579:53297373a894 750 SercomUsart *const usart_hw = &(module->hw->USART);
mbed_official 579:53297373a894 751
mbed_official 579:53297373a894 752 #ifdef FEATURE_USART_SYNC_SCHEME_V2
mbed_official 579:53297373a894 753 return (usart_hw->SYNCBUSY.reg);
mbed_official 579:53297373a894 754 #else
mbed_official 579:53297373a894 755 return (usart_hw->STATUS.reg & SERCOM_USART_STATUS_SYNCBUSY);
mbed_official 579:53297373a894 756 #endif
mbed_official 579:53297373a894 757 }
mbed_official 579:53297373a894 758
mbed_official 579:53297373a894 759 #if !defined (__DOXYGEN__)
mbed_official 579:53297373a894 760 /**
mbed_official 579:53297373a894 761 * \internal
mbed_official 579:53297373a894 762 * Waits until synchronization is complete
mbed_official 579:53297373a894 763 */
mbed_official 579:53297373a894 764 static inline void _usart_wait_for_sync(
mbed_official 579:53297373a894 765 const struct usart_module *const module)
mbed_official 579:53297373a894 766 {
mbed_official 579:53297373a894 767 /* Sanity check. */
mbed_official 579:53297373a894 768 Assert(module);
mbed_official 579:53297373a894 769
mbed_official 579:53297373a894 770 while (usart_is_syncing(module)) {
mbed_official 579:53297373a894 771 /* Wait until the synchronization is complete */
mbed_official 579:53297373a894 772 }
mbed_official 579:53297373a894 773 }
mbed_official 579:53297373a894 774 #endif
mbed_official 579:53297373a894 775
mbed_official 579:53297373a894 776 /**
mbed_official 579:53297373a894 777 * \brief Initializes the device to predefined defaults
mbed_official 579:53297373a894 778 *
mbed_official 579:53297373a894 779 * Initialize the USART device to predefined defaults:
mbed_official 579:53297373a894 780 * - 8-bit asynchronous USART
mbed_official 579:53297373a894 781 * - No parity
mbed_official 579:53297373a894 782 * - One stop bit
mbed_official 579:53297373a894 783 * - 9600 baud
mbed_official 579:53297373a894 784 * - Transmitter enabled
mbed_official 579:53297373a894 785 * - Receiver enabled
mbed_official 579:53297373a894 786 * - GCLK generator 0 as clock source
mbed_official 579:53297373a894 787 * - Default pin configuration
mbed_official 579:53297373a894 788 *
mbed_official 579:53297373a894 789 * The configuration struct will be updated with the default
mbed_official 579:53297373a894 790 * configuration.
mbed_official 579:53297373a894 791 *
mbed_official 579:53297373a894 792 * \param[in,out] config Pointer to configuration struct
mbed_official 579:53297373a894 793 */
mbed_official 579:53297373a894 794 static inline void usart_get_config_defaults(
mbed_official 579:53297373a894 795 struct usart_config *const config)
mbed_official 579:53297373a894 796 {
mbed_official 579:53297373a894 797 /* Sanity check arguments */
mbed_official 579:53297373a894 798 Assert(config);
mbed_official 579:53297373a894 799
mbed_official 579:53297373a894 800 /* Set default config in the config struct */
mbed_official 579:53297373a894 801 config->data_order = USART_DATAORDER_LSB;
mbed_official 579:53297373a894 802 config->transfer_mode = USART_TRANSFER_ASYNCHRONOUSLY;
mbed_official 579:53297373a894 803 config->parity = USART_PARITY_NONE;
mbed_official 579:53297373a894 804 config->stopbits = USART_STOPBITS_1;
mbed_official 579:53297373a894 805 config->character_size = USART_CHARACTER_SIZE_8BIT;
mbed_official 579:53297373a894 806 config->baudrate = 9600;
mbed_official 579:53297373a894 807 config->receiver_enable = true;
mbed_official 579:53297373a894 808 config->transmitter_enable = true;
mbed_official 579:53297373a894 809 config->clock_polarity_inverted = false;
mbed_official 579:53297373a894 810 config->use_external_clock = false;
mbed_official 579:53297373a894 811 config->ext_clock_freq = 0;
mbed_official 579:53297373a894 812 config->mux_setting = USART_RX_1_TX_2_XCK_3;
mbed_official 579:53297373a894 813 config->run_in_standby = false;
mbed_official 579:53297373a894 814 config->generator_source = GCLK_GENERATOR_0;
mbed_official 579:53297373a894 815 config->pinmux_pad0 = PINMUX_DEFAULT;
mbed_official 579:53297373a894 816 config->pinmux_pad1 = PINMUX_DEFAULT;
mbed_official 579:53297373a894 817 config->pinmux_pad2 = PINMUX_DEFAULT;
mbed_official 579:53297373a894 818 config->pinmux_pad3 = PINMUX_DEFAULT;
mbed_official 579:53297373a894 819 #ifdef FEATURE_USART_OVER_SAMPLE
mbed_official 579:53297373a894 820 config->sample_adjustment = USART_SAMPLE_ADJUSTMENT_7_8_9;
mbed_official 579:53297373a894 821 config->sample_rate = USART_SAMPLE_RATE_16X_ARITHMETIC;
mbed_official 579:53297373a894 822 #endif
mbed_official 579:53297373a894 823 #ifdef FEATURE_USART_LIN_SLAVE
mbed_official 579:53297373a894 824 config->lin_slave_enable = false;
mbed_official 579:53297373a894 825 #endif
mbed_official 579:53297373a894 826 #ifdef FEATURE_USART_IMMEDIATE_BUFFER_OVERFLOW_NOTIFICATION
mbed_official 579:53297373a894 827 config->immediate_buffer_overflow_notification = false;
mbed_official 579:53297373a894 828 #endif
mbed_official 579:53297373a894 829 #ifdef FEATURE_USART_START_FRAME_DECTION
mbed_official 579:53297373a894 830 config->start_frame_detection_enable = false;
mbed_official 579:53297373a894 831 #endif
mbed_official 579:53297373a894 832 #ifdef FEATURE_USART_IRDA
mbed_official 579:53297373a894 833 config->encoding_format_enable = false;
mbed_official 579:53297373a894 834 config->receive_pulse_length = 19;
mbed_official 579:53297373a894 835 #endif
mbed_official 579:53297373a894 836 #ifdef FEATURE_USART_COLLISION_DECTION
mbed_official 579:53297373a894 837 config->collision_detection_enable = false;
mbed_official 579:53297373a894 838 #endif
mbed_official 579:53297373a894 839 }
mbed_official 579:53297373a894 840
mbed_official 579:53297373a894 841 enum status_code usart_init(
mbed_official 579:53297373a894 842 struct usart_module *const module,
mbed_official 579:53297373a894 843 Sercom *const hw,
mbed_official 579:53297373a894 844 const struct usart_config *const config);
mbed_official 579:53297373a894 845
mbed_official 579:53297373a894 846 /**
mbed_official 579:53297373a894 847 * \brief Enable the module
mbed_official 579:53297373a894 848 *
mbed_official 579:53297373a894 849 * Enables the USART module.
mbed_official 579:53297373a894 850 *
mbed_official 579:53297373a894 851 * \param[in] module Pointer to USART software instance struct
mbed_official 579:53297373a894 852 */
mbed_official 579:53297373a894 853 static inline void usart_enable(
mbed_official 579:53297373a894 854 const struct usart_module *const module)
mbed_official 579:53297373a894 855 {
mbed_official 579:53297373a894 856 /* Sanity check arguments */
mbed_official 579:53297373a894 857 Assert(module);
mbed_official 579:53297373a894 858 Assert(module->hw);
mbed_official 579:53297373a894 859
mbed_official 579:53297373a894 860 /* Get a pointer to the hardware module instance */
mbed_official 579:53297373a894 861 SercomUsart *const usart_hw = &(module->hw->USART);
mbed_official 579:53297373a894 862
mbed_official 579:53297373a894 863 #if USART_CALLBACK_MODE == true
mbed_official 579:53297373a894 864 /* Enable Global interrupt for module */
mbed_official 579:53297373a894 865 system_interrupt_enable(_sercom_get_interrupt_vector(module->hw));
mbed_official 579:53297373a894 866 #endif
mbed_official 579:53297373a894 867
mbed_official 579:53297373a894 868 /* Wait until synchronization is complete */
mbed_official 579:53297373a894 869 _usart_wait_for_sync(module);
mbed_official 579:53297373a894 870
mbed_official 579:53297373a894 871 /* Enable USART module */
mbed_official 579:53297373a894 872 usart_hw->CTRLA.reg |= SERCOM_USART_CTRLA_ENABLE;
mbed_official 579:53297373a894 873 }
mbed_official 579:53297373a894 874
mbed_official 579:53297373a894 875 /**
mbed_official 579:53297373a894 876 * \brief Disable module
mbed_official 579:53297373a894 877 *
mbed_official 579:53297373a894 878 * Disables the USART module.
mbed_official 579:53297373a894 879 *
mbed_official 579:53297373a894 880 * \param[in] module Pointer to USART software instance struct
mbed_official 579:53297373a894 881 */
mbed_official 579:53297373a894 882 static inline void usart_disable(
mbed_official 579:53297373a894 883 const struct usart_module *const module)
mbed_official 579:53297373a894 884 {
mbed_official 579:53297373a894 885 /* Sanity check arguments */
mbed_official 579:53297373a894 886 Assert(module);
mbed_official 579:53297373a894 887 Assert(module->hw);
mbed_official 579:53297373a894 888
mbed_official 579:53297373a894 889 /* Get a pointer to the hardware module instance */
mbed_official 579:53297373a894 890 SercomUsart *const usart_hw = &(module->hw->USART);
mbed_official 579:53297373a894 891
mbed_official 579:53297373a894 892 #if USART_CALLBACK_MODE == true
mbed_official 579:53297373a894 893 /* Disable Global interrupt for module */
mbed_official 579:53297373a894 894 system_interrupt_disable(_sercom_get_interrupt_vector(module->hw));
mbed_official 579:53297373a894 895 #endif
mbed_official 579:53297373a894 896 /* Wait until synchronization is complete */
mbed_official 579:53297373a894 897 _usart_wait_for_sync(module);
mbed_official 579:53297373a894 898
mbed_official 579:53297373a894 899 /* Disable USART module */
mbed_official 579:53297373a894 900 usart_hw->CTRLA.reg &= ~SERCOM_USART_CTRLA_ENABLE;
mbed_official 579:53297373a894 901 }
mbed_official 579:53297373a894 902
mbed_official 579:53297373a894 903 /**
mbed_official 579:53297373a894 904 * \brief Resets the USART module
mbed_official 579:53297373a894 905 *
mbed_official 579:53297373a894 906 * Disables and resets the USART module.
mbed_official 579:53297373a894 907 *
mbed_official 579:53297373a894 908 * \param[in] module Pointer to the USART software instance struct
mbed_official 579:53297373a894 909 */
mbed_official 579:53297373a894 910 static inline void usart_reset(
mbed_official 579:53297373a894 911 const struct usart_module *const module)
mbed_official 579:53297373a894 912 {
mbed_official 579:53297373a894 913 /* Sanity check arguments */
mbed_official 579:53297373a894 914 Assert(module);
mbed_official 579:53297373a894 915 Assert(module->hw);
mbed_official 579:53297373a894 916
mbed_official 579:53297373a894 917 /* Get a pointer to the hardware module instance */
mbed_official 579:53297373a894 918 SercomUsart *const usart_hw = &(module->hw->USART);
mbed_official 579:53297373a894 919
mbed_official 579:53297373a894 920 usart_disable(module);
mbed_official 579:53297373a894 921
mbed_official 579:53297373a894 922 /* Wait until synchronization is complete */
mbed_official 579:53297373a894 923 _usart_wait_for_sync(module);
mbed_official 579:53297373a894 924
mbed_official 579:53297373a894 925 /* Reset module */
mbed_official 579:53297373a894 926 usart_hw->CTRLA.reg = SERCOM_USART_CTRLA_SWRST;
mbed_official 579:53297373a894 927 }
mbed_official 579:53297373a894 928
mbed_official 579:53297373a894 929 /**
mbed_official 579:53297373a894 930 * \name Writing and Reading
mbed_official 579:53297373a894 931 * @{
mbed_official 579:53297373a894 932 */
mbed_official 579:53297373a894 933 enum status_code usart_write_wait(
mbed_official 579:53297373a894 934 struct usart_module *const module,
mbed_official 579:53297373a894 935 const uint16_t tx_data);
mbed_official 579:53297373a894 936
mbed_official 579:53297373a894 937 enum status_code usart_read_wait(
mbed_official 579:53297373a894 938 struct usart_module *const module,
mbed_official 579:53297373a894 939 uint16_t *const rx_data);
mbed_official 579:53297373a894 940
mbed_official 579:53297373a894 941 enum status_code usart_write_buffer_wait(
mbed_official 579:53297373a894 942 struct usart_module *const module,
mbed_official 579:53297373a894 943 const uint8_t *tx_data,
mbed_official 579:53297373a894 944 uint16_t length);
mbed_official 579:53297373a894 945
mbed_official 579:53297373a894 946 enum status_code usart_read_buffer_wait(
mbed_official 579:53297373a894 947 struct usart_module *const module,
mbed_official 579:53297373a894 948 uint8_t *rx_data,
mbed_official 579:53297373a894 949 uint16_t length);
mbed_official 579:53297373a894 950 /** @} */
mbed_official 579:53297373a894 951
mbed_official 579:53297373a894 952 /**
mbed_official 579:53297373a894 953 * \name Enabling/Disabling Receiver and Transmitter
mbed_official 579:53297373a894 954 * @{
mbed_official 579:53297373a894 955 */
mbed_official 579:53297373a894 956
mbed_official 579:53297373a894 957 /**
mbed_official 579:53297373a894 958 * \brief Enable Transceiver
mbed_official 579:53297373a894 959 *
mbed_official 579:53297373a894 960 * Enable the given transceiver. Either RX or TX.
mbed_official 579:53297373a894 961 *
mbed_official 579:53297373a894 962 * \param[in] module Pointer to USART software instance struct
mbed_official 579:53297373a894 963 * \param[in] transceiver_type Transceiver type
mbed_official 579:53297373a894 964 */
mbed_official 579:53297373a894 965 static inline void usart_enable_transceiver(
mbed_official 579:53297373a894 966 struct usart_module *const module,
mbed_official 579:53297373a894 967 enum usart_transceiver_type transceiver_type)
mbed_official 579:53297373a894 968 {
mbed_official 579:53297373a894 969 /* Sanity check arguments */
mbed_official 579:53297373a894 970 Assert(module);
mbed_official 579:53297373a894 971 Assert(module->hw);
mbed_official 579:53297373a894 972
mbed_official 579:53297373a894 973 /* Get a pointer to the hardware module instance */
mbed_official 579:53297373a894 974 SercomUsart *const usart_hw = &(module->hw->USART);
mbed_official 579:53297373a894 975
mbed_official 579:53297373a894 976 /* Wait until synchronization is complete */
mbed_official 579:53297373a894 977 _usart_wait_for_sync(module);
mbed_official 579:53297373a894 978
mbed_official 579:53297373a894 979 switch (transceiver_type) {
mbed_official 579:53297373a894 980 case USART_TRANSCEIVER_RX:
mbed_official 579:53297373a894 981 /* Enable RX */
mbed_official 579:53297373a894 982 usart_hw->CTRLB.reg |= SERCOM_USART_CTRLB_RXEN;
mbed_official 579:53297373a894 983 module->receiver_enabled = true;
mbed_official 579:53297373a894 984 break;
mbed_official 579:53297373a894 985
mbed_official 579:53297373a894 986 case USART_TRANSCEIVER_TX:
mbed_official 579:53297373a894 987 /* Enable TX */
mbed_official 579:53297373a894 988 usart_hw->CTRLB.reg |= SERCOM_USART_CTRLB_TXEN;
mbed_official 579:53297373a894 989 module->transmitter_enabled = true;
mbed_official 579:53297373a894 990 break;
mbed_official 579:53297373a894 991 }
mbed_official 579:53297373a894 992 }
mbed_official 579:53297373a894 993
mbed_official 579:53297373a894 994 /**
mbed_official 579:53297373a894 995 * \brief Disable Transceiver
mbed_official 579:53297373a894 996 *
mbed_official 579:53297373a894 997 * Disable the given transceiver (RX or TX).
mbed_official 579:53297373a894 998 *
mbed_official 579:53297373a894 999 * \param[in] module Pointer to USART software instance struct
mbed_official 579:53297373a894 1000 * \param[in] transceiver_type Transceiver type
mbed_official 579:53297373a894 1001 */
mbed_official 579:53297373a894 1002 static inline void usart_disable_transceiver(
mbed_official 579:53297373a894 1003 struct usart_module *const module,
mbed_official 579:53297373a894 1004 enum usart_transceiver_type transceiver_type)
mbed_official 579:53297373a894 1005 {
mbed_official 579:53297373a894 1006 /* Sanity check arguments */
mbed_official 579:53297373a894 1007 Assert(module);
mbed_official 579:53297373a894 1008 Assert(module->hw);
mbed_official 579:53297373a894 1009
mbed_official 579:53297373a894 1010 /* Get a pointer to the hardware module instance */
mbed_official 579:53297373a894 1011 SercomUsart *const usart_hw = &(module->hw->USART);
mbed_official 579:53297373a894 1012
mbed_official 579:53297373a894 1013 /* Wait until synchronization is complete */
mbed_official 579:53297373a894 1014 _usart_wait_for_sync(module);
mbed_official 579:53297373a894 1015
mbed_official 579:53297373a894 1016 switch (transceiver_type) {
mbed_official 579:53297373a894 1017 case USART_TRANSCEIVER_RX:
mbed_official 579:53297373a894 1018 /* Disable RX */
mbed_official 579:53297373a894 1019 usart_hw->CTRLB.reg &= ~SERCOM_USART_CTRLB_RXEN;
mbed_official 579:53297373a894 1020 module->receiver_enabled = false;
mbed_official 579:53297373a894 1021 break;
mbed_official 579:53297373a894 1022
mbed_official 579:53297373a894 1023 case USART_TRANSCEIVER_TX:
mbed_official 579:53297373a894 1024 /* Disable TX */
mbed_official 579:53297373a894 1025 usart_hw->CTRLB.reg &= ~SERCOM_USART_CTRLB_TXEN;
mbed_official 579:53297373a894 1026 module->transmitter_enabled = false;
mbed_official 579:53297373a894 1027 break;
mbed_official 579:53297373a894 1028 }
mbed_official 579:53297373a894 1029 }
mbed_official 579:53297373a894 1030
mbed_official 579:53297373a894 1031 /** @} */
mbed_official 579:53297373a894 1032
mbed_official 579:53297373a894 1033 #ifdef __cplusplus
mbed_official 579:53297373a894 1034 }
mbed_official 579:53297373a894 1035 #endif
mbed_official 579:53297373a894 1036
mbed_official 579:53297373a894 1037 /** @} */
mbed_official 579:53297373a894 1038
mbed_official 579:53297373a894 1039 /**
mbed_official 579:53297373a894 1040 * \page asfdoc_sam0_sercom_usart_extra Extra Information for SERCOM USART Driver
mbed_official 579:53297373a894 1041 *
mbed_official 579:53297373a894 1042 * \section asfdoc_sam0_sercom_usart_extra_acronyms Acronyms
mbed_official 579:53297373a894 1043 *
mbed_official 579:53297373a894 1044 * Below is a table listing the acronyms used in this module, along with their
mbed_official 579:53297373a894 1045 * intended meanings.
mbed_official 579:53297373a894 1046 *
mbed_official 579:53297373a894 1047 * <table>
mbed_official 579:53297373a894 1048 * <tr>
mbed_official 579:53297373a894 1049 * <th>Acronym</th>
mbed_official 579:53297373a894 1050 * <th>Description</th>
mbed_official 579:53297373a894 1051 * </tr>
mbed_official 579:53297373a894 1052 * <tr>
mbed_official 579:53297373a894 1053 * <td>SERCOM</td>
mbed_official 579:53297373a894 1054 * <td>Serial Communication Interface</td>
mbed_official 579:53297373a894 1055 * </tr>
mbed_official 579:53297373a894 1056 * <tr>
mbed_official 579:53297373a894 1057 * <td>USART</td>
mbed_official 579:53297373a894 1058 * <td>Universal Synchronous and Asynchronous Serial Receiver and Transmitter</td>
mbed_official 579:53297373a894 1059 * </tr>
mbed_official 579:53297373a894 1060 * <tr>
mbed_official 579:53297373a894 1061 * <td>LSB</td>
mbed_official 579:53297373a894 1062 * <td>Least Significant Bit</td>
mbed_official 579:53297373a894 1063 * </tr>
mbed_official 579:53297373a894 1064 * <tr>
mbed_official 579:53297373a894 1065 * <td>MSB</td>
mbed_official 579:53297373a894 1066 * <td>Most Significant Bit</td>
mbed_official 579:53297373a894 1067 * </tr>
mbed_official 579:53297373a894 1068 * <tr>
mbed_official 579:53297373a894 1069 * <td>DMA</td>
mbed_official 579:53297373a894 1070 * <td>Direct Memory Access</td>
mbed_official 579:53297373a894 1071 * </tr>
mbed_official 579:53297373a894 1072 * </table>
mbed_official 579:53297373a894 1073 *
mbed_official 579:53297373a894 1074 *
mbed_official 579:53297373a894 1075 * \section asfdoc_sam0_sercom_usart_extra_dependencies Dependencies
mbed_official 579:53297373a894 1076 * This driver has the following dependencies:
mbed_official 579:53297373a894 1077 *
mbed_official 579:53297373a894 1078 * - \ref asfdoc_sam0_system_pinmux_group "System Pin Multiplexer Driver"
mbed_official 579:53297373a894 1079 * - \ref asfdoc_sam0_system_clock_group "System clock configuration"
mbed_official 579:53297373a894 1080 *
mbed_official 579:53297373a894 1081 *
mbed_official 579:53297373a894 1082 * \section asfdoc_sam0_sercom_usart_extra_errata Errata
mbed_official 579:53297373a894 1083 * There are no errata related to this driver.
mbed_official 579:53297373a894 1084 *
mbed_official 579:53297373a894 1085 *
mbed_official 579:53297373a894 1086 * \section asfdoc_sam0_sercom_usart_extra_history Module History
mbed_official 579:53297373a894 1087 * An overview of the module history is presented in the table below, with
mbed_official 579:53297373a894 1088 * details on the enhancements and fixes made to the module since its first
mbed_official 579:53297373a894 1089 * release. The current version of this corresponds to the newest version in
mbed_official 579:53297373a894 1090 * the table.
mbed_official 579:53297373a894 1091 *
mbed_official 579:53297373a894 1092 * <table>
mbed_official 579:53297373a894 1093 * <tr>
mbed_official 579:53297373a894 1094 * <th>Changelog</th>
mbed_official 579:53297373a894 1095 * </tr>
mbed_official 579:53297373a894 1096 * <tr>
mbed_official 579:53297373a894 1097 * <td>Add support for SAML21 (same features as SAMD21)</td>
mbed_official 579:53297373a894 1098 * </tr>
mbed_official 579:53297373a894 1099 * <tr>
mbed_official 579:53297373a894 1100 * <td>Add support for SAMD10/D11 (same features as SAMD21)</td>
mbed_official 579:53297373a894 1101 * </tr>
mbed_official 579:53297373a894 1102 * <tr>
mbed_official 579:53297373a894 1103 * <td>Add support for SAMR21 (same features as SAMD21)</td>
mbed_official 579:53297373a894 1104 * </tr>
mbed_official 579:53297373a894 1105 * <tr>
mbed_official 579:53297373a894 1106 * <td>Add support for SAMD21 and added new feature as below:
mbed_official 579:53297373a894 1107 \li Oversample
mbed_official 579:53297373a894 1108 \li Buffer overflow notification
mbed_official 579:53297373a894 1109 \li Irda
mbed_official 579:53297373a894 1110 \li Lin slave
mbed_official 579:53297373a894 1111 \li Start frame detection
mbed_official 579:53297373a894 1112 \li Hardware flow control
mbed_official 579:53297373a894 1113 \li Collision detection
mbed_official 579:53297373a894 1114 \li DMA support </td>
mbed_official 579:53297373a894 1115 * </tr>
mbed_official 579:53297373a894 1116 * <tr>
mbed_official 579:53297373a894 1117 * <td>\li Added new \c transmitter_enable and \c receiver_enable Boolean
mbed_official 579:53297373a894 1118 * values to \c struct usart_config
mbed_official 579:53297373a894 1119 * \li Altered \c usart_write_* and usart_read_* functions to abort with
mbed_official 579:53297373a894 1120 * an error code if the relevant transceiver is not enabled
mbed_official 579:53297373a894 1121 * \li Fixed \c usart_write_buffer_wait() and \c usart_read_buffer_wait()
mbed_official 579:53297373a894 1122 * not aborting correctly when a timeout condition occurs</td>
mbed_official 579:53297373a894 1123 * </tr>
mbed_official 579:53297373a894 1124 * <tr>
mbed_official 579:53297373a894 1125 * <td>Initial Release</td>
mbed_official 579:53297373a894 1126 * </tr>
mbed_official 579:53297373a894 1127 * </table>
mbed_official 579:53297373a894 1128 */
mbed_official 579:53297373a894 1129
mbed_official 579:53297373a894 1130 /**
mbed_official 579:53297373a894 1131 * \page asfdoc_sam0_sercom_usart_exqsg Examples for SERCOM USART Driver
mbed_official 579:53297373a894 1132 *
mbed_official 579:53297373a894 1133 * This is a list of the available Quick Start guides (QSGs) and example
mbed_official 579:53297373a894 1134 * applications for \ref asfdoc_sam0_sercom_usart_group. QSGs are simple examples with
mbed_official 579:53297373a894 1135 * step-by-step instructions to configure and use this driver in a selection of
mbed_official 579:53297373a894 1136 * use cases. Note that QSGs can be compiled as a standalone application or be
mbed_official 579:53297373a894 1137 * added to the user application.
mbed_official 579:53297373a894 1138 *
mbed_official 579:53297373a894 1139 * - \subpage asfdoc_sam0_sercom_usart_basic_use_case
mbed_official 579:53297373a894 1140 * \if USART_CALLBACK_MODE
mbed_official 579:53297373a894 1141 * - \subpage asfdoc_sam0_sercom_usart_callback_use_case
mbed_official 579:53297373a894 1142 * \endif
mbed_official 579:53297373a894 1143 * - \subpage asfdoc_sam0_sercom_usart_dma_use_case
mbed_official 579:53297373a894 1144 */
mbed_official 579:53297373a894 1145
mbed_official 579:53297373a894 1146 /**
mbed_official 579:53297373a894 1147 * \page asfdoc_sam0_sercom_usart_mux_settings SERCOM USART MUX Settings
mbed_official 579:53297373a894 1148 *
mbed_official 579:53297373a894 1149 * The following lists the possible internal SERCOM module pad function
mbed_official 579:53297373a894 1150 * assignments, for the four SERCOM pads when in USART mode. Note that this is
mbed_official 579:53297373a894 1151 * in addition to the physical GPIO pin MUX of the device, and can be used in
mbed_official 579:53297373a894 1152 * conjunction to optimize the serial data pin-out.
mbed_official 579:53297373a894 1153 *
mbed_official 579:53297373a894 1154 * When TX and RX are connected to the same pin, the USART will operate in
mbed_official 579:53297373a894 1155 * half-duplex mode if both the transmitter and receivers are enabled.
mbed_official 579:53297373a894 1156 *
mbed_official 579:53297373a894 1157 * \note When RX and XCK are connected to the same pin, the receiver must not
mbed_official 579:53297373a894 1158 * be enabled if the USART is configured to use an external clock.
mbed_official 579:53297373a894 1159 *
mbed_official 579:53297373a894 1160 *
mbed_official 579:53297373a894 1161 * <table>
mbed_official 579:53297373a894 1162 * <tr>
mbed_official 579:53297373a894 1163 * <th>MUX/Pad</th>
mbed_official 579:53297373a894 1164 * <th>PAD 0</th>
mbed_official 579:53297373a894 1165 * <th>PAD 1</th>
mbed_official 579:53297373a894 1166 * <th>PAD 2</th>
mbed_official 579:53297373a894 1167 * <th>PAD 3</th>
mbed_official 579:53297373a894 1168 * </tr>
mbed_official 579:53297373a894 1169 * <tr>
mbed_official 579:53297373a894 1170 * <td>RX_0_TX_0_XCK_1</td>
mbed_official 579:53297373a894 1171 * <td>TX / RX</td>
mbed_official 579:53297373a894 1172 * <td>XCK</td>
mbed_official 579:53297373a894 1173 * <td>-</td>
mbed_official 579:53297373a894 1174 * <td>-</td>
mbed_official 579:53297373a894 1175 * </tr>
mbed_official 579:53297373a894 1176 * <tr>
mbed_official 579:53297373a894 1177 * <td>RX_0_TX_2_XCK_3</td>
mbed_official 579:53297373a894 1178 * <td>RX</td>
mbed_official 579:53297373a894 1179 * <td>-</td>
mbed_official 579:53297373a894 1180 * <td>TX</td>
mbed_official 579:53297373a894 1181 * <td>XCK</td>
mbed_official 579:53297373a894 1182 * </tr>
mbed_official 579:53297373a894 1183 * <tr>
mbed_official 579:53297373a894 1184 * <td>RX_1_TX_0_XCK_1</td>
mbed_official 579:53297373a894 1185 * <td>TX</td>
mbed_official 579:53297373a894 1186 * <td>RX / XCK</td>
mbed_official 579:53297373a894 1187 * <td>-</td>
mbed_official 579:53297373a894 1188 * <td>-</td>
mbed_official 579:53297373a894 1189 * </tr>
mbed_official 579:53297373a894 1190 * <tr>
mbed_official 579:53297373a894 1191 * <td>RX_1_TX_2_XCK_3</td>
mbed_official 579:53297373a894 1192 * <td>-</td>
mbed_official 579:53297373a894 1193 * <td>RX</td>
mbed_official 579:53297373a894 1194 * <td>TX</td>
mbed_official 579:53297373a894 1195 * <td>XCK</td>
mbed_official 579:53297373a894 1196 * </tr>
mbed_official 579:53297373a894 1197 * <tr>
mbed_official 579:53297373a894 1198 * <td>RX_2_TX_0_XCK_1</td>
mbed_official 579:53297373a894 1199 * <td>TX</td>
mbed_official 579:53297373a894 1200 * <td>XCK</td>
mbed_official 579:53297373a894 1201 * <td>RX</td>
mbed_official 579:53297373a894 1202 * <td>-</td>
mbed_official 579:53297373a894 1203 * </tr>
mbed_official 579:53297373a894 1204 * <tr>
mbed_official 579:53297373a894 1205 * <td>RX_2_TX_2_XCK_3</td>
mbed_official 579:53297373a894 1206 * <td>-</td>
mbed_official 579:53297373a894 1207 * <td>-</td>
mbed_official 579:53297373a894 1208 * <td>TX / RX</td>
mbed_official 579:53297373a894 1209 * <td>XCK</td>
mbed_official 579:53297373a894 1210 * </tr>
mbed_official 579:53297373a894 1211 * <tr>
mbed_official 579:53297373a894 1212 * <td>RX_3_TX_0_XCK_1</td>
mbed_official 579:53297373a894 1213 * <td>TX</td>
mbed_official 579:53297373a894 1214 * <td>XCK</td>
mbed_official 579:53297373a894 1215 * <td>-</td>
mbed_official 579:53297373a894 1216 * <td>RX</td>
mbed_official 579:53297373a894 1217 * </tr>
mbed_official 579:53297373a894 1218 * <tr>
mbed_official 579:53297373a894 1219 * <td>RX_3_TX_2_XCK_3</td>
mbed_official 579:53297373a894 1220 * <td>-</td>
mbed_official 579:53297373a894 1221 * <td>-</td>
mbed_official 579:53297373a894 1222 * <td>TX</td>
mbed_official 579:53297373a894 1223 * <td>RX / XCK</td>
mbed_official 579:53297373a894 1224 * </tr>
mbed_official 579:53297373a894 1225 * </table>
mbed_official 579:53297373a894 1226 *
mbed_official 579:53297373a894 1227 * \page asfdoc_sam0_sercom_usart_document_revision_history Document Revision History
mbed_official 579:53297373a894 1228 *
mbed_official 579:53297373a894 1229 * <table>
mbed_official 579:53297373a894 1230 * <tr>
mbed_official 579:53297373a894 1231 * <th>Doc. Rev.</td>
mbed_official 579:53297373a894 1232 * <th>Date</td>
mbed_official 579:53297373a894 1233 * <th>Comments</td>
mbed_official 579:53297373a894 1234 * </tr>
mbed_official 579:53297373a894 1235 * <tr>
mbed_official 579:53297373a894 1236 * <td>F</td>
mbed_official 579:53297373a894 1237 * <td>11/2014</td>
mbed_official 579:53297373a894 1238 * <td>Add support for SAML21.</td>
mbed_official 579:53297373a894 1239 * </tr>
mbed_official 579:53297373a894 1240 * <tr>
mbed_official 579:53297373a894 1241 * <td>E</td>
mbed_official 579:53297373a894 1242 * <td>12/2014</td>
mbed_official 579:53297373a894 1243 * <td>Add support for SAMR21 and SAMD10/D11.</td>
mbed_official 579:53297373a894 1244 * </tr>
mbed_official 579:53297373a894 1245 * <tr>
mbed_official 579:53297373a894 1246 * <td>D</td>
mbed_official 579:53297373a894 1247 * <td>01/2014</td>
mbed_official 579:53297373a894 1248 * <td>Add support for SAMD21.</td>
mbed_official 579:53297373a894 1249 * </tr>
mbed_official 579:53297373a894 1250 * <tr>
mbed_official 579:53297373a894 1251 * <td>C</td>
mbed_official 579:53297373a894 1252 * <td>10/2013</td>
mbed_official 579:53297373a894 1253 * <td>Replaced the pad multiplexing documentation with a condensed table.</td>
mbed_official 579:53297373a894 1254 * </tr>
mbed_official 579:53297373a894 1255 * <tr>
mbed_official 579:53297373a894 1256 * <td>B</td>
mbed_official 579:53297373a894 1257 * <td>06/2013</td>
mbed_official 579:53297373a894 1258 * <td>Corrected documentation typos.</td>
mbed_official 579:53297373a894 1259 * </tr>
mbed_official 579:53297373a894 1260 * <tr>
mbed_official 579:53297373a894 1261 * <td>A</td>
mbed_official 579:53297373a894 1262 * <td>06/2013</td>
mbed_official 579:53297373a894 1263 * <td>Initial release</td>
mbed_official 579:53297373a894 1264 * </tr>
mbed_official 579:53297373a894 1265 * </table>
mbed_official 579:53297373a894 1266 */
mbed_official 579:53297373a894 1267 #endif /* USART_H_INCLUDED */