'Raspberry Pi Pico DMA to a I2C device

I'm using the C/C++ SDK of the Pi Pico and trying to use the DMA to read I2C data in the background. However, there is no example script in Pico-Examples that shows how to use the DMA to read from I2C. There is one for SPI, called spi-dma. But It doesn't directly correlate to I2C because I have to give the device address too along with the register address for I2C.

Can anyone help me understand what to change in the following lines for it to work with an I2C device?

const uint dma_rx = dma_claim_unused_channel(true);

static uint8_t rxbuf[1024];

dma_channel_config c = dma_channel_get_default_config(dma_rx);

channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
channel_config_set_dreq(&c, spi_get_dreq(spi_default, false));
channel_config_set_read_increment(&c, false);
channel_config_set_write_increment(&c, true);
dma_channel_configure(dma_rx, &c,
                      rxbuf, // write address
                      &spi_get_hw(spi_default)->dr, // read address
                      TEST_SIZE, // element count (each element is of size transfer_data_size)
                      false); // don't start yet

dma_start_channel_mask(1u << dma_rx);
dma_channel_wait_for_finish_blocking(dma_rx);
dma_channel_unclaim(dma_rx);

I Know a few changes to be made like

channel_config_set_dreq(&c, i2c_get_dreq(i2c_default, false));
dma_channel_configure(dma_rx, &c,
                     rxbuf, // write address
                     i2c_get_hw(i2c_default), // read address
                     TEST_SIZE, // element count (each element is of size transfer_data_size)
                     true); // don't start yet

But what more after this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source