/****************************************************************************
*
*文件名：DSP28_ECan.c
*
*功  能：2812内部ECan模块的初始化文件
*
*作  者: likyo from hellodsp
*
****************************************************************************/

#include "DSP28_Device.h"

/****************************************************************************
*
*名    称：InitECan()
*
*功    能：初始化ECan模块
*
*入口参数：无
*
*出口参数：无
*
****************************************************************************/
//
void InitECan(void)
{   
	struct ECAN_REGS ECanaShadow;
	EALLOW;
	
	//配置GPIO引脚工作在eCAN功能
	GpioMuxRegs.GPFMUX.bit.CANRXA_GPIOF7=1;
	GpioMuxRegs.GPFMUX.bit.CANTXA_GPIOF6=1;
	
	//配置eCAN的RX和TX分别为eCAN的接收和发送引脚
	ECanaShadow.CANTIOC.all = ECanaRegs.CANTIOC.all;
	ECanaShadow.CANTIOC.bit.TXFUNC = 1;
	ECanaRegs.CANTIOC.all = ECanaShadow.CANTIOC.all;
	
	ECanaShadow.CANRIOC.all = ECanaRegs.CANRIOC.all;
	ECanaShadow.CANRIOC.bit.RXFUNC = 1;
	ECanaRegs.CANRIOC.all = ECanaShadow.CANRIOC.all;
	EDIS;

    //不使能邮箱，在写MSGID之前要完成该操作
	ECanaRegs.CANME.all = 0;
	//主控寄存器的配置
    ECanaRegs.CANMC.all=0;//主控寄存器各位清〇

    EALLOW;
	ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
	//工作在正常模式
	ECanaShadow.CANMC.bit.STM = 0;				
	//工作在eCAN模式
	ECanaShadow.CANMC.bit.SCM = 1;
	//存储方向
	ECanaShadow.CANMC.bit.DBO = 0;				
	ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
	EDIS;
    //请求向邮箱里写数据
	EALLOW;
    ECanaShadow.CANMC.all=ECanaRegs.CANMC.all;
	ECanaShadow.CANMC.bit.CDR=1;
	ECanaShadow.CANMC.bit.MBNR=0;//确定0号邮箱；以对其写数据
	ECanaRegs.CANMC.all=ECanaShadow.CANMC.all;
	EDIS;

    //初始化邮箱0控制区域为0，控制区域所有的位都初始化为0
	ECanaMboxes.MBOX0.MCF.all = 0x00000000;
    

	//清除所有的TA位
	ECanaRegs.CANTA.all = 0xFFFFFFFF;
	
	//清除所有的RMP位
	ECanaRegs.CANRMP.all = 0xFFFFFFFF;


    
	//配置时钟参数
    EALLOW;
	ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
	ECanaShadow.CANMC.bit.CCR = 1;
	ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
	EDIS;
    //CPU请求向CANBTC和CANGAM写配置信息，该位置1后必须等到CANED.CCE为1，才能
	//对CANBTC进行操作。
	do
	{
	  ECanaShadow.CANES.all = ECanaRegs.CANES.all;
	}
	while(ECanaShadow.CANES.bit.CCE != 1);
	
	EALLOW;
	//(BRPREG+1)=100 ，CAN时钟为150MHz
	ECanaShadow.CANBTC.bit.BRP = 99;
	//CAN通信的波特率为100kbps
	ECanaShadow.CANBTC.bit.TSEG2 = 2;
	ECanaShadow.CANBTC.bit.TSEG1 = 10;
	ECanaRegs.CANBTC.all=ECanaShadow.CANBTC.all;
	
	//CPU请求正常操作
	ECanaShadow.CANMC.all=ECanaRegs.CANMC.all;
	ECanaShadow.CANMC.bit.CCR = 0;
	ECanaRegs.CANMC.all=ECanaShadow.CANMC.all;
	EDIS;
	do
	{
	  ECanaShadow.CANES.all = ECanaRegs.CANES.all;
	}
	while(ECanaShadow.CANES.bit.CCE != 0);

	//设置邮箱0为发送邮箱
	ECanaShadow.CANMD.all = ECanaRegs.CANMD.all;
    ECanaShadow.CANMD.bit.MD0 =0;
	ECanaRegs.CANMD.all = ECanaShadow.CANMD.all;
	//全局接受屏蔽，标示符位必须一一匹配
	ECanaRegs.CANGAM.all=0;
	//设置接收邮箱的ID，扩展帧，IDE，	
	ECanaMboxes.MBOX0.MID.all =  0xA0C80000;
	//数据长度 8个BYTE
	ECanaMboxes.MBOX0.MCF.bit.DLC = 8;
	//设置发送优先级 
	ECanaMboxes.MBOX0.MCF.bit.TPL = 0;
	//有远方应答帧被请求*/
	ECanaMboxes.MBOX0.MCF.bit.RTR = 1;
	//向邮箱RAM区写数据
	ECanaMboxes.MBOX0.MDRL.all = 0x00000001;
	ECanaMboxes.MBOX0.MDRH.all = 0x10000000;
	//请求正常工作
	EALLOW;
	ECanaShadow.CANMC.all=ECanaRegs.CANMC.all;
	ECanaShadow.CANMC.bit.CDR=0;
	ECanaRegs.CANMC.all=ECanaShadow.CANMC.all;
    EDIS;
    
    //邮箱使能Mailbox0	
	ECanaShadow.CANME.all = ECanaRegs.CANME.all;
	ECanaShadow.CANME.bit.ME0 =1;
	ECanaRegs.CANME.all = ECanaShadow.CANME.all;
	
	
		
}	

//===========================================================================
// No more.
//===========================================================================
