title: STM32F4 Hanging in I2C_CheckEvent()
body: <p>I've been finding that the I2C communication between my STM32F4 and IMU device randomly hangs. The STM32F4 is the master and the only other device on the bus is an IMU6050 6 axis accel/gyro. The program ends up hanging by getting stuck in a loop within the I2C_CheckEvent() function.</p>
  
  <p>Initialization is as follows:</p>
  
  <pre><code>RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;            
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;          
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;            
  GPIO_Init(DATAPORT, &amp;GPIO_InitStructure);                   
  GPIO_PinAFConfig(DATAPORT, GPIO_PinSource8, GPIO_AF_I2C1);
  GPIO_PinAFConfig(DATAPORT, GPIO_PinSource9, GPIO_AF_I2C1);
  
  /* Set the I2C parameters, pretty much default except ack */
  I2C_InitStructure.I2C_ClockSpeed = 100000;
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStructure.I2C_OwnAddress1 = 0;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_Init(I2C1, &amp;I2C_InitStructure);
  
  /* Enable the I2C peripheral */
  I2C_Cmd(I2C1, ENABLE);
  
  /* Config the IMU with defult settings */
  IMUConfig(DEFAULTACCELRANGE, DEFAULTGYRORANGE);
  </code></pre>
  
  <p>The initialization of the IMU is taken care of, I'm sure that all works as I get valid data for a little while. The application randomly crashes after a few seconds of running.</p>
  
  <p>The read function:</p>
  
  <pre><code>void IMUReadBytes(uint8_t startAddress, uint8_t numOfBytes, uint8_t *location){
      I2C_GenerateSTART(I2C1, ENABLE);
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
  
      I2C_Send7bitAddress(I2C1, IMU_ADDRESS &lt;&lt; 1, I2C_Direction_Transmitter);
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  
      I2C_SendData(I2C1, startAddress);
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  
      I2C_GenerateSTART(I2C1, ENABLE);
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
  
      I2C_Send7bitAddress(I2C1, IMU_ADDRESS &lt;&lt; 1, I2C_Direction_Receiver);
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
  
      int num = 0;
      for(num = 0; num &lt;= numOfBytes; num++){
          if(num == numOfBytes){
              I2C_AcknowledgeConfig(I2C1, DISABLE);
              I2C_GenerateSTOP(I2C1, ENABLE);
          }
  
          while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
          location[num] = I2C_ReceiveData(I2C1);
      }
      I2C_AcknowledgeConfig(I2C1, ENABLE);
  }
  </code></pre>
  
  <p>Each time, the application seems to crash via one of the while check events, it's different each time.</p>
  
  <p>Possibly unrelated, but when compiling, I get the warning: </p>
  
  <pre><code>source\application\source\imu.c:129:5: warning: passing argument 3 of 'IMUReadBytes' from incompatible pointer type [enabled by default]
  source\application\source\imu.c:81:6: note: expected 'uint8_t *' but argument is of type 'uint8_t (*)[14]'
  </code></pre>
  
  <p>I'm not sure if that has anything to do with it.</p>
  
  <p>As I said, the code works fine for a little while, sometime even a couple mins. I've got the gyro and accel values linked to some motors and during the time it works, everything runs perfectly. No idea why it gets stuck</p>