2022-05-10 08:10 AM
PB3 and PB10 are enabled as I2C2_SDA and I2C2_SCL to connect to LIS2DH12 SDA and SCL, the i2c2 code is generated in STM32CubeIDE v1.6.1.
PB3 and PB10 have pull-up resistors.
LIS2DH12 CS and SD0 are connected to Power Supply 3.3V. The address for it is right shifted 1 bit to 0x19.
The code is generated as the following.
main.c
int main(void)
{
...
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C2_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
...
}
/**
* @brief I2C2 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C2_Init(void)
{
/* USER CODE BEGIN I2C2_Init 0 */
/* USER CODE END I2C2_Init 0 */
/* USER CODE BEGIN I2C2_Init 1 */
/* USER CODE END I2C2_Init 1 */
hi2c2.Instance = I2C2;
hi2c2.Init.ClockSpeed = 100000;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C2_Init 2 */
/* USER CODE END I2C2_Init 2 */
}
stm32f4xx_hal_msp.c
/**
* @brief I2C MSP Initialization
* This function configures the hardware resources used in this example
* @param hi2c: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hi2c->Instance==I2C2)
{
/* USER CODE BEGIN I2C2_MspInit 0 */
/* USER CODE END I2C2_MspInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C2 GPIO Configuration
PB10 ------> I2C2_SCL
PB3 ------> I2C2_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_I2C2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C2_CLK_ENABLE();
/* USER CODE BEGIN I2C2_MspInit 1 */
/* USER CODE END I2C2_MspInit 1 */
}
}
However, when issuing the command to access LIS2DH12 WHO_AM_I register. It always return HAL_ERROR.
+extern I2C_HandleTypeDef hi2c2;
+ REG_FN(f_sensorid)
+{
+ // I2C Debug Start
+#define LIS2DH12_ADDR 0x19
+#define WHO_AM_I 0x0f
+ uint8_t buf = WHO_AM_I;
+ uint8_t ID;
+ HAL_StatusTypeDef ret;
+
+ // Tell LIS2DH12 that we want to read from the WHOAMI register
+ ret = HAL_I2C_Master_Transmit(&hi2c2, LIS2DH12_ADDR, &buf, 1, 1000);
+ if ( ret != HAL_OK ) {
+ uart_printf("Error Tx\r\n");
+ } else {
+
+ // Read 2 bytes from the temperature register
+ ret = HAL_I2C_Master_Receive(&hi2c2, LIS2DH12_ADDR, &ID, 1, 1000);
+ if (ret != HAL_OK ) {
+ uart_printf("Error Rx\r\n");
+ } else {
+
+ //Combine the bytes
+ //HAL_I2C_Mem_Read(&hi2c2, LIS2DH12_ADDR, WHO_AM_I, 1U, &ID, 1U, 1000);
+ uart_printf("WHO AM I: %02x\r\n", ID);
+ }
+ }
+#endif
+ // I2C Debug End