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 * \file
mbed_official 579:53297373a894 3 *
mbed_official 579:53297373a894 4 * \brief SAM SERCOM I2C Master Driver
mbed_official 579:53297373a894 5 *
mbed_official 579:53297373a894 6 * Copyright (C) 2012-2014 Atmel Corporation. All rights reserved.
mbed_official 579:53297373a894 7 *
mbed_official 579:53297373a894 8 * \asf_license_start
mbed_official 579:53297373a894 9 *
mbed_official 579:53297373a894 10 * \page License
mbed_official 579:53297373a894 11 *
mbed_official 579:53297373a894 12 * Redistribution and use in source and binary forms, with or without
mbed_official 579:53297373a894 13 * modification, are permitted provided that the following conditions are met:
mbed_official 579:53297373a894 14 *
mbed_official 579:53297373a894 15 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 579:53297373a894 16 * this list of conditions and the following disclaimer.
mbed_official 579:53297373a894 17 *
mbed_official 579:53297373a894 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 579:53297373a894 19 * this list of conditions and the following disclaimer in the documentation
mbed_official 579:53297373a894 20 * and/or other materials provided with the distribution.
mbed_official 579:53297373a894 21 *
mbed_official 579:53297373a894 22 * 3. The name of Atmel may not be used to endorse or promote products derived
mbed_official 579:53297373a894 23 * from this software without specific prior written permission.
mbed_official 579:53297373a894 24 *
mbed_official 579:53297373a894 25 * 4. This software may only be redistributed and used in connection with an
mbed_official 579:53297373a894 26 * Atmel microcontroller product.
mbed_official 579:53297373a894 27 *
mbed_official 579:53297373a894 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
mbed_official 579:53297373a894 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mbed_official 579:53297373a894 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
mbed_official 579:53297373a894 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
mbed_official 579:53297373a894 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 579:53297373a894 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
mbed_official 579:53297373a894 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
mbed_official 579:53297373a894 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
mbed_official 579:53297373a894 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
mbed_official 579:53297373a894 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 579:53297373a894 38 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 579:53297373a894 39 *
mbed_official 579:53297373a894 40 * \asf_license_stop
mbed_official 579:53297373a894 41 *
mbed_official 579:53297373a894 42 */
mbed_official 579:53297373a894 43 /**
mbed_official 579:53297373a894 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
mbed_official 579:53297373a894 45 */
mbed_official 579:53297373a894 46
mbed_official 579:53297373a894 47 #ifndef I2C_MASTER_H_INCLUDED
mbed_official 579:53297373a894 48 #define I2C_MASTER_H_INCLUDED
mbed_official 579:53297373a894 49
mbed_official 579:53297373a894 50 #include "i2c_common.h"
mbed_official 579:53297373a894 51 #include <sercom.h>
mbed_official 579:53297373a894 52 #include <pinmux.h>
mbed_official 579:53297373a894 53
mbed_official 579:53297373a894 54 #if I2C_MASTER_CALLBACK_MODE == true
mbed_official 579:53297373a894 55 # include <sercom_interrupt.h>
mbed_official 579:53297373a894 56 #endif
mbed_official 579:53297373a894 57
mbed_official 579:53297373a894 58 #ifdef __cplusplus
mbed_official 579:53297373a894 59 extern "C" {
mbed_official 579:53297373a894 60 #endif
mbed_official 579:53297373a894 61
mbed_official 579:53297373a894 62 #ifndef PINMUX_DEFAULT
mbed_official 579:53297373a894 63 # define PINMUX_DEFAULT 0
mbed_official 579:53297373a894 64 #endif
mbed_official 579:53297373a894 65
mbed_official 579:53297373a894 66 /**
mbed_official 579:53297373a894 67 * \addtogroup asfdoc_sam0_sercom_i2c_group
mbed_official 579:53297373a894 68 *
mbed_official 579:53297373a894 69 * @{
mbed_official 579:53297373a894 70 */
mbed_official 579:53297373a894 71
mbed_official 579:53297373a894 72 /**
mbed_official 579:53297373a894 73 * \brief I<SUP>2</SUP>C master packet for read/write
mbed_official 579:53297373a894 74 *
mbed_official 579:53297373a894 75 * Structure to be used when transferring I<SUP>2</SUP>C master packets.
mbed_official 579:53297373a894 76 */
mbed_official 579:53297373a894 77 struct i2c_master_packet {
mbed_official 579:53297373a894 78 /** Address to slave device. */
mbed_official 579:53297373a894 79 uint16_t address;
mbed_official 579:53297373a894 80 /** Length of data array. */
mbed_official 579:53297373a894 81 uint16_t data_length;
mbed_official 579:53297373a894 82 /** Data array containing all data to be transferred. */
mbed_official 579:53297373a894 83 uint8_t *data;
mbed_official 579:53297373a894 84 /** Use 10-bit addressing. Set to false if the feature is not supported by the device. */
mbed_official 579:53297373a894 85 bool ten_bit_address;
mbed_official 579:53297373a894 86 /** Use high speed transfer. Set to false if the feature is not supported by the device. */
mbed_official 579:53297373a894 87 bool high_speed;
mbed_official 579:53297373a894 88 /** High speed mode master code (0000 1XXX), valid when high_speed is true. */
mbed_official 579:53297373a894 89 uint8_t hs_master_code;
mbed_official 579:53297373a894 90 };
mbed_official 579:53297373a894 91
mbed_official 579:53297373a894 92 /** \brief Interrupt flags
mbed_official 579:53297373a894 93 *
mbed_official 579:53297373a894 94 * Flags used when reading or setting interrupt flags.
mbed_official 579:53297373a894 95 */
mbed_official 579:53297373a894 96 enum i2c_master_interrupt_flag {
mbed_official 579:53297373a894 97 /** Interrupt flag used for write. */
mbed_official 579:53297373a894 98 I2C_MASTER_INTERRUPT_WRITE = 0,
mbed_official 579:53297373a894 99 /** Interrupt flag used for read. */
mbed_official 579:53297373a894 100 I2C_MASTER_INTERRUPT_READ = 1,
mbed_official 579:53297373a894 101 };
mbed_official 579:53297373a894 102
mbed_official 579:53297373a894 103 /**
mbed_official 579:53297373a894 104 * \brief Values for hold time after start bit.
mbed_official 579:53297373a894 105 *
mbed_official 579:53297373a894 106 * Values for the possible I<SUP>2</SUP>C master mode SDA internal hold times after start
mbed_official 579:53297373a894 107 * bit has been sent.
mbed_official 579:53297373a894 108 */
mbed_official 579:53297373a894 109 enum i2c_master_start_hold_time {
mbed_official 579:53297373a894 110 /** Internal SDA hold time disabled. */
mbed_official 579:53297373a894 111 I2C_MASTER_START_HOLD_TIME_DISABLED = SERCOM_I2CM_CTRLA_SDAHOLD(0),
mbed_official 579:53297373a894 112 /** Internal SDA hold time 50ns - 100ns. */
mbed_official 579:53297373a894 113 I2C_MASTER_START_HOLD_TIME_50NS_100NS = SERCOM_I2CM_CTRLA_SDAHOLD(1),
mbed_official 579:53297373a894 114 /** Internal SDA hold time 300ns - 600ns. */
mbed_official 579:53297373a894 115 I2C_MASTER_START_HOLD_TIME_300NS_600NS = SERCOM_I2CM_CTRLA_SDAHOLD(2),
mbed_official 579:53297373a894 116 /** Internal SDA hold time 400ns - 800ns. */
mbed_official 579:53297373a894 117 I2C_MASTER_START_HOLD_TIME_400NS_800NS = SERCOM_I2CM_CTRLA_SDAHOLD(3),
mbed_official 579:53297373a894 118 };
mbed_official 579:53297373a894 119
mbed_official 579:53297373a894 120 /**
mbed_official 579:53297373a894 121 * \ brief Values for inactive bus time-out.
mbed_official 579:53297373a894 122 *
mbed_official 579:53297373a894 123 * If the inactive bus time-out is enabled and the bus is inactive for
mbed_official 579:53297373a894 124 * longer than the time-out setting, the bus state logic will be set to idle.
mbed_official 579:53297373a894 125 */
mbed_official 579:53297373a894 126 enum i2c_master_inactive_timeout {
mbed_official 579:53297373a894 127 /** Inactive bus time-out disabled. */
mbed_official 579:53297373a894 128 I2C_MASTER_INACTIVE_TIMEOUT_DISABLED = SERCOM_I2CM_CTRLA_INACTOUT(0),
mbed_official 579:53297373a894 129 /** Inactive bus time-out 5-6 SCL cycle time-out. */
mbed_official 579:53297373a894 130 I2C_MASTER_INACTIVE_TIMEOUT_55US = SERCOM_I2CM_CTRLA_INACTOUT(1),
mbed_official 579:53297373a894 131 /** Inactive bus time-out 10-11 SCL cycle time-out. */
mbed_official 579:53297373a894 132 I2C_MASTER_INACTIVE_TIMEOUT_105US = SERCOM_I2CM_CTRLA_INACTOUT(2),
mbed_official 579:53297373a894 133 /** Inactive bus time-out 20-21 SCL cycle time-out. */
mbed_official 579:53297373a894 134 I2C_MASTER_INACTIVE_TIMEOUT_205US = SERCOM_I2CM_CTRLA_INACTOUT(3),
mbed_official 579:53297373a894 135 };
mbed_official 579:53297373a894 136
mbed_official 579:53297373a894 137 /**
mbed_official 579:53297373a894 138 * \brief I<SUP>2</SUP>C frequencies
mbed_official 579:53297373a894 139 *
mbed_official 579:53297373a894 140 * Values for I<SUP>2</SUP>C speeds supported by the module. The driver
mbed_official 579:53297373a894 141 * will also support setting any other value, in which case set
mbed_official 579:53297373a894 142 * the value in the \ref i2c_master_config at desired value divided by 1000.
mbed_official 579:53297373a894 143 *
mbed_official 579:53297373a894 144 * Example: If 10KHz operation is required, give baud_rate in the configuration
mbed_official 579:53297373a894 145 * structure the value 10.
mbed_official 579:53297373a894 146 */
mbed_official 579:53297373a894 147 enum i2c_master_baud_rate {
mbed_official 579:53297373a894 148 /** Baud rate at 100KHz (Standard-mode). */
mbed_official 579:53297373a894 149 I2C_MASTER_BAUD_RATE_100KHZ = 100,
mbed_official 579:53297373a894 150 /** Baud rate at 400KHz (Fast-mode). */
mbed_official 579:53297373a894 151 I2C_MASTER_BAUD_RATE_400KHZ = 400,
mbed_official 579:53297373a894 152 #ifdef FEATURE_I2C_FAST_MODE_PLUS_AND_HIGH_SPEED
mbed_official 579:53297373a894 153 /** Baud rate at 1MHz (Fast-mode Plus). */
mbed_official 579:53297373a894 154 I2C_MASTER_BAUD_RATE_1000KHZ = 1000,
mbed_official 579:53297373a894 155 /** Baud rate at 3.4MHz (High-speed mode). */
mbed_official 579:53297373a894 156 I2C_MASTER_BAUD_RATE_3400KHZ = 3400,
mbed_official 579:53297373a894 157 #endif
mbed_official 579:53297373a894 158 };
mbed_official 579:53297373a894 159
mbed_official 579:53297373a894 160 #ifdef FEATURE_I2C_FAST_MODE_PLUS_AND_HIGH_SPEED
mbed_official 579:53297373a894 161 /**
mbed_official 579:53297373a894 162 * \brief Enum for the transfer speed
mbed_official 579:53297373a894 163 *
mbed_official 579:53297373a894 164 * Enum for the transfer speed.
mbed_official 579:53297373a894 165 */
mbed_official 579:53297373a894 166 enum i2c_master_transfer_speed {
mbed_official 579:53297373a894 167 /** Standard-mode (Sm) up to 100KHz and Fast-mode (Fm) up to 400KHz. */
mbed_official 579:53297373a894 168 I2C_MASTER_SPEED_STANDARD_AND_FAST = SERCOM_I2CM_CTRLA_SPEED(0),
mbed_official 579:53297373a894 169 /** Fast-mode Plus (Fm+) up to 1MHz. */
mbed_official 579:53297373a894 170 I2C_MASTER_SPEED_FAST_MODE_PLUS = SERCOM_I2CM_CTRLA_SPEED(1),
mbed_official 579:53297373a894 171 /** High-speed mode (Hs-mode) up to 3.4MHz. */
mbed_official 579:53297373a894 172 I2C_MASTER_SPEED_HIGH_SPEED = SERCOM_I2CM_CTRLA_SPEED(2),
mbed_official 579:53297373a894 173 };
mbed_official 579:53297373a894 174 #endif
mbed_official 579:53297373a894 175
mbed_official 579:53297373a894 176 #if I2C_MASTER_CALLBACK_MODE == true
mbed_official 579:53297373a894 177 /**
mbed_official 579:53297373a894 178 * \brief Callback types
mbed_official 579:53297373a894 179 *
mbed_official 579:53297373a894 180 * The available callback types for the I<SUP>2</SUP>C master module.
mbed_official 579:53297373a894 181 */
mbed_official 579:53297373a894 182 enum i2c_master_callback {
mbed_official 579:53297373a894 183 /** Callback for packet write complete. */
mbed_official 579:53297373a894 184 I2C_MASTER_CALLBACK_WRITE_COMPLETE = 0,
mbed_official 579:53297373a894 185 /** Callback for packet read complete. */
mbed_official 579:53297373a894 186 I2C_MASTER_CALLBACK_READ_COMPLETE = 1,
mbed_official 579:53297373a894 187 /** Callback for error. */
mbed_official 579:53297373a894 188 I2C_MASTER_CALLBACK_ERROR = 2,
mbed_official 579:53297373a894 189 # if !defined(__DOXYGEN__)
mbed_official 579:53297373a894 190 /** Total number of callbacks. */
mbed_official 579:53297373a894 191 _I2C_MASTER_CALLBACK_N = 3,
mbed_official 579:53297373a894 192 # endif
mbed_official 579:53297373a894 193 };
mbed_official 579:53297373a894 194
mbed_official 579:53297373a894 195 # if !defined(__DOXYGEN__)
mbed_official 579:53297373a894 196 /* Prototype for software module. */
mbed_official 579:53297373a894 197 struct i2c_master_module;
mbed_official 579:53297373a894 198
mbed_official 579:53297373a894 199 typedef void (*i2c_master_callback_t)(
mbed_official 579:53297373a894 200 struct i2c_master_module *const module);
mbed_official 579:53297373a894 201 # endif
mbed_official 579:53297373a894 202 #endif
mbed_official 579:53297373a894 203
mbed_official 579:53297373a894 204 /**
mbed_official 579:53297373a894 205 * \brief SERCOM I<SUP>2</SUP>C Master driver software device instance structure.
mbed_official 579:53297373a894 206 *
mbed_official 579:53297373a894 207 * SERCOM I<SUP>2</SUP>C Master driver software instance structure, used to
mbed_official 579:53297373a894 208 * retain software state information of an associated hardware module instance.
mbed_official 579:53297373a894 209 *
mbed_official 579:53297373a894 210 * \note The fields of this structure should not be altered by the user
mbed_official 579:53297373a894 211 * application; they are reserved for module-internal use only.
mbed_official 579:53297373a894 212 */
mbed_official 579:53297373a894 213 struct i2c_master_module {
mbed_official 579:53297373a894 214 #if !defined(__DOXYGEN__)
mbed_official 579:53297373a894 215 /** Hardware instance initialized for the struct. */
mbed_official 579:53297373a894 216 Sercom *hw;
mbed_official 579:53297373a894 217 /** Module lock. */
mbed_official 579:53297373a894 218 volatile bool locked;
mbed_official 579:53297373a894 219 /** Unknown bus state timeout. */
mbed_official 579:53297373a894 220 uint16_t unknown_bus_state_timeout;
mbed_official 579:53297373a894 221 /** Buffer write timeout value. */
mbed_official 579:53297373a894 222 uint16_t buffer_timeout;
mbed_official 579:53297373a894 223 /** If true, stop condition will be sent after a read/write. */
mbed_official 579:53297373a894 224 bool send_stop;
mbed_official 579:53297373a894 225 # if I2C_MASTER_CALLBACK_MODE == true
mbed_official 579:53297373a894 226 /** Pointers to callback functions. */
mbed_official 579:53297373a894 227 volatile i2c_master_callback_t callbacks[_I2C_MASTER_CALLBACK_N];
mbed_official 579:53297373a894 228 /** Mask for registered callbacks. */
mbed_official 579:53297373a894 229 volatile uint8_t registered_callback;
mbed_official 579:53297373a894 230 /** Mask for enabled callbacks. */
mbed_official 579:53297373a894 231 volatile uint8_t enabled_callback;
mbed_official 579:53297373a894 232 /** The total number of bytes to transfer. */
mbed_official 579:53297373a894 233 volatile uint16_t buffer_length;
mbed_official 579:53297373a894 234 /**
mbed_official 579:53297373a894 235 * Counter used for bytes left to send in write and to count number of
mbed_official 579:53297373a894 236 * obtained bytes in read.
mbed_official 579:53297373a894 237 */
mbed_official 579:53297373a894 238 volatile uint16_t buffer_remaining;
mbed_official 579:53297373a894 239 /** Data buffer for packet write and read. */
mbed_official 579:53297373a894 240 volatile uint8_t *buffer;
mbed_official 579:53297373a894 241 /** Save direction of async request. 1 = read, 0 = write. */
mbed_official 579:53297373a894 242 volatile enum i2c_transfer_direction transfer_direction;
mbed_official 579:53297373a894 243 /** Status for status read back in error callback. */
mbed_official 579:53297373a894 244 volatile enum status_code status;
mbed_official 579:53297373a894 245 # endif
mbed_official 579:53297373a894 246 #endif
mbed_official 579:53297373a894 247 };
mbed_official 579:53297373a894 248
mbed_official 579:53297373a894 249 /**
mbed_official 579:53297373a894 250 * \brief Configuration structure for the I<SUP>2</SUP>C Master device
mbed_official 579:53297373a894 251 *
mbed_official 579:53297373a894 252 * This is the configuration structure for the I<SUP>2</SUP>C Master device. It
mbed_official 579:53297373a894 253 * is used as an argument for \ref i2c_master_init to provide the desired
mbed_official 579:53297373a894 254 * configurations for the module. The structure should be initialized using the
mbed_official 579:53297373a894 255 * \ref i2c_master_get_config_defaults .
mbed_official 579:53297373a894 256 */
mbed_official 579:53297373a894 257 struct i2c_master_config {
mbed_official 579:53297373a894 258 /** Baud rate (in KHz) for I<SUP>2</SUP>C operations in
mbed_official 579:53297373a894 259 * standard-mode, Fast-mode and Fast-mode Plus Transfers,
mbed_official 579:53297373a894 260 * \ref i2c_master_baud_rate. */
mbed_official 579:53297373a894 261 uint32_t baud_rate;
mbed_official 579:53297373a894 262 #ifdef FEATURE_I2C_FAST_MODE_PLUS_AND_HIGH_SPEED
mbed_official 579:53297373a894 263 /** Baud rate (in KHz) for I<SUP>2</SUP>C operations in
mbed_official 579:53297373a894 264 * High-speed mode, \ref i2c_master_baud_rate. */
mbed_official 579:53297373a894 265 uint32_t baud_rate_high_speed;
mbed_official 579:53297373a894 266 /** Transfer speed mode. */
mbed_official 579:53297373a894 267 enum i2c_master_transfer_speed transfer_speed;
mbed_official 579:53297373a894 268 #endif
mbed_official 579:53297373a894 269 /** GCLK generator to use as clock source. */
mbed_official 579:53297373a894 270 enum gclk_generator generator_source;
mbed_official 579:53297373a894 271 /** Bus hold time after start signal on data line. */
mbed_official 579:53297373a894 272 enum i2c_master_start_hold_time start_hold_time;
mbed_official 579:53297373a894 273 /** Unknown bus state \ref asfdoc_sam0_sercom_i2c_unknown_bus_timeout "timeout". */
mbed_official 579:53297373a894 274 uint16_t unknown_bus_state_timeout;
mbed_official 579:53297373a894 275 /** Timeout for packet write to wait for slave. */
mbed_official 579:53297373a894 276 uint16_t buffer_timeout;
mbed_official 579:53297373a894 277 /** Set to keep module active in sleep modes. */
mbed_official 579:53297373a894 278 bool run_in_standby;
mbed_official 579:53297373a894 279 /** PAD0 (SDA) pinmux. */
mbed_official 579:53297373a894 280 uint32_t pinmux_pad0;
mbed_official 579:53297373a894 281 /** PAD1 (SCL) pinmux. */
mbed_official 579:53297373a894 282 uint32_t pinmux_pad1;
mbed_official 579:53297373a894 283 /** Set to enable SCL low time-out. */
mbed_official 579:53297373a894 284 bool scl_low_timeout;
mbed_official 579:53297373a894 285 /** Inactive bus time out. */
mbed_official 579:53297373a894 286 enum i2c_master_inactive_timeout inactive_timeout;
mbed_official 579:53297373a894 287 #ifdef FEATURE_I2C_SCL_STRETCH_MODE
mbed_official 579:53297373a894 288 /** Set to enable SCL stretch only after ACK bit (required for high speed). */
mbed_official 579:53297373a894 289 bool scl_stretch_only_after_ack_bit;
mbed_official 579:53297373a894 290 #endif
mbed_official 579:53297373a894 291 #ifdef FEATURE_I2C_SCL_EXTEND_TIMEOUT
mbed_official 579:53297373a894 292 /** Set to enable slave SCL low extend time-out. */
mbed_official 579:53297373a894 293 bool slave_scl_low_extend_timeout;
mbed_official 579:53297373a894 294 /** Set to enable maser SCL low extend time-out. */
mbed_official 579:53297373a894 295 bool master_scl_low_extend_timeout;
mbed_official 579:53297373a894 296 #endif
mbed_official 579:53297373a894 297 };
mbed_official 579:53297373a894 298
mbed_official 579:53297373a894 299 /**
mbed_official 579:53297373a894 300 * \name Lock/Unlock
mbed_official 579:53297373a894 301 * @{
mbed_official 579:53297373a894 302 */
mbed_official 579:53297373a894 303
mbed_official 579:53297373a894 304 /**
mbed_official 579:53297373a894 305 * \brief Attempt to get lock on driver instance
mbed_official 579:53297373a894 306 *
mbed_official 579:53297373a894 307 * This function checks the instance's lock, which indicates whether or not it
mbed_official 579:53297373a894 308 * is currently in use, and sets the lock if it was not already set.
mbed_official 579:53297373a894 309 *
mbed_official 579:53297373a894 310 * The purpose of this is to enable exclusive access to driver instances, so
mbed_official 579:53297373a894 311 * that, e.g., transactions by different services will not interfere with each
mbed_official 579:53297373a894 312 * other.
mbed_official 579:53297373a894 313 *
mbed_official 579:53297373a894 314 * \param[in,out] module Pointer to the driver instance to lock
mbed_official 579:53297373a894 315 *
mbed_official 579:53297373a894 316 * \retval STATUS_OK If the module was locked
mbed_official 579:53297373a894 317 * \retval STATUS_BUSY If the module was already locked
mbed_official 579:53297373a894 318 */
mbed_official 579:53297373a894 319 static inline enum status_code i2c_master_lock(
mbed_official 579:53297373a894 320 struct i2c_master_module *const module)
mbed_official 579:53297373a894 321 {
mbed_official 579:53297373a894 322 enum status_code status;
mbed_official 579:53297373a894 323
mbed_official 579:53297373a894 324 system_interrupt_enter_critical_section();
mbed_official 579:53297373a894 325
mbed_official 579:53297373a894 326 if (module->locked) {
mbed_official 579:53297373a894 327 status = STATUS_BUSY;
mbed_official 579:53297373a894 328 } else {
mbed_official 579:53297373a894 329 module->locked = true;
mbed_official 579:53297373a894 330 status = STATUS_OK;
mbed_official 579:53297373a894 331 }
mbed_official 579:53297373a894 332
mbed_official 579:53297373a894 333 system_interrupt_leave_critical_section();
mbed_official 579:53297373a894 334
mbed_official 579:53297373a894 335 return status;
mbed_official 579:53297373a894 336 }
mbed_official 579:53297373a894 337
mbed_official 579:53297373a894 338 /**
mbed_official 579:53297373a894 339 * \brief Unlock driver instance
mbed_official 579:53297373a894 340 *
mbed_official 579:53297373a894 341 * This function clears the instance lock, indicating that it is available for
mbed_official 579:53297373a894 342 * use.
mbed_official 579:53297373a894 343 *
mbed_official 579:53297373a894 344 * \param[in,out] module Pointer to the driver instance to lock
mbed_official 579:53297373a894 345 *
mbed_official 579:53297373a894 346 * \retval STATUS_OK If the module was locked
mbed_official 579:53297373a894 347 * \retval STATUS_BUSY If the module was already locked
mbed_official 579:53297373a894 348 */
mbed_official 579:53297373a894 349 static inline void i2c_master_unlock(struct i2c_master_module *const module)
mbed_official 579:53297373a894 350 {
mbed_official 579:53297373a894 351 module->locked = false;
mbed_official 579:53297373a894 352 }
mbed_official 579:53297373a894 353
mbed_official 579:53297373a894 354 /** @} */
mbed_official 579:53297373a894 355
mbed_official 579:53297373a894 356 /**
mbed_official 579:53297373a894 357 * \name Configuration and Initialization
mbed_official 579:53297373a894 358 * @{
mbed_official 579:53297373a894 359 */
mbed_official 579:53297373a894 360
mbed_official 579:53297373a894 361 /**
mbed_official 579:53297373a894 362 * \brief Returns the synchronization status of the module
mbed_official 579:53297373a894 363 *
mbed_official 579:53297373a894 364 * Returns the synchronization status of the module.
mbed_official 579:53297373a894 365 *
mbed_official 579:53297373a894 366 * \param[in] module Pointer to software module structure
mbed_official 579:53297373a894 367 *
mbed_official 579:53297373a894 368 * \return Status of the synchronization.
mbed_official 579:53297373a894 369 * \retval true Module is busy synchronizing
mbed_official 579:53297373a894 370 * \retval false Module is not synchronizing
mbed_official 579:53297373a894 371 */
mbed_official 579:53297373a894 372 static inline bool i2c_master_is_syncing (
mbed_official 579:53297373a894 373 const struct i2c_master_module *const module)
mbed_official 579:53297373a894 374 {
mbed_official 579:53297373a894 375 /* Sanity check. */
mbed_official 579:53297373a894 376 Assert(module);
mbed_official 579:53297373a894 377 Assert(module->hw);
mbed_official 579:53297373a894 378
mbed_official 579:53297373a894 379 SercomI2cm *const i2c_hw = &(module->hw->I2CM);
mbed_official 579:53297373a894 380
mbed_official 579:53297373a894 381 #if defined(FEATURE_SERCOM_SYNCBUSY_SCHEME_VERSION_1)
mbed_official 579:53297373a894 382 return (i2c_hw->STATUS.reg & SERCOM_I2CM_STATUS_SYNCBUSY);
mbed_official 579:53297373a894 383 #elif defined(FEATURE_SERCOM_SYNCBUSY_SCHEME_VERSION_2)
mbed_official 579:53297373a894 384 return (i2c_hw->SYNCBUSY.reg & SERCOM_I2CM_SYNCBUSY_MASK);
mbed_official 579:53297373a894 385 #else
mbed_official 579:53297373a894 386 # error Unknown SERCOM SYNCBUSY scheme!
mbed_official 579:53297373a894 387 #endif
mbed_official 579:53297373a894 388 }
mbed_official 579:53297373a894 389
mbed_official 579:53297373a894 390 #if !defined(__DOXYGEN__)
mbed_official 579:53297373a894 391 /**
mbed_official 579:53297373a894 392 * \internal
mbed_official 579:53297373a894 393 * Wait for hardware module to sync
mbed_official 579:53297373a894 394 *
mbed_official 579:53297373a894 395 * \param[in] module Pointer to software module structure
mbed_official 579:53297373a894 396 */
mbed_official 579:53297373a894 397 static void _i2c_master_wait_for_sync(
mbed_official 579:53297373a894 398 const struct i2c_master_module *const module)
mbed_official 579:53297373a894 399 {
mbed_official 579:53297373a894 400 /* Sanity check. */
mbed_official 579:53297373a894 401 Assert(module);
mbed_official 579:53297373a894 402
mbed_official 579:53297373a894 403 while (i2c_master_is_syncing(module)) {
mbed_official 579:53297373a894 404 /* Wait for I2C module to sync. */
mbed_official 579:53297373a894 405 }
mbed_official 579:53297373a894 406 }
mbed_official 579:53297373a894 407 #endif
mbed_official 579:53297373a894 408
mbed_official 579:53297373a894 409 /**
mbed_official 579:53297373a894 410 * \brief Gets the I<SUP>2</SUP>C master default configurations
mbed_official 579:53297373a894 411 *
mbed_official 579:53297373a894 412 * Use to initialize the configuration structure to known default values.
mbed_official 579:53297373a894 413 *
mbed_official 579:53297373a894 414 * The default configuration is as follows:
mbed_official 579:53297373a894 415 * - Baudrate 100KHz
mbed_official 579:53297373a894 416 * - GCLK generator 0
mbed_official 579:53297373a894 417 * - Do not run in standby
mbed_official 579:53297373a894 418 * - Start bit hold time 300ns - 600ns
mbed_official 579:53297373a894 419 * - Buffer timeout = 65535
mbed_official 579:53297373a894 420 * - Unknown bus status timeout = 65535
mbed_official 579:53297373a894 421 * - Do not run in standby
mbed_official 579:53297373a894 422 * - PINMUX_DEFAULT for SERCOM pads
mbed_official 579:53297373a894 423 *
mbed_official 579:53297373a894 424 * Those default configuration only availale if the device supports it:
mbed_official 579:53297373a894 425 * - High speed baudrate 3.4MHz
mbed_official 579:53297373a894 426 * - Standard-mode and Fast-mode transfer speed
mbed_official 579:53297373a894 427 * - SCL stretch disabled
mbed_official 579:53297373a894 428 * - slave SCL low extend time-out disabled
mbed_official 579:53297373a894 429 * - maser SCL low extend time-out disabled
mbed_official 579:53297373a894 430 *
mbed_official 579:53297373a894 431 * \param[out] config Pointer to configuration structure to be initiated
mbed_official 579:53297373a894 432 */
mbed_official 579:53297373a894 433 static inline void i2c_master_get_config_defaults(
mbed_official 579:53297373a894 434 struct i2c_master_config *const config)
mbed_official 579:53297373a894 435 {
mbed_official 579:53297373a894 436 /*Sanity check argument. */
mbed_official 579:53297373a894 437 Assert(config);
mbed_official 579:53297373a894 438 config->baud_rate = I2C_MASTER_BAUD_RATE_100KHZ;
mbed_official 579:53297373a894 439 #ifdef FEATURE_I2C_FAST_MODE_PLUS_AND_HIGH_SPEED
mbed_official 579:53297373a894 440 config->baud_rate_high_speed = I2C_MASTER_BAUD_RATE_3400KHZ;
mbed_official 579:53297373a894 441 config->transfer_speed = I2C_MASTER_SPEED_STANDARD_AND_FAST;
mbed_official 579:53297373a894 442 #endif
mbed_official 579:53297373a894 443 config->generator_source = GCLK_GENERATOR_0;
mbed_official 579:53297373a894 444 config->run_in_standby = false;
mbed_official 579:53297373a894 445 config->start_hold_time = I2C_MASTER_START_HOLD_TIME_300NS_600NS;
mbed_official 579:53297373a894 446 config->buffer_timeout = 65535;
mbed_official 579:53297373a894 447 config->unknown_bus_state_timeout = 65535;
mbed_official 579:53297373a894 448 config->pinmux_pad0 = PINMUX_DEFAULT;
mbed_official 579:53297373a894 449 config->pinmux_pad1 = PINMUX_DEFAULT;
mbed_official 579:53297373a894 450 config->scl_low_timeout = false;
mbed_official 579:53297373a894 451 config->inactive_timeout = I2C_MASTER_INACTIVE_TIMEOUT_DISABLED;
mbed_official 579:53297373a894 452 #ifdef FEATURE_I2C_SCL_STRETCH_MODE
mbed_official 579:53297373a894 453 config->scl_stretch_only_after_ack_bit = false;
mbed_official 579:53297373a894 454 #endif
mbed_official 579:53297373a894 455 #ifdef FEATURE_I2C_SCL_EXTEND_TIMEOUT
mbed_official 579:53297373a894 456 config->slave_scl_low_extend_timeout = false;
mbed_official 579:53297373a894 457 config->master_scl_low_extend_timeout = false;
mbed_official 579:53297373a894 458 #endif
mbed_official 579:53297373a894 459 }
mbed_official 579:53297373a894 460
mbed_official 579:53297373a894 461 enum status_code i2c_master_init(
mbed_official 579:53297373a894 462 struct i2c_master_module *const module,
mbed_official 579:53297373a894 463 Sercom *const hw,
mbed_official 579:53297373a894 464 const struct i2c_master_config *const config);
mbed_official 579:53297373a894 465
mbed_official 579:53297373a894 466 /**
mbed_official 579:53297373a894 467 * \brief Enables the I<SUP>2</SUP>C module
mbed_official 579:53297373a894 468 *
mbed_official 579:53297373a894 469 * Enables the requested I<SUP>2</SUP>C module and set the bus state to IDLE
mbed_official 579:53297373a894 470 * after the specified \ref asfdoc_sam0_sercom_i2c_timeout "timeout" period if no
mbed_official 579:53297373a894 471 * stop bit is detected.
mbed_official 579:53297373a894 472 *
mbed_official 579:53297373a894 473 * \param[in] module Pointer to the software module struct
mbed_official 579:53297373a894 474 */
mbed_official 579:53297373a894 475 static inline void i2c_master_enable(
mbed_official 579:53297373a894 476 const struct i2c_master_module *const module)
mbed_official 579:53297373a894 477 {
mbed_official 579:53297373a894 478 /* Sanity check of arguments. */
mbed_official 579:53297373a894 479 Assert(module);
mbed_official 579:53297373a894 480 Assert(module->hw);
mbed_official 579:53297373a894 481
mbed_official 579:53297373a894 482 SercomI2cm *const i2c_module = &(module->hw->I2CM);
mbed_official 579:53297373a894 483
mbed_official 579:53297373a894 484 /* Timeout counter used to force bus state. */
mbed_official 579:53297373a894 485 uint32_t timeout_counter = 0;
mbed_official 579:53297373a894 486
mbed_official 579:53297373a894 487 /* Wait for module to sync. */
mbed_official 579:53297373a894 488 _i2c_master_wait_for_sync(module);
mbed_official 579:53297373a894 489
mbed_official 579:53297373a894 490 /* Enable module. */
mbed_official 579:53297373a894 491 i2c_module->CTRLA.reg |= SERCOM_I2CM_CTRLA_ENABLE;
mbed_official 579:53297373a894 492
mbed_official 579:53297373a894 493 #if I2C_MASTER_CALLBACK_MODE == true
mbed_official 579:53297373a894 494 /* Enable module interrupts */
mbed_official 579:53297373a894 495 system_interrupt_enable(_sercom_get_interrupt_vector(module->hw));
mbed_official 579:53297373a894 496 #endif
mbed_official 579:53297373a894 497 /* Start timeout if bus state is unknown. */
mbed_official 579:53297373a894 498 while (!(i2c_module->STATUS.reg & SERCOM_I2CM_STATUS_BUSSTATE(1))) {
mbed_official 579:53297373a894 499 timeout_counter++;
mbed_official 579:53297373a894 500 if(timeout_counter >= (module->unknown_bus_state_timeout)) {
mbed_official 579:53297373a894 501 /* Timeout, force bus state to idle. */
mbed_official 579:53297373a894 502 i2c_module->STATUS.reg = SERCOM_I2CM_STATUS_BUSSTATE(1);
mbed_official 579:53297373a894 503 /* Workaround #1 */
mbed_official 579:53297373a894 504 return;
mbed_official 579:53297373a894 505 }
mbed_official 579:53297373a894 506 }
mbed_official 579:53297373a894 507 }
mbed_official 579:53297373a894 508
mbed_official 579:53297373a894 509 /**
mbed_official 579:53297373a894 510 * \brief Disable the I<SUP>2</SUP>C module
mbed_official 579:53297373a894 511 *
mbed_official 579:53297373a894 512 * Disables the requested I<SUP>2</SUP>C module.
mbed_official 579:53297373a894 513 *
mbed_official 579:53297373a894 514 * \param[in] module Pointer to the software module struct
mbed_official 579:53297373a894 515 */
mbed_official 579:53297373a894 516 static inline void i2c_master_disable(
mbed_official 579:53297373a894 517 const struct i2c_master_module *const module)
mbed_official 579:53297373a894 518 {
mbed_official 579:53297373a894 519 /* Sanity check of arguments. */
mbed_official 579:53297373a894 520 Assert(module);
mbed_official 579:53297373a894 521 Assert(module->hw);
mbed_official 579:53297373a894 522
mbed_official 579:53297373a894 523 SercomI2cm *const i2c_module = &(module->hw->I2CM);
mbed_official 579:53297373a894 524
mbed_official 579:53297373a894 525 /* Wait for module to sync. */
mbed_official 579:53297373a894 526 _i2c_master_wait_for_sync(module);
mbed_official 579:53297373a894 527
mbed_official 579:53297373a894 528 /* Disable module. */
mbed_official 579:53297373a894 529 i2c_module->CTRLA.reg &= ~SERCOM_I2CM_CTRLA_ENABLE;
mbed_official 579:53297373a894 530
mbed_official 579:53297373a894 531 #if I2C_MASTER_CALLBACK_MODE == true
mbed_official 579:53297373a894 532 /* Disable module interrupts */
mbed_official 579:53297373a894 533 system_interrupt_disable(_sercom_get_interrupt_vector(module->hw));
mbed_official 579:53297373a894 534 #endif
mbed_official 579:53297373a894 535 }
mbed_official 579:53297373a894 536
mbed_official 579:53297373a894 537 void i2c_master_reset(struct i2c_master_module *const module);
mbed_official 579:53297373a894 538
mbed_official 579:53297373a894 539 /** @} */
mbed_official 579:53297373a894 540
mbed_official 579:53297373a894 541 /**
mbed_official 579:53297373a894 542 * \name Read and Write
mbed_official 579:53297373a894 543 * @{
mbed_official 579:53297373a894 544 */
mbed_official 579:53297373a894 545
mbed_official 579:53297373a894 546 enum status_code i2c_master_read_packet_wait(
mbed_official 579:53297373a894 547 struct i2c_master_module *const module,
mbed_official 579:53297373a894 548 struct i2c_master_packet *const packet);
mbed_official 579:53297373a894 549
mbed_official 579:53297373a894 550 enum status_code i2c_master_read_packet_wait_no_stop(
mbed_official 579:53297373a894 551 struct i2c_master_module *const module,
mbed_official 579:53297373a894 552 struct i2c_master_packet *const packet);
mbed_official 579:53297373a894 553
mbed_official 579:53297373a894 554 enum status_code i2c_master_write_packet_wait(
mbed_official 579:53297373a894 555 struct i2c_master_module *const module,
mbed_official 579:53297373a894 556 struct i2c_master_packet *const packet);
mbed_official 579:53297373a894 557
mbed_official 579:53297373a894 558 enum status_code i2c_master_write_packet_wait_no_stop(
mbed_official 579:53297373a894 559 struct i2c_master_module *const module,
mbed_official 579:53297373a894 560 struct i2c_master_packet *const packet);
mbed_official 579:53297373a894 561
mbed_official 579:53297373a894 562 void i2c_master_send_stop(struct i2c_master_module *const module);
mbed_official 579:53297373a894 563
mbed_official 579:53297373a894 564 /** @} */
mbed_official 579:53297373a894 565
mbed_official 579:53297373a894 566 #ifdef FEATURE_I2C_DMA_SUPPORT
mbed_official 579:53297373a894 567 /**
mbed_official 579:53297373a894 568 * \name SERCOM I2C Master with DMA Interfaces
mbed_official 579:53297373a894 569 * @{
mbed_official 579:53297373a894 570 */
mbed_official 579:53297373a894 571
mbed_official 579:53297373a894 572 /**
mbed_official 579:53297373a894 573 * \brief Set I<SUP>2</SUP>C for DMA transfer with slave address and transfer size.
mbed_official 579:53297373a894 574 *
mbed_official 579:53297373a894 575 * This function will set the slave address, transfer size and enable the auto transfer
mbed_official 579:53297373a894 576 * mode for DMA.
mbed_official 579:53297373a894 577 *
mbed_official 579:53297373a894 578 * \param[in,out] module Pointer to the driver instance to lock
mbed_official 579:53297373a894 579 * \param[in] addr I<SUP>2</SUP>C slave address
mbed_official 579:53297373a894 580 * \param[in] length I<SUP>2</SUP>C transfer length with DMA
mbed_official 579:53297373a894 581 * \param[in] direction I<SUP>2</SUP>C transfer direction
mbed_official 579:53297373a894 582 *
mbed_official 579:53297373a894 583 */
mbed_official 579:53297373a894 584 static inline void i2c_master_dma_set_transfer(struct i2c_master_module *const module,
mbed_official 579:53297373a894 585 uint16_t addr, uint8_t length, enum i2c_transfer_direction direction)
mbed_official 579:53297373a894 586 {
mbed_official 579:53297373a894 587 module->hw->I2CM.ADDR.reg =
mbed_official 579:53297373a894 588 SERCOM_I2CM_ADDR_ADDR(addr<<1) |
mbed_official 579:53297373a894 589 SERCOM_I2CM_ADDR_LENEN |
mbed_official 579:53297373a894 590 SERCOM_I2CM_ADDR_LEN(length) |
mbed_official 579:53297373a894 591 direction;
mbed_official 579:53297373a894 592 }
mbed_official 579:53297373a894 593
mbed_official 579:53297373a894 594 /** @} */
mbed_official 579:53297373a894 595 #endif
mbed_official 579:53297373a894 596
mbed_official 579:53297373a894 597 /** @} */
mbed_official 579:53297373a894 598
mbed_official 579:53297373a894 599 #ifdef __cplusplus
mbed_official 579:53297373a894 600 }
mbed_official 579:53297373a894 601 #endif
mbed_official 579:53297373a894 602
mbed_official 579:53297373a894 603 #endif /* I2C_MASTER_H_INCLUDED */