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

STM32 CAN 驱动实现 / STM32 CAN driver implementation. 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 queue_size)
 构造 CAN 驱动对象 / Construct CAN driver object
 
ErrorCode Init (void)
 初始化驱动 / Initialize driver
 
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 ()
 处理接收中断 / Handle RX interrupt
 
void ProcessErrorInterrupt ()
 处理错误中断 / Handle error interrupt
 
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
 
MPMCQueue< ClassicPacktx_queue_
 发送软件队列。 TX software queue.
 
bool tx_retry_valid_ {false}
 待重试帧有效标记。 Pending retry frame flag.
 
ClassicPack tx_retry_pack_ {}
 待重试帧。 Pending retry frame.
 
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 LIBXR_PACKED_END 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

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

Definition at line 41 of file stm32_can.hpp.

Constructor & Destructor Documentation

◆ STM32CAN()

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

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

Parameters
hcanHAL CAN 句柄 / HAL CAN handle
queue_size发送队列大小 / TX queue size
Precondition
queue_size 必须大于 1。 queue_size must be greater than 1.

Definition at line 47 of file stm32_can.cpp.

48 : CAN(), hcan_(hcan), id_(STM32_CAN_GetID(hcan->Instance)), tx_queue_(queue_size)
49{
50 map[id_] = this;
51 Init();
52}
CAN()=default
构造函数。Constructor.
ErrorCode Init(void)
初始化驱动 / Initialize driver
Definition stm32_can.cpp:54
MPMCQueue< ClassicPack > tx_queue_
发送软件队列。 TX software queue.

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 492 of file stm32_can.cpp.

493{
494 if (pack.type == Type::ERROR)
495 {
496 return ErrorCode::ARG_ERR;
497 }
498
499 if (tx_queue_.Push(pack) != ErrorCode::OK)
500 {
501 return ErrorCode::FULL;
502 }
503
504 // kick
505 TxService();
506 return ErrorCode::OK;
507}
@ ERROR
错误帧(虚拟事件)。Error frame (virtual event).
ErrorCode Push(const Data &item)
推入一个强类型元素。
@ FULL
已满 | Full
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
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 607 of file stm32_can.cpp.

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

初始化驱动 / Initialize driver

Returns
ErrorCode 错误码 / Error code

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}
@ FAILED
操作失败 | Operation failed

◆ ProcessErrorInterrupt()

void STM32CAN::ProcessErrorInterrupt ( )

处理错误中断 / Handle error interrupt

Definition at line 544 of file stm32_can.cpp.

545{
546 if (hcan_ == nullptr || hcan_->Instance == nullptr)
547 {
548 return;
549 }
550
551 uint32_t esr = hcan_->Instance->ESR;
552
553 CAN::ClassicPack pack{};
554 pack.type = CAN::Type::ERROR;
555 pack.dlc = 0;
556
557 CAN::ErrorID eid = CAN::ErrorID::CAN_ERROR_ID_GENERIC;
558
559 if (esr & CAN_ESR_BOFF)
560 {
561 eid = CAN::ErrorID::CAN_ERROR_ID_BUS_OFF;
562 }
563 else if (esr & CAN_ESR_EPVF)
564 {
565 eid = CAN::ErrorID::CAN_ERROR_ID_ERROR_PASSIVE;
566 }
567 else if (esr & CAN_ESR_EWGF)
568 {
569 eid = CAN::ErrorID::CAN_ERROR_ID_ERROR_WARNING;
570 }
571 else
572 {
573 uint32_t lec = (esr >> 4) & 0x7u;
574
575 switch (lec)
576 {
577 case 0x01:
578 eid = CAN::ErrorID::CAN_ERROR_ID_STUFF;
579 break;
580 case 0x02:
581 eid = CAN::ErrorID::CAN_ERROR_ID_FORM;
582 break;
583 case 0x03:
584 eid = CAN::ErrorID::CAN_ERROR_ID_ACK;
585 break;
586 case 0x04:
587 eid = CAN::ErrorID::CAN_ERROR_ID_BIT1;
588 break;
589 case 0x05:
590 eid = CAN::ErrorID::CAN_ERROR_ID_BIT0;
591 break;
592 case 0x06:
593 eid = CAN::ErrorID::CAN_ERROR_ID_CRC;
594 break;
595 default:
596 eid = CAN::ErrorID::CAN_ERROR_ID_OTHER;
597 break;
598 }
599 }
600
601 pack.id = static_cast<uint32_t>(eid);
602
603 // 在中断上下文中分发错误帧
604 OnMessage(pack, true);
605}
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 ( )

处理接收中断 / Handle RX interrupt

Definition at line 509 of file stm32_can.cpp.

510{
511 while (HAL_CAN_GetRxMessage(hcan_, fifo_, &rx_buff_.header, rx_buff_.pack.data) ==
512 HAL_OK)
513 {
514 if (rx_buff_.header.IDE == CAN_ID_STD)
515 {
516 rx_buff_.pack.id = rx_buff_.header.StdId;
517 rx_buff_.pack.type = Type::STANDARD;
518 }
519 else
520 {
521 rx_buff_.pack.id = rx_buff_.header.ExtId;
522 rx_buff_.pack.type = Type::EXTENDED;
523 }
524
525 if (rx_buff_.header.RTR == CAN_RTR_REMOTE)
526 {
527 if (rx_buff_.pack.type == Type::STANDARD)
528 {
529 rx_buff_.pack.type = Type::REMOTE_STANDARD;
530 }
531 else
532 {
533 rx_buff_.pack.type = Type::REMOTE_EXTENDED;
534 }
535 }
536
537 uint8_t dlc = (rx_buff_.header.DLC <= 8u) ? rx_buff_.header.DLC : 8u;
538 rx_buff_.pack.dlc = dlc;
539
540 OnMessage(rx_buff_.pack, true);
541 }
542}
@ 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 // Derive field maxima from masks instead of hard-coded constants.
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_retry_valid_)
449 {
450 p = tx_retry_pack_;
451 }
452 else if (tx_queue_.Pop(p) != ErrorCode::OK)
453 {
454 break; // 队列空
455 }
456
457 CAN_TxHeaderTypeDef hdr{};
458 BuildTxHeader(p, hdr);
459
460 uint32_t mailbox = 0u;
461 if (HAL_CAN_AddTxMessage(hcan_, &hdr, p.data, &mailbox) != HAL_OK)
462 {
463 tx_retry_pack_ = p;
464 tx_retry_valid_ = true;
465 break;
466 }
467
468 tx_retry_valid_ = false;
469 txMailbox = mailbox;
470 }
471
472 // 先解锁,再检查是否仍有待处理发送请求。
473 // Release lock first, then check whether pending TX work remains.
474 tx_lock_.store(0u, std::memory_order_release);
475
476 // 再看是否还有 pend:若没有,直接退出
477 if (tx_pend_.load(std::memory_order_acquire) == 0u)
478 {
479 return;
480 }
481
482 // 允许出现 PEND=1, LOCK=0 的“无人服务”状态:尝试重新加锁再服务
483 expected = 0u;
484 if (!tx_lock_.compare_exchange_strong(expected, 1u, std::memory_order_acquire,
485 std::memory_order_relaxed))
486 {
487 return;
488 }
489 }
490}
ErrorCode Pop(Data &item)
弹出一个强类型元素。
ClassicPack tx_retry_pack_
待重试帧。 Pending retry frame.
bool tx_retry_valid_
待重试帧有效标记。 Pending retry frame flag.

Field Documentation

◆ fifo_

uint32_t LibXR::STM32CAN::fifo_

Definition at line 89 of file stm32_can.hpp.

◆ hcan_

CAN_HandleTypeDef* LibXR::STM32CAN::hcan_

Definition at line 86 of file stm32_can.hpp.

◆ header [1/2]

CAN_RxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 94 of file stm32_can.hpp.

◆ header [2/2]

CAN_TxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 100 of file stm32_can.hpp.

◆ id_

stm32_can_id_t LibXR::STM32CAN::id_

Definition at line 88 of file stm32_can.hpp.

◆ map

STM32CAN * STM32CAN::map = {nullptr}
static

Definition at line 90 of file stm32_can.hpp.

◆ pack

ClassicPack LibXR::STM32CAN::pack

Definition at line 95 of file stm32_can.hpp.

◆ tx_lock_

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

Definition at line 113 of file stm32_can.hpp.

113{0};

◆ tx_pend_

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

Definition at line 114 of file stm32_can.hpp.

114{0};

◆ tx_queue_

MPMCQueue<ClassicPack> LibXR::STM32CAN::tx_queue_

发送软件队列。 TX software queue.

Definition at line 107 of file stm32_can.hpp.

◆ tx_retry_pack_

ClassicPack LibXR::STM32CAN::tx_retry_pack_ {}

待重试帧。 Pending retry frame.

Definition at line 111 of file stm32_can.hpp.

111{};

◆ tx_retry_valid_

bool LibXR::STM32CAN::tx_retry_valid_ {false}

待重试帧有效标记。 Pending retry frame flag.

Definition at line 109 of file stm32_can.hpp.

109{false};

◆ txMailbox

uint32_t LibXR::STM32CAN::txMailbox

Definition at line 104 of file stm32_can.hpp.


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