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);
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))
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))
;
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];
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);
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
+}