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

CH32 CAN 驱动实现 / CH32 CAN driver implementation. More...

#include <ch32_can.hpp>

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

Public Member Functions

 CH32CAN (ch32_can_id_t id, uint32_t pool_size)
 构造 CAN 驱动对象 / Construct CAN driver object
 
ErrorCode Init ()
 初始化过滤器和中断路由 / Initialize filters and IRQ routing
 
ErrorCode SetConfig (const CAN::Configuration &cfg) override
 设置 CAN 配置 / Set CAN configuration
 
uint32_t GetClockFreq () const override
 获取 CAN 时钟频率 / Get CAN clock frequency
 
ErrorCode AddMessage (const ClassicPack &pack) override
 发送消息入队 / Enqueue TX message
 
ErrorCode GetErrorState (CAN::ErrorState &state) const override
 获取总线错误状态 / Get bus error state
 
void ProcessRxInterrupt ()
 处理接收中断 / Handle RX interrupt
 
void ProcessTxInterrupt ()
 处理发送中断 / Handle TX interrupt
 
void ProcessErrorInterrupt ()
 处理错误中断 / Handle error interrupt
 
- 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 CH32CANmap [CH32_CAN_NUMBER] = {nullptr}
 中断分发表 / IRQ dispatch map
 
- 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< ClassicPacktx_pool_
 
std::atomic< uint32_t > tx_lock_ {0}
 
std::atomic< uint32_t > tx_pend_ {0}
 
CAN::Configuration cfg_cache_ {}
 缓存配置(用于保留原值语义) / Cached configuration for keep-previous semantics
 
CanRxMsg rx_msg_ {}
 中断上下文收发缓冲 / RX/TX buffers in IRQ context
 
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.
 

Detailed Description

CH32 CAN 驱动实现 / CH32 CAN driver implementation.

设计目标与 STM32 CAN 驱动行为保持一致。 Designed to keep behavior aligned with the STM32 CAN driver.

Note
基于 WCH StdPeriph CAN 接口实现。 / Implemented on top of WCH StdPeriph CAN APIs.

Definition at line 23 of file ch32_can.hpp.

Constructor & Destructor Documentation

◆ CH32CAN()

CH32CAN::CH32CAN ( ch32_can_id_t id,
uint32_t pool_size )
explicit

构造 CAN 驱动对象 / Construct CAN driver object

Parameters
idCAN 实例编号 / CAN instance ID
pool_size发送池大小 / TX pool size (ClassicPack entries)

Definition at line 128 of file ch32_can.cpp.

129 : CAN(), instance_(CH32_CAN_GetInstanceID(id)), id_(id), tx_pool_(pool_size)
130{
131 if constexpr (LibXR::CH32UsbCanShared::usb_can_share_enabled())
132 {
133#if defined(CAN1) && !defined(CAN2)
134 const bool USB_ALREADY_INITED =
135 LibXR::CH32UsbCanShared::usb_inited.load(std::memory_order_acquire);
136 // 在 USB/CAN 共享中断拓扑下,CAN1 必须先于 USB 初始化。
137 // On shared USB/CAN interrupt configurations, CAN1 must initialize before USB.
138 ASSERT(USB_ALREADY_INITED == false);
139#endif
140 }
141
142#if defined(CAN2)
143 if (id == CH32_CAN1)
144 {
145 filter_bank_ = 0;
146 fifo_ = CAN_FilterFIFO0; // 0
147 }
148 else
149 {
150 filter_bank_ = 14;
151 fifo_ = CAN_FilterFIFO1; // 1
152 }
153#else
154 filter_bank_ = 0;
155 fifo_ = CAN_FilterFIFO0; // 0
156#endif
157
158 ASSERT(id_ < CH32_CAN_NUMBER);
159 ASSERT(instance_ != nullptr);
160
161 map[id_] = this;
162
163 // 打开外设时钟。
164 // Enable the peripheral clock.
165 RCC_APB1PeriphClockCmd(CH32_CAN_RCC_PERIPH_MAP[id_], ENABLE);
166
167 // 在调用 SetConfig() 之前,保持 CAN 处于初始化模式。
168 // Keep CAN in initialization mode until SetConfig() is called.
169 (void)CAN_OperatingModeRequest(instance_, CAN_OperatingMode_Initialization);
170
171#if defined(CAN2)
172 // 在双 CAN 变体上,配置默认的共享过滤器分界点。
173 // On dual-CAN variants, configure the default shared filter split point.
174 CAN_SlaveStartBank(CH32_CAN_DEFAULT_SLAVE_START_BANK);
175#endif
176
177 (void)Init();
178}
CAN()=default
构造函数。Constructor.
ErrorCode Init()
初始化过滤器和中断路由 / Initialize filters and IRQ routing
Definition ch32_can.cpp:180
static CH32CAN * map[CH32_CAN_NUMBER]
中断分发表 / IRQ dispatch map
Definition ch32_can.hpp:76

Member Function Documentation

◆ AddMessage()

ErrorCode CH32CAN::AddMessage ( const ClassicPack & pack)
overridevirtual

发送消息入队 / Enqueue TX message

Implements LibXR::CAN.

Definition at line 554 of file ch32_can.cpp.

555{
556 if (pack.type == Type::ERROR)
557 {
558 return ErrorCode::ARG_ERR;
559 }
560
561 if (tx_pool_.Put(pack) != ErrorCode::OK)
562 {
563 return ErrorCode::FULL;
564 }
565
566 TxService();
567 return ErrorCode::OK;
568}
@ ERROR
错误帧(虚拟事件)。Error frame (virtual event).
ErrorCode Put(const Data &data)
向池中放入一个元素 / Put an element into the pool
@ FULL
已满 | Full
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error

◆ BuildTxMsg()

void CH32CAN::BuildTxMsg ( const ClassicPack & p,
CanTxMsg & m )
inlinestaticprivate

Definition at line 484 of file ch32_can.cpp.

485{
486 const bool IS_EXT = (p.type == Type::EXTENDED) || (p.type == Type::REMOTE_EXTENDED);
487 const bool IS_RTR =
488 (p.type == Type::REMOTE_STANDARD) || (p.type == Type::REMOTE_EXTENDED);
489
490 m.DLC = (p.dlc <= 8u) ? static_cast<uint8_t>(p.dlc) : 8u;
491 m.IDE = IS_EXT ? CAN_ID_EXT : CAN_ID_STD;
492 m.RTR = IS_RTR ? CAN_RTR_REMOTE : CAN_RTR_DATA;
493
494 m.StdId = IS_EXT ? 0u : (p.id & 0x7FFu);
495 m.ExtId = IS_EXT ? (p.id & 0x1FFFFFFFu) : 0u;
496
497 std::memcpy(m.Data, p.data, 8);
498}
@ EXTENDED
扩展数据帧(29-bit ID)。Extended data frame (29-bit ID).
@ REMOTE_EXTENDED
扩展远程帧。Extended remote frame.
@ REMOTE_STANDARD
标准远程帧。Standard remote frame.

◆ DisableIRQs()

void CH32CAN::DisableIRQs ( )
private

Definition at line 220 of file ch32_can.cpp.

221{
222 uint32_t it = 0u;
223
224#ifdef CAN_IT_FMP0
225 if (fifo_ == 0u)
226 {
227 it |= CAN_IT_FMP0;
228 }
229#endif
230#ifdef CAN_IT_FMP1
231 if (fifo_ == 1u)
232 {
233 it |= CAN_IT_FMP1;
234 }
235#endif
236
237#ifdef CAN_IT_TME
238 it |= CAN_IT_TME;
239#endif
240
241#ifdef CAN_IT_ERR
242 it |= CAN_IT_ERR;
243#endif
244#ifdef CAN_IT_BOF
245 it |= CAN_IT_BOF;
246#endif
247#ifdef CAN_IT_EPV
248 it |= CAN_IT_EPV;
249#endif
250#ifdef CAN_IT_EWG
251 it |= CAN_IT_EWG;
252#endif
253#ifdef CAN_IT_LEC
254 it |= CAN_IT_LEC;
255#endif
256
257 if (it != 0u)
258 {
259 CAN_ITConfig(instance_, it, DISABLE);
260 }
261}

◆ EnableIRQs()

void CH32CAN::EnableIRQs ( )
private

Definition at line 263 of file ch32_can.cpp.

264{
265 uint32_t it = 0u;
266
267#ifdef CAN_IT_FMP0
268 if (fifo_ == 0u)
269 {
270 it |= CAN_IT_FMP0;
271 }
272#endif
273#ifdef CAN_IT_FMP1
274 if (fifo_ == 1u)
275 {
276 it |= CAN_IT_FMP1;
277 }
278#endif
279
280#ifdef CAN_IT_TME
281 it |= CAN_IT_TME;
282#endif
283
284#ifdef CAN_IT_ERR
285 it |= CAN_IT_ERR;
286#endif
287#ifdef CAN_IT_BOF
288 it |= CAN_IT_BOF;
289#endif
290#ifdef CAN_IT_EPV
291 it |= CAN_IT_EPV;
292#endif
293#ifdef CAN_IT_EWG
294 it |= CAN_IT_EWG;
295#endif
296#ifdef CAN_IT_LEC
297 it |= CAN_IT_LEC;
298#endif
299
300 if (it != 0u)
301 {
302 // Clear pending interrupt flags before enabling interrupt sources.
303#ifdef CAN_IT_TME
304 if ((it & CAN_IT_TME) != 0u)
305 {
306 CAN_ClearITPendingBit(instance_, CAN_IT_TME);
307 }
308#endif
309
310#ifdef CAN_IT_FMP0
311 if (fifo_ == 0u && ((it & CAN_IT_FMP0) != 0u))
312 {
313 CAN_ClearITPendingBit(instance_, CAN_IT_FMP0);
314 }
315#endif
316#ifdef CAN_IT_FMP1
317 if (fifo_ == 1u && ((it & CAN_IT_FMP1) != 0u))
318 {
319 CAN_ClearITPendingBit(instance_, CAN_IT_FMP1);
320 }
321#endif
322
323 CAN_ITConfig(instance_, it, ENABLE);
324 }
325}

◆ GetClockFreq()

uint32_t CH32CAN::GetClockFreq ( ) const
overridevirtual

获取 CAN 时钟频率 / Get CAN clock frequency

Implements LibXR::CAN.

Definition at line 477 of file ch32_can.cpp.

478{
479 RCC_ClocksTypeDef clocks{};
480 RCC_GetClocksFreq(&clocks);
481 return clocks.PCLK1_Frequency;
482}

◆ GetErrorState()

ErrorCode CH32CAN::GetErrorState ( CAN::ErrorState & state) const
overridevirtual

获取总线错误状态 / Get bus error state

Reimplemented from LibXR::CAN.

Definition at line 741 of file ch32_can.cpp.

742{
743 if (instance_ == nullptr)
744 {
745 return ErrorCode::ARG_ERR;
746 }
747
748 state.rx_error_counter = CAN_GetReceiveErrorCounter(instance_);
749 state.tx_error_counter = CAN_GetLSBTransmitErrorCounter(instance_);
750
751 state.bus_off = (CAN_GetFlagStatus(instance_, CAN_FLAG_BOF) != RESET);
752 state.error_passive = (CAN_GetFlagStatus(instance_, CAN_FLAG_EPV) != RESET);
753 state.error_warning = (CAN_GetFlagStatus(instance_, CAN_FLAG_EWG) != RESET);
754
755 return ErrorCode::OK;
756}
bool bus_off
是否处于 BUS-OFF。True if controller is bus-off.
Definition can.hpp:92
bool error_passive
是否处于 Error Passive。True if error-passive.
Definition can.hpp:93
uint8_t tx_error_counter
发送错误计数 TEC。Transmit error counter (TEC).
Definition can.hpp:89
bool error_warning
是否处于 Error Warning。True if error-warning.
Definition can.hpp:94
uint8_t rx_error_counter
接收错误计数 REC。Receive error counter (REC).
Definition can.hpp:90

◆ Init()

ErrorCode CH32CAN::Init ( )

初始化过滤器和中断路由 / Initialize filters and IRQ routing

Definition at line 180 of file ch32_can.cpp.

181{
182 if (instance_ == nullptr)
183 {
184 return ErrorCode::ARG_ERR;
185 }
186
187 // 默认全接收过滤器(ID-mask 模式,全 0)。
188 // Default accept-all filter (ID-mask mode, all zeros).
189 CAN_FilterInitTypeDef f = {};
190 f.CAN_FilterIdHigh = 0u;
191 f.CAN_FilterIdLow = 0u;
192 f.CAN_FilterMaskIdHigh = 0u;
193 f.CAN_FilterMaskIdLow = 0u;
194 f.CAN_FilterFIFOAssignment = fifo_;
195 f.CAN_FilterNumber = filter_bank_;
196 f.CAN_FilterMode = CAN_FilterMode_IdMask;
197 f.CAN_FilterScale = CAN_FilterScale_32bit;
198 f.CAN_FilterActivation = ENABLE;
199
200 CAN_FilterInit(&f);
201
202 EnableIRQs();
203
204 // 为当前 CAN 实例打开 NVIC。
205 // Enable NVIC for this CAN instance.
206 ch32_can_enable_nvic(id_, fifo_);
207
208 if constexpr (LibXR::CH32UsbCanShared::usb_can_share_enabled())
209 {
210#if defined(CAN1) && !defined(CAN2)
211 LibXR::CH32UsbCanShared::register_can1_rx0(&can1_rx0_thunk);
212 LibXR::CH32UsbCanShared::register_can1_tx(&can1_tx_thunk);
213 LibXR::CH32UsbCanShared::can1_inited.store(true, std::memory_order_release);
214#endif
215 }
216
217 return ErrorCode::OK;
218}

◆ ProcessErrorInterrupt()

void CH32CAN::ProcessErrorInterrupt ( )

处理错误中断 / Handle error interrupt

Definition at line 646 of file ch32_can.cpp.

647{
648 if (instance_ == nullptr)
649 {
650 return;
651 }
652
653 // 在清 pending 位前先快照错误标志和 LEC。
654 // Snapshot error flags and LEC before clearing pending bits.
655 const bool BOF = (CAN_GetFlagStatus(instance_, CAN_FLAG_BOF) != RESET);
656 const bool EPV = (CAN_GetFlagStatus(instance_, CAN_FLAG_EPV) != RESET);
657 const bool EWG = (CAN_GetFlagStatus(instance_, CAN_FLAG_EWG) != RESET);
658 const uint8_t LEC = CAN_GetLastErrorCode(instance_);
659
660#ifdef CAN_IT_LEC
661 if (CAN_GetITStatus(instance_, CAN_IT_LEC) != RESET)
662 {
663 CAN_ClearITPendingBit(instance_, CAN_IT_LEC);
664 }
665#endif
666#ifdef CAN_IT_ERR
667 if (CAN_GetITStatus(instance_, CAN_IT_ERR) != RESET)
668 {
669 CAN_ClearITPendingBit(instance_, CAN_IT_ERR);
670 }
671#endif
672#ifdef CAN_IT_BOF
673 if (CAN_GetITStatus(instance_, CAN_IT_BOF) != RESET)
674 {
675 CAN_ClearITPendingBit(instance_, CAN_IT_BOF);
676 }
677#endif
678#ifdef CAN_IT_EPV
679 if (CAN_GetITStatus(instance_, CAN_IT_EPV) != RESET)
680 {
681 CAN_ClearITPendingBit(instance_, CAN_IT_EPV);
682 }
683#endif
684#ifdef CAN_IT_EWG
685 if (CAN_GetITStatus(instance_, CAN_IT_EWG) != RESET)
686 {
687 CAN_ClearITPendingBit(instance_, CAN_IT_EWG);
688 }
689#endif
690
691 ClassicPack p{};
692 p.type = Type::ERROR;
693 p.dlc = 0u;
694
695 CAN::ErrorID eid = CAN::ErrorID::CAN_ERROR_ID_GENERIC;
696
697 if (BOF)
698 {
699 eid = CAN::ErrorID::CAN_ERROR_ID_BUS_OFF;
700 }
701 else if (EPV)
702 {
703 eid = CAN::ErrorID::CAN_ERROR_ID_ERROR_PASSIVE;
704 }
705 else if (EWG)
706 {
707 eid = CAN::ErrorID::CAN_ERROR_ID_ERROR_WARNING;
708 }
709 else
710 {
711 switch (LEC)
712 {
713 case CAN_ErrorCode_StuffErr:
714 eid = CAN::ErrorID::CAN_ERROR_ID_STUFF;
715 break;
716 case CAN_ErrorCode_FormErr:
717 eid = CAN::ErrorID::CAN_ERROR_ID_FORM;
718 break;
719 case CAN_ErrorCode_ACKErr:
720 eid = CAN::ErrorID::CAN_ERROR_ID_ACK;
721 break;
722 case CAN_ErrorCode_BitRecessiveErr:
723 eid = CAN::ErrorID::CAN_ERROR_ID_BIT1;
724 break;
725 case CAN_ErrorCode_BitDominantErr:
726 eid = CAN::ErrorID::CAN_ERROR_ID_BIT0;
727 break;
728 case CAN_ErrorCode_CRCErr:
729 eid = CAN::ErrorID::CAN_ERROR_ID_CRC;
730 break;
731 default:
732 eid = CAN::ErrorID::CAN_ERROR_ID_OTHER;
733 break;
734 }
735 }
736
737 p.id = static_cast<uint32_t>(eid);
738 OnMessage(p, true);
739}
void OnMessage(const ClassicPack &pack, bool in_isr)
分发接收到的经典 CAN 帧。 Dispatch a received classic CAN frame.
Definition can.cpp:18
ErrorID
ClassicPack::type == Type::ERROR 时使用的虚拟 ID。 Virtual IDs used when ClassicPack::type == Type::ERROR.
Definition can.hpp:146

◆ ProcessRxInterrupt()

void CH32CAN::ProcessRxInterrupt ( )

处理接收中断 / Handle RX interrupt

Definition at line 588 of file ch32_can.cpp.

589{
590 if (instance_ == nullptr)
591 {
592 return;
593 }
594
595 // 先排空 RX FIFO,再确认 RX 挂起标志。
596 // Drain the RX FIFO first, then acknowledge RX pending flags.
597 while (CAN_MessagePending(instance_, fifo_) != 0u)
598 {
599 CAN_Receive(instance_, fifo_, &rx_msg_);
600
601 ClassicPack p{};
602 if (rx_msg_.IDE == CAN_ID_STD)
603 {
604 p.id = rx_msg_.StdId;
605 p.type = Type::STANDARD;
606 }
607 else
608 {
609 p.id = rx_msg_.ExtId;
610 p.type = Type::EXTENDED;
611 }
612
613 if (rx_msg_.RTR == CAN_RTR_REMOTE)
614 {
616 }
617
618 p.dlc = (rx_msg_.DLC <= 8u) ? rx_msg_.DLC : 8u;
619 std::memcpy(p.data, rx_msg_.Data, 8);
620
621 OnMessage(p, true);
622 }
623
624 // 确认 RX 挂起标志。
625 // Acknowledge RX pending flags.
626#ifdef CAN_IT_FMP0
627 if (fifo_ == 0u)
628 {
629 if (CAN_GetITStatus(instance_, CAN_IT_FMP0) != RESET)
630 {
631 CAN_ClearITPendingBit(instance_, CAN_IT_FMP0);
632 }
633 }
634#endif
635#ifdef CAN_IT_FMP1
636 if (fifo_ == 1u)
637 {
638 if (CAN_GetITStatus(instance_, CAN_IT_FMP1) != RESET)
639 {
640 CAN_ClearITPendingBit(instance_, CAN_IT_FMP1);
641 }
642 }
643#endif
644}
Type
CAN 消息类型。CAN frame type.
Definition can.hpp:23
@ STANDARD
标准数据帧(11-bit ID)。Standard data frame (11-bit ID).
CanRxMsg rx_msg_
中断上下文收发缓冲 / RX/TX buffers in IRQ context
Definition ch32_can.hpp:102

◆ ProcessTxInterrupt()

void CH32CAN::ProcessTxInterrupt ( )

处理发送中断 / Handle TX interrupt

Definition at line 570 of file ch32_can.cpp.

571{
572#ifdef CAN_IT_TME
573 if (instance_ == nullptr)
574 {
575 return;
576 }
577
578 if (CAN_GetITStatus(instance_, CAN_IT_TME) != RESET)
579 {
580 CAN_ClearITPendingBit(instance_, CAN_IT_TME);
581 TxService();
582 }
583#else
584 TxService();
585#endif
586}

◆ SetConfig()

ErrorCode CH32CAN::SetConfig ( const CAN::Configuration & cfg)
overridevirtual

设置 CAN 配置 / Set CAN configuration

Implements LibXR::CAN.

Definition at line 352 of file ch32_can.cpp.

353{
354 if (instance_ == nullptr)
355 {
356 return ErrorCode::ARG_ERR;
357 }
358
359 // 保证所有返回路径都会恢复 IRQ 状态。
360 // Ensure IRQ state restoration on all return paths.
361 struct IrqGuard
362 {
363 CH32CAN* self;
364 explicit IrqGuard(CH32CAN* s) : self(s) { self->DisableIRQs(); }
365 ~IrqGuard() { self->EnableIRQs(); }
366 } guard(this);
367
369 bool entered_init = false;
370
371 // 构造实际使用的时序参数;为 0 的字段复用缓存配置。
372 // Build effective timing values; zero fields reuse cached configuration.
373 CAN::Configuration cfg = cfg_in;
374
375 if (!fill_keep_zero_from_cache(cfg.bit_timing, cfg_cache_.bit_timing))
376 {
377 return ErrorCode::ARG_ERR;
378 }
379
380 // 校验 bxCAN 时序约束。
381 // Validate bxCAN timing constraints.
382 const CAN::BitTiming& bt = cfg.bit_timing;
383
384 // BRP: 1..1024.
385 if (bt.brp < 1u || bt.brp > 1024u)
386 {
387 ASSERT(false);
388 return ErrorCode::ARG_ERR;
389 }
390
391 // BS1 = PROP_SEG + PHASE_SEG1: 1..16.
392 const uint32_t BS1 = bt.prop_seg + bt.phase_seg1;
393 if (BS1 < 1u || BS1 > 16u)
394 {
395 ASSERT(false);
396 return ErrorCode::ARG_ERR;
397 }
398
399 // BS2: 1..8.
400 if (bt.phase_seg2 < 1u || bt.phase_seg2 > 8u)
401 {
402 ASSERT(false);
403 return ErrorCode::ARG_ERR;
404 }
405
406 // SJW: 1..4 and SJW <= BS2.
407 if (bt.sjw < 1u || bt.sjw > 4u || bt.sjw > bt.phase_seg2)
408 {
409 ASSERT(false);
410 return ErrorCode::ARG_ERR;
411 }
412
413 // 进入初始化模式。
414 // Enter initialization mode.
415 if (CAN_OperatingModeRequest(instance_, CAN_OperatingMode_Initialization) ==
416 CAN_ModeStatus_Failed)
417 {
418 return ErrorCode::FAILED;
419 }
420 entered_init = true;
421
422 // 填充并应用初始化结构体。
423 // Apply the initialization structure.
424 CAN_InitTypeDef init;
425 std::memset(&init, 0, sizeof(init));
426 CAN_StructInit(&init);
427
428 init.CAN_Prescaler = static_cast<uint16_t>(bt.brp);
429 init.CAN_Mode = ch32_can_mode_macro(cfg.mode);
430
431 init.CAN_SJW = static_cast<uint8_t>(bt.sjw - 1u);
432 init.CAN_BS1 = static_cast<uint8_t>(BS1 - 1u);
433 init.CAN_BS2 = static_cast<uint8_t>(bt.phase_seg2 - 1u);
434
435 // 工作模式映射。
436 // Mode mapping.
437 init.CAN_NART = cfg.mode.one_shot ? ENABLE : DISABLE;
438
439 // 控制器默认选项。
440 // Default controller options.
441 init.CAN_TTCM = DISABLE;
442 init.CAN_ABOM = ENABLE; // auto bus-off management
443 init.CAN_AWUM = DISABLE; // auto wake-up
444 init.CAN_RFLM = DISABLE; // FIFO not locked
445 init.CAN_TXFP = ENABLE; // prioritize by TX request order
446
447 if (CAN_Init(instance_, &init) != CAN_InitStatus_Success)
448 {
450 }
451 else
452 {
453 if (CAN_OperatingModeRequest(instance_, CAN_OperatingMode_Normal) ==
454 CAN_ModeStatus_Failed)
455 {
457 }
458 else
459 {
460 // 仅在配置成功后更新缓存。
461 // Update the cache only on successful configuration.
462 cfg_cache_ = cfg;
463 ec = ErrorCode::OK;
464 }
465 }
466
467 // 如果进入 init 模式后配置失败,则请求回到 normal 模式。
468 // If configuration fails after entering init mode, request normal mode.
469 if (ec != ErrorCode::OK && entered_init)
470 {
471 (void)CAN_OperatingModeRequest(instance_, CAN_OperatingMode_Normal);
472 }
473
474 return ec;
475}
CH32 CAN 驱动实现 / CH32 CAN driver implementation.
Definition ch32_can.hpp:24
CAN::Configuration cfg_cache_
缓存配置(用于保留原值语义) / Cached configuration for keep-previous semantics
Definition ch32_can.hpp:99
ErrorCode
定义错误码枚举
Definition libxr_def.hpp:64
@ FAILED
操作失败 | Operation failed
CAN 位时序配置。Bit timing configuration for CAN.
Definition can.hpp:37
uint32_t brp
预分频。Baud rate prescaler.
Definition can.hpp:38
uint32_t sjw
同步跳宽。Synchronization jump width.
Definition can.hpp:42
uint32_t phase_seg2
相位段 2。Phase segment 2.
Definition can.hpp:41
uint32_t prop_seg
传播段。Propagation segment.
Definition can.hpp:39
uint32_t phase_seg1
相位段 1。Phase segment 1.
Definition can.hpp:40
CAN 配置参数。CAN configuration parameters.
Definition can.hpp:62
BitTiming bit_timing
位时序配置。Bit timing configuration.
Definition can.hpp:65
Mode mode
工作模式。Operating mode.
Definition can.hpp:66
bool one_shot
单次发送模式。One-shot transmission.
Definition can.hpp:54

◆ TxService()

void CH32CAN::TxService ( )
private

Definition at line 500 of file ch32_can.cpp.

501{
502 if (instance_ == nullptr)
503 {
504 return;
505 }
506
507 tx_pend_.store(1u, std::memory_order_release);
508
509 uint32_t expected = 0u;
510 if (!tx_lock_.compare_exchange_strong(expected, 1u, std::memory_order_acquire,
511 std::memory_order_relaxed))
512 {
513 return;
514 }
515
516 for (;;)
517 {
518 tx_pend_.store(0u, std::memory_order_release);
519
520 for (;;)
521 {
522 ClassicPack p{};
523 if (tx_pool_.Get(p) != ErrorCode::OK)
524 {
525 break;
526 }
527
528 BuildTxMsg(p, tx_msg_);
529
530 uint8_t mb = CAN_Transmit(instance_, &tx_msg_);
531 if (mb == CAN_TxStatus_NoMailBox)
532 {
533 (void)tx_pool_.Put(p);
534 break;
535 }
536 }
537
538 tx_lock_.store(0u, std::memory_order_release);
539
540 if (tx_pend_.load(std::memory_order_acquire) == 0u)
541 {
542 return;
543 }
544
545 expected = 0u;
546 if (!tx_lock_.compare_exchange_strong(expected, 1u, std::memory_order_acquire,
547 std::memory_order_relaxed))
548 {
549 return;
550 }
551 }
552}
ErrorCode Get(Data &data)
从池中取出一个元素 / Retrieve an element from the pool

Field Documentation

◆ cfg_cache_

CAN::Configuration LibXR::CH32CAN::cfg_cache_ {}
private

缓存配置(用于保留原值语义) / Cached configuration for keep-previous semantics

Definition at line 99 of file ch32_can.hpp.

99{};

◆ fifo_

uint8_t LibXR::CH32CAN::fifo_ {0}
private

Definition at line 90 of file ch32_can.hpp.

90{0};

◆ filter_bank_

uint8_t LibXR::CH32CAN::filter_bank_ {0}
private

Definition at line 91 of file ch32_can.hpp.

91{0};

◆ id_

ch32_can_id_t LibXR::CH32CAN::id_ {CH32_CAN_ID_ERROR}
private

Definition at line 88 of file ch32_can.hpp.

88{CH32_CAN_ID_ERROR};

◆ instance_

CAN_TypeDef* LibXR::CH32CAN::instance_ {nullptr}
private

Definition at line 87 of file ch32_can.hpp.

87{nullptr};

◆ map

CH32CAN * CH32CAN::map = {nullptr}
static

中断分发表 / IRQ dispatch map

Definition at line 76 of file ch32_can.hpp.

◆ rx_msg_

CanRxMsg LibXR::CH32CAN::rx_msg_ {}
private

中断上下文收发缓冲 / RX/TX buffers in IRQ context

Definition at line 102 of file ch32_can.hpp.

102{};

◆ tx_lock_

std::atomic<uint32_t> LibXR::CH32CAN::tx_lock_ {0}
private

Definition at line 95 of file ch32_can.hpp.

95{0};

◆ tx_msg_

CanTxMsg LibXR::CH32CAN::tx_msg_ {}
private

Definition at line 103 of file ch32_can.hpp.

103{};

◆ tx_pend_

std::atomic<uint32_t> LibXR::CH32CAN::tx_pend_ {0}
private

Definition at line 96 of file ch32_can.hpp.

96{0};

◆ tx_pool_

LockFreePool<ClassicPack> LibXR::CH32CAN::tx_pool_
private

Definition at line 93 of file ch32_can.hpp.


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