cancel
Showing results for 
Search instead for 
Did you mean: 

I2C not responding to read sequence on STM32L0

Scribe
Associate II

Hi guys,

I'm using the latest ST generated I2C hal driver on an STM32L0. On my I2C master, I'm requesting to read from a register, which sends the following sequence:

Start -> Address (W) -> Register 1B -> Stop -> Start -> Address (R) -> Data 4B -> Stop

On the slave end, it's receiving the address, which is triggering the address callback, it's matching, it requests to receive 1 Byte via HAL_I2C_Slave_Seq_Receive_IT(), it receives this, the listen complete callback is then raised which re-enables the listen callback. It then receives and seems to correctly handle the request for the slave to send the 4 data bytes, however what ends up on the bus is 0xFF for each byte. I'm unsure why the data isn't being transmitted.

I do have clock stretching turned off (as master doesn't support it) but have tried this at low speeds such as 10KHz.

My code is implemented as follows:

// Setup
while( HAL_OK != HAL_I2C_EnableListen_IT( &hi2c1 ) );
 
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
  if(AddrMatchCode == 4)
  {	
		if( TransferDirection == I2C_DIRECTION_TRANSMIT )
		{
			while(HAL_I2C_Slave_Seq_Receive_IT( &hi2c1, buffer, 1, I2C_FIRST_FRAME) != HAL_OK);
			mode = I2C_MODE_SET_REGISTER;
		}
		else
		{			
			if( mode == I2C_MODE_RECEIVE_REGISTER )
			{
				buffer[0] = 0xAA;
				buffer[1] = 0xAA;
				buffer[2] = 0xAA;;
				buffer[3] = 0xAA;
				while(HAL_I2C_Slave_Seq_Receive_IT( &hi2c1, buffer, 4, I2C_LAST_FRAME) != HAL_OK);
				mode = I2C_MODE_NONE;
			}	
		}
	}
}
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
{
	while( HAL_OK != HAL_I2C_EnableListen_IT( &hi2c1 ) );
}
 
void HAL_I2C_SlaveRxCpltCallback( I2C_HandleTypeDef *hi2c )
{
	switch( mode )
	{
		case I2C_MODE_SET_REGISTER:
			currentRegister = buffer[0];
			mode = I2C_MODE_RECEIVE_REGISTER;
			break;
 
		default: break;
	}
}

Any ideas?

Many thanks

2 REPLIES 2

I don't use HAL to know for sure, but it seems you are using the same HAL_I2C_Slave_Seq_Receive_IT function for both receiving and transmitting. Isn't it wrong?

I thought this too, but I've tried both, same result! Also ST example 'I2C_TwoBoards_RestartAdvComIT' uses Receive like this too, I think because it's part of a receive sequence?