DSP

DSP28335 CAN模块使用

2019-07-13 10:36发布

相关函数  InitECanbGpio(); //初始化CANB  InitECanb(); void InitECanb(void) // Initialize eCAN-B module
{
   struct ECAN_REGS ECanbShadow;

    EALLOW; // EALLOW enables access to protected bits
/* Configure eCAN RX and TX pins for CAN operation using eCAN regs*/
    ECanbShadow.CANTIOC.all = ECanbRegs.CANTIOC.all;
    ECanbShadow.CANTIOC.bit.TXFUNC = 1;
    ECanbRegs.CANTIOC.all = ECanbShadow.CANTIOC.all;

    ECanbShadow.CANRIOC.all = ECanbRegs.CANRIOC.all;
    ECanbShadow.CANRIOC.bit.RXFUNC = 1;
    ECanbRegs.CANRIOC.all = ECanbShadow.CANRIOC.all;


/* Initialize all bits of 'Master Control Field' to zero */
// Some bits of MSGCTRL register come up in an unknown state. For proper operation,
// all bits (including reserved bits) of MSGCTRL must be initialized to zero
    //SCC模式只有0~15号邮箱
    ECanbMboxes.MBOX0.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX1.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX2.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX3.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX4.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX5.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX6.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX7.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX8.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX9.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX10.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX11.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX12.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX13.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX14.MSGCTRL.all = 0x00000000;
    ECanbMboxes.MBOX15.MSGCTRL.all = 0x00000000;


// TAn, RMPn, GIFn bits are all zero upon reset and are cleared again
// as a matter of precaution.
ECanbRegs.CANTA.all = 0xFFFFFFFF; /* Clear all TAn bits */
ECanbRegs.CANRMP.all = 0xFFFFFFFF; /* Clear all RMPn bits */
ECanbRegs.CANGIF0.all = 0xFFFFFFFF; /* Clear all interrupt flag bits */
ECanbRegs.CANGIF1.all = 0xFFFFFFFF;

/* Configure bit timing parameters for eCANB*/
ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.CCR = 1 ;            // Set CCR = 1
        ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
        ECanbShadow.CANES.all = ECanbRegs.CANES.all;

    do
{
   ECanbShadow.CANES.all = ECanbRegs.CANES.all;
} while(ECanbShadow.CANES.bit.CCE != 1 ); // Wait for CCE bit to be  cleared..


    ECanbShadow.CANBTC.all = 0;


    #if (CPU_FRQ_150MHZ)                       // CPU_FRQ_150MHz is defined in DSP2833x_Examples.h
/* The following block for all 150 MHz SYSCLKOUT (75 MHz CAN clock) - default. Bit rate = 500k Mbps
  See Note at end of file */
ECanbShadow.CANBTC.bit.BRPREG = 9;
ECanbShadow.CANBTC.bit.TSEG2REG = 2;
ECanbShadow.CANBTC.bit.TSEG1REG = 10;
#endif
#if (CPU_FRQ_100MHZ)                       // CPU_FRQ_100MHz is defined in DSP2833x_Examples.h
/* The following block is only for 100 MHz SYSCLKOUT (50 MHz CAN clock). Bit rate = 1 Mbps
  See Note at end of file */
   ECanbShadow.CANBTC.bit.BRPREG = 4;
ECanbShadow.CANBTC.bit.TSEG2REG = 1;
ECanbShadow.CANBTC.bit.TSEG1REG = 6;
#endif

    //ECanbShadow.CANBTC.all = ECanbRegs.CANBTC.all;
    ECanbShadow.CANBTC.bit.SAM = 1;
    ECanbRegs.CANBTC.all = ECanbShadow.CANBTC.all;


    ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.CCR = 0 ;                         // Set CCR = 0
    ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;


    ECanbShadow.CANES.all = ECanbRegs.CANES.all;


    do
    {
        ECanbShadow.CANES.all = ECanbRegs.CANES.all;
    } while(ECanbShadow.CANES.bit.CCE != 0 ); // Wait for CCE bit to be  cleared..

/* Disable all Mailboxes  */
  ECanbRegs.CANME.all = 0; // Required before writing the MSGIDs


    EDIS;




    /* Configure Mailbox under test as a Transmit mailbox */
    ECanbShadow.CANMD.all = ECanbRegs.CANMD.all;
    ECanbShadow.CANMD.all = 0xff00;                //邮箱0~7为发送,8~15为接收
    ECanbRegs.CANMD.all = ECanbShadow.CANMD.all;


    ECanbShadow.CANGAM.all = ECanbRegs.CANGAM.all;
    ECanbShadow.CANGAM.bit.AMI = 1;              //标准帧和扩展帧都接受
    ECanbShadow.CANGAM.bit.GAM2816 |= 0x1fff;      //LAM(0)用于邮箱0~2 ,LAM(3)用于邮箱3~5,对于邮箱6~15,用全局接收屏蔽
    ECanbRegs.CANGAM.all = ECanbShadow.CANGAM.all;

    //接收中断设置
    EALLOW;
    ECanbShadow.CANMIM.all = ECanbRegs.CANMIM.all;
    ECanbShadow.CANMIM.all = 0xff00 ;              // 开启8~15号邮箱中断
    ECanbRegs.CANMIM.all = ECanbShadow.CANMIM.all;
    ECanbShadow.CANMIL.all = ECanbRegs.CANMIL.all;
    ECanbShadow.CANMIL.all = 0xff00 ;              // 8~15号邮箱中断在中断线1上产生
    ECanbRegs.CANMIL.all = ECanbShadow.CANMIL.all;
    ECanbShadow.CANGIM.all = ECanbRegs.CANGIM.all;
    ECanbShadow.CANGIM.all = 0x02 ;              //  中断线1使能
    ECanbRegs.CANGIM.all = ECanbShadow.CANGIM.all;
    EDIS;

    //ECanbMboxes.MBOX15.MSGID.bit.AME=1;  //15号邮箱接收任何ID的报文
    ECanbMboxes.MBOX5.MSGID.all = 0; // stand Identifier
    /* Enable Mailbox under test */
    ECanbShadow.CANME.all = ECanbRegs.CANME.all;
    ECanbShadow.CANME.all = 0xffff;
    ECanbRegs.CANME.all = ECanbShadow.CANME.all;
}




//注意邮箱编号需要根据实际修改
//CANB 发送函数 Can_Id为11位标准帧ID, length为CAN数据长度(单位是字节), Data_L为低四位字节,Data_H为高四位字节
void CanBSend(Uint32 Can_Id, u8 length, Uint32 Data_L, Uint32 Data_H)
{
struct ECAN_REGS ECanbShadow;

//修改ID前要禁止邮箱才能往寄存器里面写值
ECanbShadow.CANME.all = ECanbRegs.CANME.all;
ECanbShadow.CANME.all = 0;
ECanbRegs.CANME.all = ECanbShadow.CANME.all;
     /* Write to the MSGID field  */
ECanbMboxes.MBOX5.MSGID.all = ( (Can_Id|0x10000000)<<18); // stand Identifier
//使能邮箱
ECanbShadow.CANME.all = ECanbRegs.CANME.all;
ECanbShadow.CANME.all = 0xffff;
ECanbRegs.CANME.all = ECanbShadow.CANME.all;

/* Write to DLC field in Master Control reg */
ECanbMboxes.MBOX5.MSGCTRL.bit.DLC = length; //8;
/* Write to the mailbox RAM field */
ECanbMboxes.MBOX5.MDL.all =Data_L;  // 0x54555555; //高位4字节
ECanbMboxes.MBOX5.MDH.all =Data_H ;  // 0x55578555; //低位4字节


//struct ECAN_REGS ECanbShadow;
ECanbShadow.CANTRS.all = 0;
    ECanbShadow.CANTRS.bit.TRS5 = 1;             // Set TRS for mailbox under test
ECanbRegs.CANTRS.all = ECanbShadow.CANTRS.all;
do
{
  ECanbShadow.CANTA.all = ECanbRegs.CANTA.all;
    } while(ECanbShadow.CANTA.bit.TA5 == 0 );   // Wait for TA5 bit to be set..
    ECanbShadow.CANTA.all = 0;
    ECanbShadow.CANTA.bit.TA5 = 1;             // Clear TA5
    ECanbRegs.CANTA.all = ECanbShadow.CANTA.all;
}




Uint32  Mbox_DL = 0;
Uint32  Mbox_DH = 0;
Uint32  Mbox_MSGID = 0;
Uint32 ttt=0;
interrupt void CANB_Recv_ISR(void)     // CANB接收中断
{
    struct ECAN_REGS ECanbShadow;
    volatile struct MBOX *Mailbox;
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;


ttt=(ECanbRegs.CANGIF1.all)&0x0f;
Mailbox = &ECanbMboxes.MBOX0 + ttt;    //邮箱0地址+邮箱中断向量得到偏移地址
Mbox_DL = Mailbox->MDL.all;           // = 0x9555AAAn (n is the MBX number)
Mbox_DH = Mailbox->MDH.all;           // = 0x89ABCDEF (a constant)
Mbox_MSGID = ((Mailbox->MSGID.all)&0X1FFC0000)>>18;      // = 0x9555AAAn (n is the MBX number)


ECanbShadow.CANGIF1.all = ECanbRegs.CANGIF1.all;
ECanbShadow.CANGIF1.bit.GMIF1 = 1;             //清除中断标志位
    ECanbRegs.CANGIF1.all = ECanbShadow.CANGIF1.all;
ECanbRegs.CANRMP.all = 0XFF00;  //清中断接收标志
}