mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

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

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

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

Import librarymbed

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

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

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

Initial version of drivers for SAMR21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 579:53297373a894 1 /**
mbed_official 579:53297373a894 2 * \file
mbed_official 579:53297373a894 3 *
mbed_official 579:53297373a894 4 * \brief SAM I<SUP>2</SUP>C Slave Driver
mbed_official 579:53297373a894 5 *
mbed_official 579:53297373a894 6 * Copyright (C) 2013-2014 Atmel Corporation. All rights reserved.
mbed_official 579:53297373a894 7 *
mbed_official 579:53297373a894 8 * \asf_license_start
mbed_official 579:53297373a894 9 *
mbed_official 579:53297373a894 10 * \page License
mbed_official 579:53297373a894 11 *
mbed_official 579:53297373a894 12 * Redistribution and use in source and binary forms, with or without
mbed_official 579:53297373a894 13 * modification, are permitted provided that the following conditions are met:
mbed_official 579:53297373a894 14 *
mbed_official 579:53297373a894 15 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 579:53297373a894 16 * this list of conditions and the following disclaimer.
mbed_official 579:53297373a894 17 *
mbed_official 579:53297373a894 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 579:53297373a894 19 * this list of conditions and the following disclaimer in the documentation
mbed_official 579:53297373a894 20 * and/or other materials provided with the distribution.
mbed_official 579:53297373a894 21 *
mbed_official 579:53297373a894 22 * 3. The name of Atmel may not be used to endorse or promote products derived
mbed_official 579:53297373a894 23 * from this software without specific prior written permission.
mbed_official 579:53297373a894 24 *
mbed_official 579:53297373a894 25 * 4. This software may only be redistributed and used in connection with an
mbed_official 579:53297373a894 26 * Atmel microcontroller product.
mbed_official 579:53297373a894 27 *
mbed_official 579:53297373a894 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
mbed_official 579:53297373a894 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mbed_official 579:53297373a894 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
mbed_official 579:53297373a894 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
mbed_official 579:53297373a894 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 579:53297373a894 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
mbed_official 579:53297373a894 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
mbed_official 579:53297373a894 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
mbed_official 579:53297373a894 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
mbed_official 579:53297373a894 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 579:53297373a894 38 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 579:53297373a894 39 *
mbed_official 579:53297373a894 40 * \asf_license_stop
mbed_official 579:53297373a894 41 *
mbed_official 579:53297373a894 42 */
mbed_official 579:53297373a894 43 /**
mbed_official 579:53297373a894 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
mbed_official 579:53297373a894 45 */
mbed_official 579:53297373a894 46
mbed_official 579:53297373a894 47 #include "i2c_slave.h"
mbed_official 579:53297373a894 48 #if I2C_SLAVE_CALLBACK_MODE == true
mbed_official 579:53297373a894 49 # include "i2c_slave_interrupt.h"
mbed_official 579:53297373a894 50 #endif
mbed_official 579:53297373a894 51
mbed_official 579:53297373a894 52 /**
mbed_official 579:53297373a894 53 * \internal Sets configuration to module
mbed_official 579:53297373a894 54 *
mbed_official 579:53297373a894 55 * \param[out] module Pointer to software module structure
mbed_official 579:53297373a894 56 * \param[in] config Configuration structure with configurations to set
mbed_official 579:53297373a894 57 *
mbed_official 579:53297373a894 58 * \return Status of setting configuration.
mbed_official 579:53297373a894 59 * \retval STATUS_OK Module was configured correctly
mbed_official 579:53297373a894 60 * \retval STATUS_ERR_ALREADY_INITIALIZED If setting other GCLK generator than
mbed_official 579:53297373a894 61 * previously set
mbed_official 579:53297373a894 62 */
mbed_official 579:53297373a894 63 static enum status_code _i2c_slave_set_config(
mbed_official 579:53297373a894 64 struct i2c_slave_module *const module,
mbed_official 579:53297373a894 65 const struct i2c_slave_config *const config)
mbed_official 579:53297373a894 66 {
mbed_official 579:53297373a894 67 uint32_t tmp_ctrla;
mbed_official 579:53297373a894 68
mbed_official 579:53297373a894 69 /* Sanity check arguments. */
mbed_official 579:53297373a894 70 Assert(module);
mbed_official 579:53297373a894 71 Assert(module->hw);
mbed_official 579:53297373a894 72 Assert(config);
mbed_official 579:53297373a894 73
mbed_official 579:53297373a894 74 SercomI2cs *const i2c_hw = &(module->hw->I2CS);
mbed_official 579:53297373a894 75 Sercom *const sercom_hw = module->hw;
mbed_official 579:53297373a894 76
mbed_official 579:53297373a894 77 module->buffer_timeout = config->buffer_timeout;
mbed_official 579:53297373a894 78 module->ten_bit_address = config->ten_bit_address;
mbed_official 579:53297373a894 79
mbed_official 579:53297373a894 80 struct system_pinmux_config pin_conf;
mbed_official 579:53297373a894 81 system_pinmux_get_config_defaults(&pin_conf);
mbed_official 579:53297373a894 82
mbed_official 579:53297373a894 83 uint32_t pad0 = config->pinmux_pad0;
mbed_official 579:53297373a894 84 uint32_t pad1 = config->pinmux_pad1;
mbed_official 579:53297373a894 85
mbed_official 579:53297373a894 86 /* SERCOM PAD0 - SDA */
mbed_official 579:53297373a894 87 if (pad0 == PINMUX_DEFAULT) {
mbed_official 579:53297373a894 88 pad0 = _sercom_get_default_pad(sercom_hw, 0);
mbed_official 579:53297373a894 89 }
mbed_official 579:53297373a894 90 pin_conf.mux_position = pad0 & 0xFFFF;
mbed_official 579:53297373a894 91 pin_conf.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT_WITH_READBACK;
mbed_official 579:53297373a894 92 system_pinmux_pin_set_config(pad0 >> 16, &pin_conf);
mbed_official 579:53297373a894 93
mbed_official 579:53297373a894 94 /* SERCOM PAD1 - SCL */
mbed_official 579:53297373a894 95 if (pad1 == PINMUX_DEFAULT) {
mbed_official 579:53297373a894 96 pad1 = _sercom_get_default_pad(sercom_hw, 1);
mbed_official 579:53297373a894 97 }
mbed_official 579:53297373a894 98 pin_conf.mux_position = pad1 & 0xFFFF;
mbed_official 579:53297373a894 99 pin_conf.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT_WITH_READBACK;
mbed_official 579:53297373a894 100 system_pinmux_pin_set_config(pad1 >> 16, &pin_conf);
mbed_official 579:53297373a894 101
mbed_official 579:53297373a894 102 /* Prepare config to write to register CTRLA */
mbed_official 579:53297373a894 103 if (config->run_in_standby || system_is_debugger_present()) {
mbed_official 579:53297373a894 104 tmp_ctrla = SERCOM_I2CS_CTRLA_RUNSTDBY;
mbed_official 579:53297373a894 105 } else {
mbed_official 579:53297373a894 106 tmp_ctrla = 0;
mbed_official 579:53297373a894 107 }
mbed_official 579:53297373a894 108
mbed_official 579:53297373a894 109 tmp_ctrla |= ((uint32_t)config->sda_hold_time |
mbed_official 579:53297373a894 110 config->transfer_speed |
mbed_official 579:53297373a894 111 (config->scl_low_timeout << SERCOM_I2CS_CTRLA_LOWTOUTEN_Pos) |
mbed_official 579:53297373a894 112 (config->scl_stretch_only_after_ack_bit << SERCOM_I2CS_CTRLA_SCLSM_Pos) |
mbed_official 579:53297373a894 113 (config->slave_scl_low_extend_timeout << SERCOM_I2CS_CTRLA_SEXTTOEN_Pos));
mbed_official 579:53297373a894 114
mbed_official 579:53297373a894 115 i2c_hw->CTRLA.reg |= tmp_ctrla;
mbed_official 579:53297373a894 116
mbed_official 579:53297373a894 117 /* Set CTRLB configuration */
mbed_official 579:53297373a894 118 i2c_hw->CTRLB.reg = SERCOM_I2CS_CTRLB_SMEN | config->address_mode;
mbed_official 579:53297373a894 119
mbed_official 579:53297373a894 120 i2c_hw->ADDR.reg = config->address << SERCOM_I2CS_ADDR_ADDR_Pos |
mbed_official 579:53297373a894 121 config->address_mask << SERCOM_I2CS_ADDR_ADDRMASK_Pos |
mbed_official 579:53297373a894 122 config->ten_bit_address << SERCOM_I2CS_ADDR_TENBITEN_Pos |
mbed_official 579:53297373a894 123 config->enable_general_call_address << SERCOM_I2CS_ADDR_GENCEN_Pos;
mbed_official 579:53297373a894 124
mbed_official 579:53297373a894 125 return STATUS_OK;
mbed_official 579:53297373a894 126 }
mbed_official 579:53297373a894 127
mbed_official 579:53297373a894 128 /**
mbed_official 579:53297373a894 129 * \brief Initializes the requested I<SUP>2</SUP>C hardware module
mbed_official 579:53297373a894 130 *
mbed_official 579:53297373a894 131 * Initializes the SERCOM I<SUP>2</SUP>C Slave device requested and sets the provided
mbed_official 579:53297373a894 132 * software module struct. Run this function before any further use of
mbed_official 579:53297373a894 133 * the driver.
mbed_official 579:53297373a894 134 *
mbed_official 579:53297373a894 135 * \param[out] module Pointer to software module struct
mbed_official 579:53297373a894 136 * \param[in] hw Pointer to the hardware instance
mbed_official 579:53297373a894 137 * \param[in] config Pointer to the configuration struct
mbed_official 579:53297373a894 138 *
mbed_official 579:53297373a894 139 * \return Status of initialization.
mbed_official 579:53297373a894 140 * \retval STATUS_OK Module initiated correctly
mbed_official 579:53297373a894 141 * \retval STATUS_ERR_DENIED If module is enabled
mbed_official 579:53297373a894 142 * \retval STATUS_BUSY If module is busy resetting
mbed_official 579:53297373a894 143 * \retval STATUS_ERR_ALREADY_INITIALIZED If setting other GCLK generator than
mbed_official 579:53297373a894 144 * previously set
mbed_official 579:53297373a894 145 */
mbed_official 579:53297373a894 146 enum status_code i2c_slave_init(
mbed_official 579:53297373a894 147 struct i2c_slave_module *const module,
mbed_official 579:53297373a894 148 Sercom *const hw,
mbed_official 579:53297373a894 149 const struct i2c_slave_config *const config)
mbed_official 579:53297373a894 150 {
mbed_official 579:53297373a894 151 /* Sanity check arguments. */
mbed_official 579:53297373a894 152 Assert(module);
mbed_official 579:53297373a894 153 Assert(hw);
mbed_official 579:53297373a894 154 Assert(config);
mbed_official 579:53297373a894 155
mbed_official 579:53297373a894 156 /* Initialize software module */
mbed_official 579:53297373a894 157 module->hw = hw;
mbed_official 579:53297373a894 158
mbed_official 579:53297373a894 159 SercomI2cs *const i2c_hw = &(module->hw->I2CS);
mbed_official 579:53297373a894 160
mbed_official 579:53297373a894 161 /* Check if module is enabled. */
mbed_official 579:53297373a894 162 if (i2c_hw->CTRLA.reg & SERCOM_I2CS_CTRLA_ENABLE) {
mbed_official 579:53297373a894 163 return STATUS_ERR_DENIED;
mbed_official 579:53297373a894 164 }
mbed_official 579:53297373a894 165
mbed_official 579:53297373a894 166 /* Check if reset is in progress. */
mbed_official 579:53297373a894 167 if (i2c_hw->CTRLA.reg & SERCOM_I2CS_CTRLA_SWRST) {
mbed_official 579:53297373a894 168 return STATUS_BUSY;
mbed_official 579:53297373a894 169 }
mbed_official 579:53297373a894 170
mbed_official 579:53297373a894 171 uint32_t sercom_index = _sercom_get_sercom_inst_index(module->hw);
mbed_official 579:53297373a894 172 #if (SAML21)
mbed_official 579:53297373a894 173 uint32_t pm_index = sercom_index + MCLK_APBCMASK_SERCOM0_Pos;
mbed_official 579:53297373a894 174 #else
mbed_official 579:53297373a894 175 uint32_t pm_index = sercom_index + PM_APBCMASK_SERCOM0_Pos;
mbed_official 579:53297373a894 176 #endif
mbed_official 579:53297373a894 177 uint32_t gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
mbed_official 579:53297373a894 178
mbed_official 579:53297373a894 179 /* Turn on module in PM */
mbed_official 579:53297373a894 180 system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, 1 << pm_index);
mbed_official 579:53297373a894 181
mbed_official 579:53297373a894 182 /* Set up the GCLK for the module */
mbed_official 579:53297373a894 183 struct system_gclk_chan_config gclk_chan_conf;
mbed_official 579:53297373a894 184 system_gclk_chan_get_config_defaults(&gclk_chan_conf);
mbed_official 579:53297373a894 185 gclk_chan_conf.source_generator = config->generator_source;
mbed_official 579:53297373a894 186 system_gclk_chan_set_config(gclk_index, &gclk_chan_conf);
mbed_official 579:53297373a894 187 system_gclk_chan_enable(gclk_index);
mbed_official 579:53297373a894 188 sercom_set_gclk_generator(config->generator_source, false);
mbed_official 579:53297373a894 189
mbed_official 579:53297373a894 190 #if I2C_SLAVE_CALLBACK_MODE == true
mbed_official 579:53297373a894 191 /* Get sercom instance index. */
mbed_official 579:53297373a894 192 uint8_t instance_index = _sercom_get_sercom_inst_index(module->hw);
mbed_official 579:53297373a894 193
mbed_official 579:53297373a894 194 /* Save software module in interrupt handler. */
mbed_official 579:53297373a894 195 _sercom_set_handler(instance_index, _i2c_slave_interrupt_handler);
mbed_official 579:53297373a894 196
mbed_official 579:53297373a894 197 /* Save software module. */
mbed_official 579:53297373a894 198 _sercom_instances[instance_index] = module;
mbed_official 579:53297373a894 199
mbed_official 579:53297373a894 200 /* Initialize values in module. */
mbed_official 579:53297373a894 201 module->registered_callback = 0;
mbed_official 579:53297373a894 202 module->enabled_callback = 0;
mbed_official 579:53297373a894 203 module->buffer_length = 0;
mbed_official 579:53297373a894 204 module->nack_on_address = config->enable_nack_on_address;
mbed_official 579:53297373a894 205 #endif
mbed_official 579:53297373a894 206
mbed_official 579:53297373a894 207 /* Set SERCOM module to operate in I2C slave mode. */
mbed_official 579:53297373a894 208 i2c_hw->CTRLA.reg = SERCOM_I2CS_CTRLA_MODE(0x4);
mbed_official 579:53297373a894 209
mbed_official 579:53297373a894 210 /* Set config and return status. */
mbed_official 579:53297373a894 211 return _i2c_slave_set_config(module, config);
mbed_official 579:53297373a894 212 }
mbed_official 579:53297373a894 213
mbed_official 579:53297373a894 214 /**
mbed_official 579:53297373a894 215 * \brief Resets the hardware module
mbed_official 579:53297373a894 216 *
mbed_official 579:53297373a894 217 * This will reset the module to hardware defaults.
mbed_official 579:53297373a894 218 *
mbed_official 579:53297373a894 219 * \param[in,out] module Pointer to software module structure
mbed_official 579:53297373a894 220 */
mbed_official 579:53297373a894 221 void i2c_slave_reset(
mbed_official 579:53297373a894 222 struct i2c_slave_module *const module)
mbed_official 579:53297373a894 223 {
mbed_official 579:53297373a894 224 /* Sanity check arguments. */
mbed_official 579:53297373a894 225 Assert(module);
mbed_official 579:53297373a894 226 Assert(module->hw);
mbed_official 579:53297373a894 227
mbed_official 579:53297373a894 228 SercomI2cs *const i2c_hw = &(module->hw->I2CS);
mbed_official 579:53297373a894 229
mbed_official 579:53297373a894 230 #if I2C_SLAVE_CALLBACK_MODE == true
mbed_official 579:53297373a894 231 /* Reset module instance. */
mbed_official 579:53297373a894 232 module->registered_callback = 0;
mbed_official 579:53297373a894 233 module->enabled_callback = 0;
mbed_official 579:53297373a894 234 module->buffer_length = 0;
mbed_official 579:53297373a894 235 module->buffer_remaining = 0;
mbed_official 579:53297373a894 236 module->buffer = NULL;
mbed_official 579:53297373a894 237 #endif
mbed_official 579:53297373a894 238
mbed_official 579:53297373a894 239 /* Disable module */
mbed_official 579:53297373a894 240 i2c_slave_disable(module);
mbed_official 579:53297373a894 241
mbed_official 579:53297373a894 242 #if I2C_SLAVE_CALLBACK_MODE == true
mbed_official 579:53297373a894 243 /* Clear all pending interrupts. */
mbed_official 579:53297373a894 244 system_interrupt_enter_critical_section();
mbed_official 579:53297373a894 245 system_interrupt_clear_pending(_sercom_get_interrupt_vector(module->hw));
mbed_official 579:53297373a894 246 system_interrupt_leave_critical_section();
mbed_official 579:53297373a894 247 #endif
mbed_official 579:53297373a894 248
mbed_official 579:53297373a894 249 /* Wait for sync. */
mbed_official 579:53297373a894 250 _i2c_slave_wait_for_sync(module);
mbed_official 579:53297373a894 251
mbed_official 579:53297373a894 252 /* Reset module. */
mbed_official 579:53297373a894 253 i2c_hw->CTRLA.reg = SERCOM_I2CS_CTRLA_SWRST;
mbed_official 579:53297373a894 254 }
mbed_official 579:53297373a894 255
mbed_official 579:53297373a894 256 /**
mbed_official 579:53297373a894 257 * \internal Waits for answer on bus
mbed_official 579:53297373a894 258 *
mbed_official 579:53297373a894 259 * \param[in] module Pointer to software module structure
mbed_official 579:53297373a894 260 *
mbed_official 579:53297373a894 261 * \return Status of bus.
mbed_official 579:53297373a894 262 * \retval STATUS_OK If given response from slave device
mbed_official 579:53297373a894 263 * \retval STATUS_ERR_TIMEOUT If no response was given within specified timeout
mbed_official 579:53297373a894 264 * period
mbed_official 579:53297373a894 265 */
mbed_official 579:53297373a894 266 static enum status_code _i2c_slave_wait_for_bus(
mbed_official 579:53297373a894 267 struct i2c_slave_module *const module)
mbed_official 579:53297373a894 268 {
mbed_official 579:53297373a894 269 /* Sanity check arguments. */
mbed_official 579:53297373a894 270 Assert(module);
mbed_official 579:53297373a894 271 Assert(module->hw);
mbed_official 579:53297373a894 272
mbed_official 579:53297373a894 273 SercomI2cm *const i2c_module = &(module->hw->I2CM);
mbed_official 579:53297373a894 274
mbed_official 579:53297373a894 275 /* Wait for reply. */
mbed_official 579:53297373a894 276 uint16_t timeout_counter = 0;
mbed_official 579:53297373a894 277 while ((!(i2c_module->INTFLAG.reg & SERCOM_I2CS_INTFLAG_DRDY)) &&
mbed_official 579:53297373a894 278 (!(i2c_module->INTFLAG.reg & SERCOM_I2CS_INTFLAG_PREC)) &&
mbed_official 579:53297373a894 279 (!(i2c_module->INTFLAG.reg & SERCOM_I2CS_INTFLAG_AMATCH))) {
mbed_official 579:53297373a894 280
mbed_official 579:53297373a894 281 /* Check timeout condition. */
mbed_official 579:53297373a894 282 if (++timeout_counter >= module->buffer_timeout) {
mbed_official 579:53297373a894 283 return STATUS_ERR_TIMEOUT;
mbed_official 579:53297373a894 284 }
mbed_official 579:53297373a894 285 }
mbed_official 579:53297373a894 286 return STATUS_OK;
mbed_official 579:53297373a894 287 }
mbed_official 579:53297373a894 288
mbed_official 579:53297373a894 289 /**
mbed_official 579:53297373a894 290 * \brief Writes a packet to the master
mbed_official 579:53297373a894 291 *
mbed_official 579:53297373a894 292 * Writes a packet to the master. This will wait for the master to issue
mbed_official 579:53297373a894 293 * a request.
mbed_official 579:53297373a894 294 *
mbed_official 579:53297373a894 295 * \param[in] module Pointer to software module structure
mbed_official 579:53297373a894 296 * \param[in] packet Packet to write to master
mbed_official 579:53297373a894 297 *
mbed_official 579:53297373a894 298 * \return Status of packet write.
mbed_official 579:53297373a894 299 * \retval STATUS_OK Packet was written successfully
mbed_official 579:53297373a894 300 * \retval STATUS_ERR_DENIED Start condition not received, another
mbed_official 579:53297373a894 301 * interrupt flag is set
mbed_official 579:53297373a894 302 * \retval STATUS_ERR_IO There was an error in the previous transfer
mbed_official 579:53297373a894 303 * \retval STATUS_ERR_BAD_FORMAT Master wants to write data
mbed_official 579:53297373a894 304 * \retval STATUS_ERR_INVALID_ARG Invalid argument(s) was provided
mbed_official 579:53297373a894 305 * \retval STATUS_ERR_BUSY The I<SUP>2</SUP>C module is busy with a job
mbed_official 579:53297373a894 306 * \retval STATUS_ERR_ERR_OVERFLOW Master NACKed before entire packet was
mbed_official 579:53297373a894 307 * transferred
mbed_official 579:53297373a894 308 * \retval STATUS_ERR_TIMEOUT No response was given within the timeout
mbed_official 579:53297373a894 309 * period
mbed_official 579:53297373a894 310 */
mbed_official 579:53297373a894 311 enum status_code i2c_slave_write_packet_wait(
mbed_official 579:53297373a894 312 struct i2c_slave_module *const module,
mbed_official 579:53297373a894 313 struct i2c_slave_packet *const packet)
mbed_official 579:53297373a894 314 {
mbed_official 579:53297373a894 315 /* Sanity check arguments. */
mbed_official 579:53297373a894 316 Assert(module);
mbed_official 579:53297373a894 317 Assert(module->hw);
mbed_official 579:53297373a894 318 Assert(packet);
mbed_official 579:53297373a894 319
mbed_official 579:53297373a894 320 SercomI2cs *const i2c_hw = &(module->hw->I2CS);
mbed_official 579:53297373a894 321
mbed_official 579:53297373a894 322 uint16_t length = packet->data_length;
mbed_official 579:53297373a894 323
mbed_official 579:53297373a894 324 if (length == 0) {
mbed_official 579:53297373a894 325 return STATUS_ERR_INVALID_ARG;
mbed_official 579:53297373a894 326 }
mbed_official 579:53297373a894 327
mbed_official 579:53297373a894 328 #if I2C_SLAVE_CALLBACK_MODE == true
mbed_official 579:53297373a894 329 /* Check if the module is busy with a job or AMATCH is enabled */
mbed_official 579:53297373a894 330 if (module->buffer_remaining > 0 ||
mbed_official 579:53297373a894 331 (i2c_hw->INTENSET.reg & SERCOM_I2CS_INTFLAG_AMATCH)) {
mbed_official 579:53297373a894 332 return STATUS_BUSY;
mbed_official 579:53297373a894 333 }
mbed_official 579:53297373a894 334 #endif
mbed_official 579:53297373a894 335
mbed_official 579:53297373a894 336 enum status_code status;
mbed_official 579:53297373a894 337 /* Wait for master to send address packet */
mbed_official 579:53297373a894 338 status = _i2c_slave_wait_for_bus(module);
mbed_official 579:53297373a894 339
mbed_official 579:53297373a894 340 if (status != STATUS_OK) {
mbed_official 579:53297373a894 341 /* Timeout, return */
mbed_official 579:53297373a894 342 return status;
mbed_official 579:53297373a894 343 }
mbed_official 579:53297373a894 344 if (!(i2c_hw->INTFLAG.reg & SERCOM_I2CS_INTFLAG_AMATCH)) {
mbed_official 579:53297373a894 345 /* Not address interrupt, something is wrong */
mbed_official 579:53297373a894 346 return STATUS_ERR_DENIED;
mbed_official 579:53297373a894 347 }
mbed_official 579:53297373a894 348
mbed_official 579:53297373a894 349 if (module->ten_bit_address) {
mbed_official 579:53297373a894 350 /* ACK the first address */
mbed_official 579:53297373a894 351 i2c_hw->CTRLB.reg &= ~SERCOM_I2CS_CTRLB_ACKACT;
mbed_official 579:53297373a894 352 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_CMD(0x3);
mbed_official 579:53297373a894 353
mbed_official 579:53297373a894 354 /* Wait for address interrupt */
mbed_official 579:53297373a894 355 status = _i2c_slave_wait_for_bus(module);
mbed_official 579:53297373a894 356
mbed_official 579:53297373a894 357 if (status != STATUS_OK) {
mbed_official 579:53297373a894 358 /* Timeout, return */
mbed_official 579:53297373a894 359 return STATUS_ERR_TIMEOUT;
mbed_official 579:53297373a894 360 }
mbed_official 579:53297373a894 361
mbed_official 579:53297373a894 362 if (!(i2c_hw->INTFLAG.reg & SERCOM_I2CS_INTFLAG_AMATCH)) {
mbed_official 579:53297373a894 363 /* Not address interrupt, something is wrong */
mbed_official 579:53297373a894 364 return STATUS_ERR_DENIED;
mbed_official 579:53297373a894 365 }
mbed_official 579:53297373a894 366 }
mbed_official 579:53297373a894 367
mbed_official 579:53297373a894 368 /* Check if there was an error in last transfer */
mbed_official 579:53297373a894 369 if (i2c_hw->STATUS.reg & (SERCOM_I2CS_STATUS_BUSERR |
mbed_official 579:53297373a894 370 SERCOM_I2CS_STATUS_COLL | SERCOM_I2CS_STATUS_LOWTOUT)) {
mbed_official 579:53297373a894 371 return STATUS_ERR_IO;
mbed_official 579:53297373a894 372 }
mbed_official 579:53297373a894 373
mbed_official 579:53297373a894 374 /* Check direction */
mbed_official 579:53297373a894 375 if (!(i2c_hw->STATUS.reg & SERCOM_I2CS_STATUS_DIR)) {
mbed_official 579:53297373a894 376 /* Write request from master, send NACK and return */
mbed_official 579:53297373a894 377 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_ACKACT;
mbed_official 579:53297373a894 378 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_CMD(0x3);
mbed_official 579:53297373a894 379 return STATUS_ERR_BAD_FORMAT;
mbed_official 579:53297373a894 380 }
mbed_official 579:53297373a894 381
mbed_official 579:53297373a894 382 /* Read request from master, ACK address */
mbed_official 579:53297373a894 383 i2c_hw->CTRLB.reg &= ~SERCOM_I2CS_CTRLB_ACKACT;
mbed_official 579:53297373a894 384 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_CMD(0x3);
mbed_official 579:53297373a894 385
mbed_official 579:53297373a894 386 uint16_t i = 0;
mbed_official 579:53297373a894 387
mbed_official 579:53297373a894 388 /* Wait for data interrupt */
mbed_official 579:53297373a894 389 status = _i2c_slave_wait_for_bus(module);
mbed_official 579:53297373a894 390 if (status != STATUS_OK) {
mbed_official 579:53297373a894 391 /* Timeout, return */
mbed_official 579:53297373a894 392 return status;
mbed_official 579:53297373a894 393 }
mbed_official 579:53297373a894 394
mbed_official 579:53297373a894 395 while (length--) {
mbed_official 579:53297373a894 396 /* Write data */
mbed_official 579:53297373a894 397 _i2c_slave_wait_for_sync(module);
mbed_official 579:53297373a894 398 i2c_hw->DATA.reg = packet->data[i++];
mbed_official 579:53297373a894 399
mbed_official 579:53297373a894 400 /* Wait for response from master */
mbed_official 579:53297373a894 401 status = _i2c_slave_wait_for_bus(module);
mbed_official 579:53297373a894 402
mbed_official 579:53297373a894 403 if (status != STATUS_OK) {
mbed_official 579:53297373a894 404 /* Timeout, return */
mbed_official 579:53297373a894 405 return status;
mbed_official 579:53297373a894 406 }
mbed_official 579:53297373a894 407
mbed_official 579:53297373a894 408 if (i2c_hw->STATUS.reg & SERCOM_I2CS_STATUS_RXNACK &&
mbed_official 579:53297373a894 409 length !=0) {
mbed_official 579:53297373a894 410 /* NACK from master, abort */
mbed_official 579:53297373a894 411 /* Release line */
mbed_official 579:53297373a894 412 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_CMD(0x02);
mbed_official 579:53297373a894 413
mbed_official 579:53297373a894 414 return STATUS_ERR_OVERFLOW;
mbed_official 579:53297373a894 415 }
mbed_official 579:53297373a894 416 /* ACK from master, continue writing */
mbed_official 579:53297373a894 417 }
mbed_official 579:53297373a894 418
mbed_official 579:53297373a894 419 /* Release line */
mbed_official 579:53297373a894 420 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_CMD(0x02);
mbed_official 579:53297373a894 421
mbed_official 579:53297373a894 422 return STATUS_OK;
mbed_official 579:53297373a894 423 }
mbed_official 579:53297373a894 424
mbed_official 579:53297373a894 425 /**
mbed_official 579:53297373a894 426 * \brief Reads a packet from the master
mbed_official 579:53297373a894 427 *
mbed_official 579:53297373a894 428 * Reads a packet from the master. This will wait for the master to issue a
mbed_official 579:53297373a894 429 * request.
mbed_official 579:53297373a894 430 *
mbed_official 579:53297373a894 431 * \param[in] module Pointer to software module structure
mbed_official 579:53297373a894 432 * \param[out] packet Packet to read from master
mbed_official 579:53297373a894 433 *
mbed_official 579:53297373a894 434 * \return Status of packet read.
mbed_official 579:53297373a894 435 * \retval STATUS_OK Packet was read successfully
mbed_official 579:53297373a894 436 * \retval STATUS_ABORTED Master sent stop condition or repeated
mbed_official 579:53297373a894 437 * start before specified length of bytes
mbed_official 579:53297373a894 438 * was received
mbed_official 579:53297373a894 439 * \retval STATUS_ERR_IO There was an error in the previous transfer
mbed_official 579:53297373a894 440 * \retval STATUS_ERR_DENIED Start condition not received, another
mbed_official 579:53297373a894 441 * interrupt flag is set
mbed_official 579:53297373a894 442 * \retval STATUS_ERR_INVALID_ARG Invalid argument(s) was provided
mbed_official 579:53297373a894 443 * \retval STATUS_ERR_BUSY The I<SUP>2</SUP>C module is busy with a job
mbed_official 579:53297373a894 444 * \retval STATUS_ERR_BAD_FORMAT Master wants to read data
mbed_official 579:53297373a894 445 * \retval STATUS_ERR_ERR_OVERFLOW Last byte received overflows buffer
mbed_official 579:53297373a894 446 */
mbed_official 579:53297373a894 447 enum status_code i2c_slave_read_packet_wait(
mbed_official 579:53297373a894 448 struct i2c_slave_module *const module,
mbed_official 579:53297373a894 449 struct i2c_slave_packet *const packet)
mbed_official 579:53297373a894 450 {
mbed_official 579:53297373a894 451 /* Sanity check arguments. */
mbed_official 579:53297373a894 452 Assert(module);
mbed_official 579:53297373a894 453 Assert(module->hw);
mbed_official 579:53297373a894 454 Assert(packet);
mbed_official 579:53297373a894 455
mbed_official 579:53297373a894 456 SercomI2cs *const i2c_hw = &(module->hw->I2CS);
mbed_official 579:53297373a894 457
mbed_official 579:53297373a894 458 uint16_t length = packet->data_length;
mbed_official 579:53297373a894 459
mbed_official 579:53297373a894 460 if (length == 0) {
mbed_official 579:53297373a894 461 return STATUS_ERR_INVALID_ARG;
mbed_official 579:53297373a894 462 }
mbed_official 579:53297373a894 463
mbed_official 579:53297373a894 464 #if I2C_SLAVE_CALLBACK_MODE == true
mbed_official 579:53297373a894 465 /* Check if the module is busy with a job or AMATCH is enabled */
mbed_official 579:53297373a894 466 if (module->buffer_remaining > 0 ||
mbed_official 579:53297373a894 467 (i2c_hw->INTENSET.reg & SERCOM_I2CS_INTFLAG_AMATCH)) {
mbed_official 579:53297373a894 468 return STATUS_BUSY;
mbed_official 579:53297373a894 469 }
mbed_official 579:53297373a894 470 #endif
mbed_official 579:53297373a894 471
mbed_official 579:53297373a894 472 enum status_code status;
mbed_official 579:53297373a894 473
mbed_official 579:53297373a894 474 /* Wait for master to send address packet */
mbed_official 579:53297373a894 475 status = _i2c_slave_wait_for_bus(module);
mbed_official 579:53297373a894 476 if (status != STATUS_OK) {
mbed_official 579:53297373a894 477 /* Timeout, return */
mbed_official 579:53297373a894 478 return status;
mbed_official 579:53297373a894 479 }
mbed_official 579:53297373a894 480
mbed_official 579:53297373a894 481 if (!(i2c_hw->INTFLAG.reg & SERCOM_I2CS_INTFLAG_AMATCH)) {
mbed_official 579:53297373a894 482 /* Not address interrupt, something is wrong */
mbed_official 579:53297373a894 483 return STATUS_ERR_DENIED;
mbed_official 579:53297373a894 484 }
mbed_official 579:53297373a894 485
mbed_official 579:53297373a894 486 /* Check if there was an error in the last transfer */
mbed_official 579:53297373a894 487 if (i2c_hw->STATUS.reg & (SERCOM_I2CS_STATUS_BUSERR |
mbed_official 579:53297373a894 488 SERCOM_I2CS_STATUS_COLL | SERCOM_I2CS_STATUS_LOWTOUT)) {
mbed_official 579:53297373a894 489 return STATUS_ERR_IO;
mbed_official 579:53297373a894 490 }
mbed_official 579:53297373a894 491 /* Check direction */
mbed_official 579:53297373a894 492 if ((i2c_hw->STATUS.reg & SERCOM_I2CS_STATUS_DIR)) {
mbed_official 579:53297373a894 493 /* Read request from master, send NACK and return */
mbed_official 579:53297373a894 494 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_ACKACT;
mbed_official 579:53297373a894 495 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_CMD(0x3);
mbed_official 579:53297373a894 496 return STATUS_ERR_BAD_FORMAT;
mbed_official 579:53297373a894 497 }
mbed_official 579:53297373a894 498
mbed_official 579:53297373a894 499 /* Write request from master, ACK address */
mbed_official 579:53297373a894 500 i2c_hw->CTRLB.reg &= ~SERCOM_I2CS_CTRLB_ACKACT;
mbed_official 579:53297373a894 501 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_CMD(0x3);
mbed_official 579:53297373a894 502
mbed_official 579:53297373a894 503 uint16_t i = 0;
mbed_official 579:53297373a894 504 while (length--) {
mbed_official 579:53297373a894 505
mbed_official 579:53297373a894 506 /* Wait for next byte or stop condition */
mbed_official 579:53297373a894 507 status = _i2c_slave_wait_for_bus(module);
mbed_official 579:53297373a894 508 if (status != STATUS_OK) {
mbed_official 579:53297373a894 509 /* Timeout, return */
mbed_official 579:53297373a894 510 return status;
mbed_official 579:53297373a894 511 }
mbed_official 579:53297373a894 512
mbed_official 579:53297373a894 513 if ((i2c_hw->INTFLAG.reg & SERCOM_I2CS_INTFLAG_PREC) ||
mbed_official 579:53297373a894 514 i2c_hw->INTFLAG.reg & SERCOM_I2CS_INTFLAG_AMATCH) {
mbed_official 579:53297373a894 515 /* Master sent stop condition, or repeated start, read done */
mbed_official 579:53297373a894 516 /* Clear stop flag */
mbed_official 579:53297373a894 517 i2c_hw->INTFLAG.reg = SERCOM_I2CS_INTFLAG_PREC;
mbed_official 579:53297373a894 518 return STATUS_ABORTED;
mbed_official 579:53297373a894 519 }
mbed_official 579:53297373a894 520
mbed_official 579:53297373a894 521 /* Read data */
mbed_official 579:53297373a894 522 _i2c_slave_wait_for_sync(module);
mbed_official 579:53297373a894 523 packet->data[i++] = i2c_hw->DATA.reg;
mbed_official 579:53297373a894 524
mbed_official 579:53297373a894 525 }
mbed_official 579:53297373a894 526
mbed_official 579:53297373a894 527 /* Packet read done, wait for packet to NACK, Stop or repeated start */
mbed_official 579:53297373a894 528 status = _i2c_slave_wait_for_bus(module);
mbed_official 579:53297373a894 529
mbed_official 579:53297373a894 530 if (i2c_hw->INTFLAG.reg & SERCOM_I2CS_INTFLAG_DRDY) {
mbed_official 579:53297373a894 531 /* Buffer is full, send NACK */
mbed_official 579:53297373a894 532 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_ACKACT;
mbed_official 579:53297373a894 533 i2c_hw->CTRLB.reg |= SERCOM_I2CS_CTRLB_CMD(0x2);
mbed_official 579:53297373a894 534 }
mbed_official 579:53297373a894 535 if (i2c_hw->INTFLAG.reg & SERCOM_I2CS_INTFLAG_PREC) {
mbed_official 579:53297373a894 536 /* Clear stop flag */
mbed_official 579:53297373a894 537 i2c_hw->INTFLAG.reg = SERCOM_I2CS_INTFLAG_PREC;
mbed_official 579:53297373a894 538 }
mbed_official 579:53297373a894 539 return STATUS_OK;
mbed_official 579:53297373a894 540 }
mbed_official 579:53297373a894 541
mbed_official 579:53297373a894 542 /**
mbed_official 579:53297373a894 543 * \brief Waits for a start condition on the bus
mbed_official 579:53297373a894 544 *
mbed_official 579:53297373a894 545 * \note This function is only available for 7-bit slave addressing.
mbed_official 579:53297373a894 546 *
mbed_official 579:53297373a894 547 * Waits for the master to issue a start condition on the bus.
mbed_official 579:53297373a894 548 * Note that this function does not check for errors in the last transfer,
mbed_official 579:53297373a894 549 * this will be discovered when reading or writing.
mbed_official 579:53297373a894 550 *
mbed_official 579:53297373a894 551 * \param[in] module Pointer to software module structure
mbed_official 579:53297373a894 552 *
mbed_official 579:53297373a894 553 * \return Direction of the current transfer, when in slave mode.
mbed_official 579:53297373a894 554 * \retval I2C_SLAVE_DIRECTION_NONE No request from master within timeout
mbed_official 579:53297373a894 555 * period
mbed_official 579:53297373a894 556 * \retval I2C_SLAVE_DIRECTION_READ Write request from master
mbed_official 579:53297373a894 557 * \retval I2C_SLAVE_DIRECTION_WRITE Read request from master
mbed_official 579:53297373a894 558 */
mbed_official 579:53297373a894 559 enum i2c_slave_direction i2c_slave_get_direction_wait(
mbed_official 579:53297373a894 560 struct i2c_slave_module *const module)
mbed_official 579:53297373a894 561 {
mbed_official 579:53297373a894 562 /* Sanity check arguments. */
mbed_official 579:53297373a894 563 Assert(module);
mbed_official 579:53297373a894 564 Assert(module->hw);
mbed_official 579:53297373a894 565
mbed_official 579:53297373a894 566 SercomI2cs *const i2c_hw = &(module->hw->I2CS);
mbed_official 579:53297373a894 567
mbed_official 579:53297373a894 568 enum status_code status;
mbed_official 579:53297373a894 569
mbed_official 579:53297373a894 570 /* Wait for address interrupt */
mbed_official 579:53297373a894 571 status = _i2c_slave_wait_for_bus(module);
mbed_official 579:53297373a894 572
mbed_official 579:53297373a894 573 if (status != STATUS_OK) {
mbed_official 579:53297373a894 574 /* Timeout, return */
mbed_official 579:53297373a894 575 return I2C_SLAVE_DIRECTION_NONE;
mbed_official 579:53297373a894 576 }
mbed_official 579:53297373a894 577
mbed_official 579:53297373a894 578 if (!(i2c_hw->INTFLAG.reg & SERCOM_I2CS_INTFLAG_AMATCH)) {
mbed_official 579:53297373a894 579 /* Not address interrupt, something is wrong */
mbed_official 579:53297373a894 580 return I2C_SLAVE_DIRECTION_NONE;
mbed_official 579:53297373a894 581 }
mbed_official 579:53297373a894 582
mbed_official 579:53297373a894 583 /* Check direction */
mbed_official 579:53297373a894 584 if ((i2c_hw->STATUS.reg & SERCOM_I2CS_STATUS_DIR)) {
mbed_official 579:53297373a894 585 /* Read request from master */
mbed_official 579:53297373a894 586 return I2C_SLAVE_DIRECTION_WRITE;
mbed_official 579:53297373a894 587 } else {
mbed_official 579:53297373a894 588 /* Write request from master */
mbed_official 579:53297373a894 589 return I2C_SLAVE_DIRECTION_READ;
mbed_official 579:53297373a894 590 }
mbed_official 579:53297373a894 591 }
mbed_official 579:53297373a894 592
mbed_official 579:53297373a894 593 /**
mbed_official 579:53297373a894 594 * \brief Retrieves the current module status
mbed_official 579:53297373a894 595 *
mbed_official 579:53297373a894 596 * Checks the status of the module and returns it as a bitmask of status
mbed_official 579:53297373a894 597 * flags.
mbed_official 579:53297373a894 598 *
mbed_official 579:53297373a894 599 * \param[in] module Pointer to the I<SUP>2</SUP>C slave software device struct
mbed_official 579:53297373a894 600 *
mbed_official 579:53297373a894 601 * \return Bitmask of status flags.
mbed_official 579:53297373a894 602 *
mbed_official 579:53297373a894 603 * \retval I2C_SLAVE_STATUS_ADDRESS_MATCH A valid address has been received
mbed_official 579:53297373a894 604 * \retval I2C_SLAVE_STATUS_DATA_READY A I<SUP>2</SUP>C slave byte transmission is
mbed_official 579:53297373a894 605 * successfully completed
mbed_official 579:53297373a894 606 * \retval I2C_SLAVE_STATUS_STOP_RECEIVED A stop condition is detected for a
mbed_official 579:53297373a894 607 * transaction being processed
mbed_official 579:53297373a894 608 * \retval I2C_SLAVE_STATUS_CLOCK_HOLD The slave is holding the SCL line
mbed_official 579:53297373a894 609 * low
mbed_official 579:53297373a894 610 * \retval I2C_SLAVE_STATUS_SCL_LOW_TIMEOUT An SCL low time-out has occurred
mbed_official 579:53297373a894 611 * \retval I2C_SLAVE_STATUS_REPEATED_START Indicates a repeated start, only
mbed_official 579:53297373a894 612 * valid if \ref
mbed_official 579:53297373a894 613 * I2C_SLAVE_STATUS_ADDRESS_MATCH is
mbed_official 579:53297373a894 614 * set
mbed_official 579:53297373a894 615 * \retval I2C_SLAVE_STATUS_RECEIVED_NACK The last data packet sent was not
mbed_official 579:53297373a894 616 * acknowledged
mbed_official 579:53297373a894 617 * \retval I2C_SLAVE_STATUS_COLLISION The I<SUP>2</SUP>C slave was not able to
mbed_official 579:53297373a894 618 * transmit a high data or NACK bit
mbed_official 579:53297373a894 619 * \retval I2C_SLAVE_STATUS_BUS_ERROR An illegal bus condition has
mbed_official 579:53297373a894 620 * occurred on the bus
mbed_official 579:53297373a894 621 */
mbed_official 579:53297373a894 622 uint32_t i2c_slave_get_status(
mbed_official 579:53297373a894 623 struct i2c_slave_module *const module)
mbed_official 579:53297373a894 624 {
mbed_official 579:53297373a894 625 /* Sanity check arguments */
mbed_official 579:53297373a894 626 Assert(module);
mbed_official 579:53297373a894 627 Assert(module->hw);
mbed_official 579:53297373a894 628
mbed_official 579:53297373a894 629 SercomI2cs *const i2c_hw = &(module->hw->I2CS);
mbed_official 579:53297373a894 630
mbed_official 579:53297373a894 631 uint8_t intflags = i2c_hw->INTFLAG.reg;
mbed_official 579:53297373a894 632 uint8_t status = i2c_hw->STATUS.reg;
mbed_official 579:53297373a894 633 uint32_t status_flags = 0;
mbed_official 579:53297373a894 634
mbed_official 579:53297373a894 635 /* Check Address Match flag */
mbed_official 579:53297373a894 636 if (intflags & SERCOM_I2CS_INTFLAG_AMATCH) {
mbed_official 579:53297373a894 637 status_flags |= I2C_SLAVE_STATUS_ADDRESS_MATCH;
mbed_official 579:53297373a894 638 }
mbed_official 579:53297373a894 639 /* Check Data Ready flag */
mbed_official 579:53297373a894 640 if (intflags & SERCOM_I2CS_INTFLAG_DRDY) {
mbed_official 579:53297373a894 641 status_flags |= I2C_SLAVE_STATUS_DATA_READY;
mbed_official 579:53297373a894 642 }
mbed_official 579:53297373a894 643 /* Check Stop flag */
mbed_official 579:53297373a894 644 if (intflags & SERCOM_I2CS_INTFLAG_PREC) {
mbed_official 579:53297373a894 645 status_flags |= I2C_SLAVE_STATUS_STOP_RECEIVED;
mbed_official 579:53297373a894 646 }
mbed_official 579:53297373a894 647 /* Check Clock Hold */
mbed_official 579:53297373a894 648 if (status & SERCOM_I2CS_STATUS_CLKHOLD) {
mbed_official 579:53297373a894 649 status_flags |= I2C_SLAVE_STATUS_CLOCK_HOLD;
mbed_official 579:53297373a894 650 }
mbed_official 579:53297373a894 651 /* Check SCL Low Timeout */
mbed_official 579:53297373a894 652 if (status & SERCOM_I2CS_STATUS_LOWTOUT) {
mbed_official 579:53297373a894 653 status_flags |= I2C_SLAVE_STATUS_SCL_LOW_TIMEOUT;
mbed_official 579:53297373a894 654 }
mbed_official 579:53297373a894 655 /* Check Repeated Start */
mbed_official 579:53297373a894 656 if (status & SERCOM_I2CS_STATUS_SR) {
mbed_official 579:53297373a894 657 status_flags |= I2C_SLAVE_STATUS_REPEATED_START;
mbed_official 579:53297373a894 658 }
mbed_official 579:53297373a894 659 /* Check Received Not Acknowledge */
mbed_official 579:53297373a894 660 if (status & SERCOM_I2CS_STATUS_RXNACK) {
mbed_official 579:53297373a894 661 status_flags |= I2C_SLAVE_STATUS_RECEIVED_NACK;
mbed_official 579:53297373a894 662 }
mbed_official 579:53297373a894 663 /* Check Transmit Collision */
mbed_official 579:53297373a894 664 if (status & SERCOM_I2CS_STATUS_COLL) {
mbed_official 579:53297373a894 665 status_flags |= I2C_SLAVE_STATUS_COLLISION;
mbed_official 579:53297373a894 666 }
mbed_official 579:53297373a894 667 /* Check Bus Error */
mbed_official 579:53297373a894 668 if (status & SERCOM_I2CS_STATUS_BUSERR) {
mbed_official 579:53297373a894 669 status_flags |= I2C_SLAVE_STATUS_BUS_ERROR;
mbed_official 579:53297373a894 670 }
mbed_official 579:53297373a894 671
mbed_official 579:53297373a894 672 return status_flags;
mbed_official 579:53297373a894 673 }
mbed_official 579:53297373a894 674
mbed_official 579:53297373a894 675 /**
mbed_official 579:53297373a894 676 * \brief Clears a module status flag
mbed_official 579:53297373a894 677 *
mbed_official 579:53297373a894 678 * Clears the given status flag of the module.
mbed_official 579:53297373a894 679 *
mbed_official 579:53297373a894 680 * \note Not all status flags can be cleared.
mbed_official 579:53297373a894 681 *
mbed_official 579:53297373a894 682 * \param[in] module Pointer to the I<SUP>2</SUP>C software device struct
mbed_official 579:53297373a894 683 * \param[in] status_flags Bit mask of status flags to clear
mbed_official 579:53297373a894 684 *
mbed_official 579:53297373a894 685 */
mbed_official 579:53297373a894 686 void i2c_slave_clear_status(
mbed_official 579:53297373a894 687 struct i2c_slave_module *const module,
mbed_official 579:53297373a894 688 uint32_t status_flags)
mbed_official 579:53297373a894 689 {
mbed_official 579:53297373a894 690 /* Sanity check arguments */
mbed_official 579:53297373a894 691 Assert(module);
mbed_official 579:53297373a894 692 Assert(module->hw);
mbed_official 579:53297373a894 693
mbed_official 579:53297373a894 694 SercomI2cs *const i2c_hw = &(module->hw->I2CS);
mbed_official 579:53297373a894 695
mbed_official 579:53297373a894 696 /* Clear Address Match flag */
mbed_official 579:53297373a894 697 if (status_flags & I2C_SLAVE_STATUS_ADDRESS_MATCH) {
mbed_official 579:53297373a894 698 i2c_hw->INTFLAG.reg = SERCOM_I2CS_INTFLAG_AMATCH;
mbed_official 579:53297373a894 699 }
mbed_official 579:53297373a894 700 /* Clear Data Ready flag */
mbed_official 579:53297373a894 701 if (status_flags & I2C_SLAVE_STATUS_DATA_READY) {
mbed_official 579:53297373a894 702 i2c_hw->INTFLAG.reg = SERCOM_I2CS_INTFLAG_DRDY;
mbed_official 579:53297373a894 703 }
mbed_official 579:53297373a894 704 /* Clear Stop flag */
mbed_official 579:53297373a894 705 if (status_flags & I2C_SLAVE_STATUS_STOP_RECEIVED) {
mbed_official 579:53297373a894 706 i2c_hw->INTFLAG.reg = SERCOM_I2CS_INTFLAG_PREC;
mbed_official 579:53297373a894 707 }
mbed_official 579:53297373a894 708 /* Clear SCL Low Timeout */
mbed_official 579:53297373a894 709 if (status_flags & I2C_SLAVE_STATUS_SCL_LOW_TIMEOUT) {
mbed_official 579:53297373a894 710 i2c_hw->STATUS.reg = SERCOM_I2CS_STATUS_LOWTOUT;
mbed_official 579:53297373a894 711 }
mbed_official 579:53297373a894 712 /* Clear Transmit Collision */
mbed_official 579:53297373a894 713 if (status_flags & I2C_SLAVE_STATUS_COLLISION) {
mbed_official 579:53297373a894 714 i2c_hw->STATUS.reg = SERCOM_I2CS_STATUS_COLL;
mbed_official 579:53297373a894 715 }
mbed_official 579:53297373a894 716 /* Clear Bus Error */
mbed_official 579:53297373a894 717 if (status_flags & I2C_SLAVE_STATUS_BUS_ERROR) {
mbed_official 579:53297373a894 718 i2c_hw->STATUS.reg = SERCOM_I2CS_STATUS_BUSERR;
mbed_official 579:53297373a894 719 }
mbed_official 579:53297373a894 720 }