embeddedintegrationcodebase

Code Structure: Multiple Devices on I2C line, with multiple source files


I am working on programming a TI MSP432. We have 4 different devices on a single I2C line.

In order to keep our codebase legible we keep the functions pertaining to different devices in different C files. The files were originally developed with only one device on the I2C line, and therefore the I2C interrupts are in the individual files.

What is the proper way to integrate these files? Do we just put the I2C interrupt in the main file, and have it call a function in each file? Or somehow dynamically change what interrupt is called, or something else?


Solution

  • You should create a single generic I2C device driver and implement the individual devices in terms of that driver. How you do that precisely depends on a number of issues such as whether you are using an RTOS or multi-threading environment.

    For example you might have an EEPROM device, which would use the I2C driver which would manage any mutual exclusion or enquiring of transactions on the bus depending on the needs of your application, while EEPROM device interface handles perhaps mutual exclusive access to the EEPROM itself; separating the needs of the bus from that of the device and ultimately the application.