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

STM32CAN 类,用于处理 STM32 系统的 CAN 通道。 Provides handling for STM32 CAN channels. More...

#include <stm32_can.hpp>

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

Public Member Functions

 STM32CAN (CAN_HandleTypeDef *hcan, uint32_t pool_size)
 STM32CAN 类,用于处理 STM32 系统的 CAN 通道。 Provides handling for STM32 CAN.
 
ErrorCode Init (void)
 初始化
 
ErrorCode SetConfig (const CAN::Configuration &cfg) override
 设置 CAN 配置。Set CAN configuration.
 
uint32_t GetClockFreq () const override
 获取 CAN 外设时钟频率(Hz)。 Get CAN peripheral clock frequency in Hz.
 
ErrorCode AddMessage (const ClassicPack &pack) override
 添加经典 CAN 消息。Add classic CAN message.
 
ErrorCode GetErrorState (CAN::ErrorState &state) const override
 查询当前错误状态(快照)。 Query current CAN controller error state (snapshot).
 
void ProcessRxInterrupt ()
 处理接收中断
 
void ProcessErrorInterrupt ()
 处理错误中断
 
void TxService ()
 
- 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 Member Functions

static void BuildTxHeader (const ClassicPack &p, CAN_TxHeaderTypeDef &h)
 
- 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.
 

Data Fields

CAN_HandleTypeDef * hcan_
 
stm32_can_id_t id_
 
uint32_t fifo_
 
struct { 
 
   CAN_RxHeaderTypeDef   header 
 
   ClassicPack   pack 
 
rx_buff_ 
 
struct { 
 
   CAN_TxHeaderTypeDef   header 
 
   ClassicPack   pack 
 
tx_buff_ 
 
uint32_t txMailbox
 
LockFreePool< ClassicPacktx_pool_
 
std::atomic< uint32_t > tx_lock_ {0}
 
std::atomic< uint32_t > tx_pend_ {0}
 

Static Public Attributes

static STM32CANmap [STM32_CAN_NUMBER] = {nullptr}
 
- Static Public Attributes inherited from LibXR::CAN
static constexpr uint32_t CAN_ERROR_ID_PREFIX = 0xFFFF0000u
 错误 ID 前缀 Error ID prefix.
 

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.
 
- Protected Member Functions inherited from LibXR::CAN
void OnMessage (const ClassicPack &pack, bool in_isr)
 分发接收到的经典 CAN 帧。 Dispatch a received classic CAN frame.
 

Detailed Description

STM32CAN 类,用于处理 STM32 系统的 CAN 通道。 Provides handling for STM32 CAN channels.

Definition at line 43 of file stm32_can.hpp.

Constructor & Destructor Documentation

◆ STM32CAN()

STM32CAN::STM32CAN ( CAN_HandleTypeDef * hcan,
uint32_t pool_size )

STM32CAN 类,用于处理 STM32 系统的 CAN 通道。 Provides handling for STM32 CAN.

Parameters
hcanSTM32CAN对象 CAN object
pool_size发送池大小 Send pool size

Definition at line 47 of file stm32_can.cpp.

48 : CAN(), hcan_(hcan), id_(STM32_CAN_GetID(hcan->Instance)), tx_pool_(pool_size)
49{
50 map[id_] = this;
51 Init();
52}
CAN()=default
构造函数。Constructor.
ErrorCode Init(void)
初始化
Definition stm32_can.cpp:54

Member Function Documentation

◆ AddMessage()

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

添加经典 CAN 消息。Add classic CAN message.

Parameters
pack经典 CAN 帧。Classic CAN frame.
Returns
ErrorCode 操作结果。Error code.

Implements LibXR::CAN.

Definition at line 486 of file stm32_can.cpp.

487{
488 if (pack.type == Type::ERROR)
489 {
490 return ErrorCode::ARG_ERR;
491 }
492
493 // 池满直接返回 FULL,不做补救
494 if (tx_pool_.Put(pack) != ErrorCode::OK)
495 {
496 return ErrorCode::FULL;
497 }
498
499 // kick
500 TxService();
501 return ErrorCode::OK;
502}
@ ERROR
错误帧(虚拟事件)。Error frame (virtual event).
ErrorCode Put(const Data &data)
向池中放入一个元素 / Put an element into the pool
Type type
帧类型。Frame type.
Definition can.hpp:131

◆ BuildTxHeader()

void STM32CAN::BuildTxHeader ( const ClassicPack & p,
CAN_TxHeaderTypeDef & h )
inlinestatic

Definition at line 402 of file stm32_can.cpp.

403{
404 const bool IS_EXT = (p.type == Type::EXTENDED) || (p.type == Type::REMOTE_EXTENDED);
405 const bool IS_RTR =
406 (p.type == Type::REMOTE_STANDARD) || (p.type == Type::REMOTE_EXTENDED);
407
408 h.DLC = (p.dlc <= 8u) ? p.dlc : 8u;
409 h.IDE = IS_EXT ? CAN_ID_EXT : CAN_ID_STD;
410 h.RTR = IS_RTR ? CAN_RTR_REMOTE : CAN_RTR_DATA;
411
412 h.StdId = IS_EXT ? 0u : (p.id & 0x7FFu);
413 h.ExtId = IS_EXT ? (p.id & 0x1FFFFFFFu) : 0u;
414
415 h.TransmitGlobalTime = DISABLE;
416}
@ EXTENDED
扩展数据帧(29-bit ID)。Extended data frame (29-bit ID).
@ REMOTE_EXTENDED
扩展远程帧。Extended remote frame.
@ REMOTE_STANDARD
标准远程帧。Standard remote frame.

◆ GetClockFreq()

uint32_t STM32CAN::GetClockFreq ( ) const
overridevirtual

获取 CAN 外设时钟频率(Hz)。 Get CAN peripheral clock frequency in Hz.

Implements LibXR::CAN.

Definition at line 396 of file stm32_can.cpp.

397{
398 // 经典 bxCAN 始终挂在 APB1 上
399 return HAL_RCC_GetPCLK1Freq();
400}

◆ GetErrorState()

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

查询当前错误状态(快照)。 Query current CAN controller error state (snapshot).

默认实现返回 ErrorCode::NOT_SUPPORT;具体实现(如 bxCAN/FDCAN)可重载, 从硬件寄存器读取 TEC/REC 及状态位,并填充 ErrorState。

Parameters
state输出参数,用于返回当前错误状态。
Returns
ErrorCode 操作结果;若未实现则返回 ErrorCode::NOT_SUPPORT。

Reimplemented from LibXR::CAN.

Definition at line 602 of file stm32_can.cpp.

603{
604 if (hcan_ == nullptr || hcan_->Instance == nullptr)
605 {
606 return ErrorCode::ARG_ERR;
607 }
608
609 // 直接读取 bxCAN 的错误状态寄存器 ESR
610 uint32_t esr = hcan_->Instance->ESR;
611
612 // TEC: bits 23:16, REC: bits 31:24(参考 RM & CAN_ESR 描述)
613 state.tx_error_counter = static_cast<uint8_t>((esr >> 16) & 0xFFu);
614 state.rx_error_counter = static_cast<uint8_t>((esr >> 24) & 0xFFu);
615
616 // 状态位:BOFF / EPVF / EWGF
617 state.bus_off = (esr & CAN_ESR_BOFF) != 0u;
618 state.error_passive = (esr & CAN_ESR_EPVF) != 0u;
619 state.error_warning = (esr & CAN_ESR_EWGF) != 0u;
620
621 return ErrorCode::OK;
622}
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 STM32CAN::Init ( void )

初始化

Returns
ErrorCode

Definition at line 54 of file stm32_can.cpp.

55{
56 CAN_FilterTypeDef can_filter = {};
57
58 can_filter.FilterIdHigh = 0;
59 can_filter.FilterIdLow = 0;
60 can_filter.FilterMode = CAN_FILTERMODE_IDMASK;
61 can_filter.FilterScale = CAN_FILTERSCALE_32BIT;
62 can_filter.FilterMaskIdHigh = 0;
63 can_filter.FilterMaskIdLow = 0;
64 can_filter.FilterActivation = ENABLE;
65
66#ifdef CAN3
67 if (id_ == STM32_CAN1)
68 {
69 can_filter.FilterBank = 0;
70 can_filter.SlaveStartFilterBank = 14;
71 fifo_ = CAN_RX_FIFO0;
72 }
73 else if (id_ == STM32_CAN2)
74 {
75 can_filter.FilterBank = 14;
76 can_filter.SlaveStartFilterBank = 14;
77 fifo_ = CAN_RX_FIFO0;
78 }
79 else if (id_ == STM32_CAN3)
80 {
81 can_filter.FilterBank = 0;
82 fifo_ = CAN_RX_FIFO1;
83 }
84#else
85#ifdef CAN2
86 if (id_ == STM32_CAN1)
87 {
88 can_filter.FilterBank = 0;
89 can_filter.SlaveStartFilterBank = 14;
90 fifo_ = CAN_RX_FIFO0;
91 }
92 else if (id_ == STM32_CAN2)
93 {
94 can_filter.FilterBank = 14;
95 can_filter.SlaveStartFilterBank = 14;
96 fifo_ = CAN_RX_FIFO1;
97 }
98#else
99 if (id_ == STM32_CAN1)
100 {
101 can_filter.FilterBank = 0;
102 can_filter.SlaveStartFilterBank = 14;
103 fifo_ = CAN_RX_FIFO0;
104 }
105#endif
106#endif
107 else
108 {
109 ASSERT(false);
110 return ErrorCode::FAILED;
111 }
112
113 can_filter.FilterFIFOAssignment = fifo_;
114
115 if (HAL_CAN_ConfigFilter(hcan_, &can_filter) != HAL_OK)
116 {
117 return ErrorCode::FAILED;
118 }
119
120 if (HAL_CAN_Start(hcan_) != HAL_OK)
121 {
122 return ErrorCode::FAILED;
123 }
124
125 if (fifo_ == CAN_RX_FIFO0)
126 {
127 HAL_CAN_ActivateNotification(hcan_, CAN_IT_RX_FIFO0_MSG_PENDING);
128 }
129 else
130 {
131 HAL_CAN_ActivateNotification(hcan_, CAN_IT_RX_FIFO1_MSG_PENDING);
132 }
133
134 HAL_CAN_ActivateNotification(hcan_, CAN_IT_ERROR);
135 HAL_CAN_ActivateNotification(hcan_, CAN_IT_BUSOFF);
136 HAL_CAN_ActivateNotification(hcan_, CAN_IT_ERROR_PASSIVE);
137 HAL_CAN_ActivateNotification(hcan_, CAN_IT_ERROR_WARNING);
138 HAL_CAN_ActivateNotification(hcan_, CAN_IT_TX_MAILBOX_EMPTY);
139
140 return ErrorCode::OK;
141}

◆ ProcessErrorInterrupt()

void STM32CAN::ProcessErrorInterrupt ( )

处理错误中断

Definition at line 539 of file stm32_can.cpp.

540{
541 if (hcan_ == nullptr || hcan_->Instance == nullptr)
542 {
543 return;
544 }
545
546 uint32_t esr = hcan_->Instance->ESR;
547
548 CAN::ClassicPack pack{};
549 pack.type = CAN::Type::ERROR;
550 pack.dlc = 0;
551
552 CAN::ErrorID eid = CAN::ErrorID::CAN_ERROR_ID_GENERIC;
553
554 if (esr & CAN_ESR_BOFF)
555 {
556 eid = CAN::ErrorID::CAN_ERROR_ID_BUS_OFF;
557 }
558 else if (esr & CAN_ESR_EPVF)
559 {
560 eid = CAN::ErrorID::CAN_ERROR_ID_ERROR_PASSIVE;
561 }
562 else if (esr & CAN_ESR_EWGF)
563 {
564 eid = CAN::ErrorID::CAN_ERROR_ID_ERROR_WARNING;
565 }
566 else
567 {
568 uint32_t lec = (esr >> 4) & 0x7u;
569
570 switch (lec)
571 {
572 case 0x01:
573 eid = CAN::ErrorID::CAN_ERROR_ID_STUFF;
574 break;
575 case 0x02:
576 eid = CAN::ErrorID::CAN_ERROR_ID_FORM;
577 break;
578 case 0x03:
579 eid = CAN::ErrorID::CAN_ERROR_ID_ACK;
580 break;
581 case 0x04:
582 eid = CAN::ErrorID::CAN_ERROR_ID_BIT1;
583 break;
584 case 0x05:
585 eid = CAN::ErrorID::CAN_ERROR_ID_BIT0;
586 break;
587 case 0x06:
588 eid = CAN::ErrorID::CAN_ERROR_ID_CRC;
589 break;
590 default:
591 eid = CAN::ErrorID::CAN_ERROR_ID_OTHER;
592 break;
593 }
594 }
595
596 pack.id = static_cast<uint32_t>(eid);
597
598 // 在中断上下文中分发错误帧
599 OnMessage(pack, true);
600}
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
经典 CAN 帧数据结构。Classic CAN frame structure.
Definition can.hpp:129
uint32_t id
CAN ID(11/29 bit 或 ErrorID)。CAN ID (11/29 bits or ErrorID).
Definition can.hpp:130
uint8_t dlc
有效数据长度(0~8)。Data length code (0–8).
Definition can.hpp:132

◆ ProcessRxInterrupt()

void STM32CAN::ProcessRxInterrupt ( )

处理接收中断

Definition at line 504 of file stm32_can.cpp.

505{
506 while (HAL_CAN_GetRxMessage(hcan_, fifo_, &rx_buff_.header, rx_buff_.pack.data) ==
507 HAL_OK)
508 {
509 if (rx_buff_.header.IDE == CAN_ID_STD)
510 {
511 rx_buff_.pack.id = rx_buff_.header.StdId;
512 rx_buff_.pack.type = Type::STANDARD;
513 }
514 else
515 {
516 rx_buff_.pack.id = rx_buff_.header.ExtId;
517 rx_buff_.pack.type = Type::EXTENDED;
518 }
519
520 if (rx_buff_.header.RTR == CAN_RTR_REMOTE)
521 {
522 if (rx_buff_.pack.type == Type::STANDARD)
523 {
524 rx_buff_.pack.type = Type::REMOTE_STANDARD;
525 }
526 else
527 {
528 rx_buff_.pack.type = Type::REMOTE_EXTENDED;
529 }
530 }
531
532 uint8_t dlc = (rx_buff_.header.DLC <= 8u) ? rx_buff_.header.DLC : 8u;
533 rx_buff_.pack.dlc = dlc;
534
535 OnMessage(rx_buff_.pack, true);
536 }
537}
@ STANDARD
标准数据帧(11-bit ID)。Standard data frame (11-bit ID).

◆ SetConfig()

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

设置 CAN 配置。Set CAN configuration.

Parameters
cfgCAN 配置参数。CAN configuration.
Returns
ErrorCode 操作结果。Operation result.

Implements LibXR::CAN.

Definition at line 143 of file stm32_can.cpp.

144{
145 if (hcan_ == nullptr || hcan_->Instance == nullptr)
146 {
147 ASSERT(false);
148 return ErrorCode::ARG_ERR;
149 }
150
151 CAN_TypeDef* can = hcan_->Instance;
152
153 // 先关掉与 Init 对应的中断
154 uint32_t it_mask = 0u;
155
156#ifdef CAN_IT_RX_FIFO0_MSG_PENDING
157 if (fifo_ == CAN_RX_FIFO0)
158 {
159 it_mask |= CAN_IT_RX_FIFO0_MSG_PENDING;
160 }
161#endif
162
163#ifdef CAN_IT_RX_FIFO1_MSG_PENDING
164 if (fifo_ == CAN_RX_FIFO1)
165 {
166 it_mask |= CAN_IT_RX_FIFO1_MSG_PENDING;
167 }
168#endif
169
170#ifdef CAN_IT_ERROR
171 it_mask |= CAN_IT_ERROR;
172#endif
173
174#ifdef CAN_IT_TX_MAILBOX_EMPTY
175 it_mask |= CAN_IT_TX_MAILBOX_EMPTY;
176#endif
177
178 if (it_mask != 0u)
179 {
180 HAL_CAN_DeactivateNotification(hcan_, it_mask);
181 }
182
183 // 停止 CAN,进入配置状态
184 if (HAL_CAN_Stop(hcan_) != HAL_OK)
185 {
186 return ErrorCode::FAILED;
187 }
188
189 // 一次发送模式(不自动重发)→ NART
190#ifdef CAN_MCR_NART
191 if (cfg.mode.one_shot)
192 {
193 SET_BIT(can->MCR, CAN_MCR_NART);
194 }
195 else
196 {
197 CLEAR_BIT(can->MCR, CAN_MCR_NART);
198 }
199#endif
200
201 const auto& bt = cfg.bit_timing;
202
203 // ====== 范围校验(0 表示“保持原值”,跳过检查) ======
204 // 最大值从掩码推出来,避免硬编码
205 constexpr uint32_t BRP_FIELD_MAX =
206 (CAN_BTR_BRP_Msk >> CAN_BTR_BRP_Pos); // 存的是 brp-1
207 constexpr uint32_t TS1_FIELD_MAX =
208 (CAN_BTR_TS1_Msk >> CAN_BTR_TS1_Pos); // 存的是 ts1-1
209 constexpr uint32_t TS2_FIELD_MAX =
210 (CAN_BTR_TS2_Msk >> CAN_BTR_TS2_Pos); // 存的是 ts2-1
211 constexpr uint32_t SJW_FIELD_MAX =
212 (CAN_BTR_SJW_Msk >> CAN_BTR_SJW_Pos); // 存的是 sjw-1
213
214 // 实际可配置的量(寄存器值 + 1)
215 constexpr uint32_t BRP_MAX = BRP_FIELD_MAX + 1u; // 1..1024
216 constexpr uint32_t TS1_MAX = TS1_FIELD_MAX + 1u; // 1..16
217 constexpr uint32_t TS2_MAX = TS2_FIELD_MAX + 1u; // 1..8
218 constexpr uint32_t SJW_MAX = SJW_FIELD_MAX + 1u; // 1..4
219
220 // brp 范围检查
221 if (bt.brp != 0u)
222 {
223 if (bt.brp < 1u || bt.brp > BRP_MAX)
224 {
225 ASSERT(false);
226 return ErrorCode::ARG_ERR;
227 }
228 }
229
230 // tseg1 = prop_seg + phase_seg1
231 uint32_t tseg1 = bt.prop_seg + bt.phase_seg1;
232 if (bt.prop_seg != 0u || bt.phase_seg1 != 0u)
233 {
234 if (tseg1 < 1u || tseg1 > TS1_MAX)
235 {
236 ASSERT(false);
237 return ErrorCode::ARG_ERR;
238 }
239 }
240
241 if (bt.phase_seg2 != 0u)
242 {
243 if (bt.phase_seg2 < 1u || bt.phase_seg2 > TS2_MAX)
244 {
245 ASSERT(false);
246 return ErrorCode::ARG_ERR;
247 }
248 }
249
250 if (bt.sjw != 0u)
251 {
252 if (bt.sjw < 1u || bt.sjw > SJW_MAX)
253 {
254 ASSERT(false);
255 return ErrorCode::ARG_ERR;
256 }
257
258 // 规范上 SJW ≤ TSEG2(只在二者都要更新时检查)
259 if (bt.phase_seg2 != 0u && bt.sjw > bt.phase_seg2)
260 {
261 ASSERT(false);
262 return ErrorCode::ARG_ERR;
263 }
264 }
265
266 uint32_t btr_old = can->BTR;
267 uint32_t btr_new = btr_old;
268 uint32_t btr_mask = 0u;
269
270 // BRP:bt.brp == 0 → 保持原值
271 if (bt.brp != 0u)
272 {
273 uint32_t brp = (bt.brp - 1u) & BRP_FIELD_MAX;
274 uint32_t mask = CAN_BTR_BRP_Msk;
275 btr_mask |= mask;
276 btr_new &= ~mask;
277 btr_new |= (brp << CAN_BTR_BRP_Pos);
278 }
279
280 // TSEG1 = PROP_SEG + PHASE_SEG1,两者都为 0 → 保持原值
281 if (bt.prop_seg != 0u || bt.phase_seg1 != 0u)
282 {
283 uint32_t ts1 = (tseg1 - 1u) & TS1_FIELD_MAX;
284 uint32_t mask = CAN_BTR_TS1_Msk;
285 btr_mask |= mask;
286 btr_new &= ~mask;
287 btr_new |= (ts1 << CAN_BTR_TS1_Pos);
288 }
289
290 // TSEG2 = PHASE_SEG2,phase_seg2 == 0 → 保持原值
291 if (bt.phase_seg2 != 0u)
292 {
293 uint32_t ts2 = (bt.phase_seg2 - 1u) & TS2_FIELD_MAX;
294 uint32_t mask = CAN_BTR_TS2_Msk;
295 btr_mask |= mask;
296 btr_new &= ~mask;
297 btr_new |= (ts2 << CAN_BTR_TS2_Pos);
298 }
299
300 // SJW,sjw == 0 → 保持原值
301 if (bt.sjw != 0u)
302 {
303 uint32_t sjw = (bt.sjw - 1u) & SJW_FIELD_MAX;
304 uint32_t mask = CAN_BTR_SJW_Msk;
305 btr_mask |= mask;
306 btr_new &= ~mask;
307 btr_new |= (sjw << CAN_BTR_SJW_Pos);
308 }
309
310 // 三采样:只有 HAL 定义了 CAN_BTR_SAM 才动;否则忽略 triple_sampling
311#ifdef CAN_BTR_SAM
312 {
313 uint32_t mask = CAN_BTR_SAM;
314 btr_mask |= mask;
315 btr_new &= ~mask;
316 if (cfg.mode.triple_sampling)
317 {
318 btr_new |= mask;
319 }
320 }
321#else
322 (void)cfg.mode.triple_sampling;
323#endif
324
325 // Loopback:bool 两态,强制覆盖
326#ifdef CAN_BTR_LBKM
327 {
328 uint32_t mask = CAN_BTR_LBKM;
329 btr_mask |= mask;
330 btr_new &= ~mask;
331 if (cfg.mode.loopback)
332 {
333 btr_new |= mask;
334 }
335 }
336#endif
337
338 // Listen-only:bool 两态,强制覆盖
339#ifdef CAN_BTR_SILM
340 {
341 uint32_t mask = CAN_BTR_SILM;
342 btr_mask |= mask;
343 btr_new &= ~mask;
344 if (cfg.mode.listen_only)
345 {
346 btr_new |= mask;
347 }
348 }
349#endif
350
351 // 只改被 btr_mask 覆盖到的位,其余保持原值
352 if (btr_mask != 0u)
353 {
354 btr_old &= ~btr_mask;
355 btr_old |= (btr_new & btr_mask);
356 can->BTR = btr_old;
357 }
358
359 // 重新启动 CAN
360 if (HAL_CAN_Start(hcan_) != HAL_OK)
361 {
362 return ErrorCode::FAILED;
363 }
364
365 // 按 Init 的方式恢复中断
366 uint32_t it_rx = 0u;
367
368#ifdef CAN_IT_RX_FIFO0_MSG_PENDING
369 if (fifo_ == CAN_RX_FIFO0)
370 {
371 it_rx = CAN_IT_RX_FIFO0_MSG_PENDING;
372 }
373#endif
374
375#ifdef CAN_IT_RX_FIFO1_MSG_PENDING
376 if (fifo_ == CAN_RX_FIFO1)
377 {
378 it_rx = CAN_IT_RX_FIFO1_MSG_PENDING;
379 }
380#endif
381
382 if (it_rx != 0u)
383 {
384 HAL_CAN_ActivateNotification(hcan_, it_rx);
385 }
386
387 HAL_CAN_ActivateNotification(hcan_, CAN_IT_ERROR);
388 HAL_CAN_ActivateNotification(hcan_, CAN_IT_BUSOFF);
389 HAL_CAN_ActivateNotification(hcan_, CAN_IT_ERROR_PASSIVE);
390 HAL_CAN_ActivateNotification(hcan_, CAN_IT_ERROR_WARNING);
391 HAL_CAN_ActivateNotification(hcan_, CAN_IT_TX_MAILBOX_EMPTY);
392
393 return ErrorCode::OK;
394}
uint32_t prop_seg
传播段。Propagation segment.
Definition can.hpp:39
BitTiming bit_timing
位时序配置。Bit timing configuration.
Definition can.hpp:65
Mode mode
工作模式。Operating mode.
Definition can.hpp:66
bool triple_sampling
三采样。Triple sampling.
Definition can.hpp:53
bool loopback
回环模式。Loopback mode.
Definition can.hpp:51
bool one_shot
单次发送模式。One-shot transmission.
Definition can.hpp:54
bool listen_only
只听(静默)模式。Listen-only (silent) mode.
Definition can.hpp:52

◆ TxService()

void STM32CAN::TxService ( )

Definition at line 418 of file stm32_can.cpp.

419{
420 if (hcan_ == nullptr || hcan_->Instance == nullptr)
421 {
422 return;
423 }
424
425 // 标记:需要一次 TX 服务(无论本次是否抢到锁)
426 tx_pend_.store(1u, std::memory_order_release);
427
428 // 尝试抢占服务锁:0 -> 1
429 uint32_t expected = 0u;
430 if (!tx_lock_.compare_exchange_strong(expected, 1u, std::memory_order_acquire,
431 std::memory_order_relaxed))
432 {
433 // 有别的上下文在服务;PEND 已置位,它会在结束时看到并再服务
434 return;
435 }
436
437 constexpr uint32_t TME_MASK = (CAN_TSR_TME0 | CAN_TSR_TME1 | CAN_TSR_TME2);
438
439 for (;;)
440 {
441 // 本轮服务开始:消费掉 pend(期间如有新 kick,会再次置 1)
442 tx_pend_.store(0u, std::memory_order_release);
443
444 // 尽可能把空 mailbox 填满:一直到队列空或者 AddTxMessage 失败(邮箱满/忙)
445 while ((hcan_->Instance->TSR & TME_MASK) != 0u)
446 {
447 ClassicPack p{};
448 if (tx_pool_.Get(p) != ErrorCode::OK)
449 {
450 break; // 队列空
451 }
452
453 CAN_TxHeaderTypeDef hdr{};
454 BuildTxHeader(p, hdr);
455
456 uint32_t mailbox = 0u;
457 if (HAL_CAN_AddTxMessage(hcan_, &hdr, p.data, &mailbox) != HAL_OK)
458 {
459 // 发送失败:回队列,不做任何兜底/处理
460 (void)tx_pool_.Put(p);
461 break;
462 }
463
464 txMailbox = mailbox;
465 }
466
467 // 按你的要求:先解锁
468 tx_lock_.store(0u, std::memory_order_release);
469
470 // 再看是否还有 pend:若没有,直接退出
471 if (tx_pend_.load(std::memory_order_acquire) == 0u)
472 {
473 return;
474 }
475
476 // 允许出现 PEND=1, LOCK=0 的“无人服务”状态:尝试重新加锁再服务
477 expected = 0u;
478 if (!tx_lock_.compare_exchange_strong(expected, 1u, std::memory_order_acquire,
479 std::memory_order_relaxed))
480 {
481 return;
482 }
483 }
484}
ErrorCode Get(Data &data)
从池中取出一个元素 / Retrieve an element from the pool

Field Documentation

◆ fifo_

uint32_t LibXR::STM32CAN::fifo_

Definition at line 90 of file stm32_can.hpp.

◆ hcan_

CAN_HandleTypeDef* LibXR::STM32CAN::hcan_

Definition at line 87 of file stm32_can.hpp.

◆ header [1/2]

CAN_RxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 95 of file stm32_can.hpp.

◆ header [2/2]

CAN_TxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 101 of file stm32_can.hpp.

◆ id_

stm32_can_id_t LibXR::STM32CAN::id_

Definition at line 89 of file stm32_can.hpp.

◆ map

STM32CAN * STM32CAN::map = {nullptr}
static

Definition at line 91 of file stm32_can.hpp.

◆ pack

ClassicPack LibXR::STM32CAN::pack

Definition at line 96 of file stm32_can.hpp.

◆ tx_lock_

std::atomic<uint32_t> LibXR::STM32CAN::tx_lock_ {0}

Definition at line 109 of file stm32_can.hpp.

109{0};

◆ tx_pend_

std::atomic<uint32_t> LibXR::STM32CAN::tx_pend_ {0}

Definition at line 110 of file stm32_can.hpp.

110{0};

◆ tx_pool_

LockFreePool<ClassicPack> LibXR::STM32CAN::tx_pool_

Definition at line 107 of file stm32_can.hpp.

◆ txMailbox

uint32_t LibXR::STM32CAN::txMailbox

Definition at line 105 of file stm32_can.hpp.


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