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()

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

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

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

Definition at line 47 of file stm32_can.hpp.

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

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 144 of file stm32_can.hpp.

145 {
146 CAN_TxHeaderTypeDef txHeader; // NOLINT
147
148 txHeader.DLC = sizeof(pack.data);
149
150 switch (pack.type)
151 {
152 case Type::STANDARD:
153 txHeader.IDE = CAN_ID_STD;
154 txHeader.RTR = CAN_RTR_DATA;
155 break;
156 case Type::EXTENDED:
157 txHeader.IDE = CAN_ID_EXT;
158 txHeader.RTR = CAN_RTR_DATA;
159 break;
161 txHeader.IDE = CAN_ID_STD;
162 txHeader.RTR = CAN_RTR_REMOTE;
163 break;
165 txHeader.IDE = CAN_ID_EXT;
166 txHeader.RTR = CAN_RTR_REMOTE;
167 break;
168 default:
169 ASSERT(false);
170 return ErrorCode::FAILED;
171 }
172 txHeader.StdId = (pack.type == Type::EXTENDED) ? 0 : pack.id;
173 txHeader.ExtId = (pack.type == Type::EXTENDED) ? pack.id : 0;
174 txHeader.TransmitGlobalTime = DISABLE;
175
176 if (HAL_CAN_AddTxMessage(hcan_, &txHeader, pack.data, &txMailbox) != HAL_OK)
177 {
178 Mutex::LockGuard lock(write_mutex_);
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 59 of file stm32_can.hpp.

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

◆ 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 if (rx_buff_.header.StdId == 2046)
200 {
201 __NOP();
202 }
203 rx_buff_.pack.id = rx_buff_.header.StdId;
204 rx_buff_.pack.type = Type::STANDARD;
205 }
206 else
207 {
208 rx_buff_.pack.id = rx_buff_.header.ExtId;
209 rx_buff_.pack.type = Type::EXTENDED;
210 }
211
212 if (rx_buff_.header.RTR == CAN_RTR_REMOTE)
213 {
214 if (rx_buff_.pack.type == Type::STANDARD)
215 {
216 rx_buff_.pack.type = Type::REMOTE_STANDARD;
217 }
218 else
219 {
220 rx_buff_.pack.type = Type::REMOTE_EXTENDED;
221 }
222 }
223 OnMessage(rx_buff_.pack, true);
224 }
225 }

◆ ProcessTxInterrupt()

void LibXR::STM32CAN::ProcessTxInterrupt ( )
inline

处理发送中断

Definition at line 231 of file stm32_can.hpp.

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

◆ hcan_

CAN_HandleTypeDef* LibXR::STM32CAN::hcan_

Definition at line 272 of file stm32_can.hpp.

◆ header [1/2]

CAN_RxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 281 of file stm32_can.hpp.

◆ header [2/2]

CAN_TxHeaderTypeDef LibXR::STM32CAN::header

Definition at line 287 of file stm32_can.hpp.

◆ id_

stm32_can_id_t LibXR::STM32CAN::id_

Definition at line 274 of file stm32_can.hpp.

◆ map

STM32CAN * STM32CAN::map = {nullptr}
static

Definition at line 277 of file stm32_can.hpp.

◆ pack

ClassicPack LibXR::STM32CAN::pack

Definition at line 282 of file stm32_can.hpp.

◆ tx_queue_

LockFreeQueue<ClassicPack> LibXR::STM32CAN::tx_queue_

Definition at line 275 of file stm32_can.hpp.

◆ txMailbox

uint32_t LibXR::STM32CAN::txMailbox

Definition at line 291 of file stm32_can.hpp.

◆ write_mutex_

Mutex LibXR::STM32CAN::write_mutex_

Definition at line 292 of file stm32_can.hpp.


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