2018-12-18 02:59 AM
I am newbie to use Nucleo-L152RE board. I have a HTU21D-Temperature & Humidity sensor which used I2C interface to read the temperature data.
So, with using the STM32CubeMX I have generated code for STM32L152RETx chip with UART2 (debug purpose) and I2C1 (PB6/PB7).
Below is my snippet code.
#define HTU21D_ADDR 0x80
#define Temperature_REG 0xE3
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
int main(void)
{
uint8_t t_data[2];
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
MX_USART2_UART_Init();
MX_I2C1_Init();
HAL_I2C_Master_Transmit(&hi2c1, HTU21D_ADDR, &Temperature_REG, 1, 1000);
HAL_I2C_Master_Receive(&hi2c1, HTU21D_ADDR, t_data, 2, 1000);
printf("Temperature read data %x:%x\r\n",t_data[0],t_data[1]);
}
void MX_I2C1_Init()
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = I2C_SPEEDCLOCK;
hi2c1.Init.DutyCycle = I2C_CCR_DUTY;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
hi2c1.Init.OwnAddress1 = 0x00;
hi2c1.Init.OwnAddress2 = 0x00;
}
Every time getting the zero value from the sensor. So, can you please tell me what is the problem here?.
Connection of HTU21D sensor and Nucleo-L152RE board are:
SCK --> PB6
SDA --> PB7
VCC --> 3V3
GND --> GND
Attached snap shot of interface between sensor and Nucleo.
2018-12-18 03:16 AM
You should check what are returning the HAL_I2C_Master_Transmit and HAL_I2C_Master_Receive functions. Maybe they are failing and you can find more information from their return code.
2018-12-18 03:27 AM
Yes, I have checked the return status of transmit and receive. It has given me status =2 (HAL_BUSY). So, what I have to do here?.
2018-12-18 07:17 PM
Has anyone give me the solution for the i2c busy flag error?. Is there any pull-up/down setting required?. What is wrong here?. @Community members, can you please help me to resolved this issue.
2018-12-19 12:26 AM
I2C requires open-drain and pull-ups on both data and clock pins, if you don't have the pull-ups on PCB level you must activate them on MCU level.
As for the busy flag, can you debug step-by-step into the HAL calls and see where exactly it returns the busy code?
2018-12-19 01:03 AM
Thanks for your quick support.
As far pull-ups, it is present on SDA and SCK line on PCB. And after debug, I have found that in HAL_I2C_Master_Transmit() "I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart)" this is returning the HAL_TIMEOUT error so HAL_I2C_Master_Transmit() gives the status HAL_BUSY. Now, what is the next step to do?. Can you please guide me.
2018-12-19 01:13 AM
Did you generate the initialization code using STM32CubeMX? Can you share the IOC file? I don't see any calls to HAL_I2C_Init.
2018-12-19 02:06 AM
2018-12-19 03:00 AM
> I forgot in snippet code.
Aha, so that was not the whole code, OK.
I don't see any issues with your IOC, I generated init. code using it, getting somewhat different code than your snippet, but maybe it's because my Cube and firmware packages are updated to the latest versions.
Anyhow, returning to your step-by-step debugging, there are several calls to I2C_WaitOnFlagUntilTimeout inside the HAL_I2C_Master_Transmit function, so which one exactly is timing out for you? If it's the first one, then it's strange, I2C peripheral's busy flag shouldn't be set just after enabling it, before any usage.
2018-12-19 03:09 AM
Yes, it is the first one. Its a full code just only HAL_init() function to forgot in snippet. Now, what should I do?