2 #include "stm32f1xx_hal.h"
7 static uint bmp_read(uint reg, uint bytes)
9 LL_I2C_GenerateStartCondition(I2C1);
10 while (!LL_I2C_IsActiveFlag_SB(I2C1))
12 LL_I2C_TransmitData8(I2C1, 0xee);
13 while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
15 LL_I2C_ClearFlag_ADDR(I2C1);
16 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
18 LL_I2C_TransmitData8(I2C1, reg);
19 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
21 LL_I2C_GenerateStopCondition(I2C1);
23 LL_I2C_GenerateStartCondition(I2C1);
24 while (!LL_I2C_IsActiveFlag_SB(I2C1))
26 LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK);
27 LL_I2C_TransmitData8(I2C1, 0xef);
28 while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
31 // This is quite tricky (see manual)
35 LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
36 LL_I2C_ClearFlag_ADDR(I2C1);
37 LL_I2C_GenerateStopCondition(I2C1);
38 while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
40 d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
44 LL_I2C_ClearFlag_ADDR(I2C1);
45 LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
46 while (!LL_I2C_IsActiveFlag_BTF(I2C1))
48 LL_I2C_GenerateStopCondition(I2C1);
49 for (uint i=0; i<2; i++)
50 d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
54 LL_I2C_ClearFlag_ADDR(I2C2);
57 for (uint i=0; i<bytes; i++)
59 if (i < bytes-3 || i == bytes-1)
61 while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
64 else if (i == bytes-3)
66 while (!LL_I2C_IsActiveFlag_BTF(I2C1))
68 LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
70 else if (i == bytes-2)
71 LL_I2C_GenerateStopCondition(I2C1);
72 d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
79 static void bmp_start_measure(uint type)
81 LL_I2C_GenerateStartCondition(I2C1);
82 while (!LL_I2C_IsActiveFlag_SB(I2C1))
85 LL_I2C_TransmitData8(I2C1, 0xee);
86 while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
88 LL_I2C_ClearFlag_ADDR(I2C1);
90 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
92 LL_I2C_TransmitData8(I2C1, 0xf4);
94 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
96 LL_I2C_TransmitData8(I2C1, type);
98 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
100 LL_I2C_GenerateStopCondition(I2C1);
103 // Formulae from BMP085 specs
104 static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], s16 *tt, s16 *pp)
114 // FIXME: Why is this unused?
120 int X1 = (UT-AC6)*AC5 / (1<<15);
121 int X2 = MC*(1<<11) / (X1+MD);
123 int T = (B5+8) / (1<<4);
127 X1 = (B2*(B6*B6/(1<<12))) / (1<<11);
128 X2 = AC2 * B6 / (1<<11);
130 int B3 = (((AC1*4 + X3) << oss) + 2) / 4;
131 X1 = AC3 * B6 / (1<<13);
132 X2 = (B1*(B6*B6/(1<<12))) / (1<<16);
133 X3 = ((X1+X2) + 2) / (1<<2);
134 uint B4 = (uint)(AC4 * (X3 + 32768)) / (1U<<15);
135 uint B7 = (uint)(UP-B3) * (uint)(50000>>oss);
141 X1 = (p/(1<<8)) * (p/(1<<8));
142 X1 = (X1*3038) / (1<<16);
143 X2 = (-7357*p) / (1<<16);
144 p = p + (X1 + X2 + 3791) / (1<<4);
148 static u16 bmp_constants[11];
152 for (uint i=0; i<11; i++)
153 bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
162 static byte bmp_state = BMP_IDLE;
164 static u32 raw_press;
174 debug_puts("BMP: Start measure\n");
175 bmp_start_measure(0x2e);
179 if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
181 debug_puts("BMP: Temperature done\n");
182 raw_press = bmp_read(0xf6, 2);
183 bmp_start_measure(0xf4 | (BMP_OSS<<6));
187 if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
189 debug_puts("BMP: Pressure done\n");
190 raw_press = bmp_read(0xf6, 3);
191 bmp_recalc(raw_temp, raw_press, BMP_OSS, bmp_constants, &adjusted_temp, &adjusted_press);
192 bmp_state = BMP_IDLE;