|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 CNRS |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 5 | + * General Public License v2.1. See the file LICENSE in the top level |
| 6 | + * directory for more details. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @ingroup tests |
| 11 | + * @{ |
| 12 | + * |
| 13 | + * @file |
| 14 | + * @brief Test application for the ABP2 Honeywell ABP2 series |
| 15 | + * pressure and temperature sensor driver. |
| 16 | + * |
| 17 | + * @author David Picard <[email protected]> |
| 18 | + * @} |
| 19 | + */ |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | +#include <string.h> |
| 23 | + |
| 24 | +#include "ztimer.h" |
| 25 | +#include "timex.h" |
| 26 | +#include "phydat.h" |
| 27 | +#include "abp2.h" |
| 28 | +#include "abp2_params.h" |
| 29 | + |
| 30 | +#define STATUS_SLEEP_MS (1) |
| 31 | +#define MEASUREMENT_SLEEP_MS (1000) |
| 32 | +#define MAX_LOOPS_STATUS (10) |
| 33 | +#define MAX_LOOPS_MEAS (5) |
| 34 | + |
| 35 | +static abp2_t dev; |
| 36 | + |
| 37 | +int main(void) |
| 38 | +{ |
| 39 | + int32_t press = 0; |
| 40 | + int32_t temp = 0; |
| 41 | + abp2_params_t prms; |
| 42 | + phydat_t phyPress; |
| 43 | + phydat_t phyTemp; |
| 44 | + void *ptr; |
| 45 | + int blockingMode = 1; |
| 46 | + int cntMeas = 0; |
| 47 | + |
| 48 | + puts("ABP2 Honeywell series pressure and temperature sensor\n"); |
| 49 | + |
| 50 | + ptr = memcpy(&prms, &abp2_params, sizeof(abp2_params)); |
| 51 | + if(!ptr) |
| 52 | + { |
| 53 | + puts("main() >> ERROR: memcpy() failed"); |
| 54 | + return 1; |
| 55 | + } |
| 56 | + |
| 57 | +#ifdef BOARD_NUCLEO_L031K6 |
| 58 | + /* change chip select pin: */ |
| 59 | + prms.cs = GPIO_PIN(PORT_A, 11); |
| 60 | +#endif |
| 61 | + |
| 62 | + printf("Initializing SPI bus %d\n", prms.spi); |
| 63 | + spi_init(prms.spi); |
| 64 | + |
| 65 | + printf("Initializing ABP2 at SPI_DEV(%i)... ", prms.spi); |
| 66 | + |
| 67 | + if (abp2_init(&dev, &prms) == 0) |
| 68 | + { |
| 69 | + puts("[OK]"); |
| 70 | + } |
| 71 | + else { |
| 72 | + puts("[Failed]"); |
| 73 | + return -1; |
| 74 | + } |
| 75 | +#ifdef BOARD_NUCLEO_L031K6 |
| 76 | + ztimer_sleep(ZTIMER_MSEC, 5000); |
| 77 | +#endif |
| 78 | + puts("========================="); |
| 79 | + puts(" Measuring"); |
| 80 | + puts("========================="); |
| 81 | + |
| 82 | + printf("Pressure range = %d .. %d\n", (int)prms.rangeMin, (int)prms.rangeMax); |
| 83 | + |
| 84 | + while (1) { |
| 85 | + int res; |
| 86 | + |
| 87 | + ztimer_sleep(ZTIMER_MSEC, MEASUREMENT_SLEEP_MS); |
| 88 | + |
| 89 | + if (blockingMode) { |
| 90 | + res = abp2_read(&dev, &press, &temp); |
| 91 | + if (res) |
| 92 | + { |
| 93 | + printf("abp2_read() >> ERROR errno = %d", res); |
| 94 | + continue; |
| 95 | + } |
| 96 | + } |
| 97 | + else { |
| 98 | + res = abp2_read_nb(&dev, &press, &temp); |
| 99 | + if (res) |
| 100 | + { |
| 101 | + printf("abp2_read_nb() >> ERROR errno = %d", res); |
| 102 | + continue; |
| 103 | + } |
| 104 | + } |
| 105 | + /* printf() doesn't support %f on all boards. This is where phydat comes in handy. |
| 106 | + * The default range in RIOT/drivers/abp2/include/abp2_params.h is 0..160000ubar = 0..160mbar. |
| 107 | + */ |
| 108 | + |
| 109 | + phyPress.val[0] = press; /* press is already in ubar */ |
| 110 | + phyPress.scale = -6; /* 1 ubar = 1e-06 bar */ |
| 111 | + phyPress.unit = UNIT_BAR; /* set the unit */ |
| 112 | + phydat_dump(&phyPress, 1); /* print the value in a pretty format */ |
| 113 | + |
| 114 | + phyTemp.val[0] = temp; /* temp is already in mdeg C */ |
| 115 | + phyTemp.scale = -3; /* 1 mdegC = 1e-03 degC */ |
| 116 | + phyTemp.unit = UNIT_TEMP_C; /* set the unit */ |
| 117 | + phydat_dump(&phyTemp, 1); /* print the value in a pretty format */ |
| 118 | + |
| 119 | + /* Switch between blocking and non-blocking modes periodically: */ |
| 120 | + cntMeas++; |
| 121 | + if (cntMeas == MAX_LOOPS_MEAS) |
| 122 | + { |
| 123 | + cntMeas = 0; |
| 124 | + blockingMode = 1 - blockingMode; |
| 125 | + if(blockingMode) |
| 126 | + puts("Switch to blocking mode"); |
| 127 | + else |
| 128 | + puts("Switch to non-blocking mode"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return 0; |
| 133 | +} |
0 commit comments