libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
can.hpp
1#pragma once
2
3#include "libxr.hpp"
4#include "message.hpp"
5
6namespace LibXR
7{
8
13class CAN
14{
15 public:
19 enum class Type : uint8_t
20 {
21 STANDARD = 0,
22 EXTENDED = 1,
23 REMOTE_STANDARD = 2,
24 REMOTE_EXTENDED = 3,
25 TYPE_NUM
26 };
27
34 CAN() {}
35
39 typedef struct __attribute__((packed))
40 {
41 uint32_t id;
42 Type type;
43 uint8_t data[8];
44 } ClassicPack;
45
47
48 enum class FilterMode : uint8_t
49 {
50 ID_MASK = 0,
51 ID_RANGE = 1
52 };
53
54 typedef struct
55 {
56 FilterMode mode;
57 uint32_t start_id_mask;
58 uint32_t end_id_match;
59 Type type;
60 Callback cb;
61 } Filter;
62
72 void Register(Callback cb, Type type, FilterMode mode = FilterMode::ID_RANGE,
73 uint32_t start_id_mask = 0, uint32_t end_id_match = UINT32_MAX)
74 {
75 ASSERT(type < Type::TYPE_NUM);
76
77 auto node = new (std::align_val_t(LIBXR_CACHE_LINE_SIZE))
78 LockFreeList::Node<Filter>({mode, start_id_mask, end_id_match, type, cb});
79 subscriber_list_[static_cast<uint8_t>(type)].Add(*node);
80 }
81
87 virtual ErrorCode AddMessage(const ClassicPack &pack) = 0;
88
89 protected:
90 void OnMessage(const ClassicPack &pack, bool in_isr)
91 {
92 ASSERT(pack.type < Type::TYPE_NUM);
93 subscriber_list_[static_cast<uint8_t>(pack.type)].Foreach<Filter>(
94 [&](Filter &node)
95 {
96 switch (node.mode)
97 {
98 case FilterMode::ID_MASK:
99 if ((pack.id & node.start_id_mask) == node.end_id_match)
100 {
101 node.cb.Run(in_isr, pack);
102 }
103 break;
104 case FilterMode::ID_RANGE:
105 if (pack.id >= node.start_id_mask && pack.id <= node.end_id_match)
106 {
107 node.cb.Run(in_isr, pack);
108 }
109 break;
110 }
111
112 return ErrorCode::OK;
113 });
114 }
115
116 private:
117 LockFreeList subscriber_list_[static_cast<uint8_t>(Type::TYPE_NUM)];
118};
119
125class FDCAN : public CAN
126{
127 public:
135 FDCAN() {}
136
140 typedef struct __attribute__((packed))
141 {
142 uint32_t id;
143 Type type;
144 uint8_t len;
145 uint8_t data[64];
146 } FDPack;
147
148 using CAN::AddMessage;
149 using CAN::FilterMode;
150 using CAN::OnMessage;
151
152 using CallbackFD = LibXR::Callback<const FDPack &>;
153
154 typedef struct
155 {
156 FilterMode mode;
157 uint32_t start_id_mask;
158 uint32_t end_id_mask;
159 Type type;
160 CallbackFD cb;
161 } Filter;
162
172 void Register(CallbackFD cb, Type type, FilterMode mode = FilterMode::ID_RANGE,
173 uint32_t start_id_mask = 0, uint32_t end_id_mask = UINT32_MAX)
174 {
175 ASSERT(type < Type::TYPE_NUM);
176 auto node = new (std::align_val_t(LIBXR_CACHE_LINE_SIZE))
177 LockFreeList::Node<Filter>({mode, start_id_mask, end_id_mask, type, cb});
178 subscriber_list_fd_[static_cast<uint8_t>(type)].Add(*node);
179 }
180
186 virtual ErrorCode AddMessage(const FDPack &pack) = 0;
187
188 protected:
189 void OnMessage(const FDPack &pack, bool in_isr)
190 {
191 ASSERT(pack.type < Type::TYPE_NUM);
192 subscriber_list_fd_[static_cast<uint8_t>(pack.type)].Foreach<Filter>(
193 [&](Filter &node)
194 {
195 if (pack.id >= node.start_id_mask && pack.id <= node.end_id_mask)
196 {
197 node.cb.Run(in_isr, pack);
198 }
199
200 return ErrorCode::OK;
201 });
202 }
203
204 private:
205 LockFreeList subscriber_list_fd_[static_cast<uint8_t>(Type::TYPE_NUM)];
206};
207
208} // namespace LibXR
CAN通信接口,定义标准CAN通信结构,支持不同类型的消息 (CAN communication interface that defines a standard CAN structure supp...
Definition can.hpp:14
void Register(Callback cb, Type type, FilterMode mode=FilterMode::ID_RANGE, uint32_t start_id_mask=0, uint32_t end_id_match=UINT32_MAX)
注册回调函数 Registers a callback function
Definition can.hpp:72
Type
CAN 消息类型 (Enumeration of CAN message types).
Definition can.hpp:20
@ EXTENDED
扩展 CAN 消息 (Extended CAN message).
@ REMOTE_EXTENDED
远程扩展 CAN 消息 (Remote extended CAN message).
@ STANDARD
标准 CAN 消息 (Standard CAN message).
@ REMOTE_STANDARD
远程标准 CAN 消息 (Remote standard CAN message).
virtual ErrorCode AddMessage(const ClassicPack &pack)=0
添加 CAN 消息到系统 (Adds a CAN message to the system).
struct __attribute__((packed))
经典 CAN 消息结构 (Structure representing a classic CAN message).
Definition can.hpp:39
CAN()
构造 CAN 对象,可指定主题名称和通信域 (Constructs a CAN object with an optional topic name and domain).
Definition can.hpp:34
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
Definition libxr_cb.hpp:125
void Run(bool in_isr, PassArgs &&...args) const
执行回调函数,并传递参数。 Executes the callback function, passing the arguments.
Definition libxr_cb.hpp:208
FDCAN 通信接口,扩展 CAN 功能,支持灵活数据速率(FD)CAN 消息 (FDCAN communication interface that extends CAN functionality...
Definition can.hpp:126
struct __attribute__((packed))
FD CAN 消息结构 (Structure representing an FD CAN message).
Definition can.hpp:140
FDCAN()
构造 FDCAN 对象,可指定主题名称和通信域 (Constructs an FDCAN object with optional topic names and domain).
Definition can.hpp:135
void Register(CallbackFD cb, Type type, FilterMode mode=FilterMode::ID_RANGE, uint32_t start_id_mask=0, uint32_t end_id_mask=UINT32_MAX)
注册回调函数 Registers a callback function
Definition can.hpp:172
virtual ErrorCode AddMessage(const FDPack &pack)=0
添加 FD CAN 消息到系统 (Adds an FD CAN message to the system).
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
LibXR 命名空间
Definition ch32_gpio.hpp:9