18.8.3.2.1 Code

The following must be added to the user application:

  • Address to respond to:
    #define SLAVE_ADDRESS 0x12
    
  • A sample buffer to send, number of entries to send and address of slave:
    #define DATA_LENGTH 10
    uint8_t read_buffer[DATA_LENGTH];
    
  • Globally accessible module structure:
    struct i2c_slave_module i2c_slave_instance;
    
  • Function for setting up the module:
    void configure_i2c_slave(void)
    {
        /* 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;
    
        /* Initialize and enable device with config_i2c_slave */
        i2c_slave_init(&i2c_slave_instance, CONF_I2C_SLAVE_MODULE, &config_i2c_slave);
    
        i2c_slave_enable(&i2c_slave_instance);
    }
    
  • Globally accessible DMA module structure:
    struct dma_resource i2c_dma_resource;
    
  • Globally accessible DMA transfer descriptor:
    COMPILER_ALIGNED(16)
    DmacDescriptor i2c_dma_descriptor;
    
  • Function for setting up the DMA resource:
    void configure_dma_resource(struct dma_resource *resource)
    {
        struct dma_resource_config config;
    
        dma_get_config_defaults(&config);
    
        config.peripheral_trigger = CONF_I2C_DMA_TRIGGER;
        config.trigger_action = DMA_TRIGGER_ACTON_BEAT;
    
        dma_allocate(resource, &config);
    }
    
  • Function for setting up the DMA transfer descriptor:
    void setup_dma_descriptor(DmacDescriptor *descriptor)
    {
        struct dma_descriptor_config descriptor_config;
    
        dma_descriptor_get_config_defaults(&descriptor_config);
    
        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);
    
        dma_descriptor_create(descriptor, &descriptor_config);
    }
    
  • Add to user application main():
    configure_i2c_slave();
    
    configure_dma_resource(&i2c_dma_resource);
    setup_dma_descriptor(&i2c_dma_descriptor);
    dma_add_descriptor(&i2c_dma_resource, &i2c_dma_descriptor);