libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::CAN Class Referenceabstract

CAN 通信抽象类,定义经典 CAN 帧与订阅接口。 Abstract class for CAN communication with classic CAN frames and subscription API. More...

#include <can.hpp>

Inheritance diagram for LibXR::CAN:
[legend]
Collaboration diagram for LibXR::CAN:
[legend]

Data Structures

struct  BitTiming
 CAN 位时序配置。Bit timing configuration for CAN. More...
 
struct  ClassicPack
 经典 CAN 帧数据结构。Classic CAN frame structure. More...
 
struct  Configuration
 CAN 配置参数。CAN configuration parameters. More...
 
struct  ErrorState
 CAN 当前错误状态快照(来自硬件计数器/状态机)。 Snapshot of current CAN controller error state (from HW counters/state). More...
 
struct  Filter
 经典 CAN 订阅过滤器。Classic CAN subscription filter. More...
 
struct  Mode
 CAN 工作模式。CAN operating mode. More...
 

Public Types

enum class  Type : uint8_t {
  STANDARD = 0 , EXTENDED = 1 , REMOTE_STANDARD = 2 , REMOTE_EXTENDED = 3 ,
  ERROR = 4 , TYPE_NUM = 5
}
 CAN 消息类型。CAN frame type. More...
 
enum class  ErrorID : uint32_t {
  CAN_ERROR_ID_GENERIC = CAN_ERROR_ID_PREFIX , CAN_ERROR_ID_BUS_OFF = CAN_ERROR_ID_PREFIX + 1 , CAN_ERROR_ID_ERROR_PASSIVE = CAN_ERROR_ID_PREFIX + 2 , CAN_ERROR_ID_ERROR_WARNING = CAN_ERROR_ID_PREFIX + 3 ,
  CAN_ERROR_ID_PROTOCOL = CAN_ERROR_ID_PREFIX + 4 , CAN_ERROR_ID_ACK = CAN_ERROR_ID_PREFIX + 5 , CAN_ERROR_ID_STUFF = CAN_ERROR_ID_PREFIX + 6 , CAN_ERROR_ID_FORM = CAN_ERROR_ID_PREFIX + 7 ,
  CAN_ERROR_ID_BIT0 = CAN_ERROR_ID_PREFIX + 8 , CAN_ERROR_ID_BIT1 = CAN_ERROR_ID_PREFIX + 9 , CAN_ERROR_ID_CRC = CAN_ERROR_ID_PREFIX + 10 , CAN_ERROR_ID_OTHER = CAN_ERROR_ID_PREFIX + 11
}
 ClassicPack::type == Type::ERROR 时使用的虚拟 ID。 Virtual IDs used when ClassicPack::type == Type::ERROR. More...
 
enum class  FilterMode : uint8_t { ID_MASK = 0 , ID_RANGE = 1 }
 CAN 过滤器模式。CAN filter mode. More...
 
using Callback = LibXR::Callback<const ClassicPack &>
 回调类型。Callback type.
 

Public Member Functions

virtual ErrorCode SetConfig (const CAN::Configuration &cfg)=0
 设置 CAN 配置。Set CAN configuration.
 
virtual uint32_t GetClockFreq () const =0
 获取 CAN 外设时钟频率(Hz)。 Get CAN peripheral clock frequency in Hz.
 
virtual ErrorCode GetErrorState (ErrorState &state) const
 查询当前错误状态(快照)。 Query current CAN controller error state (snapshot).
 
 CAN ()=default
 构造函数。Constructor.
 
virtual ~CAN ()=default
 虚析构函数。Virtual destructor.
 
void Register (Callback cb, Type type, FilterMode mode=FilterMode::ID_RANGE, uint32_t start_id_mask=0, uint32_t end_id_mask=UINT32_MAX)
 注册经典 CAN 消息回调。 Register classic CAN message callback.
 
virtual ErrorCode AddMessage (const ClassicPack &pack)=0
 添加经典 CAN 消息。Add classic CAN message.
 

Static Public Member Functions

static constexpr uint32_t FromErrorID (ErrorID e) noexcept
 将 ErrorID 转为 id。Convert ErrorID to ClassicPack::id.
 
static constexpr bool IsErrorId (uint32_t id) noexcept
 判断 id 是否处于错误 ID 空间。Check if id is in error ID space.
 
static constexpr ErrorID ToErrorID (uint32_t id) noexcept
 将 id 解释为 ErrorID。Interpret id as ErrorID.
 

Static Public Attributes

static constexpr uint32_t CAN_ERROR_ID_PREFIX = 0xFFFF0000u
 错误 ID 前缀 Error ID prefix.
 

Protected Member Functions

void OnMessage (const ClassicPack &pack, bool in_isr)
 分发接收到的经典 CAN 帧。 Dispatch a received classic CAN frame.
 

Private Attributes

LockFreeList subscriber_list_ [static_cast< uint8_t >(Type::TYPE_NUM)]
 按帧类型划分的订阅者链表数组。Subscriber lists per frame type.
 

Detailed Description

CAN 通信抽象类,定义经典 CAN 帧与订阅接口。 Abstract class for CAN communication with classic CAN frames and subscription API.

Definition at line 15 of file can.hpp.

Member Typedef Documentation

◆ Callback

回调类型。Callback type.

Definition at line 180 of file can.hpp.

Member Enumeration Documentation

◆ ErrorID

enum class LibXR::CAN::ErrorID : uint32_t
strong

ClassicPack::type == Type::ERROR 时使用的虚拟 ID。 Virtual IDs used when ClassicPack::type == Type::ERROR.

Definition at line 145 of file can.hpp.

146 {
147 CAN_ERROR_ID_GENERIC = CAN_ERROR_ID_PREFIX,
148 CAN_ERROR_ID_BUS_OFF = CAN_ERROR_ID_PREFIX + 1,
149 CAN_ERROR_ID_ERROR_PASSIVE = CAN_ERROR_ID_PREFIX + 2,
150 CAN_ERROR_ID_ERROR_WARNING = CAN_ERROR_ID_PREFIX + 3,
151 CAN_ERROR_ID_PROTOCOL = CAN_ERROR_ID_PREFIX + 4,
152 CAN_ERROR_ID_ACK = CAN_ERROR_ID_PREFIX + 5,
153 CAN_ERROR_ID_STUFF = CAN_ERROR_ID_PREFIX + 6,
154 CAN_ERROR_ID_FORM = CAN_ERROR_ID_PREFIX + 7,
155 CAN_ERROR_ID_BIT0 = CAN_ERROR_ID_PREFIX + 8,
156 CAN_ERROR_ID_BIT1 = CAN_ERROR_ID_PREFIX + 9,
157 CAN_ERROR_ID_CRC = CAN_ERROR_ID_PREFIX + 10,
158 CAN_ERROR_ID_OTHER = CAN_ERROR_ID_PREFIX + 11
159 };
static constexpr uint32_t CAN_ERROR_ID_PREFIX
错误 ID 前缀 Error ID prefix.
Definition can.hpp:138

◆ FilterMode

enum class LibXR::CAN::FilterMode : uint8_t
strong

CAN 过滤器模式。CAN filter mode.

Enumerator
ID_MASK 

掩码匹配:(id & start_id_mask) == end_id_mask。 Mask match: (id & start_id_mask) == end_id_mask.

ID_RANGE 

区间匹配:start_id_mask <= id <= end_id_mask。 Range match: start_id_mask <= id <= end_id_mask.

Definition at line 186 of file can.hpp.

187 {
188 ID_MASK = 0,
190 ID_RANGE = 1
192 };

◆ Type

enum class LibXR::CAN::Type : uint8_t
strong

CAN 消息类型。CAN frame type.

Enumerator
STANDARD 

标准数据帧(11-bit ID)。Standard data frame (11-bit ID).

EXTENDED 

扩展数据帧(29-bit ID)。Extended data frame (29-bit ID).

REMOTE_STANDARD 

标准远程帧。Standard remote frame.

REMOTE_EXTENDED 

扩展远程帧。Extended remote frame.

ERROR 

错误帧(虚拟事件)。Error frame (virtual event).

TYPE_NUM 

类型数量上界。Number of frame types.

Definition at line 22 of file can.hpp.

23 {
24 STANDARD = 0,
25 EXTENDED = 1,
26 REMOTE_STANDARD = 2,
27 REMOTE_EXTENDED = 3,
28 ERROR = 4,
29 TYPE_NUM = 5
30 };
@ EXTENDED
扩展数据帧(29-bit ID)。Extended data frame (29-bit ID).
@ REMOTE_EXTENDED
扩展远程帧。Extended remote frame.
@ STANDARD
标准数据帧(11-bit ID)。Standard data frame (11-bit ID).
@ REMOTE_STANDARD
标准远程帧。Standard remote frame.
@ ERROR
错误帧(虚拟事件)。Error frame (virtual event).
@ TYPE_NUM
类型数量上界。Number of frame types.

Member Function Documentation

◆ AddMessage()

virtual ErrorCode LibXR::CAN::AddMessage ( const ClassicPack & pack)
pure virtual

添加经典 CAN 消息。Add classic CAN message.

Parameters
pack经典 CAN 帧。Classic CAN frame.
Returns
ErrorCode 操作结果。Error code.

Implemented in LibXR::FDCAN, LibXR::STM32CAN, and LibXR::STM32CANFD.

◆ FromErrorID()

static constexpr uint32_t LibXR::CAN::FromErrorID ( ErrorID e)
inlinestaticconstexprnoexcept

将 ErrorID 转为 id。Convert ErrorID to ClassicPack::id.

Definition at line 162 of file can.hpp.

163 {
164 return static_cast<uint32_t>(e);
165 }

◆ GetClockFreq()

virtual uint32_t LibXR::CAN::GetClockFreq ( ) const
pure virtual

获取 CAN 外设时钟频率(Hz)。 Get CAN peripheral clock frequency in Hz.

Implemented in LibXR::STM32CAN, and LibXR::STM32CANFD.

◆ GetErrorState()

virtual ErrorCode LibXR::CAN::GetErrorState ( ErrorState & state) const
inlinevirtual

查询当前错误状态(快照)。 Query current CAN controller error state (snapshot).

默认实现返回 ErrorCode::NOT_SUPPORT;具体实现(如 bxCAN/FDCAN)可重载, 从硬件寄存器读取 TEC/REC 及状态位,并填充 ErrorState。

Parameters
state输出参数,用于返回当前错误状态。
Returns
ErrorCode 操作结果;若未实现则返回 ErrorCode::NOT_SUPPORT。

Reimplemented in LibXR::STM32CAN, and LibXR::STM32CANFD.

Definition at line 107 of file can.hpp.

108 {
109 (void)state;
110 return ErrorCode::NOT_SUPPORT;
111 }

◆ IsErrorId()

static constexpr bool LibXR::CAN::IsErrorId ( uint32_t id)
inlinestaticconstexprnoexcept

判断 id 是否处于错误 ID 空间。Check if id is in error ID space.

Definition at line 168 of file can.hpp.

169 {
170 return (id & 0xFFFF0000u) == CAN_ERROR_ID_PREFIX;
171 }

◆ OnMessage()

void CAN::OnMessage ( const ClassicPack & pack,
bool in_isr )
protected

分发接收到的经典 CAN 帧。 Dispatch a received classic CAN frame.

Parameters
pack接收到的帧。Received frame.
in_isr是否在中断上下文中。True if called in ISR context.

Definition at line 18 of file can.cpp.

19{
20 ASSERT(pack.type < Type::TYPE_NUM);
21 subscriber_list_[static_cast<uint8_t>(pack.type)].Foreach<Filter>(
22 [&](Filter &node)
23 {
24 switch (node.mode)
25 {
27 if ((pack.id & node.start_id_mask) == node.end_id_mask)
28 {
29 node.cb.Run(in_isr, pack);
30 }
31 break;
33 if (pack.id >= node.start_id_mask && pack.id <= node.end_id_mask)
34 {
35 node.cb.Run(in_isr, pack);
36 }
37 break;
38 }
39
40 return ErrorCode::OK;
41 });
42}
LockFreeList subscriber_list_[static_cast< uint8_t >(Type::TYPE_NUM)]
按帧类型划分的订阅者链表数组。Subscriber lists per frame type.
Definition can.hpp:238

◆ Register()

void CAN::Register ( Callback cb,
Type type,
FilterMode mode = FilterMode::ID_RANGE,
uint32_t start_id_mask = 0,
uint32_t end_id_mask = UINT32_MAX )

注册经典 CAN 消息回调。 Register classic CAN message callback.

Parameters
cb回调函数。Callback function.
type帧类型。Frame type.
mode过滤器模式。Filter mode.
start_id_mask起始 ID 或掩码。Start ID or mask.
end_id_mask结束 ID 或匹配值。End ID or match value.

Definition at line 8 of file can.cpp.

10{
11 ASSERT(type < Type::TYPE_NUM);
12
13 auto node = new (std::align_val_t(LIBXR_CACHE_LINE_SIZE))
14 LockFreeList::Node<Filter>(Filter{mode, start_id_mask, end_id_mask, type, cb});
15 subscriber_list_[static_cast<uint8_t>(type)].Add(*node);
16}
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...

◆ SetConfig()

virtual ErrorCode LibXR::CAN::SetConfig ( const CAN::Configuration & cfg)
pure virtual

设置 CAN 配置。Set CAN configuration.

Parameters
cfg配置参数。Configuration parameters.
Returns
ErrorCode 操作结果。Error code.

Implemented in LibXR::FDCAN, LibXR::STM32CAN, and LibXR::STM32CANFD.

◆ ToErrorID()

static constexpr ErrorID LibXR::CAN::ToErrorID ( uint32_t id)
inlinestaticconstexprnoexcept

将 id 解释为 ErrorID。Interpret id as ErrorID.

Definition at line 174 of file can.hpp.

175 {
176 return static_cast<ErrorID>(id);
177 }
ErrorID
ClassicPack::type == Type::ERROR 时使用的虚拟 ID。 Virtual IDs used when ClassicPack::type == Type::ERROR.
Definition can.hpp:146

Field Documentation

◆ CAN_ERROR_ID_PREFIX

uint32_t LibXR::CAN::CAN_ERROR_ID_PREFIX = 0xFFFF0000u
staticconstexpr

错误 ID 前缀 Error ID prefix.

Definition at line 138 of file can.hpp.

◆ subscriber_list_

LockFreeList LibXR::CAN::subscriber_list_[static_cast< uint8_t >(Type::TYPE_NUM)]
private

按帧类型划分的订阅者链表数组。Subscriber lists per frame type.

Definition at line 238 of file can.hpp.


The documentation for this class was generated from the following files: