本文中CAN1采用16位掩码,CAN2只能使用大于等于14的筛选器组,根据手册只能使用32位的掩码(因为大于14的筛选器没有说明)。
经测试,STM32手册中的筛选器配置图错误,库函数的命名正确。参考STM32手册中的CAN过滤器16位标识符屏蔽配置错误说明 - smilingfrog - 博客园 (cnblogs.com)。
另外,2个掩码条件满足任意一个即可被读取。
CanTxMsg CAN1_Message;
CanTxMsg CAN2_Message;
CanRxMsg CAN1_RecData;
CanRxMsg CAN2_RecData;
void MyCAN_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//----------DATA Init----------
CAN1_Message.StdId = 0xAA;
CAN1_Message.IDE = 0;
CAN1_Message.RTR = 0;
CAN2_Message.StdId = 0xBB;
CAN2_Message.IDE = 0;
CAN2_Message.RTR = 0;
//----------GPIO Init----------
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//CAN1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12, GPIO_AF_CAN1);
//CAN2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5| GPIO_Pin_6;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5 , GPIO_AF_CAN2);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6 , GPIO_AF_CAN2);
//----------CAN Init----------
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = ENABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = MyCAN_SJW;
CAN_InitStructure.CAN_BS1 = MyCAN_BS1;
CAN_InitStructure.CAN_BS2 = MyCAN_BS2;
CAN_InitStructure.CAN_Prescaler = MyCAN_PRE;
CAN_Init(CAN1, &CAN_InitStructure);
CAN_Init(CAN2, &CAN_InitStructure);
//----------Filter Init----------
//CAN1
CAN_FilterInitStructure.CAN_FilterNumber = 6;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
CAN_FilterInitStructure.CAN_FilterIdLow = 0xAA << 5;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0xAA << 5;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFE0;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFE0;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
//CAN2
CAN_FilterInitStructure.CAN_FilterNumber = 14;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
//----------NVIC Init----------
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//CAN1
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_Init(&NVIC_InitStructure);
//CAN2
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_Init(&NVIC_InitStructure);
//----------ENABLE----------
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN2,CAN_IT_FMP0, ENABLE);
}
void CAN1_SendData(uchar* Str, uchar Len)
{
CAN1_Message.DLC = Len;
for(uchar i = 0; i < Len; i ++)
{
CAN1_Message.Data[i] = Str[i];
}
uchar MailBox = CAN_Transmit(CAN1, &CAN1_Message);
while(CAN_TransmitStatus(CAN1, MailBox) == CAN_TxStatus_Failed);
}
void CAN2_SendData(uchar* Str, uchar Len)
{
CAN2_Message.DLC = Len;
for(uchar i = 0; i < Len; i ++)
{
CAN2_Message.Data[i] = Str[i];
}
uchar MailBox = CAN_Transmit(CAN2, &CAN2_Message);
while(CAN_TransmitStatus(CAN2, MailBox) == CAN_TxStatus_Failed);
}
void CAN1_RX0_IRQHandler(void)
{
CAN_Receive(CAN1, 0, &CAN1_RecData);
}
void CAN2_RX0_IRQHandler(void)
{
CAN_Receive(CAN2, 0, &CAN2_RecData);
}