Solaris Software
Loading...
Searching...
No Matches
bmp390.c
Go to the documentation of this file.
1/**
2 * @file bmp390.c
3 * @brief BMP390 pressure sensor driver and SPP producer implementation.
4 *
5 * Handles sensor configuration, SPI acquisition, compensation,
6 * reference calibration and packet publication through PUBSUB.
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 <stdio.h>
20
21#include <string.h>
22#include <math.h>
23#ifdef SPP_DEBUG_PRINT
24#include <stdio.h>
25#endif
26
27
28/* ----------------------------------------------------------------
29 * CONSTANTS
30 * ---------------------------------------------------------------- */
31#define K_BMP390_TASK_TIMEOUT_MS 5000U
32#define K_BMP390_TAG "BMP390"
33
34/* ----------------------------------------------------------------
35 * STATIC FUNCTIONS DECLARATIONS
36 * ---------------------------------------------------------------- */
39static SPP_RetVal_t SPP_SERVICES_BMP390_softReset(void *p_spiHandler);
40static SPP_RetVal_t SPP_SERVICES_BMP390_enableSpiMode(void *p_spiHandler);
41static SPP_RetVal_t SPP_SERVICES_BMP390_configCheck(void *p_spiHandler);
42static SPP_RetVal_t SPP_SERVICES_BMP390_auxConfig(void *p_spiHandler);
43static SPP_RetVal_t SPP_SERVICES_BMP390_prepareMeasure(void *p_spiHandler);
46static SPP_RetVal_t SPP_SERVICES_BMP390_readRawTemp(void *p_spiHandler, uint32_t *raw_temp);
48static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetTemp(void *p_spiHandler, const BMP390_temp_params_t *temp_params,
49 spp_uint32_t *raw_temp, float *comp_temp);
52static SPP_RetVal_t SPP_SERVICES_BMP390_readRawPress(void *p_spiHandler, spp_uint32_t *raw_press);
54static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetPress(void *p_spiHandler, const BMP390_press_params_t *press_params,
55 float t_lin, spp_uint32_t *raw_press, float *comp_press);
56static SPP_RetVal_t SPP_SERVICES_BMP390_getAltitude(void *p_spiHandler, float *altitude_m, float *pressure_pa,
57 float *temperature_c);
58static SPP_RetVal_t SPP_SERVICES_BMP390_intEnableDrdy(void *p_spiHandler);
59
60/* Performance testing*/
61#define K_BMP390_PERFORMANCE_SAMPLES (10000U)
62
63// static spp_uint32_t s_bmpPerformance[K_BMP390_PERFORMANCE_SAMPLES] = {0U};
64// static spp_uint32_t s_tempSpiPerformance[K_BMP390_PERFORMANCE_SAMPLES] = {0U};
65// static spp_uint32_t s_pressSpiPerformance[K_BMP390_PERFORMANCE_SAMPLES] = {0U};
66
67// static spp_uint32_t s_lastTempSpi = 0U;
68// static spp_uint32_t s_lastPressSpi = 0U;
69
70// static spp_uint16_t s_performanceSamples = 0U;
71// static spp_bool_t s_performanceWarmupDone = false;
72
73/* ----------------------------------------------------------------
74* VARIABLES
75* ---------------------------------------------------------------- */
76static const SPP_SERVICE_ProducerContract_t g_bmp390ProducerContract = {.p_nameProducer = "bmp390",
77 .tiemoutMs = K_BMP390_TASK_TIMEOUT_MS,
79 .acquireData = SPP_SERVICES_BMP390_acquireData};
81static const char *const k_svcTag = "BMP_SVC";
82static float s_pd1, s_pd2, s_pd3, s_pd4;
83static float s_po1, s_po2;
84static float s_compPress;
85
86/* Calibration */
87#define K_BMP390_CALIBRATION_SAMPLES 500U
90
93
96
97
98/* ----------------------------------------------------------------
99* PUBLIC FUNCTIONS
100* ---------------------------------------------------------------- */
105/* Performance Test */
106// spp_uint16_t SPP_SERVICES_BMP390_getPerformanceSampleCount(void)
107// {
108// return s_performanceSamples;
109// }
110
111
112// SPP_RetVal_t SPP_SERVICES_BMP390_getPerformanceSample(spp_uint16_t i, spp_uint32_t *p_bmp, spp_uint32_t *p_tempSpi,
113// spp_uint32_t *p_pressSpi)
114// {
115// if ((i >= s_performanceSamples) || (p_bmp == NULL) || (p_tempSpi == NULL) || (p_pressSpi == NULL))
116// {
117// return K_SPP_ERROR;
118// }
119
120// *p_bmp = s_bmpPerformance[i];
121// *p_tempSpi = s_tempSpiPerformance[i];
122// *p_pressSpi = s_pressSpiPerformance[i];
123
124// return K_SPP_OK;
125// }
126
127/* ----------------------------------------------------------------
128* STATIC FUNCTIONS
129* ---------------------------------------------------------------- */
130
131/**
132 * @brief Initializes the BMP390 sensor and its hardware interfaces.
133 *
134 * Configures the GPIO interrupt, initializes the SPI device,
135 * configures the sensor and enables data-ready interrupts.
136 *
137 * @return K_SPP_OK on success, error code otherwise.
138 */
140{
142
147
148 // Configure GPIO interrupt before SPI device init
151
152 if (ret == K_SPP_OK)
153 {
156 }
157
158 // Get the SPI bus handler and init the SPI device
159 if (ret == K_SPP_OK)
160 {
162 if (s_bmpData.spiConfig.p_spiHandler == NULL)
163 {
164 ret = K_SPP_ERROR;
165 }
166 }
167
168 if (ret == K_SPP_OK)
169 {
171 }
172
173 if (ret == K_SPP_OK)
174 {
176 }
177
178 if (ret == K_SPP_OK)
179 {
181 }
182
183 if (ret == K_SPP_OK)
184 {
186 }
187
188 return ret;
189}
190
191/**
192 * @brief Acquires BMP390 measurements and publishes them through PUBSUB.
193 *
194 * Checks the DRDY flag, reads compensated measurements and performs
195 * reference calibration using the first 500 samples.
196 * After calibration, publishes altitude, pressure and temperature.
197 *
198 * @param kpid Assigned producer KPID.
199 * @return K_SPP_OK on normal execution, error code on acquisition
200 * or packet preparation failure.
201 */
203{
204 BMP390_t *p_bmpData = &s_bmpData;
205 float altitude = 0.0f;
206 float pressure = 0.0f;
207 float temperature = 0.0f;
208
209 if (!p_bmpData->gpioConfig.drdyFlag)
210 {
211 return K_SPP_OK;
212 }
213
214 p_bmpData->gpioConfig.drdyFlag = false;
215
216 // spp_uint32_t t0 = SPP_HAL_TIME_getTimeUs();
217
218 SPP_RetVal_t ret =
219 SPP_SERVICES_BMP390_getAltitude(p_bmpData->spiConfig.p_spiHandler, &altitude, &pressure, &temperature);
220
221 // spp_uint32_t t1 = SPP_HAL_TIME_getTimeUs();
222
223 if (ret != K_SPP_OK)
224 {
225 SPP_LOGE(k_svcTag, "getAltitude failed ret=%d", (int)ret);
226 return ret;
227 }
228
229 if (s_isCalibrated == false)
230 {
231 s_pressureSum += pressure;
232 s_temperatureSum += temperature;
234
235 //printf("CAL [%u/%u] altitude=%.2f m\n", s_calibrationSamples, K_BMP390_CALIBRATION_SAMPLES, altitude);
236
238 {
240
242
243 s_isCalibrated = true;
244
245 printf("CAL DONE P0=%.2f Pa T0=%.2f C\n", s_referencePressure, s_referenceTemperature);
246 }
247
248 return K_SPP_OK; // Do not packet calibration samples
249 }
250
252 if (p_packet == NULL)
253 {
254 SPP_LOGW(k_svcTag, "No free packet");
255 return K_SPP_ERROR;
256 }
257 // if (s_performanceWarmupDone == false)
258 // {
259 // s_performanceWarmupDone = true;
260 // }
261 // else if (s_performanceSamples < K_BMP390_PERFORMANCE_SAMPLES)
262 // {
263 // s_bmpPerformance[s_performanceSamples] = t1 - t0;
264 // s_tempSpiPerformance[s_performanceSamples] = s_lastTempSpi;
265 // s_pressSpiPerformance[s_performanceSamples] = s_lastPressSpi;
266
267 // s_performanceSamples++;
268 // }
269
270#ifdef SPP_DEBUG_PRINT
271 SPP_LOGI(K_BMP390_TAG, "[BMP] alt=%.1fm P=%.1fhPa T=%.2fC\n", altitude, pressure / 100.0f, temperature);
272#endif
273
274
275 spp_float32_t payload[3] = {altitude, pressure, temperature};
276 ret = SPP_SERVICES_DATABANK_packetData(p_packet, kpid.value, p_bmpData->seq++, payload,
277 (spp_uint16_t)sizeof(payload));
278
279 // for (int i = 0; i < 3; i++)
280 // {
281 // spp_uint32_t bits;
282 // memcpy(&bits, &payload[i], sizeof(bits)); // reinterpreta los bits sin convertir el valor
283 // printf("payload[%d] = 0x%08lX\n", i, bits);
284 // }
285 if (ret != K_SPP_OK)
286 {
287 SPP_LOGE(k_svcTag, "packetData failed ret=%d", (int)ret);
289 return ret;
290 }
291
292 // Publish the packet
293 (void)SPP_SERVICES_PUBSUB_publish(p_packet);
294 return K_SPP_OK;
295}
296
297
298/* ----------------------------------------------------------------
299 * Driver — configuration helpers
300 * ---------------------------------------------------------------- */
301
302/**
303 * @brief Sends a soft-reset command to the sensor and waits 100 ms for it to reboot.
304 * @param p_spiHandler SPI device handle.
305 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
306 */
308{
310 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
312 return ret;
313}
314
315/**
316 * @brief Writes the interface-config register to lock the sensor into SPI mode.
317 * @param p_spiHandler SPI device handle.
318 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
319 */
321{
323 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, (spp_uint8_t)sizeof(buf));
325 return ret;
326}
327
328/**
329 * @brief Reads the chip-ID register and returns an error if the value is not 0x60.
330 * @param p_spiHandler SPI device handle.
331 * @return K_SPP_OK if chip ID matches, K_SPP_ERROR otherwise.
332 */
334{
335 spp_uint8_t buf[9] = {
339
340 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, (spp_uint8_t)sizeof(buf));
341 if (ret != K_SPP_OK)
342 {
343 return ret;
344 }
345
346 SPP_LOGI(K_BMP390_TAG, "ID: 0x%02X", buf[8]);
347
348 printf("BMP390 ID=0x%02X\n", buf[8]);
349 if (buf[8] != 0x60U)
350 {
351 SPP_LOGE(K_BMP390_TAG, "BMP390 not detected! Expected 0x60, got 0x%02X", buf[8]);
352 return K_SPP_ERROR;
353 }
354
355 return ret;
356}
357
358/**
359 * @brief Resets the sensor, enables SPI mode, and verifies the chip ID.
360 * @param p_spiHandler SPI device handle.
361 * @return K_SPP_OK on success, K_SPP_ERROR if any step fails.
362 */
364{
365 SPP_RetVal_t ret;
366
367 ret = SPP_SERVICES_BMP390_softReset(p_spiHandler);
368 if (ret != K_SPP_OK)
369 {
370 return ret;
371 }
372
373 ret = SPP_SERVICES_BMP390_enableSpiMode(p_spiHandler);
374 if (ret != K_SPP_OK)
375 {
376 return ret;
377 }
378
379 ret = SPP_SERVICES_BMP390_configCheck(p_spiHandler);
380 if (ret != K_SPP_OK)
381 {
382 return ret;
383 }
384
385 return ret;
386}
387
388/**
389 * @brief Writes OSR, ODR, IIR and power-control registers to start continuous measurements.
390 * @param p_spiHandler SPI device handle.
391 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
392 */
402
403/* ----------------------------------------------------------------
404 * Driver — temperature
405 * ---------------------------------------------------------------- */
406
407/**
408 * @brief Reads raw temperature calibration coefficients from the sensor's NVM registers.
409 * @param p_spiHandler SPI device handle.
410 * @param tcalib Output struct populated with raw par_t1/t2/t3 values.
411 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
412 */
414{
415 spp_uint8_t buf[15] = {
421
422 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
423 if (ret != K_SPP_OK)
424 {
425 return ret;
426 }
427
428 spp_uint8_t raw[5];
429 raw[0] = buf[2];
430 raw[1] = buf[5];
431 raw[2] = buf[8];
432 raw[3] = buf[11];
433 raw[4] = buf[14];
434
435 tcalib->par_t1 = (uint16_t)((raw[1] << 8) | raw[0]);
436 tcalib->par_t2 = (int16_t)((raw[3] << 8) | raw[2]);
437 tcalib->par_t3 = (int8_t)raw[4];
438
439 return ret;
440}
441
442/**
443 * @brief Reads raw temperature coefficients and scales them to floating-point parameters.
444 * @param p_spiHandler SPI device handle.
445 * @param out Output struct populated with scaled PAR_T1/T2/T3.
446 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
447 */
449{
451
452 SPP_RetVal_t ret = SPP_SERVICES_BMP390_readRawTempCoeffs(p_spiHandler, &raw);
453 if (ret != K_SPP_OK)
454 {
455 return ret;
456 }
457
458 out->PAR_T1 = raw.par_t1 * 256.0f; /* 2^8 */
459 out->PAR_T2 = raw.par_t2 / 1073741824.0f; /* 2^30 */
460 out->PAR_T3 = raw.par_t3 / 281474976710656.0f; /* 2^48 */
461
462 return ret;
463}
464
465/**
466 * @brief Reads the 24-bit raw temperature ADC value from the sensor.
467 * @param p_spiHandler SPI device handle.
468 * @param raw_temp Output: raw 24-bit ADC reading.
469 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
470 */
471static SPP_RetVal_t SPP_SERVICES_BMP390_readRawTemp(void *p_spiHandler, uint32_t *raw_temp)
472{
473 spp_uint8_t buf[9] = {
477
478 // spp_uint32_t t0 = SPP_HAL_TIME_getTimeUs();
479 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
480 // spp_uint32_t t1 = SPP_HAL_TIME_getTimeUs();
481
482 // s_lastTempSpi = t1 - t0;
483
484 if (ret != K_SPP_OK)
485 {
486 return ret;
487 }
488
489 spp_uint8_t xlsb = buf[2];
490 spp_uint8_t lsb = buf[5];
491 spp_uint8_t msb = buf[8];
492
493 *raw_temp = ((spp_uint32_t)msb << 16) | ((spp_uint32_t)lsb << 8) | (spp_uint32_t)xlsb;
494
495 return ret;
496}
497
498/**
499 * @brief Applies the BMP390 double-quadratic temperature compensation formula.
500 * @param raw_temp Raw 24-bit ADC reading from the sensor.
501 * @param params Scaled calibration parameters (PAR_T1/T2/T3).
502 * @return Compensated temperature in degrees Celsius (t_lin).
503 */
505{
506 float partial1 = (float)raw_temp - params->PAR_T1;
507 float partial2 = partial1 * params->PAR_T2;
508 float t_lin = partial2 + (partial1 * partial1) * params->PAR_T3;
509
510 return t_lin;
511}
512
513/**
514 * @brief Reads the raw temperature ADC and returns the compensated value.
515 * @param p_spiHandler SPI device handle.
516 * @param temp_params Scaled calibration parameters.
517 * @param raw_temp Output: raw ADC reading.
518 * @param comp_temp Output: compensated temperature in °C.
519 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
520 */
521static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetTemp(void *p_spiHandler, const BMP390_temp_params_t *temp_params,
522 spp_uint32_t *raw_temp, float *comp_temp)
523{
524 SPP_RetVal_t ret = SPP_SERVICES_BMP390_readRawTemp(p_spiHandler, raw_temp);
525 if (ret != K_SPP_OK)
526 {
527 return ret;
528 }
529
530 *comp_temp = SPP_SERVICES_BMP390_compensateTemperature(*raw_temp, (BMP390_temp_params_t *)temp_params);
531
532 return ret;
533}
534
535/* ----------------------------------------------------------------
536 * Driver — pressure
537 * ---------------------------------------------------------------- */
538
539/**
540 * @brief Reads raw pressure calibration coefficients from the sensor's NVM registers.
541 * @param p_spiHandler SPI device handle.
542 * @param pcalib Output struct populated with raw par_p1..p11 values.
543 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
544 */
546{
595
596 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
597 if (ret != K_SPP_OK)
598 {
599 return ret;
600 }
601
602 spp_uint8_t raw[16];
603 for (int i = 0; i < 16; i++)
604 {
605 raw[i] = buf[3 * i + 2];
606 }
607
608 pcalib->par_p1 = (spp_uint16_t)((raw[1] << 8) | raw[0]);
609 pcalib->par_p2 = (spp_uint16_t)((raw[3] << 8) | raw[2]);
610 pcalib->par_p3 = (spp_int8_t)raw[4];
611 pcalib->par_p4 = (spp_int8_t)raw[5];
612 pcalib->par_p5 = (spp_uint16_t)((raw[7] << 8) | raw[6]);
613 pcalib->par_p6 = (spp_uint16_t)((raw[9] << 8) | raw[8]);
614 pcalib->par_p7 = (spp_int8_t)raw[10];
615 pcalib->par_p8 = (spp_int8_t)raw[11];
616 pcalib->par_p9 = (spp_int16_t)((raw[13] << 8) | raw[12]);
617 pcalib->par_p10 = (spp_int8_t)raw[14];
618 pcalib->par_p11 = (spp_int8_t)raw[15];
619
620 return ret;
621}
622
623/**
624 * @brief Reads raw pressure coefficients and scales them to floating-point parameters.
625 * @param p_spiHandler SPI device handle.
626 * @param out Output struct populated with scaled PAR_P1..P11.
627 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
628 */
630{
632
634 if (ret != K_SPP_OK)
635 {
636 return ret;
637 }
638
639 out->PAR_P1 = (raw.par_p1 - 16384.0f) / 1048576.0f; /* (p1 - 2^14) / 2^20 */
640 out->PAR_P2 = (raw.par_p2 - 16384.0f) / 536870912.0f; /* (p2 - 2^14) / 2^29 */
641 out->PAR_P3 = raw.par_p3 / 4294967296.0f; /* / 2^32 */
642 out->PAR_P4 = raw.par_p4 / 137438953472.0f; /* / 2^37 */
643 out->PAR_P5 = raw.par_p5 * 8.0f; /* / 2^-3 */
644 out->PAR_P6 = raw.par_p6 / 64.0f; /* / 2^6 */
645 out->PAR_P7 = raw.par_p7 / 256.0f; /* / 2^8 */
646 out->PAR_P8 = raw.par_p8 / 32768.0f; /* / 2^15 */
647 out->PAR_P9 = raw.par_p9 / 281474976710656.0f; /* / 2^48 */
648 out->PAR_P10 = raw.par_p10 / 281474976710656.0f; /* / 2^48 */
649 out->PAR_P11 = raw.par_p11 / 36893488147419103232.0f; /* / 2^65 */
650
651 return ret;
652}
653
654/**
655 * @brief Reads the 24-bit raw pressure ADC value from the sensor.
656 * @param p_spiHandler SPI device handle.
657 * @param raw_press Output: raw 24-bit ADC reading.
658 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
659 */
660static SPP_RetVal_t SPP_SERVICES_BMP390_readRawPress(void *p_spiHandler, spp_uint32_t *raw_press)
661{
662 spp_uint8_t buf[9] = {
666
667 // spp_uint32_t t0 = SPP_HAL_TIME_getTimeUs();
668 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
669 // spp_uint32_t t1 = SPP_HAL_TIME_getTimeUs();
670
671 // s_lastPressSpi = t1 - t0;
672
673 if (ret != K_SPP_OK)
674 {
675 return ret;
676 }
677
678 spp_uint8_t xlsb = buf[2];
679 spp_uint8_t lsb = buf[5];
680 spp_uint8_t msb = buf[8];
681
682 *raw_press = ((spp_uint32_t)msb << 16) | ((spp_uint32_t)lsb << 8) | (spp_uint32_t)xlsb;
683
684 return ret;
685}
686
687/**
688 * @brief Applies the BMP390 pressure compensation formula using the linearised temperature.
689 * @param raw_press Raw 24-bit ADC pressure reading.
690 * @param t_lin Linearised (compensated) temperature in °C.
691 * @param p Scaled pressure calibration parameters.
692 * @return Compensated pressure in Pa.
693 */
695{
696 s_pd1 = p->PAR_P6 * t_lin;
697 s_pd2 = p->PAR_P7 * (t_lin * t_lin);
698 s_pd3 = p->PAR_P8 * (t_lin * t_lin * t_lin);
699 s_po1 = p->PAR_P5 + s_pd1 + s_pd2 + s_pd3;
700
701 s_pd1 = p->PAR_P2 * t_lin;
702 s_pd2 = p->PAR_P3 * (t_lin * t_lin);
703 s_pd3 = p->PAR_P4 * (t_lin * t_lin * t_lin);
704 s_po2 = raw_press * (p->PAR_P1 + s_pd1 + s_pd2 + s_pd3);
705
706 s_pd1 = raw_press * raw_press;
707 s_pd2 = p->PAR_P9 + p->PAR_P10 * t_lin;
708 s_pd3 = s_pd1 * s_pd2;
709 s_pd4 = s_pd3 + (raw_press * raw_press * raw_press) * p->PAR_P11;
710
712
713 return s_compPress;
714}
715
716/**
717 * @brief Reads raw pressure ADC and returns the compensated value in Pa.
718 * @param p_spiHandler SPI device handle.
719 * @param press_params Scaled pressure calibration parameters.
720 * @param t_lin Linearised temperature used for pressure compensation.
721 * @param raw_press Output: raw 24-bit ADC reading.
722 * @param comp_press Output: compensated pressure in Pa.
723 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
724 */
725static SPP_RetVal_t SPP_SERVICES_BMP390_auxGetPress(void *p_spiHandler, const BMP390_press_params_t *press_params,
726 float t_lin, spp_uint32_t *raw_press, float *comp_press)
727{
728 SPP_RetVal_t ret = SPP_SERVICES_BMP390_readRawPress(p_spiHandler, raw_press);
729 if (ret != K_SPP_OK)
730 {
731 return ret;
732 }
733
734 *comp_press = SPP_SERVICES_BMP390_compensatePressure(*raw_press, t_lin, (BMP390_press_params_t *)press_params);
735
736 return ret;
737}
738
739/* ----------------------------------------------------------------
740 * Driver — altitude
741 * ---------------------------------------------------------------- */
742
743/**
744 * @brief Reads compensated temperature and pressure, and computes relative altitude.
745 * @param p_spiHandler SPI device handle.
746 * @param altitude_m Output: altitude relative to launch-site reference pressure and temperature.
747 * @param pressure_pa Output: compensated pressure in Pa.
748 * @param temperature_c Output: compensated temperature in °C.
749 * @return K_SPP_OK on success, K_SPP_ERROR_NULL_POINTER if any pointer is NULL,
750 * K_SPP_ERROR on SPI failure.
751 */
752static SPP_RetVal_t SPP_SERVICES_BMP390_getAltitude(void *p_spiHandler, float *altitude_m, float *pressure_pa,
753 float *temperature_c)
754{
755 if ((p_spiHandler == NULL) || (altitude_m == NULL) || (pressure_pa == NULL) || (temperature_c == NULL))
756 {
758 }
759
760 static spp_bool_t s_inited = false;
761 static BMP390_temp_params_t s_tempParams;
762 static BMP390_press_params_t s_pressParams;
763 static spp_uint32_t s_rawTemp;
764 static spp_uint32_t s_rawPress;
765
766 float t_lin = 0.0f;
767 float compPress = 0.0f;
768
769 if (s_inited == false)
770 {
771 SPP_RetVal_t ret = SPP_SERVICES_BMP390_calibrateTempParams(p_spiHandler, &s_tempParams);
772 if (ret != K_SPP_OK)
773 {
774 return ret;
775 }
776
777 ret = SPP_SERVICES_BMP390_calibratePressParams(p_spiHandler, &s_pressParams);
778 if (ret != K_SPP_OK)
779 {
780 return ret;
781 }
782
783 s_inited = true;
784 }
785
786 SPP_RetVal_t ret = SPP_SERVICES_BMP390_auxGetTemp(p_spiHandler, &s_tempParams, &s_rawTemp, &t_lin);
787 if (ret != K_SPP_OK)
788 {
789 return ret;
790 }
791
792 ret = SPP_SERVICES_BMP390_auxGetPress(p_spiHandler, &s_pressParams, t_lin, &s_rawPress, &compPress);
793 if (ret != K_SPP_OK)
794 {
795 return ret;
796 }
797
798 *temperature_c = t_lin;
799 *pressure_pa = compPress;
800
801 if (s_isCalibrated == false)
802 {
803 *altitude_m = 0.0f;
804 }
805 else
806 {
807 const spp_float32_t referenceTemperatureK = s_referenceTemperature + 273.15f;
808
809 *altitude_m = (referenceTemperatureK / 0.0065f) * (1.0f - powf(compPress / s_referencePressure, 1.0f / 5.255f));
810 }
811
812 return K_SPP_OK;
813}
814
815/* ----------------------------------------------------------------
816 * Driver — interrupt
817 * ---------------------------------------------------------------- */
818
819/**
820 * @brief Enables the active-high data-ready interrupt output on the sensor.
821 * @param p_spiHandler SPI device handle.
822 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
823 */
825{
827
828 return SPP_HAL_SPI_transmit(p_spiHandler, buf, sizeof(buf));
829}
spp_float32_t s_referenceTemperature
Definition bmp390.c:95
spp_float32_t s_temperatureSum
Definition bmp390.c:92
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:660
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:471
spp_uint16_t s_calibrationSamples
Definition bmp390.c:89
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:333
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:363
static const SPP_SERVICE_ProducerContract_t g_bmp390ProducerContract
Definition bmp390.c:76
spp_float32_t s_referencePressure
Definition bmp390.c:94
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:545
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:320
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:694
static float s_pd3
Definition bmp390.c:82
static float s_pd2
Definition bmp390.c:82
#define K_BMP390_TAG
Definition bmp390.c:32
static const char *const k_svcTag
Definition bmp390.c:81
#define K_BMP390_TASK_TIMEOUT_MS
Definition bmp390.c:31
#define K_BMP390_CALIBRATION_SAMPLES
Definition bmp390.c:87
static float s_po1
Definition bmp390.c:83
static SPP_RetVal_t SPP_SERVICES_BMP390_readRawTempCoeffs(void *p_spiHandler, BMP390_temp_calib_t *tcalib)
Reads raw temperature calibration coefficients from the sensor's NVM registers.
Definition bmp390.c:413
static float s_po2
Definition bmp390.c:83
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:504
static float s_compPress
Definition bmp390.c:84
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:521
static SPP_RetVal_t SPP_SERVICES_BMP390_init(void)
Initializes the BMP390 sensor and its hardware interfaces.
Definition bmp390.c:139
static float s_pd4
Definition bmp390.c:82
const SPP_SERVICE_ProducerContract_t * SPP_SERVICES_BMP390_getProducerContract(void)
Returns the BMP390 producer contract for PUBSUB registration.
Definition bmp390.c:101
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:448
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:629
static BMP390_t s_bmpData
Definition bmp390.c:80
spp_bool_t s_isCalibrated
Definition bmp390.c:88
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:393
static SPP_RetVal_t SPP_SERVICES_BMP390_acquireData(SPP_Kpid_t kpid)
Acquires BMP390 measurements and publishes them through PUBSUB.
Definition bmp390.c:202
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:824
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:307
spp_float32_t s_pressureSum
Definition bmp390.c:91
static float s_pd1
Definition bmp390.c:82
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:725
static SPP_RetVal_t SPP_SERVICES_BMP390_getAltitude(void *p_spiHandler, float *altitude_m, float *pressure_pa, float *temperature_c)
Reads compensated temperature and pressure, and computes relative altitude.
Definition bmp390.c:752
BMP390 barometric sensor driver and SPP producer.
#define K_BMP390_TEMP_RAW_REG
Definition bmp390.h:56
#define K_BMP390_PRESS_CALIB_REG_START
Definition bmp390.h:60
#define K_BMP390_IF_CONF_REG
Definition bmp390.h:36
#define K_BMP390_INT_PIN_NUM
Definition bmp390.h:67
#define BMP390_VALUE_OSR
Definition bmp390.h:43
#define BMP390_VALUE_PWRCTRL
Definition bmp390.h:41
#define K_BMP390_SPI_READ
Definition bmp390.h:26
#define BMP390_VALUE_IIR
Definition bmp390.h:47
#define K_BMP390_SPI_BUS_IDX
Definition bmp390.h:29
#define K_BMP390_REG_OSR
Definition bmp390.h:42
#define BMP390_SOFT_RESET_CMD
Definition bmp390.h:35
#define K_BMP390_INT_CTRL_DRDY_EN
Definition bmp390.h:65
#define K_BMP390_SPI_WRITE
Definition bmp390.h:27
#define K_BMP390_INT_CTRL_LEVEL
Definition bmp390.h:66
#define K_BMP390_CHIP_ID_REG
Definition bmp390.h:32
#define K_BMP390_TEMP_CALIB_REG_START
Definition bmp390.h:55
#define K_BMP390_REG_INT_CTRL
Definition bmp390.h:64
#define K_BMP390_SOFT_RESET_REG
Definition bmp390.h:34
#define BMP390_VALUE_ODR
Definition bmp390.h:45
#define K_BMP390_REG_ODR
Definition bmp390.h:44
#define K_BMP390_REG_IIR
Definition bmp390.h:46
#define K_BMP390_PRESS_RAW_REG
Definition bmp390.h:61
#define BMP390_IF_CONF_SPI
Definition bmp390.h:37
#define K_BMP390_REG_PWRCTRL
Definition bmp390.h:40
#define K_BMP390_INT_INTR_TYPE
Definition bmp390.h:68
#define K_BMP390_INT_PULL
Definition bmp390.h:69
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:37
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:18
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:129
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)
Performs register-based SPI transactions.
Definition spi.c:79
SPP SPI HAL API — dispatches through the registered HAL port.
SPP_GpioIsrCtx_t isrCtx
Definition bmp390.h:140
spp_uint32_t intIntrType
Definition bmp390.h:137
spp_uint32_t intPull
Definition bmp390.h:138
volatile spp_bool_t drdyFlag
Definition bmp390.h:139
spp_uint32_t intPin
Definition bmp390.h:136
spp_uint8_t spiDevIdx
Definition bmp390.h:131
void * p_spiHandler
Definition bmp390.h:132
Raw pressure calibration coefficients.
Definition bmp390.h:95
spp_int8_t par_p10
Definition bmp390.h:105
spp_uint16_t par_p5
Definition bmp390.h:100
spp_int8_t par_p7
Definition bmp390.h:102
spp_int8_t par_p4
Definition bmp390.h:99
spp_int8_t par_p8
Definition bmp390.h:103
spp_int16_t par_p9
Definition bmp390.h:104
spp_uint16_t par_p2
Definition bmp390.h:97
spp_uint16_t par_p6
Definition bmp390.h:101
spp_int8_t par_p3
Definition bmp390.h:98
spp_int8_t par_p11
Definition bmp390.h:106
spp_uint16_t par_p1
Definition bmp390.h:96
Scaled pressure calibration parameters.
Definition bmp390.h:111
BMP390 sensor instance.
Definition bmp390.h:150
BMP390_SpiConfig_t spiConfig
Definition bmp390.h:152
BMP390_GpioConfig_t gpioConfig
Definition bmp390.h:151
spp_uint16_t seq
Definition bmp390.h:153
Raw temperature calibration coefficients (as read from sensor).
Definition bmp390.h:78
uint16_t par_t1
Definition bmp390.h:79
Scaled temperature calibration parameters.
Definition bmp390.h:87
volatile spp_bool_t * p_flag
Definition gpio.h:28
Defines the callbacks and metadata of an SPP producer.
Definition service.h:25
void SPP_HAL_TIME_delayMs(spp_uint32_t ms)
Block for the requested number of milliseconds.
Definition time.c:35
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