Skip to main content
AR_26
Associate II
December 1, 2020
Question

Not able to read data from STM32G031 Mcu I2C slave

  • December 1, 2020
  • 2 replies
  • 954 views

I have configured my stm32g031 mcu as an i2c slave. My PC terminal is my master device and is connected to mcu via FTDI cable. When I do a i2c write, I can see mcu receives the data. But when I do an i2c read, I can see it reads 255 on my pc terminal.

When I debug my code, I can see the control coming to ,

uint8_t id = 0xA0;
HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, &id, 1, I2C_LAST_FRAME);

But the mcu does not sends data out.

Below is the python code I use to communicate with MCU,

# Instantiate an I2C controller
i2c = I2cController()
 
# Configure the first interface (IF/1) of the FTDI device as an I2C master
i2c.configure('ftdi://ftdi:2232h/1')
 
# Get a port to an I2C slave device
slave = i2c.get_port(0x30)
 
#Write data
slave.write(b'\xAA')
 
#Read data
slave.read(1)

Any suggestions what could be wrong here?

This topic has been closed for replies.

2 replies

KnarfB
Super User
December 4, 2020

Whats your complete I2C slave code?

AR_26
AR_26Author
Associate II
December 5, 2020

@KnarfB​ In main function I enable the Listen mode and then use the AddrCallback to receive or transmit data, as below

void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c,
 uint8_t TransferDirection,
 uint16_t AddrMatchCode)
{
	if(hi2c->Instance == I2C1)
	{
		uint8_t id = 0xA0;
		if(TransferDirection == I2C_DIRECTION_TRANSMIT)
			HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, data, sizeof(data),I2C_FIRST_FRAME);
		else
			HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, &id, 1, I2C_LAST_FRAME);
	}
}
 
 
int main(void)
{
 HAL_Init();
 SystemClock_Config();
 MX_I2C1_Init();
 
 while(1) {
 HAL_I2C_EnableListen_IT(&hi2c1);
 }
}