HardFOC Core
API reference
Loading...
Searching...
No Matches
SfmHandler.h
Go to the documentation of this file.
1
25#ifndef COMPONENT_HANDLER_SFM_HANDLER_H_
26#define COMPONENT_HANDLER_SFM_HANDLER_H_
27
28#include <atomic>
29#include <cstddef>
30#include <cstdint>
31#include <memory>
32
33#include "base/BaseI2c.h"
34#include "core/hf-core-drivers/external/hf-sfm-flow-meter-driver/inc/sfm.hpp"
35#include "RtosMutex.h"
36
37//==============================================================================
38// CRTP I2C ADAPTER
39//==============================================================================
40
53class HalI2cSfmComm : public sfm::I2cInterface<HalI2cSfmComm> {
54public:
55 explicit HalI2cSfmComm(BaseI2c& i2c_device) noexcept;
56
59 bool Write(std::uint8_t addr7, const std::uint8_t* data, std::size_t len) noexcept;
60 bool Read(std::uint8_t addr7, std::uint8_t* out, std::size_t len) noexcept;
61 void DelayMs(std::uint32_t ms) noexcept;
62 bool EnsureInitialized() noexcept;
64
66 [[nodiscard]] std::uint8_t DeviceAddress() const noexcept;
67
68private:
69 BaseI2c& i2c_device_;
70 std::uint8_t tx_scratch_[8]{};
71 std::uint8_t rx_scratch_[18]{};
72};
73
74//==============================================================================
75// HANDLER CONFIGURATION
76//==============================================================================
77
81 sfm::Gas default_gas{sfm::Gas::Air};
83 std::uint16_t o2_permille{0};
85 std::uint16_t averaging_window{0};
87 bool soft_reset_on_init{false};
89 std::uint8_t device_index{0};
90};
91
92//==============================================================================
93// HANDLER
94//==============================================================================
95
100public:
101 using DriverType = sfm::Driver<HalI2cSfmComm>;
102
108 explicit SfmHandler(BaseI2c& i2c_device,
109 const SfmHandlerConfig& config = SfmHandlerConfig{},
110 RtosMutex* bus_mutex = nullptr) noexcept;
111
112 SfmHandler(const SfmHandler&) = delete;
113 SfmHandler& operator=(const SfmHandler&) = delete;
116
122 bool EnsureInitialized() noexcept;
123
130 void Invalidate() noexcept { initialized_.store(false, std::memory_order_release); }
131
133 [[nodiscard]] bool IsPresent() const noexcept {
134 return initialized_.load(std::memory_order_acquire);
135 }
136
138 [[nodiscard]] const sfm::ProductInfo& Identity() const noexcept { return identity_; }
139
141 [[nodiscard]] bool SupportsGas(sfm::Gas gas) const noexcept;
142
144 [[nodiscard]] float FullScaleSlm() const noexcept;
145
146 //==========================================================================
147 // Measurement session
148 //==========================================================================
149
155 sfm::DriverResult<void> Start(sfm::Gas gas) noexcept;
156
158 sfm::DriverResult<void> Start() noexcept { return Start(config_.default_gas); }
159
161 sfm::DriverResult<void> Stop() noexcept;
162
164 sfm::DriverResult<sfm::Measurement> ReadMeasurement() noexcept;
165
167 sfm::DriverResult<void> ConfigureAveraging(std::uint16_t window) noexcept;
168
170 sfm::DriverResult<void> UpdateConcentration(std::uint16_t o2_permille) noexcept;
171
173 sfm::DriverResult<float> ReadTemperature() noexcept;
174
180 sfm::DriverResult<sfm::Measurement> DecodeFrame(const std::uint8_t frame[9]) noexcept;
181
183 [[nodiscard]] sfm::Family Family() const noexcept;
184
186 [[nodiscard]] std::uint8_t Address() const noexcept { return comm_.DeviceAddress(); }
187
189 [[nodiscard]] sfm::Gas ActiveGas() const noexcept;
191 [[nodiscard]] sfm::Scaling ActiveScaling() const noexcept;
193 [[nodiscard]] bool Measuring() const noexcept;
194
196 struct Counters {
197 std::uint32_t frames{0};
198 std::uint32_t no_data{0};
199 std::uint32_t crc_errors{0};
200 std::uint32_t bus_errors{0};
201 };
202 [[nodiscard]] Counters GetCounters() const noexcept;
203
204private:
205 bool EnsureInitializedLocked() noexcept;
206
209 std::unique_ptr<DriverType> driver_;
210
211 sfm::ProductInfo identity_{};
212 std::atomic<bool> initialized_{false};
213
214 std::atomic<std::uint32_t> frames_{0};
215 std::atomic<std::uint32_t> no_data_{0};
216 std::atomic<std::uint32_t> crc_errors_{0};
217 std::atomic<std::uint32_t> bus_errors_{0};
218
219 RtosMutex private_mutex_;
220 RtosMutex* bus_mutex_;
221};
222
223#endif // COMPONENT_HANDLER_SFM_HANDLER_H_
CRTP transport bridging sfm::I2cInterface to a BaseI2c device.
Definition SfmHandler.h:53
bool Write(std::uint8_t addr7, const std::uint8_t *data, std::size_t len) noexcept
Definition SfmHandler.cpp:28
void DelayMs(std::uint32_t ms) noexcept
Definition SfmHandler.cpp:63
std::uint8_t tx_scratch_[8]
Definition SfmHandler.h:70
bool EnsureInitialized() noexcept
Definition SfmHandler.cpp:70
std::uint8_t rx_scratch_[18]
Definition SfmHandler.h:71
bool Read(std::uint8_t addr7, std::uint8_t *out, std::size_t len) noexcept
Definition SfmHandler.cpp:49
BaseI2c & i2c_device_
Definition SfmHandler.h:69
std::uint8_t DeviceAddress() const noexcept
Address configured on the bound BaseI2c device.
Definition SfmHandler.cpp:24
Handler wrapping one SF06 flow meter on a BaseI2c device.
Definition SfmHandler.h:99
sfm::Family Family() const noexcept
Part family (from the identifier once probed).
Definition SfmHandler.cpp:244
SfmHandlerConfig config_
Definition SfmHandler.h:207
sfm::DriverResult< float > ReadTemperature() noexcept
SFx6000: flow-chip temperature while measuring (NotSupported on SF06).
Definition SfmHandler.cpp:225
SfmHandler(SfmHandler &&)=delete
SfmHandler & operator=(SfmHandler &&)=delete
void Invalidate() noexcept
Forget the cached identity: the next EnsureInitialized() (or Start) reads the product identifier agai...
Definition SfmHandler.h:130
const sfm::ProductInfo & Identity() const noexcept
Cached identity (zeroed until probe succeeds).
Definition SfmHandler.h:138
std::atomic< std::uint32_t > bus_errors_
Definition SfmHandler.h:217
sfm::DriverResult< void > ConfigureAveraging(std::uint16_t window) noexcept
Change the averaging window while measuring (0 = average-until-read).
Definition SfmHandler.cpp:201
sfm::DriverResult< sfm::Measurement > ReadMeasurement() noexcept
Read one flow / temperature / status frame (NoData when no fresh sample).
Definition SfmHandler.cpp:183
sfm::ProductInfo identity_
Definition SfmHandler.h:211
sfm::Driver< HalI2cSfmComm > DriverType
Definition SfmHandler.h:101
SfmHandler(const SfmHandler &)=delete
std::atomic< std::uint32_t > no_data_
Definition SfmHandler.h:215
sfm::DriverResult< sfm::Measurement > DecodeFrame(const std::uint8_t frame[9]) noexcept
Decode a 9-byte frame fetched by an interrupt / DMA reader with the active family and scaling; counts...
Definition SfmHandler.cpp:233
bool Measuring() const noexcept
True while a continuous measurement is running.
Definition SfmHandler.cpp:250
sfm::Gas ActiveGas() const noexcept
Gas selected by the last successful Start().
Definition SfmHandler.cpp:246
std::atomic< std::uint32_t > crc_errors_
Definition SfmHandler.h:216
std::uint8_t Address() const noexcept
7-bit address of the bound device.
Definition SfmHandler.h:186
std::atomic< bool > initialized_
Definition SfmHandler.h:212
RtosMutex * bus_mutex_
Definition SfmHandler.h:220
std::atomic< std::uint32_t > frames_
Definition SfmHandler.h:214
bool EnsureInitializedLocked() noexcept
Definition SfmHandler.cpp:93
Counters GetCounters() const noexcept
Definition SfmHandler.cpp:252
sfm::DriverResult< void > UpdateConcentration(std::uint16_t o2_permille) noexcept
Update the O2 fraction of a running mixture table.
Definition SfmHandler.cpp:213
bool EnsureInitialized() noexcept
Probe the device: optional soft reset, stop any running measurement, read product identifier + serial...
Definition SfmHandler.cpp:85
sfm::DriverResult< void > Start() noexcept
Start(config.default_gas).
Definition SfmHandler.h:158
std::unique_ptr< DriverType > driver_
Definition SfmHandler.h:209
SfmHandler & operator=(const SfmHandler &)=delete
sfm::DriverResult< void > Stop() noexcept
Stop continuous measurement (idle).
Definition SfmHandler.cpp:175
sfm::Scaling ActiveScaling() const noexcept
Scale / offset / unit captured at Start().
Definition SfmHandler.cpp:248
bool IsPresent() const noexcept
True once EnsureInitialized() has succeeded.
Definition SfmHandler.h:133
float FullScaleSlm() const noexcept
Full-scale flow of the probed variant [slm] (0 when unknown).
Definition SfmHandler.cpp:136
bool SupportsGas(sfm::Gas gas) const noexcept
Whether the probed variant carries a factory table for gas.
Definition SfmHandler.cpp:132
HalI2cSfmComm comm_
Definition SfmHandler.h:208
RtosMutex private_mutex_
Definition SfmHandler.h:219
Diagnostics counters since construction.
Definition SfmHandler.h:196
Construction-time configuration for one SF06 flow meter.
Definition SfmHandler.h:79
std::uint16_t averaging_window
On-sensor averaging: 0 = average-until-read, 1…128 = fixed-N.
Definition SfmHandler.h:85
bool soft_reset_on_init
Issue a general-call soft reset during bring-up when the bus supports it.
Definition SfmHandler.h:87
std::uint16_t o2_permille
O2 volume fraction (‰) for mixture tables.
Definition SfmHandler.h:83
sfm::Gas default_gas
Gas table requested at Start() when the caller passes none.
Definition SfmHandler.h:81
std::uint8_t device_index
Logical index for diagnostics.
Definition SfmHandler.h:89