]> mj.ucw.cz Git - home-hw.git/blobdiff - Src/stm32f1xx_it.c
Whoa! Interrupt-driven I2C is working!
[home-hw.git] / Src / stm32f1xx_it.c
index 68d7309bf1ca77516b6d604540fffb0fa8e24cec..c571e46e4437c3f3268db2f52146528da32cbebe 100644 (file)
@@ -246,6 +246,52 @@ void TIM4_IRQHandler(void)
 void I2C1_EV_IRQHandler(void)
 {
   /* USER CODE BEGIN I2C1_EV_IRQn 0 */
+  u32 sr1 = I2C1->SR1;
+  if (sr1 & I2C_SR1_SB)
+    LL_I2C_TransmitData8(I2C1, bmp_i2c_addr);
+  else if (bmp_i2c_addr & 1)
+    {
+      // Receive
+      if (sr1 & I2C_SR1_ADDR)
+       {
+         LL_I2C_DisableBitPOS(I2C1);
+         if (bmp_i2c_len == 1)
+           LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
+         LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK);
+         LL_I2C_ClearFlag_ADDR(I2C1);
+       }
+      else if (sr1 & I2C_SR1_RXNE)
+       {
+         if (bmp_i2c_len > 0)
+           {
+             if (bmp_i2c_len == 1)
+               {
+                 LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
+                 LL_I2C_GenerateStopCondition(I2C1);
+               }
+             *bmp_i2c_ptr++ = LL_I2C_ReceiveData8(I2C1);
+             bmp_i2c_len--;
+           }
+         else
+           LL_I2C_DisableIT_RX(I2C1);
+       }
+    }
+  else
+    {
+      // Transmit
+      if (sr1 & I2C_SR1_ADDR)
+       LL_I2C_ClearFlag_ADDR(I2C1);
+      else if (sr1 & I2C_SR1_TXE)
+       {
+         if (bmp_i2c_len)
+           {
+             LL_I2C_TransmitData8(I2C1, *bmp_i2c_ptr++);
+             bmp_i2c_len--;
+           }
+         else
+           LL_I2C_DisableIT_TX(I2C1);
+       }
+    }
 
   /* USER CODE END I2C1_EV_IRQn 0 */