2022-04-14 06:40 AM
Hi all, I have been trying to read any data from HTS221 on extension board, and HAL I2C functions returns error.
I try to read WHO_AM_I register but HAL_I2C_Memread function always returns timeout so error. Any suggestion would be appreciated, thanks.
The simplest code to read WHO_AM_I register below
#include "main.h"
#define DEV_ADDR 0xBF
#define WHO_AM_I 0x0F
I2C_HandleTypeDef hi2c1;
uint8_t Data = 0;
int32_t status = 0;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
status = HAL_I2C_IsDeviceReady(&hi2c1,DEV_ADDR,5,100);
status = HAL_I2C_Mem_Read( &hi2c1, DEV_ADDR, WHO_AM_I, 1, &Data, 1, 100);
HAL_GPIO_TogglePin( LED_EXT_SHIELD_GPIO_Port, LED_EXT_SHIELD_Pin );
while (1) { }
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00707CBB;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_10BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(LED_EXT_SHIELD_GPIO_Port, LED_EXT_SHIELD_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = USER_BUTTON_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(USER_BUTTON_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LED_EXT_SHIELD_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_EXT_SHIELD_GPIO_Port, &GPIO_InitStruct);
}
Solved! Go to Solution.
2022-04-25 02:58 AM
Hi @Eleon BORLINI i found the problem, i2c bus was broken on my nucleo i tried different board and it runs successfully.
Thanks for interest.
2022-04-22 06:46 AM
Hi @oziesin ,
are you facing the issue only with the humidity sensor, or also on the other sensors?
And is your project based on this firmware basis I-CUBE-LRWAN - LoRaWAN software expansion for STM32Cube (UM2073) - STMicroelectronics?
-Eleon
2022-04-25 02:58 AM
Hi @Eleon BORLINI i found the problem, i2c bus was broken on my nucleo i tried different board and it runs successfully.
Thanks for interest.
2022-04-26 01:12 AM
Hi @oziesin ,
glad you found and shared the solution to the issue!
Best,
-Eleon