CAN_HandleTypeDef CAN1_Handler;
CAN_HandleTypeDef CAN2_Handler;
uint8_t CAN1_Mode_Init(uint32_t tsjw,uint32_t tbs2,uint32_t tbs1,uint16_t brp,uint32_t mode)
{
CAN_FilterTypeDef CAN1_FilerConf;
CAN1_Handler.Instance=CAN1;
CAN1_Handler.Init.Prescaler=brp;
CAN1_Handler.Init.Mode=mode;
CAN1_Handler.Init.SyncJumpWidth=tsjw;
CAN1_Handler.Init.TimeSeg1=tbs1;
CAN1_Handler.Init.TimeSeg2=tbs2;
CAN1_Handler.Init.TimeTriggeredMode=DISABLE;
CAN1_Handler.Init.AutoBusOff=DISABLE;
CAN1_Handler.Init.AutoWakeUp=DISABLE;
CAN1_Handler.Init.AutoRetransmission=DISABLE;
CAN1_Handler.Init.ReceiveFifoLocked=DISABLE;
CAN1_Handler.Init.TransmitFifoPriority=DISABLE;
if(HAL_CAN_Init(&CAN1_Handler)!=HAL_OK) return 1;
CAN1_FilerConf.FilterIdHigh=0X0000;
CAN1_FilerConf.FilterIdLow=0X0000;
CAN1_FilerConf.FilterMaskIdHigh=0X0000;
CAN1_FilerConf.FilterMaskIdLow=0X0000;
CAN1_FilerConf.FilterFIFOAssignment=CAN_FILTER_FIFO0;
CAN1_FilerConf.FilterBank=0;
CAN1_FilerConf.FilterMode=CAN_FILTERMODE_IDMASK;
CAN1_FilerConf.FilterScale=CAN_FILTERSCALE_32BIT;
CAN1_FilerConf.FilterActivation=ENABLE;
CAN1_FilerConf.SlaveStartFilterBank=15;
if(HAL_CAN_ConfigFilter(&CAN1_Handler,&CAN1_FilerConf)!=HAL_OK) return 2;
return 0;
}
uint8_t CAN2_Mode_Init(uint32_t tsjw,uint32_t tbs2,uint32_t tbs1,uint16_t brp,uint32_t mode)
{
CAN_FilterTypeDef CAN2_FilerConf;
CAN2_Handler.Instance=CAN2;
CAN2_Handler.Init.Prescaler=brp;
CAN2_Handler.Init.Mode=mode;
CAN2_Handler.Init.SyncJumpWidth=tsjw;
CAN2_Handler.Init.TimeSeg1=tbs1;
CAN2_Handler.Init.TimeSeg2=tbs2;
CAN2_Handler.Init.TimeTriggeredMode=DISABLE;
CAN2_Handler.Init.AutoBusOff=DISABLE;
CAN2_Handler.Init.AutoWakeUp=DISABLE;
CAN2_Handler.Init.AutoRetransmission=DISABLE;
CAN2_Handler.Init.ReceiveFifoLocked=DISABLE;
CAN2_Handler.Init.TransmitFifoPriority=DISABLE;
if(HAL_CAN_Init(&CAN2_Handler)!=HAL_OK) return 1;
CAN2_FilerConf.FilterIdHigh=0X0000;
CAN2_FilerConf.FilterIdLow=0X0000;
CAN2_FilerConf.FilterMaskIdHigh=0X0000;
CAN2_FilerConf.FilterMaskIdLow=0X0000;
CAN2_FilerConf.FilterFIFOAssignment=CAN_FILTER_FIFO0;
CAN2_FilerConf.FilterBank=15;
CAN2_FilerConf.FilterMode=CAN_FILTERMODE_IDMASK;
CAN2_FilerConf.FilterScale=CAN_FILTERSCALE_32BIT;
CAN2_FilerConf.FilterActivation=ENABLE;
CAN2_FilerConf.SlaveStartFilterBank=15;
if(HAL_CAN_ConfigFilter(&CAN2_Handler,&CAN2_FilerConf)!=HAL_OK) return 2;
return 0;
}
static int CAN1_CLK_ENABLED=0;
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_Initure;
if(hcan->Instance == CAN1_Handler.Instance)
{
CAN1_CLK_ENABLED++;
if(CAN1_CLK_ENABLED == 1)
{
__HAL_RCC_CAN1_CLK_ENABLE();
}
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_11|GPIO_PIN_12;
GPIO_Initure.Mode=GPIO_MODE_AF_PP;
GPIO_Initure.Pull=GPIO_PULLUP;
GPIO_Initure.Speed=GPIO_SPEED_FAST;
GPIO_Initure.Alternate=GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
#if CAN1_RX0_INT_ENABLE
HAL_NVIC_SetPriority(CAN1_RX0_IRQn,1,0);
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
#endif
}
if(hcan->Instance == CAN2_Handler.Instance)
{
__HAL_RCC_CAN2_CLK_ENABLE();
CAN1_CLK_ENABLED++;
if(CAN1_CLK_ENABLED == 1)
{
__HAL_RCC_CAN1_CLK_ENABLE();
}
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_12|GPIO_PIN_13;
GPIO_Initure.Mode=GPIO_MODE_AF_PP;
GPIO_Initure.Pull=GPIO_PULLUP;
GPIO_Initure.Speed=GPIO_SPEED_FAST;
GPIO_Initure.Alternate=GPIO_AF9_CAN2;
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
#if CAN2_RX0_INT_ENABLE
HAL_NVIC_SetPriority(CAN2_RX0_IRQn,1,0);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
#endif
}
}
#if CAN1_RX0_INT_ENABLE
void CAN1_RX0_IRQHandler(void)
{
HAL_CAN_IRQHandler(&CAN1_Handler);
}
#endif
#if CAN2_RX0_INT_ENABLE
void CAN2_RX0_IRQHandler(void)
{
HAL_CAN_IRQHandler(&CAN2_Handler);
}
#endif
#if CAN1_RX0_INT_ENABLE | CAN2_RX0_INT_ENABLE
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef* hcan)
{
static CAN_RxHeaderTypeDef CAN_RX_HDR;
CAN_MSG_TYPE CANA_RX_BUF;
CAN_MSG_TYPE CANB_RX_BUF;
if (hcan->Instance == CAN1_Handler.Instance)
{
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &CAN_RX_HDR, CANA_RX_BUF.data) == HAL_OK)
{
CANA_RX_BUF.id = CAN_RX_HDR.StdId;
CanTasks(CANA_RX_BUF);
HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
}
}
else if (hcan->Instance == CAN2_Handler.Instance)
{
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &CAN_RX_HDR, CANB_RX_BUF.data) == HAL_OK)
{
CANB_RX_BUF.id = CAN_RX_HDR.StdId;
CanTasks(CANB_RX_BUF);
HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
}
}
}
#endif
uint8_t CAN12_Transmit(CAN_MSG_TYPE buf)
{
uint32_t txmailbox = 0;
uint32_t offset = 0;
CAN_TxHeaderTypeDef hdr;
hdr.IDE = CAN_ID_STD;
hdr.RTR = CAN_RTR_DATA;
hdr.StdId = buf.id;
hdr.TransmitGlobalTime = DISABLE;
while (buf.len != 0)
{
hdr.DLC = buf.len > 8 ? 8 : buf.len;
if (HAL_CAN_AddTxMessage(&CAN1_Handler, &hdr, ((uint8_t *)buf.data) + offset, &txmailbox) != HAL_OK)
{
return 1;
}
if (HAL_CAN_AddTxMessage(&CAN2_Handler, &hdr, ((uint8_t *)buf.data) + offset, &txmailbox) != HAL_OK)
{
return 2;
}
offset += hdr.DLC;
buf.len -= hdr.DLC;
}
return 0;
}
void CAN12_start(void)
{
HAL_CAN_Start(&CAN1_Handler);
HAL_CAN_Start(&CAN2_Handler);
HAL_CAN_ActivateNotification(&CAN1_Handler, CAN_IT_RX_FIFO0_MSG_PENDING);
HAL_CAN_ActivateNotification(&CAN2_Handler, CAN_IT_RX_FIFO0_MSG_PENDING);
}
#ifndef __CAN_H
#define __CAN_H
#include "sys.h"
typedef struct
{
uint16_t id;
uint8_t data[8];
uint8_t len;
}CAN_MSG_TYPE;
#define CAN1_RX0_INT_ENABLE 1
#define CAN2_RX0_INT_ENABLE 1
uint8_t CAN1_Mode_Init(uint32_t tsjw,uint32_t tbs2,uint32_t tbs1,uint16_t brp,uint32_t mode);
uint8_t CAN2_Mode_Init(uint32_t tsjw,uint32_t tbs2,uint32_t tbs1,uint16_t brp,uint32_t mode);
uint8_t CAN12_Transmit(CAN_MSG_TYPE buf);
void CAN12_start(void);
#endif