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:
Collaboration diagram for LibXR::STM32CAN:

Public Member Functions

 STM32CAN (CAN_HandleTypeDef *hcan, const char *tp_name, 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 (const char *name_tp="can", Topic::Domain *domain=nullptr)
 构造 CAN 对象,可指定主题名称和通信域 (Constructs a CAN object with an optional topic name and domain).
 
struct __attribute__ ((packed))
 经典 CAN 消息结构 (Structure representing a classic CAN message).
 

Data Fields

CAN_HandleTypeDefhcan_
 
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
 
- Data Fields inherited from LibXR::CAN
 ClassicPack
 
Topic classic_tp_
 经典 CAN 消息的主题 (Topic for classic CAN messages).
 

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 }
 CAN 消息类型 (Enumeration of CAN message types). More...
 

Detailed Description

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

Definition at line 38 of file stm32_can.hpp.

Constructor & Destructor Documentation

◆ STM32CAN()

LibXR::STM32CAN::STM32CAN ( CAN_HandleTypeDef hcan,
const char tp_name,
uint32_t  queue_size 
)
inline

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

Parameters
hcanSTM32CAN对象 CAN object
tp_name话题名称 Topic name
queue_size发送队列大小 Send queue size

Definition at line 48 of file stm32_can.hpp.

49 : CAN(tp_name),
50 hcan_(hcan),
51 id_(STM32_CAN_GetID(hcan->Instance)),
52 tx_queue_(queue_size)
53 {
54 map[id_] = this;
55 Init();
56 }
CAN(const char *name_tp="can", Topic::Domain *domain=nullptr)
构造 CAN 对象,可指定主题名称和通信域 (Constructs a CAN object with an optional topic name and domain).
Definition can.hpp:33
ErrorCode Init(void)
初始化
Definition stm32_can.hpp:63
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ AddMessage()

ErrorCode LibXR::STM32CAN::AddMessage ( const ClassicPack &  pack)
inlineoverridevirtual

添加 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 148 of file stm32_can.hpp.

149 {
151
152 txHeader.DLC = sizeof(pack.data);
153
154 switch (pack.type)
155 {
156 case Type::STANDARD:
157 txHeader.IDE = CAN_ID_STD;
159 break;
160 case Type::EXTENDED:
161 txHeader.IDE = CAN_ID_EXT;
163 break;
165 txHeader.IDE = CAN_ID_STD;
167 break;
169 txHeader.IDE = CAN_ID_EXT;
171 break;
172 }
173 txHeader.StdId = (pack.type == Type::EXTENDED) ? 0 : pack.id;
174 txHeader.ExtId = (pack.type == Type::EXTENDED) ? pack.id : 0;
175 txHeader.TransmitGlobalTime = DISABLE;
176
177 if (HAL_CAN_AddTxMessage(hcan_, &txHeader, pack.data, &txMailbox) != HAL_OK)
178 {
179 if (tx_queue_.Push(pack) != ErrorCode::OK)
180 {
181 return ErrorCode::FULL;
182 }
183 }
184
185 return ErrorCode::OK;
186 }
@ 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

◆ Init()

ErrorCode LibXR::STM32CAN::Init ( void  )
inline

初始化

Returns
ErrorCode

Definition at line 63 of file stm32_can.hpp.

64 {
66
67 can_filter.FilterIdHigh = 0;
68 can_filter.FilterIdLow = 0;
71 can_filter.FilterMaskIdHigh = 0;
72 can_filter.FilterMaskIdLow = 0;
73 can_filter.FilterFIFOAssignment = fifo_;
74 can_filter.FilterActivation = ENABLE;
75#ifdef CAN3
76 if (id_ == STM32_CAN1)
77 {
78 can_filter.FilterBank = 0;
79 can_filter.SlaveStartFilterBank = 14;
80 fifo_ = CAN_RX_FIFO0;
81 }
82 else if (id_ == STM32_CAN2)
83 {
84 can_filter.FilterBank = 14;
85 can_filter.SlaveStartFilterBank = 14;
86 fifo_ = CAN_RX_FIFO0;
87 }
88 else if (id_ == STM32_CAN3)
89 {
90 can_filter.FilterBank = 3;
91 fifo_ = CAN_RX_FIFO1;
92 }
93#else
94#ifdef CAN2
95 if (id_ == STM32_CAN1)
96 {
97 can_filter.FilterBank = 0;
98 can_filter.SlaveStartFilterBank = 14;
99 fifo_ = CAN_RX_FIFO0;
100 }
101 else if (id_ == STM32_CAN2)
102 {
103 can_filter.FilterBank = 14;
104 can_filter.SlaveStartFilterBank = 14;
105 fifo_ = CAN_RX_FIFO1;
106 }
107#else
108 if (id_ == STM32_CAN1)
109 {
110 can_filter.FilterBank = 0;
111 can_filter.SlaveStartFilterBank = 14;
112 fifo_ = CAN_RX_FIFO0;
113 }
114#endif
115#endif
116 else
117 {
118 ASSERT(false);
119 return ErrorCode::FAILED;
120 }
121 can_filter.FilterFIFOAssignment = fifo_;
122
123 if (HAL_CAN_ConfigFilter(hcan_, &can_filter) != HAL_OK)
124 {
125 return ErrorCode::FAILED;
126 }
127
128 if (HAL_CAN_Start(hcan_) != HAL_OK)
129 {
130 return ErrorCode::FAILED;
131 }
132
133 if (fifo_ == CAN_RX_FIFO0)
134 {
136 }
137 else
138 {
140 }
141
144
145 return ErrorCode::OK;
146 }

◆ ProcessRxInterrupt()

void LibXR::STM32CAN::ProcessRxInterrupt ( )
inline

处理接收中断

Definition at line 192 of file stm32_can.hpp.

193 {
194 while (HAL_CAN_GetRxMessage(hcan_, fifo_, &rx_buff_.header, rx_buff_.pack.data) ==
195 HAL_OK)
196 {
197 if (rx_buff_.header.IDE == CAN_ID_STD)
198 {
199 rx_buff_.pack.id = rx_buff_.header.StdId;
200 rx_buff_.pack.type = Type::STANDARD;
201 }
202 else
203 {
204 rx_buff_.pack.id = rx_buff_.header.ExtId;
205 rx_buff_.pack.type = Type::EXTENDED;
206 }
207
208 if (rx_buff_.header.RTR == CAN_RTR_REMOTE)
209 {
210 if (rx_buff_.pack.type == Type::STANDARD)
211 {
212 rx_buff_.pack.type = Type::REMOTE_STANDARD;
213 }
214 else
215 {
216 rx_buff_.pack.type = Type::REMOTE_EXTENDED;
217 }
218 }
219
220 classic_tp_.PublishFromCallback(rx_buff_.pack, true);
221 }
222 }
Topic classic_tp_
经典 CAN 消息的主题 (Topic for classic CAN messages).
Definition can.hpp:48
void PublishFromCallback(Data &data, bool in_isr)
从回调函数发布数据 Publishes data from a callback function
Definition message.hpp:661

◆ ProcessTxInterrupt()

void LibXR::STM32CAN::ProcessTxInterrupt ( )
inline

处理发送中断

Definition at line 228 of file stm32_can.hpp.

229 {
230 if (tx_queue_.Peek(tx_buff_.pack) == ErrorCode::OK)
231 {
232 tx_buff_.header.DLC = sizeof(tx_buff_.pack.data);
233 switch (tx_buff_.pack.type)
234 {
235 case Type::STANDARD:
236 tx_buff_.header.IDE = CAN_ID_STD;
237 tx_buff_.header.RTR = CAN_RTR_DATA;
238 break;
239 case Type::EXTENDED:
240 tx_buff_.header.IDE = CAN_ID_EXT;
241 tx_buff_.header.RTR = CAN_RTR_DATA;
242 break;
244 tx_buff_.header.IDE = CAN_ID_STD;
245 tx_buff_.header.RTR = CAN_RTR_REMOTE;
246 break;
248 tx_buff_.header.IDE = CAN_ID_EXT;
249 tx_buff_.header.RTR = CAN_RTR_REMOTE;
250 break;
251 }
252 tx_buff_.header.StdId =
253 (tx_buff_.pack.type == Type::EXTENDED) ? 0 : tx_buff_.pack.id;
254 tx_buff_.header.ExtId =
255 (tx_buff_.pack.type == Type::EXTENDED) ? tx_buff_.pack.id : 0;
256 tx_buff_.header.TransmitGlobalTime = DISABLE;
257
258 if (HAL_CAN_AddTxMessage(hcan_, &tx_buff_.header, tx_buff_.pack.data, &txMailbox) ==
259 HAL_OK)
260 {
261 tx_queue_.Pop();
262 }
263 }
264 }
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 270 of file stm32_can.hpp.

◆ hcan_

CAN_HandleTypeDef* LibXR::STM32CAN::hcan_

Definition at line 266 of file stm32_can.hpp.

◆ header [1/2]

CAN_RxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 275 of file stm32_can.hpp.

◆ header [2/2]

CAN_TxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 281 of file stm32_can.hpp.

◆ id_

stm32_can_id_t LibXR::STM32CAN::id_

Definition at line 268 of file stm32_can.hpp.

◆ map

STM32CAN * STM32CAN::map = {nullptr}
static

Definition at line 271 of file stm32_can.hpp.

◆ pack

ClassicPack LibXR::STM32CAN::pack

Definition at line 276 of file stm32_can.hpp.

◆ tx_queue_

LockFreeQueue<ClassicPack> LibXR::STM32CAN::tx_queue_

Definition at line 269 of file stm32_can.hpp.

◆ txMailbox

uint32_t LibXR::STM32CAN::txMailbox

Definition at line 285 of file stm32_can.hpp.


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