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_DisableBitPOS(I2C1);
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_EnableBitPOS(I2C1);
45 LL_I2C_ClearFlag_ADDR(I2C1);
46 LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
47 while (!LL_I2C_IsActiveFlag_BTF(I2C1))
49 LL_I2C_GenerateStopCondition(I2C1);
50 for (uint i=0; i<2; i++)
51 d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
55 LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK);
56 LL_I2C_ClearFlag_ADDR(I2C2);
59 for (uint i=0; i<bytes; i++)
61 if (i+3 < bytes || i+1 == bytes)
64 while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
68 else if (i+3 == bytes)
70 while (!LL_I2C_IsActiveFlag_BTF(I2C1))
72 LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
74 else if (i+2 == bytes)
76 while (!LL_I2C_IsActiveFlag_BTF(I2C1))
78 LL_I2C_GenerateStopCondition(I2C1);
80 d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
87 static void bmp_start_measure(uint type)
89 LL_I2C_GenerateStartCondition(I2C1);
90 while (!LL_I2C_IsActiveFlag_SB(I2C1))
93 LL_I2C_TransmitData8(I2C1, 0xee);
94 while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
96 LL_I2C_ClearFlag_ADDR(I2C1);
98 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
100 LL_I2C_TransmitData8(I2C1, 0xf4);
102 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
104 LL_I2C_TransmitData8(I2C1, type);
106 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
108 LL_I2C_GenerateStopCondition(I2C1);
111 // Formulae from BMP085 specs
112 static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], s16 *tt, s16 *pp)
122 // FIXME: Why is this unused?
128 int X1 = (UT-AC6)*AC5 / (1<<15);
129 int X2 = MC*(1<<11) / (X1+MD);
131 int T = (B5+8) / (1<<4);
135 X1 = (B2*(B6*B6/(1<<12))) / (1<<11);
136 X2 = AC2 * B6 / (1<<11);
138 int B3 = (((AC1*4 + X3) << oss) + 2) / 4;
139 X1 = AC3 * B6 / (1<<13);
140 X2 = (B1*(B6*B6/(1<<12))) / (1<<16);
141 X3 = ((X1+X2) + 2) / (1<<2);
142 uint B4 = (uint)(AC4 * (X3 + 32768)) / (1U<<15);
143 uint B7 = (uint)(UP-B3) * (uint)(50000>>oss);
149 X1 = (p/(1<<8)) * (p/(1<<8));
150 X1 = (X1*3038) / (1<<16);
151 X2 = (-7357*p) / (1<<16);
152 p = p + (X1 + X2 + 3791) / (1<<4);
156 static u16 bmp_constants[11];
160 for (uint i=0; i<11; i++)
161 bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
170 static byte bmp_state = BMP_IDLE;
172 static u32 raw_press;
182 debug_puts("BMP: Start measure\n");
183 bmp_start_measure(0x2e);
187 if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
189 debug_puts("BMP: Temperature measured\n");
190 raw_temp = bmp_read(0xf6, 2);
191 debug_printf("BMP: Temperature read: %u\n", raw_temp);
192 bmp_start_measure(0xf4 | (BMP_OSS<<6));
196 if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
198 debug_puts("BMP: Pressure measured\n");
199 raw_press = bmp_read(0xf6, 3);
200 debug_printf("BMP: Pressure read: %u\n", raw_press);
201 bmp_recalc(raw_temp, raw_press, BMP_OSS, bmp_constants, &adjusted_temp, &adjusted_press);
202 debug_printf("BMP: Adjusted temp %u, press %u\n", adjusted_temp, adjusted_press);
203 bmp_state = BMP_IDLE;