cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S003F3 I2C Slave Not Working - Troubleshooting Assistance Needed

jhkim
Associate

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.

 
* stm8_interrupt_vector.c
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
 * Copyright (c) 2007 STMicroelectronics
 */
 
typedef void @far (*interrupt_handler_t)(void);
 
struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};
 
@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development, 
   it is recommended to set a breakpoint on the following instruction
*/
return;
}
 
extern void _stext();     /* startup routine */
 
struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap  */
{0x82, NonHandledInterrupt}, /* irq0  */
{0x82, NonHandledInterrupt}, /* irq1  */
{0x82, NonHandledInterrupt}, /* irq2  */
{0x82, NonHandledInterrupt}, /* irq3  */
{0x82, NonHandledInterrupt}, /* irq4  */
{0x82, NonHandledInterrupt}, /* irq5  */
{0x82, NonHandledInterrupt}, /* irq6  */
{0x82, NonHandledInterrupt}, /* irq7  */
{0x82, NonHandledInterrupt}, /* irq8  */
{0x82, NonHandledInterrupt}, /* irq9  */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};

 

 

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?

 

 

2 REPLIES 2
AA1
Senior III

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);

 

jhkim
Associate

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.