본문 바로가기

[ST_MICRO]/STM32F3

[STM32F373 EVM] I2C 테스트 - 9축 IMU 센서 테스트

[STM32F373 EVM] I2C 테스트 - 9축 IMU 센서 테스트

 

 

 

[STM32F373 EVM]에는 I2C가 PB8(SCL), PB9(SDA)에 할당되어 있다. I2C테스트를 위해 9축 IMU센서(BNO055 EVM)의 롤,피치 값을 TFT LCD에 표시하는 예제를 작성해 보았다.

 

PB8 -> SPI2_SCK/I2S2_CK,I2C1_SCL, USART3_TX,CAN_RX, CEC, TIM16_CH1,TIM4_CH3, TIM19_CH3,COMP1_OUT, TSC_SYNC

PB9 -> SPI2_NSS/I2S2_WS,I2C1_SDA, USART3_RX,CAN_TX, IR_OUT,TIM17_CH1, TIM4_CH4,TIM19_CH4, COMP2_OUT

 
 
STM32F373 EVM 회로도 핀맵
 
 
 
STM32F373의 I2C 블록도
 

 

 

 

 

STM32F373 I2C 초기화 함수

#define I2Cx                             I2C1

#define I2Cx_CLK_ENABLE()                __I2C1_CLK_ENABLE()

#define I2Cx_SDA_GPIO_CLK_ENABLE()       __GPIOB_CLK_ENABLE()

#define I2Cx_SCL_GPIO_CLK_ENABLE()       __GPIOB_CLK_ENABLE() 

 

#define I2Cx_FORCE_RESET()               __I2C1_FORCE_RESET()

#define I2Cx_RELEASE_RESET()             __I2C1_RELEASE_RESET()

 

//Definition for I2Cx Pins

#define I2Cx_SCL_PIN                    GPIO_PIN_8

#define I2Cx_SCL_GPIO_PORT              GPIOB

#define I2Cx_SCL_AF                     GPIO_AF4_I2C1

#define I2Cx_SDA_PIN                    GPIO_PIN_9

#define I2Cx_SDA_GPIO_PORT              GPIOB

#define I2Cx_SDA_AF                     GPIO_AF4_I2C1

 

 

void BNO055_Init(void)

{

  GPIO_InitTypeDef  GPIO_InitStruct;

  

  /*##-1- Enable peripherals and GPIO Clocks #################################*/

  /* Enable GPIO TX/RX clock */

  I2Cx_SCL_GPIO_CLK_ENABLE();

  I2Cx_SDA_GPIO_CLK_ENABLE();

  /* Enable I2C1 clock */

  I2Cx_CLK_ENABLE(); 

  

  /*##-2- Configure peripheral GPIO ##########################################*/  

  /* I2C TX GPIO pin configuration  */

  GPIO_InitStruct.Pin       = I2Cx_SCL_PIN;

  GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;

  GPIO_InitStruct.Pull      = GPIO_PULLUP;

  GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;

  GPIO_InitStruct.Alternate = I2Cx_SCL_AF;

  

  HAL_GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);

    

  /* I2C RX GPIO pin configuration  */

  GPIO_InitStruct.Pin = I2Cx_SDA_PIN;

  GPIO_InitStruct.Alternate = I2Cx_SDA_AF;

    

  HAL_GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);

  

  

  I2cHandle.Instance             = I2Cx;

  

  I2cHandle.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;

  I2cHandle.Init.Timing      = 400000;

  I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;

  //I2cHandle.Init.DutyCycle       = I2C_DUTYCYCLE_2;

  I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_ENABLED;

  I2cHandle.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLED;

  I2cHandle.Init.OwnAddress1     = 0xFE;

  I2cHandle.Init.OwnAddress2     = 0xFE;    

  

  

  HAL_I2C_Init(&I2cHandle);  

}

 

 

 

STM32F373 I2C 송수신 함수

unsigned char read8(unsigned char addr, unsigned char data)

{

    unsigned char tx_buf[2];

    unsigned char rx_buf[4];

    tx_buf[0] = data;

   

    while(HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)addr, (uint8_t*)tx_buf, 1, 10000)!= HAL_OK)

    {

 

    }

    

    while(HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)addr, (uint8_t *)rx_buf, 1, 10000) != HAL_OK)

    {

    }

  

    result = rx_buf[0];

}

 

void write8(unsigned char addr, unsigned char reg, unsigned char data)

{

    unsigned char tx_buf[2];

 

    tx_buf[0] = reg;

    tx_buf[1] = data;

 

    while(HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)addr, (uint8_t*)tx_buf, 2, 10000)!= HAL_OK)

    {

    }

}

 

 

 

STM32F373 I2C 테스트 동영상

I2C인터페이스로 BNO055 센서의 롤,피치 값을 TFT LCD에 표시하는 테스트 

 

반응형