STM32 HAL庫的CAN匯流排濾波器設定
stm32cubemx生成的can初始化程式碼
#include "can.h"
#include "gpio.h"
CAN_HandleTypeDef hcan1;
CAN_HandleTypeDef hcan2;
void MX_CAN1_Init(void)
{
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 3;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SJW = CAN_SJW_1TQ;
hcan1.Init.BS1 = CAN_BS1_9TQ;
hcan1.Init.BS2 = CAN_BS2_4TQ;
hcan1.Init.TTCM = DISABLE;
hcan1.Init.ABOM = ENABLE;
hcan1.Init.AWUM = DISABLE;
hcan1.Init.NART = DISABLE;
hcan1.Init.RFLM = DISABLE;
hcan1.Init.TXFP = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
void MX_CAN2_Init(void)
{
hcan2.Instance = CAN2;
hcan2.Init.Prescaler = 3;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.SJW = CAN_SJW_1TQ;
hcan2.Init.BS1 = CAN_BS1_9TQ;
hcan2.Init.BS2 = CAN_BS2_4TQ;
hcan2.Init.TTCM = DISABLE;
hcan2.Init.ABOM = ENABLE;
hcan2.Init.AWUM = DISABLE;
hcan2.Init.NART = DISABLE;
hcan2.Init.RFLM = DISABLE;
hcan2.Init.TXFP = DISABLE;
if (HAL_CAN_Init(&hcan2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
針對濾波器的設定
#include "my_can.h"
uint16_t data[4]={0};
void My_CAN_FilterConfig(CAN_HandleTypeDef* _hcan)
{
CAN_FilterConfTypeDef CAN_FilterConfigStructure;
static CanTxMsgTypeDef Tx1Message;
static CanRxMsgTypeDef Rx1Message;
static CanTxMsgTypeDef Tx2Message;
static CanRxMsgTypeDef Rx2Message;
CAN_FilterConfigStructure.FilterNumber = 0;
CAN_FilterConfigStructure.FilterFIFOAssignment = CAN_FilterFIFO0;
CAN_FilterConfigStructure.BankNumber = 14;
CAN_FilterConfigStructure.FilterActivation = ENABLE;
if(_hcan == &hcan1){
CAN_FilterConfigStructure.FilterMode = CAN_FILTERMODE_IDLIST;
CAN_FilterConfigStructure.FilterScale = CAN_FILTERSCALE_16BIT;
CAN_FilterConfigStructure.FilterIdHigh = (0x201 << 5);
CAN_FilterConfigStructure.FilterIdLow = (0x202 << 5);
CAN_FilterConfigStructure.FilterMaskIdHigh = (0x203 << 5);
CAN_FilterConfigStructure.FilterMaskIdLow = (0x204 << 5);
if(HAL_CAN_ConfigFilter(_hcan, &CAN_FilterConfigStructure) != HAL_OK)
{
while(1);
}
_hcan->pTxMsg = &Tx1Message;
_hcan->pRxMsg = &Rx1Message;
}
if(_hcan == &hcan2){
CAN_FilterConfigStructure.FilterMode = CAN_FILTERMODE_IDLIST;
CAN_FilterConfigStructure.FilterScale = CAN_FILTERSCALE_16BIT;
CAN_FilterConfigStructure.FilterIdHigh = (0x301 << 5);
CAN_FilterConfigStructure.FilterIdLow = (0x302<< 5);
CAN_FilterConfigStructure.FilterMaskIdHigh = (0x303<< 5);
CAN_FilterConfigStructure.FilterMaskIdLow = (0x304<<5);
CAN_FilterConfigStructure.FilterNumber = 14;
if(HAL_CAN_ConfigFilter(_hcan, &CAN_FilterConfigStructure) != HAL_OK)
{
while(1);
}
_hcan->pTxMsg = &Tx2Message;
_hcan->pRxMsg = &Rx2Message;
}
}
通過can傳送資料
HAL_StatusTypeDef CAN2_SEND_Data(uint16_t *p)
{
HCAN_2.pTxMsg->RTR = CAN_RTR_DATA;
HCAN_2.pTxMsg->IDE = CAN_ID_STD;
HCAN_2.pTxMsg->DLC = 8;
HCAN_2.pTxMsg->StdId = 0x123;
HCAN_2.pTxMsg->Data[0] = (uint8_t)(((int16_t)p[0])>>8);
HCAN_2.pTxMsg->Data[1] = (uint8_t)(p[0]);
HCAN_2.pTxMsg->Data[2] = (uint8_t)(p[1]>>8);
HCAN_2.pTxMsg->Data[3] = (uint8_t)(p[1]);
HCAN_2.pTxMsg->Data[4] = (uint8_t)((p[2])>>8);
HCAN_2.pTxMsg->Data[5] = (uint8_t)(p[2]);
HCAN_2.pTxMsg->Data[6] = (uint8_t)(((int16_t)p[3])>>8);
HCAN_2.pTxMsg->Data[7] = (uint8_t)(p[3]);
return(HAL_CAN_Transmit(&HCAN_2, 100));
}
can匯流排中斷服務函數以及資料處理常式
void Receive_DATA(CAN_HandleTypeDef* hcan)
{
data[0] = (uint16_t)(hcan->pRxMsg->Data[0]<<8 | hcan->pRxMsg->Data[1]);
data[1] = (int16_t)(hcan->pRxMsg->Data[2]<<8 | hcan->pRxMsg->Data[3]);
data[2] = (int16_t)(hcan->pRxMsg->Data[4]<<8 | hcan->pRxMsg->Data[5]);
data[3] = (int16_t)(hcan->pRxMsg->Data[6]<<8 | hcan->pRxMsg->Data[7]);
}
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* _hcan)
{
if(_hcan==&HCAN)
{
switch (_hcan->pRxMsg->StdId)
{
case 0x201:
Receive_DATA(_hcan);
break;
}
__HAL_CAN_ENABLE_IT(_hcan, CAN_IT_FMP0);
}
else if(_hcan==&HCAN_2)
{
switch (_hcan->pRxMsg->StdId)
{
case 0x301:
break;
}
__HAL_CAN_ENABLE_IT(_hcan, CAN_IT_FMP0);
}
}