2021-02-24 08:54 PM
On the latest version of CubeIDE, using CubeMX and HAL libraries, I am completely unable to get any response out of the I2C hardware. With the pins directly connected to a logic analyser, they stay low when using HAL_I2C_Master_Transmit or HAL_I2C_Mem_Write.
The pins work fine when using GPIO HAL functions.
I've tried using 4.6k pullups with no effect.
My code works using the STM32G474 nucleo board.
SPI peripheral works without error.
I'm using the bluepill breakout board for the STM32F103C8T6.
I've tried two of these boards with the same result.
The Master_Transmit returns HAL_BUSY.
The I2C peripheral reports error 0x20, Timeout error.
I have tried with high timeout values, including max timeout, with no effect.
I've attached the project file for reference.
Code:
/* USER CODE BEGIN 2 */
uint8_t dat = 0x22;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_Delay(250);
HAL_I2C_Master_Transmit(&hi2c1, 0xA2, &dat, 1, 10);
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
main.h for the LED:
#define LED_Pin GPIO_PIN_13
#define LED_GPIO_Port GPIOC
2021-02-25 08:07 PM
Turns out that I had something on the bus pulling the clock pin low.
Connect the microcontroller to the bus using pullups, no issues.
2021-02-25 11:02 PM
I suppose you know pull-up resistors are a requirement for the I2C bus.
But such stuck-events can happen occiasionally with certain slave devices.
The best option is then to re-configure the pins as GPIO (with pull-up), and clock SCL until the device releases SDA.
Most slaves have relatively simple state-driven bus logic.