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