#include "can.h"
extern CanTxMsg TxMessage;
static void Init_RxMes(CanRxMsg *RxMessage)
{
uint8_t i = 0;
RxMessage->StdId = 0x00;
RxMessage->ExtId = 0x00;
RxMessage->IDE = CAN_ID_STD;
RxMessage->DLC = 0;
RxMessage->FMI = 0;
for (i = 0; i < 8; i++)
{
RxMessage->Data[i] = 0x00;
}
}
/*
* 函数名:CAN_GPIO_Config
* 描述 :CAN的GPIO 配置,PB8上拉输入,PB9推挽输出
* 输入 :无
* 输出 : 无
* 调用 :内部调用
*/
static void CAN_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*外设时钟设置*/
/* GPIOB, GPIOD and AFIO clocks enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB, ENABLE);
/* CAN1 and CAN2 Periph clocks enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);
// /* Remap CAN1 and CAN2 GPIOs */
/* Configure CAN1 RX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN2 RX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN1 TX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN2 TX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap2_CAN1 , ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE);
}
/*
* 函数名:CAN_NVIC_Config
* 描述 :CAN的NVIC 配置,第1优先级组,0,0优先级
* 输入 :无
* 输出 : 无
* 调用 :内部调用
*/
//static void CAN_NVIC_Config(void)
//{
// NVIC_InitTypeDef NVIC_InitStructure;
// /* Configure one bit for preemption priority */
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// /*中断设置*/
//// NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; //CAN1 RX0中断
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级为0
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
//}
/*
* 函数名:CAN_Mode_Config
* 描述 :CAN的模式 配置
* 输入 :无
* 输出 : 无
* 调用 :内部调用
*/
static void CAN_Mode_Config(void)
{
CAN_InitTypeDef CAN_InitStructure;
/************************CAN通信参数设置**********************************/
/*CAN寄存器初始化*/
CAN_DeInit(CAN1);
CAN_DeInit(CAN2);
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM = DISABLE; /*失能时间触发通信模式*/
CAN_InitStructure.CAN_ABOM = DISABLE; /*失能自动离线管理*/
CAN_InitStructure.CAN_AWUM = DISABLE; /*失能自动唤醒模式*/
CAN_InitStructure.CAN_NART = DISABLE; /*失能非自动重传模式*/
CAN_InitStructure.CAN_RFLM = DISABLE; /*失能接收FIFO锁定模式*/
CAN_InitStructure.CAN_TXFP = DISABLE; /*失能发送FIFO*/
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; /*工作在正常模式*/
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler = 4;
CAN_Init(CAN1, &CAN_InitStructure);
CAN_Init(CAN2, &CAN_InitStructure);
}
/*
* 函数名:CAN_Filter_Config
* 描述 :CAN的过滤器 配置
* 输入 :无
* 输出 : 无
* 调用 :内部调用
*/
static void CAN_Filter_Config(void)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN1滤波器初始化*/
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* CAN2滤波器初始化*/
CAN_FilterInitStructure.CAN_FilterNumber = 14;
CAN_FilterInit(&CAN_FilterInitStructure);
// /*CAN通信中断使能*/
// CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
}
/*
* 函数名:CAN_Config
* 描述 :完整配置CAN的功能
* 输入 :无
* 输出 : 无
* 调用 :外部调用
*/
void CAN_Config(void)
{
CAN_GPIO_Config();
// CAN_NVIC_Config();
CAN_Mode_Config();
CAN_Filter_Config();
}
/*
* 函数名:CAN_SetMsg
* 描述 :CAN通信报文内容设置
* 输入 :无
* 输出 : 无
* 调用 :外部调用
*/
void CAN_SetMsg(void)
{
TxMessage.StdId = 0x321; /*设定标准标识符*/
TxMessage.ExtId = 0x01; /*设定扩展标识符*/
TxMessage.RTR = CAN_RTR_DATA; /*数据帧*/
TxMessage.IDE = CAN_ID_STD; /*使用标准标识符*/
TxMessage.DLC = 1; /*待传输消息的帧长度*/
}
/**************************END OF FILE************************************/ |