2022-08-17 05:39 AM
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,
2022-08-17 07:28 AM
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.
2022-08-17 05:21 PM
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 .
2022-08-18 02:51 AM
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 ?
2022-08-18 05:28 PM
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.