STM32RC542RCT6 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.
