STM32 Writing Example
This source code is provided as-is, without any warranties, express or implied, including but not limited to fitness for a particular purpose or merchantability. The user assumes full responsibility for testing, validation, and ensuring compliance with all applicable regulations and standards.
uint8_t IS4310_I2C_Chip_Address; // This variable stores the I2C chip address of the IS4310.
IS4310_I2C_Chip_Address = 0x11; // The IS4310's I2C address is 0x11.
// The STM32 HAL I2C library requires the I2C address to be shifted left by one bit.
// Let's shift the IS4310 I2C address accordingly:
IS4310_I2C_Chip_Address = IS4310_I2C_Chip_Address << 1;
uint16_t Holding_Register_Address_To_Write; // This variable stores the holding register address where we want to write data in the IS4310.
Holding_Register_Address_To_Write = 0; // For this example, we will write to address 0. Addresses 0 to 499 are available.
uint16_t Sensor_Data; // This variable holds the data that will be stored in the holding register.
Sensor_Data = 0x1234; // Let's assume a sensor fills this variable with data, for example: 0x1234.
// The HAL library for writing to I2C memory devices requires the data to be stored in a uint8_t array.
// Therefore, we need to split our uint16_t data into a 2-byte (uint8_t) array.
uint8_t writeValuesArray[2];
writeValuesArray[0] = (uint8_t) (Sensor_Data >> 8); // High byte, we need to store the 0x12 here.
writeValuesArray[1] = (uint8_t) Sensor_Data; // Low byte, we need to store the 0x34 here.
/*
* This is the HAL function to write to an I2C memory device. The IS4310 is designed to operate as an I2C memory
* to simplify communication.
*
* Parameters:
* 1. &hi2c1: The I2C handle configured in CubeMX. Don't forget the '&'.
* 2. IS4310_I2C_Chip_Address: The I2C address of the IS4310 (must be left-shifted).
* 3. Holding_Register_Address_To_Write: The holding register address in the IS4310 where we want to write data.
* 4. I2C_MEMADD_SIZE_16BIT: Specifies the memory addressing size. The IS4310 uses a 16-bit addressing scheme.
* This keyword is an internal constant of HAL libraries.
* 5. writeValuesArray: An 8-bit array containing the data to be written by the HAL function.
* 6. 2: The number of bytes to write. Since one holding register is 16 bits, we need to write 2 bytes.
* 7. 1000: Timeout in milliseconds. If the HAL fails to write within this time, it will skip the operation
* to prevent the code from getting stuck.
*/
HAL_I2C_Mem_Write(&hi2c1, IS4310_I2C_Chip_Address, Holding_Register_Address_To_Write, I2C_MEMADD_SIZE_16BIT, writeValuesArray, 2, 1000);
Last updated