Internal Interface Wrap
API reference
Loading...
Searching...
No Matches
Classes | Namespaces | Macros | Typedefs | Enumerations | Functions | Variables
BaseCan.h File Reference

Abstract base class for CAN bus implementations in the HardFOC system. More...

#include "HardwareTypes.h"
#include <atomic>
#include <cstdint>
#include <string_view>
Include dependency graph for BaseCan.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  hf_can_config_t
 Platform-agnostic CAN bus configuration. More...
 
struct  hf_can_message_t
 Platform-agnostic CAN / CAN FD message (BaseCan v2). More...
 
struct  hf_can_bus_status_t
 Error counters and queue statistics of one controller (BaseCan::GetBusStatus()). More...
 
struct  hf_can_status_t
 CAN bus status information structure. More...
 
struct  hf_can_reception_info_t
 CAN-FD specific receive callback with enhanced information. More...
 
struct  hf_can_statistics_t
 CAN bus statistics structure for performance monitoring. More...
 
struct  hf_can_diagnostics_t
 CAN diagnostics structure for detailed error analysis. More...
 
class  BaseCan
 Abstract base class defining the unified CAN bus API. More...
 

Namespaces

namespace  hf_can_flag
 Packed frame flags (see hf_can_message_t::GetFlags()).
 

Macros

#define HF_CAN_ERR_LIST(X)
 HardFOC CAN error codes.
 
#define X(NAME, VALUE, DESC)   NAME = VALUE,
 
#define X(NAME, VALUE, DESC)
 

Typedefs

using hf_can_receive_callback_t = void(*)(const hf_can_message_t &message)
 CAN message receive callback function type.
 
using hf_can_fd_receive_callback_t = void(*)(const hf_can_message_t &message, const hf_can_reception_info_t &info)
 

Enumerations

enum class  hf_can_err_t : hf_u8_t {
  X , CAN_SUCCESS = 0 , CAN_ERR_FAILURE = 1 , CAN_ERR_NOT_INITIALIZED = 2 ,
  CAN_ERR_ALREADY_INITIALIZED = 3 , CAN_ERR_INVALID_PARAMETER = 4 , CAN_ERR_NULL_POINTER = 5 , CAN_ERR_OUT_OF_MEMORY = 6 ,
  CAN_ERR_BUS_OFF = 7 , CAN_ERR_BUS_ERROR = 8 , CAN_ERR_BUS_BUSY = 9 , CAN_ERR_BUS_NOT_AVAILABLE = 10 ,
  CAN_ERR_BUS_RECOVERY_FAILED = 11 , CAN_ERR_BUS_ARBITRATION_LOST = 12 , CAN_ERR_MESSAGE_TIMEOUT = 13 , CAN_ERR_MESSAGE_LOST = 14 ,
  CAN_ERR_MESSAGE_INVALID = 15 , CAN_ERR_MESSAGE_TOO_LONG = 16 , CAN_ERR_MESSAGE_INVALID_ID = 17 , CAN_ERR_MESSAGE_INVALID_DLC = 18 ,
  CAN_ERR_QUEUE_FULL = 19 , CAN_ERR_QUEUE_EMPTY = 20 , CAN_ERR_TX_FAILED = 21 , CAN_ERR_TX_ABORTED = 22 ,
  CAN_ERR_TX_ERROR_PASSIVE = 23 , CAN_ERR_TX_ERROR_WARNING = 24 , CAN_ERR_RX_OVERRUN = 25 , CAN_ERR_RX_ERROR_PASSIVE = 26 ,
  CAN_ERR_RX_ERROR_WARNING = 27 , CAN_ERR_RX_FIFO_FULL = 28 , CAN_ERR_HARDWARE_FAULT = 29 , CAN_ERR_COMMUNICATION_FAILURE = 30 ,
  CAN_ERR_DEVICE_NOT_RESPONDING = 31 , CAN_ERR_VOLTAGE_OUT_OF_RANGE = 32 , CAN_ERR_CLOCK_ERROR = 33 , CAN_ERR_TRANSCEIVER_ERROR = 34 ,
  CAN_ERR_INVALID_CONFIGURATION = 35 , CAN_ERR_UNSUPPORTED_OPERATION = 36 , CAN_ERR_INVALID_BAUD_RATE = 37 , CAN_ERR_INVALID_CONTROLLER_ID = 38 ,
  CAN_ERR_FILTER_ERROR = 39 , CAN_ERR_FILTER_FULL = 40 , CAN_ERR_STUFF_ERROR = 41 , CAN_ERR_FORM_ERROR = 42 ,
  CAN_ERR_CRC_ERROR = 43 , CAN_ERR_ACK_ERROR = 44 , CAN_ERR_BIT_ERROR = 45 , CAN_ERR_SYSTEM_ERROR = 46 ,
  CAN_ERR_PERMISSION_DENIED = 47 , CAN_ERR_OPERATION_ABORTED = 48 , CAN_ERR_FAIL = 49 , CAN_ERR_RESOURCE_BUSY = 50 ,
  CAN_ERR_INVALID_STATE = 51 , CAN_ERR_NOT_SUPPORTED = 52 , CAN_ERR_TIMEOUT_ALT = 53 , CAN_ERR_UNKNOWN = 54
}
 
enum class  hf_can_frame_error_t : hf_u8_t {
  NONE = 0 , ID_RANGE , CLASSIC_TOO_LONG , FD_TOO_LONG ,
  FD_LENGTH_NOT_EXACT , RTR_ON_FD , FD_BITS_ON_CLASSIC , RTR_WITH_DATA
}
 Why a frame is not well formed. More...
 
enum class  hf_can_bus_state_t : hf_u8_t {
  STOPPED = 0 , ERROR_ACTIVE , ERROR_WARNING , ERROR_PASSIVE ,
  BUS_OFF
}
 Controller error state (ISO 11898-1 fault confinement). More...
 

Functions

constexpr std::string_view HfCanErrToString (hf_can_err_t err) noexcept
 Convert hf_can_err_t to human-readable string.
 
constexpr hf_u8_t HfCanDlcToLen (hf_u8_t dlc, bool fd) noexcept
 DLC code to payload length in bytes (ISO 11898-1).
 
constexpr hf_u8_t HfCanLenToDlc (hf_u8_t len, bool fd) noexcept
 Payload length in bytes to the smallest DLC code that carries it.
 
constexpr bool HfCanIsValidLength (hf_u8_t len, bool fd) noexcept
 Is len a payload length a frame of this kind can have exactly?
 
constexpr hf_can_bus_state_t HfCanStateFromCounters (hf_u16_t tec, hf_u16_t rec) noexcept
 Error state implied by the counters (warning at 96, passive at 128, bus-off above 255).
 

Variables

constexpr hf_u8_t HF_CAN_CLASSIC_MAX_LEN = 8U
 Largest classic CAN payload in bytes.
 
constexpr hf_u8_t HF_CAN_FD_MAX_LEN = 64U
 Largest CAN FD payload in bytes.
 
constexpr hf_u32_t HF_CAN_MAX_STD_ID = 0x7FFU
 Largest 11-bit (base format) identifier.
 
constexpr hf_u32_t HF_CAN_MAX_EXT_ID = 0x1FFFFFFFU
 Largest 29-bit (extended format) identifier.
 
constexpr hf_u8_t HF_CAN_DLC_INVALID = 0xFFU
 Returned by HfCanLenToDlc() when a length cannot be carried.
 
constexpr hf_u8_t hf_can_flag::EXT = 0x01U
 29-bit identifier
 
constexpr hf_u8_t hf_can_flag::RTR = 0x02U
 remote request (classic only)
 
constexpr hf_u8_t hf_can_flag::FDF = 0x04U
 FD frame format.
 
constexpr hf_u8_t hf_can_flag::BRS = 0x08U
 bit-rate switch (FD only)
 
constexpr hf_u8_t hf_can_flag::ESI = 0x10U
 error state indicator (FD only)
 
constexpr hf_u8_t hf_can_flag::ALL = EXT | RTR | FDF | BRS | ESI
 

Detailed Description

Abstract base class for CAN bus implementations in the HardFOC system.

This header-only file defines the abstract base class for CAN bus communication that provides a consistent API across different CAN controller implementations. Concrete implementations for various microcontrollers inherit from this class.

ERROR HANDLING:

Author
Nebiyu Tadesse
Date
2025
Note
This is a header-only abstract base class following the same pattern as BaseAdc.
Users should program against this interface, not specific implementations.
All legacy camelCase types and bool-returning methods have been removed.

Macro Definition Documentation

◆ X [1/2]

#define X (   NAME,
  VALUE,
  DESC 
)    NAME = VALUE,

◆ X [2/2]

#define X (   NAME,
  VALUE,
  DESC 
)
Value:
case hf_can_err_t::NAME: \
return DESC;
RAII soft-CS: runs assert_fn on construction and deassert_fn on destruction (including early return /...
Definition SoftChipSelectGuard.h:32

Typedef Documentation

◆ hf_can_fd_receive_callback_t

◆ hf_can_receive_callback_t

CAN message receive callback function type.

Note
Updated to use new hf_can_message_t structure

Enumeration Type Documentation

◆ hf_can_bus_state_t

Controller error state (ISO 11898-1 fault confinement).

Same order as vortex::contracts::BusState.

Enumerator
STOPPED 

not started / in reset

ERROR_ACTIVE 

TEC and REC < 96.

ERROR_WARNING 

TEC or REC >= 96.

ERROR_PASSIVE 

TEC or REC >= 128.

BUS_OFF 

TEC > 255.

◆ hf_can_err_t

Enumerator
X 
CAN_SUCCESS 
CAN_ERR_FAILURE 
CAN_ERR_NOT_INITIALIZED 
CAN_ERR_ALREADY_INITIALIZED 
CAN_ERR_INVALID_PARAMETER 
CAN_ERR_NULL_POINTER 
CAN_ERR_OUT_OF_MEMORY 
CAN_ERR_BUS_OFF 
CAN_ERR_BUS_ERROR 
CAN_ERR_BUS_BUSY 
CAN_ERR_BUS_NOT_AVAILABLE 
CAN_ERR_BUS_RECOVERY_FAILED 
CAN_ERR_BUS_ARBITRATION_LOST 
CAN_ERR_MESSAGE_TIMEOUT 
CAN_ERR_MESSAGE_LOST 
CAN_ERR_MESSAGE_INVALID 
CAN_ERR_MESSAGE_TOO_LONG 
CAN_ERR_MESSAGE_INVALID_ID 
CAN_ERR_MESSAGE_INVALID_DLC 
CAN_ERR_QUEUE_FULL 
CAN_ERR_QUEUE_EMPTY 
CAN_ERR_TX_FAILED 
CAN_ERR_TX_ABORTED 
CAN_ERR_TX_ERROR_PASSIVE 
CAN_ERR_TX_ERROR_WARNING 
CAN_ERR_RX_OVERRUN 
CAN_ERR_RX_ERROR_PASSIVE 
CAN_ERR_RX_ERROR_WARNING 
CAN_ERR_RX_FIFO_FULL 
CAN_ERR_HARDWARE_FAULT 
CAN_ERR_COMMUNICATION_FAILURE 
CAN_ERR_DEVICE_NOT_RESPONDING 
CAN_ERR_VOLTAGE_OUT_OF_RANGE 
CAN_ERR_CLOCK_ERROR 
CAN_ERR_TRANSCEIVER_ERROR 
CAN_ERR_INVALID_CONFIGURATION 
CAN_ERR_UNSUPPORTED_OPERATION 
CAN_ERR_INVALID_BAUD_RATE 
CAN_ERR_INVALID_CONTROLLER_ID 
CAN_ERR_FILTER_ERROR 
CAN_ERR_FILTER_FULL 
CAN_ERR_STUFF_ERROR 
CAN_ERR_FORM_ERROR 
CAN_ERR_CRC_ERROR 
CAN_ERR_ACK_ERROR 
CAN_ERR_BIT_ERROR 
CAN_ERR_SYSTEM_ERROR 
CAN_ERR_PERMISSION_DENIED 
CAN_ERR_OPERATION_ABORTED 
CAN_ERR_FAIL 
CAN_ERR_RESOURCE_BUSY 
CAN_ERR_INVALID_STATE 
CAN_ERR_NOT_SUPPORTED 
CAN_ERR_TIMEOUT_ALT 
CAN_ERR_UNKNOWN 

◆ hf_can_frame_error_t

Why a frame is not well formed.

Same cases as vortex::contracts::FrameError except UnknownFlag, which cannot occur because hf_can_message_t keeps its flags as bools.

Enumerator
NONE 
ID_RANGE 

identifier does not fit 11 / 29 bits

CLASSIC_TOO_LONG 

classic frame with length > 8

FD_TOO_LONG 

FD frame with length > 64.

FD_LENGTH_NOT_EXACT 

FD length that has no DLC code (for example 9)

RTR_ON_FD 

FD frames have no remote form.

FD_BITS_ON_CLASSIC 

BRS / ESI without FD format.

RTR_WITH_DATA 

remote frame with a requested length above 8

Function Documentation

◆ HfCanDlcToLen()

constexpr hf_u8_t HfCanDlcToLen ( hf_u8_t  dlc,
bool  fd 
)
constexprnoexcept

DLC code to payload length in bytes (ISO 11898-1).

Parameters
dlcDLC code 0..15
fdtrue for an FD frame
Returns
Length in bytes. Classic codes 9..15 mean 8 bytes. Codes above 15 give 0.

◆ HfCanErrToString()

constexpr std::string_view HfCanErrToString ( hf_can_err_t  err)
constexprnoexcept

Convert hf_can_err_t to human-readable string.

Parameters
errThe error code to convert
Returns
String view of the error description

◆ HfCanIsValidLength()

constexpr bool HfCanIsValidLength ( hf_u8_t  len,
bool  fd 
)
constexprnoexcept

Is len a payload length a frame of this kind can have exactly?

Classic: 0..8. FD: 0..8, 12, 16, 20, 24, 32, 48, 64.

◆ HfCanLenToDlc()

constexpr hf_u8_t HfCanLenToDlc ( hf_u8_t  len,
bool  fd 
)
constexprnoexcept

Payload length in bytes to the smallest DLC code that carries it.

Returns
HF_CAN_DLC_INVALID when len is above 8 (classic) or 64 (FD).

◆ HfCanStateFromCounters()

constexpr hf_can_bus_state_t HfCanStateFromCounters ( hf_u16_t  tec,
hf_u16_t  rec 
)
constexprnoexcept

Error state implied by the counters (warning at 96, passive at 128, bus-off above 255).

Variable Documentation

◆ HF_CAN_CLASSIC_MAX_LEN

constexpr hf_u8_t HF_CAN_CLASSIC_MAX_LEN = 8U
inlineconstexpr

Largest classic CAN payload in bytes.

◆ HF_CAN_DLC_INVALID

constexpr hf_u8_t HF_CAN_DLC_INVALID = 0xFFU
inlineconstexpr

Returned by HfCanLenToDlc() when a length cannot be carried.

◆ HF_CAN_FD_MAX_LEN

constexpr hf_u8_t HF_CAN_FD_MAX_LEN = 64U
inlineconstexpr

Largest CAN FD payload in bytes.

◆ HF_CAN_MAX_EXT_ID

constexpr hf_u32_t HF_CAN_MAX_EXT_ID = 0x1FFFFFFFU
inlineconstexpr

Largest 29-bit (extended format) identifier.

◆ HF_CAN_MAX_STD_ID

constexpr hf_u32_t HF_CAN_MAX_STD_ID = 0x7FFU
inlineconstexpr

Largest 11-bit (base format) identifier.