cancel
Showing results for 
Search instead for 
Did you mean: 

Hi I am going to use ST32L432KC MCU to get data from a sensor (BMI160 shuttle board) through I2C communication. 1- Does wire.h library in Arduino work for the ST32 MCUs? And how can I use it/define the pins for it?

SKaze
Associate II

2- Does anyone have experience getting data from BMI160 with ST32 MCU?

Thanks

Saber

2 REPLIES 2

No, there might be ports, but typically here people use the HAL libraries

STM32Cube_FW_L4_V1.15.1\Projects\NUCLEO-L432KC\Examples\I2C

STM32Cube_FW_L4_V1.15.1\Projects\STM32L476G-EVAL\Examples\I2C\I2C_EEPROM

Should be able to read registers inside BMI160 using pretty generic I2C code.

I2C_HandleTypeDef I2CHandle = {0};
 
void I2Cx_Init(void) // PB6 I2C1_SCL, PB7 I2C1_SDA
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  __I2C1_CLK_ENABLE();
  __GPIOB_CLK_ENABLE();
 
  /* I2C SCL/SDA GPIO pin configuration  */
  GPIO_InitStruct.Pin       = GPIO_PIN_6 | GPIO_PIN_7;
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
 
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  /* Force the I2C peripheral clock reset */
  __HAL_RCC_I2C1_FORCE_RESET();
 
  /* Release the I2C peripheral clock reset */
  __HAL_RCC_I2C1_RELEASE_RESET();
 
  if (HAL_I2C_GetState(&I2CHandle) == HAL_I2C_STATE_RESET)
  {
    I2CHandle.Instance              = I2C1;
    I2CHandle.Init.Timing           = I2C_TIMING;
    I2CHandle.Init.OwnAddress1      = 0;
    I2CHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
    I2CHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLE;
    I2CHandle.Init.OwnAddress2      = 0;
    I2CHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLE;
    I2CHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLE;
 
    /* Init the I2C */
    HAL_I2C_Init(&I2CHandle);
 
    HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 1);
    HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
    HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 2);
    HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
  }
  else
    puts("HAL_I2C_Init not called");
}
 
...
  HAL_StatusTypeDef status = HAL_OK;
  uint8_t addr = 0;
  uint8_t value;
 
  status = HAL_I2C_Mem_Read(&I2CHandle, SLAVE_I2C_ADDR, addr, I2C_MEMADD_SIZE_8BIT, &value, 1, 100);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
prain
Senior III

The wire.h and some other Arduino libraries are already ported for stm32. ​check stm32duino project for more details.