Microchip® Advanced Software Framework

Quick Start Guide for Using DMA with SERCOM I2C Slave

The supported board list:

  • SAMD21 Xplained Pro
  • SAMR21 Xplained Pro
  • SAML21 Xplained Pro
  • SAML22 Xplained Pro
  • SAMDA1 Xplained Pro
  • SAMC21 Xplained Pro
  • SAMHA1G16A Xplained Pro

In this use case, the I2C will used and set up as follows:

  • Slave mode
  • 100KHz operation speed
  • Not operational in standby
  • 65535 unknown bus state timeout value

Prerequisites

The device must be connected to an I2C slave.

Setup

Code

The following must be added to the user application:

Workflow

  1. Configure and enable module:
    {
    /* Create and initialize config_i2c_slave structure */
    struct i2c_slave_config config_i2c_slave;
    i2c_slave_get_config_defaults(&config_i2c_slave);
    /* Change address and address_mode */
    config_i2c_slave.address = SLAVE_ADDRESS;
    config_i2c_slave.address_mode = I2C_SLAVE_ADDRESS_MODE_MASK;
    config_i2c_slave.buffer_timeout = 1000;
    #if SAMR30
    config_i2c_slave.pinmux_pad0 = CONF_SLAVE_SDA_PINMUX;
    config_i2c_slave.pinmux_pad1 = CONF_SLAVE_SCK_PINMUX;
    #endif
    /* Initialize and enable device with config_i2c_slave */
    i2c_slave_init(&i2c_slave_instance, CONF_I2C_SLAVE_MODULE, &config_i2c_slave);
    }
    1. Create and initialize configuration structure.
      struct i2c_slave_config config_i2c_slave;
      i2c_slave_get_config_defaults(&config_i2c_slave);
    2. Change settings in the configuration.
      config_i2c_slave.address = SLAVE_ADDRESS;
      config_i2c_slave.address_mode = I2C_SLAVE_ADDRESS_MODE_MASK;
      config_i2c_slave.buffer_timeout = 1000;
      #if SAMR30
      config_i2c_slave.pinmux_pad0 = CONF_SLAVE_SDA_PINMUX;
      config_i2c_slave.pinmux_pad1 = CONF_SLAVE_SCK_PINMUX;
      #endif
    3. Initialize the module with the set configurations.
      i2c_slave_init(&i2c_slave_instance, CONF_I2C_SLAVE_MODULE, &config_i2c_slave);
    4. Enable the module.
  2. Configure DMA
    1. Create a DMA resource configuration structure, which can be filled out to adjust the configuration of a single DMA transfer.
      struct dma_resource_config config;
    2. Initialize the DMA resource configuration struct with the module's default values.
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    3. Set extra configurations for the DMA resource. It is using peripheral trigger. SERCOM RX trigger causes a beat transfer in this example.
      config.peripheral_trigger = CONF_I2C_DMA_TRIGGER;
      config.trigger_action = DMA_TRIGGER_ACTION_BEAT;
    4. Allocate a DMA resource with the configurations.
      dma_allocate(resource, &config);
    5. Create a DMA transfer descriptor configuration structure, which can be filled out to adjust the configuration of a single DMA transfer.
      struct dma_descriptor_config descriptor_config;
    6. Initialize the DMA transfer descriptor configuration struct with the module's default values.
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    7. Set the specific parameters for a DMA transfer with transfer size, source address, and destination address.
      descriptor_config.beat_size = DMA_BEAT_SIZE_BYTE;
      descriptor_config.src_increment_enable = false;
      descriptor_config.block_transfer_count = DATA_LENGTH;
      descriptor_config.destination_address = (uint32_t)read_buffer + DATA_LENGTH;
      descriptor_config.source_address =
      (uint32_t)(&i2c_slave_instance.hw->I2CS.DATA.reg);
    8. Create the DMA transfer descriptor.
      dma_descriptor_create(descriptor, &descriptor_config);

Implementation

Code

Add to user application main():

while (true) {
SERCOM_I2CS_INTFLAG_AMATCH) {
SERCOM_I2CS_INTFLAG_AMATCH);
}
}

Workflow

  1. Start to wait a packet from master.
  2. Once data ready, clear the address match status.
    while (true) {
    SERCOM_I2CS_INTFLAG_AMATCH) {
    SERCOM_I2CS_INTFLAG_AMATCH);
    }
    }