2 #include "stm32f1xx_hal.h"
7 static byte bmp_i2c_buf[4];
8 volatile byte *bmp_i2c_ptr;
9 volatile byte bmp_i2c_len;
10 volatile byte bmp_i2c_addr;
12 static uint bmp_read(uint reg, uint bytes)
15 bmp_i2c_ptr = bmp_i2c_buf;
19 LL_I2C_GenerateStartCondition(I2C1);
20 LL_I2C_EnableIT_TX(I2C1);
25 bmp_i2c_ptr = bmp_i2c_buf;
29 LL_I2C_GenerateStartCondition(I2C1);
30 LL_I2C_EnableIT_RX(I2C1);
36 for (uint i=0; i<bytes; i++)
37 d = (d << 8) | bmp_i2c_buf[i];
42 static void bmp_start_measure(uint type)
44 LL_I2C_GenerateStartCondition(I2C1);
45 while (!LL_I2C_IsActiveFlag_SB(I2C1))
48 LL_I2C_TransmitData8(I2C1, 0xee);
49 while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
51 LL_I2C_ClearFlag_ADDR(I2C1);
53 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
55 LL_I2C_TransmitData8(I2C1, 0xf4);
57 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
59 LL_I2C_TransmitData8(I2C1, type);
61 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
63 LL_I2C_GenerateStopCondition(I2C1);
66 // Formulae from BMP085 specs
67 static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], s16 *tt, s16 *pp)
77 // FIXME: Why is this unused?
83 int X1 = (UT-AC6)*AC5 / (1<<15);
84 int X2 = MC*(1<<11) / (X1+MD);
86 int T = (B5+8) / (1<<4);
90 X1 = (B2*(B6*B6/(1<<12))) / (1<<11);
91 X2 = AC2 * B6 / (1<<11);
93 int B3 = (((AC1*4 + X3) << oss) + 2) / 4;
94 X1 = AC3 * B6 / (1<<13);
95 X2 = (B1*(B6*B6/(1<<12))) / (1<<16);
96 X3 = ((X1+X2) + 2) / (1<<2);
97 uint B4 = (uint)(AC4 * (X3 + 32768)) / (1U<<15);
98 uint B7 = (uint)(UP-B3) * (uint)(50000>>oss);
104 X1 = (p/(1<<8)) * (p/(1<<8));
105 X1 = (X1*3038) / (1<<16);
106 X2 = (-7357*p) / (1<<16);
107 p = p + (X1 + X2 + 3791) / (1<<4);
111 static u16 bmp_constants[11];
115 for (uint i=0; i<11; i++)
117 bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
118 debug_printf("BMP: const[%d] = %u\n", i, bmp_constants[i]);
128 static byte bmp_state = BMP_IDLE;
130 static u32 raw_press;
140 debug_puts("BMP: Start measure\n");
141 bmp_start_measure(0x2e);
145 if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
147 debug_puts("BMP: Temperature measured\n");
148 raw_temp = bmp_read(0xf6, 2);
149 debug_printf("BMP: Temperature read: %u\n", raw_temp);
150 bmp_start_measure(0xf4 | (BMP_OSS<<6));
154 if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
156 debug_puts("BMP: Pressure measured\n");
157 raw_press = bmp_read(0xf6, 3);
158 debug_printf("BMP: Pressure read: %u\n", raw_press);
159 bmp_recalc(raw_temp, raw_press, BMP_OSS, bmp_constants, &adjusted_temp, &adjusted_press);
160 debug_printf("BMP: Adjusted temp %u, press %u\n", adjusted_temp, adjusted_press);
161 bmp_state = BMP_IDLE;