2015-03-18 3:45 AM
Hi everyone,
I am working with a sensor which communicates using i2c. I am very new to stm 32 and could only figure out the basics of the code.I have successfully obtained data from other sensors using i2c. This particular sensor has the following protocol to obtain data.__GPIOB_CLK_ENABLE();
GPIO_i2c.Mode = GPIO_MODE_AF_OD;
GPIO_i2c.Pull = GPIO_PULLUP;
GPIO_i2c.Pin |= GPIO_PIN_6|GPIO_PIN_7;
//GPIO_Uart.Speed = GPIO_SPEED_LOW;
GPIO_i2c.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_i2c);
__I2C1_CLK_ENABLE();
I2cHandle.Instance = I2C1;
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2cHandle.Init.ClockSpeed = 400000;
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
I2cHandle.Init.OwnAddress1 = 0x30F;
and the functions as I am using are given below
HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)0xB4, (uint8_t*)read1, 1, 10000);
HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)0xB5, (uint8_t *)Buffer1, 3, 10000);
HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)0x00, (uint8_t*)read2, 3, 10000);
HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)0x01, (uint8_t *)Buffer2, 2, 10000);
where read1 has the address of the register and 0xB4 is address for read ,while 0xB5 is for write.
#restart #i2c #ack
2015-03-20 11:58 PM
Try using the so called Memory read varaint (HAL_I2C_Mem_Read)
2018-10-14 11:52 PM
yes,use function HAL_I2C_Mem_Read