]> mj.ucw.cz Git - home-hw.git/blobdiff - Src/bmp085.c
Makefile: deps
[home-hw.git] / Src / bmp085.c
index 4df07c2ea9f9554ce96ce9da11f4dc8d942ea242..d150b90fb5cfdd25faee56be2935321464184848 100644 (file)
@@ -4,46 +4,42 @@
 #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(I2C2);
-  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)
     ;
-  LL_I2C_ClearFlag_ADDR(I2C2);
 
   uint d = 0;
   for (uint i=0; i<bytes; i++)
-    {
-      if (i == bytes-1)
-       LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
-      while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
-       ;
-      d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
-    }
+    d = (d << 8) | bmp_i2c_buf[i];
 
-  LL_I2C_GenerateStopCondition(I2C1);
   return d;
 }
 
-static uint bmp_start_measure(uint type, uint bytes)
+static void bmp_start_measure(uint type)
 {
   LL_I2C_GenerateStartCondition(I2C1);
   while (!LL_I2C_IsActiveFlag_SB(I2C1))
@@ -52,7 +48,7 @@ static uint bmp_start_measure(uint type, uint bytes)
   LL_I2C_TransmitData8(I2C1, 0xee);
   while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
     ;
-  LL_I2C_ClearFlag_ADDR(I2C2);
+  LL_I2C_ClearFlag_ADDR(I2C1);
 
   while (!LL_I2C_IsActiveFlag_TXE(I2C1))
     ;
@@ -65,15 +61,10 @@ static uint bmp_start_measure(uint type, uint bytes)
   while (!LL_I2C_IsActiveFlag_TXE(I2C1))
     ;
   LL_I2C_GenerateStopCondition(I2C1);
-
-  while (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
-    ;
-
-  return bmp_read(0xf6, bytes);
 }
 
 // Formulae from BMP085 specs
-static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], int *tt, int *pp)
+static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], s16 *tt, s16 *pp)
 {
   s16 AC1 = cc[0];
   s16 AC2 = cc[1];
@@ -83,7 +74,8 @@ static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], int *tt, int *pp)
   u16 AC6 = cc[5];
   s16 B1 = cc[6];
   s16 B2 = cc[7];
-  s16 MB = cc[8];
+  // FIXME: Why is this unused?
+  // s16 MB = cc[8];
   s16 MC = cc[9];
   s16 MD = cc[10];
   UP >>= (8-oss);
@@ -121,33 +113,52 @@ 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]);
+    }
 }
 
-#if 0
-void run_test(void)
+enum bmp_state {
+  BMP_IDLE,
+  BMP_TEMP,
+  BMP_PRESSURE,
+};
+
+static byte bmp_state = BMP_IDLE;
+static u16 raw_temp;
+static u32 raw_press;
+#define BMP_OSS 3
+s16 adjusted_temp;
+s16 adjusted_press;
+
+void bmp_step(void)
 {
-  for (;;)
+  switch (bmp_state)
     {
-      debug_puts("Constants:");
-      u16 cc[11];
-      for (uint i=0; i<11; i++)
-       {
-         cc[i] = bmp_read(0xaa + 2*i, 2);
-         debug_printf(" %04x", cc[i]);
-       }
-      debug_puts("\r\n");
-
-      uint raw_temp = bmp_measure(0x2e, 2);
-      debug_printf("Raw temperature: %04x\r\n", raw_temp);
-
-      uint oss = 3;    // Over-sampling setting
-      uint raw_press = bmp_measure(0xf4 | (oss<<6), 3);
-      debug_printf("Raw pressure: %06x\r\n", raw_press);
-
-      int temp, press;
-      bmp_recalc(raw_temp, raw_press, oss, cc, &temp, &press);
-      debug_printf("Temperature: %d ddegC\r\n", temp);
-      debug_printf("Pressure: %d Pa\r\n", press);
+    case BMP_IDLE:
+      debug_puts("BMP: Start measure\n");
+      bmp_start_measure(0x2e);
+      bmp_state++;
+      break;
+    case BMP_TEMP:
+      if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
+       return;
+      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 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;
     }
-#endif
+}