HardFOC Flux V1 HAL
Unified HAL API for the HardFOC Flux valve-control platform (ESP32-S3)
Loading...
Searching...
No Matches
CommChannelsManager.h
Go to the documentation of this file.
1
22#ifndef FLUX_COMM_CHANNELS_MANAGER_H_
23#define FLUX_COMM_CHANNELS_MANAGER_H_
24
25#include <array>
26#include <atomic>
27#include <cstdint>
28
29// Forward declaration — only appears as the return type of GetI2cBus()
30class BaseI2c;
31
32#include "core/hf-core-drivers/internal/hf-internal-interface-wrap/inc/base/BaseSpi.h"
33#include "core/hf-core-drivers/internal/hf-internal-interface-wrap/inc/base/BaseCan.h"
34#include "core/hf-core-drivers/internal/hf-internal-interface-wrap/inc/base/BaseUart.h"
35#include "core/hf-core-drivers/internal/hf-internal-interface-wrap/inc/base/BaseUsbSerialJtag.h"
36#include "RtosMutex.h"
38
39//==============================================================================
40// COMM CHANNELS ERROR CODES
41//==============================================================================
42
63
69constexpr const char* CommErrorToString(CommError error) noexcept {
70 switch (error) {
71 case CommError::SUCCESS: return "Success";
72 case CommError::NOT_INITIALIZED: return "Not initialized";
73 case CommError::INITIALIZATION_FAILED: return "Initialization failed";
74 case CommError::BUS_NOT_AVAILABLE: return "Bus not available";
75 case CommError::DEVICE_NOT_FOUND: return "Device not found";
76 case CommError::INVALID_BUS_INDEX: return "Invalid bus index";
77 case CommError::INVALID_DEVICE_INDEX: return "Invalid device index";
78 case CommError::DEVICE_ALREADY_EXISTS: return "Device already exists";
79 case CommError::DEVICE_CREATION_FAILED: return "Device creation failed";
80 case CommError::COMMUNICATION_FAILED: return "Communication failed";
81 case CommError::INVALID_PARAMETER: return "Invalid parameter";
82 case CommError::MUTEX_LOCK_FAILED: return "Mutex lock failed";
83 default: return "Unknown error";
84 }
85}
86
87//==============================================================================
88// COMM CHANNELS DIAGNOSTICS
89//==============================================================================
90
103
104//==============================================================================
105// SPI DEVICE IDENTIFIERS
106//==============================================================================
107
113enum class SpiDeviceId : uint8_t {
116 ADS7952_ADC_0 = 2,
118};
119
120//==============================================================================
121// COMM CHANNELS MANAGER
122//==============================================================================
123
135public:
137 static CommChannelsManager& GetInstance() noexcept;
138
139 // Non-copyable, non-movable
144
145 //==========================================================================
146 // INITIALIZATION
147 //==========================================================================
148
153 bool EnsureInitialized() noexcept;
154
159 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_.load(std::memory_order_acquire); }
160
165 [[nodiscard]] CommError GetLastError() const noexcept { return last_error_.load(std::memory_order_acquire); }
166
173 [[nodiscard]] flux::hal::ManagerError GetManagerError() const noexcept {
174 return manager_error_.load(std::memory_order_acquire);
175 }
176
178 [[nodiscard]] bool IsPressureBusReady() const noexcept { return spi3_bus_valid_; }
179
181 [[nodiscard]] bool IsCanReady() const noexcept { return can_bus_valid_; }
182
188 [[nodiscard]] CommError GetSystemDiagnostics(CommSystemDiagnostics& diagnostics) const noexcept;
189
194 bool Deinitialize() noexcept;
195
196 //==========================================================================
197 // SPI ACCESSORS
198 //==========================================================================
199
205 BaseSpi* GetSpiDevice(SpiDeviceId device_id) noexcept;
206
212 BaseSpi* GetSpiDevice(uint8_t device_index) noexcept;
213
214 //==========================================================================
215 // I2C — not present on Flux V1
216 //==========================================================================
217
222 BaseI2c* GetI2cBus() noexcept { return nullptr; }
223
224 //==========================================================================
225 // CAN ACCESSOR
226 //==========================================================================
227
232 BaseCan* GetCanBus() noexcept;
233
234 //==========================================================================
235 // UART ACCESSORS
236 //==========================================================================
237
246 uint8_t port_number;
247 int16_t tx_pin;
248 int16_t rx_pin;
249 int16_t rts_pin;
250 int16_t cts_pin;
251 uint32_t baud_bps;
254 };
255
259 static constexpr uint8_t kMaxUartPorts = 3;
260
271 BaseUart* AcquireUart(const UartAcquireConfig& cfg) noexcept;
272
278 BaseUart* GetUart(uint8_t port_number) noexcept;
279
280 //==========================================================================
281 // USB SERIAL/JTAG ACCESSORS (ESP32-S3/-C3/-C6 native USB-VCP + JTAG)
282 //==========================================================================
283
302 BaseUsbSerialJtag* AcquireUsbSerialJtag(
303 const hf_usb_serial_jtag_config_t& cfg = {}) noexcept;
304
310 BaseUsbSerialJtag* GetUsbSerialJtag() noexcept;
311
312 //==========================================================================
313 // DIAGNOSTICS
314 //==========================================================================
315
317 void DumpStatistics() const noexcept;
318
319private:
320 CommChannelsManager() noexcept = default;
322
323 bool Initialize() noexcept;
324 void ReleaseLocked() noexcept;
325
326 //==========================================================================
327 // HARDWARE RESOURCES — objects live in the platform binding (bsp_bind);
328 // the manager holds Base* pointers only
329 //==========================================================================
330
331 static constexpr uint8_t kSpiDeviceCount = static_cast<uint8_t>(SpiDeviceId::DEVICE_COUNT);
332
333 bool spi_bus_valid_{false};
334 bool spi3_bus_valid_{false};
335
337 std::array<BaseSpi*, kSpiDeviceCount> spi_devices_{};
338
339 BaseCan* can_bus_{nullptr};
340 bool can_bus_valid_{false};
341
342 // Lazy UART pool — one slot per hardware UART port. App-driven:
343 // sensors (BASIS-2, FDO2, etc.) call `AcquireUart(cfg)` at boot.
344 std::array<BaseUart*, kMaxUartPorts> uarts_{};
345
346 // Lazy USB Serial/JTAG. The controller is a single silicon block, owned by
347 // the platform binding; we only cache the resolved pointer here.
348 BaseUsbSerialJtag* usb_serial_jtag_{nullptr};
349
350 std::atomic<bool> initialized_{false};
351 std::atomic<CommError> last_error_{CommError::SUCCESS};
352 std::atomic<flux::hal::ManagerError> manager_error_{flux::hal::ManagerError::NotInitialized};
353 mutable RtosMutex mutex_;
354
355 void UpdateLastError(CommError error) noexcept {
356 last_error_.store(error, std::memory_order_release);
357 }
358};
359
360//==============================================================================
361// CONVENIENCE
362//==============================================================================
363
368[[nodiscard]] inline CommChannelsManager& GetCommChannelsManager() noexcept {
370}
371
372#endif // FLUX_COMM_CHANNELS_MANAGER_H_
CommChannelsManager & GetCommChannelsManager() noexcept
Convenience accessor — equivalent to CommChannelsManager::GetInstance().
Definition CommChannelsManager.h:368
constexpr const char * CommErrorToString(CommError error) noexcept
Convert CommError to a human-readable string.
Definition CommChannelsManager.h:69
CommError
Communication channels manager error codes for consistent error reporting.
Definition CommChannelsManager.h:49
@ NOT_INITIALIZED
Manager not yet initialised.
@ INVALID_BUS_INDEX
Bus index out of range.
@ MUTEX_LOCK_FAILED
RTOS mutex acquire timed out.
@ INVALID_DEVICE_INDEX
Device index out of range.
@ COMMUNICATION_FAILED
A bus-level transfer failed.
@ DEVICE_ALREADY_EXISTS
A device at that address already exists.
@ DEVICE_NOT_FOUND
Device index or id not in registry.
@ SUCCESS
Operation completed successfully.
@ DEVICE_CREATION_FAILED
Heap allocation or driver init for device failed.
@ BUS_NOT_AVAILABLE
Requested bus is not present or not ready.
@ INITIALIZATION_FAILED
Bus or device initialisation failed.
@ INVALID_PARAMETER
Null pointer or out-of-range argument.
SpiDeviceId
SPI device identifiers for the Flux V1 board.
Definition CommChannelsManager.h:113
@ DEVICE_COUNT
Sentinel — total number of SPI devices.
@ MAX22200_SOLENOID_DRIVER
MAX22200 octal solenoid driver (SPI2, Mode 0)
@ TLE92466_LOWSIDE_DRIVER
TLE92466ED six-channel driver (SPI2, Mode 1)
@ ADS7952_ADC_0
ADS7952 12-ch ADC device 0 (SPI3, Mode 0)
Singleton managing all board communication channels for Flux V1.
Definition CommChannelsManager.h:134
std::array< BaseSpi *, kSpiDeviceCount > spi_devices_
One device per SpiDeviceId; nullptr when its bus or chip select failed.
Definition CommChannelsManager.h:337
BaseUsbSerialJtag * usb_serial_jtag_
Definition CommChannelsManager.h:348
BaseUart * GetUart(uint8_t port_number) noexcept
Look up a previously-acquired UART by port number.
Definition CommChannelsManager.cpp:290
std::atomic< flux::hal::ManagerError > manager_error_
Definition CommChannelsManager.h:352
void DumpStatistics() const noexcept
Log communication channel statistics and bus health to the console.
Definition CommChannelsManager.cpp:325
std::array< BaseUart *, kMaxUartPorts > uarts_
Definition CommChannelsManager.h:344
BaseUsbSerialJtag * GetUsbSerialJtag() noexcept
Look up a previously-acquired USB Serial/JTAG instance.
Definition CommChannelsManager.cpp:317
CommChannelsManager & operator=(const CommChannelsManager &)=delete
CommChannelsManager(const CommChannelsManager &)=delete
bool IsCanReady() const noexcept
The CAN controller object exists (configured, not started).
Definition CommChannelsManager.h:181
BaseUart * AcquireUart(const UartAcquireConfig &cfg) noexcept
Acquire (lazy-create + cache) a UART on the given hardware port.
Definition CommChannelsManager.cpp:233
BaseUsbSerialJtag * AcquireUsbSerialJtag(const hf_usb_serial_jtag_config_t &cfg={}) noexcept
Lazy-acquire the built-in USB Serial/JTAG controller.
Definition CommChannelsManager.cpp:299
static constexpr uint8_t kMaxUartPorts
Definition CommChannelsManager.h:259
BaseCan * GetCanBus() noexcept
Get the TWAI/CAN bus.
Definition CommChannelsManager.cpp:215
static constexpr uint8_t kSpiDeviceCount
Definition CommChannelsManager.h:331
CommError GetSystemDiagnostics(CommSystemDiagnostics &diagnostics) const noexcept
Fill a diagnostics snapshot with current system state.
Definition CommChannelsManager.cpp:341
BaseI2c * GetI2cBus() noexcept
Flux V1 does not bring up an I²C bus in CommChannelsManager.
Definition CommChannelsManager.h:222
bool spi3_bus_valid_
Pressure bus (ADS7952) up.
Definition CommChannelsManager.h:334
bool Initialize() noexcept
Definition CommChannelsManager.cpp:106
bool spi_bus_valid_
Valve bus (MAX22200 + TLE92466ED) up.
Definition CommChannelsManager.h:333
bool Deinitialize() noexcept
Release all bus resources and reset to uninitialized state.
Definition CommChannelsManager.cpp:368
CommChannelsManager(CommChannelsManager &&)=delete
void UpdateLastError(CommError error) noexcept
Definition CommChannelsManager.h:355
bool IsPressureBusReady() const noexcept
The pressure SPI bus and its ADS7952 chip select are up.
Definition CommChannelsManager.h:178
BaseSpi * GetSpiDevice(SpiDeviceId device_id) noexcept
Get an SPI device handle by enum identifier.
Definition CommChannelsManager.cpp:201
bool can_bus_valid_
Definition CommChannelsManager.h:340
void ReleaseLocked() noexcept
Definition CommChannelsManager.cpp:186
RtosMutex mutex_
Definition CommChannelsManager.h:353
std::atomic< bool > initialized_
Definition CommChannelsManager.h:350
static CommChannelsManager & GetInstance() noexcept
Return the singleton CommChannelsManager instance.
Definition CommChannelsManager.cpp:78
CommChannelsManager & operator=(CommChannelsManager &&)=delete
bool IsInitialized() const noexcept
Check whether the communication subsystem has been initialised.
Definition CommChannelsManager.h:159
BaseCan * can_bus_
CAN controller (configured, not started)
Definition CommChannelsManager.h:339
flux::hal::ManagerError GetManagerError() const noexcept
Lifecycle / bind error of the last EnsureInitialized() (shared catalog).
Definition CommChannelsManager.h:173
CommError GetLastError() const noexcept
Get the most recent error code.
Definition CommChannelsManager.h:165
bool EnsureInitialized() noexcept
Initialise all communication buses if not already initialised.
Definition CommChannelsManager.cpp:87
std::atomic< CommError > last_error_
Definition CommChannelsManager.h:351
Shared HAL types for Flux V1: the manager error catalog and the facade result.
ManagerError
Lifecycle / bind failure catalog shared by every manager (GetManagerError()).
Definition flux_hal_types.hpp:37
@ NotInitialized
Manager not initialised (or shut down)
Lazy-acquire configuration for a hardware UART port.
Definition CommChannelsManager.h:245
int16_t tx_pin
GPIO; -1 to skip pin assignment.
Definition CommChannelsManager.h:247
uint8_t port_number
Hardware UART port (0/1/2)
Definition CommChannelsManager.h:246
int16_t cts_pin
-1 = no CTS (typical).
Definition CommChannelsManager.h:250
uint16_t tx_buffer_size
Definition CommChannelsManager.h:252
uint16_t rx_buffer_size
Definition CommChannelsManager.h:253
int16_t rts_pin
-1 = plain UART, else RS-485 DE/RE.
Definition CommChannelsManager.h:249
int16_t rx_pin
GPIO; -1 to skip pin assignment.
Definition CommChannelsManager.h:248
uint32_t baud_bps
Definition CommChannelsManager.h:251
System-level diagnostics snapshot for communication channels.
Definition CommChannelsManager.h:94
bool i2c_bus_valid
Always false — Flux V1 has no I²C manager bus.
Definition CommChannelsManager.h:98
bool system_healthy
True when all configured buses are operational.
Definition CommChannelsManager.h:95
bool can_bus_valid
CAN/TWAI bus initialised and usable.
Definition CommChannelsManager.h:99
uint8_t spi_device_count
Number of SPI devices with valid indices.
Definition CommChannelsManager.h:100
bool spi_bus_valid
SPI bus initialised and usable.
Definition CommChannelsManager.h:97
bool system_initialized
True if EnsureInitialized() completed.
Definition CommChannelsManager.h:96
CommError last_error
Most recent error code from any operation.
Definition CommChannelsManager.h:101