cancel
Showing results for 
Search instead for 
Did you mean: 

I2C does not work with SPI Slave mode on STM32L011

SUchi.4
Associate II

I want to use I2C at master mode, and SPI at slave mode.

After HAL_SPI_TransmitReceive_IT api is called, HAL_I2C_Master_Transmit api does not work on STM32L011K4T6.

Before HAL_SPI_TransmitReceive_IT api is called, HAL_I2C_Master_Transmit api does work normally.

For example, below code is not work. Hang up at HAL_I2C_Master_Transmit.

  HAL_I2C_Master_Transmit(&hi2c1, I2C_SLV_ADDR, send_data, 2, I2C_TIMEOUT);
  HAL_I2C_Master_Transmit(&hi2c1, I2C_SLV_ADDR, send_data, 1, I2C_TIMEOUT);
  HAL_I2C_Master_Receive(&hi2c1, I2C_SLV_ADDR, &data, 1, I2C_TIMEOUT);
  printf("I2C READ 0x07 0x%02x\n", data);
 
  HAL_SPI_TransmitReceive_IT(&hspi1, send_buffer, recv_buffer,
                              kSpiTransferSize);
 
  HAL_I2C_Master_Transmit(&hi2c1, I2C_SLV_ADDR, send_data, 2, I2C_TIMEOUT);
  HAL_I2C_Master_Transmit(&hi2c1, I2C_SLV_ADDR, send_data, 1, I2C_TIMEOUT);
  HAL_I2C_Master_Receive(&hi2c1, I2C_SLV_ADDR, &data, 1, I2C_TIMEOUT);
  printf("I2C READ 0x07 0x%02x\n", data);

But, below is work normally.

  HAL_I2C_Master_Transmit(&hi2c1, I2C_SLV_ADDR, send_data, 2, I2C_TIMEOUT);
  HAL_I2C_Master_Transmit(&hi2c1, I2C_SLV_ADDR, send_data, 1, I2C_TIMEOUT);
  HAL_I2C_Master_Receive(&hi2c1, I2C_SLV_ADDR, &data, 1, I2C_TIMEOUT);
  printf("I2C READ 0x07 0x%02x\n", data);
 
  HAL_I2C_Master_Transmit(&hi2c1, I2C_SLV_ADDR, send_data, 2, I2C_TIMEOUT);
  HAL_I2C_Master_Transmit(&hi2c1, I2C_SLV_ADDR, send_data, 1, I2C_TIMEOUT);
  HAL_I2C_Master_Receive(&hi2c1, I2C_SLV_ADDR, &data, 1, I2C_TIMEOUT);
  printf("I2C READ 0x07 0x%02x\n", data);

I can't understand, why HAL_I2C_Master_Transmit does not work after HAL_SPI_TransmitReceive_IT.

Best Regards,

4 REPLIES 4
Mike_ST
ST Employee

Hello,

it is difficult to answer without more information.

What happens when you are debugging ? Does it fall into hardfault_handler ?

Maybe you can share more source code so people can check ?

Regards.

SUchi.4
Associate II

Thank you for your reply.

I attached ioc file and main.c .

The other source codes are not modified.

In my debugging, I checked HAL_I2C_Master_Transmit (main.c line 128) function by step-by-step execution.

At below code in HAL_I2C_Master_Transmit, SDA and SCL signal was falled at the same time (not start condition).

And after that, SDA and SCL remained Low all the time.

      hi2c->XferSize = hi2c->XferCount;
      I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
                         I2C_GENERATE_START_WRITE);

​ Therefore, it hangs on the I2C_WaitOnSTOPFlagUntilTimeout function in HAL_I2C_Master_Transmit .

Mike_ST
ST Employee

Hello,

I have tried to replicate your setup on nucleo 32 STM32L031 board (I don't have L011).

I have no issue going through the HAL_SPI_TransmitReceive_IT function.

But I have not the same I2C/SPI peripherals as you might have on your setup... So it is difficult to compare.

Do you have pull-up resistors on the I2C lines ?

Have you activated the SPI interrupt ?

SUchi.4
Associate II

Hello,

> Do you have pull-up resistors on the I2C lines ?

Yes. HAL_I2C_Master_Transmit before HAL_SPI_TransmitReceive_IT works well.

And if commented out HAL_SPI_TransmitReceive_IT, all HAL_I2C_Master_Transmit work well.

> Have you activated the SPI interrupt ?​

I tried to add HAL_NVIC_EnableIRQ(SPI1_IRQn); like below.

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_DMA_Init();
  MX_SPI1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
 
  HAL_NVIC_EnableIRQ(SPI1_IRQn);

But still the same, HAL_I2C_Master_Transmit after HAL_SPI_TransmitReceive_IT have issue.