]> mj.ucw.cz Git - home-hw.git/commitdiff
Whoa! Interrupt-driven I2C is working!
authorMartin Mares <mj@ucw.cz>
Sat, 30 Jun 2018 08:54:16 +0000 (10:54 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 30 Jun 2018 08:54:16 +0000 (10:54 +0200)
Inc/app.h
Src/bmp085.c
Src/stm32f1xx_it.c

index 9eb2b0dfd45f0aa9ffd56aec1421fd8d737e9102..acae0ae4800437aa238b8edbd9875574581a94f3 100644 (file)
--- a/Inc/app.h
+++ b/Inc/app.h
@@ -19,5 +19,9 @@ void tx_packet_send(void);
 extern s16 adjusted_temp;
 extern s16 adjusted_press;
 
+extern volatile byte *bmp_i2c_ptr;
+extern volatile byte bmp_i2c_len;
+extern volatile byte bmp_i2c_addr;
+
 void bmp_init(void);
 void bmp_step(void);
index 76c64811c940059b964fc2fe257e735eda795a5c..d150b90fb5cfdd25faee56be2935321464184848 100644 (file)
@@ -4,82 +4,37 @@
 #include "util.h"
 #include "app.h"
 
+static byte bmp_i2c_buf[4];
+volatile byte *bmp_i2c_ptr;
+volatile byte bmp_i2c_len;
+volatile byte bmp_i2c_addr;
+
 static uint bmp_read(uint reg, uint bytes)
 {
+  bmp_i2c_buf[0] = reg;
+  bmp_i2c_ptr = bmp_i2c_buf;
+  bmp_i2c_len = 1;
+  bmp_i2c_addr = 0xee;
+
   LL_I2C_GenerateStartCondition(I2C1);
-  while (!LL_I2C_IsActiveFlag_SB(I2C1))
-    ;
-  LL_I2C_TransmitData8(I2C1, 0xee);
-  while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
-    ;
-  LL_I2C_ClearFlag_ADDR(I2C1);
-  while (!LL_I2C_IsActiveFlag_TXE(I2C1))
-    ;
-  LL_I2C_TransmitData8(I2C1, reg);
-  while (!LL_I2C_IsActiveFlag_TXE(I2C1))
+  LL_I2C_EnableIT_TX(I2C1);
+
+  while (bmp_i2c_len)
     ;
-  LL_I2C_GenerateStopCondition(I2C1);
+
+  bmp_i2c_ptr = bmp_i2c_buf;
+  bmp_i2c_len = bytes;
+  bmp_i2c_addr = 0xef;
 
   LL_I2C_GenerateStartCondition(I2C1);
-  while (!LL_I2C_IsActiveFlag_SB(I2C1))
-    ;
-  LL_I2C_DisableBitPOS(I2C1);
-  LL_I2C_TransmitData8(I2C1, 0xef);
-  while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
+  LL_I2C_EnableIT_RX(I2C1);
+
+  while (bmp_i2c_len)
     ;
 
-  // This is quite tricky (see manual)
   uint d = 0;
-  if (bytes == 1)
-    {
-      LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
-      LL_I2C_ClearFlag_ADDR(I2C1);
-      LL_I2C_GenerateStopCondition(I2C1);
-      while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
-       ;
-      d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
-    }
-  else if (bytes == 2)
-    {
-      LL_I2C_EnableBitPOS(I2C1);
-      LL_I2C_ClearFlag_ADDR(I2C1);
-      LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
-      while (!LL_I2C_IsActiveFlag_BTF(I2C1))
-       ;
-      LL_I2C_GenerateStopCondition(I2C1);
-      for (uint i=0; i<2; i++)
-       d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
-    }
-  else
-    {
-      LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK);
-      LL_I2C_ClearFlag_ADDR(I2C2);
-
-      uint d = 0;
-      for (uint i=0; i<bytes; i++)
-       {
-         if (i+3 < bytes || i+1 == bytes)
-           {
-#if 0
-             while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
-               ;
-#endif
-           }
-         else if (i+3 == bytes)
-           {
-             while (!LL_I2C_IsActiveFlag_BTF(I2C1))
-               ;
-             LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
-           }
-         else if (i+2 == bytes)
-           {
-             while (!LL_I2C_IsActiveFlag_BTF(I2C1))
-               ;
-             LL_I2C_GenerateStopCondition(I2C1);
-           }
-         d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
-       }
-    }
+  for (uint i=0; i<bytes; i++)
+    d = (d << 8) | bmp_i2c_buf[i];
 
   return d;
 }
@@ -158,7 +113,10 @@ static u16 bmp_constants[11];
 void bmp_init(void)
 {
   for (uint i=0; i<11; i++)
-    bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
+    {
+      bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
+      debug_printf("BMP: const[%d] = %u\n", i, bmp_constants[i]);
+    }
 }
 
 enum bmp_state {
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 */