0
\$\begingroup\$

Hi there :) I'm trying to use the Wake-on-Motion functionality of this particular MEMS (MPU9250) with my STM32L152C-Discovery board: if the MPU9250 sense a motion, the INT pin will be high. To achieve this I'm setting the registers as the datasheet suggests with the STM32 micro, handling also the interrupt, but it doesn't work.

Here is my main code(in main.c):

uint8_t interruptFlag;
int main(void)
{

  HAL_Init();


  SystemClock_Config();


  MX_GPIO_Init();
  MX_I2C1_Init();

  uint8_t MAddr=0x68;
  uint8_t buffer;

    if(HAL_I2C_IsDeviceReady(&hi2c1, MAddr<<1, 2, 10) == HAL_OK)
    {
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
          HAL_Delay(1000);
          HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
    }
    //register values
    uint8_t maskPWR_MGMT_1[2] = {0x6b, 0b00000001};//6b
    uint8_t maskPWR_MGMT_2[2] = {0x6c, 0b00000111};//6c
    uint8_t maskACCEL_CONFIG_2[2] ={0x1d, 0b00000101};//1d
    uint8_t maskINT_ENABLE[2] = {0x38, 0x40};//38
    uint8_t maskMOT_DETECT_CTRL[2] = {0x69, 0b11000000};//69
    uint8_t maskWOM_THR[2]={0x1f, 0x7f};//1f
    uint8_t maskLP_ACCEL_ODR[2] = {0x1e, 0b00000100};//1e
    uint8_t maskPWR_MGMT_1_new[2] = {0x6b, 0b00100000};//6b
    uint8_t maskINT_PIN_CFG[2] = {0x37, 0b00110000};//3a

    //writing the registers
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, maskPWR_MGMT_1, 2, 30);
    HAL_Delay(10);
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, maskPWR_MGMT_2, 2, 30);
    HAL_Delay(10);
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, maskACCEL_CONFIG_2, 2, 30);
    HAL_Delay(10);
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, maskINT_ENABLE, 2, 30);
    HAL_Delay(10);
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, maskINT_PIN_CFG, 2, 30);
    HAL_Delay(10);
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, maskMOT_DETECT_CTRL, 2, 30);
    HAL_Delay(10);
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, maskWOM_THR, 2, 30);
    HAL_Delay(10);
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, maskLP_ACCEL_ODR, 2, 30);
    HAL_Delay(10);
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, maskPWR_MGMT_1_new, 2, 30);
    HAL_Delay(10);

    //reading the register
    HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, &buffer, 1, 30);
    HAL_I2C_Master_Receive(&hi2c1, MAddr<<1, &buffer, 1, 30);

  while (1)
  {

      if(interruptFlag == 1)//if interrupt happens
      {
          //read a register
          buffer = 0x3a;
          HAL_I2C_Master_Transmit(&hi2c1, MAddr<<1, &buffer, 1, 30);
          HAL_I2C_Master_Receive(&hi2c1, MAddr<<1, &buffer, 1, 30);
          //set the led off and the flag to 0
          HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);//led off
          interruptFlag=0;//flag 0
      }
  }

}

also the Interrupt code(in the stm32l1xx_it.c)

extern uint8_t interruptFlag;//use the flag of the main.c
void EXTI9_5_IRQHandler(void)
{
    if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5))//if the INT pin is high
    {
        interruptFlag = 1;//flat to 1
        HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);//led on
    }
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
}

As the register map datasheet says(in particular the INT_PIN_CFG register), the INT pin will stay high until the microcontroller reads a register of the MPU9250. I also tryed the HAL_I2C_Mem_Read function in the while instruction, but i get the same problem.

I'm also monitoring the INT pin during the reset, and I noticed that at the reset, the INT pin goes low for few moments and then goes up, then the LED lights up, and the INT pin remains high.

I used the debug functionalities of Atollic TrueSTUDIO, and i putted some breakpoints in the interruptFlag = 1 line and in the line right after the if condition in the main. The code stops at the interruptFlag=1 line without problems, but the code doesn't stop at the breakpoint inside the if condition in the main. It seems that it does not enter the if condition.

What am I doing wrong?

\$\endgroup\$
  • 2
    \$\begingroup\$ Try making interruptFlag into a volatile, ie: volatile uint8_t interruptFlag; \$\endgroup\$ – Ron Beyer May 2 at 18:22
  • \$\begingroup\$ Thanks a lot, it works now! \$\endgroup\$ – RawCode May 3 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.