]> mj.ucw.cz Git - home-hw.git/blobdiff - Src/bmp085.c
More I2C debugging
[home-hw.git] / Src / bmp085.c
index 76f548f16230d46bc5e1a05990a86e8399fdae5e..76c64811c940059b964fc2fe257e735eda795a5c 100644 (file)
@@ -23,7 +23,7 @@ static uint bmp_read(uint reg, uint bytes)
   LL_I2C_GenerateStartCondition(I2C1);
   while (!LL_I2C_IsActiveFlag_SB(I2C1))
     ;
-  LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK);
+  LL_I2C_DisableBitPOS(I2C1);
   LL_I2C_TransmitData8(I2C1, 0xef);
   while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
     ;
@@ -41,6 +41,7 @@ static uint bmp_read(uint reg, uint bytes)
     }
   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))
@@ -51,24 +52,31 @@ static uint bmp_read(uint reg, uint bytes)
     }
   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 < bytes-3 || i == bytes-1)
+         if (i+3 < bytes || i+1 == bytes)
            {
+#if 0
              while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
                ;
+#endif
            }
-         else if (i == bytes-3)
+         else if (i+3 == bytes)
            {
              while (!LL_I2C_IsActiveFlag_BTF(I2C1))
                ;
              LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
            }
-         else if (i == bytes-2)
-           LL_I2C_GenerateStopCondition(I2C1);
+         else if (i+2 == bytes)
+           {
+             while (!LL_I2C_IsActiveFlag_BTF(I2C1))
+               ;
+             LL_I2C_GenerateStopCondition(I2C1);
+           }
          d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
        }
     }
@@ -178,17 +186,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;
     }