]> mj.ucw.cz Git - home-hw.git/blobdiff - Src/bmp085.c
Makefile: deps
[home-hw.git] / Src / bmp085.c
index 76f548f16230d46bc5e1a05990a86e8399fdae5e..d150b90fb5cfdd25faee56be2935321464184848 100644 (file)
@@ -4,74 +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_AcknowledgeNextData(I2C1, LL_I2C_ACK);
-  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_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_ClearFlag_ADDR(I2C2);
-
-      uint d = 0;
-      for (uint i=0; i<bytes; i++)
-       {
-         if (i < bytes-3 || i == bytes-1)
-           {
-             while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
-               ;
-           }
-         else if (i == bytes-3)
-           {
-             while (!LL_I2C_IsActiveFlag_BTF(I2C1))
-               ;
-             LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
-           }
-         else if (i == bytes-2)
-           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;
 }
@@ -150,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 {
@@ -178,17 +144,20 @@ void bmp_step(void)
     case BMP_TEMP:
       if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
        return;
-      debug_puts("BMP: Temperature done\n");
-      raw_press = bmp_read(0xf6, 2);
+      debug_puts("BMP: Temperature measured\n");
+      raw_temp = bmp_read(0xf6, 2);
+      debug_printf("BMP: Temperature read: %u\n", raw_temp);
       bmp_start_measure(0xf4 | (BMP_OSS<<6));
       bmp_state++;
       break;
     case BMP_PRESSURE:
       if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
        return;
-      debug_puts("BMP: Pressure done\n");
+      debug_puts("BMP: Pressure measured\n");
       raw_press = bmp_read(0xf6, 3);
+      debug_printf("BMP: Pressure read: %u\n", raw_press);
       bmp_recalc(raw_temp, raw_press, BMP_OSS, bmp_constants, &adjusted_temp, &adjusted_press);
+      debug_printf("BMP: Adjusted temp %u, press %u\n", adjusted_temp, adjusted_press);
       bmp_state = BMP_IDLE;
       break;
     }