]> mj.ucw.cz Git - home-hw.git/blob - Src/bmp085.c
BMP085 basics
[home-hw.git] / Src / bmp085.c
1 #include "stm32f1xx.h"
2 #include "stm32f1xx_hal.h"
3
4 #include "util.h"
5 #include "app.h"
6
7 static uint bmp_read(uint reg, uint bytes)
8 {
9   LL_I2C_GenerateStartCondition(I2C1);
10   while (!LL_I2C_IsActiveFlag_SB(I2C1))
11     ;
12   LL_I2C_TransmitData8(I2C1, 0xee);
13   while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
14     ;
15   LL_I2C_ClearFlag_ADDR(I2C2);
16   while (!LL_I2C_IsActiveFlag_TXE(I2C1))
17     ;
18   LL_I2C_TransmitData8(I2C1, reg);
19   while (!LL_I2C_IsActiveFlag_TXE(I2C1))
20     ;
21   LL_I2C_GenerateStopCondition(I2C1);
22
23   LL_I2C_GenerateStartCondition(I2C1);
24   while (!LL_I2C_IsActiveFlag_SB(I2C1))
25     ;
26   LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK);
27   LL_I2C_TransmitData8(I2C1, 0xef);
28   while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
29     ;
30   LL_I2C_ClearFlag_ADDR(I2C2);
31
32   uint d = 0;
33   for (uint i=0; i<bytes; i++)
34     {
35       if (i == bytes-1)
36         LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
37       while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
38         ;
39       d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
40     }
41
42   LL_I2C_GenerateStopCondition(I2C1);
43   return d;
44 }
45
46 static uint bmp_start_measure(uint type, uint bytes)
47 {
48   LL_I2C_GenerateStartCondition(I2C1);
49   while (!LL_I2C_IsActiveFlag_SB(I2C1))
50     ;
51
52   LL_I2C_TransmitData8(I2C1, 0xee);
53   while (!LL_I2C_IsActiveFlag_ADDR(I2C1))
54     ;
55   LL_I2C_ClearFlag_ADDR(I2C2);
56
57   while (!LL_I2C_IsActiveFlag_TXE(I2C1))
58     ;
59   LL_I2C_TransmitData8(I2C1, 0xf4);
60
61   while (!LL_I2C_IsActiveFlag_TXE(I2C1))
62     ;
63   LL_I2C_TransmitData8(I2C1, type);
64
65   while (!LL_I2C_IsActiveFlag_TXE(I2C1))
66     ;
67   LL_I2C_GenerateStopCondition(I2C1);
68
69   while (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
70     ;
71
72   return bmp_read(0xf6, bytes);
73 }
74
75 // Formulae from BMP085 specs
76 static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], int *tt, int *pp)
77 {
78   s16 AC1 = cc[0];
79   s16 AC2 = cc[1];
80   s16 AC3 = cc[2];
81   u16 AC4 = cc[3];
82   u16 AC5 = cc[4];
83   u16 AC6 = cc[5];
84   s16 B1 = cc[6];
85   s16 B2 = cc[7];
86   s16 MB = cc[8];
87   s16 MC = cc[9];
88   s16 MD = cc[10];
89   UP >>= (8-oss);
90
91   int X1 = (UT-AC6)*AC5 / (1<<15);
92   int X2 = MC*(1<<11) / (X1+MD);
93   int B5 = X1 + X2;
94   int T = (B5+8) / (1<<4);
95   *tt = T;
96
97   int B6 = B5 - 4000;
98   X1 = (B2*(B6*B6/(1<<12))) / (1<<11);
99   X2 = AC2 * B6 / (1<<11);
100   int X3 = X1 + X2;
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);
107   int p;
108   if (B7 < 0x80000000)
109     p = (B7*2) / B4;
110   else
111     p = B7 / B4 * 2;
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);
116   *pp = p;
117 }
118
119 static u16 bmp_constants[11];
120
121 void bmp_init(void)
122 {
123   for (uint i=0; i<11; i++)
124     bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
125 }
126
127 #if 0
128 void run_test(void)
129 {
130   for (;;)
131     {
132       debug_puts("Constants:");
133       u16 cc[11];
134       for (uint i=0; i<11; i++)
135         {
136           cc[i] = bmp_read(0xaa + 2*i, 2);
137           debug_printf(" %04x", cc[i]);
138         }
139       debug_puts("\r\n");
140
141       uint raw_temp = bmp_measure(0x2e, 2);
142       debug_printf("Raw temperature: %04x\r\n", raw_temp);
143
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);
147
148       int temp, 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);
152     }
153 #endif