I2C Module - smartel99/NilaiTFO GitHub Wiki

The I2C module is designed to be used in master mode uniquely. Slave mode will be coming in a future version.

Initialization

To instantiate an I2C module in your application, you must first configure it in the CubeMX software. Don't forget to call the MX_I2Cx_Init function before instantiating the I2C module!

To instantiate the module in your application:

void MyApplication::InitializeModules()
{
  AddModule(new I2cModule(&hi2c1, "i2c1");
}

The constructor of I2cModule takes a pointer to a I2C_HandleTypeDef created by the Cube and a string that is used to identify the module.

To simplify access to this new instance, you can create a macro that will get that instance in the application:

#define I2C1_MODULE    static_cast<I2cModule*>(MyApplication::GetModule("i2c1")

This macro allows you to use the module like this:

I2C1_MODULE->TransmitFrame(0x40, { 0x00, 0x01, 0x02 });

Configuration

This API does not offer any configuration utilities. To setup the hardware I2C, please use the Cube.

Usage

I2C::Frame

This structure is used when using the I2cModule. It contains the following members:

  • deviceAddress [uint8_t]: The address of the device to talk to.
  • registerAddress [uint8_t]: The address of the register in the device.
  • data [std::vector<uint8_t>]: The data to send to or that was received from the device.

To construct a I2C::Frame

// Want to send 3 bytes of data to device with address 0x40:
I2C::Frame toDevice = I2C::Frame(0x40, { 0x00, 0x01, 0x02 };

// Want to write 0x00 in register 0x51 of device at address 0x40:
I2C::Frame toRegister = I2C::Frame(0x40, 0x51, { 0x00 });

Sending Data to a Device

To send any amount of data to a device, use the TransmitFrame method. This method has three overloads:

TransmitFrame(uint8_t addr, const uint8_t* data, size_t len)

  • addr: The device's address
  • data: Pointer to the data to send
  • len: Number of bytes of data to send
// Send 3 bytes of data to device at address 0x40:
uint8_t data[3] = { 0x01, 0x02, 0x03 };
I2C1_MODULE->TransmitFrame(0x40, data, (sizeof(data)/sizeof(data[0])));

TransmitFrame(uint8_t addr, const std::vector<uint8_t>& data)

  • addr: The device's address
  • data: Vector containing the data to send
// Send 3 bytes of data to device at address 0x40:
I2C1_MODULE->TransmitFrame(0x40, { 0x01, 0x02, 0x03 });

TransmitFrame(const I2C::Frame& frame)

  • frame: Structure containing the device's address and the data to send
// Send 3 bytes of data to device at address 0x40:
I2C::Frame f;
f.deviceAddress = 0x40;
f.data = { 0x01, 0x02, 0x03 });
I2C1_MODULE->TransmitFrame(f);

Sending Data to a Device's Register

To send any amount of data to a device's register, use the TransmitFrameToRegister method. This method has three overloads:

TransmitFrameToRegister(uint8_t addr, uint8_t regAddr, const uint8_t* data, size_t len)

  • addr: The device's address
  • regAddr: The register's address
  • data: Pointer to the data to send
  • len: The number of bytes of data to send
// Send 3 bytes of data to register 0x51 of device at address 0x40:
uint8_t data[3] = { 0x01, 0x02, 0x03 };
I2C1_MODULE->TransmitFrameToRegister(0x40, 0x51, data, (sizeof(data)/sizeof(data[0]));

TransmitFrameToRegister(uint8_t addr, uint8_t regAddr, const std::vector<uint8_t>& data)

  • addr: The device's address
  • regAddr: The register's address
  • data: A vector containing the data to send
// Send 3 bytes of data to register 0x51 of device at address 0x40:
I2C1_MODULE->TransmitFrameToRegister(0x40, 0x51, { 0x01, 0x02, 0x03 });

TransmitFrameToRegister(const I2C::Frame& frame)

  • frame: Structure containing the device address, register address and the data to send
// Send 3 bytes of data to register 0x51 of device at address 0x40:
I2C::Frame f;
f.deviceAddress = 0x40;
f.registerAddress = 0x51;
f.data = { 0x01, 0x02, 0x03 };
I2C1_MODULE->TransmitFrameToRegister(f);

Reading Data from a Device

To receive data from a device, use the ReceiveFrame method. It takes as parameters the device's address and the number of bytes to receive. It returns an I2C::Frame containing the data received.

// Read 3 bytes of data from device at address 0x40:
I2C::Frame f = I2C1_MODULE->ReceiveFrame(0x40, 3);

Reading Data from a Device's Registers

To receive data from a device's registers, use the ReceiveFrameFromRegister method. It takes as parameters the device's address, the register's address and the number of bytes to receive. It returns an I2C::Frame containing the data received.

// Read 3 bytes of data from register 0x51 of device at address 0x40:
I2C::Frame f = I2C1_MODULE->ReceiveFrameFromRegister(0x40, 0x51, 3);
⚠️ **GitHub.com Fallback** ⚠️