Solaris Software
Loading...
Searching...
No Matches
bmp390.c
Go to the documentation of this file.
1/**
2 * @file bmp390_service.c
3 * @brief BMP390 pressure sensor driver + SPP service implementation.
4 *
5 * Combines the low-level SPI driver with an acquisition task that reads
6 * altitude, pressure and temperature at 5 Hz and logs packets to SD card.
7 */
8
10
11#include "spp/hal/spi/spi.h"
12#include "spp/hal/time/time.h"
15#include "spp/core/packet.h"
18
19#include <string.h>
20#include <math.h>
21#ifdef SPP_DEBUG_PRINT
22#include <stdio.h>
23#endif
24
25
26/* ----------------------------------------------------------------
27 * CONSTANTS
28 * ---------------------------------------------------------------- */
29#define K_BMP390_TASK_TIMEOUT_MS 5000U
30#define K_BMP390_TAG "BMP390"
31
32/* ----------------------------------------------------------------
33 * STATIC FUNCTIONS DECLARATIONS
34 * ---------------------------------------------------------------- */
37static SPP_RetVal_t SPP_SERVICES_BMP390_softReset(void *p_spiHandler);
38static SPP_RetVal_t SPP_SERVICES_BMP390_enableSpiMode(void *p_spiHandler);
39static SPP_RetVal_t SPP_SERVICES_BMP390_configCheck(void *p_spiHandler);
40static SPP_RetVal_t SPP_SERVICES_BMP390_auxConfig(void *p_spiHandler);
41static SPP_RetVal_t SPP_SERVICES_BMP390_prepareMeasure(void *p_spiHandler);
44static SPP_RetVal_t SPP_SERVICES_BMP390_readRawTemp(void *p_spiHandler, uint32_t *raw_temp);
46static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetTemp(void *p_spiHandler, const BMP390_temp_params_t *temp_params,
47 spp_uint32_t *raw_temp, float *comp_temp);
50static SPP_RetVal_t SPP_SERVICES_BMP390_readRawPress(void *p_spiHandler, spp_uint32_t *raw_press);
52static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetPress(void *p_spiHandler, const BMP390_press_params_t *press_params,
53 float t_lin, spp_uint32_t *raw_press, float *comp_press);
54static SPP_RetVal_t SPP_SERVICES_BMP390_getAltitude(void *p_spiHandler, float *altitude_m, float *pressure_pa,
55 float *temperature_c);
56static SPP_RetVal_t SPP_SERVICES_BMP390_intEnableDrdy(void *p_spiHandler);
57
58/* Performance testing*/
59#define K_BMP390_PERFORMANCE_SAMPLES (10000U)
60
61// static spp_uint32_t s_bmpPerformance[K_BMP390_PERFORMANCE_SAMPLES] = {0U};
62// static spp_uint32_t s_tempSpiPerformance[K_BMP390_PERFORMANCE_SAMPLES] = {0U};
63// static spp_uint32_t s_pressSpiPerformance[K_BMP390_PERFORMANCE_SAMPLES] = {0U};
64
65// static spp_uint32_t s_lastTempSpi = 0U;
66// static spp_uint32_t s_lastPressSpi = 0U;
67
68// static spp_uint16_t s_performanceSamples = 0U;
69// static spp_bool_t s_performanceWarmupDone = false;
70
71/* ----------------------------------------------------------------
72* VARIABLES
73* ---------------------------------------------------------------- */
74static const SPP_SERVICE_ProducerContract_t g_bmp390ProducerContract = {.p_nameProducer = "bmp390",
75 .tiemoutMs = K_BMP390_TASK_TIMEOUT_MS,
77 .acquireData = SPP_SERVICES_BMP390_acquireData};
79static const char *const k_svcTag = "BMP_SVC";
80static float s_pd1, s_pd2, s_pd3, s_pd4;
81static float s_po1, s_po2;
82static float s_compPress;
83
84/* ----------------------------------------------------------------
85* PUBLIC FUNCTIONS
86* ---------------------------------------------------------------- */
91/* Performance Test */
92// spp_uint16_t SPP_SERVICES_BMP390_getPerformanceSampleCount(void)
93// {
94// return s_performanceSamples;
95// }
96
97
98// SPP_RetVal_t SPP_SERVICES_BMP390_getPerformanceSample(spp_uint16_t i, spp_uint32_t *p_bmp, spp_uint32_t *p_tempSpi,
99// spp_uint32_t *p_pressSpi)
100// {
101// if ((i >= s_performanceSamples) || (p_bmp == NULL) || (p_tempSpi == NULL) || (p_pressSpi == NULL))
102// {
103// return K_SPP_ERROR;
104// }
105
106// *p_bmp = s_bmpPerformance[i];
107// *p_tempSpi = s_tempSpiPerformance[i];
108// *p_pressSpi = s_pressSpiPerformance[i];
109
110// return K_SPP_OK;
111// }
112
113/* ----------------------------------------------------------------
114* STATIC FUNCTIONS
115* ---------------------------------------------------------------- */
116/**
117* @brief BMP390 sensor instance initialisation. This function is called by the service task.
118 It is responsible for initialising the SPI bus, the GPIO ISR, and the sensor driver.
119* @param p_data Pointer to the BMP390 sensor instance.
120* @return K_SPP_OK on success.
121*/
163
164
165/**
166 * @brief Acquisition callback invoked by the SPP service task on each cycle.
167 *
168 * Checks the DRDY flag, reads compensated altitude, pressure and temperature
169 * from the sensor, packs the values into an SPP packet and publishes it.
170 * Silently returns if DRDY is not set or no free packet is available.
171 *
172 * @param p_data Pointer to the BMP390_t sensor instance.
173 */
175{
176 BMP390_t *p_bmpData = &s_bmpData;
177 float altitude = 0.0f;
178 float pressure = 0.0f;
179 float temperature = 0.0f;
180
181 if (!p_bmpData->gpioConfig.drdyFlag)
182 {
183 return K_SPP_OK;
184 }
185 p_bmpData->gpioConfig.drdyFlag = false;
186
188 if (p_packet == NULL)
189 {
190 SPP_LOGW(k_svcTag, "No free packet");
191 return K_SPP_ERROR;
192 }
193
194 // spp_uint32_t t0 = SPP_HAL_TIME_getTimeUs();
195
196 SPP_RetVal_t ret =
197 SPP_SERVICES_BMP390_getAltitude(p_bmpData->spiConfig.p_spiHandler, &altitude, &pressure, &temperature);
198
199 // spp_uint32_t t1 = SPP_HAL_TIME_getTimeUs();
200
201 if (ret != K_SPP_OK)
202 {
203 SPP_LOGE(k_svcTag, "getAltitude failed ret=%d", (int)ret);
205 return ret;
206 }
207
208 // if (s_performanceWarmupDone == false)
209 // {
210 // s_performanceWarmupDone = true;
211 // }
212 // else if (s_performanceSamples < K_BMP390_PERFORMANCE_SAMPLES)
213 // {
214 // s_bmpPerformance[s_performanceSamples] = t1 - t0;
215 // s_tempSpiPerformance[s_performanceSamples] = s_lastTempSpi;
216 // s_pressSpiPerformance[s_performanceSamples] = s_lastPressSpi;
217
218 // s_performanceSamples++;
219 // }
220
221#ifdef SPP_DEBUG_PRINT
222 SPP_LOGI(K_BMP390_TAG, "[BMP] alt=%.1fm P=%.1fhPa T=%.2fC\n", altitude, pressure / 100.0f, temperature);
223#endif
224
225
226 spp_float32_t payload[3] = {altitude, pressure, temperature};
227 ret = SPP_SERVICES_DATABANK_packetData(p_packet, kpid.value, p_bmpData->seq++, payload,
228 (spp_uint16_t)sizeof(payload));
229
230 // for (int i = 0; i < 3; i++)
231 // {
232 // spp_uint32_t bits;
233 // memcpy(&bits, &payload[i], sizeof(bits)); // reinterpreta los bits sin convertir el valor
234 // printf("payload[%d] = 0x%08lX\n", i, bits);
235 // }
236 if (ret != K_SPP_OK)
237 {
238 SPP_LOGE(k_svcTag, "packetData failed ret=%d", (int)ret);
240 return ret;
241 }
242
243 // Publish the packet
244 (void)SPP_SERVICES_PUBSUB_publish(p_packet);
245 return K_SPP_OK;
246}
247
248
249/* ----------------------------------------------------------------
250 * Driver — configuration helpers
251 * ---------------------------------------------------------------- */
252
253/**
254 * @brief Sends a soft-reset command to the sensor and waits 100 ms for it to reboot.
255 * @param p_spiHandler SPI device handle.
256 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
257 */
259{
261 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
263 return ret;
264}
265
266/**
267 * @brief Writes the interface-config register to lock the sensor into SPI mode.
268 * @param p_spiHandler SPI device handle.
269 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
270 */
272{
274 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, (spp_uint8_t)sizeof(buf));
276 return ret;
277}
278
279/**
280 * @brief Reads the chip-ID register and returns an error if the value is not 0x60.
281 * @param p_spiHandler SPI device handle.
282 * @return K_SPP_OK if chip ID matches, K_SPP_ERROR otherwise.
283 */
285{
286 spp_uint8_t buf[9] = {
290
291 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, (spp_uint8_t)sizeof(buf));
292 if (ret != K_SPP_OK)
293 {
294 return ret;
295 }
296
297 SPP_LOGI(K_BMP390_TAG, "ID: 0x%02X", buf[8]);
298
299 if (buf[8] != 0x60U)
300 {
301 SPP_LOGE(K_BMP390_TAG, "BMP390 not detected! Expected 0x60, got 0x%02X", buf[8]);
302 return K_SPP_ERROR;
303 }
304
305 return ret;
306}
307
308/**
309 * @brief Resets the sensor, enables SPI mode, and verifies the chip ID.
310 * @param p_spiHandler SPI device handle.
311 * @return K_SPP_OK on success, K_SPP_ERROR if any step fails.
312 */
314{
315 SPP_RetVal_t ret;
316
317 ret = SPP_SERVICES_BMP390_softReset(p_spiHandler);
318 if (ret != K_SPP_OK)
319 {
320 return ret;
321 }
322
323 ret = SPP_SERVICES_BMP390_enableSpiMode(p_spiHandler);
324 if (ret != K_SPP_OK)
325 {
326 return ret;
327 }
328
329 ret = SPP_SERVICES_BMP390_configCheck(p_spiHandler);
330 if (ret != K_SPP_OK)
331 {
332 return ret;
333 }
334
335 return ret;
336}
337
338/**
339 * @brief Writes OSR, ODR, IIR and power-control registers to start continuous measurements.
340 * @param p_spiHandler SPI device handle.
341 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
342 */
352
353/**
354 * @brief Polls the DRDY flag until set or until the timeout elapses.
355 * @param p_bmp Pointer to the driver context containing the ISR flag.
356 * @param timeout_ms Maximum time to wait in milliseconds.
357 * @return K_SPP_OK when data is ready, K_SPP_ERROR on timeout.
358 */
359
360/* ----------------------------------------------------------------
361 * Driver — temperature
362 * ---------------------------------------------------------------- */
363
364/**
365 * @brief Reads raw temperature calibration coefficients from the sensor's NVM registers.
366 * @param p_spiHandler SPI device handle.
367 * @param tcalib Output struct populated with raw par_t1/t2/t3 values.
368 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
369 */
371{
372 spp_uint8_t buf[15] = {
378
379 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
380 if (ret != K_SPP_OK)
381 {
382 return ret;
383 }
384
385 spp_uint8_t raw[5];
386 raw[0] = buf[2];
387 raw[1] = buf[5];
388 raw[2] = buf[8];
389 raw[3] = buf[11];
390 raw[4] = buf[14];
391
392 tcalib->par_t1 = (uint16_t)((raw[1] << 8) | raw[0]);
393 tcalib->par_t2 = (int16_t)((raw[3] << 8) | raw[2]);
394 tcalib->par_t3 = (int8_t)raw[4];
395
396 return ret;
397}
398
399/**
400 * @brief Reads raw temperature coefficients and scales them to floating-point parameters.
401 * @param p_spiHandler SPI device handle.
402 * @param out Output struct populated with scaled PAR_T1/T2/T3.
403 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
404 */
406{
408
409 SPP_RetVal_t ret = SPP_SERVICES_BMP390_readRawTempCoeffs(p_spiHandler, &raw);
410 if (ret != K_SPP_OK)
411 {
412 return ret;
413 }
414
415 out->PAR_T1 = raw.par_t1 * 256.0f; /* 2^8 */
416 out->PAR_T2 = raw.par_t2 / 1073741824.0f; /* 2^30 */
417 out->PAR_T3 = raw.par_t3 / 281474976710656.0f; /* 2^48 */
418
419 return ret;
420}
421
422/**
423 * @brief Reads the 24-bit raw temperature ADC value from the sensor.
424 * @param p_spiHandler SPI device handle.
425 * @param raw_temp Output: raw 24-bit ADC reading.
426 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
427 */
428static SPP_RetVal_t SPP_SERVICES_BMP390_readRawTemp(void *p_spiHandler, uint32_t *raw_temp)
429{
430 spp_uint8_t buf[9] = {
434
435 // spp_uint32_t t0 = SPP_HAL_TIME_getTimeUs();
436 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
437 // spp_uint32_t t1 = SPP_HAL_TIME_getTimeUs();
438
439 // s_lastTempSpi = t1 - t0;
440
441 if (ret != K_SPP_OK)
442 {
443 return ret;
444 }
445
446 spp_uint8_t xlsb = buf[2];
447 spp_uint8_t lsb = buf[5];
448 spp_uint8_t msb = buf[8];
449
450 *raw_temp = ((spp_uint32_t)msb << 16) | ((spp_uint32_t)lsb << 8) | (spp_uint32_t)xlsb;
451
452 return ret;
453}
454
455/**
456 * @brief Applies the BMP390 double-quadratic temperature compensation formula.
457 * @param raw_temp Raw 24-bit ADC reading from the sensor.
458 * @param params Scaled calibration parameters (PAR_T1/T2/T3).
459 * @return Compensated temperature in degrees Celsius (t_lin).
460 */
462{
463 float partial1 = (float)raw_temp - params->PAR_T1;
464 float partial2 = partial1 * params->PAR_T2;
465 float t_lin = partial2 + (partial1 * partial1) * params->PAR_T3;
466
467 return t_lin;
468}
469
470/**
471 * @brief Reads the raw temperature ADC and returns the compensated value.
472 * @param p_spiHandler SPI device handle.
473 * @param temp_params Scaled calibration parameters.
474 * @param raw_temp Output: raw ADC reading.
475 * @param comp_temp Output: compensated temperature in °C.
476 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
477 */
478static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetTemp(void *p_spiHandler, const BMP390_temp_params_t *temp_params,
479 spp_uint32_t *raw_temp, float *comp_temp)
480{
481 SPP_RetVal_t ret = SPP_SERVICES_BMP390_readRawTemp(p_spiHandler, raw_temp);
482 if (ret != K_SPP_OK)
483 {
484 return ret;
485 }
486
487 *comp_temp = SPP_SERVICES_BMP390_compensateTemperature(*raw_temp, (BMP390_temp_params_t *)temp_params);
488
489 return ret;
490}
491
492/* ----------------------------------------------------------------
493 * Driver — pressure
494 * ---------------------------------------------------------------- */
495
496/**
497 * @brief Reads raw pressure calibration coefficients from the sensor's NVM registers.
498 * @param p_spiHandler SPI device handle.
499 * @param pcalib Output struct populated with raw par_p1..p11 values.
500 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
501 */
503{
552
553 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
554 if (ret != K_SPP_OK)
555 {
556 return ret;
557 }
558
559 spp_uint8_t raw[16];
560 for (int i = 0; i < 16; i++)
561 {
562 raw[i] = buf[3 * i + 2];
563 }
564
565 pcalib->par_p1 = (spp_uint16_t)((raw[1] << 8) | raw[0]);
566 pcalib->par_p2 = (spp_uint16_t)((raw[3] << 8) | raw[2]);
567 pcalib->par_p3 = (spp_int8_t)raw[4];
568 pcalib->par_p4 = (spp_int8_t)raw[5];
569 pcalib->par_p5 = (spp_uint16_t)((raw[7] << 8) | raw[6]);
570 pcalib->par_p6 = (spp_uint16_t)((raw[9] << 8) | raw[8]);
571 pcalib->par_p7 = (spp_int8_t)raw[10];
572 pcalib->par_p8 = (spp_int8_t)raw[11];
573 pcalib->par_p9 = (spp_int16_t)((raw[13] << 8) | raw[12]);
574 pcalib->par_p10 = (spp_int8_t)raw[14];
575 pcalib->par_p11 = (spp_int8_t)raw[15];
576
577 return ret;
578}
579
580/**
581 * @brief Reads raw pressure coefficients and scales them to floating-point parameters.
582 * @param p_spiHandler SPI device handle.
583 * @param out Output struct populated with scaled PAR_P1..P11.
584 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
585 */
587{
589
591 if (ret != K_SPP_OK)
592 {
593 return ret;
594 }
595
596 out->PAR_P1 = (raw.par_p1 - 16384.0f) / 1048576.0f; /* (p1 - 2^14) / 2^20 */
597 out->PAR_P2 = (raw.par_p2 - 16384.0f) / 536870912.0f; /* (p2 - 2^14) / 2^29 */
598 out->PAR_P3 = raw.par_p3 / 4294967296.0f; /* / 2^32 */
599 out->PAR_P4 = raw.par_p4 / 137438953472.0f; /* / 2^37 */
600 out->PAR_P5 = raw.par_p5 * 8.0f; /* / 2^-3 */
601 out->PAR_P6 = raw.par_p6 / 64.0f; /* / 2^6 */
602 out->PAR_P7 = raw.par_p7 / 256.0f; /* / 2^8 */
603 out->PAR_P8 = raw.par_p8 / 32768.0f; /* / 2^15 */
604 out->PAR_P9 = raw.par_p9 / 281474976710656.0f; /* / 2^48 */
605 out->PAR_P10 = raw.par_p10 / 281474976710656.0f; /* / 2^48 */
606 out->PAR_P11 = raw.par_p11 / 36893488147419103232.0f; /* / 2^65 */
607
608 return ret;
609}
610
611/**
612 * @brief Reads the 24-bit raw pressure ADC value from the sensor.
613 * @param p_spiHandler SPI device handle.
614 * @param raw_press Output: raw 24-bit ADC reading.
615 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
616 */
617static SPP_RetVal_t SPP_SERVICES_BMP390_readRawPress(void *p_spiHandler, spp_uint32_t *raw_press)
618{
619 spp_uint8_t buf[9] = {
623
624 // spp_uint32_t t0 = SPP_HAL_TIME_getTimeUs();
625 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
626 // spp_uint32_t t1 = SPP_HAL_TIME_getTimeUs();
627
628 // s_lastPressSpi = t1 - t0;
629
630 if (ret != K_SPP_OK)
631 {
632 return ret;
633 }
634
635 spp_uint8_t xlsb = buf[2];
636 spp_uint8_t lsb = buf[5];
637 spp_uint8_t msb = buf[8];
638
639 *raw_press = ((spp_uint32_t)msb << 16) | ((spp_uint32_t)lsb << 8) | (spp_uint32_t)xlsb;
640
641 return ret;
642}
643
644/**
645 * @brief Applies the BMP390 pressure compensation formula using the linearised temperature.
646 * @param raw_press Raw 24-bit ADC pressure reading.
647 * @param t_lin Linearised (compensated) temperature in °C.
648 * @param p Scaled pressure calibration parameters.
649 * @return Compensated pressure in Pa.
650 */
652{
653 s_pd1 = p->PAR_P6 * t_lin;
654 s_pd2 = p->PAR_P7 * (t_lin * t_lin);
655 s_pd3 = p->PAR_P8 * (t_lin * t_lin * t_lin);
656 s_po1 = p->PAR_P5 + s_pd1 + s_pd2 + s_pd3;
657
658 s_pd1 = p->PAR_P2 * t_lin;
659 s_pd2 = p->PAR_P3 * (t_lin * t_lin);
660 s_pd3 = p->PAR_P4 * (t_lin * t_lin * t_lin);
661 s_po2 = raw_press * (p->PAR_P1 + s_pd1 + s_pd2 + s_pd3);
662
663 s_pd1 = raw_press * raw_press;
664 s_pd2 = p->PAR_P9 + p->PAR_P10 * t_lin;
665 s_pd3 = s_pd1 * s_pd2;
666 s_pd4 = s_pd3 + (raw_press * raw_press * raw_press) * p->PAR_P11;
667
669
670 return s_compPress;
671}
672
673/**
674 * @brief Reads raw pressure ADC and returns the compensated value in Pa.
675 * @param p_spiHandler SPI device handle.
676 * @param press_params Scaled pressure calibration parameters.
677 * @param t_lin Linearised temperature used for pressure compensation.
678 * @param raw_press Output: raw 24-bit ADC reading.
679 * @param comp_press Output: compensated pressure in Pa.
680 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
681 */
682static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetPress(void *p_spiHandler, const BMP390_press_params_t *press_params,
683 float t_lin, spp_uint32_t *raw_press, float *comp_press)
684{
685 SPP_RetVal_t ret = SPP_SERVICES_BMP390_readRawPress(p_spiHandler, raw_press);
686 if (ret != K_SPP_OK)
687 {
688 return ret;
689 }
690
691 *comp_press = SPP_SERVICES_BMP390_compensatePressure(*raw_press, t_lin, (BMP390_press_params_t *)press_params);
692
693 return ret;
694}
695
696/* ----------------------------------------------------------------
697 * Driver — altitude
698 * ---------------------------------------------------------------- */
699
700/**
701 * @brief Reads compensated altitude, pressure and temperature from the sensor.
702 * @param p_spiHandler SPI device handle.
703 * @param p_bmp Driver context (unused; DRDY is handled by the caller).
704 * @param altitude_m Output: altitude in metres (barometric formula, ref 1013.25 hPa).
705 * @param pressure_pa Output: compensated pressure in Pa.
706 * @param temperature_c Output: compensated temperature in °C.
707 * @return K_SPP_OK on success, K_SPP_ERROR_NULL_POINTER if any pointer is NULL,
708 * K_SPP_ERROR on SPI failure.
709 */
710static SPP_RetVal_t SPP_SERVICES_BMP390_getAltitude(void *p_spiHandler, float *altitude_m, float *pressure_pa,
711 float *temperature_c)
712{
713 if ((p_spiHandler == NULL) || (altitude_m == NULL) || (pressure_pa == NULL) || (temperature_c == NULL))
714 {
716 }
717
718 static spp_bool_t s_inited = false;
719 static BMP390_temp_params_t s_tempParams;
720 static BMP390_press_params_t s_pressParams;
721 static spp_uint32_t s_rawTemp;
722 static spp_uint32_t s_rawPress;
723
724 float t_lin = 0.0f;
725 float compPress = 0.0f;
726
727 if (s_inited == false)
728 {
729 SPP_RetVal_t ret = SPP_SERVICES_BMP390_calibrateTempParams(p_spiHandler, &s_tempParams);
730 if (ret != K_SPP_OK)
731 {
732 return ret;
733 }
734
735 ret = SPP_SERVICES_BMP390_calibratePressParams(p_spiHandler, &s_pressParams);
736 if (ret != K_SPP_OK)
737 {
738 return ret;
739 }
740
741 s_inited = true;
742 }
743
744 SPP_RetVal_t ret = SPP_SERVICES_BMP390_auxGetTemp(p_spiHandler, &s_tempParams, &s_rawTemp, &t_lin);
745 if (ret != K_SPP_OK)
746 {
747 return ret;
748 }
749
750 ret = SPP_SERVICES_BMP390_auxGetPress(p_spiHandler, &s_pressParams, t_lin, &s_rawPress, &compPress);
751 if (ret != K_SPP_OK)
752 {
753 return ret;
754 }
755
756 *temperature_c = t_lin;
757 *pressure_pa = compPress;
758 *altitude_m = 44330.0f * (1.0f - powf(compPress / 101325.0f, 1.0f / 5.255f));
759
760 return K_SPP_OK;
761}
762
763/* ----------------------------------------------------------------
764 * Driver — interrupt
765 * ---------------------------------------------------------------- */
766
767/**
768 * @brief Enables the active-high data-ready interrupt output on the sensor.
769 * @param p_spiHandler SPI device handle.
770 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
771 */
773{
775
776 return SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
777}
static SPP_RetVal_t SPP_SERVICES_BMP390_readRawPress(void *p_spiHandler, spp_uint32_t *raw_press)
Reads the 24-bit raw pressure ADC value from the sensor.
Definition bmp390.c:617
static SPP_RetVal_t SPP_SERVICES_BMP390_readRawTemp(void *p_spiHandler, uint32_t *raw_temp)
Reads the 24-bit raw temperature ADC value from the sensor.
Definition bmp390.c:428
static SPP_RetVal_t SPP_SERVICES_BMP390_configCheck(void *p_spiHandler)
Reads the chip-ID register and returns an error if the value is not 0x60.
Definition bmp390.c:284
static SPP_RetVal_t SPP_SERVICES_BMP390_auxConfig(void *p_spiHandler)
Resets the sensor, enables SPI mode, and verifies the chip ID.
Definition bmp390.c:313
static const SPP_SERVICE_ProducerContract_t g_bmp390ProducerContract
Definition bmp390.c:74
static SPP_RetVal_t SPP_SERVICES_BMP390_readRawPressCoeffs(void *p_spiHandler, BMP390_press_calib_t *pcalib)
Reads raw pressure calibration coefficients from the sensor's NVM registers.
Definition bmp390.c:502
static SPP_RetVal_t SPP_SERVICES_BMP390_enableSpiMode(void *p_spiHandler)
Writes the interface-config register to lock the sensor into SPI mode.
Definition bmp390.c:271
static float SPP_SERVICES_BMP390_compensatePressure(spp_uint32_t raw_press, float t_lin, BMP390_press_params_t *p)
Applies the BMP390 pressure compensation formula using the linearised temperature.
Definition bmp390.c:651
static float s_pd3
Definition bmp390.c:80
static float s_pd2
Definition bmp390.c:80
#define K_BMP390_TAG
Definition bmp390.c:30
static const char *const k_svcTag
Definition bmp390.c:79
#define K_BMP390_TASK_TIMEOUT_MS
Definition bmp390.c:29
static float s_po1
Definition bmp390.c:81
static SPP_RetVal_t SPP_SERVICES_BMP390_readRawTempCoeffs(void *p_spiHandler, BMP390_temp_calib_t *tcalib)
Polls the DRDY flag until set or until the timeout elapses.
Definition bmp390.c:370
static float s_po2
Definition bmp390.c:81
static float SPP_SERVICES_BMP390_compensateTemperature(spp_uint32_t raw_temp, BMP390_temp_params_t *params)
Applies the BMP390 double-quadratic temperature compensation formula.
Definition bmp390.c:461
static float s_compPress
Definition bmp390.c:82
static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetTemp(void *p_spiHandler, const BMP390_temp_params_t *temp_params, spp_uint32_t *raw_temp, float *comp_temp)
Reads the raw temperature ADC and returns the compensated value.
Definition bmp390.c:478
static SPP_RetVal_t SPP_SERVICES_BMP390_init(void)
BMP390 sensor instance initialisation. This function is called by the service task....
Definition bmp390.c:122
static float s_pd4
Definition bmp390.c:80
const SPP_SERVICE_ProducerContract_t * SPP_SERVICES_BMP390_getProducerContract(void)
Definition bmp390.c:87
static SPP_RetVal_t SPP_SERVICES_BMP390_calibrateTempParams(void *p_spiHandler, BMP390_temp_params_t *out)
Reads raw temperature coefficients and scales them to floating-point parameters.
Definition bmp390.c:405
static SPP_RetVal_t SPP_SERVICES_BMP390_calibratePressParams(void *p_spiHandler, BMP390_press_params_t *out)
Reads raw pressure coefficients and scales them to floating-point parameters.
Definition bmp390.c:586
static BMP390_t s_bmpData
Definition bmp390.c:78
static SPP_RetVal_t SPP_SERVICES_BMP390_prepareMeasure(void *p_spiHandler)
Writes OSR, ODR, IIR and power-control registers to start continuous measurements.
Definition bmp390.c:343
static SPP_RetVal_t SPP_SERVICES_BMP390_acquireData(SPP_Kpid_t kpid)
Acquisition callback invoked by the SPP service task on each cycle.
Definition bmp390.c:174
static SPP_RetVal_t SPP_SERVICES_BMP390_intEnableDrdy(void *p_spiHandler)
Enables the active-high data-ready interrupt output on the sensor.
Definition bmp390.c:772
static SPP_RetVal_t SPP_SERVICES_BMP390_softReset(void *p_spiHandler)
Sends a soft-reset command to the sensor and waits 100 ms for it to reboot.
Definition bmp390.c:258
static float s_pd1
Definition bmp390.c:80
static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetPress(void *p_spiHandler, const BMP390_press_params_t *press_params, float t_lin, spp_uint32_t *raw_press, float *comp_press)
Reads raw pressure ADC and returns the compensated value in Pa.
Definition bmp390.c:682
static SPP_RetVal_t SPP_SERVICES_BMP390_getAltitude(void *p_spiHandler, float *altitude_m, float *pressure_pa, float *temperature_c)
Reads compensated altitude, pressure and temperature from the sensor.
Definition bmp390.c:710
BMP390 barometric pressure sensor driver and service.
#define K_BMP390_TEMP_RAW_REG
Definition bmp390.h:75
#define K_BMP390_PRESS_CALIB_REG_START
Definition bmp390.h:79
#define K_BMP390_IF_CONF_REG
Definition bmp390.h:55
#define K_BMP390_INT_PIN_NUM
Definition bmp390.h:86
#define BMP390_VALUE_OSR
Definition bmp390.h:62
#define BMP390_VALUE_PWRCTRL
Definition bmp390.h:60
#define K_BMP390_SPI_READ
Definition bmp390.h:45
#define BMP390_VALUE_IIR
Definition bmp390.h:66
#define K_BMP390_SPI_BUS_IDX
Definition bmp390.h:48
#define K_BMP390_REG_OSR
Definition bmp390.h:61
#define BMP390_SOFT_RESET_CMD
Definition bmp390.h:54
#define K_BMP390_INT_CTRL_DRDY_EN
Definition bmp390.h:84
#define K_BMP390_SPI_WRITE
Definition bmp390.h:46
#define K_BMP390_INT_CTRL_LEVEL
Definition bmp390.h:85
#define K_BMP390_CHIP_ID_REG
Definition bmp390.h:51
#define K_BMP390_TEMP_CALIB_REG_START
Definition bmp390.h:74
#define K_BMP390_REG_INT_CTRL
Definition bmp390.h:83
#define K_BMP390_SOFT_RESET_REG
Definition bmp390.h:53
#define BMP390_VALUE_ODR
Definition bmp390.h:64
#define K_BMP390_REG_ODR
Definition bmp390.h:63
#define K_BMP390_REG_IIR
Definition bmp390.h:65
#define K_BMP390_PRESS_RAW_REG
Definition bmp390.h:80
#define BMP390_IF_CONF_SPI
Definition bmp390.h:56
#define K_BMP390_REG_PWRCTRL
Definition bmp390.h:59
#define K_BMP390_INT_INTR_TYPE
Definition bmp390.h:87
#define K_BMP390_INT_PULL
Definition bmp390.h:88
SPP_Packet_t * SPP_SERVICES_DATABANK_getPacket(void)
Acquire a free packet from the pool.
Definition databank.c:53
SPP_RetVal_t SPP_SERVICES_DATABANK_returnPacket(SPP_Packet_t *p_packet)
Return a packet to the pool after processing.
Definition databank.c:64
SPP_RetVal_t SPP_SERVICES_DATABANK_packetData(SPP_Packet_t *p_packet, spp_uint16_t apid, spp_uint16_t seq, const void *p_data, spp_uint16_t dataLen)
Fill a packet with data and compute its CRC.
Definition databank.c:117
Static packet pool manager.
Finite State Machine (FSM) module for the SPP core.
SPP_RetVal_t SPP_HAL_GPIO_registerIsr(spp_uint32_t pin, void *p_isrCtx)
Register an ISR handler for a GPIO pin.
Definition gpio.c:39
SPP_RetVal_t SPP_HAL_GPIO_configInterrupt(spp_uint32_t pin, spp_uint32_t intrType, spp_uint32_t pull)
Configure a GPIO pin as an interrupt input.
Definition gpio.c:19
SPP logging service.
#define SPP_LOGI(tag, fmt,...)
Definition log.h:69
#define SPP_LOGE(tag, fmt,...)
Definition log.h:67
#define SPP_LOGW(tag, fmt,...)
Definition log.h:68
Solaris Packet Protocol (SPP) packet structure definitions.
SPP_Packet_t
Definition packet.h:79
SPP_RetVal_t SPP_SERVICES_PUBSUB_publish(SPP_Packet_t *p_pkt)
Copies a packet into the internal buffer and returns it to the databank.
Definition pubsub.c:128
Publish-subscribe packet router.
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition returnTypes.h:24
@ K_SPP_OK
Definition returnTypes.h:25
@ K_SPP_ERROR
Definition returnTypes.h:26
@ K_SPP_ERROR_NULL_POINTER
Definition returnTypes.h:30
void * SPP_HAL_SPI_getHandle(spp_uint8_t deviceIdx)
Obtain the SPI device handle for the given device index.
Definition spi.c:39
SPP_RetVal_t SPP_HAL_SPI_deviceInit(void *p_handle)
Initialise a specific SPI device.
Definition spi.c:49
SPP_RetVal_t SPP_HAL_SPI_transmit(void *p_handle, spp_uint8_t *p_data, spp_uint8_t length)
Perform a full-duplex SPI transaction.
Definition spi.c:79
SPP SPI HAL API — dispatches through the registered HAL port.
SPP_GpioIsrCtx_t isrCtx
Definition bmp390.h:159
spp_uint32_t intIntrType
Definition bmp390.h:156
spp_uint32_t intPull
Definition bmp390.h:157
volatile spp_bool_t drdyFlag
Definition bmp390.h:158
spp_uint32_t intPin
Definition bmp390.h:155
spp_uint8_t spiDevIdx
Definition bmp390.h:150
void * p_spiHandler
Definition bmp390.h:151
Raw pressure calibration coefficients.
Definition bmp390.h:114
spp_int8_t par_p10
Definition bmp390.h:124
spp_uint16_t par_p5
Definition bmp390.h:119
spp_int8_t par_p7
Definition bmp390.h:121
spp_int8_t par_p4
Definition bmp390.h:118
spp_int8_t par_p8
Definition bmp390.h:122
spp_int16_t par_p9
Definition bmp390.h:123
spp_uint16_t par_p2
Definition bmp390.h:116
spp_uint16_t par_p6
Definition bmp390.h:120
spp_int8_t par_p3
Definition bmp390.h:117
spp_int8_t par_p11
Definition bmp390.h:125
spp_uint16_t par_p1
Definition bmp390.h:115
Scaled pressure calibration parameters.
Definition bmp390.h:130
BMP390 sensor instance.
Definition bmp390.h:169
BMP390_SpiConfig_t spiConfig
Definition bmp390.h:171
BMP390_GpioConfig_t gpioConfig
Definition bmp390.h:170
spp_uint16_t seq
Definition bmp390.h:172
Raw temperature calibration coefficients (as read from sensor).
Definition bmp390.h:97
uint16_t par_t1
Definition bmp390.h:98
Scaled temperature calibration parameters.
Definition bmp390.h:106
volatile spp_bool_t * p_flag
Definition gpio.h:28
Describes the contract of a producer module. This module needs to have:
Definition service.h:28
void SPP_HAL_TIME_delayMs(spp_uint32_t ms)
Block for the requested number of milliseconds.
Definition time.c:37
SPP HAL time API — dispatches through the registered HAL port.
uint16_t spp_uint16_t
Definition types.h:29
uint32_t spp_uint32_t
Definition types.h:31
int8_t spp_int8_t
Definition types.h:28
uint8_t spp_uint8_t
Definition types.h:27
bool spp_bool_t
Definition types.h:36
float spp_float32_t
Definition types.h:35
int16_t spp_int16_t
Definition types.h:30
spp_uint16_t value
Definition kpid.h:25