diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index be1e997..6796917 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,10 +1,7 @@ -# Use the base image for ESP-IDF development FROM espressif/idf:v6.0-dev -# Install necessary packages USER root -# Install sudo and other essential packages RUN apt-get update && apt-get install -y \ sudo \ curl \ @@ -19,41 +16,39 @@ RUN apt-get update && apt-get install -y \ locales \ doxygen \ graphviz \ - texlive-full \ + texlive-latex-extra \ + texlive-fonts-recommended \ + texlive-plain-generic \ + latexmk \ + clang \ + clangd \ clang-format \ clang-tidy \ + clang-tools \ cppcheck \ + flawfinder \ + python3-pip \ + && locale-gen en_US.UTF-8 \ + && update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 \ && rm -rf /var/lib/apt/lists/* -# Set nano as the default Git editor RUN git config --global core.editor "nano" -# Set the working directory WORKDIR /root -# Install arduino-cli as the root user RUN curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh -# Install git completion RUN curl -o /etc/bash_completion.d/git-completion.bash \ https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -# Copy and set up terminal scripts COPY .devcontainer/setup-terminal.sh /root/setup-terminal.sh RUN chmod +x /root/setup-terminal.sh -# Update .bashrc for git completion and custom scripts RUN echo "source /etc/bash_completion.d/git-completion.bash" >> /root/.bashrc \ && echo "source /root/setup-terminal.sh" >> /root/.bashrc \ && echo "source /opt/esp/idf/export.sh >/dev/null 2>&1" >> /root/.bashrc -# Set locale to a UTF-8 compatible setting -RUN apt-get update && apt-get install -y locales && \ - locale-gen en_US.UTF-8 && \ - update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 +ENV LANG=en_US.UTF-8 +ENV LC_ALL=en_US.UTF-8 -ENV LANG en_US.UTF-8 -ENV LC_ALL en_US.UTF-8 - -# Ensure the final user is root -USER root +USER root \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index f6d5da4..8d8853c 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -3,40 +3,52 @@ "build": { "dockerfile": "Dockerfile", "context": "..", - "args": { "USERNAME": "root", "USER_UID": "1000", "USER_GID": "1000" } + "args": { + "USERNAME": "root", + "USER_UID": "1000", + "USER_GID": "1000" + } }, - "runArgs": [ "--device=/dev/ttyACM0", "--privileged" - ], - - "settings": { "terminal.integrated.defaultProfile.linux": "bash" }, - - "extensions": [ - "vsciot-vscode.vscode-arduino", - "ms-vscode.cpptools", - "eamodio.gitlens", - "cschlosser.doxdocgen", - "samubarb.vscode-doxyfile", - "openai.tu-extension-codex", - "github.vscode-pull-request-github", - "llvm-vs-code-extensions.vscode-clangd", - "jbenden.c-cpp-flylint" - ], - "remoteUser": "root", - "mounts": [ "source=${localEnv:HOME}/.ssh,target=/tmp/host-ssh,type=bind,consistency=cached,readonly", "source=${localEnv:HOME}/.gitconfig,target=/root/.gitconfig,type=bind,consistency=cached,readonly" ], - "postCreateCommand": "/bin/bash -lc 'echo \"source /opt/esp/idf/export.sh\" >> ~/.bashrc && mkdir -p /root/.ssh && cp -a /tmp/host-ssh/. /root/.ssh/ 2>/dev/null || true && chown -R root:root /root/.ssh && chmod 700 /root/.ssh && for f in /root/.ssh/config /root/.ssh/known_hosts /root/.ssh/id_rsa /root/.ssh/raspberrypi /root/.ssh/id_ed25519; do [ -f \"$f\" ] && chmod 600 \"$f\"; done && for f in /root/.ssh/id_rsa.pub /root/.ssh/raspberrypi.pub /root/.ssh/id_ed25519.pub; do [ -f \"$f\" ] && chmod 644 \"$f\"; done && echo \"Dev container ready for ESP-IDF development!\"'", - "remoteEnv": { "IDF_PATH": "/opt/esp/idf", "PATH": "/opt/esp/idf/tools:${env:PATH}" + }, + "customizations": { + "vscode": { + "settings": { + "terminal.integrated.defaultProfile.linux": "bash", + "C_Cpp.intelliSenseEngine": "disabled", + "editor.formatOnSave": true, + "clangd.path": "/usr/bin/clangd", + "[c]": { + "editor.defaultFormatter": "xaver.clang-format" + }, + "[cpp]": { + "editor.defaultFormatter": "xaver.clang-format" + } + }, + "extensions": [ + "vsciot-vscode.vscode-arduino", + "ms-vscode.cpptools", + "eamodio.gitlens", + "cschlosser.doxdocgen", + "samubarb.vscode-doxyfile", + "openai.tu-extension-codex", + "github.vscode-pull-request-github", + "llvm-vs-code-extensions.vscode-clangd", + "xaver.clang-format", + "jbenden.c-cpp-flylint" + ] + } } } \ No newline at end of file diff --git a/solaris-v1/components/general/CMakeLists.txt b/solaris-v1/components/general/CMakeLists.txt index 18b9c9c..52dc3a8 100644 --- a/solaris-v1/components/general/CMakeLists.txt +++ b/solaris-v1/components/general/CMakeLists.txt @@ -4,5 +4,5 @@ idf_component_register( "gpio_int.c" INCLUDE_DIRS "." REQUIRES esp_driver_spi esp_driver_gpio - PRIV_REQUIRES icm_driver pressure_sensor_driver + PRIV_REQUIRES icm_driver pressureSensorDriver ) diff --git a/solaris-v1/components/general/macros.h b/solaris-v1/components/general/macros.h index 00f6024..4aff962 100644 --- a/solaris-v1/components/general/macros.h +++ b/solaris-v1/components/general/macros.h @@ -29,6 +29,20 @@ extern "C" { /** @brief GPIO pin for SPI clock. */ #define PIN_NUM_CLK 48 +/* ---------------------------------------------------------------- + * SPI operation type + * ---------------------------------------------------------------- */ + +/** + * @brief SPI read operation bit. + */ +#define K_READ_OP 0x80U + +/** + * @brief SPI write operation bit. + */ +#define K_WRITE_OP 0x00U + /** * @brief Legacy SPI device context. * diff --git a/solaris-v1/components/icm_driver/icm20948.c b/solaris-v1/components/icm_driver/icm20948.c index 9f9deb6..e05b4f8 100644 --- a/solaris-v1/components/icm_driver/icm20948.c +++ b/solaris-v1/components/icm_driver/icm20948.c @@ -1,4 +1,5 @@ #include "icm20948.h" +#include "macros.h" #include "spp/core/returntypes.h" #include "spp/hal/spi/spi.h" #include "spp/osal/task.h" @@ -97,7 +98,7 @@ static const spp_uint8_t s_dmp3Image[] = { */ static retval_t ICM20948_writeReg(void *p_spi, spp_uint8_t reg, spp_uint8_t value) { - spp_uint8_t txBuffer[2] = {K_ICM20948_WRITE_OP | reg, value}; + spp_uint8_t txBuffer[2] = {K_WRITE_OP | reg, value}; return SPP_HAL_SPI_Transmit(p_spi, txBuffer, 2U); } @@ -112,7 +113,7 @@ static retval_t ICM20948_writeReg(void *p_spi, spp_uint8_t reg, spp_uint8_t valu */ static retval_t ICM20948_readReg(void *p_spi, spp_uint8_t reg, spp_uint8_t *p_value) { - spp_uint8_t txRxBuffer[2] = {K_ICM20948_READ_OP | reg, K_ICM20948_EMPTY_MESSAGE}; + spp_uint8_t txRxBuffer[2] = {K_READ_OP | reg, K_WRITE_OP}; retval_t ret = SPP_HAL_SPI_Transmit(p_spi, txRxBuffer, 2U); if (p_value != NULL) @@ -1427,8 +1428,8 @@ void ICM20948_checkFifoData(void *p_data) return; } - txRxData[0] = K_ICM20948_READ_OP | K_ICM20948_REG_INT_STATUS; - txRxData[1] = K_ICM20948_EMPTY_MESSAGE; + txRxData[0] = K_READ_OP | K_ICM20948_REG_INT_STATUS; + txRxData[1] = K_WRITE_OP; ret = SPP_HAL_SPI_Transmit(p_spi, txRxData, 2U); if (ret != SPP_OK) @@ -1439,8 +1440,8 @@ void ICM20948_checkFifoData(void *p_data) { spp_uint8_t intStatus = txRxData[1]; - txRxData[0] = K_ICM20948_READ_OP | K_ICM20948_REG_DMP_INT_STATUS; - txRxData[1] = K_ICM20948_EMPTY_MESSAGE; + txRxData[0] = K_READ_OP | K_ICM20948_REG_DMP_INT_STATUS; + txRxData[1] = K_WRITE_OP; ret = SPP_HAL_SPI_Transmit(p_spi, txRxData, 2U); if (ret != SPP_OK) @@ -1450,9 +1451,9 @@ void ICM20948_checkFifoData(void *p_data) if ((intStatus & 0x02U) != 0U) { - txRxData[0] = K_ICM20948_READ_OP | K_ICM20948_REG_FIFO_COUNTH; - txRxData[1] = K_ICM20948_EMPTY_MESSAGE; - txRxData[2] = K_ICM20948_EMPTY_MESSAGE; + txRxData[0] = K_READ_OP | K_ICM20948_REG_FIFO_COUNTH; + txRxData[1] = K_WRITE_OP; + txRxData[2] = K_WRITE_OP; ret = SPP_HAL_SPI_Transmit(p_spi, txRxData, 3U); if (ret != SPP_OK) @@ -1476,7 +1477,7 @@ void ICM20948_checkFifoData(void *p_data) { spp_uint8_t fifoBuffer[K_ICM20948_DMP_PACKET_SIZE_BYTES + 1U] = {0U}; - fifoBuffer[0] = K_ICM20948_READ_OP | K_ICM20948_REG_FIFO_R_W; + fifoBuffer[0] = K_READ_OP | K_ICM20948_REG_FIFO_R_W; ret = SPP_HAL_SPI_Transmit(p_spi, fifoBuffer, K_ICM20948_DMP_PACKET_SIZE_BYTES + 1U); if (ret != SPP_OK) diff --git a/solaris-v1/components/icm_driver/include/icm20948.h b/solaris-v1/components/icm_driver/include/icm20948.h index f716fa2..a66a80f 100644 --- a/solaris-v1/components/icm_driver/include/icm20948.h +++ b/solaris-v1/components/icm_driver/include/icm20948.h @@ -47,49 +47,12 @@ * Hardware pins * ---------------------------------------------------------------- */ -/** - * @brief SPI host used by the ICM20948 device. - */ -#define K_ICM20948_SPI_HOST_USED SPI2_HOST /** * @brief Chip-select GPIO number. */ #define K_ICM20948_PIN_NUM_CS 21U -/** - * @brief CIPO/MISO GPIO number. - */ -#define K_ICM20948_PIN_NUM_CIPO 47U - -/** - * @brief COPI/MOSI GPIO number. - */ -#define K_ICM20948_PIN_NUM_COPI 38U - -/** - * @brief SPI clock GPIO number. - */ -#define K_ICM20948_PIN_NUM_CLK 48U - -/* ---------------------------------------------------------------- - * SPI operation type - * ---------------------------------------------------------------- */ - -/** - * @brief SPI read operation bit. - */ -#define K_ICM20948_READ_OP 0x80U - -/** - * @brief SPI write operation bit. - */ -#define K_ICM20948_WRITE_OP 0x00U - -/** - * @brief Dummy byte used during SPI read operations. - */ -#define K_ICM20948_EMPTY_MESSAGE 0x00U /* ---------------------------------------------------------------- * Register banks diff --git a/solaris-v1/components/pressure_sensor_driver/CMakeLists.txt b/solaris-v1/components/pressureSensorDriver/CMakeLists.txt similarity index 100% rename from solaris-v1/components/pressure_sensor_driver/CMakeLists.txt rename to solaris-v1/components/pressureSensorDriver/CMakeLists.txt diff --git a/solaris-v1/components/pressure_sensor_driver/bmp390.c b/solaris-v1/components/pressureSensorDriver/bmp390.c similarity index 54% rename from solaris-v1/components/pressure_sensor_driver/bmp390.c rename to solaris-v1/components/pressureSensorDriver/bmp390.c index 46fc552..6e20ff1 100644 --- a/solaris-v1/components/pressure_sensor_driver/bmp390.c +++ b/solaris-v1/components/pressureSensorDriver/bmp390.c @@ -1,17 +1,18 @@ -#include "bmp390.h" +#include "include/bmp390.h" +#include "macros.h" #include #include #include "spi.h" #include "osal/task.h" #include "spp_log.h" -static const char* TAG = "BMP390"; +static const char *TAG = "BMP390"; spp_uint8_t id, ifc; -bmp390_temp_calib_t raw_calib; -bmp390_temp_params_t temp_params; +BMP390_temp_calib_t raw_calib; +BMP390_temp_params_t temp_params; spp_uint32_t raw_temp; -bmp390_press_calib_t raw_press_calib; -bmp390_press_params_t press_params; +BMP390_press_calib_t raw_press_calib; +BMP390_press_params_t press_params; spp_uint32_t raw_press; float t_lin; spp_uint8_t st; @@ -28,7 +29,7 @@ float comp_press; * including SPI communication setup, event group creation, and GPIO interrupt * configuration. It must be called before any sensor operations. * - * @param[in,out] p_data Pointer to a bmp_data_t structure that will be + * @param[in,out] p_data Pointer to a BMP390_Data_t structure that will be * populated with initialization data including: * - SPI handler * - Event group pointer @@ -44,20 +45,20 @@ float comp_press; * 5. Registers the ISR callback for the interrupt pin * 6. Deletes the current task upon completion */ -void BmpInit(void* p_data) +void BMP390_init(void *p_data) { - bmp_data_t* p_bmp = (bmp_data_t*)p_data; + BMP390_Data_t *p_bmp = (BMP390_Data_t *)p_data; - void* p_buffer_eg; + void *p_buffer_eg; p_buffer_eg = SPP_OSAL_GetEventGroupsBuffer(); p_bmp->p_event_group = SPP_OSAL_EventGroupCreate(p_buffer_eg); p_bmp->isr_ctx.p_event_group = p_bmp->p_event_group; - p_bmp->isr_ctx.bits = BMP390_EVT_DRDY; + p_bmp->isr_ctx.bits = K_BMP390_EVT_DRDY; - SPP_HAL_GPIO_ConfigInterrupt(p_bmp->int_pin, p_bmp->int_intr_type, p_bmp->int_pull); - SPP_HAL_GPIO_RegisterISR(p_bmp->int_pin, (void*)&p_bmp->isr_ctx); + SPP_HAL_GPIO_ConfigInterrupt(p_bmp->intPin, p_bmp->intIntrType, p_bmp->intPull); + SPP_HAL_GPIO_RegisterISR(p_bmp->intPin, (void *)&p_bmp->isr_ctx); } @@ -68,13 +69,9 @@ void BmpInit(void* p_data) * @param[in] p_spi Pointer to the SPI device handle. * @return retval_t Status code indicating success or failure. */ -retval_t bmp390_soft_reset(void *p_spi) +retval_t BMP390_soft_reset(void *p_spi) { - spp_uint8_t buf[2] = - { - (spp_uint8_t)BMP390_SOFT_RESET_REG, - (spp_uint8_t)BMP390_SOFT_RESET_CMD - }; + spp_uint8_t buf[2] = {(spp_uint8_t)K_BMP390_SOFT_RESET_REG, (spp_uint8_t)BMP390_SOFT_RESET_CMD}; retval_t ret = SPP_HAL_SPI_Transmit(p_spi, buf, sizeof(buf)); SPP_OSAL_TaskDelay(100); @@ -88,13 +85,9 @@ retval_t bmp390_soft_reset(void *p_spi) * @param[in] p_spi Pointer to the SPI device handle. * @return retval_t Status code indicating success or failure. */ -retval_t bmp390_enable_spi_mode(void *p_spi) +retval_t BMP390_enable_spi_mode(void *p_spi) { - spp_uint8_t buf[2] = - { - (spp_uint8_t)BMP390_IF_CONF_REG, - (spp_uint8_t)BMP390_IF_CONF_SPI - }; + spp_uint8_t buf[2] = {(spp_uint8_t)K_BMP390_IF_CONF_REG, (spp_uint8_t)BMP390_IF_CONF_SPI}; retval_t ret = SPP_HAL_SPI_Transmit(p_spi, buf, (spp_uint8_t)sizeof(buf)); SPP_OSAL_TaskDelay(100); @@ -108,26 +101,26 @@ retval_t bmp390_enable_spi_mode(void *p_spi) * @param[in] p_spi Pointer to the SPI device handle. * @return retval_t Status code indicating success or failure. */ -retval_t bmp390_config_check(void *p_spi) +retval_t BMP390_config_check(void *p_spi) { - spp_uint8_t buf[9] = - { - (spp_uint8_t)(READ_OP | BMP390_IF_CONF_REG), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | BMP390_SOFT_RESET_REG), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | BMP390_CHIP_ID_REG), EMPTY_MESSAGE, EMPTY_MESSAGE - }; - + spp_uint8_t buf[9] = { + (spp_uint8_t)(K_READ_OP | K_BMP390_IF_CONF_REG), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | K_BMP390_SOFT_RESET_REG), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | K_BMP390_CHIP_ID_REG), K_WRITE_OP, K_WRITE_OP}; + retval_t ret; ret = SPP_HAL_SPI_Transmit(p_spi, buf, (spp_uint8_t)sizeof(buf)); - - if (ret != SPP_OK) { + + if (ret != SPP_OK) + { return ret; } SPP_LOGI(TAG, "ID: 0x%02X", buf[8]); - if (buf[8] != 0x60) { + if (buf[8] != 0x60) + { SPP_LOGE(TAG, "BMP390 not detected! Expected ID: 0x%02X, Read ID: 0x%02X", 0x60, buf[5]); return SPP_ERROR; } @@ -141,19 +134,22 @@ retval_t bmp390_config_check(void *p_spi) * @param[in] p_spi Pointer to the SPI device handle. * @return retval_t Status code from the initialization sequence. */ -retval_t bmp390_aux_config(void *p_spi) +retval_t BMP390_auxConfig(void *p_spi) { retval_t ret; - ret = bmp390_soft_reset(p_spi); - if (ret != SPP_OK) return ret; + ret = BMP390_soft_reset(p_spi); + if (ret != SPP_OK) + return ret; - ret = bmp390_enable_spi_mode(p_spi); - if (ret != SPP_OK) return ret; + ret = BMP390_enable_spi_mode(p_spi); + if (ret != SPP_OK) + return ret; + + ret = BMP390_config_check(p_spi); + if (ret != SPP_OK) + return ret; - ret = bmp390_config_check(p_spi); - if (ret != SPP_OK) return ret; - return ret; } @@ -171,15 +167,12 @@ retval_t bmp390_aux_config(void *p_spi) * * @return retval_t Status code indicating success or failure of the SPI transmission. */ -retval_t bmp390_prepare_measure(void *p_spi) +retval_t BMP390_prepareMeasure(void *p_spi) { - spp_uint8_t buf[8] = - { - (spp_uint8_t)BMP390_REG_OSR, (spp_uint8_t)BMP390_VALUE_OSR, - (spp_uint8_t)BMP390_REG_ODR, (spp_uint8_t)BMP390_VALUE_ODR, - (spp_uint8_t)BMP390_REG_IIR, (spp_uint8_t)BMP390_VALUE_IIR, - (spp_uint8_t)BMP390_REG_PWRCTRL, (spp_uint8_t)BMP390_VALUE_PWRCTRL - }; + spp_uint8_t buf[8] = {(spp_uint8_t)K_BMP390_REG_OSR, (spp_uint8_t)BMP390_VALUE_OSR, + (spp_uint8_t)K_BMP390_REG_ODR, (spp_uint8_t)BMP390_VALUE_ODR, + (spp_uint8_t)K_BMP390_REG_IIR, (spp_uint8_t)BMP390_VALUE_IIR, + (spp_uint8_t)K_BMP390_REG_PWRCTRL, (spp_uint8_t)BMP390_VALUE_PWRCTRL}; retval_t ret = SPP_HAL_SPI_Transmit(p_spi, buf, sizeof(buf)); @@ -194,18 +187,14 @@ retval_t bmp390_prepare_measure(void *p_spi) * * @return SPP_OK if data ready event was signaled, error code otherwise */ -retval_t bmp390_wait_drdy(bmp_data_t* p_bmp, spp_uint32_t timeout_ms) +retval_t BMP390_waitDrdy(BMP390_Data_t *p_bmp, spp_uint32_t timeout_ms) { osal_eventbits_t bits; - retval_t ret = OSAL_EventGroupWaitBits( - p_bmp->p_event_group, - BMP390_EVT_DRDY, - 1, // clear_on_exit - 0, // wait_for_all_bits - timeout_ms, - &bits - ); + retval_t ret = OSAL_EventGroupWaitBits(p_bmp->p_event_group, K_BMP390_EVT_DRDY, + 1, // clear_on_exit + 0, // wait_for_all_bits + timeout_ms, &bits); return ret; } @@ -219,20 +208,20 @@ retval_t bmp390_wait_drdy(bmp_data_t* p_bmp, spp_uint32_t timeout_ms) * * @return retval_t Status code indicating success or failure of the SPI transmission. */ -retval_t bmp390_read_raw_temp_coeffs(void *p_spi, bmp390_temp_calib_t *tcalib) +retval_t BMP390_read_raw_temp_coeffs(void *p_spi, BMP390_temp_calib_t *tcalib) { retval_t ret; spp_uint8_t buf[15] = { - (spp_uint8_t)(READ_OP | (BMP390_TEMP_CALIB_REG_START + 0)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_TEMP_CALIB_REG_START + 1)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_TEMP_CALIB_REG_START + 2)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_TEMP_CALIB_REG_START + 3)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_TEMP_CALIB_REG_START + 4)), EMPTY_MESSAGE, EMPTY_MESSAGE - }; + (spp_uint8_t)(K_READ_OP | (K_BMP390_TEMP_CALIB_REG_START + 0)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_TEMP_CALIB_REG_START + 1)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_TEMP_CALIB_REG_START + 2)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_TEMP_CALIB_REG_START + 3)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_TEMP_CALIB_REG_START + 4)), K_WRITE_OP, K_WRITE_OP}; ret = SPP_HAL_SPI_Transmit(p_spi, buf, sizeof(buf)); - if (ret != SPP_OK) { + if (ret != SPP_OK) + { return ret; } @@ -258,19 +247,20 @@ retval_t bmp390_read_raw_temp_coeffs(void *p_spi, bmp390_temp_calib_t *tcalib) * * @return retval_t Status code indicating success or failure of the SPI transmission. */ -retval_t bmp390_calibrate_temp_params(void *p_spi, bmp390_temp_params_t *out) +retval_t BMP390_calibrate_temp_params(void *p_spi, BMP390_temp_params_t *out) { retval_t ret; - bmp390_temp_calib_t raw; + BMP390_temp_calib_t raw; - ret = bmp390_read_raw_temp_coeffs(p_spi, &raw); - if (ret != SPP_OK) { + ret = BMP390_read_raw_temp_coeffs(p_spi, &raw); + if (ret != SPP_OK) + { return ret; } - out->PAR_T1 = raw.par_t1 * 256.0f; // 2^(-8) - out->PAR_T2 = raw.par_t2 / 1073741824.0f; // 2^30 - out->PAR_T3 = raw.par_t3 / 281474976710656.0f; // 2^48 + out->PAR_T1 = raw.par_t1 * 256.0f; // 2^(-8) + out->PAR_T2 = raw.par_t2 / 1073741824.0f; // 2^30 + out->PAR_T3 = raw.par_t3 / 281474976710656.0f; // 2^48 return ret; } @@ -283,26 +273,26 @@ retval_t bmp390_calibrate_temp_params(void *p_spi, bmp390_temp_params_t *out) * * @return retval_t Status code indicating success or failure of the SPI transmission. */ -retval_t bmp390_read_raw_temp(void *p_spi, uint32_t *raw_temp) +retval_t BMP390_read_raw_temp(void *p_spi, uint32_t *raw_temp) { retval_t ret; spp_uint8_t buf[9] = { - (spp_uint8_t)(READ_OP | (BMP390_TEMP_RAW_REG + 0)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_TEMP_RAW_REG + 1)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_TEMP_RAW_REG + 2)), EMPTY_MESSAGE, EMPTY_MESSAGE - }; + (spp_uint8_t)(K_READ_OP | (K_BMP390_TEMP_RAW_REG + 0)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_TEMP_RAW_REG + 1)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_TEMP_RAW_REG + 2)), K_WRITE_OP, K_WRITE_OP}; ret = SPP_HAL_SPI_Transmit(p_spi, buf, sizeof(buf)); - if (ret != SPP_OK) { + if (ret != SPP_OK) + { return ret; } spp_uint8_t xlsb = buf[2]; - spp_uint8_t lsb = buf[5]; - spp_uint8_t msb = buf[8]; + spp_uint8_t lsb = buf[5]; + spp_uint8_t msb = buf[8]; - *raw_temp = ((spp_uint32_t)msb << 16) | ((spp_uint32_t)lsb << 8) | (spp_uint32_t)xlsb; + *raw_temp = ((spp_uint32_t)msb << 16) | ((spp_uint32_t)lsb << 8) | (spp_uint32_t)xlsb; return ret; } @@ -315,7 +305,7 @@ retval_t bmp390_read_raw_temp(void *p_spi, uint32_t *raw_temp) * * @return Compensated temperature value (float) in Celsius. */ -float bmp390_compensate_temperature(spp_uint32_t raw_temp, bmp390_temp_params_t *params) +float BMP390_compensate_temperature(spp_uint32_t raw_temp, BMP390_temp_params_t *params) { float partial1 = (float)raw_temp - params->PAR_T1; float partial2 = partial1 * params->PAR_T2; @@ -334,16 +324,18 @@ float bmp390_compensate_temperature(spp_uint32_t raw_temp, bmp390_temp_params_t * * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_aux_get_temp(void *p_spi, const bmp390_temp_params_t *temp_params, spp_uint32_t *raw_temp, float *comp_temp) +retval_t BMP390_aux_get_temp(void *p_spi, const BMP390_temp_params_t *temp_params, + spp_uint32_t *raw_temp, float *comp_temp) { retval_t ret; - ret = bmp390_read_raw_temp(p_spi, raw_temp); - if (ret != SPP_OK) { + ret = BMP390_read_raw_temp(p_spi, raw_temp); + if (ret != SPP_OK) + { return ret; } - *comp_temp = bmp390_compensate_temperature(*raw_temp, (bmp390_temp_params_t*)temp_params); + *comp_temp = BMP390_compensate_temperature(*raw_temp, (BMP390_temp_params_t *)temp_params); return ret; } @@ -357,48 +349,49 @@ retval_t bmp390_aux_get_temp(void *p_spi, const bmp390_temp_params_t *temp_param * * @return retval_t Status code indicating success or failure of the SPI transmission. */ -retval_t bmp390_read_raw_press_coeffs(void *p_spi, bmp390_press_calib_t *pcalib) +retval_t BMP390_read_raw_press_coeffs(void *p_spi, BMP390_press_calib_t *pcalib) { retval_t ret; spp_uint8_t buf[48] = { - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 0)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 1)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 2)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 3)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 4)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 5)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 6)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 7)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 8)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 9)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 10)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 11)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 12)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 13)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 14)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_CALIB_REG_START + 15)), EMPTY_MESSAGE, EMPTY_MESSAGE - }; + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 0)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 1)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 2)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 3)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 4)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 5)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 6)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 7)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 8)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 9)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 10)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 11)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 12)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 13)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 14)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_CALIB_REG_START + 15)), K_WRITE_OP, K_WRITE_OP}; ret = SPP_HAL_SPI_Transmit(p_spi, buf, sizeof(buf)); - if (ret != SPP_OK) return ret; + if (ret != SPP_OK) + return ret; spp_uint8_t raw[16]; - for (int i = 0; i < 16; i++) { + for (int i = 0; i < 16; i++) + { raw[i] = buf[3 * i + 2]; } - pcalib->par_p1 = (spp_uint16_t)((raw[1] << 8) | raw[0]); - pcalib->par_p2 = (spp_uint16_t)((raw[3] << 8) | raw[2]); - pcalib->par_p3 = (spp_int8_t) raw[4]; - pcalib->par_p4 = (spp_int8_t) raw[5]; - pcalib->par_p5 = (spp_uint16_t)((raw[7] << 8) | raw[6]); - pcalib->par_p6 = (spp_uint16_t)((raw[9] << 8) | raw[8]); - pcalib->par_p7 = (spp_int8_t) raw[10]; - pcalib->par_p8 = (spp_int8_t) raw[11]; - pcalib->par_p9 = (spp_int16_t)((raw[13] << 8) | raw[12]); - pcalib->par_p10 = (spp_int8_t) raw[14]; - pcalib->par_p11 = (spp_int8_t) raw[15]; + pcalib->par_p1 = (spp_uint16_t)((raw[1] << 8) | raw[0]); + pcalib->par_p2 = (spp_uint16_t)((raw[3] << 8) | raw[2]); + pcalib->par_p3 = (spp_int8_t)raw[4]; + pcalib->par_p4 = (spp_int8_t)raw[5]; + pcalib->par_p5 = (spp_uint16_t)((raw[7] << 8) | raw[6]); + pcalib->par_p6 = (spp_uint16_t)((raw[9] << 8) | raw[8]); + pcalib->par_p7 = (spp_int8_t)raw[10]; + pcalib->par_p8 = (spp_int8_t)raw[11]; + pcalib->par_p9 = (spp_int16_t)((raw[13] << 8) | raw[12]); + pcalib->par_p10 = (spp_int8_t)raw[14]; + pcalib->par_p11 = (spp_int8_t)raw[15]; return ret; } @@ -411,27 +404,28 @@ retval_t bmp390_read_raw_press_coeffs(void *p_spi, bmp390_press_calib_t *pcalib) * * @return retval_t Status code indicating success or failure of the SPI transmission. */ -retval_t bmp390_calibrate_press_params(void *p_spi, bmp390_press_params_t *out) +retval_t BMP390_calibrate_press_params(void *p_spi, BMP390_press_params_t *out) { retval_t ret; - bmp390_press_calib_t raw; + BMP390_press_calib_t raw; - ret = bmp390_read_raw_press_coeffs(p_spi, &raw); - if (ret != SPP_OK) { + ret = BMP390_read_raw_press_coeffs(p_spi, &raw); + if (ret != SPP_OK) + { return ret; } - out->PAR_P1 = (raw.par_p1 - 16384.0f) / 1048576.0f; // (p1 - 2^14) / 2^20 - out->PAR_P2 = (raw.par_p2 - 16384.0f) / 536870912.0f; // (p2 - 2^14) / 2^29 - out->PAR_P3 = raw.par_p3 / 4294967296.0f; // / 2^32 - out->PAR_P4 = raw.par_p4 / 137438953472.0f; // / 2^37 - out->PAR_P5 = raw.par_p5 * 8.0f; // / 2^-3 - out->PAR_P6 = raw.par_p6 / 64.0f; // / 2^6 - out->PAR_P7 = raw.par_p7 / 256.0f; // / 2^8 - out->PAR_P8 = raw.par_p8 / 32768.0f; // / 2^15 - out->PAR_P9 = raw.par_p9 / 281474976710656.0f; // / 2^48 - out->PAR_P10 = raw.par_p10 / 281474976710656.0f; // / 2^48 - out->PAR_P11 = raw.par_p11 / 36893488147419103232.0f; // / 2^65 + out->PAR_P1 = (raw.par_p1 - 16384.0f) / 1048576.0f; // (p1 - 2^14) / 2^20 + out->PAR_P2 = (raw.par_p2 - 16384.0f) / 536870912.0f; // (p2 - 2^14) / 2^29 + out->PAR_P3 = raw.par_p3 / 4294967296.0f; // / 2^32 + out->PAR_P4 = raw.par_p4 / 137438953472.0f; // / 2^37 + out->PAR_P5 = raw.par_p5 * 8.0f; // / 2^-3 + out->PAR_P6 = raw.par_p6 / 64.0f; // / 2^6 + out->PAR_P7 = raw.par_p7 / 256.0f; // / 2^8 + out->PAR_P8 = raw.par_p8 / 32768.0f; // / 2^15 + out->PAR_P9 = raw.par_p9 / 281474976710656.0f; // / 2^48 + out->PAR_P10 = raw.par_p10 / 281474976710656.0f; // / 2^48 + out->PAR_P11 = raw.par_p11 / 36893488147419103232.0f; // / 2^65 return ret; } @@ -443,26 +437,26 @@ retval_t bmp390_calibrate_press_params(void *p_spi, bmp390_press_params_t *out) * * @return retval_t Status code indicating success or failure of the SPI transmission. */ -retval_t bmp390_read_raw_press(void *p_spi, spp_uint32_t *raw_press) +retval_t BMP390_read_raw_press(void *p_spi, spp_uint32_t *raw_press) { retval_t ret; spp_uint8_t buf[9] = { - (spp_uint8_t)(READ_OP | (BMP390_PRESS_RAW_REG + 0)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_RAW_REG + 1)), EMPTY_MESSAGE, EMPTY_MESSAGE, - (spp_uint8_t)(READ_OP | (BMP390_PRESS_RAW_REG + 2)), EMPTY_MESSAGE, EMPTY_MESSAGE - }; + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_RAW_REG + 0)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_RAW_REG + 1)), K_WRITE_OP, K_WRITE_OP, + (spp_uint8_t)(K_READ_OP | (K_BMP390_PRESS_RAW_REG + 2)), K_WRITE_OP, K_WRITE_OP}; ret = SPP_HAL_SPI_Transmit(p_spi, buf, sizeof(buf)); - if (ret != SPP_OK) { + if (ret != SPP_OK) + { return ret; } spp_uint8_t xlsb = buf[2]; - spp_uint8_t lsb = buf[5]; - spp_uint8_t msb = buf[8]; + spp_uint8_t lsb = buf[5]; + spp_uint8_t msb = buf[8]; - *raw_press = ((spp_uint32_t)msb << 16) | ((spp_uint32_t)lsb << 8) | (spp_uint32_t)xlsb; + *raw_press = ((spp_uint32_t)msb << 16) | ((spp_uint32_t)lsb << 8) | (spp_uint32_t)xlsb; return ret; } @@ -476,22 +470,22 @@ retval_t bmp390_read_raw_press(void *p_spi, spp_uint32_t *raw_press) * * @return Compensated pressure value (float) in Pascal. */ -float bmp390_compensate_pressure(spp_uint32_t raw_press, float t_lin, bmp390_press_params_t *p) +float BMP390_compensate_pressure(spp_uint32_t raw_press, float t_lin, BMP390_press_params_t *p) { partial_data1 = p->PAR_P6 * t_lin; partial_data2 = p->PAR_P7 * (t_lin * t_lin); partial_data3 = p->PAR_P8 * (t_lin * t_lin * t_lin); - partial_out1 = p->PAR_P5 + partial_data1 + partial_data2 + partial_data3; + partial_out1 = p->PAR_P5 + partial_data1 + partial_data2 + partial_data3; partial_data1 = p->PAR_P2 * t_lin; partial_data2 = p->PAR_P3 * (t_lin * t_lin); partial_data3 = p->PAR_P4 * (t_lin * t_lin * t_lin); - partial_out2 = raw_press * (p->PAR_P1 + partial_data1 + partial_data2 + partial_data3); + partial_out2 = raw_press * (p->PAR_P1 + partial_data1 + partial_data2 + partial_data3); - partial_data1 = raw_press * raw_press; - partial_data2 = p->PAR_P9 + p->PAR_P10 * t_lin; - partial_data3 = partial_data1 * partial_data2; - partial_data4 = partial_data3 + (raw_press * raw_press * raw_press) * p->PAR_P11; + partial_data1 = raw_press * raw_press; + partial_data2 = p->PAR_P9 + p->PAR_P10 * t_lin; + partial_data3 = partial_data1 * partial_data2; + partial_data4 = partial_data3 + (raw_press * raw_press * raw_press) * p->PAR_P11; comp_press = partial_out1 + partial_out2 + partial_data4; @@ -510,16 +504,19 @@ float bmp390_compensate_pressure(spp_uint32_t raw_press, float t_lin, bmp390_pre * * @return retval_t Status code indicating success or failure of the SPI transmission. */ -retval_t bmp390_aux_get_press(void *p_spi, const bmp390_press_params_t *press_params, float t_lin, spp_uint32_t *raw_press, float *comp_press) +retval_t BMP390_aux_get_press(void *p_spi, const BMP390_press_params_t *press_params, float t_lin, + spp_uint32_t *raw_press, float *comp_press) { retval_t ret; - ret = bmp390_read_raw_press(p_spi, raw_press); - if (ret != SPP_OK) { + ret = BMP390_read_raw_press(p_spi, raw_press); + if (ret != SPP_OK) + { return ret; } - *comp_press = bmp390_compensate_pressure(*raw_press, t_lin, (bmp390_press_params_t*)press_params); + *comp_press = + BMP390_compensate_pressure(*raw_press, t_lin, (BMP390_press_params_t *)press_params); return ret; } @@ -541,19 +538,21 @@ retval_t bmp390_aux_get_press(void *p_spi, const bmp390_press_params_t *press_pa * * @return retval_t Status code indicating success or failure of the SPI transmission. */ -retval_t bmp390_get_altitude(void *p_spi, bmp_data_t *p_bmp, float *altitude_m, float *pressure_pa, float *temperature_c) +retval_t BMP390_getAltitude(void *p_spi, BMP390_Data_t *p_bmp, float *altitude_m, + float *pressure_pa, float *temperature_c) { (void)p_bmp; // DRDY wait is handled outside - if ((p_spi == NULL) || (altitude_m == NULL) || (pressure_pa == NULL) || (temperature_c == NULL)) { + if ((p_spi == NULL) || (altitude_m == NULL) || (pressure_pa == NULL) || (temperature_c == NULL)) + { return SPP_ERROR_NULL_POINTER; } retval_t ret; static spp_bool_t s_inited = false; - static bmp390_temp_params_t temp_params_static; - static bmp390_press_params_t press_params_static; + static BMP390_temp_params_t temp_params_static; + static BMP390_press_params_t press_params_static; static spp_uint32_t raw_temp_static; static spp_uint32_t raw_press_static; @@ -562,26 +561,30 @@ retval_t bmp390_get_altitude(void *p_spi, bmp_data_t *p_bmp, float *altitude_m, if (s_inited == false) { - ret = bmp390_calibrate_temp_params(p_spi, &temp_params_static); - if (ret != SPP_OK) return ret; + ret = BMP390_calibrate_temp_params(p_spi, &temp_params_static); + if (ret != SPP_OK) + return ret; - ret = bmp390_calibrate_press_params(p_spi, &press_params_static); - if (ret != SPP_OK) return ret; + ret = BMP390_calibrate_press_params(p_spi, &press_params_static); + if (ret != SPP_OK) + return ret; s_inited = true; } // No wait here, DRDY is managed by caller task - ret = bmp390_aux_get_temp(p_spi, &temp_params_static, &raw_temp_static, &t_lin); - if (ret != SPP_OK) return ret; + ret = BMP390_aux_get_temp(p_spi, &temp_params_static, &raw_temp_static, &t_lin); + if (ret != SPP_OK) + return ret; - ret = bmp390_aux_get_press(p_spi, &press_params_static, t_lin, &raw_press_static, &comp_press); - if (ret != SPP_OK) return ret; + ret = BMP390_aux_get_press(p_spi, &press_params_static, t_lin, &raw_press_static, &comp_press); + if (ret != SPP_OK) + return ret; *temperature_c = t_lin; - *pressure_pa = comp_press; - *altitude_m = 44330.0f * (1.0f - powf(comp_press / 101325.0f, 1.0f / 5.255f)); + *pressure_pa = comp_press; + *altitude_m = 44330.0f * (1.0f - powf(comp_press / 101325.0f, 1.0f / 5.255f)); return SPP_OK; } @@ -590,16 +593,18 @@ retval_t bmp390_get_altitude(void *p_spi, bmp_data_t *p_bmp, float *altitude_m, * Interrupt Configuration * ---------------------------------------------------------------- */ -/** @copydoc bmp390_int_enable_drdy */ -retval_t bmp390_int_enable_drdy(void *p_spi) +/** @copydoc BMP390_intEnableDrdy */ +retval_t BMP390_intEnableDrdy(void *p_spi) { retval_t ret; - spp_uint8_t buf[2] = { BMP390_REG_INT_CTRL, (spp_uint8_t)(BMP390_INT_CTRL_LEVEL | BMP390_INT_CTRL_DRDY_EN) }; // NO latch + spp_uint8_t buf[2] = { + K_BMP390_REG_INT_CTRL, + (spp_uint8_t)(K_BMP390_INT_CTRL_LEVEL | K_BMP390_INT_CTRL_DRDY_EN)}; // NO latch ret = SPP_HAL_SPI_Transmit(p_spi, buf, sizeof(buf)); - if (ret != SPP_OK) { + if (ret != SPP_OK) + { return ret; } return ret; } - diff --git a/solaris-v1/components/pressure_sensor_driver/include/bmp390.h b/solaris-v1/components/pressureSensorDriver/include/bmp390.h similarity index 60% rename from solaris-v1/components/pressure_sensor_driver/include/bmp390.h rename to solaris-v1/components/pressureSensorDriver/include/bmp390.h index dc6fd7c..6714d80 100644 --- a/solaris-v1/components/pressure_sensor_driver/include/bmp390.h +++ b/solaris-v1/components/pressureSensorDriver/include/bmp390.h @@ -1,5 +1,5 @@ /** - * @file bmp390.h + * @file BMP390.h * @brief BMP390 barometric pressure sensor driver. * * Provides SPI-based communication with the Bosch BMP390 sensor for reading @@ -7,10 +7,10 @@ * interrupt via GPIO event groups. * * The driver follows a three-step workflow: - * 1. Initialise and configure the sensor (BmpInit, bmp390_aux_config, - * bmp390_prepare_measure). - * 2. Wait for the data-ready interrupt (bmp390_wait_drdy) or poll status. - * 3. Read compensated altitude/pressure/temperature (bmp390_get_altitude). + * 1. Initialise and configure the sensor (BMP390_init, BMP390_auxConfig, + * BMP390_prepareMeasure). + * 2. Wait for the data-ready interrupt (BMP390_waitDrdy) or poll status. + * 3. Read compensated altitude/pressure/temperature (BMP390_getAltitude). */ #ifndef BMP390_H @@ -24,23 +24,10 @@ #include "hal/gpio/gpio.h" #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -/* ============================================================================ - * SPI Protocol Constants - * ========================================================================= */ - -/** - * @brief SPI read operation flag (bit 7 set). - */ -#define READ_OP 0x80 - -/** - * @brief Empty byte used as padding in SPI transactions. - */ -#define EMPTY_MESSAGE 0x00 - /* ============================================================================ * Initialisation Constants * ========================================================================= */ @@ -48,55 +35,56 @@ extern "C" { /** * @brief FreeRTOS task priority for the BMP390 initialisation task. */ -#define BMP_INIT_PRIO 4 +#define K_BMP_INIT_PRIO 4 /** * @brief Stack size in bytes for the BMP390 initialisation task. */ -#define BMP_INIT_TASK_STACK_SIZE 4096 +#define K_BMP_INIT_TASK_STACK_SIZE 4096 /** * @brief Event group bit indicating BMP390 data-ready. */ -#define BMP390_EVT_DRDY (1u << 0) +#define K_BMP390_EVT_DRDY (1u << 0) -/* ============================================================================ + /* ============================================================================ * Data Types * ========================================================================= */ -/** + /** * @brief BMP390 device context. * * Groups the SPI handler, event group, ISR context and GPIO interrupt * configuration required to operate one BMP390 sensor instance. */ -typedef struct { - void* p_handler_spi; /**< SPI device handle. */ - void* p_event_group; /**< Event group for DRDY signalling. */ - spp_gpio_isr_ctx_t isr_ctx; /**< ISR context (event group + bits). */ - spp_uint32_t int_pin; /**< GPIO pin number for the interrupt. */ - spp_uint32_t int_intr_type; /**< Interrupt trigger type. */ - spp_uint32_t int_pull; /**< Pull resistor: 0=none, 1=up, 2=down. */ -} bmp_data_t; + typedef struct + { + void *p_handler_spi; /**< SPI device handle. */ + void *p_event_group; /**< Event group for DRDY signalling. */ + spp_gpio_isr_ctx_t isr_ctx; /**< ISR context (event group + bits). */ + spp_uint32_t intPin; /**< GPIO pin number for the interrupt. */ + spp_uint32_t intIntrType; /**< Interrupt trigger type. */ + spp_uint32_t intPull; /**< Pull resistor: 0=none, 1=up, 2=down. */ + } BMP390_Data_t; /** * @brief Chip-select GPIO pin for the BMP390. */ -#define PIN_NUM_CS 18 +#define K_BMP390_PIN_NUM_CS 18 -/* ============================================================================ + /* ============================================================================ * Initialisation * ========================================================================= */ -/** + /** * @brief Initialises the BMP390 driver. * * Creates the event group, configures the GPIO interrupt and registers the * ISR for data-ready signalling. * - * @param[in,out] p_data Pointer to a bmp_data_t context to populate. + * @param[in,out] p_data Pointer to a BMP390_Data_t context to populate. */ -void BmpInit(void* p_data); + void BMP390_init(void *p_data); /* ============================================================================ * Configuration and Identity Check @@ -105,17 +93,17 @@ void BmpInit(void* p_data); /** * @brief BMP390 chip ID register address. */ -#define BMP390_CHIP_ID_REG 0x00 +#define K_BMP390_CHIP_ID_REG 0x00 /** * @brief Expected value of the BMP390 chip ID register. */ -#define BMP390_CHIP_ID_VALUE 0x60 +#define K_BMP390_CHIP_ID_VALUE 0x60 /** * @brief BMP390 soft-reset command register address. */ -#define BMP390_SOFT_RESET_REG 0x7E +#define K_BMP390_SOFT_RESET_REG 0x7E /** * @brief Command byte that triggers a BMP390 soft reset. @@ -125,142 +113,144 @@ void BmpInit(void* p_data); /** * @brief BMP390 interface configuration register address. */ -#define BMP390_IF_CONF_REG 0x1A +#define K_BMP390_IF_CONF_REG 0x1A /** * @brief Value to select SPI 4-wire mode in IF_CONF. */ -#define BMP390_IF_CONF_SPI 0x00 +#define BMP390_IF_CONF_SPI 0x00 -/** + /** * @brief Performs a soft reset of the BMP390. * * @param[in] p_spi SPI device handle. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_soft_reset(void *p_spi); + retval_t BMP390_soft_reset(void *p_spi); -/** + /** * @brief Enables SPI 4-wire mode on the BMP390. * * @param[in] p_spi SPI device handle. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_enable_spi_mode(void *p_spi); + retval_t BMP390_enable_spi_mode(void *p_spi); -/** + /** * @brief Reads and verifies the BMP390 chip ID. * * @param[in] p_spi SPI device handle. * @return SPP_OK if chip ID matches 0x60, SPP_ERROR otherwise. */ -retval_t bmp390_config_check(void *p_spi); + retval_t BMP390_config_check(void *p_spi); /* ============================================================================ * Measurement Configuration * ========================================================================= */ /** @brief Power control register address. */ -#define BMP390_REG_PWRCTRL 0x1B +#define K_BMP390_REG_PWRCTRL 0x1B /** @brief Power control value: normal mode, pressure + temperature enabled. */ -#define BMP390_VALUE_PWRCTRL 0x33 +#define BMP390_VALUE_PWRCTRL 0x33 /** @brief Oversampling settings register address. */ -#define BMP390_REG_OSR 0x1C +#define K_BMP390_REG_OSR 0x1C /** @brief Oversampling value (no oversampling, +/- 0.2 m accuracy). */ -#define BMP390_VALUE_OSR 0x00 +#define BMP390_VALUE_OSR 0x00 /** @brief Output data rate register address. */ -#define BMP390_REG_ODR 0x1D +#define K_BMP390_REG_ODR 0x1D /** @brief ODR value for 50 Hz output. */ -#define BMP390_VALUE_ODR 0x02 +#define BMP390_VALUE_ODR 0x02 /** @brief IIR filter coefficient register address. */ -#define BMP390_REG_IIR 0x1F +#define K_BMP390_REG_IIR 0x1F /** @brief IIR filter coefficient 1. */ -#define BMP390_VALUE_IIR 0x02 +#define BMP390_VALUE_IIR 0x02 -/** + /** * @brief Configures measurement registers (OSR, ODR, IIR, power control). * * @param[in] p_spi SPI device handle. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_prepare_measure(void* p_spi); + retval_t BMP390_prepareMeasure(void *p_spi); /** @brief Status register address. */ -#define BMP390_REG_STATUS 0x03 +#define K_BMP390_REG_STATUS 0x03 /** @brief Status bit: temperature data ready. */ -#define BMP390_STATUS_DRDY_TEMP 0x40 +#define K_BMP390_STATUS_DRDY_TEMP 0x40 /** @brief Status bit: pressure data ready. */ -#define BMP390_STATUS_DRDY_PRES 0x20 +#define BMP390_STATUS_DRDY_PRES 0x20 -/** + /** * @brief Blocks until the BMP390 data-ready interrupt fires. * * @param[in] p_bmp Pointer to BMP390 device context. * @param[in] timeout_ms Maximum wait time in milliseconds (0 = forever). * @return SPP_OK if data-ready was signalled, error code on timeout. */ -retval_t bmp390_wait_drdy(bmp_data_t* p_bmp, spp_uint32_t timeout_ms); + retval_t BMP390_waitDrdy(BMP390_Data_t *p_bmp, spp_uint32_t timeout_ms); /* ============================================================================ * Temperature Calibration and Reading * ========================================================================= */ /** @brief Start address of temperature calibration registers. */ -#define BMP390_TEMP_CALIB_REG_START 0x31 +#define K_BMP390_TEMP_CALIB_REG_START 0x31 -/** + /** * @brief Raw temperature calibration coefficients (as read from the sensor). */ -typedef struct { - uint16_t par_t1; /**< Calibration coefficient T1 (unsigned 16-bit). */ - int16_t par_t2; /**< Calibration coefficient T2 (signed 16-bit). */ - int8_t par_t3; /**< Calibration coefficient T3 (signed 8-bit). */ - float t_lin; /**< Linearised temperature used for pressure comp. */ -} bmp390_temp_calib_t; + typedef struct + { + uint16_t par_t1; /**< Calibration coefficient T1 (unsigned 16-bit). */ + int16_t par_t2; /**< Calibration coefficient T2 (signed 16-bit). */ + int8_t par_t3; /**< Calibration coefficient T3 (signed 8-bit). */ + float t_lin; /**< Linearised temperature used for pressure comp. */ + } BMP390_temp_calib_t; -/** + /** * @brief Reads raw temperature calibration coefficients via SPI. * * @param[in] p_spi SPI device handle. * @param[out] tcalib Structure to receive the raw coefficients. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_read_raw_temp_coeffs(void *p_spi, bmp390_temp_calib_t *tcalib); + retval_t BMP390_read_raw_temp_coeffs(void *p_spi, BMP390_temp_calib_t *tcalib); -/** + /** * @brief Scaled temperature calibration parameters used for compensation. */ -typedef struct { - float PAR_T1; /**< Scaled T1 = raw_t1 * 2^8. */ - float PAR_T2; /**< Scaled T2 = raw_t2 / 2^30. */ - float PAR_T3; /**< Scaled T3 = raw_t3 / 2^48. */ -} bmp390_temp_params_t; + typedef struct + { + float PAR_T1; /**< Scaled T1 = raw_t1 * 2^8. */ + float PAR_T2; /**< Scaled T2 = raw_t2 / 2^30. */ + float PAR_T3; /**< Scaled T3 = raw_t3 / 2^48. */ + } BMP390_temp_params_t; -/** + /** * @brief Reads and scales temperature calibration parameters. * * @param[in] p_spi SPI device handle. * @param[out] out Structure to receive scaled parameters. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_calibrate_temp_params(void *p_spi, bmp390_temp_params_t *out); + retval_t BMP390_calibrate_temp_params(void *p_spi, BMP390_temp_params_t *out); /** @brief Start address of raw temperature data registers (24-bit). */ -#define BMP390_TEMP_RAW_REG 0x07 +#define K_BMP390_TEMP_RAW_REG 0x07 -/** + /** * @brief Reads the 24-bit raw temperature value from the sensor. * * @param[in] p_spi SPI device handle. * @param[out] raw_temp Receives the 24-bit raw ADC value. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_read_raw_temp(void *p_spi, uint32_t *raw_temp); + retval_t BMP390_read_raw_temp(void *p_spi, uint32_t *raw_temp); -/** + /** * @brief Compensates a raw temperature reading. * * Applies the BMP390 datasheet compensation formula using the scaled @@ -270,100 +260,102 @@ retval_t bmp390_read_raw_temp(void *p_spi, uint32_t *raw_temp); * @param[in] params Scaled calibration parameters. * @return Compensated temperature in degrees Celsius. */ -float bmp390_compensate_temperature(spp_uint32_t raw_temp, bmp390_temp_params_t *params); + float BMP390_compensate_temperature(spp_uint32_t raw_temp, BMP390_temp_params_t *params); /* ============================================================================ * Pressure Calibration and Reading * ========================================================================= */ /** @brief Start address of pressure calibration registers. */ -#define BMP390_PRESS_CALIB_REG_START 0x36 +#define K_BMP390_PRESS_CALIB_REG_START 0x36 -/** + /** * @brief Raw pressure calibration coefficients (as read from the sensor). */ -typedef struct { - spp_uint16_t par_p1; /**< Coefficient P1 (unsigned 16-bit). */ - spp_uint16_t par_p2; /**< Coefficient P2 (unsigned 16-bit). */ - spp_int8_t par_p3; /**< Coefficient P3 (signed 8-bit). */ - spp_int8_t par_p4; /**< Coefficient P4 (signed 8-bit). */ - spp_uint16_t par_p5; /**< Coefficient P5 (unsigned 16-bit). */ - spp_uint16_t par_p6; /**< Coefficient P6 (unsigned 16-bit). */ - spp_int8_t par_p7; /**< Coefficient P7 (signed 8-bit). */ - spp_int8_t par_p8; /**< Coefficient P8 (signed 8-bit). */ - spp_int16_t par_p9; /**< Coefficient P9 (signed 16-bit). */ - spp_int8_t par_p10; /**< Coefficient P10 (signed 8-bit). */ - spp_int8_t par_p11; /**< Coefficient P11 (signed 8-bit). */ -} bmp390_press_calib_t; - -/** + typedef struct + { + spp_uint16_t par_p1; /**< Coefficient P1 (unsigned 16-bit). */ + spp_uint16_t par_p2; /**< Coefficient P2 (unsigned 16-bit). */ + spp_int8_t par_p3; /**< Coefficient P3 (signed 8-bit). */ + spp_int8_t par_p4; /**< Coefficient P4 (signed 8-bit). */ + spp_uint16_t par_p5; /**< Coefficient P5 (unsigned 16-bit). */ + spp_uint16_t par_p6; /**< Coefficient P6 (unsigned 16-bit). */ + spp_int8_t par_p7; /**< Coefficient P7 (signed 8-bit). */ + spp_int8_t par_p8; /**< Coefficient P8 (signed 8-bit). */ + spp_int16_t par_p9; /**< Coefficient P9 (signed 16-bit). */ + spp_int8_t par_p10; /**< Coefficient P10 (signed 8-bit). */ + spp_int8_t par_p11; /**< Coefficient P11 (signed 8-bit). */ + } BMP390_press_calib_t; + + /** * @brief Reads raw pressure calibration coefficients via SPI. * * @param[in] p_spi SPI device handle. * @param[out] pcalib Structure to receive the raw coefficients. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_read_raw_press_coeffs(void *p_spi, bmp390_press_calib_t *pcalib); + retval_t BMP390_read_raw_press_coeffs(void *p_spi, BMP390_press_calib_t *pcalib); -/** + /** * @brief Scaled pressure calibration parameters used for compensation. * * Each field is computed from the corresponding raw coefficient using the * scaling factors documented in the BMP390 datasheet. */ -typedef struct { - float PAR_P1; /**< Scaled P1 = (raw - 2^14) / 2^20. */ - float PAR_P2; /**< Scaled P2 = (raw - 2^14) / 2^29. */ - float PAR_P3; /**< Scaled P3 = raw / 2^32. */ - float PAR_P4; /**< Scaled P4 = raw / 2^37. */ - float PAR_P5; /**< Scaled P5 = raw * 2^3. */ - float PAR_P6; /**< Scaled P6 = raw / 2^6. */ - float PAR_P7; /**< Scaled P7 = raw / 2^8. */ - float PAR_P8; /**< Scaled P8 = raw / 2^15. */ - float PAR_P9; /**< Scaled P9 = raw / 2^48. */ - float PAR_P10; /**< Scaled P10 = raw / 2^48. */ - float PAR_P11; /**< Scaled P11 = raw / 2^65. */ -} bmp390_press_params_t; - -/** + typedef struct + { + float PAR_P1; /**< Scaled P1 = (raw - 2^14) / 2^20. */ + float PAR_P2; /**< Scaled P2 = (raw - 2^14) / 2^29. */ + float PAR_P3; /**< Scaled P3 = raw / 2^32. */ + float PAR_P4; /**< Scaled P4 = raw / 2^37. */ + float PAR_P5; /**< Scaled P5 = raw * 2^3. */ + float PAR_P6; /**< Scaled P6 = raw / 2^6. */ + float PAR_P7; /**< Scaled P7 = raw / 2^8. */ + float PAR_P8; /**< Scaled P8 = raw / 2^15. */ + float PAR_P9; /**< Scaled P9 = raw / 2^48. */ + float PAR_P10; /**< Scaled P10 = raw / 2^48. */ + float PAR_P11; /**< Scaled P11 = raw / 2^65. */ + } BMP390_press_params_t; + + /** * @brief Reads and scales pressure calibration parameters. * * @param[in] p_spi SPI device handle. * @param[out] out Structure to receive scaled parameters. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_calibrate_press_params(void *p_spi, bmp390_press_params_t *out); + retval_t BMP390_calibrate_press_params(void *p_spi, BMP390_press_params_t *out); /** @brief Start address of raw pressure data registers (24-bit). */ -#define BMP390_PRESS_RAW_REG 0x04 +#define K_BMP390_PRESS_RAW_REG 0x04 -/** + /** * @brief Reads the 24-bit raw pressure value from the sensor. * * @param[in] p_spi SPI device handle. * @param[out] raw_press Receives the 24-bit raw ADC value. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_read_raw_press(void *p_spi, spp_uint32_t *raw_press); + retval_t BMP390_read_raw_press(void *p_spi, spp_uint32_t *raw_press); -/** + /** * @brief Compensates a raw pressure reading. * * Applies the BMP390 datasheet 11-coefficient polynomial compensation * using the linearised temperature value. * * @param[in] raw_press Raw 24-bit ADC value. - * @param[in] t_lin Linearised temperature from bmp390_compensate_temperature(). + * @param[in] t_lin Linearised temperature from BMP390_compensate_temperature(). * @param[in] p Scaled calibration parameters. * @return Compensated pressure in Pascal. */ -float bmp390_compensate_pressure(spp_uint32_t raw_press, float t_lin, bmp390_press_params_t *p); + float BMP390_compensate_pressure(spp_uint32_t raw_press, float t_lin, BMP390_press_params_t *p); -/* ============================================================================ + /* ============================================================================ * Altitude Calculation * ========================================================================= */ -/** + /** * @brief Reads sensors and computes barometric altitude. * * Performs the full read-compensate-calculate pipeline in one call: @@ -379,24 +371,25 @@ float bmp390_compensate_pressure(spp_uint32_t raw_press, float t_lin, bmp390_pre * @return SPP_OK on success, SPP_ERROR_NULL_POINTER if any output pointer * is NULL, or an SPI error code. */ -retval_t bmp390_get_altitude(void *p_spi, bmp_data_t *p_bmp, float *altitude_m, float *pressure_pa, float *temperature_c); + retval_t BMP390_getAltitude(void *p_spi, BMP390_Data_t *p_bmp, float *altitude_m, + float *pressure_pa, float *temperature_c); -/* ============================================================================ + /* ============================================================================ * Auxiliary Helpers * ========================================================================= */ -/** + /** * @brief Performs soft reset, SPI mode enable and chip ID check. * - * Convenience function that chains bmp390_soft_reset(), - * bmp390_enable_spi_mode() and bmp390_config_check(). + * Convenience function that chains BMP390_soft_reset(), + * BMP390_enable_spi_mode() and BMP390_config_check(). * * @param[in] p_spi SPI device handle. * @return SPP_OK on success, first failing error code otherwise. */ -retval_t bmp390_aux_config(void *p_spi); + retval_t BMP390_auxConfig(void *p_spi); -/** + /** * @brief Reads and compensates temperature in one call. * * @param[in] p_spi SPI device handle. @@ -405,9 +398,10 @@ retval_t bmp390_aux_config(void *p_spi); * @param[out] comp_temp Compensated temperature in degrees Celsius. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_aux_get_temp(void *p_spi, const bmp390_temp_params_t *temp_params, spp_uint32_t *raw_temp, float *comp_temp); + retval_t BMP390_aux_get_temp(void *p_spi, const BMP390_temp_params_t *temp_params, + spp_uint32_t *raw_temp, float *comp_temp); -/** + /** * @brief Reads and compensates pressure in one call. * * @param[in] p_spi SPI device handle. @@ -417,20 +411,21 @@ retval_t bmp390_aux_get_temp(void *p_spi, const bmp390_temp_params_t *temp_param * @param[out] comp_press Compensated pressure in Pascal. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_aux_get_press(void *p_spi, const bmp390_press_params_t *press_params, float t_lin, spp_uint32_t *raw_press, float *comp_press); + retval_t BMP390_aux_get_press(void *p_spi, const BMP390_press_params_t *press_params, + float t_lin, spp_uint32_t *raw_press, float *comp_press); /* ============================================================================ * Interrupt Configuration * ========================================================================= */ /** @brief Interrupt control register address. */ -#define BMP390_REG_INT_CTRL 0x19 +#define K_BMP390_REG_INT_CTRL 0x19 /** @brief INT_CTRL bit: enable data-ready interrupt output. */ -#define BMP390_INT_CTRL_DRDY_EN 0x40 +#define K_BMP390_INT_CTRL_DRDY_EN 0x40 /** @brief INT_CTRL bit: active-high interrupt level. */ -#define BMP390_INT_CTRL_LEVEL 0x02 +#define K_BMP390_INT_CTRL_LEVEL 0x02 -/** + /** * @brief Enables the data-ready interrupt output on the BMP390 INT pin. * * Configures active-high, non-latched DRDY interrupt. @@ -438,7 +433,7 @@ retval_t bmp390_aux_get_press(void *p_spi, const bmp390_press_params_t *press_pa * @param[in] p_spi SPI device handle. * @return SPP_OK on success, error code otherwise. */ -retval_t bmp390_int_enable_drdy(void *p_spi); + retval_t BMP390_intEnableDrdy(void *p_spi); #ifdef __cplusplus } diff --git a/solaris-v1/external/spp b/solaris-v1/external/spp index 2a72b8b..675b39c 160000 --- a/solaris-v1/external/spp +++ b/solaris-v1/external/spp @@ -1 +1 @@ -Subproject commit 2a72b8b119fdc74007cb506b2253b6809b901d44 +Subproject commit 675b39c900e39acb2d6d50b8e0d981743aba7140 diff --git a/solaris-v1/main/CMakeLists.txt b/solaris-v1/main/CMakeLists.txt index 2177095..f2c1558 100644 --- a/solaris-v1/main/CMakeLists.txt +++ b/solaris-v1/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( - SRCS "main.c" "bmp_service.c" + SRCS "main.c" "bmpService.c" INCLUDE_DIRS "." - REQUIRES general icm_driver pressure_sensor_driver datalogger_driver spp_wrapper spp_port_wrapper + REQUIRES general icm_driver pressureSensorDriver datalogger_driver spp_wrapper spp_port_wrapper ) \ No newline at end of file diff --git a/solaris-v1/main/bmp_service.c b/solaris-v1/main/bmp_service.c deleted file mode 100644 index 9b64a72..0000000 --- a/solaris-v1/main/bmp_service.c +++ /dev/null @@ -1,211 +0,0 @@ -#include "bmp_service.h" - -#include "bmp390.h" -#include "gpio_int.h" - -#include "services/databank/databank.h" -#include "services/db_flow/db_flow.h" - -#include "osal/task.h" -#include "spp_log.h" - -#include - -static const char* TAG = "BMP_SERVICE"; - -#define BMP_APID_DBG 0x0101 -#define BMP_TASK_PRIO 5 -#define BMP_TASK_DELAY_MS 200 - -static void* s_spi = NULL; -static bmp_data_t s_bmp; -static spp_uint16_t s_seq = 0; - -static void log_packet_basic(const char* prefix, const spp_packet_t* pkt) -{ - if (pkt == NULL) { - SPP_LOGE(TAG, "%s pkt=NULL", prefix); - return; - } - - SPP_LOGI(TAG, "%s pkt=%p ver=%u apid=0x%04X seq=%u len=%u crc=%u", - prefix, - (void*)pkt, - (unsigned)pkt->primary_header.version, - (unsigned)pkt->primary_header.apid, - (unsigned)pkt->primary_header.seq, - (unsigned)pkt->primary_header.payload_len, - (unsigned)pkt->crc); -} - -static void log_packet_payload_floats(const char* prefix, const spp_packet_t* pkt) -{ - if (pkt == NULL) return; - - if (pkt->primary_header.payload_len < 12u) { - SPP_LOGI(TAG, "%s payload too small len=%u", prefix, (unsigned)pkt->primary_header.payload_len); - return; - } - - float alt = 0.0f; - float p = 0.0f; - float t = 0.0f; - - memcpy(&alt, &pkt->payload[0], sizeof(float)); - memcpy(&p, &pkt->payload[4], sizeof(float)); - memcpy(&t, &pkt->payload[8], sizeof(float)); - - SPP_LOGI(TAG, "%s payload alt=%.2f p=%.2f t=%.2f", prefix, alt, p, t); -} - -static void bmp_service_task(void* arg) -{ - (void)arg; - - SPP_LOGI(TAG, "Task start"); - - for (;;) - { - // Wait DRDY - retval_t ret = bmp390_wait_drdy(&s_bmp, 5000); - if (ret != SPP_OK) { - SPP_LOGE(TAG, "DRDY wait failed ret=%d", (int)ret); - continue; - } - SPP_LOGI(TAG, "DRDY received"); - - // Get free packet - SPP_LOGI(TAG, "Requesting free packet..."); - spp_packet_t* pkt = SPP_DATABANK_getPacket(); - if (pkt == NULL) { - SPP_LOGI(TAG, "No free packet"); - continue; - } - SPP_LOGI(TAG, "Got free packet ptr=%p", (void*)pkt); - log_packet_basic("FREE_PKT", pkt); - - // Read BMP measures - float alt = 0.0f; - float p = 0.0f; - float t = 0.0f; - - ret = bmp390_get_altitude(s_spi, &s_bmp, &alt, &p, &t); - if (ret != SPP_OK) { - SPP_LOGE(TAG, "bmp390_get_altitude failed ret=%d -> return packet", (int)ret); - (void)SPP_DATABANK_returnPacket(pkt); - continue; - } - - SPP_LOGI(TAG, "BMP read alt=%.2f p=%.2f t=%.2f", alt, p, t); - - // Fill packet header (CRC=0) - pkt->primary_header.version = SPP_PKT_VERSION; - pkt->primary_header.apid = BMP_APID_DBG; - pkt->primary_header.seq = s_seq++; - pkt->primary_header.payload_len = 12u; - - // Secondary header unused for debug - pkt->secondary_header.timestamp_ms = 0; - pkt->secondary_header.drop_counter = 0; - - pkt->crc = 0; - - // Fill payload with floats (no encoding) - memset(pkt->payload, 0, SPP_PKT_PAYLOAD_MAX); - memcpy(&pkt->payload[0], &alt, sizeof(float)); - memcpy(&pkt->payload[4], &p, sizeof(float)); - memcpy(&pkt->payload[8], &t, sizeof(float)); - - log_packet_basic("FILLED", pkt); - log_packet_payload_floats("FILLED", pkt); - - // Publish to ready FIFO - SPP_LOGI(TAG, "Publishing to DB_FLOW..."); - ret = DB_FLOW_PushReady(pkt); - if (ret != SPP_OK) { - SPP_LOGE(TAG, "DB_FLOW_PushReady failed ret=%d -> return packet", (int)ret); - (void)SPP_DATABANK_returnPacket(pkt); - continue; - } - SPP_LOGI(TAG, "Published ok ready=%lu", (unsigned long)DB_FLOW_ReadyCount()); - - // Simulated consumer pop - SPP_LOGI(TAG, "Consumer pop..."); - spp_packet_t* pkt_rx = NULL; - ret = DB_FLOW_PopReady(&pkt_rx); - if ((ret != SPP_OK) || (pkt_rx == NULL)) { - SPP_LOGE(TAG, "DB_FLOW_PopReady failed ret=%d pkt_rx=%p", (int)ret, (void*)pkt_rx); - continue; - } - - SPP_LOGI(TAG, "Consumer got ptr=%p ready=%lu", (void*)pkt_rx, (unsigned long)DB_FLOW_ReadyCount()); - log_packet_basic("RECEIVED", pkt_rx); - log_packet_payload_floats("RECEIVED", pkt_rx); - - // Return to databank (databank clears it) - SPP_LOGI(TAG, "Returning packet to databank..."); - ret = SPP_DATABANK_returnPacket(pkt_rx); - if (ret != SPP_OK) { - SPP_LOGE(TAG, "SPP_DATABANK_returnPacket failed ret=%d", (int)ret); - continue; - } - - // Databank does memset(pkt, 0) - log_packet_basic("AFTER_RETURN", pkt_rx); - log_packet_payload_floats("AFTER_RETURN", pkt_rx); - - SPP_OSAL_TaskDelay(BMP_TASK_DELAY_MS); - } -} - -retval_t BMP_ServiceInit(void* p_spi_bmp) -{ - if (p_spi_bmp == NULL) return SPP_ERROR_NULL_POINTER; - - s_spi = p_spi_bmp; - - // Setup ISR context - s_bmp.int_pin = (spp_uint32_t)INT_GPIO; - s_bmp.int_intr_type = 1; - s_bmp.int_pull = 0; - BmpInit(&s_bmp); - - // BMP config - retval_t ret = bmp390_aux_config(s_spi); - if (ret != SPP_OK) return ret; - - ret = bmp390_prepare_measure(s_spi); - if (ret != SPP_OK) return ret; - - ret = bmp390_int_enable_drdy(s_spi); - if (ret != SPP_OK) return ret; - - return SPP_OK; -} - -retval_t BMP_ServiceStart(void) -{ - void* storage = SPP_OSAL_GetTaskStorage(); - if (storage == NULL) { - SPP_LOGE(TAG, "No task storage"); - return SPP_ERROR; - } - - // Create the task in loop - void* th = SPP_OSAL_TaskCreate( - (void*)bmp_service_task, - "bmp_service", - 4096, - NULL, - BMP_TASK_PRIO, - storage - ); - - if (th == NULL) { - SPP_LOGE(TAG, "TaskCreate failed"); - return SPP_ERROR; - } - - SPP_LOGI(TAG, "Task created"); - return SPP_OK; -} \ No newline at end of file diff --git a/solaris-v1/main/bmp_service.h b/solaris-v1/main/bmp_service.h deleted file mode 100644 index 50afec0..0000000 --- a/solaris-v1/main/bmp_service.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef BMP_SERVICE_H -#define BMP_SERVICE_H - -#include "core/returntypes.h" - -retval_t BMP_ServiceInit(void* p_spi_bmp); -retval_t BMP_ServiceStart(void); - -#endif \ No newline at end of file diff --git a/solaris-v1/main/main.c b/solaris-v1/main/main.c index fe1aa75..c258e74 100644 --- a/solaris-v1/main/main.c +++ b/solaris-v1/main/main.c @@ -1,4 +1,4 @@ -#include "core/core.h" +#include "core.h" #include "icm20948.h" #include "spi.h" @@ -11,7 +11,7 @@ #include "services/databank/databank.h" #include "services/db_flow/db_flow.h" -#include "bmp_service.h" +#include "bmpService.h" #include