1、对于can的驱动函数文件加注释。在can(加注释).c中标了“//2024.6”的语句加以理解并写出注释。放入博客中
//======================================================================
//文件名称:can.c
//功能概要:uart底层驱动构件源文件
//版权所有:苏州大学嵌入式系统与物联网研究所(sumcu.suda.edu.cn)
//更新记录:2021-02-03 V1.0 JJL
//======================================================================
#include "can.h"
CAN_TypeDef *CAN_ARR[] = {(CAN_TypeDef*)CAN1_BASE};
IRQn_Type table_irq_can[2] = {CAN1_RX0_IRQn, CAN1_RX1_IRQn};
uint8_t can_send_once(uint8_t canNo, uint32_t DestID, uint16_t len ,uint8_t* buff);
uint8_t CAN_HWInit(uint8_t CANChannel);
uint8_t CAN_SWInit_Entry(uint8_t canNo);
void CAN_SWInit_CTLMode(uint8_t canNo);
void CAN_SWInit_BT(uint8_t canNo, uint32_t CANMode, uint32_t Prescaler);
uint8_t CAN_SWInit_Quit(uint8_t canNo);
uint8_t CANFilterConfig(uint8_t canNo, uint32_t canID, uint32_t FilterBank, uint32_t Can_Rx_FifoNo, uint8_t IsActivate, uint32_t FilterMode, uint32_t FilterScale);
//=====================================================================
//函数名称:can_init
//函数返回:无
//参数说明:canNo:模块号,本芯片只有CAN_1
// canID:自身CAN节点的唯一标识,例如按照CANopen协议给出
// BitRate:位速率
//功能概要:初始化CAN模块
//=====================================================================
void can_init(uint8_t canNo, uint32_t canID, uint32_t BitRate)
{
//声明Init函数使用的局部变量
uint32_t CANMode;
uint32_t CANFilterBank;
uint32_t CANFiltermode;
uint32_t CAN_Filterscale;
//给Init函数使用的局部变量赋初值
CANMode = CAN_MODE_NORMAL; //2024.6 CAN设置为正常模式
CANFilterBank = CANFilterBank0;
CANFiltermode = CAN_FILTERMODE_IDMASK;
CAN_Filterscale = CAN_FILTERSCALE_32BIT;
//(1)CAN总线硬件初始化
CAN_HWInit(CAN_CHANNEL);
//(2)CAN总线进入软件初始化模式
CAN_SWInit_Entry(canNo);
//(3)CAN总线模式设置
CAN_SWInit_CTLMode(canNo);
//(4)CAN总线位时序配置
CAN_SWInit_BT(canNo,CANMode,BitRate);
//(5)CAN总线过滤器初始化
CANFilterConfig(canNo, canID, CANFilterBank, CAN_RX_FIFO0, 1, CANFiltermode, CAN_Filterscale);
//(6)CAN总线退出软件初始化模式,进入正常模式
CAN_SWInit_Quit(canNo);
}
//=====================================================================
//函数名称:can_send
//函数返回:0=正常,1=错误
//参数说明:canNo:模块号,本芯片只有CAN_1
// DestID:目标CAN节点的唯一标识,例如按照CANopen协议给出
// len:待发送数据的字节数
// buff:待发送数据发送缓冲区首地址
//功能概要:CAN模块发送数据
//=====================================================================
uint8_t can_send(uint8_t canNo, uint32_t DestID, uint16_t len ,uint8_t* buff)
{
if(DestID > 0x1FFFFFFFU) return 1;
uint8_t send_length;
for(int i = len; i > 0; i = i-8)
{
send_length = (i>8)?8:i;
if(can_send_once(canNo,DestID,send_length,buff+len-i) == 1) //2024.6 CAN模块向DestID放送数据
{
return 1;
}
}
return 0;
}
//=====================================================================
//函数名称:can_recv
//函数返回:接收到的字节数
//参数说明:canNo:模块号,本芯片只有CAN_1
// buff:接收到的数据存放的内存区首地址
//功能概要:在CAN模块接收中断中调用本函数接收已经到达的数据
//=====================================================================
uint8_t can_recv(uint8_t canNo, uint8_t *buff)
{
uint8_t len;
uint32_t RxFifo = CAN_RX_FIFO0;
//(1)判断哪个邮箱收到了报文信息
if(RxFifo == CAN_RX_FIFO0)
{
if ((CAN_ARR[canNo-1]->RF0R & CAN_RF0R_FMP0) == 0U) //2024.6 检查CAN消息待处理区0中是否有未处理的报文
{
return 1;
}
}
else
{
if ((CAN_ARR[canNo-1]->RF1R & CAN_RF1R_FMP1) == 0U)
{
return 1;
}
}
//(2)获取数据长度
len = (CAN_RDT0R_DLC & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos; //2024.6 清除消息待处理区的报文挂起标志(RFOM0),表示已经处理完当前报文,消息待处理区可以接收下一帧数据。
//(3)获取数据帧中的数据
buff[0] = (uint8_t)((CAN_RDL0R_DATA0 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA0_Pos);
buff[1] = (uint8_t)((CAN_RDL0R_DATA1 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA1_Pos);
buff[2] = (uint8_t)((CAN_RDL0R_DATA2 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA2_Pos);
buff[3] = (uint8_t)((CAN_RDL0R_DATA3 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA3_Pos);
buff[4] = (uint8_t)((CAN_RDH0R_DATA4 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA4_Pos);
buff[5] = (uint8_t)((CAN_RDH0R_DATA5 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA5_Pos);
buff[6] = (uint8_t)((CAN_RDH0R_DATA6 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA6_Pos);
buff[7] = (uint8_t)((CAN_RDH0R_DATA7 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA7_Pos);
//(4)清除标志位,等待接收下一帧数据
if (RxFifo == CAN_RX_FIFO0)
{
SET_BIT(CAN_ARR[canNo-1]->RF0R, CAN_RF0R_RFOM0); //2024.6 清除消息待处理区的报文挂起标志(RFOM0),表示已经处理完当前报文,消息待处理区可以接收下一帧数据
}
else
{
SET_BIT(CAN_ARR[canNo-1]->RF1R, CAN_RF1R_RFOM1);
}
return len;
}
//=====================================================================
//函数名称:CAN_enable_re_int
//函数返回:无
//参数说明:canNo:模块基地址号,Can_Rx_FifoNo:中断使用的邮箱号
//功能概要:CAN接收中断开启
//=====================================================================
void can_enable_recv_int(uint8_t canNo)
{
uint8_t Can_Rx_FifoNo;
Can_Rx_FifoNo = CAN_RX_FIFO0;
if(Can_Rx_FifoNo == CAN_RX_FIFO0)
SET_BIT(CAN_ARR[canNo-1]->IER, CAN_IER_FMPIE0); //2024.6 设置CAN控制器中断使能寄存器(IER)的接收区0消息挂起中断使能位,启用接收区的中断。
else
SET_BIT(CAN_ARR[canNo-1]->IER,CAN_IER_FMPIE1);
NVIC_EnableIRQ(table_irq_can[Can_Rx_FifoNo]); //2024.6 使能中断
}
//=====================================================================
//函数名称:can_disable_recv_int
//函数返回:无
//参数说明:canNo:模块号,本芯片只有CAN_1
//功能概要:关闭CAN接收中断
//=====================================================================
void can_disable_recv_int (uint8_t canNo)
{
uint8_t Can_Rx_FifoNo;
Can_Rx_FifoNo = CAN_RX_FIFO0;
if(Can_Rx_FifoNo == CAN_RX_FIFO0)
CLEAR_BIT(CAN_ARR[canNo-1]->IER, CAN_IER_FMPIE0);
else
CLEAR_BIT(CAN_ARR[canNo-1]->IER,CAN_IER_FMPIE1);
NVIC_DisableIRQ(table_irq_can[Can_Rx_FifoNo]);
}
//=====================================================================
//函数名称:can_send_once
//函数返回:0=正常,1=错误
//参数说明:canNo:模块号,本芯片只有CAN_1
// DestID:目标CAN节点的唯一标识,例如按照CANopen协议给出
// len:待发送数据的字节数
// buff:待发送数据发送缓冲区首地址
//功能概要:CAN模块发送一次数据
//=====================================================================
uint8_t can_send_once(uint8_t canNo, uint32_t DestID, uint16_t len ,uint8_t* buff)
{
//(1)定义Can发送函数所需要用到的变量
uint32_t transmit_mailbox;
uint32_t register_tsr;
uint32_t rtr;
rtr = CAN_RTR_DATA;
register_tsr = READ_REG(CAN_ARR[canNo-1]->TSR);
//(2)判断3个邮箱中是否有空闲邮箱,若有,选取其中一个进行发送,选取顺序为1,2,3
if (((register_tsr & CAN_TSR_TME0) != 0U) ||
((register_tsr & CAN_TSR_TME1) != 0U) ||
((register_tsr & CAN_TSR_TME2) != 0U))
{
transmit_mailbox = (register_tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos; //2024.6 从 TSR 寄存器中提取空闲接受区编号,将其存储在 transmit_mailbox 变量中。
if(transmit_mailbox > 2U)
{
return 1;
}
//(2.1)判断并设置发送帧为标准帧还是扩展帧
if(DestID <= 0x7FFU) //2024.6 判断目标 ID 是否为标准帧(ID <= 0x7FF),以决定接下来设置的帧类型
{
CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TIR = ((DestID << CAN_TI0R_STID_Pos)|CAN_ID_STD|rtr); //2024.6 设置发送帧为标准帧并设置目标ID
}
else
{
CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TIR = ((DestID << CAN_TI0R_EXID_Pos)|CAN_ID_EXT|rtr); //2024.6 设置发送帧为扩展帧并设置目标ID
}
//(2.2)设置发送帧的数据长度
CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TDTR = len;
//SET_BIT(CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TDTR, CAN_TDT0R_TGT);
//(2.3)设置发送帧的数据
WRITE_REG(CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TDHR, //2024.6 将发送数据缓冲区的高4字节数据写入邮箱的高位数据寄存器TDHR
((uint32_t)buff[7] << CAN_TDH0R_DATA7_Pos) |
((uint32_t)buff[6] << CAN_TDH0R_DATA6_Pos) |
((uint32_t)buff[5] << CAN_TDH0R_DATA5_Pos) |
((uint32_t)buff[4] << CAN_TDH0R_DATA4_Pos));
WRITE_REG(CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TDLR,
((uint32_t)buff[3] << CAN_TDL0R_DATA3_Pos) |
((uint32_t)buff[2] << CAN_TDL0R_DATA2_Pos) |
((uint32_t)buff[1] << CAN_TDL0R_DATA1_Pos) |
((uint32_t)buff[0] << CAN_TDL0R_DATA0_Pos));
//(2.4)发送Can数据报
SET_BIT(CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TIR, CAN_TI0R_TXRQ); //2024.6 设置发送请求位TXRQ,请求发送邮箱中的数据帧
return 0;
}
else
{
return 1;
}
}
//=====================================================================
//函数名称:CAN_HWInit
//函数返回:0=正常,1=错误
//参数说明:CANChannel:硬件引脚组号,共有3组,分别为PTA11&PTA12(CAN_CHANNEL0),PTB8&PTB9(CAN_CHANNEL1),PTD0&PTD1(2)
//功能概要:CAN模块引脚初始化
//=====================================================================
uint8_t CAN_HWInit(uint8_t CANChannel)
{
if(CANChannel < 0 || CANChannel > 2)
{
return 1;
}
if(CANChannel == 0)
{
RCC->APB1ENR1 |= RCC_APB1ENR1_CAN1EN; //2024.6 设置RCC->APB1ENR1寄存器的CAN1EN位来使能CAN1的时钟
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
GPIOA->MODER &= ~(GPIO_MODER_MODE11|GPIO_MODER_MODE12);
GPIOA->MODER |= (GPIO_MODER_MODE11_1|GPIO_MODER_MODE12_1); //2024.6 配置GPIOA的引脚模式,将PTA11和PTA12配置为复用功能(即CAN模式)
GPIOA->AFR[1] &= ~(GPIO_AFRH_AFSEL11|GPIO_AFRH_AFSEL12);
GPIOA->AFR[1] |= (GPIO_AFRH_AFSEL11_0|GPIO_AFRH_AFSEL11_3)|(GPIO_AFRH_AFSEL12_0|GPIO_AFRH_AFSEL12_3); //2024.6 配置GPIOA的复用功能寄存器,选择PTA11和PTA12的复用功能为CAN1
}
else if(CANChannel == 1)
{
RCC->APB1ENR1 |= RCC_APB1ENR1_CAN1EN;
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
GPIOB->MODER &= ~(GPIO_MODER_MODE8|GPIO_MODER_MODE9);
GPIOB->MODER |= (GPIO_MODER_MODE8_1|GPIO_MODER_MODE9_1);
GPIOB->AFR[1] &= ~(GPIO_AFRH_AFSEL8|GPIO_AFRH_AFSEL9);
GPIOB->AFR[1] |= ((GPIO_AFRH_AFSEL8_0|GPIO_AFRH_AFSEL8_3)|
(GPIO_AFRH_AFSEL9_0|GPIO_AFRH_AFSEL9_3));
}
else
{
RCC->APB1ENR1 |= RCC_APB1ENR1_CAN1EN;
RCC->AHB2ENR |= RCC_AHB2ENR_GPIODEN;
GPIOD->MODER &= ~(GPIO_MODER_MODE0|GPIO_MODER_MODE1);
GPIOD->MODER |= (GPIO_MODER_MODE0_1|GPIO_MODER_MODE1_1);
GPIOD->AFR[0] &= ~(GPIO_AFRL_AFSEL0|GPIO_AFRL_AFSEL1);
GPIOD->AFR[0] |= ((GPIO_AFRL_AFSEL0_0 | GPIO_AFRL_AFSEL0_3)|
(GPIO_AFRL_AFSEL1_0 | GPIO_AFRL_AFSEL1_3));
}
return 0;
}
//=====================================================================
//函数名称:CAN_SWInit_Entry
//函数返回:0=正常,1=错误
//参数说明:canNo:模块基地址号,本芯片只有CAN_1,
//功能概要:进入初始化模式
//=====================================================================
uint8_t CAN_SWInit_Entry(uint8_t canNo)
{
int i;
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_SLEEP); //2024.6 清除CAN_MCR_SLEEP位来退出睡眠模式,使CAN模块从睡眠状态恢复为工作状态
i = 0;
while ((CAN_ARR[canNo-1]->MSR & CAN_MSR_SLAK) != 0U) //2024.6 循环中等待CAN_MSR_SLAK标志位被清除
{
if(i++ > 0x30000)
{
return 1;
}
}
SET_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_INRQ); //2024.6 设置CAN_MCR_INRQ位来使能初始化请求,将CAN控制器置于初始化模式
i = 0;
while ((CAN_ARR[canNo-1]->MSR & CAN_MSR_INAK) == 0U)
{
if(i++ > 0x30000)
{
return 1;
}
}
return 0;
}
//=====================================================================
//函数名称:CAN_SWInit_CTLMode
//函数返回:无
//参数说明:canNo:模块基地址号,本芯片只有CAN_1,
//功能概要:CAN总线模式设置
//=====================================================================
void CAN_SWInit_CTLMode(uint8_t canNo)
{
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_TTCM); //2024.6 清除CAN_MCR_TTCM位来禁用时间触发通信模式
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_ABOM); //2024.6 清除CAN_MCR_ABOM位来禁用自动总线关闭功能
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_AWUM); //2024.6 清除CAN_MCR_AWUM位来禁用自动唤醒模式
SET_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_NART); //2024.6 设置CAN_MCR_NART位来禁用自动重传功能
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_RFLM); //2024.6 清除CAN_MCR_RFLM位来禁用接收FIFO锁定模式
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_TXFP); //2024.6 清除CAN_MCR_TXFP位来禁用发送优先级顺序
}
//函数名称:CAN_SWInit_CTLMode
//函数返回:无
//参数说明:canNo:模块基地址号,本芯片只有CAN_1,
// CANMode:CAN总线工作模式,分别为正常模式(CAN_MODE_NORMAL)、回环模式(CAN_MODE_LOOPBACK)、
// 静默模式(CAN_MODE_SILENT)以及回环与静默组合模式(CAN_MODE_SILENT_LOOPBACK)
//功能概要:CAN总线位时序配置
void CAN_SWInit_BT(uint8_t canNo, uint32_t CANMode, uint32_t Prescaler)
{
CAN_ARR[canNo-1]->BTR |= ((uint32_t)(Prescaler-1)|CAN_SJW_1TQ|CAN_BTR_TS1_1|CAN_BTR_TS1_0|CAN_BTR_TS2_2|CANMode); //2024.6
}
//=====================================================================
//函数名称:CAN_SWInit_Quit
//函数返回:0=正常,1=错误
//参数说明:canNo:模块基地址号
//功能概要:退出初始化模式,进入正常模式
//=====================================================================
uint8_t CAN_SWInit_Quit(uint8_t canNo)
{
int i;
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_INRQ); //2024.6 将CAN_MCR_INRQ清除,让CAN退出初始化模式
i = 0;
while ((CAN_ARR[canNo-1]->MSR & CAN_MSR_INAK) != 0U) //2024.6 等待CAN控制器确认已经成功切换到正常模式
{
if (i++ > 0x30000)
{
return 1;
}
}
return 0;
}
//=====================================================================
//函数名称: CANFilterConfig
//函数返回:0=正常,1=错误
//参数说明: canNo:模块基地址号,
// canID:自身CAN节点的唯一标识,例如按照CANopen协议给出
// Can_Rx_FifoNo:中断使用的邮箱号,
// IsActivate:是否激活过滤器
// CANFilterBank:CAN总线过滤器组选择,共有28个,(CANFilterBank0~CANFilterBank27)
// CANFiltermode:CAN总线过滤器模式,分别为掩码模式(CAN_FILTERMODE_IDMASK)和列表模式(CAN_FILTERMODE_IDLIST)
// CAN_Filterscale:CAN总线过滤器位数,分别为32位(CAN_FILTERSCALE_32BIT)和16位(CAN_FILTERSCALE_16BIT)
//功能概要:CAN接收中断开启
//=====================================================================
uint8_t CANFilterConfig(uint8_t canNo, uint32_t CanID, uint32_t FilterBank, uint32_t Can_Rx_FifoNo, uint8_t IsActivate, uint32_t FilterMode, uint32_t FilterScale)
{
uint32_t FilterIdHigh, FilterIdLow, FilterMaskIdHigh, FilterMaskIdLow, filternbrbitpos;
if(CanID <= 0x7FFU) CanID = CanID << CAN_TI0R_STID_Pos; //2024.6 将CanID左移CAN_TI0R_STID_Pos位,用于处理标准帧的CanID
FilterIdHigh = (CanID >> 16) & 0xFFFF; //2024.6 提取CanID的高16位
FilterIdLow = (CanID & 0xFFFF); //2024.6 提取CanID的低16位
FilterMaskIdHigh = 0xFFE0; //2024.6 设置过滤器的高16位屏蔽码
FilterMaskIdLow = 0x0000; //2024.6 设置过滤器的低16位屏蔽码
filternbrbitpos = (uint32_t)1 << (FilterBank & 0x1FU); //2024.6 根据过滤器组号计算位偏移量
//设置过滤器初始化模式 (FINIT=1),在此模式下可以进行过滤器初始化
SET_BIT(CAN_ARR[canNo-1]->FMR, CAN_FMR_FINIT); //2024.6 设置指定过滤器组的激活位
CLEAR_BIT(CAN_ARR[canNo-1]->FA1R, filternbrbitpos); //2024.6 清除指定过滤器组的激活位
if (FilterScale == CAN_FILTERSCALE_16BIT)
{
CLEAR_BIT(CAN_ARR[canNo-1]->FS1R, filternbrbitpos);
CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR1 =
((0x0000FFFFU & (uint32_t)FilterMaskIdLow) << 16U) |
(0x0000FFFFU & (uint32_t)FilterIdLow);
CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR2 =
((0x0000FFFFU & (uint32_t)FilterMaskIdHigh) << 16U) |
(0x0000FFFFU & (uint32_t)FilterIdHigh);
}
if (FilterScale == CAN_FILTERSCALE_32BIT) //2024.6 过滤器的位数为32位
{
SET_BIT(CAN_ARR[canNo-1]->FS1R, filternbrbitpos); //2024.6 配置为32位过滤器
CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR1 = //2024.6 配置32位过滤器的标识码
((0x0000FFFFU & (uint32_t)FilterIdHigh) << 16U) |
(0x0000FFFFU & (uint32_t)FilterIdLow);
CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR2 =
((0x0000FFFFU & (uint32_t)FilterMaskIdHigh) << 16U) |
(0x0000FFFFU & (uint32_t)FilterMaskIdLow);
}
if (FilterMode == CAN_FILTERMODE_IDMASK) //2024.6 根据过滤器模式设置
{
CLEAR_BIT(CAN_ARR[canNo-1]->FM1R, filternbrbitpos); //2024.6 设置为掩码模式
}
else
{
SET_BIT(CAN_ARR[canNo-1]->FM1R, filternbrbitpos);
}
if (Can_Rx_FifoNo == CAN_FILTER_FIFO0) //2024.6 根据中断邮箱号设置
{
CLEAR_BIT(CAN_ARR[canNo-1]->FFA1R, filternbrbitpos); //2024.6 配置为接收邮箱0
}
else
{
SET_BIT(CAN_ARR[canNo-1]->FFA1R, filternbrbitpos);
}
if (IsActivate == 1) //2024.6 根据激活标志位设置过滤器激活
{
SET_BIT(CAN_ARR[canNo-1]->FA1R, filternbrbitpos); //2024.6 激活指定的过滤器组
}
//退出过滤器初始化模式 (FINIT=0)
CLEAR_BIT(CAN_ARR[canNo-1]->FMR, CAN_FMR_FINIT); //2024.6 置反标志位并退出初始化模式
return 0;
}