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.
TARGET_SAMD21G18A/TOOLCHAIN_ARM_MICRO/i2c_slave.h@171:3a7713b1edbc, 2018-11-08 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Nov 08 11:45:42 2018 +0000
- Revision:
- 171:3a7713b1edbc
- Parent:
- TARGET_SAMD21G18A/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/sercom/i2c/i2c_slave.h@111:4336505e4b1c
mbed library. Release version 164
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 111:4336505e4b1c | 1 | /** |
Kojto | 111:4336505e4b1c | 2 | * \file |
Kojto | 111:4336505e4b1c | 3 | * |
Kojto | 111:4336505e4b1c | 4 | * \brief SAM SERCOM I2C Slave Driver |
Kojto | 111:4336505e4b1c | 5 | * |
Kojto | 111:4336505e4b1c | 6 | * Copyright (c) 2013-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_SLAVE_H_INCLUDED |
Kojto | 111:4336505e4b1c | 48 | #define I2C_SLAVE_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_SLAVE_CALLBACK_MODE == true |
Kojto | 111:4336505e4b1c | 55 | # include <sercom_interrupt.h> |
Kojto | 111:4336505e4b1c | 56 | #endif |
Kojto | 111:4336505e4b1c | 57 | |
Kojto | 111:4336505e4b1c | 58 | #ifndef PINMUX_DEFAULT |
Kojto | 111:4336505e4b1c | 59 | # define PINMUX_DEFAULT 0 |
Kojto | 111:4336505e4b1c | 60 | #endif |
Kojto | 111:4336505e4b1c | 61 | |
Kojto | 111:4336505e4b1c | 62 | #ifdef __cplusplus |
Kojto | 111:4336505e4b1c | 63 | extern "C" { |
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 | /** |
Kojto | 111:4336505e4b1c | 74 | * \name I2C Slave Status Flags |
Kojto | 111:4336505e4b1c | 75 | * |
Kojto | 111:4336505e4b1c | 76 | * I<SUP>2</SUP>C slave status flags, returned by \ref i2c_slave_get_status() and cleared |
Kojto | 111:4336505e4b1c | 77 | * by \ref i2c_slave_clear_status(). |
Kojto | 111:4336505e4b1c | 78 | * @{ |
Kojto | 111:4336505e4b1c | 79 | */ |
Kojto | 111:4336505e4b1c | 80 | |
Kojto | 111:4336505e4b1c | 81 | /** Address Match. |
Kojto | 111:4336505e4b1c | 82 | * \note Should only be cleared internally by driver. |
Kojto | 111:4336505e4b1c | 83 | */ |
Kojto | 111:4336505e4b1c | 84 | #define I2C_SLAVE_STATUS_ADDRESS_MATCH (1UL << 0) |
Kojto | 111:4336505e4b1c | 85 | /** Data Ready. */ |
Kojto | 111:4336505e4b1c | 86 | #define I2C_SLAVE_STATUS_DATA_READY (1UL << 1) |
Kojto | 111:4336505e4b1c | 87 | /** Stop Received. */ |
Kojto | 111:4336505e4b1c | 88 | #define I2C_SLAVE_STATUS_STOP_RECEIVED (1UL << 2) |
Kojto | 111:4336505e4b1c | 89 | /** Clock Hold. |
Kojto | 111:4336505e4b1c | 90 | * \note Cannot be cleared, only valid when I2C_SLAVE_STATUS_ADDRESS_MATCH is |
Kojto | 111:4336505e4b1c | 91 | * set. |
Kojto | 111:4336505e4b1c | 92 | */ |
Kojto | 111:4336505e4b1c | 93 | #define I2C_SLAVE_STATUS_CLOCK_HOLD (1UL << 3) |
Kojto | 111:4336505e4b1c | 94 | /** SCL Low Timeout. */ |
Kojto | 111:4336505e4b1c | 95 | #define I2C_SLAVE_STATUS_SCL_LOW_TIMEOUT (1UL << 4) |
Kojto | 111:4336505e4b1c | 96 | /** Repeated Start. |
Kojto | 111:4336505e4b1c | 97 | * \note Cannot be cleared, only valid when I2C_SLAVE_STATUS_ADDRESS_MATCH is |
Kojto | 111:4336505e4b1c | 98 | * set. |
Kojto | 111:4336505e4b1c | 99 | */ |
Kojto | 111:4336505e4b1c | 100 | #define I2C_SLAVE_STATUS_REPEATED_START (1UL << 5) |
Kojto | 111:4336505e4b1c | 101 | /** Received not acknowledge. |
Kojto | 111:4336505e4b1c | 102 | * \note Cannot be cleared. |
Kojto | 111:4336505e4b1c | 103 | */ |
Kojto | 111:4336505e4b1c | 104 | #define I2C_SLAVE_STATUS_RECEIVED_NACK (1UL << 6) |
Kojto | 111:4336505e4b1c | 105 | /** Transmit Collision. */ |
Kojto | 111:4336505e4b1c | 106 | #define I2C_SLAVE_STATUS_COLLISION (1UL << 7) |
Kojto | 111:4336505e4b1c | 107 | /** Bus error. */ |
Kojto | 111:4336505e4b1c | 108 | #define I2C_SLAVE_STATUS_BUS_ERROR (1UL << 8) |
Kojto | 111:4336505e4b1c | 109 | |
Kojto | 111:4336505e4b1c | 110 | /** @} */ |
Kojto | 111:4336505e4b1c | 111 | |
Kojto | 111:4336505e4b1c | 112 | /** |
Kojto | 111:4336505e4b1c | 113 | * \brief I<SUP>2</SUP>C slave packet for read/write |
Kojto | 111:4336505e4b1c | 114 | * |
Kojto | 111:4336505e4b1c | 115 | * Structure to be used when transferring I<SUP>2</SUP>C slave packets. |
Kojto | 111:4336505e4b1c | 116 | */ |
Kojto | 111:4336505e4b1c | 117 | struct i2c_slave_packet { |
Kojto | 111:4336505e4b1c | 118 | /** Length of data array. */ |
Kojto | 111:4336505e4b1c | 119 | uint16_t data_length; |
Kojto | 111:4336505e4b1c | 120 | /** Data array containing all data to be transferred. */ |
Kojto | 111:4336505e4b1c | 121 | uint8_t *data; |
Kojto | 111:4336505e4b1c | 122 | }; |
Kojto | 111:4336505e4b1c | 123 | |
Kojto | 111:4336505e4b1c | 124 | #if I2C_SLAVE_CALLBACK_MODE == true |
Kojto | 111:4336505e4b1c | 125 | /** |
Kojto | 111:4336505e4b1c | 126 | * \brief Callback types |
Kojto | 111:4336505e4b1c | 127 | * |
Kojto | 111:4336505e4b1c | 128 | * The available callback types for the I<SUP>2</SUP>C slave. |
Kojto | 111:4336505e4b1c | 129 | */ |
Kojto | 111:4336505e4b1c | 130 | enum i2c_slave_callback { |
Kojto | 111:4336505e4b1c | 131 | /** Callback for packet write complete. */ |
Kojto | 111:4336505e4b1c | 132 | I2C_SLAVE_CALLBACK_WRITE_COMPLETE, |
Kojto | 111:4336505e4b1c | 133 | /** Callback for packet read complete. */ |
Kojto | 111:4336505e4b1c | 134 | I2C_SLAVE_CALLBACK_READ_COMPLETE, |
Kojto | 111:4336505e4b1c | 135 | /** |
Kojto | 111:4336505e4b1c | 136 | * Callback for read request from master - can be used to |
Kojto | 111:4336505e4b1c | 137 | * issue a write. |
Kojto | 111:4336505e4b1c | 138 | */ |
Kojto | 111:4336505e4b1c | 139 | I2C_SLAVE_CALLBACK_READ_REQUEST, |
Kojto | 111:4336505e4b1c | 140 | /** |
Kojto | 111:4336505e4b1c | 141 | * Callback for write request from master - can be used to issue a read. |
Kojto | 111:4336505e4b1c | 142 | */ |
Kojto | 111:4336505e4b1c | 143 | I2C_SLAVE_CALLBACK_WRITE_REQUEST, |
Kojto | 111:4336505e4b1c | 144 | /** Callback for error. */ |
Kojto | 111:4336505e4b1c | 145 | I2C_SLAVE_CALLBACK_ERROR, |
Kojto | 111:4336505e4b1c | 146 | /** |
Kojto | 111:4336505e4b1c | 147 | * Callback for error in last transfer. Discovered on a new address |
Kojto | 111:4336505e4b1c | 148 | * interrupt. |
Kojto | 111:4336505e4b1c | 149 | */ |
Kojto | 111:4336505e4b1c | 150 | I2C_SLAVE_CALLBACK_ERROR_LAST_TRANSFER, |
Kojto | 111:4336505e4b1c | 151 | # if !defined(__DOXYGEN__) |
Kojto | 111:4336505e4b1c | 152 | /** Total number of callbacks. */ |
Kojto | 111:4336505e4b1c | 153 | _I2C_SLAVE_CALLBACK_N, |
Kojto | 111:4336505e4b1c | 154 | # endif |
Kojto | 111:4336505e4b1c | 155 | }; |
Kojto | 111:4336505e4b1c | 156 | |
Kojto | 111:4336505e4b1c | 157 | # if !defined(__DOXYGEN__) |
Kojto | 111:4336505e4b1c | 158 | /** Software module prototype. */ |
Kojto | 111:4336505e4b1c | 159 | struct i2c_slave_module; |
Kojto | 111:4336505e4b1c | 160 | |
Kojto | 111:4336505e4b1c | 161 | /** Callback type. */ |
Kojto | 111:4336505e4b1c | 162 | typedef void (*i2c_slave_callback_t)( |
Kojto | 111:4336505e4b1c | 163 | struct i2c_slave_module *const module); |
Kojto | 111:4336505e4b1c | 164 | # endif |
Kojto | 111:4336505e4b1c | 165 | #endif |
Kojto | 111:4336505e4b1c | 166 | |
Kojto | 111:4336505e4b1c | 167 | /** |
Kojto | 111:4336505e4b1c | 168 | * \brief Enum for the possible SDA hold times with respect to the negative |
Kojto | 111:4336505e4b1c | 169 | * edge of SCL |
Kojto | 111:4336505e4b1c | 170 | * |
Kojto | 111:4336505e4b1c | 171 | * Enum for the possible SDA hold times with respect to the negative edge |
Kojto | 111:4336505e4b1c | 172 | * of SCL. |
Kojto | 111:4336505e4b1c | 173 | */ |
Kojto | 111:4336505e4b1c | 174 | enum i2c_slave_sda_hold_time { |
Kojto | 111:4336505e4b1c | 175 | /** SDA hold time disabled. */ |
Kojto | 111:4336505e4b1c | 176 | I2C_SLAVE_SDA_HOLD_TIME_DISABLED = |
Kojto | 111:4336505e4b1c | 177 | ((SERCOM_I2CS_CTRLA_SDAHOLD_Msk & ((0) << SERCOM_I2CS_CTRLA_SDAHOLD_Pos))), |
Kojto | 111:4336505e4b1c | 178 | /** SDA hold time 50ns - 100ns. */ |
Kojto | 111:4336505e4b1c | 179 | I2C_SLAVE_SDA_HOLD_TIME_50NS_100NS = |
Kojto | 111:4336505e4b1c | 180 | ((SERCOM_I2CS_CTRLA_SDAHOLD_Msk & ((1) << SERCOM_I2CS_CTRLA_SDAHOLD_Pos))), |
Kojto | 111:4336505e4b1c | 181 | /** SDA hold time 300ns - 600ns. */ |
Kojto | 111:4336505e4b1c | 182 | I2C_SLAVE_SDA_HOLD_TIME_300NS_600NS = |
Kojto | 111:4336505e4b1c | 183 | ((SERCOM_I2CS_CTRLA_SDAHOLD_Msk & ((2) << SERCOM_I2CS_CTRLA_SDAHOLD_Pos))), |
Kojto | 111:4336505e4b1c | 184 | /** SDA hold time 400ns - 800ns. */ |
Kojto | 111:4336505e4b1c | 185 | I2C_SLAVE_SDA_HOLD_TIME_400NS_800NS = |
Kojto | 111:4336505e4b1c | 186 | ((SERCOM_I2CS_CTRLA_SDAHOLD_Msk & ((3) << SERCOM_I2CS_CTRLA_SDAHOLD_Pos))), |
Kojto | 111:4336505e4b1c | 187 | }; |
Kojto | 111:4336505e4b1c | 188 | |
Kojto | 111:4336505e4b1c | 189 | /** |
Kojto | 111:4336505e4b1c | 190 | * \brief Enum for the possible address modes |
Kojto | 111:4336505e4b1c | 191 | * |
Kojto | 111:4336505e4b1c | 192 | * Enum for the possible address modes. |
Kojto | 111:4336505e4b1c | 193 | */ |
Kojto | 111:4336505e4b1c | 194 | enum i2c_slave_address_mode { |
Kojto | 111:4336505e4b1c | 195 | /** Address match on address_mask used as a mask to address. */ |
Kojto | 111:4336505e4b1c | 196 | I2C_SLAVE_ADDRESS_MODE_MASK = SERCOM_I2CS_CTRLB_AMODE(0), |
Kojto | 111:4336505e4b1c | 197 | /** Address math on both address and address_mask. */ |
Kojto | 111:4336505e4b1c | 198 | I2C_SLAVE_ADDRESS_MODE_TWO_ADDRESSES = SERCOM_I2CS_CTRLB_AMODE(1), |
Kojto | 111:4336505e4b1c | 199 | /** |
Kojto | 111:4336505e4b1c | 200 | * Address match on range of addresses between and including address and |
Kojto | 111:4336505e4b1c | 201 | * address_mask. |
Kojto | 111:4336505e4b1c | 202 | */ |
Kojto | 111:4336505e4b1c | 203 | I2C_SLAVE_ADDRESS_MODE_RANGE = SERCOM_I2CS_CTRLB_AMODE(2), |
Kojto | 111:4336505e4b1c | 204 | }; |
Kojto | 111:4336505e4b1c | 205 | |
Kojto | 111:4336505e4b1c | 206 | /** |
Kojto | 111:4336505e4b1c | 207 | * \brief Enum for the direction of a request |
Kojto | 111:4336505e4b1c | 208 | * |
Kojto | 111:4336505e4b1c | 209 | * Enum for the direction of a request. |
Kojto | 111:4336505e4b1c | 210 | */ |
Kojto | 111:4336505e4b1c | 211 | enum i2c_slave_direction { |
Kojto | 111:4336505e4b1c | 212 | /** Read. */ |
Kojto | 111:4336505e4b1c | 213 | I2C_SLAVE_DIRECTION_READ, |
Kojto | 111:4336505e4b1c | 214 | /** Write. */ |
Kojto | 111:4336505e4b1c | 215 | I2C_SLAVE_DIRECTION_WRITE, |
Kojto | 111:4336505e4b1c | 216 | /** No direction. */ |
Kojto | 111:4336505e4b1c | 217 | I2C_SLAVE_DIRECTION_NONE, |
Kojto | 111:4336505e4b1c | 218 | }; |
Kojto | 111:4336505e4b1c | 219 | |
Kojto | 111:4336505e4b1c | 220 | #ifdef FEATURE_I2C_FAST_MODE_PLUS_AND_HIGH_SPEED |
Kojto | 111:4336505e4b1c | 221 | /** |
Kojto | 111:4336505e4b1c | 222 | * \brief Enum for the transfer speed |
Kojto | 111:4336505e4b1c | 223 | * |
Kojto | 111:4336505e4b1c | 224 | * Enum for the transfer speed. |
Kojto | 111:4336505e4b1c | 225 | */ |
Kojto | 111:4336505e4b1c | 226 | enum i2c_slave_transfer_speed { |
Kojto | 111:4336505e4b1c | 227 | /** Standard-mode (Sm) up to 100KHz and Fast-mode (Fm) up to 400KHz. */ |
Kojto | 111:4336505e4b1c | 228 | I2C_SLAVE_SPEED_STANDARD_AND_FAST = SERCOM_I2CS_CTRLA_SPEED(0), |
Kojto | 111:4336505e4b1c | 229 | /** Fast-mode Plus (Fm+) up to 1MHz. */ |
Kojto | 111:4336505e4b1c | 230 | I2C_SLAVE_SPEED_FAST_MODE_PLUS = SERCOM_I2CS_CTRLA_SPEED(1), |
Kojto | 111:4336505e4b1c | 231 | /** High-speed mode (Hs-mode) up to 3.4MHz. */ |
Kojto | 111:4336505e4b1c | 232 | I2C_SLAVE_SPEED_HIGH_SPEED = SERCOM_I2CS_CTRLA_SPEED(2), |
Kojto | 111:4336505e4b1c | 233 | }; |
Kojto | 111:4336505e4b1c | 234 | #endif |
Kojto | 111:4336505e4b1c | 235 | |
Kojto | 111:4336505e4b1c | 236 | /** |
Kojto | 111:4336505e4b1c | 237 | * \brief SERCOM I<SUP>2</SUP>C Slave driver software device instance structure. |
Kojto | 111:4336505e4b1c | 238 | * |
Kojto | 111:4336505e4b1c | 239 | * SERCOM I<SUP>2</SUP>C Slave driver software instance structure, used to |
Kojto | 111:4336505e4b1c | 240 | * retain software state information of an associated hardware module instance. |
Kojto | 111:4336505e4b1c | 241 | * |
Kojto | 111:4336505e4b1c | 242 | * \note The fields of this structure should not be altered by the user |
Kojto | 111:4336505e4b1c | 243 | * application; they are reserved for module-internal use only. |
Kojto | 111:4336505e4b1c | 244 | */ |
Kojto | 111:4336505e4b1c | 245 | struct i2c_slave_module { |
Kojto | 111:4336505e4b1c | 246 | #if !defined(__DOXYGEN__) |
Kojto | 111:4336505e4b1c | 247 | /** Hardware instance initialized for the struct. */ |
Kojto | 111:4336505e4b1c | 248 | Sercom *hw; |
Kojto | 111:4336505e4b1c | 249 | /** Module lock. */ |
Kojto | 111:4336505e4b1c | 250 | volatile bool locked; |
Kojto | 111:4336505e4b1c | 251 | /** Timeout value for polled functions. */ |
Kojto | 111:4336505e4b1c | 252 | uint16_t buffer_timeout; |
Kojto | 111:4336505e4b1c | 253 | # ifdef FEATURE_I2C_10_BIT_ADDRESS |
Kojto | 111:4336505e4b1c | 254 | /** Using 10-bit addressing for the slave. */ |
Kojto | 111:4336505e4b1c | 255 | bool ten_bit_address; |
Kojto | 111:4336505e4b1c | 256 | # endif |
Kojto | 111:4336505e4b1c | 257 | # if I2C_SLAVE_CALLBACK_MODE == true |
Kojto | 111:4336505e4b1c | 258 | /** Nack on address match. */ |
Kojto | 111:4336505e4b1c | 259 | bool nack_on_address; |
Kojto | 111:4336505e4b1c | 260 | /** Pointers to callback functions. */ |
Kojto | 111:4336505e4b1c | 261 | volatile i2c_slave_callback_t callbacks[_I2C_SLAVE_CALLBACK_N]; |
Kojto | 111:4336505e4b1c | 262 | /** Mask for registered callbacks. */ |
Kojto | 111:4336505e4b1c | 263 | volatile uint8_t registered_callback; |
Kojto | 111:4336505e4b1c | 264 | /** Mask for enabled callbacks. */ |
Kojto | 111:4336505e4b1c | 265 | volatile uint8_t enabled_callback; |
Kojto | 111:4336505e4b1c | 266 | /** The total number of bytes to transfer. */ |
Kojto | 111:4336505e4b1c | 267 | volatile uint16_t buffer_length; |
Kojto | 111:4336505e4b1c | 268 | /** |
Kojto | 111:4336505e4b1c | 269 | * Counter used for bytes left to send in write and to count number of |
Kojto | 111:4336505e4b1c | 270 | * obtained bytes in read. |
Kojto | 111:4336505e4b1c | 271 | */ |
Kojto | 111:4336505e4b1c | 272 | uint16_t buffer_remaining; |
Kojto | 111:4336505e4b1c | 273 | /** Data buffer for packet write and read. */ |
Kojto | 111:4336505e4b1c | 274 | volatile uint8_t *buffer; |
Kojto | 111:4336505e4b1c | 275 | /** Save direction of request from master. 1 = read, 0 = write. */ |
Kojto | 111:4336505e4b1c | 276 | volatile enum i2c_transfer_direction transfer_direction; |
Kojto | 111:4336505e4b1c | 277 | /** Status for status read back in error callback. */ |
Kojto | 111:4336505e4b1c | 278 | volatile enum status_code status; |
Kojto | 111:4336505e4b1c | 279 | # endif |
Kojto | 111:4336505e4b1c | 280 | #endif |
Kojto | 111:4336505e4b1c | 281 | }; |
Kojto | 111:4336505e4b1c | 282 | |
Kojto | 111:4336505e4b1c | 283 | /** |
Kojto | 111:4336505e4b1c | 284 | * \brief Configuration structure for the I<SUP>2</SUP>C Slave device |
Kojto | 111:4336505e4b1c | 285 | * |
Kojto | 111:4336505e4b1c | 286 | * This is the configuration structure for the I<SUP>2</SUP>C Slave device. It is used |
Kojto | 111:4336505e4b1c | 287 | * as an argument for \ref i2c_slave_init to provide the desired |
Kojto | 111:4336505e4b1c | 288 | * configurations for the module. The structure should be initialized using the |
Kojto | 111:4336505e4b1c | 289 | * \ref i2c_slave_get_config_defaults. |
Kojto | 111:4336505e4b1c | 290 | */ |
Kojto | 111:4336505e4b1c | 291 | struct i2c_slave_config { |
Kojto | 111:4336505e4b1c | 292 | /** Set to enable the SCL low timeout. */ |
Kojto | 111:4336505e4b1c | 293 | bool enable_scl_low_timeout; |
Kojto | 111:4336505e4b1c | 294 | /** SDA hold time with respect to the negative edge of SCL. */ |
Kojto | 111:4336505e4b1c | 295 | enum i2c_slave_sda_hold_time sda_hold_time; |
Kojto | 111:4336505e4b1c | 296 | /** Timeout to wait for master in polled functions. */ |
Kojto | 111:4336505e4b1c | 297 | uint16_t buffer_timeout; |
Kojto | 111:4336505e4b1c | 298 | /** Addressing mode. */ |
Kojto | 111:4336505e4b1c | 299 | enum i2c_slave_address_mode address_mode; |
Kojto | 111:4336505e4b1c | 300 | /** Address or upper limit of address range. */ |
Kojto | 111:4336505e4b1c | 301 | uint16_t address; |
Kojto | 111:4336505e4b1c | 302 | /** Address mask, second address or lower limit of address range. */ |
Kojto | 111:4336505e4b1c | 303 | uint16_t address_mask; |
Kojto | 111:4336505e4b1c | 304 | #ifdef FEATURE_I2C_10_BIT_ADDRESS |
Kojto | 111:4336505e4b1c | 305 | /** Enable 10-bit addressing. */ |
Kojto | 111:4336505e4b1c | 306 | bool ten_bit_address; |
Kojto | 111:4336505e4b1c | 307 | #endif |
Kojto | 111:4336505e4b1c | 308 | /** |
Kojto | 111:4336505e4b1c | 309 | * Enable general call address recognition (general call address |
Kojto | 111:4336505e4b1c | 310 | * is defined as 0000000 with direction bit 0). |
Kojto | 111:4336505e4b1c | 311 | */ |
Kojto | 111:4336505e4b1c | 312 | bool enable_general_call_address; |
Kojto | 111:4336505e4b1c | 313 | |
Kojto | 111:4336505e4b1c | 314 | #ifdef FEATURE_I2C_FAST_MODE_PLUS_AND_HIGH_SPEED |
Kojto | 111:4336505e4b1c | 315 | /** Transfer speed mode. */ |
Kojto | 111:4336505e4b1c | 316 | enum i2c_slave_transfer_speed transfer_speed; |
Kojto | 111:4336505e4b1c | 317 | #endif |
Kojto | 111:4336505e4b1c | 318 | |
Kojto | 111:4336505e4b1c | 319 | #if I2C_SLAVE_CALLBACK_MODE == true |
Kojto | 111:4336505e4b1c | 320 | /** |
Kojto | 111:4336505e4b1c | 321 | * Enable NACK on address match (this can be changed after initialization |
Kojto | 111:4336505e4b1c | 322 | * via the \ref i2c_slave_enable_nack_on_address and |
Kojto | 111:4336505e4b1c | 323 | * \ref i2c_slave_disable_nack_on_address functions). |
Kojto | 111:4336505e4b1c | 324 | */ |
Kojto | 111:4336505e4b1c | 325 | bool enable_nack_on_address; |
Kojto | 111:4336505e4b1c | 326 | #endif |
Kojto | 111:4336505e4b1c | 327 | /** GCLK generator to use as clock source. */ |
Kojto | 111:4336505e4b1c | 328 | enum gclk_generator generator_source; |
Kojto | 111:4336505e4b1c | 329 | /** Set to keep module active in sleep modes. */ |
Kojto | 111:4336505e4b1c | 330 | bool run_in_standby; |
Kojto | 111:4336505e4b1c | 331 | /** PAD0 (SDA) pinmux. */ |
Kojto | 111:4336505e4b1c | 332 | uint32_t pinmux_pad0; |
Kojto | 111:4336505e4b1c | 333 | /** PAD1 (SCL) pinmux. */ |
Kojto | 111:4336505e4b1c | 334 | uint32_t pinmux_pad1; |
Kojto | 111:4336505e4b1c | 335 | /** Set to enable SCL low time-out. */ |
Kojto | 111:4336505e4b1c | 336 | bool scl_low_timeout; |
Kojto | 111:4336505e4b1c | 337 | #ifdef FEATURE_I2C_SCL_STRETCH_MODE |
Kojto | 111:4336505e4b1c | 338 | /** Set to enable SCL stretch only after ACK bit (required for high speed). */ |
Kojto | 111:4336505e4b1c | 339 | bool scl_stretch_only_after_ack_bit; |
Kojto | 111:4336505e4b1c | 340 | #endif |
Kojto | 111:4336505e4b1c | 341 | #ifdef FEATURE_I2C_SCL_EXTEND_TIMEOUT |
Kojto | 111:4336505e4b1c | 342 | /** Set to enable slave SCL low extend time-out. */ |
Kojto | 111:4336505e4b1c | 343 | bool slave_scl_low_extend_timeout; |
Kojto | 111:4336505e4b1c | 344 | #endif |
Kojto | 111:4336505e4b1c | 345 | }; |
Kojto | 111:4336505e4b1c | 346 | |
Kojto | 111:4336505e4b1c | 347 | |
Kojto | 111:4336505e4b1c | 348 | /** |
Kojto | 111:4336505e4b1c | 349 | * \name Lock/Unlock |
Kojto | 111:4336505e4b1c | 350 | * @{ |
Kojto | 111:4336505e4b1c | 351 | */ |
Kojto | 111:4336505e4b1c | 352 | |
Kojto | 111:4336505e4b1c | 353 | /** |
Kojto | 111:4336505e4b1c | 354 | * \brief Attempt to get lock on driver instance |
Kojto | 111:4336505e4b1c | 355 | * |
Kojto | 111:4336505e4b1c | 356 | * This function checks the instance's lock, which indicates whether or not it |
Kojto | 111:4336505e4b1c | 357 | * is currently in use, and sets the lock if it was not already set. |
Kojto | 111:4336505e4b1c | 358 | * |
Kojto | 111:4336505e4b1c | 359 | * The purpose of this is to enable exclusive access to driver instances, so |
Kojto | 111:4336505e4b1c | 360 | * that, e.g., transactions by different services will not interfere with each |
Kojto | 111:4336505e4b1c | 361 | * other. |
Kojto | 111:4336505e4b1c | 362 | * |
Kojto | 111:4336505e4b1c | 363 | * \param[in,out] module Pointer to the driver instance to lock |
Kojto | 111:4336505e4b1c | 364 | * |
Kojto | 111:4336505e4b1c | 365 | * \retval STATUS_OK If the module was locked |
Kojto | 111:4336505e4b1c | 366 | * \retval STATUS_BUSY If the module was already locked |
Kojto | 111:4336505e4b1c | 367 | */ |
Kojto | 111:4336505e4b1c | 368 | static inline enum status_code i2c_slave_lock( |
Kojto | 111:4336505e4b1c | 369 | struct i2c_slave_module *const module) |
Kojto | 111:4336505e4b1c | 370 | { |
Kojto | 111:4336505e4b1c | 371 | enum status_code status; |
Kojto | 111:4336505e4b1c | 372 | |
Kojto | 111:4336505e4b1c | 373 | system_interrupt_enter_critical_section(); |
Kojto | 111:4336505e4b1c | 374 | |
Kojto | 111:4336505e4b1c | 375 | if (module->locked) { |
Kojto | 111:4336505e4b1c | 376 | status = STATUS_BUSY; |
Kojto | 111:4336505e4b1c | 377 | } else { |
Kojto | 111:4336505e4b1c | 378 | module->locked = true; |
Kojto | 111:4336505e4b1c | 379 | status = STATUS_OK; |
Kojto | 111:4336505e4b1c | 380 | } |
Kojto | 111:4336505e4b1c | 381 | |
Kojto | 111:4336505e4b1c | 382 | system_interrupt_leave_critical_section(); |
Kojto | 111:4336505e4b1c | 383 | |
Kojto | 111:4336505e4b1c | 384 | return status; |
Kojto | 111:4336505e4b1c | 385 | } |
Kojto | 111:4336505e4b1c | 386 | |
Kojto | 111:4336505e4b1c | 387 | /** |
Kojto | 111:4336505e4b1c | 388 | * \brief Unlock driver instance |
Kojto | 111:4336505e4b1c | 389 | * |
Kojto | 111:4336505e4b1c | 390 | * This function clears the instance lock, indicating that it is available for |
Kojto | 111:4336505e4b1c | 391 | * use. |
Kojto | 111:4336505e4b1c | 392 | * |
Kojto | 111:4336505e4b1c | 393 | * \param[in,out] module Pointer to the driver instance to lock |
Kojto | 111:4336505e4b1c | 394 | * |
Kojto | 111:4336505e4b1c | 395 | * \retval STATUS_OK If the module was locked |
Kojto | 111:4336505e4b1c | 396 | * \retval STATUS_BUSY If the module was already locked |
Kojto | 111:4336505e4b1c | 397 | */ |
Kojto | 111:4336505e4b1c | 398 | static inline void i2c_slave_unlock(struct i2c_slave_module *const module) |
Kojto | 111:4336505e4b1c | 399 | { |
Kojto | 111:4336505e4b1c | 400 | module->locked = false; |
Kojto | 111:4336505e4b1c | 401 | } |
Kojto | 111:4336505e4b1c | 402 | |
Kojto | 111:4336505e4b1c | 403 | /** @} */ |
Kojto | 111:4336505e4b1c | 404 | |
Kojto | 111:4336505e4b1c | 405 | /** |
Kojto | 111:4336505e4b1c | 406 | * \name Configuration and Initialization |
Kojto | 111:4336505e4b1c | 407 | * @{ |
Kojto | 111:4336505e4b1c | 408 | */ |
Kojto | 111:4336505e4b1c | 409 | |
Kojto | 111:4336505e4b1c | 410 | /** |
Kojto | 111:4336505e4b1c | 411 | * \brief Returns the synchronization status of the module |
Kojto | 111:4336505e4b1c | 412 | * |
Kojto | 111:4336505e4b1c | 413 | * Returns the synchronization status of the module. |
Kojto | 111:4336505e4b1c | 414 | * |
Kojto | 111:4336505e4b1c | 415 | * \param[out] module Pointer to software module structure |
Kojto | 111:4336505e4b1c | 416 | * |
Kojto | 111:4336505e4b1c | 417 | * \return Status of the synchronization. |
Kojto | 111:4336505e4b1c | 418 | * \retval true Module is busy synchronizing |
Kojto | 111:4336505e4b1c | 419 | * \retval false Module is not synchronizing |
Kojto | 111:4336505e4b1c | 420 | */ |
Kojto | 111:4336505e4b1c | 421 | static inline bool i2c_slave_is_syncing( |
Kojto | 111:4336505e4b1c | 422 | const struct i2c_slave_module *const module) |
Kojto | 111:4336505e4b1c | 423 | { |
Kojto | 111:4336505e4b1c | 424 | /* Sanity check */ |
Kojto | 111:4336505e4b1c | 425 | Assert(module); |
Kojto | 111:4336505e4b1c | 426 | Assert(module->hw); |
Kojto | 111:4336505e4b1c | 427 | |
Kojto | 111:4336505e4b1c | 428 | SercomI2cs *const i2c_hw = &(module->hw->I2CS); |
Kojto | 111:4336505e4b1c | 429 | |
Kojto | 111:4336505e4b1c | 430 | /* Return sync status */ |
Kojto | 111:4336505e4b1c | 431 | #if defined(FEATURE_SERCOM_SYNCBUSY_SCHEME_VERSION_1) |
Kojto | 111:4336505e4b1c | 432 | return (i2c_hw->STATUS.reg & SERCOM_I2CS_STATUS_SYNCBUSY); |
Kojto | 111:4336505e4b1c | 433 | #elif defined(FEATURE_SERCOM_SYNCBUSY_SCHEME_VERSION_2) |
Kojto | 111:4336505e4b1c | 434 | return (i2c_hw->SYNCBUSY.reg & SERCOM_I2CS_SYNCBUSY_MASK); |
Kojto | 111:4336505e4b1c | 435 | #else |
Kojto | 111:4336505e4b1c | 436 | # error Unknown SERCOM SYNCBUSY scheme! |
Kojto | 111:4336505e4b1c | 437 | #endif |
Kojto | 111:4336505e4b1c | 438 | } |
Kojto | 111:4336505e4b1c | 439 | |
Kojto | 111:4336505e4b1c | 440 | #if !defined(__DOXYGEN__) |
Kojto | 111:4336505e4b1c | 441 | /** |
Kojto | 111:4336505e4b1c | 442 | * \internal Wait for hardware module to sync |
Kojto | 111:4336505e4b1c | 443 | * |
Kojto | 111:4336505e4b1c | 444 | * \param[in] module Pointer to software module structure |
Kojto | 111:4336505e4b1c | 445 | */ |
Kojto | 111:4336505e4b1c | 446 | static void _i2c_slave_wait_for_sync( |
Kojto | 111:4336505e4b1c | 447 | const struct i2c_slave_module *const module) |
Kojto | 111:4336505e4b1c | 448 | { |
Kojto | 111:4336505e4b1c | 449 | /* Sanity check. */ |
Kojto | 111:4336505e4b1c | 450 | Assert(module); |
Kojto | 111:4336505e4b1c | 451 | |
Kojto | 111:4336505e4b1c | 452 | while (i2c_slave_is_syncing(module)) { |
Kojto | 111:4336505e4b1c | 453 | /* Wait for I2C module to sync */ |
Kojto | 111:4336505e4b1c | 454 | } |
Kojto | 111:4336505e4b1c | 455 | } |
Kojto | 111:4336505e4b1c | 456 | #endif |
Kojto | 111:4336505e4b1c | 457 | |
Kojto | 111:4336505e4b1c | 458 | ///@cond INTERNAL |
Kojto | 111:4336505e4b1c | 459 | /** |
Kojto | 111:4336505e4b1c | 460 | * \internal Workaround for errata 13574 |
Kojto | 111:4336505e4b1c | 461 | * Instead set ACK/NACK of CTRLB |
Kojto | 111:4336505e4b1c | 462 | * |
Kojto | 111:4336505e4b1c | 463 | * This errata exist in part revisions of SAMD20/D21 |
Kojto | 111:4336505e4b1c | 464 | * D10/D11/L21/DAx/C20/C21, but workaround can be works in all |
Kojto | 111:4336505e4b1c | 465 | * revision of those device. As this function operation |
Kojto | 111:4336505e4b1c | 466 | * should be use less cpu time as possible, so caller |
Kojto | 111:4336505e4b1c | 467 | * function can ignore to check revision number, and use |
Kojto | 111:4336505e4b1c | 468 | * this workaround in all revision of those device. |
Kojto | 111:4336505e4b1c | 469 | * |
Kojto | 111:4336505e4b1c | 470 | * \param[in,out] module Pointer to software module structure |
Kojto | 111:4336505e4b1c | 471 | * \param[in] send_ack true send ACK, false send NACK |
Kojto | 111:4336505e4b1c | 472 | */ |
Kojto | 111:4336505e4b1c | 473 | static inline void _i2c_slave_set_ctrlb_ackact( |
Kojto | 111:4336505e4b1c | 474 | struct i2c_slave_module *const module, |
Kojto | 111:4336505e4b1c | 475 | bool send_ack) |
Kojto | 111:4336505e4b1c | 476 | { |
Kojto | 111:4336505e4b1c | 477 | Assert(module); |
Kojto | 111:4336505e4b1c | 478 | Assert(module->hw); |
Kojto | 111:4336505e4b1c | 479 | |
Kojto | 111:4336505e4b1c | 480 | SercomI2cs *const i2c_hw = &(module->hw->I2CS); |
Kojto | 111:4336505e4b1c | 481 | |
Kojto | 111:4336505e4b1c | 482 | #if (SAMD20 || SAMD21 || SAMD10 || SAMD11 || SAML21 || SAMDA1 || SAMC20 || SAMC21) |
Kojto | 111:4336505e4b1c | 483 | /* Workaround, Following two write are atomic */ |
Kojto | 111:4336505e4b1c | 484 | system_interrupt_enter_critical_section(); |
Kojto | 111:4336505e4b1c | 485 | i2c_hw->STATUS.reg = 0; |
Kojto | 111:4336505e4b1c | 486 | |
Kojto | 111:4336505e4b1c | 487 | if (send_ack == true) { |
Kojto | 111:4336505e4b1c | 488 | i2c_hw->CTRLB.reg = 0; |
Kojto | 111:4336505e4b1c | 489 | } else { |
Kojto | 111:4336505e4b1c | 490 | i2c_hw->CTRLB.reg = SERCOM_I2CS_CTRLB_ACKACT; |
Kojto | 111:4336505e4b1c | 491 | } |
Kojto | 111:4336505e4b1c | 492 | system_interrupt_leave_critical_section(); |
Kojto | 111:4336505e4b1c | 493 | #else |
Kojto | 111:4336505e4b1c | 494 | /* Normal operation */ |
Kojto | 111:4336505e4b1c | 495 | if (send_ack == true) { |
Kojto | 111:4336505e4b1c | 496 | i2c_hw->CTRLB.reg &= ~SERCOM_I2CS_CTRLB_ACKACT; |
Kojto | 111:4336505e4b1c | 497 | } else { |
Kojto | 111:4336505e4b1c | 498 | i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_ACKACT; |
Kojto | 111:4336505e4b1c | 499 | } |
Kojto | 111:4336505e4b1c | 500 | #endif |
Kojto | 111:4336505e4b1c | 501 | return; |
Kojto | 111:4336505e4b1c | 502 | } |
Kojto | 111:4336505e4b1c | 503 | |
Kojto | 111:4336505e4b1c | 504 | /** |
Kojto | 111:4336505e4b1c | 505 | * \internal Workaround for SAM0 errata 13574, |
Kojto | 111:4336505e4b1c | 506 | * instead Set CMD3 of CTRLB |
Kojto | 111:4336505e4b1c | 507 | * |
Kojto | 111:4336505e4b1c | 508 | * This errata exist in part revisions of SAMD20/D21 |
Kojto | 111:4336505e4b1c | 509 | * D10/D11/L21/DAx/C20/C21, but workaround can be works in all |
Kojto | 111:4336505e4b1c | 510 | * revision of those device. As this function operation |
Kojto | 111:4336505e4b1c | 511 | * should be use less cpu time as possible, so caller |
Kojto | 111:4336505e4b1c | 512 | * function can ignore to check revision number, and use |
Kojto | 111:4336505e4b1c | 513 | * this workaround in all revision of those device. |
Kojto | 111:4336505e4b1c | 514 | * |
Kojto | 111:4336505e4b1c | 515 | * \param[in,out] module Pointer to software module structure |
Kojto | 111:4336505e4b1c | 516 | */ |
Kojto | 111:4336505e4b1c | 517 | static inline void _i2c_slave_set_ctrlb_cmd3( |
Kojto | 111:4336505e4b1c | 518 | struct i2c_slave_module *const module) |
Kojto | 111:4336505e4b1c | 519 | { |
Kojto | 111:4336505e4b1c | 520 | Assert(module); |
Kojto | 111:4336505e4b1c | 521 | Assert(module->hw); |
Kojto | 111:4336505e4b1c | 522 | |
Kojto | 111:4336505e4b1c | 523 | SercomI2cs *const i2c_hw = &(module->hw->I2CS); |
Kojto | 111:4336505e4b1c | 524 | |
Kojto | 111:4336505e4b1c | 525 | #if (SAMD20 || SAMD21 || SAMD10 || SAMD11 || SAML21 || SAMDA1 || SAMC20 || SAMC21) |
Kojto | 111:4336505e4b1c | 526 | /* Workaround */ |
Kojto | 111:4336505e4b1c | 527 | /* |
Kojto | 111:4336505e4b1c | 528 | * Below code instead i2c_hw->CTRLB.reg = SERCOM_I2CS_CTRLB_CMD(0x3); |
Kojto | 111:4336505e4b1c | 529 | * CMD=0x3 clears all interrupts, so to keep the result similar |
Kojto | 111:4336505e4b1c | 530 | * PREC is cleared if it was set |
Kojto | 111:4336505e4b1c | 531 | */ |
Kojto | 111:4336505e4b1c | 532 | if (i2c_hw->INTFLAG.bit.PREC) { |
Kojto | 111:4336505e4b1c | 533 | i2c_hw->INTFLAG.reg = SERCOM_I2CS_INTFLAG_PREC; |
Kojto | 111:4336505e4b1c | 534 | } |
Kojto | 111:4336505e4b1c | 535 | i2c_hw->INTFLAG.reg = SERCOM_I2CS_INTFLAG_AMATCH; |
Kojto | 111:4336505e4b1c | 536 | #else |
Kojto | 111:4336505e4b1c | 537 | /* Normal operation */ |
Kojto | 111:4336505e4b1c | 538 | i2c_hw->CTRLB.reg = SERCOM_I2CS_CTRLB_CMD(0x3); |
Kojto | 111:4336505e4b1c | 539 | #endif |
Kojto | 111:4336505e4b1c | 540 | return; |
Kojto | 111:4336505e4b1c | 541 | } |
Kojto | 111:4336505e4b1c | 542 | ///@endcond |
Kojto | 111:4336505e4b1c | 543 | |
Kojto | 111:4336505e4b1c | 544 | /** |
Kojto | 111:4336505e4b1c | 545 | * \brief Gets the I<SUP>2</SUP>C slave default configurations |
Kojto | 111:4336505e4b1c | 546 | * |
Kojto | 111:4336505e4b1c | 547 | * This will initialize the configuration structure to known default values. |
Kojto | 111:4336505e4b1c | 548 | * |
Kojto | 111:4336505e4b1c | 549 | * The default configuration is as follows: |
Kojto | 111:4336505e4b1c | 550 | * - Disable SCL low timeout |
Kojto | 111:4336505e4b1c | 551 | * - 300ns - 600ns SDA hold time |
Kojto | 111:4336505e4b1c | 552 | * - Buffer timeout = 65535 |
Kojto | 111:4336505e4b1c | 553 | * - Address with mask |
Kojto | 111:4336505e4b1c | 554 | * - Address = 0 |
Kojto | 111:4336505e4b1c | 555 | * - Address mask = 0 (one single address) |
Kojto | 111:4336505e4b1c | 556 | * - General call address disabled |
Kojto | 111:4336505e4b1c | 557 | * - Address nack disabled if the interrupt driver is used |
Kojto | 111:4336505e4b1c | 558 | * - GCLK generator 0 |
Kojto | 111:4336505e4b1c | 559 | * - Do not run in standby |
Kojto | 111:4336505e4b1c | 560 | * - PINMUX_DEFAULT for SERCOM pads |
Kojto | 111:4336505e4b1c | 561 | * |
Kojto | 111:4336505e4b1c | 562 | * Those default configuration only available if the device supports it: |
Kojto | 111:4336505e4b1c | 563 | * - Not using 10-bit addressing |
Kojto | 111:4336505e4b1c | 564 | * - Standard-mode and Fast-mode transfer speed |
Kojto | 111:4336505e4b1c | 565 | * - SCL stretch disabled |
Kojto | 111:4336505e4b1c | 566 | * - slave SCL low extend time-out disabled |
Kojto | 111:4336505e4b1c | 567 | * |
Kojto | 111:4336505e4b1c | 568 | * \param[out] config Pointer to configuration structure to be initialized |
Kojto | 111:4336505e4b1c | 569 | */ |
Kojto | 111:4336505e4b1c | 570 | static inline void i2c_slave_get_config_defaults( |
Kojto | 111:4336505e4b1c | 571 | struct i2c_slave_config *const config) |
Kojto | 111:4336505e4b1c | 572 | { |
Kojto | 111:4336505e4b1c | 573 | /*Sanity check argument. */ |
Kojto | 111:4336505e4b1c | 574 | Assert(config); |
Kojto | 111:4336505e4b1c | 575 | config->enable_scl_low_timeout = false; |
Kojto | 111:4336505e4b1c | 576 | config->sda_hold_time = I2C_SLAVE_SDA_HOLD_TIME_300NS_600NS; |
Kojto | 111:4336505e4b1c | 577 | config->buffer_timeout = 65535; |
Kojto | 111:4336505e4b1c | 578 | config->address_mode = I2C_SLAVE_ADDRESS_MODE_MASK; |
Kojto | 111:4336505e4b1c | 579 | config->address = 0; |
Kojto | 111:4336505e4b1c | 580 | config->address_mask = 0; |
Kojto | 111:4336505e4b1c | 581 | #ifdef FEATURE_I2C_10_BIT_ADDRESS |
Kojto | 111:4336505e4b1c | 582 | config->ten_bit_address = false; |
Kojto | 111:4336505e4b1c | 583 | #endif |
Kojto | 111:4336505e4b1c | 584 | config->enable_general_call_address = false; |
Kojto | 111:4336505e4b1c | 585 | #ifdef FEATURE_I2C_FAST_MODE_PLUS_AND_HIGH_SPEED |
Kojto | 111:4336505e4b1c | 586 | config->transfer_speed = I2C_SLAVE_SPEED_STANDARD_AND_FAST; |
Kojto | 111:4336505e4b1c | 587 | #endif |
Kojto | 111:4336505e4b1c | 588 | #if I2C_SLAVE_CALLBACK_MODE == true |
Kojto | 111:4336505e4b1c | 589 | config->enable_nack_on_address = false; |
Kojto | 111:4336505e4b1c | 590 | #endif |
Kojto | 111:4336505e4b1c | 591 | config->generator_source = GCLK_GENERATOR_0; |
Kojto | 111:4336505e4b1c | 592 | config->run_in_standby = false; |
Kojto | 111:4336505e4b1c | 593 | config->pinmux_pad0 = PINMUX_DEFAULT; |
Kojto | 111:4336505e4b1c | 594 | config->pinmux_pad1 = PINMUX_DEFAULT; |
Kojto | 111:4336505e4b1c | 595 | config->scl_low_timeout = false; |
Kojto | 111:4336505e4b1c | 596 | #ifdef FEATURE_I2C_SCL_STRETCH_MODE |
Kojto | 111:4336505e4b1c | 597 | config->scl_stretch_only_after_ack_bit = false; |
Kojto | 111:4336505e4b1c | 598 | #endif |
Kojto | 111:4336505e4b1c | 599 | #ifdef FEATURE_I2C_SCL_EXTEND_TIMEOUT |
Kojto | 111:4336505e4b1c | 600 | config->slave_scl_low_extend_timeout = false; |
Kojto | 111:4336505e4b1c | 601 | #endif |
Kojto | 111:4336505e4b1c | 602 | } |
Kojto | 111:4336505e4b1c | 603 | |
Kojto | 111:4336505e4b1c | 604 | enum status_code i2c_slave_init(struct i2c_slave_module *const module, |
Kojto | 111:4336505e4b1c | 605 | Sercom *const hw, |
Kojto | 111:4336505e4b1c | 606 | const struct i2c_slave_config *const config); |
Kojto | 111:4336505e4b1c | 607 | |
Kojto | 111:4336505e4b1c | 608 | /** |
Kojto | 111:4336505e4b1c | 609 | * \brief Enables the I<SUP>2</SUP>C module |
Kojto | 111:4336505e4b1c | 610 | * |
Kojto | 111:4336505e4b1c | 611 | * This will enable the requested I<SUP>2</SUP>C module. |
Kojto | 111:4336505e4b1c | 612 | * |
Kojto | 111:4336505e4b1c | 613 | * \param[in] module Pointer to the software module struct |
Kojto | 111:4336505e4b1c | 614 | */ |
Kojto | 111:4336505e4b1c | 615 | static inline void i2c_slave_enable( |
Kojto | 111:4336505e4b1c | 616 | const struct i2c_slave_module *const module) |
Kojto | 111:4336505e4b1c | 617 | { |
Kojto | 111:4336505e4b1c | 618 | /* Sanity check of arguments. */ |
Kojto | 111:4336505e4b1c | 619 | Assert(module); |
Kojto | 111:4336505e4b1c | 620 | Assert(module->hw); |
Kojto | 111:4336505e4b1c | 621 | |
Kojto | 111:4336505e4b1c | 622 | SercomI2cs *const i2c_hw = &(module->hw->I2CS); |
Kojto | 111:4336505e4b1c | 623 | |
Kojto | 111:4336505e4b1c | 624 | #if I2C_SLAVE_CALLBACK_MODE == true |
Kojto | 111:4336505e4b1c | 625 | /* Enable global interrupt for module */ |
Kojto | 111:4336505e4b1c | 626 | system_interrupt_enable(_sercom_get_interrupt_vector(module->hw)); |
Kojto | 111:4336505e4b1c | 627 | #endif |
Kojto | 111:4336505e4b1c | 628 | |
Kojto | 111:4336505e4b1c | 629 | /* Wait for module to sync */ |
Kojto | 111:4336505e4b1c | 630 | _i2c_slave_wait_for_sync(module); |
Kojto | 111:4336505e4b1c | 631 | |
Kojto | 111:4336505e4b1c | 632 | /* Enable module */ |
Kojto | 111:4336505e4b1c | 633 | i2c_hw->CTRLA.reg |= SERCOM_I2CS_CTRLA_ENABLE; |
Kojto | 111:4336505e4b1c | 634 | } |
Kojto | 111:4336505e4b1c | 635 | |
Kojto | 111:4336505e4b1c | 636 | |
Kojto | 111:4336505e4b1c | 637 | /** |
Kojto | 111:4336505e4b1c | 638 | * \brief Disables the I<SUP>2</SUP>C module |
Kojto | 111:4336505e4b1c | 639 | * |
Kojto | 111:4336505e4b1c | 640 | * This will disable the I<SUP>2</SUP>C module specified in the provided software module |
Kojto | 111:4336505e4b1c | 641 | * structure. |
Kojto | 111:4336505e4b1c | 642 | * |
Kojto | 111:4336505e4b1c | 643 | * \param[in] module Pointer to the software module struct |
Kojto | 111:4336505e4b1c | 644 | */ |
Kojto | 111:4336505e4b1c | 645 | static inline void i2c_slave_disable( |
Kojto | 111:4336505e4b1c | 646 | const struct i2c_slave_module *const module) |
Kojto | 111:4336505e4b1c | 647 | { |
Kojto | 111:4336505e4b1c | 648 | /* Sanity check of arguments. */ |
Kojto | 111:4336505e4b1c | 649 | Assert(module); |
Kojto | 111:4336505e4b1c | 650 | Assert(module->hw); |
Kojto | 111:4336505e4b1c | 651 | |
Kojto | 111:4336505e4b1c | 652 | SercomI2cs *const i2c_hw = &(module->hw->I2CS); |
Kojto | 111:4336505e4b1c | 653 | |
Kojto | 111:4336505e4b1c | 654 | #if I2C_SLAVE_CALLBACK_MODE == true |
Kojto | 111:4336505e4b1c | 655 | /* Disable interrupts */ |
Kojto | 111:4336505e4b1c | 656 | i2c_hw->INTENCLR.reg = SERCOM_I2CS_INTENSET_PREC | |
Kojto | 111:4336505e4b1c | 657 | SERCOM_I2CS_INTENSET_AMATCH | SERCOM_I2CS_INTENSET_DRDY; |
Kojto | 111:4336505e4b1c | 658 | |
Kojto | 111:4336505e4b1c | 659 | /* Clear interrupt flags */ |
Kojto | 111:4336505e4b1c | 660 | i2c_hw->INTFLAG.reg = SERCOM_I2CS_INTFLAG_PREC | SERCOM_I2CS_INTFLAG_AMATCH | |
Kojto | 111:4336505e4b1c | 661 | SERCOM_I2CS_INTFLAG_DRDY; |
Kojto | 111:4336505e4b1c | 662 | |
Kojto | 111:4336505e4b1c | 663 | /* Disable global interrupt for module */ |
Kojto | 111:4336505e4b1c | 664 | system_interrupt_disable(_sercom_get_interrupt_vector(module->hw)); |
Kojto | 111:4336505e4b1c | 665 | #endif |
Kojto | 111:4336505e4b1c | 666 | |
Kojto | 111:4336505e4b1c | 667 | /* Wait for module to sync */ |
Kojto | 111:4336505e4b1c | 668 | _i2c_slave_wait_for_sync(module); |
Kojto | 111:4336505e4b1c | 669 | |
Kojto | 111:4336505e4b1c | 670 | /* Disable module */ |
Kojto | 111:4336505e4b1c | 671 | i2c_hw->CTRLA.reg &= ~SERCOM_I2CS_CTRLA_ENABLE; |
Kojto | 111:4336505e4b1c | 672 | } |
Kojto | 111:4336505e4b1c | 673 | |
Kojto | 111:4336505e4b1c | 674 | void i2c_slave_reset( |
Kojto | 111:4336505e4b1c | 675 | struct i2c_slave_module *const module); |
Kojto | 111:4336505e4b1c | 676 | |
Kojto | 111:4336505e4b1c | 677 | /** @} */ |
Kojto | 111:4336505e4b1c | 678 | |
Kojto | 111:4336505e4b1c | 679 | /** |
Kojto | 111:4336505e4b1c | 680 | * \name Read and Write |
Kojto | 111:4336505e4b1c | 681 | * @{ |
Kojto | 111:4336505e4b1c | 682 | */ |
Kojto | 111:4336505e4b1c | 683 | |
Kojto | 111:4336505e4b1c | 684 | enum status_code i2c_slave_write_packet_wait( |
Kojto | 111:4336505e4b1c | 685 | struct i2c_slave_module *const module, |
Kojto | 111:4336505e4b1c | 686 | struct i2c_slave_packet *const packet); |
Kojto | 111:4336505e4b1c | 687 | enum status_code i2c_slave_read_packet_wait( |
Kojto | 111:4336505e4b1c | 688 | struct i2c_slave_module *const module, |
Kojto | 111:4336505e4b1c | 689 | struct i2c_slave_packet *const packet); |
Kojto | 111:4336505e4b1c | 690 | enum i2c_slave_direction i2c_slave_get_direction_wait( |
Kojto | 111:4336505e4b1c | 691 | struct i2c_slave_module *const module); |
Kojto | 111:4336505e4b1c | 692 | |
Kojto | 111:4336505e4b1c | 693 | /** @} */ |
Kojto | 111:4336505e4b1c | 694 | |
Kojto | 111:4336505e4b1c | 695 | /** |
Kojto | 111:4336505e4b1c | 696 | * \name Status Management |
Kojto | 111:4336505e4b1c | 697 | * @{ |
Kojto | 111:4336505e4b1c | 698 | */ |
Kojto | 111:4336505e4b1c | 699 | uint32_t i2c_slave_get_status( |
Kojto | 111:4336505e4b1c | 700 | struct i2c_slave_module *const module); |
Kojto | 111:4336505e4b1c | 701 | void i2c_slave_clear_status( |
Kojto | 111:4336505e4b1c | 702 | struct i2c_slave_module *const module, |
Kojto | 111:4336505e4b1c | 703 | uint32_t status_flags); |
Kojto | 111:4336505e4b1c | 704 | /** @} */ |
Kojto | 111:4336505e4b1c | 705 | |
Kojto | 111:4336505e4b1c | 706 | #ifdef FEATURE_I2C_DMA_SUPPORT |
Kojto | 111:4336505e4b1c | 707 | /** |
Kojto | 111:4336505e4b1c | 708 | * \name SERCOM I2C Slave with DMA Interfaces |
Kojto | 111:4336505e4b1c | 709 | * @{ |
Kojto | 111:4336505e4b1c | 710 | */ |
Kojto | 111:4336505e4b1c | 711 | |
Kojto | 111:4336505e4b1c | 712 | /** |
Kojto | 111:4336505e4b1c | 713 | * \brief Read SERCOM I<SUP>2</SUP>C interrupt status. |
Kojto | 111:4336505e4b1c | 714 | * |
Kojto | 111:4336505e4b1c | 715 | * Read I<SUP>2</SUP>C interrupt status for DMA transfer. |
Kojto | 111:4336505e4b1c | 716 | * |
Kojto | 111:4336505e4b1c | 717 | * \param[in,out] module Pointer to the driver instance to lock |
Kojto | 111:4336505e4b1c | 718 | * |
Kojto | 111:4336505e4b1c | 719 | */ |
Kojto | 111:4336505e4b1c | 720 | static inline uint8_t i2c_slave_dma_read_interrupt_status(struct i2c_slave_module *const module) |
Kojto | 111:4336505e4b1c | 721 | { |
Kojto | 111:4336505e4b1c | 722 | return (uint8_t)module->hw->I2CS.INTFLAG.reg; |
Kojto | 111:4336505e4b1c | 723 | } |
Kojto | 111:4336505e4b1c | 724 | |
Kojto | 111:4336505e4b1c | 725 | /** |
Kojto | 111:4336505e4b1c | 726 | * \brief Write SERCOM I<SUP>2</SUP>C interrupt status. |
Kojto | 111:4336505e4b1c | 727 | * |
Kojto | 111:4336505e4b1c | 728 | * Write I<SUP>2</SUP>C interrupt status for DMA transfer. |
Kojto | 111:4336505e4b1c | 729 | * |
Kojto | 111:4336505e4b1c | 730 | * \param[in,out] module Pointer to the driver instance to lock |
Kojto | 111:4336505e4b1c | 731 | * \param[in] flag Interrupt flag status |
Kojto | 111:4336505e4b1c | 732 | * |
Kojto | 111:4336505e4b1c | 733 | */ |
Kojto | 111:4336505e4b1c | 734 | static inline void i2c_slave_dma_write_interrupt_status(struct i2c_slave_module *const module, |
Kojto | 111:4336505e4b1c | 735 | uint8_t flag) |
Kojto | 111:4336505e4b1c | 736 | { |
Kojto | 111:4336505e4b1c | 737 | module->hw->I2CS.INTFLAG.reg = flag; |
Kojto | 111:4336505e4b1c | 738 | } |
Kojto | 111:4336505e4b1c | 739 | |
Kojto | 111:4336505e4b1c | 740 | /** @} */ |
Kojto | 111:4336505e4b1c | 741 | #endif |
Kojto | 111:4336505e4b1c | 742 | |
Kojto | 111:4336505e4b1c | 743 | /** @} */ |
Kojto | 111:4336505e4b1c | 744 | |
Kojto | 111:4336505e4b1c | 745 | #ifdef __cplusplus |
Kojto | 111:4336505e4b1c | 746 | } |
Kojto | 111:4336505e4b1c | 747 | #endif |
Kojto | 111:4336505e4b1c | 748 | |
Kojto | 111:4336505e4b1c | 749 | #endif /* I2C_SLAVE_H_INCLUDED */ |