#include "bsp_can.h"
stc_can_rxframe_t stcRxFrame;
stc_can_txframe_t stcTxFrame;
uint8_t u8RxFlag = FALSE;
static void Can_Gpio_Init(void)
{
stc_gpio_cfg_t stcGpioCfg;
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
///< 端口方向配置->输入
stcGpioCfg.enDir = GpioDirIn;
///< 端口驱动能力配置->高驱动能力
stcGpioCfg.enDrv = GpioDrvL;
///< 端口上下拉配置->无
stcGpioCfg.enPu = GpioPuDisable;
stcGpioCfg.enPd = GpioPdDisable;
///< 端口开漏输出配置->开漏输出关闭
stcGpioCfg.enOD = GpioOdDisable;
///< 端口输入/输出值寄存器总线控制模式配置->AHB
stcGpioCfg.enCtrlMode = GpioAHB;
Gpio_Init(EVB_CAN_RX_PORT, EVB_CAN_RX_PIN, &stcGpioCfg);
stcGpioCfg.enDir = GpioDirOut;
Gpio_Init(EVB_CAN_TX_PORT, EVB_CAN_TX_PIN, &stcGpioCfg);
Gpio_Init(EVB_CAN_STB_PORT, EVB_CAN_STB_PIN, &stcGpioCfg);
///<CAN RX\TX复用功能配置
Gpio_SetAfMode(EVB_CAN_RX_PORT, EVB_CAN_RX_PIN, GpioAf1);
Gpio_SetAfMode(EVB_CAN_TX_PORT, EVB_CAN_TX_PIN, GpioAf1);
///<STB 低-PHY有效
Gpio_ClrIO(EVB_CAN_STB_PORT, EVB_CAN_STB_PIN);
}
void BSP_Can_Init(void)
{
stc_can_init_config_t stcCanInitCfg;
stc_can_filter_t stcFilter;
Can_Gpio_Init();
Sysctrl_SetPeripheralGate(SysctrlPeripheralCan, TRUE);
//<<CAN 波特率配置
// 手册page884.exp: bt = (PRESC+1)((SEG_1+2) + (SEG_2+1))*(1/8M) = 1us (1000k)
#ifdef CAN_CONFIG_48MHZ_500K
stcCanInitCfg.stcCanBt.PRESC = 6-1;
stcCanInitCfg.stcCanBt.SEG_1 = 9-2;
stcCanInitCfg.stcCanBt.SEG_2 = 7-1;
stcCanInitCfg.stcCanBt.SJW = 3-1;
// stcCanInitCfg.stcCanBt.PRESC = 8-1;
// stcCanInitCfg.stcCanBt.SEG_1 = 7-2;
// stcCanInitCfg.stcCanBt.SEG_2 = 5-1;
// stcCanInitCfg.stcCanBt.SJW = 5-1;
#endif
stcCanInitCfg.stcWarningLimit.CanErrorWarningLimitVal = 10;
stcCanInitCfg.stcWarningLimit.CanWarningLimitVal = 16-1;
stcCanInitCfg.enCanRxBufAll = CanRxNormal;
stcCanInitCfg.enCanRxBufMode = CanRxBufNotStored;
stcCanInitCfg.enCanSTBMode = CanSTBFifoMode;
CAN_Init(&stcCanInitCfg);
//<<CAN 滤波器配置
stcFilter.enAcfFormat = CanAllFrames;
stcFilter.enFilterSel = CanFilterSel1;
stcFilter.u32CODE = 0x00000236;
stcFilter.u32MASK = 0x1FFFFFFF; /*0x1FFFFFFF is all received*/
CAN_FilterConfig(&stcFilter, TRUE);
//<<Can Irq Enable
CAN_IrqCmd(CanRxIrqEn, TRUE); /* 接收中断使能 */
CAN_IrqCmd(CanBusErrorIrqEn, TRUE); /*总线错误中断使能*/
EnableNvic(CAN_IRQn, IrqLevel0, TRUE);
}
/********************************************************************************************************
**函数信息 :void CAN_send_Frame()
**功能描述 :CAN发送一帧报文
**输入参数 :stcTxFrame
**输出参数 :None
** 备注 目前只考虑标准帧
********************************************************************************************************/
void CAN_send_Frame(stc_can_txframe_t *TxFrame, uint32_t Can_ID,uint8_t frame_len)
{
TxFrame->StdID = Can_ID;
TxFrame->Control_f.DLC = frame_len;
TxFrame->Control_f.IDE = 0; /*标准帧 or 扩展帧*/
TxFrame->Control_f.RTR = 0; /*数据帧 or 远程帧*/
TxFrame->enBufferSel = (en_can_buffer_sel_t)0U; /*使用主缓冲器*/
CAN_SetFrame(TxFrame); /*数据塞进fifo缓冲器*/
CAN_TransmitCmd(CanPTBTxCmd);
}
void Can_IRQHandler(void)
{
if(TRUE == CAN_Irq**Get(CanRxIrq**))
{
CAN_Irq**Clr(CanRxIrq**);
CAN_Receive(&stcRxFrame);
u8RxFlag = TRUE;
}
if(TRUE == CAN_Irq**Get(CanBusErrorIrq**)) /*当总线短路时,进入此中断*/
{
CAN_Irq**Clr(CanBusErrorIrq**);
// M0P_CAN->CFG_STAT = 0x00; /* 清除由硬件置位的Reset 标志位,必须在中断外部执行*/
}
}
头文件:
#ifndef __BSP_CAN_H__
#define __BSP_CAN_H__
#include "can.h"
#include "gpio.h"
#define CAN_CONFIG_48MHZ_500K
void BSP_Can_Init(void);
void CAN_send_Frame(stc_can_txframe_t *TxFrame, uint32_t Can_ID,uint8_t frame_len);
extern uint8_t u8RxFlag ;
extern stc_can_rxframe_t stcRxFrame;
extern stc_can_txframe_t stcTxFrame;
#endif
|