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(I2C2);
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))
30 LL_I2C_ClearFlag_ADDR(I2C2);
33 for (uint i=0; i<bytes; i++)
36 LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
37 while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
39 d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
42 LL_I2C_GenerateStopCondition(I2C1);
46 static uint bmp_start_measure(uint type, uint bytes)
48 LL_I2C_GenerateStartCondition(I2C1);
49 while (!LL_I2C_IsActiveFlag_SB(I2C1))
52 LL_I2C_TransmitData8(I2C1, 0xee);
53 while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
55 LL_I2C_ClearFlag_ADDR(I2C2);
57 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
59 LL_I2C_TransmitData8(I2C1, 0xf4);
61 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
63 LL_I2C_TransmitData8(I2C1, type);
65 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
67 LL_I2C_GenerateStopCondition(I2C1);
69 while (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
72 return bmp_read(0xf6, bytes);
75 // Formulae from BMP085 specs
76 static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], int *tt, int *pp)
91 int X1 = (UT-AC6)*AC5 / (1<<15);
92 int X2 = MC*(1<<11) / (X1+MD);
94 int T = (B5+8) / (1<<4);
98 X1 = (B2*(B6*B6/(1<<12))) / (1<<11);
99 X2 = AC2 * B6 / (1<<11);
101 int B3 = (((AC1*4 + X3) << oss) + 2) / 4;
102 X1 = AC3 * B6 / (1<<13);
103 X2 = (B1*(B6*B6/(1<<12))) / (1<<16);
104 X3 = ((X1+X2) + 2) / (1<<2);
105 uint B4 = (uint)(AC4 * (X3 + 32768)) / (1U<<15);
106 uint B7 = (uint)(UP-B3) * (uint)(50000>>oss);
112 X1 = (p/(1<<8)) * (p/(1<<8));
113 X1 = (X1*3038) / (1<<16);
114 X2 = (-7357*p) / (1<<16);
115 p = p + (X1 + X2 + 3791) / (1<<4);
119 static u16 bmp_constants[11];
123 for (uint i=0; i<11; i++)
124 bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
132 debug_puts("Constants:");
134 for (uint i=0; i<11; i++)
136 cc[i] = bmp_read(0xaa + 2*i, 2);
137 debug_printf(" %04x", cc[i]);
141 uint raw_temp = bmp_measure(0x2e, 2);
142 debug_printf("Raw temperature: %04x\r\n", raw_temp);
144 uint oss = 3; // Over-sampling setting
145 uint raw_press = bmp_measure(0xf4 | (oss<<6), 3);
146 debug_printf("Raw pressure: %06x\r\n", raw_press);
149 bmp_recalc(raw_temp, raw_press, oss, cc, &temp, &press);
150 debug_printf("Temperature: %d ddegC\r\n", temp);
151 debug_printf("Pressure: %d Pa\r\n", press);