|
| 1 | +#ifndef ADAPTIVESENSORHW_HPP_ |
| 2 | +#define ADAPTIVESENSORHW_HPP_ |
| 3 | + |
| 4 | +#include "AdaptiveSensor.hpp" |
| 5 | +#include "GpioDriver.hpp" |
| 6 | + |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include "usart.h" |
| 10 | + |
| 11 | +#define ADAPT_ADDRESS 1 |
| 12 | + |
| 13 | +#define ADAPT_CMD_GET_SW_VERSION 1 |
| 14 | +#define ADAPT_CMD_GET_HW_VERSION 2 |
| 15 | +#define ADAPT_CMD_GET_ARTICLE 10 |
| 16 | +#define ADAPT_CMD_BOARD_RESET 11 |
| 17 | +#define ADAPT_CMD_HARD_RESET 12 |
| 18 | +#define ADAPT_CMD_SOFT_RESET 13 |
| 19 | +#define ADAPT_CMD_GET_FLOW_MEAS 16 |
| 20 | + |
| 21 | +// pre-computed CRC8 crcs for the commands |
| 22 | +#define ADAPT_CRC_GET_SW_VERSION 0xB2 |
| 23 | +#define ADAPT_CRC_GET_HW_VERSION 0x9F |
| 24 | +#define ADAPT_CRC_GET_ARTICLE 0xA8 |
| 25 | +#define ADAPT_CRC_BOARD_RESET 0x5C |
| 26 | +#define ADAPT_CRC_HARD_RESET 0xF2 |
| 27 | +#define ADAPT_CRC_SOFT_RESET 0x06 |
| 28 | +#define ADAPT_CRC_GET_FLOW_MEAS 0x28 |
| 29 | + |
| 30 | +// timeout in milliseconds when expecting a response from adaptive sensor |
| 31 | +#define ADAPT_TIMEOUT 100 |
| 32 | + |
| 33 | +// structs are packed differently in gcc vs MSVC. |
| 34 | +// MSVC could be used to compile this for unit tests. |
| 35 | +#ifdef __GNUC__ |
| 36 | +#define PACKED __attribute__((packed)) |
| 37 | +#else |
| 38 | +#define PACKED |
| 39 | +#pragma pack(push, 1) |
| 40 | +#endif |
| 41 | + |
| 42 | +struct adapt_comm_cmd { |
| 43 | + uint8_t address; // 0x01 |
| 44 | + uint8_t cmd; // per above |
| 45 | + uint8_t len; // 0 |
| 46 | + uint8_t crc; // crc8 |
| 47 | +} PACKED; |
| 48 | + |
| 49 | +#ifdef _MSC_VER |
| 50 | +#pragma pack(pop) |
| 51 | +#endif |
| 52 | + |
| 53 | +/** |
| 54 | + * @brief Hardware implementation of the AdaptiveSensor using STM32 HAL. |
| 55 | + */ |
| 56 | +class AdaptiveSensorHw : public AdaptiveSensor |
| 57 | +{ |
| 58 | + public: |
| 59 | + /** |
| 60 | + * @brief Constructor for AdaptiveSensorHw. |
| 61 | + * @param uart Pointer to the uart. |
| 62 | + * @param dePort GPIO port for RS485 drive enable. |
| 63 | + * @param dePin GPIO pin for RS485 drive enable. |
| 64 | + * |
| 65 | + * Because the schematic used the wrong pin for UART_DE, the DE pin needs to |
| 66 | + * handled manually instead of by the hal uart driver. Brkjing DE high to send |
| 67 | + * data and low to receive response. |
| 68 | + */ |
| 69 | + AdaptiveSensorHw(UART_HandleTypeDef* uart, GPIO_TypeDef* dePort, uint16_t dePin); |
| 70 | + |
| 71 | + /** |
| 72 | + * @brief Initialize the hardware adaptive sensor |
| 73 | + */ |
| 74 | + void init() override; |
| 75 | + |
| 76 | + /** |
| 77 | + * @brief Do a reset of the adaptive sensor board. Probably want this one |
| 78 | + * @return 0 on success, <0 on communication error, >0 on error returned from sensor |
| 79 | + */ |
| 80 | + int8_t boardReset() override; |
| 81 | + |
| 82 | + /** |
| 83 | + * @brief Do a hard reset of the sensor. |
| 84 | + * @return 0 on success, <0 on communication error, >0 on error returned from sensor |
| 85 | + */ |
| 86 | + int8_t hardReset() override; |
| 87 | + |
| 88 | + /** |
| 89 | + * @brief Do a soft reset of the sensor. |
| 90 | + * @return 0 on success, <0 on communication error, >0 on error returned from sensor |
| 91 | + */ |
| 92 | + int8_t softReset() override; |
| 93 | + |
| 94 | + /** |
| 95 | + * @brief Get the current flow getFlowMeasurement |
| 96 | + * @param[out] value Measured flow value in mSlm^2 (milli-standard-liters per minute) |
| 97 | + * Divide by 1000 to get slpm |
| 98 | + * @return 0 on success, <0 on communication error, >0 on error returned from sensor |
| 99 | + */ |
| 100 | + int8_t getFlowMeasurement(int32_t *value) override; |
| 101 | + |
| 102 | + /** |
| 103 | + * @brief Get the software version of the adaptive sensor |
| 104 | + * @param[out] swVersion max 9 bytes xxx.yyyc (ex: "10.32a" |
| 105 | + * @return 0 on success, <0 on communication error, >0 on error returned from sensor |
| 106 | + */ |
| 107 | + int8_t getSwVersion(char swVersion[ADAPT_SW_VER_STRLEN]) override; |
| 108 | + |
| 109 | + /** |
| 110 | + * @brief Get the hardware version of the adaptive sensor |
| 111 | + * @param[out] swVersion max 9 bytes xxx.yyyc (ex: "10.32a" |
| 112 | + * @return 0 on success, <0 on communication error, >0 on error returned from sensor |
| 113 | + */ |
| 114 | + int8_t getHwVersion(char hwVersion[ADAPT_HW_VER_STRLEN]) override; |
| 115 | + |
| 116 | + /** |
| 117 | + * @brief Get the "Sensirion article id" of the adaptive sensor for example 1-101053-01 |
| 118 | + * @param[out] article max 12 bytes |
| 119 | + * @return 0 on success, <0 on communication error, >0 on error returned from sensor |
| 120 | + */ |
| 121 | + int8_t getArticle(char article[ADAPT_ART_STRLEN]) override; |
| 122 | + |
| 123 | + private: |
| 124 | + UART_HandleTypeDef* m_uart; |
| 125 | + GpioDriver m_driveEnable; |
| 126 | + |
| 127 | + int8_t sendCmd(const struct adapt_comm_cmd& cmd); |
| 128 | + int8_t getResponse(uint8_t cmd, uint8_t *response, size_t sz); |
| 129 | + bool verifyCrc(const std::vector<uint8_t>& msg); |
| 130 | +}; |
| 131 | + |
| 132 | +extern void adaptiveSensorUartCallback(UART_HandleTypeDef *huart); |
| 133 | + |
| 134 | +#endif /* ADAPTIVESENSORHW_HPP_ */ |
0 commit comments