forked from simondlevy/BreezySTM32
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdrv_ms5611.c
456 lines (398 loc) · 10.4 KB
/
drv_ms5611.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
drv_ms5611.c : driver for Measurement Specialties MS5611 barometer
Adapted from https://github.com/multiwii/baseflight/blob/master/src/drv_ms5611.c
This file is part of BreezySTM32.
BreezySTM32 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BreezySTM32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BreezySTM32. If not, see <http://www.gnu.org/licenses/>.
*/
#include <breezystm32.h>
#include <limits.h>
#include <math.h>
// MS5611, Standard address 0x77
static const uint8_t ADDR = 0x77;
static const uint8_t PROM_RD = 0xA0; // Prom read command
#define CMD_RESET 0x1E // ADC reset command
#define CMD_ADC_READ 0x00 // ADC read command
#define CMD_ADC_CONV 0x40 // ADC conversion command
#define CMD_ADC_D1 0x00 // ADC D1 conversion
#define CMD_ADC_D2 0x10 // ADC D2 conversion
#define CMD_ADC_256 0x00 // ADC OSR=256
#define CMD_ADC_512 0x02 // ADC OSR=512
#define CMD_ADC_1024 0x04 // ADC OSR=1024
#define CMD_ADC_2048 0x06 // ADC OSR=2048
#define CMD_ADC_4096 0x08 // ADC OSR=4096
#define CMD_PROM_RD 0xA0 // Prom read command
#define PROM_NB 8
#define REBOOT_PERIOD_MS 1000 * 60 * 30 // reboot the device every 30 minutes
static uint8_t cmd;
static void master_cb(uint8_t result);
uint8_t pres_buf_[3];
uint8_t temp_buf_[3];
int32_t pres_raw_;
int32_t temp_raw_;
float pressure_;
float temperature_;
uint16_t prom[8];
uint32_t next_update_ms_;
uint32_t next_reboot_ms_;
uint32_t last_update_ms_;
bool waiting_for_cb_;
bool new_data_;
bool baro_present_;
typedef enum
{
START_TEMP = 0,
READ_TEMP = 1,
START_PRESS = 2,
READ_PRESS = 3,
} state_t;
static state_t state_;
typedef enum
{
CB_TEMP_READ1,
CB_TEMP_READ2,
CB_PRES_READ1,
CB_PRES_READ2,
CB_TEMP_START,
CB_PRES_START,
CB_RESET,
CB_WRITE_ZERO,
} callback_type_t;
static callback_type_t callback_type_;
static void reset(void)
{
i2cWrite(ADDR, CMD_RESET, 1);
delayMicroseconds(2800);
}
static bool read_prom()
{
uint8_t buf[2] = {0, 0};
// try a few times
for (int i = 0; i < 8; i++)
{
i2cWrite(ADDR, 0xFF, PROM_RD + 2* i);
if (i2cRead(ADDR, 0xFF, 2, buf) == true)
prom[i] = (uint16_t)(buf[0] << 8 | buf[1]);
else
{
reset();
delay(3);
i2cWrite(0, 0, 0);
delay(3);
// didn't work, try again
return false;
}
}
return true;
}
int8_t calc_crc()
{
uint32_t res = 0;
uint8_t crc = prom[7] & 0xF;
prom[7] &= 0xFF00;
bool blank = true;
for (int i = 0; i < 16; i++)
{
if (prom[i >> 1])
{
blank = false;
}
if (i & 1)
res ^= ((prom[i >> 1]) & 0x00FF);
else
res ^= (prom[i >> 1] >> 8);
for (int j = 8; j > 0; j--)
{
if (res & 0x8000)
res ^= 0x1800;
res <<= 1;
}
}
prom[7] |= crc;
if (!blank && crc == ((res >> 12) & 0xF))
return 0;
return -1;
}
bool ms5611_init()
{
baro_present_ = false;
while (millis() < 10); // wait for chip to power on
next_update_ms_ = millis();
last_update_ms_ = millis();
i2cWrite(0, 0, 0); // cycle the I2C clk signal (from the datasheet)
delay(1);
if (i2cWrite(ADDR, RESET, 1) != true)
{
baro_present_ = false;
return false;
}
else
{
baro_present_ = true;
}
delay(3);
// Read the PROM (try a couple of times if it fails)
bool got_valid_prom = false;
for (int i = 0; i < 5; i++)
{
if (read_prom() == true)
{
if (calc_crc() != 0)
continue;
else
{
got_valid_prom = true;
break;
}
}
}
if (got_valid_prom)
{
state_ = START_TEMP;
new_data_ = false;
baro_present_ = true;
next_reboot_ms_ = REBOOT_PERIOD_MS;
waiting_for_cb_ = false;
return true;
}
else
{
return false;
}
}
bool ms5611_present()
{
if (baro_present_ && waiting_for_cb_ && (millis() > last_update_ms_ + 200))
baro_present_ = false;
return baro_present_;
}
static void convert()
{
int32_t press = 0;
int32_t temp = 0;
int64_t delta = 0;
temp_raw_ = (temp_buf_[0] << 16) | (temp_buf_[1] << 8) | temp_buf_[2];
pres_raw_ = (pres_buf_[0] << 16) | (pres_buf_[1] << 8) | pres_buf_[2];
if(pres_raw_ > 9085466 * 2 / 3 && temp_raw_ > 0)
{
int32_t dT = temp_raw_ - ((int32_t)(prom[5]) << 8);
int64_t off = ((int64_t)(prom[2]) << 16) + (((int64_t)(prom[4]) * dT) >> 7);
int64_t sens = ((int64_t)(prom[1]) << 15) + (((int64_t)(prom[3]) * dT) >> 8);
temp = 2000 + ((dT * (int64_t)(prom[6])) >> 23);
// temperature lower than 20degC
if (temp < 2000)
{
delta = temp - 2000;
delta = 5 * delta * delta;
off -= delta >> 1;
sens -= delta >> 2;
// temperature lower than -15degC
if (temp < -1500)
{
delta = temp + 1500;
delta = delta * delta;
off -= 7 * delta;
sens -= (11 * delta) >> 1;
}
temp -= ((dT * dT) >> 31);
}
press = ((((uint64_t)(pres_raw_) * sens) >> 21) - off) >> 15;
pressure_ = (float)(press); // Pa
temperature_ = (float)(temp) / 100.0 + 273.0; // K
}
new_data_ = false;
}
void temp_read_cb1(uint8_t result)
{
(void) result;
waiting_for_cb_ = false;
last_update_ms_ = millis();
callback_type_ = CB_TEMP_READ2;
i2c_queue_job(READ, ADDR, 0xFF, temp_buf_, 3, NULL, &master_cb);
}
void pres_read_cb1(uint8_t result)
{
(void) result;
waiting_for_cb_ = false;
last_update_ms_ = millis();
callback_type_ = CB_PRES_READ2;
i2c_queue_job(READ, ADDR, 0xFF, pres_buf_, 3, NULL, &master_cb);
}
void temp_read_cb2(uint8_t result)
{
(void) result;
state_ = START_PRESS;
waiting_for_cb_ = false;
last_update_ms_ = millis();
next_update_ms_ = last_update_ms_ + 10;
new_data_ = true;
}
void pres_read_cb2(uint8_t result)
{
(void) result;
state_ = START_TEMP;
waiting_for_cb_ = false;
last_update_ms_ = millis();
next_update_ms_ = last_update_ms_ + 10;
new_data_ = true;
}
void temp_start_cb(uint8_t result)
{
(void) result;
state_ = READ_TEMP;
waiting_for_cb_ = false;
last_update_ms_ = millis();
next_update_ms_ = last_update_ms_ + 10;
}
void pres_start_cb(uint8_t result)
{
(void) result;
state_ = READ_PRESS;
waiting_for_cb_ = false;
last_update_ms_ = millis();
next_update_ms_ = last_update_ms_ + 10;
}
void reset_cb(uint8_t result)
{
(void) result;
last_update_ms_ = millis();
next_update_ms_ = last_update_ms_ + 10;
next_reboot_ms_ = last_update_ms_ + REBOOT_PERIOD_MS;
waiting_for_cb_ = false;
callback_type_ = CB_WRITE_ZERO;
uint8_t data = 0;
i2c_queue_job(WRITE, 0, 0xFF, &data, 1, NULL, &master_cb);
}
void write_zero_cb(uint8_t result)
{
(void) result;
last_update_ms_ = millis();
next_update_ms_ = last_update_ms_ + 10;
next_reboot_ms_ = last_update_ms_ + REBOOT_PERIOD_MS;
waiting_for_cb_ = false;
state_ = START_TEMP;
}
void master_cb(uint8_t result)
{
if (result != I2C_JOB_ERROR)
baro_present_ = true;
switch (callback_type_)
{
case CB_TEMP_READ1:
temp_read_cb1(result);
break;
case CB_TEMP_READ2:
temp_read_cb2(result);
break;
case CB_PRES_READ1:
pres_read_cb1(result);
break;
case CB_PRES_READ2:
pres_read_cb2(result);
break;
case CB_TEMP_START:
temp_start_cb(result);
break;
case CB_PRES_START:
pres_start_cb(result);
break;
case CB_RESET:
reset_cb(result);
break;
case CB_WRITE_ZERO:
write_zero_cb(result);
break;
}
}
bool start_temp_meas()
{
waiting_for_cb_ = true;
last_update_ms_ = millis();
callback_type_ = CB_TEMP_START;
cmd = CMD_ADC_CONV + CMD_ADC_D2 + CMD_ADC_4096;
i2c_queue_job(WRITE, ADDR, 0xFF, &cmd, 1, NULL, &master_cb);
return true;
}
bool start_pres_meas()
{
waiting_for_cb_ = true;
last_update_ms_ = millis();
callback_type_ = CB_PRES_START;
cmd = CMD_ADC_CONV + CMD_ADC_D1 + CMD_ADC_4096;
i2c_queue_job(WRITE, ADDR, 0xFF, &cmd, 1, NULL, &master_cb);
return true;
}
bool read_pres_mess()
{
waiting_for_cb_ = true;
last_update_ms_ = millis();
callback_type_ = CB_PRES_READ1;
cmd = CMD_ADC_READ;
i2c_queue_job(WRITE, ADDR, 0xFF, &cmd, 1, NULL, &master_cb);
return true;
}
bool read_temp_mess()
{
waiting_for_cb_ = true;
last_update_ms_ = millis();
callback_type_ = CB_TEMP_READ1;
cmd = CMD_ADC_READ;
i2c_queue_job(WRITE, ADDR, 0xFF, &cmd, 1, NULL, &master_cb);
return true;
}
void ms5611_async_update()
{
uint32_t now_ms = millis();
// Sometimes the barometer fails to respond. If this happens, then reset it
// the barometer also seems to stop responding after 72 minutes (suspiciously close to a overflow of uint32_t with a microsecond timer)
// to avoid that, just reboot periodically
if ((waiting_for_cb_ && now_ms) > last_update_ms_ + 20 || (now_ms > next_reboot_ms_))
{
last_update_ms_ = now_ms;
callback_type_ = CB_RESET;
uint8_t command = RESET;
i2c_queue_job(READ, ADDR, 0xFF, &command, 1, NULL, &master_cb);
}
else if (now_ms > next_update_ms_)
{
switch (state_)
{
case START_TEMP:
if (start_temp_meas())
next_update_ms_ += 100;
break;
case READ_TEMP:
if (read_temp_mess())
next_update_ms_ += 100;
break;
case START_PRESS:
if (start_pres_meas())
next_update_ms_ += 100;
break;
case READ_PRESS:
if (read_pres_mess())
next_update_ms_ += 100;
break;
default:
state_ = START_TEMP;
break;
}
}
if (new_data_)
{
convert();
}
}
void ms5611_async_read(float *pressure, float *temperature)
{
(*pressure) = pressure_;
(*temperature) = temperature_;
}