17.8.3.2.1 Code
The following must be added to the user application:
- A sample buffer to send, number of entries to send and address of slave:
Number of times to try to send packet if it fails:#define DATA_LENGTH 10staticuint8_t buffer[DATA_LENGTH] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,};#define SLAVE_ADDRESS 0x12#define TIMEOUT 1000 - Globally accessible module structure:
structi2c_master_module i2c_master_instance; - Function for setting up the module:
staticvoidconfigure_i2c_master(void){/* Initialize config structure and software module. */structi2c_master_config config_i2c_master;i2c_master_get_config_defaults(&config_i2c_master);/* Change buffer timeout to something longer. */config_i2c_master.buffer_timeout = 10000;/* Initialize and enable device with config. */i2c_master_init(&i2c_master_instance, CONF_I2C_MASTER_MODULE, &config_i2c_master);i2c_master_enable(&i2c_master_instance);} - Globally accessible DMA module structure:
structdma_resource example_resource; - Globally transfer done flag:
staticvolatilebooltransfer_is_done =false; - Globally accessible DMA transfer descriptor:
COMPILER_ALIGNED(16)DmacDescriptor example_descriptor; - Function for transfer done callback:
staticvoidtransfer_done(structdma_resource*constresource ){UNUSED(resource);transfer_is_done =true;} - Function for setting up the DMA resource:
staticvoidconfigure_dma_resource(structdma_resource *resource){structdma_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:
staticvoidsetup_dma_descriptor(DmacDescriptor *descriptor){structdma_descriptor_config descriptor_config;dma_descriptor_get_config_defaults(&descriptor_config);descriptor_config.beat_size = DMA_BEAT_SIZE_BYTE;descriptor_config.dst_increment_enable =false;descriptor_config.block_transfer_count = DATA_LENGTH;descriptor_config.source_address = (uint32_t)buffer + DATA_LENGTH;descriptor_config.destination_address =(uint32_t)(&i2c_master_instance.hw->I2CM.DATA.reg);dma_descriptor_create(descriptor, &descriptor_config);} - Add to user application main():
configure_i2c_master();configure_dma_resource(&example_resource);setup_dma_descriptor(&example_descriptor);dma_add_descriptor(&example_resource, &example_descriptor);dma_register_callback(&example_resource, transfer_done,DMA_CALLBACK_TRANSFER_DONE);dma_enable_callback(&example_resource, DMA_CALLBACK_TRANSFER_DONE);
