Skip to main content
Visitor
Sep 22, 2026 6:06 PM UTC
Solved

STM32C542RCT6 and TMP102 SparkFun

Hello Dear Community:

I am facing the following issue. I generated the project from Cube32MX for STM32C542RCT6.
I modified the following lines seems that a bug is present in the AF aka alternate functions.


 

* @file : mx_i2c1.c

* @brief : I2C1 Peripheral initialization



HAL_I2C_EnableAnalogFilter(&hI2C1);

 

/* ### I2C1 GPIO Configuration ########################### */

/* GPIO Clocks activation */

HAL_RCC_GPIOB_EnableClock();

 

hal_gpio_config_t gpio_config;

 

/**

[GPIO Pin] ------> [Signal Name]

 

PB8 ------> I2C1_SCL

PB9 ------> I2C1_SDA

**/

gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;

gpio_config.output_type = HAL_GPIO_OUTPUT_OPENDRAIN;

gpio_config.pull = HAL_GPIO_PULL_NO;

gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;

gpio_config.alternate = HAL_GPIO_AF4_I2C1; => HERE IS THE MODIFICATION

HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_8 | HAL_GPIO_PIN_9, &gpio_config);

In the main:

/*

* You can start your application code here

*/

for (uint8_t addr = 0x08; addr < 0x78; addr++) {

if (HAL_I2C_MASTER_Transmit(hi2c1, addr << 1, NULL, 0, 10) == HAL_OK) {

printf("Device found at 0x%02X\r\n", addr);

}

}

while (1) {

// Tell TMP102 that we want to read from the temperature register

buf[0] = REG_TEMP;

ret = HAL_I2C_MASTER_Transmit(hi2c1, 0x48, buf, 1, HAL_MAX_DELAY);

if ( ret != HAL_OK ) {

strcpy((char*)buf, "Error Tx\r\n");

} else {

 

// Read 2 bytes from the temperature register

ret = HAL_I2C_MASTER_Receive(hi2c1, 0x48, buf, 2, HAL_MAX_DELAY);

if ( ret != HAL_OK ) {

strcpy((char*)buf, "Error Rx\r\n");

} else {

 

//Combine the bytes

val = ((int16_t)buf[0] << 4) | (buf[1] >> 4);

 

// Convert to 2's complement, since temperature can be negative

if ( val > 0x7FF ) {

val |= 0xF000;

}

 

// Convert to float temperature value (Celsius)

temp_c = val * 0.0625;

 

// Convert temperature to decimal format

temp_c *= 100;

sprintf((char*)buf,

"%u.%u C\r\n",

((unsigned int)temp_c / 100),

((unsigned int)temp_c % 100));

}

}

 

// Send out buffer (temperature or error message)

HAL_UART_Transmit(huart2, buf, strlen((char*)buf), HAL_MAX_DELAY);

 

// Wait

HAL_Delay(500);

}

}



1) I cannot see Device found at
2) It returns HAL_ERROR

Debugging I can see:

* @file stm32c5xx_hal_i2c.c

* @brief I2C HAL module driver.


hal_status = I2C_WaitOnFlagUntilTimeout(hi2c, LL_I2C_ISR_BUSY, 1U, I2C_DEFAULT_TIMEOUT_MS, tick_start);
if (hal_status == HAL_OK) => this is OK

And here is HAL_ERROR:
while (hi2c->xfer_count > 0U) { hal_status = I2C_WaitOnTXISFlagUntilTimeout(hi2c, timeout_ms, tick_start); if (hal_status == HAL_OK)

Thanks for your support.
Manuel.
Manuel5Best answer
Visitor
Sep 23, 2026 8:27 AM UTC

After rechecking the Data Sheet and User Manual I’ve found the issue:

I created a brand new MCU in STM32CubeMX2:

/* [GPIO Pin] ------> [Signal Name]

PB6 ------> I2C1_SCL

PB7 ------> I2C1_SDA
*/

gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;

gpio_config.output_type = HAL_GPIO_OUTPUT_OPENDRAIN;

gpio_config.pull = HAL_GPIO_PULL_NO;

gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;

gpio_config.alternate = HAL_GPIO_AF_4;

HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_6 | HAL_GPIO_PIN_7, &gpio_config);


The generation must match:
Table 15. ARDUINO® Digital[15:8] connector (CN5) pinout
Depicted in:
UM3615 
STM32C5 Nucleo-64 board (MB2213)

 


The device is found and the temperature is being sent back.
Thanks for your support.
Manu.

 

10 replies

TDK
Sep 22, 2026 7:11 PM UTC

gpio_config.alternate = HAL_GPIO_AF4_I2C1; => HERE IS THE MODIFICATION

What was this line pre-modification?

Can you include your IOC file?

Did modifying that line fix the problem? It’s unclear from the post if the issue is still present.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Manuel5Author
Visitor
Sep 22, 2026 8:03 PM UTC

Hello.

Before modification:

* @file : mx_i2c1.c

* @brief : I2C1 Peripheral initialization

/**

[GPIO Pin] ------> [Signal Name]

 

PB8 ------> I2C1_SCL

PB9 ------> I2C1_SDA

**/

gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;

gpio_config.output_type = HAL_GPIO_OUTPUT_OPENDRAIN;

gpio_config.pull = HAL_GPIO_PULL_NO;

gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;

gpio_config.alternate = HAL_GPIO_AF_4;

HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_8 | HAL_GPIO_PIN_9, &gpio_config);

 

Regards.
Manu.

Manuel5Author
Visitor
Sep 22, 2026 8:06 PM UTC

That modification did not work.
Still fails with HAL_ERROR so as mentioned in:

* @file stm32c5xx_hal_i2c.c

* @brief I2C HAL module driver.


hal_status = I2C_WaitOnFlagUntilTimeout(hi2c, LL_I2C_ISR_BUSY, 1U, I2C_DEFAULT_TIMEOUT_MS, tick_start);
if (hal_status == HAL_OK) => this is OK

And here is HAL_ERROR:
while (hi2c->xfer_count > 0U) { hal_status = I2C_WaitOnTXISFlagUntilTimeout(hi2c, timeout_ms, tick_start); if (hal_status == HAL_OK)

TDK
Sep 22, 2026 8:40 PM UTC

gpio_config.alternate = HAL_GPIO_AF_4;

There is no bug here. Why do you think there is? Look at the source:

#define HAL_GPIO_AF4_I2C1                HAL_GPIO_AF_4 /*!< I2C1 Alternate Function mapping        */

 

I cannot see Device found at

So probably the sensor is not powered or hooked up correctly. Use HAL_I2C_IsDeviceReady and ensure it returns HAL_OK before using any other I2C function.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Manuel5Author
Visitor
Sep 23, 2026 6:53 AM UTC

 

  • According to the documentation the TMP102 by  SparkFun does not need any external pull up.
  • The multimeter shows 3.33v stable for 1.4v-3.6v, SDA, SCL and ALT on the sensor
  • The attached image show the debugger after evaluating: ret = HAL_I2C_MASTER_PollForSlaveReady(hi2c1, 0x48, 10000);
  • Using TMP102_ADDR = 0x48 << 1; // Use 8-bit address gives the same HAL_ERROR
Manuel5Author
Visitor
Sep 23, 2026 7:00 AM UTC

Wiring images also shared through photos.
 

Manuel5AuthorBest answer
Visitor
Sep 23, 2026 8:27 AM UTC

After rechecking the Data Sheet and User Manual I’ve found the issue:

I created a brand new MCU in STM32CubeMX2:

/* [GPIO Pin] ------> [Signal Name]

PB6 ------> I2C1_SCL

PB7 ------> I2C1_SDA
*/

gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;

gpio_config.output_type = HAL_GPIO_OUTPUT_OPENDRAIN;

gpio_config.pull = HAL_GPIO_PULL_NO;

gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;

gpio_config.alternate = HAL_GPIO_AF_4;

HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_6 | HAL_GPIO_PIN_7, &gpio_config);


The generation must match:
Table 15. ARDUINO® Digital[15:8] connector (CN5) pinout
Depicted in:
UM3615 
STM32C5 Nucleo-64 board (MB2213)

 


The device is found and the temperature is being sent back.
Thanks for your support.
Manu.

 

Andrew Neil
Super User
Sep 23, 2026 9:31 AM UTC

If that’s resolved your issue, please mark it as the solution 

 

For future reference when posting code, please see:

 

Also:

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
TDK
Sep 23, 2026 12:45 PM UTC

So you were just using the wrong pins? Yeah, I don’t see how anyone could have caught that with the lack of information given in the OP.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Andrew Neil
Super User
Sep 23, 2026 1:11 PM UTC

 I don’t see how anyone could have caught that with the lack of information given in the OP.

Indeed.

 

​@Manuel5 - this is why it’s important that you provide a schematic!

https://community.st.com/community%2Dguidelines%2D160/how%2Dto%2Dwrite%2Dyour%2Dquestion%2Dto%2Dmaximize%2Dyour%2Dchances%2Dto%2Dfind%2Da%2Dsolution%2D118622

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.