]> mj.ucw.cz Git - home-hw.git/commitdiff
BMP partial
authorMartin Mares <mj@ucw.cz>
Tue, 26 Jun 2018 22:56:05 +0000 (00:56 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 26 Jun 2018 22:56:05 +0000 (00:56 +0200)
Inc/app.h
Src/bmp085.c
Src/main.c

index b125b582124b926a2e562522c88aa315f921a084..9eb2b0dfd45f0aa9ffd56aec1421fd8d737e9102 100644 (file)
--- a/Inc/app.h
+++ b/Inc/app.h
@@ -16,4 +16,8 @@ void tx_packet_send(void);
 
 // bmp085.c
 
+extern s16 adjusted_temp;
+extern s16 adjusted_press;
+
 void bmp_init(void);
+void bmp_step(void);
index 4df07c2ea9f9554ce96ce9da11f4dc8d942ea242..76f548f16230d46bc5e1a05990a86e8399fdae5e 100644 (file)
@@ -12,7 +12,7 @@ static uint bmp_read(uint reg, 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))
     ;
   LL_I2C_TransmitData8(I2C1, reg);
@@ -27,23 +27,56 @@ static uint bmp_read(uint reg, uint bytes)
   LL_I2C_TransmitData8(I2C1, 0xef);
   while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
     ;
-  LL_I2C_ClearFlag_ADDR(I2C2);
 
+  // This is quite tricky (see manual)
   uint d = 0;
-  for (uint i=0; i<bytes; i++)
+  if (bytes == 1)
     {
-      if (i == bytes-1)
-       LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
+      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);
+       }
+    }
 
-  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 +85,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 +98,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 +111,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);
@@ -124,30 +153,43 @@ void bmp_init(void)
     bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
 }
 
-#if 0
-void run_test(void)
-{
-  for (;;)
-    {
-      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);
+enum bmp_state {
+  BMP_IDLE,
+  BMP_TEMP,
+  BMP_PRESSURE,
+};
 
-      uint oss = 3;    // Over-sampling setting
-      uint raw_press = bmp_measure(0xf4 | (oss<<6), 3);
-      debug_printf("Raw pressure: %06x\r\n", raw_press);
+static byte bmp_state = BMP_IDLE;
+static u16 raw_temp;
+static u32 raw_press;
+#define BMP_OSS 3
+s16 adjusted_temp;
+s16 adjusted_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);
+void bmp_step(void)
+{
+  switch (bmp_state)
+    {
+    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 done\n");
+      raw_press = bmp_read(0xf6, 2);
+      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");
+      raw_press = bmp_read(0xf6, 3);
+      bmp_recalc(raw_temp, raw_press, BMP_OSS, bmp_constants, &adjusted_temp, &adjusted_press);
+      bmp_state = BMP_IDLE;
+      break;
     }
-#endif
+}
index 043fe33869050840d43adf271d6110e661263783..e2769f28a80d9b21d157df368d6bc03692020fb1 100644 (file)
@@ -125,7 +125,7 @@ int main(void)
   /* USER CODE BEGIN 2 */
   display_init();
   usb_start(&usb);
-  // bmp_init();
+  bmp_init();
 
   LL_TIM_EnableCounter(TIM4);
   LL_TIM_EnableIT_UPDATE(TIM4);
@@ -146,11 +146,17 @@ int main(void)
       {
        display_buffer(rx_packet);
        tx_packet_state = 1;
+       tx_packet[0] = adjusted_temp >> 8;
+       tx_packet[1] = adjusted_temp & 0xff;
+       tx_packet[2] = adjusted_press >> 8;
+       tx_packet[3] = adjusted_press & 0xff;
        usb_ep_send(&usb, 0x82, tx_packet, 8);
        rx_packet_state = 0;
        usb_ep_receive(&usb, 0x01, rx_packet, 64);
       }
 
+    bmp_step();
+
     // debug_printf("Counter = %d\n", cnt);
     // display_counter(cnt);