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>({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::TYPE_NUM);
48 auto node = new (std::align_val_t(LIBXR_CACHE_LINE_SIZE))
49 LockFreeList::Node<Filter>({mode, start_id_mask, end_id_mask, type, cb});
50 subscriber_list_fd_[static_cast<uint8_t>(type)].Add(*node);
51}
52
53void FDCAN::OnMessage(const FDPack &pack, bool in_isr)
54{
55 ASSERT(pack.type < Type::TYPE_NUM);
56 subscriber_list_fd_[static_cast<uint8_t>(pack.type)].Foreach<Filter>(
57 [&](Filter &node)
58 {
59 switch (node.mode)
60 {
61 case FilterMode::ID_MASK:
62 if ((pack.id & node.start_id_mask) == node.end_id_mask)
63 {
64 node.cb.Run(in_isr, pack);
65 }
66 break;
67 case FilterMode::ID_RANGE:
68 if (pack.id >= node.start_id_mask && pack.id <= node.end_id_mask)
69 {
70 node.cb.Run(in_isr, pack);
71 }
72 break;
73 }
74
75 return ErrorCode::OK;
76 });
77}
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
提供一个通用的回调包装,支持动态参数传递。 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