0%

CAN总线学习——过滤器及API

Main Takeaway

记录学习CAN的历程,主要是谈谈对CAN(bxCAN)过滤器及CAN的API。

来填上次的坑!

Tips:我对CAN的API应用并不是很了解,因为CAN至少需要在两块单片机上运用,所以我并未做实验,没有亲手敲过,以后实践后再补充

两种过滤模式

列表模式

将ID输入列表,进行对照即可

Tips:资源有限,受数量限制

掩码模式

验证码与屏蔽码

ID通过哪几位由屏蔽码决定,再将通过的那几位与验证码进行比较

掩码模式的计算过程

在bxCAN中,分别采用了两个寄存器(CAN_FiR1,CAN_FiR2)来存储屏蔽码与验证码,从而实现掩码模式的工作流程的

Tips:采用掩码模式的方式并不能精确的对每一个ID进行过滤eg:我想要1150——1158,然后我验证115满足即可,那么1159也会通过,不精确

模式优点缺点
列表模式能精确地过滤每个指定的CAN ID有数量限制
掩码模式取决于屏蔽码,有时无法完全精确到每一个CAN ID,部分不期望的CAN ID有时也会收到数量取决于屏蔽码,最多无上限

bxCAN的过滤器的解决方案

  • 过滤模式的位就定义在CAN_FM1R寄存器中的FBMx位:”0”表示掩码模式,”1”表示列表模式。

  • 一个标志用告诉过滤器是否需要处理32位的CAN ID:标准CAN ID位11位,而扩展CAN ID有29位,对于标准的CAN ID来说,我们有一个16位的寄存器来处理他足够了,相应地,扩展CAN ID,我们就必须使用32位的寄存器来处理它。设置了这么一个寄存器CAN_FS1R来表示CAN ID的位宽

API

1
2
3
4
/**
* @brief CAN filter configuration structure definition
*/
CAN_FilterTypeDef;
1
2
3
4
5
6
7
8
9
10
/**
* @brief Configures the CAN reception filter according to the specified
* parameters in the CAN_FilterInitStruct.
* @param hcan pointer to a CAN_HandleTypeDef structure that contains
* the configuration information for the specified CAN.
* @param sFilterConfig pointer to a CAN_FilterTypeDef structure that
* contains the filter configuration information.
* @retval None
*/
HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan,CAN_FilterTypeDef *sFilterConfig)
1
2
3
4
5
6
7
/**
* @brief Start the CAN module.
* @param hcan pointer to an CAN_HandleTypeDef structure that contains
* the configuration information for the specified CAN.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CAN_Start(CAN_HandleTypeDef *hcan)
1
2
3
4
5
6
7
8
9
10
/**
* @brief Enable interrupts.
* @param hcan pointer to an CAN_HandleTypeDef structure that contains
* the configuration information for the specified CAN.
* @param ActiveITs indicates which interrupts will be enabled.
* This parameter can be any combination of @arg CAN_Interrupts.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t
ActiveITs)
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @brief Add a message to the first free Tx mailbox and activate the
* corresponding transmission request.
* @param hcan pointer to a CAN_HandleTypeDef structure that contains
* the configuration information for the specified CAN.
* @param pHeader pointer to a CAN_TxHeaderTypeDef structure.
* @param aData array containing the payload of the Tx frame.
* @param pTxMailbox pointer to a variable where the function will return
* the TxMailbox used to store the Tx message.
* This parameter can be a value of @arg CAN_Tx_Mailboxes.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan,CAN_TxHeaderTypeDef *pHeader, uint8_t aData[], uint32_t *pTxMailbox)
1
2
3
4
5
6
7
8
9
10
11
12
/**
* @brief Get an CAN frame from the Rx FIFO zone into the message RAM.
* @param hcan pointer to an CAN_HandleTypeDef structure that contains
* the configuration information for the specified CAN.
* @param RxFifo Fifo number of the received message to be read.
* This parameter can be a value of @arg CAN_receive_FIFO_number.
* @param pHeader pointer to a CAN_RxHeaderTypeDef structure where the header
* of the Rx frame will be stored.
* @param aData array where the payload of the Rx frame will be stored.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo,CAN_RxHeaderTypeDef *pHeader, uint8_t aData[])

References