|
libxr
1.0
Want to be the best embedded framework
|
CH32CAN driver (bxCAN-like) for LibXR::CAN. More...
#include <ch32_can.hpp>
Public Member Functions | |
| CH32CAN (ch32_can_id_t id, uint32_t pool_size) | |
| Construct CH32CAN. | |
| ErrorCode | Init () |
| Initialize filter + IRQ routing. Does NOT force a bitrate; call SetConfig(). | |
| ErrorCode | SetConfig (const CAN::Configuration &cfg) override |
| Set CAN configuration (bit timing + mode). Also (re-)enables IRQs. | |
| uint32_t | GetClockFreq () const override |
| CAN clock frequency (Hz). CH32 CAN is on APB1. | |
| ErrorCode | AddMessage (const ClassicPack &pack) override |
| Enqueue a ClassicPack for transmission. | |
| ErrorCode | GetErrorState (CAN::ErrorState &state) const override |
| Read bus error state and counters. | |
| void | ProcessRxInterrupt () |
| Process RX interrupt (call from CANx_RX0 / CANx_RX1 handlers). | |
| void | ProcessTxInterrupt () |
| Process TX interrupt (call from CANx_TX handler). | |
| void | ProcessErrorInterrupt () |
| Process SCE/error interrupt (call from CANx_SCE handler). | |
Public Member Functions inherited from LibXR::CAN | |
| 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. | |
Static Public Attributes | |
| static CH32CAN * | map [CH32_CAN_NUMBER] = {nullptr} |
Static Public Attributes inherited from LibXR::CAN | |
| static constexpr uint32_t | CAN_ERROR_ID_PREFIX = 0xFFFF0000u |
| 错误 ID 前缀 Error ID prefix. | |
Private Member Functions | |
| void | EnableIRQs () |
| void | DisableIRQs () |
| void | TxService () |
Static Private Member Functions | |
| static void | BuildTxMsg (const ClassicPack &p, CanTxMsg &m) |
Private Attributes | |
| CAN_TypeDef * | instance_ {nullptr} |
| ch32_can_id_t | id_ {CH32_CAN_ID_ERROR} |
| uint8_t | fifo_ {0} |
| uint8_t | filter_bank_ {0} |
| LockFreePool< ClassicPack > | tx_pool_ |
| std::atomic< uint32_t > | tx_lock_ {0} |
| std::atomic< uint32_t > | tx_pend_ {0} |
| CAN::Configuration | cfg_cache_ {} |
| CanRxMsg | rx_msg_ {} |
| CanTxMsg | tx_msg_ {} |
Additional Inherited Members | |
Public Types inherited from LibXR::CAN | |
| 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. | |
Static Public Member Functions inherited from LibXR::CAN | |
| 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. | |
Protected Member Functions inherited from LibXR::CAN | |
| void | OnMessage (const ClassicPack &pack, bool in_isr) |
| 分发接收到的经典 CAN 帧。 Dispatch a received classic CAN frame. | |
CH32CAN driver (bxCAN-like) for LibXR::CAN.
Design goals (aligned with your STM32CAN driver):
Notes:
Definition at line 30 of file ch32_can.hpp.
|
explicit |
Construct CH32CAN.
| id | CAN instance ID. |
| pool_size | TX pool size (number of ClassicPack entries). |
Definition at line 113 of file ch32_can.cpp.
|
overridevirtual |
Enqueue a ClassicPack for transmission.
Implements LibXR::CAN.
Definition at line 509 of file ch32_can.cpp.
|
inlinestaticprivate |
Definition at line 442 of file ch32_can.cpp.
|
private |
Definition at line 180 of file ch32_can.cpp.
|
private |
Definition at line 219 of file ch32_can.cpp.
|
overridevirtual |
CAN clock frequency (Hz). CH32 CAN is on APB1.
Implements LibXR::CAN.
Definition at line 435 of file ch32_can.cpp.
|
overridevirtual |
Read bus error state and counters.
Reimplemented from LibXR::CAN.
Definition at line 688 of file ch32_can.cpp.
| ErrorCode CH32CAN::Init | ( | ) |
Initialize filter + IRQ routing. Does NOT force a bitrate; call SetConfig().
Definition at line 151 of file ch32_can.cpp.
| void CH32CAN::ProcessErrorInterrupt | ( | ) |
Process SCE/error interrupt (call from CANx_SCE handler).
Definition at line 595 of file ch32_can.cpp.
| void CH32CAN::ProcessRxInterrupt | ( | ) |
Process RX interrupt (call from CANx_RX0 / CANx_RX1 handlers).
Definition at line 540 of file ch32_can.cpp.
| void CH32CAN::ProcessTxInterrupt | ( | ) |
Process TX interrupt (call from CANx_TX handler).
Definition at line 525 of file ch32_can.cpp.
|
overridevirtual |
Set CAN configuration (bit timing + mode). Also (re-)enables IRQs.
Implements LibXR::CAN.
Definition at line 308 of file ch32_can.cpp.
|
private |
Definition at line 458 of file ch32_can.cpp.
|
private |
Definition at line 105 of file ch32_can.hpp.
|
private |
Definition at line 96 of file ch32_can.hpp.
|
private |
Definition at line 97 of file ch32_can.hpp.
|
private |
Definition at line 94 of file ch32_can.hpp.
|
private |
Definition at line 93 of file ch32_can.hpp.
|
static |
Definition at line 82 of file ch32_can.hpp.
|
private |
Definition at line 108 of file ch32_can.hpp.
|
private |
Definition at line 101 of file ch32_can.hpp.
|
private |
Definition at line 109 of file ch32_can.hpp.
|
private |
Definition at line 102 of file ch32_can.hpp.
|
private |
Definition at line 99 of file ch32_can.hpp.