2024-07-18 03:10 AM
I'm trying to operate an ESP32 in Arduino as an I2C master and an STM8S003F3 as an I2C slave.
* ESP32 I2C master source code (slaveAddr = 0x30)
void i2c_sendDataToSlave(TwoWire &i2c_device, uint8_t slaveAddr, uint8_t* slaveBuffer, size_t slaveLength)
{
i2c_device.beginTransmission(stm8sAddress);
i2c_device.write(slaveBuffer, slaveLength);
// i2c_device.endTransmission();
uint8_t error = i2c_device.endTransmission();
if (error == 0) {
Serial.println("Data sent successfully");
} else {
Serial.printf("Error sending data: %d\n", error);
}
}
I have verified that the ESP32 is operating with basic I2C master source code.
The I2C slave on the STM8S003F3 is not working.
I used the files from the Slave folder of the I2C_TwoBoards example, based on the source code obtained from here(STSW-STM8069 - STM8S/A Standard peripheral library - STMicroelectronics).
Additionally, I have included the stm8_interrupt_vector.c file that is generated when creating a project in STVD.
The STM8S003F example source code uses a 7-bit slave address of 0x30, which I have matched with the ESP32. However, despite no apparent issues in the source code, I'm curious why it's not communicating properly with the ESP32.
When debugging with UART, using printf("%d \n\r", data); outputs the value of 0x2 as 0x200. How can I modify it to output just 0x2?
2024-07-18 10:47 AM
STM uses an I2C address left-shifted by 1 bit and possibly ESP32 not. On ESP32 try with 'slaveAddr = (0x30 >> 1)'. Also you don't have an I2C interrupt handler and I2C_TwoBoards example have.
printf("%d \n\r", data); should be:
printf("%x \n\r", data); to print as hexadecimal.
I think that data is of type uint8_t, and %x or %d prints the number as uint16_t. Try this:
printf("%x \n\r", (uint16_t)data);
2024-07-19 12:24 AM
I resolved the issue by using and tuning a different example because the example in the STM library seemed to be incorrect.
The issue you mentioned was not related to the address.