Hi @andrewsglenn
To streamline the use of a single callback function for multiple I2C peripherals with DMA, you can maintain an array of user data pointers and set them directly when initializing each I2C DMA handle. This approach avoids the need to declare individual pointer variables for each I2C instance.
1. Define the User Data Structure and Array:
Define your user data structure and create an array to store user data for each I2C instance.
struct my_user_data {
// Add your specific fields here
int some_field;
};
struct my_user_data user_data[4];
2. Declare the I2C DMA Handles:
Declare an array of i2c_master_dma_handle_t to store handles for each I2C instance.
i2c_master_dma_handle_t i2c_dma_handles[4];
3. Initialize I2C DMA Handles with a Loop:
Use a loop to initialize the I2C instances and set the user data pointers.
// Array of I2C base addresses for the Flexcomm interfaces
I2C_Type *i2c_bases[4] = {I2C2, I2C3, I2C4, I2C5};
for (int i = 0; i < 4; i++) {
// Initialize the I2C instance (configure I2C peripheral, clock, etc.)
I2C_MasterInit(i2c_bases[i], &i2c_master_config, CLOCK_GetFreq(kCLOCK_Flexcomm2 + i));
// Initialize the DMA for this I2C instance
I2C_MasterCreateHandleDMA(i2c_bases[i], &i2c_dma_handles[i], i2c_master_dma_callback, &user_data[i], &dma_handle);
}
4. Define the Callback Function:
Define a callback function that can handle callbacks for any of the I2C instances.
void i2c_master_dma_callback(I2C_Type *base, i2c_master_dma_handle_t *handle, status_t status, void *userData) {
struct my_user_data *data = (struct my_user_data *)userData;
// Process the callback using the user data
// Example: data->some_field = status;
}
BR
Hang