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.
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 });
This API does not offer any configuration utilities. To setup the hardware I2C, please use the Cube.
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 });
To send any amount of data to a device, use the TransmitFrame
method.
This method has three overloads:
-
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])));
-
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 });
-
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);
To send any amount of data to a device's register, use the TransmitFrameToRegister
method.
This method has three overloads:
-
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]));
-
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 });
-
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);
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);
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);