HardFOC Core
API reference
Loading...
Searching...
No Matches
Fdo2Handler.h
Go to the documentation of this file.
1
38#ifndef COMPONENT_HANDLER_FDO2_HANDLER_H_
39#define COMPONENT_HANDLER_FDO2_HANDLER_H_
40
41#include <atomic>
42#include <cstddef>
43#include <cstdint>
44#include <memory>
45
46#include "RtosMutex.h"
47#include "base/BaseUart.h"
48
49#include "core/hf-core-drivers/external/hf-fdo2-driver/inc/fdo2.hpp"
50
51//==============================================================================
52// CRTP UART ADAPTER FOR BaseUart
53//==============================================================================
54
71class HalUartFdo2Comm : public fdo2::UartInterface<HalUartFdo2Comm> {
72public:
73 explicit HalUartFdo2Comm(BaseUart& uart) noexcept : uart_(uart) {}
74
75 void write(const std::uint8_t* data, std::size_t length) noexcept;
76 std::size_t read(std::uint8_t* out, std::size_t max,
77 std::uint32_t timeout_ms) noexcept;
78 void flush_rx() noexcept;
79 void delay_ms_impl(std::uint32_t ms) noexcept;
80
81private:
82 BaseUart& uart_;
84 std::uint8_t line_[128]{};
85 std::size_t line_len_{0};
86};
87
88//==============================================================================
89// HANDLER CONFIGURATION
90//==============================================================================
91
96 std::uint32_t line_timeout_ms{300};
100 std::uint32_t measure_timeout_ms{300};
103 std::uint8_t device_index{0};
104};
105
106//==============================================================================
107// HANDLER
108//==============================================================================
109
118public:
119 using DriverType = fdo2::Driver<HalUartFdo2Comm>;
120
131 explicit Fdo2Handler(BaseUart& uart,
132 const Fdo2HandlerConfig& config = Fdo2HandlerConfig{},
133 RtosMutex* bus_mutex = nullptr) noexcept;
134
135 Fdo2Handler(const Fdo2Handler&) = delete;
139
145 bool EnsureInitialized() noexcept;
146
148 [[nodiscard]] bool IsPresent() const noexcept {
149 return initialized_.load(std::memory_order_acquire);
150 }
151
153 [[nodiscard]] const fdo2::VersionInfo& Identity() const noexcept {
154 return identity_;
155 }
156
157 //==========================================================================
158 // Live measurement
159 //==========================================================================
160
162 fdo2::DriverResult<fdo2::MoxyReading> MeasureMoxy() noexcept;
163
168 fdo2::DriverResult<fdo2::MrawReading> MeasureMraw() noexcept;
169
171 fdo2::DriverResult<fdo2::VersionInfo> ReadVersion() noexcept;
172
174 fdo2::DriverResult<std::uint64_t> ReadUniqueId() noexcept;
175
176private:
177 bool EnsureInitializedLocked() noexcept;
178
181 std::unique_ptr<DriverType> driver_;
182
183 fdo2::VersionInfo identity_{};
184
185 std::atomic<bool> initialized_{false};
186
187 RtosMutex private_mutex_;
188 RtosMutex* bus_mutex_;
189};
190
191#endif // COMPONENT_HANDLER_FDO2_HANDLER_H_
HAL handler wrapping one FDO2-G2 oxygen probe on a BaseUart.
Definition Fdo2Handler.h:117
RtosMutex * bus_mutex_
Definition Fdo2Handler.h:188
fdo2::Driver< HalUartFdo2Comm > DriverType
Definition Fdo2Handler.h:119
bool EnsureInitialized() noexcept
Wait the FDO2-G2 power-up settle (kFdo2G2PowerUpSettleMs) and probe the device with #VERS....
Definition Fdo2Handler.cpp:90
Fdo2Handler & operator=(Fdo2Handler &&)=delete
Fdo2Handler & operator=(const Fdo2Handler &)=delete
std::unique_ptr< DriverType > driver_
Definition Fdo2Handler.h:181
std::atomic< bool > initialized_
Definition Fdo2Handler.h:185
HalUartFdo2Comm comm_
Definition Fdo2Handler.h:180
const fdo2::VersionInfo & Identity() const noexcept
Cached identity (zeroed until probe succeeds).
Definition Fdo2Handler.h:153
bool EnsureInitializedLocked() noexcept
Definition Fdo2Handler.cpp:96
fdo2::DriverResult< fdo2::MrawReading > MeasureMraw() noexcept
Definition Fdo2Handler.cpp:128
fdo2::DriverResult< fdo2::MoxyReading > MeasureMoxy() noexcept
One #MOXY round-trip — partial-pressure O2 + temperature + status.
Definition Fdo2Handler.cpp:119
fdo2::DriverResult< std::uint64_t > ReadUniqueId() noexcept
Read the device's 64-bit unique serial via #IDNR.
Definition Fdo2Handler.cpp:150
fdo2::VersionInfo identity_
Definition Fdo2Handler.h:183
Fdo2Handler(const Fdo2Handler &)=delete
Fdo2HandlerConfig config_
Definition Fdo2Handler.h:179
fdo2::DriverResult< fdo2::VersionInfo > ReadVersion() noexcept
Re-read #VERS (refreshes the cached Identity).
Definition Fdo2Handler.cpp:137
Fdo2Handler(Fdo2Handler &&)=delete
RtosMutex private_mutex_
Definition Fdo2Handler.h:187
bool IsPresent() const noexcept
True once EnsureInitialized() has succeeded.
Definition Fdo2Handler.h:148
CRTP adapter that turns a BaseUart& into a transport the templated fdo2::Driver<UartT> can drive.
Definition Fdo2Handler.h:71
HalUartFdo2Comm(BaseUart &uart) noexcept
Definition Fdo2Handler.h:73
std::size_t line_len_
Definition Fdo2Handler.h:85
void delay_ms_impl(std::uint32_t ms) noexcept
Definition Fdo2Handler.cpp:70
BaseUart & uart_
Definition Fdo2Handler.h:82
void write(const std::uint8_t *data, std::size_t length) noexcept
Definition Fdo2Handler.cpp:28
void flush_rx() noexcept
Definition Fdo2Handler.cpp:65
std::uint8_t line_[128]
Reply being assembled; 128 bytes holds the longest reply (#MRAW with CRC, < 120).
Definition Fdo2Handler.h:84
std::size_t read(std::uint8_t *out, std::size_t max, std::uint32_t timeout_ms) noexcept
Definition Fdo2Handler.cpp:34
Construction-time configuration for one FDO2-G2 instance.
Definition Fdo2Handler.h:93
std::uint8_t device_index
Definition Fdo2Handler.h:103
std::uint32_t measure_timeout_ms
Definition Fdo2Handler.h:100
std::uint32_t line_timeout_ms
Definition Fdo2Handler.h:96