libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
dps310.h
Go to the documentation of this file.
1#pragma once
2#ifndef DSY_DPS310_H
3#define DSY_DPS310_H
4
5#define DPS310_I2CADDR_DEFAULT (0x77)
6
7#define DPS310_PRSB2 0x00
8#define DPS310_TMPB2 0x03
9#define DPS310_PRSCFG 0x06
10#define DPS310_TMPCFG 0x07
11#define DPS310_MEASCFG 0x08
12#define DPS310_CFGREG 0x09
13#define DPS310_RESET 0x0C
14#define DPS310_PRODREVID 0x0D
15#define DPS310_TMPCOEFSRCE 0x28
16
17namespace daisy
18{
25{
26 public:
29
50
51 inline void Init(Config config)
52 {
53 config_ = config;
54
55 I2CHandle::Config i2c_config;
57 i2c_config.periph = config.periph;
58 i2c_config.speed = config.speed;
59
60 i2c_config.pin_config.scl = config.scl;
61 i2c_config.pin_config.sda = config.sda;
62
63 i2c_.Init(i2c_config);
64 }
65
66 void Write(uint8_t *data, uint16_t size)
67 {
68 error_ |= I2CHandle::Result::OK
69 != i2c_.TransmitBlocking(config_.address, data, size, 10);
70 }
71
72 void Read(uint8_t *data, uint16_t size)
73 {
74 error_ |= I2CHandle::Result::OK
75 != i2c_.ReceiveBlocking(config_.address, data, size, 10);
76 }
77
82 void Write8(uint8_t reg, uint8_t value)
83 {
84 uint8_t buffer[2];
85
86 buffer[0] = reg;
87 buffer[1] = value;
88
89 Write(buffer, 2);
90 }
91
93 void ReadReg(uint8_t reg, uint8_t *buff, uint8_t size)
94 {
95 Write(&reg, 1);
96 Read(buff, size);
97 }
98
103 uint8_t Read8(uint8_t reg)
104 {
105 uint8_t buffer;
106 ReadReg(reg, &buffer, 1);
107 return buffer;
108 }
109
114 uint16_t Read16(uint8_t reg)
115 {
116 uint8_t buffer[2];
117 ReadReg(reg, buffer, 2);
118
119 return uint16_t(buffer[0]) << 8 | uint16_t(buffer[1]);
120 }
121
126 uint32_t Read24(uint8_t reg)
127 {
128 uint8_t buffer[3];
129
130 ReadReg(reg, buffer, 3);
131
132 return uint32_t(buffer[0]) << 16 | uint32_t(buffer[1]) << 8
133 | uint32_t(buffer[2]);
134 }
135
136 bool GetError()
137 {
138 bool tmp = error_;
139 error_ = false;
140 return tmp;
141 }
142
143 private:
144 I2CHandle i2c_;
145 Config config_;
146
147 // true if error has occured since last check
148 bool error_;
149};
150
153{
154 public:
157
175
176 inline void Init(Config config)
177 {
178 SpiHandle::Config spi_conf;
185
186 spi_conf.periph = config.periph;
187 spi_conf.pin_config.sclk = config.sclk;
188 spi_conf.pin_config.miso = config.miso;
189 spi_conf.pin_config.mosi = config.mosi;
190 spi_conf.pin_config.nss = config.nss;
191
192 spi_.Init(spi_conf);
193 }
194
195 void Write(uint8_t *data, uint16_t size)
196 {
197 error_ |= SpiHandle::Result::OK != spi_.BlockingTransmit(data, size);
198 }
199
200 void Read(uint8_t *data, uint16_t size)
201 {
202 error_ |= SpiHandle::Result::OK != spi_.BlockingReceive(data, size, 10);
203 }
204
209 void Write8(uint8_t reg, uint8_t value)
210 {
211 uint8_t buffer[2];
212
213 buffer[0] = reg & ~0x80;
214 buffer[1] = value;
215
216 Write(buffer, 2);
217 }
218
220 void ReadReg(uint8_t reg, uint8_t *buff, uint8_t size)
221 {
222 reg = uint8_t(reg | 0x80);
223 Write(&reg, 1);
224 Read(buff, size);
225 }
226
231 uint8_t Read8(uint8_t reg)
232 {
233 uint8_t buffer;
234 ReadReg(reg, &buffer, 1);
235 return buffer;
236 }
237
242 uint16_t Read16(uint8_t reg)
243 {
244 uint8_t buffer[2];
245 ReadReg(reg, buffer, 2);
246
247 return uint16_t(buffer[0]) << 8 | uint16_t(buffer[1]);
248 }
249
254 uint32_t Read24(uint8_t reg)
255 {
256 uint8_t buffer[3];
257
258 ReadReg(reg, buffer, 3);
259
260 return uint32_t(buffer[0]) << 16 | uint32_t(buffer[1]) << 8
261 | uint32_t(buffer[2]);
262 }
263
264 bool GetError()
265 {
266 bool tmp = error_;
267 error_ = false;
268 return tmp;
269 }
270
271 private:
272 SpiHandle spi_;
273 bool error_;
274};
275
280template <typename Transport>
282{
283 public:
286
288 = {524288, 1572864, 3670016, 7864320, 253952, 516096, 1040384, 2088960};
289
302
315
326
327 struct Config
328 {
329 typename Transport::Config transport_config;
330
332 };
333
335 {
336 OK = 0,
337 ERR
338 };
339
344 {
345 config_ = config;
346
347 transport_.Init(config_.transport_config);
348
349 // make sure we're talking to the right chip
350 if(Read8(DPS310_PRODREVID) != 0x10)
351 {
352 // No DPS310 detected ... return false
353 return ERR;
354 }
355
356 reset();
358 // default to high precision
361 // continuous
363 // wait until we have at least one good measurement
365 {
366 System::Delay(10);
367 }
368
369 return GetTransportError();
370 }
371
372
374 void reset(void)
375 {
376 Write8(DPS310_RESET, 0x89);
377
378 // Wait for a bit till its out of hardware reset
379 System::Delay(10);
380
381 while(!ReadBits(DPS310_MEASCFG, 1, 6))
382 {
383 System::Delay(1);
384 }
385 }
386
387 static int32_t twosComplement(int32_t val, uint8_t bits)
388 {
389 if(val & ((uint32_t)1 << (bits - 1)))
390 {
391 val -= (uint32_t)1 << bits;
392 }
393 return val;
394 }
395
397 {
398 // Wait till we're ready to read calibration
399 while(!ReadBits(DPS310_MEASCFG, 1, 7))
400 {
401 System::Delay(1);
402 }
403
404 uint8_t coeffs[18];
405 for(uint8_t addr = 0; addr < 18; addr++)
406 {
407 coeffs[addr] = Read8(0x10 + addr);
408 }
409 _c0 = ((uint16_t)coeffs[0] << 4) | (((uint16_t)coeffs[1] >> 4) & 0x0F);
410 _c0 = twosComplement(_c0, 12);
411
412 _c1 = twosComplement((((uint16_t)coeffs[1] & 0x0F) << 8) | coeffs[2],
413 12);
414
415 _c00 = ((uint32_t)coeffs[3] << 12) | ((uint32_t)coeffs[4] << 4)
416 | (((uint32_t)coeffs[5] >> 4) & 0x0F);
417 _c00 = twosComplement(_c00, 20);
418
419 _c10 = (((uint32_t)coeffs[5] & 0x0F) << 16) | ((uint32_t)coeffs[6] << 8)
420 | (uint32_t)coeffs[7];
421 _c10 = twosComplement(_c10, 20);
422
423 _c01 = twosComplement(((uint16_t)coeffs[8] << 8) | (uint16_t)coeffs[9],
424 16);
425 _c11 = twosComplement(
426 ((uint16_t)coeffs[10] << 8) | (uint16_t)coeffs[11], 16);
427 _c20 = twosComplement(
428 ((uint16_t)coeffs[12] << 8) | (uint16_t)coeffs[13], 16);
429 _c21 = twosComplement(
430 ((uint16_t)coeffs[14] << 8) | (uint16_t)coeffs[15], 16);
431 _c30 = twosComplement(
432 ((uint16_t)coeffs[16] << 8) | (uint16_t)coeffs[17], 16);
433 }
434
435
439 bool temperatureAvailable(void) { return ReadBits(DPS310_MEASCFG, 1, 5); }
440
441
445 bool pressureAvailable(void) { return ReadBits(DPS310_MEASCFG, 1, 4); }
446
447
452 float GetAltitude(float seaLevelhPa)
453 {
454 float altitude;
455
456 Process();
457
458 altitude = 44330 * (1.0 - pow((_pressure / 100) / seaLevelhPa, 0.1903));
459
460 return altitude;
461 }
462
463
469 void setMode(dps310_mode_t mode) { WriteBits(DPS310_MEASCFG, mode, 3, 0); }
470
471
477 {
478 WriteBits(DPS310_PRSCFG, rate, 3, 4);
479 WriteBits(DPS310_PRSCFG, os, 4, 0);
480
481 if(os > DPS310_8SAMPLES)
482 {
483 WriteBits(DPS310_CFGREG, 1, 1, 2);
484 }
485 else
486 {
487 WriteBits(DPS310_CFGREG, 0, 1, 2);
488 }
489
490 pressure_scale = oversample_scalefactor[os];
491 }
492
493
499 {
500 WriteBits(DPS310_TMPCFG, rate, 3, 4);
501 WriteBits(DPS310_TMPCFG, os, 4, 0);
502 temp_scale = oversample_scalefactor[os];
503
504 // Set shift bit if necessary
505 if(os > DPS310_8SAMPLES)
506 {
507 WriteBits(DPS310_CFGREG, 1, 1, 3);
508 }
509 else
510 {
511 WriteBits(DPS310_CFGREG, 0, 1, 3);
512 }
513
514 // Find out what our calibration source is
515 uint8_t read = ReadBits(DPS310_TMPCOEFSRCE, 1, 7);
516 WriteBits(DPS310_TMPCFG, read, 1, 7);
517 }
518
519
523 void Process(void)
524 {
525 raw_temperature = twosComplement(Read24(DPS310_TMPB2), 24);
526 raw_pressure = twosComplement(Read24(DPS310_PRSB2), 24);
527
528 _scaled_rawtemp = (float)raw_temperature / temp_scale;
529 _temperature = _scaled_rawtemp * _c1 + _c0 / 2.0;
530
531 _pressure = (float)raw_pressure / pressure_scale;
532
533 _pressure
534 = (int32_t)_c00
535 + _pressure
536 * ((int32_t)_c10
537 + _pressure
538 * ((int32_t)_c20 + _pressure * (int32_t)_c30))
539 + _scaled_rawtemp
540 * ((int32_t)_c01
541 + _pressure
542 * ((int32_t)_c11 + _pressure * (int32_t)_c21));
543 }
544
548 float GetTemperature() { return _temperature; }
549
553 float GetPressure() { return _pressure / 100; }
554
559 void Write8(uint8_t reg, uint8_t value)
560 {
561 return transport_.Write8(reg, value);
562 }
563
565 void ReadReg(uint8_t reg, uint8_t *buff, uint8_t size)
566 {
567 return transport_.ReadReg(reg, buff, size);
568 }
569
574 uint8_t Read8(uint8_t reg) { return transport_.Read8(reg); }
575
580 uint16_t Read16(uint8_t reg) { return transport_.Read16(reg); }
581
586 uint32_t Read24(uint8_t reg) { return transport_.Read24(reg); }
587
592 uint16_t Read16_LE(uint8_t reg)
593 {
594 uint16_t temp = Read16(reg);
595 return (temp >> 8) | (temp << 8);
596 }
597
602 int16_t ReadS16(uint8_t reg) { return (int16_t)Read16(reg); }
603
608 int16_t ReadS16_LE(uint8_t reg) { return (int16_t)Read16_LE(reg); }
609
610
611 uint8_t ReadBits(uint8_t reg, uint8_t bits, uint8_t shift)
612 {
613 uint8_t val = Read8(reg);
614 val >>= shift;
615 return val & ((1 << (bits)) - 1);
616 }
617
618 void WriteBits(uint8_t reg, uint8_t data, uint8_t bits, uint8_t shift)
619 {
620 uint8_t val = Read8(reg);
621
622 // mask off the data before writing
623 uint8_t mask = (1 << (bits)) - 1;
624 data &= mask;
625
626 mask <<= shift;
627 val &= ~mask; // remove the current data at that spot
628 val |= data << shift; // and add in the new data
629
630 Write8(reg, val);
631 }
632
636 Result GetTransportError() { return transport_.GetError() ? ERR : OK; }
637
638 private:
639 Config config_;
640 Transport transport_;
641
642 int16_t _c0, _c1, _c01, _c11, _c20, _c21, _c30;
643 int32_t _c00, _c10;
644
645 int32_t raw_pressure, raw_temperature;
646 float _temperature, _scaled_rawtemp, _pressure;
647 int32_t temp_scale, pressure_scale;
648};
649
654} // namespace daisy
655#endif
Device support for DPS310 Barometric Pressure and Altitude Sensor.
Definition dps310.h:282
bool temperatureAvailable(void)
Definition dps310.h:439
static int32_t twosComplement(int32_t val, uint8_t bits)
Definition dps310.h:387
float GetPressure()
Definition dps310.h:553
float GetAltitude(float seaLevelhPa)
Definition dps310.h:452
Result
Definition dps310.h:335
@ OK
Definition dps310.h:336
@ ERR
Definition dps310.h:337
dps310_mode_t
Definition dps310.h:318
@ DPS310_CONT_TEMP
Continuous pressure measurements.
Definition dps310.h:323
@ DPS310_IDLE
Stopped/idle.
Definition dps310.h:319
@ DPS310_CONT_PRESSURE
Continuous pressure measurements.
Definition dps310.h:322
@ DPS310_ONE_PRESSURE
Take single pressure measurement.
Definition dps310.h:320
@ DPS310_CONT_PRESTEMP
Continuous temp+pressure measurements.
Definition dps310.h:324
@ DPS310_ONE_TEMPERATURE
Take single temperature measurement.
Definition dps310.h:321
void Write8(uint8_t reg, uint8_t value)
Definition dps310.h:559
void reset(void)
Definition dps310.h:374
int16_t ReadS16_LE(uint8_t reg)
Definition dps310.h:608
uint16_t Read16_LE(uint8_t reg)
Definition dps310.h:592
int32_t oversample_scalefactor[8]
Definition dps310.h:288
float GetTemperature()
Definition dps310.h:548
Dps310()
Definition dps310.h:284
dps310_oversample_t
Definition dps310.h:305
@ DPS310_16SAMPLES
16 Hz
Definition dps310.h:310
@ DPS310_2SAMPLES
2 Hz
Definition dps310.h:307
@ DPS310_1SAMPLE
1 Hz
Definition dps310.h:306
@ DPS310_8SAMPLES
8 Hz
Definition dps310.h:309
@ DPS310_64SAMPLES
64 Hz
Definition dps310.h:312
@ DPS310_128SAMPLES
128 Hz
Definition dps310.h:313
@ DPS310_4SAMPLES
4 Hz
Definition dps310.h:308
@ DPS310_32SAMPLES
32 Hz
Definition dps310.h:311
uint16_t Read16(uint8_t reg)
Definition dps310.h:580
int16_t ReadS16(uint8_t reg)
Definition dps310.h:602
Result Init(Config config)
Definition dps310.h:343
void setMode(dps310_mode_t mode)
Definition dps310.h:469
void Process(void)
Definition dps310.h:523
void ReadReg(uint8_t reg, uint8_t *buff, uint8_t size)
Definition dps310.h:565
uint32_t Read24(uint8_t reg)
Definition dps310.h:586
void WriteBits(uint8_t reg, uint8_t data, uint8_t bits, uint8_t shift)
Definition dps310.h:618
dps310_rate_t
Definition dps310.h:292
@ DPS310_16HZ
16 Hz
Definition dps310.h:297
@ DPS310_128HZ
128 Hz
Definition dps310.h:300
@ DPS310_8HZ
8 Hz
Definition dps310.h:296
@ DPS310_1HZ
1 Hz
Definition dps310.h:293
@ DPS310_32HZ
32 Hz
Definition dps310.h:298
@ DPS310_2HZ
2 Hz
Definition dps310.h:294
@ DPS310_4HZ
4 Hz
Definition dps310.h:295
@ DPS310_64HZ
64 Hz
Definition dps310.h:299
Result GetTransportError()
Definition dps310.h:636
uint8_t ReadBits(uint8_t reg, uint8_t bits, uint8_t shift)
Definition dps310.h:611
~Dps310()
Definition dps310.h:285
uint8_t Read8(uint8_t reg)
Definition dps310.h:574
void configurePressure(dps310_rate_t rate, dps310_oversample_t os)
Definition dps310.h:476
void configureTemperature(dps310_rate_t rate, dps310_oversample_t os)
Definition dps310.h:498
void _readCalibration(void)
Definition dps310.h:396
bool pressureAvailable(void)
Definition dps310.h:445
Definition dps310.h:25
uint32_t Read24(uint8_t reg)
Definition dps310.h:126
uint8_t Read8(uint8_t reg)
Definition dps310.h:103
void Read(uint8_t *data, uint16_t size)
Definition dps310.h:72
bool GetError()
Definition dps310.h:136
uint16_t Read16(uint8_t reg)
Definition dps310.h:114
Dps310I2CTransport()
Definition dps310.h:27
void Write(uint8_t *data, uint16_t size)
Definition dps310.h:66
~Dps310I2CTransport()
Definition dps310.h:28
void ReadReg(uint8_t reg, uint8_t *buff, uint8_t size)
Definition dps310.h:93
void Init(Config config)
Definition dps310.h:51
void Write8(uint8_t reg, uint8_t value)
Definition dps310.h:82
Definition dps310.h:153
Dps310SpiTransport()
Definition dps310.h:155
uint16_t Read16(uint8_t reg)
Definition dps310.h:242
uint32_t Read24(uint8_t reg)
Definition dps310.h:254
void Write(uint8_t *data, uint16_t size)
Definition dps310.h:195
void Init(Config config)
Definition dps310.h:176
uint8_t Read8(uint8_t reg)
Definition dps310.h:231
void Write8(uint8_t reg, uint8_t value)
Definition dps310.h:209
void Read(uint8_t *data, uint16_t size)
Definition dps310.h:200
bool GetError()
Definition dps310.h:264
void ReadReg(uint8_t reg, uint8_t *buff, uint8_t size)
Definition dps310.h:220
~Dps310SpiTransport()
Definition dps310.h:156
Definition i2c.h:26
Result Init(const Config &config)
Result ReceiveBlocking(uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout)
Result TransmitBlocking(uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout)
Definition spi.h:24
Result BlockingTransmit(uint8_t *buff, size_t size, uint32_t timeout=100)
Result BlockingReceive(uint8_t *buffer, uint16_t size, uint32_t timeout)
Result Init(const Config &config)
static void Delay(uint32_t delay_ms)
#define DPS310_PRODREVID
Register that contains the part ID.
Definition dps310.h:14
#define DPS310_I2CADDR_DEFAULT
Default breakout addres.
Definition dps310.h:5
#define DPS310_TMPB2
Highest byte of temperature data.
Definition dps310.h:8
#define DPS310_PRSB2
Highest byte of pressure data.
Definition dps310.h:7
#define DPS310_CFGREG
Interrupt/FIFO configuration.
Definition dps310.h:12
#define DPS310_TMPCFG
Temperature configuration.
Definition dps310.h:10
#define DPS310_MEASCFG
Sensor configuration.
Definition dps310.h:11
#define DPS310_RESET
Soft reset.
Definition dps310.h:13
#define DPS310_PRSCFG
Pressure configuration.
Definition dps310.h:9
#define DPS310_TMPCOEFSRCE
Temperature calibration src.
Definition dps310.h:15
Hardware defines and helpers for daisy field platform.
Definition index.h:2
@ PORTB
Definition daisy_core.h:178
@ PORTG
Definition daisy_core.h:183
Definition dps310.h:328
Transport::Config transport_config
Definition dps310.h:329
Config()
Definition dps310.h:331
Definition dps310.h:31
Config()
Definition dps310.h:39
Pin scl
Definition dps310.h:34
I2CHandle::Config::Speed speed
Definition dps310.h:33
uint8_t address
Definition dps310.h:37
I2CHandle::Config::Peripheral periph
Definition dps310.h:32
Pin sda
Definition dps310.h:35
Definition dps310.h:159
Config()
Definition dps310.h:166
Pin sclk
Definition dps310.h:161
Pin miso
Definition dps310.h:162
Pin mosi
Definition dps310.h:163
Pin nss
Definition dps310.h:164
SpiHandle::Config::Peripheral periph
Definition dps310.h:160
Definition i2c.h:30
struct daisy::I2CHandle::Config::@15 pin_config
Mode mode
Definition i2c.h:65
Pin scl
Definition i2c.h:60
Speed
Definition i2c.h:51
Speed speed
Definition i2c.h:64
Peripheral periph
Definition i2c.h:57
Peripheral
Definition i2c.h:40
Pin sda
Definition i2c.h:61
representation of hardware port/pin combination
Definition daisy_core.h:193
Definition spi.h:27
ClockPolarity clock_polarity
Definition spi.h:104
Peripheral periph
Definition spi.h:100
Pin nss
Definition spi.h:88
struct daisy::SpiHandle::Config::@18 pin_config
Mode mode
Definition spi.h:101
Peripheral
Definition spi.h:29
Pin mosi
Definition spi.h:87
Pin sclk
Definition spi.h:85
ClockPhase clock_phase
Definition spi.h:105
BaudPrescaler baud_prescaler
Definition spi.h:107
Pin miso
Definition spi.h:86
Direction direction
Definition spi.h:102