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

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Thu Nov 08 11:45:42 2018 +0000
Revision:
171:3a7713b1edbc
Parent:
TARGET_SAMD21J18A/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/sercom/i2c/i2c_master.h@111:4336505e4b1c
mbed library. Release version 164

Who changed what in which revision?

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