libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
can.cpp
1#include "can.hpp"
2
3using namespace LibXR;
4
7
8void CAN::Register(Callback cb, Type type, FilterMode mode, uint32_t start_id_mask,
9 uint32_t end_id_match)
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_match, type, cb});
15 subscriber_list_[static_cast<uint8_t>(type)].Add(*node);
16}
17
18void CAN::OnMessage(const ClassicPack &pack, bool in_isr)
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 {
26 case FilterMode::ID_MASK:
27 if ((pack.id & node.start_id_mask) == node.end_id_match)
28 {
29 node.cb.Run(in_isr, pack);
30 }
31 break;
32 case FilterMode::ID_RANGE:
33 if (pack.id >= node.start_id_mask && pack.id <= node.end_id_match)
34 {
35 node.cb.Run(in_isr, pack);
36 }
37 break;
38 }
39
40 return ErrorCode::OK;
41 });
42}
43
44void FDCAN::Register(CallbackFD cb, Type type, FilterMode mode, uint32_t start_id_mask,
45 uint32_t end_id_mask)
46{
47 ASSERT(type < Type::REMOTE_STANDARD);
48
49 auto node = new (std::align_val_t(LIBXR_CACHE_LINE_SIZE))
50 LockFreeList::Node<Filter>(Filter{mode, start_id_mask, end_id_mask, type, cb});
51 subscriber_list_fd_[static_cast<uint8_t>(type)].Add(*node);
52}
53
54void FDCAN::OnMessage(const FDPack &pack, bool in_isr)
55{
56 ASSERT(pack.type < Type::TYPE_NUM);
57 subscriber_list_fd_[static_cast<uint8_t>(pack.type)].Foreach<Filter>(
58 [&](Filter &node)
59 {
60 switch (node.mode)
61 {
62 case FilterMode::ID_MASK:
63 if ((pack.id & node.start_id_mask) == node.end_id_mask)
64 {
65 node.cb.Run(in_isr, pack);
66 }
67 break;
68 case FilterMode::ID_RANGE:
69 if (pack.id >= node.start_id_mask && pack.id <= node.end_id_mask)
70 {
71 node.cb.Run(in_isr, pack);
72 }
73 break;
74 }
75
76 return ErrorCode::OK;
77 });
78}
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.cpp:8
Type
CAN 消息类型 (Enumeration of CAN message types).
Definition can.hpp:20
@ REMOTE_STANDARD
远程标准 CAN 消息 (Remote standard CAN message).
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
Definition libxr_cb.hpp:124
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.cpp:44
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
LibXR 命名空间
Definition ch32_gpio.hpp:9