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 queue_size)
 STM32CAN 类,用于处理 STM32 系统的 CAN 通道。 Provides handling for STM32 CAN.
 
ErrorCode Init (void)
 初始化
 
ErrorCode AddMessage (const ClassicPack &pack) override
 添加 CAN 消息到系统 (Adds a CAN message to the system).
 
void ProcessRxInterrupt ()
 处理接收中断
 
void ProcessTxInterrupt ()
 处理发送中断
 
- Public Member Functions inherited from LibXR::CAN
 CAN ()
 构造 CAN 对象,可指定主题名称和通信域 (Constructs a CAN object with an optional topic name and domain).
 
struct __attribute__ ((packed))
 经典 CAN 消息结构 (Structure representing a classic CAN message).
 
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
 

Data Fields

CAN_HandleTypeDef * hcan_
 
stm32_can_id_t id_
 
LockFreeQueue< ClassicPack > tx_queue_
 
uint32_t fifo_
 
struct { 
 
   CAN_RxHeaderTypeDef   header 
 
   ClassicPack   pack 
 
rx_buff_ 
 
struct { 
 
   CAN_TxHeaderTypeDef   header 
 
   ClassicPack   pack 
 
tx_buff_ 
 
uint32_t txMailbox
 
Mutex write_mutex_
 
- Data Fields inherited from LibXR::CAN
 ClassicPack
 

Static Public Attributes

static STM32CANmap [STM32_CAN_NUMBER] = {nullptr}
 

Additional Inherited Members

- Public Types inherited from LibXR::CAN
enum class  Type : uint8_t {
  STANDARD = 0 , EXTENDED = 1 , REMOTE_STANDARD = 2 , REMOTE_EXTENDED = 3 ,
  TYPE_NUM
}
 CAN 消息类型 (Enumeration of CAN message types). More...
 
enum class  FilterMode : uint8_t { ID_MASK = 0 , ID_RANGE = 1 }
 
using Callback = LibXR::Callback<const ClassicPack &>
 
- Protected Member Functions inherited from LibXR::CAN
void OnMessage (const ClassicPack &pack, bool in_isr)
 

Detailed Description

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

Definition at line 38 of file stm32_can.hpp.

Constructor & Destructor Documentation

◆ STM32CAN()

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

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

Parameters
hcanSTM32CAN对象 CAN object
queue_size发送队列大小 Send queue size

Definition at line 45 of file stm32_can.cpp.

46 : CAN(), hcan_(hcan), id_(STM32_CAN_GetID(hcan->Instance)), tx_queue_(queue_size)
47{
48 map[id_] = this;
49 Init();
50}
CAN()
构造 CAN 对象,可指定主题名称和通信域 (Constructs a CAN object with an optional topic name and domain).
Definition can.hpp:34
ErrorCode Init(void)
初始化
Definition stm32_can.cpp:52

Member Function Documentation

◆ AddMessage()

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

添加 CAN 消息到系统 (Adds a CAN message to the system).

Parameters
pack经典 CAN 消息包 (The classic CAN message packet).
Returns
操作结果 (ErrorCode indicating success or failure).

Implements LibXR::CAN.

Definition at line 137 of file stm32_can.cpp.

138{
139 CAN_TxHeaderTypeDef txHeader; // NOLINT
140
141 txHeader.DLC = sizeof(pack.data);
142
143 switch (pack.type)
144 {
145 case Type::STANDARD:
146 txHeader.IDE = CAN_ID_STD;
147 txHeader.RTR = CAN_RTR_DATA;
148 break;
149 case Type::EXTENDED:
150 txHeader.IDE = CAN_ID_EXT;
151 txHeader.RTR = CAN_RTR_DATA;
152 break;
154 txHeader.IDE = CAN_ID_STD;
155 txHeader.RTR = CAN_RTR_REMOTE;
156 break;
158 txHeader.IDE = CAN_ID_EXT;
159 txHeader.RTR = CAN_RTR_REMOTE;
160 break;
161 default:
162 ASSERT(false);
163 return ErrorCode::FAILED;
164 }
165 txHeader.StdId = (pack.type == Type::EXTENDED) ? 0 : pack.id;
166 txHeader.ExtId = (pack.type == Type::EXTENDED) ? pack.id : 0;
167 txHeader.TransmitGlobalTime = DISABLE;
168
169 if (HAL_CAN_AddTxMessage(hcan_, &txHeader, pack.data, &txMailbox) != HAL_OK)
170 {
171 Mutex::LockGuard lock(write_mutex_);
172 if (tx_queue_.Push(pack) != ErrorCode::OK)
173 {
174 return ErrorCode::FULL;
175 }
176 }
177
178 return ErrorCode::OK;
179}
@ EXTENDED
扩展 CAN 消息 (Extended CAN message).
@ REMOTE_EXTENDED
远程扩展 CAN 消息 (Remote extended CAN message).
@ STANDARD
标准 CAN 消息 (Standard CAN message).
@ REMOTE_STANDARD
远程标准 CAN 消息 (Remote standard CAN message).
ErrorCode Push(ElementData &&item)
向队列中推入数据 / Pushes data into the queue
互斥锁的 RAII 机制封装 (RAII-style mechanism for automatic mutex management).
Definition mutex.hpp:65

◆ Init()

ErrorCode STM32CAN::Init ( void )

初始化

Returns
ErrorCode

Definition at line 52 of file stm32_can.cpp.

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

◆ ProcessRxInterrupt()

void STM32CAN::ProcessRxInterrupt ( )

处理接收中断

Definition at line 181 of file stm32_can.cpp.

182{
183 while (HAL_CAN_GetRxMessage(hcan_, fifo_, &rx_buff_.header, rx_buff_.pack.data) ==
184 HAL_OK)
185 {
186 if (rx_buff_.header.IDE == CAN_ID_STD)
187 {
188 if (rx_buff_.header.StdId == 2046)
189 {
190 __NOP();
191 }
192 rx_buff_.pack.id = rx_buff_.header.StdId;
193 rx_buff_.pack.type = Type::STANDARD;
194 }
195 else
196 {
197 rx_buff_.pack.id = rx_buff_.header.ExtId;
198 rx_buff_.pack.type = Type::EXTENDED;
199 }
200
201 if (rx_buff_.header.RTR == CAN_RTR_REMOTE)
202 {
203 if (rx_buff_.pack.type == Type::STANDARD)
204 {
205 rx_buff_.pack.type = Type::REMOTE_STANDARD;
206 }
207 else
208 {
209 rx_buff_.pack.type = Type::REMOTE_EXTENDED;
210 }
211 }
212 OnMessage(rx_buff_.pack, true);
213 }
214}

◆ ProcessTxInterrupt()

void STM32CAN::ProcessTxInterrupt ( )

处理发送中断

Definition at line 216 of file stm32_can.cpp.

217{
218 if (tx_queue_.Peek(tx_buff_.pack) == ErrorCode::OK)
219 {
220 tx_buff_.header.DLC = sizeof(tx_buff_.pack.data);
221 switch (tx_buff_.pack.type)
222 {
223 case Type::STANDARD:
224 tx_buff_.header.IDE = CAN_ID_STD;
225 tx_buff_.header.RTR = CAN_RTR_DATA;
226 break;
227 case Type::EXTENDED:
228 tx_buff_.header.IDE = CAN_ID_EXT;
229 tx_buff_.header.RTR = CAN_RTR_DATA;
230 break;
232 tx_buff_.header.IDE = CAN_ID_STD;
233 tx_buff_.header.RTR = CAN_RTR_REMOTE;
234 break;
236 tx_buff_.header.IDE = CAN_ID_EXT;
237 tx_buff_.header.RTR = CAN_RTR_REMOTE;
238 break;
239 default:
240 ASSERT(false);
241 return;
242 }
243 tx_buff_.header.StdId = (tx_buff_.pack.type == Type::EXTENDED) ? 0 : tx_buff_.pack.id;
244 tx_buff_.header.ExtId = (tx_buff_.pack.type == Type::EXTENDED) ? tx_buff_.pack.id : 0;
245 tx_buff_.header.TransmitGlobalTime = DISABLE;
246
247 if (HAL_CAN_AddTxMessage(hcan_, &tx_buff_.header, tx_buff_.pack.data, &txMailbox) ==
248 HAL_OK)
249 {
250 tx_queue_.Pop();
251 }
252 }
253}
ErrorCode Pop(ElementData &item)
从队列中弹出数据 / Pops data from the queue
ErrorCode Peek(Data &item)
获取队列头部数据但不弹出 / Retrieves the front data of the queue without popping

Field Documentation

◆ fifo_

uint32_t LibXR::STM32CAN::fifo_

Definition at line 74 of file stm32_can.hpp.

◆ hcan_

CAN_HandleTypeDef* LibXR::STM32CAN::hcan_

Definition at line 70 of file stm32_can.hpp.

◆ header [1/2]

CAN_RxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 79 of file stm32_can.hpp.

◆ header [2/2]

CAN_TxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 85 of file stm32_can.hpp.

◆ id_

stm32_can_id_t LibXR::STM32CAN::id_

Definition at line 72 of file stm32_can.hpp.

◆ map

STM32CAN * STM32CAN::map = {nullptr}
static

Definition at line 75 of file stm32_can.hpp.

◆ pack

ClassicPack LibXR::STM32CAN::pack

Definition at line 80 of file stm32_can.hpp.

◆ tx_queue_

LockFreeQueue<ClassicPack> LibXR::STM32CAN::tx_queue_

Definition at line 73 of file stm32_can.hpp.

◆ txMailbox

uint32_t LibXR::STM32CAN::txMailbox

Definition at line 89 of file stm32_can.hpp.

◆ write_mutex_

Mutex LibXR::STM32CAN::write_mutex_

Definition at line 90 of file stm32_can.hpp.


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