/* * ecan.c * * Created on: 2024年8月12日 * Author: Administrator */ #include "ecan.h" extern volatile Uint32 RX_L; extern volatile Uint32 RX_H; void InitECANB(void) { config_eCANB(); Init_mail_Tx_Rx(); } void Init_GPIOCANb(void) { EALLOW; GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0;// Enable pull-up for GPIO17 (CANRXA) GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0; // Enable pull-up for GPIO16 (CANTXA) GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 2;//复用位CANB的RX引脚 GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 2;//复用位CANB的TX引脚 EDIS; } void config_eCANB(void) { EALLOW; SysCtrlRegs.PCLKCR0.bit.ECANBENCLK=1; // eCAN-B EDIS; struct ECAN_REGS ECanbShadow; EALLOW; // EALLOW enables access to protected bits //配置eCAN的RX和TX分别为eCAN的接收和发送引脚 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; ECanbShadow.CANMC.all = ECanbRegs.CANMC.all; ECanbShadow.CANMC.bit.STM = 0; //工作在正常模式 ECanbShadow.CANMC.bit.SCB = 1; //工作在ecan模式 ECanbRegs.CANMC.all = ECanbShadow.CANMC.all; //初始化所有主设备控制区域为0,MCF所有的位都初始化为0 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; ECanbMboxes.MBOX16.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX17.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX18.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX19.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX20.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX21.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX22.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX23.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX24.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX25.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX26.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX27.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX28.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX29.MSGCTRL.all = 0x00000000; ECanbMboxes.MBOX30.MSGCTRL.all = 0x00000000; // /* Clear all TAn bits */ // ECanbRegs.CANTA.all = 0xFFFFFFFF; // // /* Clear all RMPn bits */ // ECanbRegs.CANRMP.all = 0xFFFFFFFF; // // /* Clear all interrupt flag bits */ // ECanbRegs.CANGIF0.all = 0xFFFFFFFF; // ECanbRegs.CANGIF1.all = 0xFFFFFFFF; // ECanbShadow.CANTA.all = ECanbRegs.CANTA.all; ECanbShadow.CANTA.all = 0xFFFFFFFF; ECanbRegs.CANTA.all = ECanbShadow.CANTA.all; ECanbShadow.CANRMP.all = ECanbRegs.CANRMP.all; ECanbShadow.CANRMP.all = 0xFFFFFFFF; ECanbRegs.CANRMP.all = ECanbShadow.CANRMP.all; ECanbShadow.CANGIF0.all = ECanbRegs.CANGIF0.all; ECanbShadow.CANGIF0.all = 0xFFFFFFFF; ECanbRegs.CANGIF0.all = ECanbShadow.CANGIF0.all; ECanbShadow.CANGIF1.all = ECanbRegs.CANGIF1.all; ECanbShadow.CANGIF1.all = 0xFFFFFFFF; ECanbRegs.CANGIF1.all = ECanbShadow.CANGIF1.all; //ccr置位等待cce //CPU请求向CANBTC和CANGAM写配置信息,该位置1后必须等到CANED.CCE为1,才能 //对CANBTC进行操作。 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; // CPU_FRQ_150MHz is defined in DSP2833x_Examples.h /* The following block for all 150 MHz SYSCLKOUT (75 MHz CAN clock) - default. Bit rate = 1 Mbps See Note at end of file */ ECanbShadow.CANBTC.bit.BRPREG = 19; ECanbShadow.CANBTC.bit.TSEG2REG = 2; ECanbShadow.CANBTC.bit.TSEG1REG = 10; 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 ); ECanbRegs.CANME.all = 0;//禁用所有邮箱 EDIS; } void Init_mail_Tx_Rx(void) { // Uint32 Rec_l = 0x00000000; // Uint32 Rec_h = 0x00000000; struct ECAN_REGS ECanbShadow; ECanbMboxes.MBOX16.MSGID.all = 0x80C20000;//设置接收邮箱的ID,扩展帧 ECanbMboxes.MBOX25.MSGID.all = 0x95555555; // Extended Identifier /*邮箱25为TX,为RX*/ ECanbShadow.CANMD.all = ECanbRegs.CANMD.all; ECanbShadow.CANMD.bit.MD25 = 0; ECanbShadow.CANMD.bit.MD16 =1; ECanbRegs.CANMD.all = ECanbShadow.CANMD.all; //数据长度为8字节 ECanbMboxes.MBOX25.MSGCTRL.bit.DLC = 8; ECanbMboxes.MBOX16.MSGCTRL.bit.DLC = 8; //没有远方应答帧被请求*/ ECanbMboxes.MBOX16.MSGCTRL.bit.RTR = 0; ECanbMboxes.MBOX25.MSGCTRL.bit.RTR = 0; //使能邮箱 ECanbShadow.CANME.all = ECanbRegs.CANME.all; ECanbShadow.CANME.bit.ME25 = 1; ECanbShadow.CANME.bit.ME16 =1; ECanbRegs.CANME.all = ECanbShadow.CANME.all; //邮箱中断使能 EALLOW; ECanbRegs.CANMIM.all = 0xFFFFFFFF; //邮箱中断将产生在ECAN0INT ECanbRegs.CANMIL.all = 0; ECanbRegs.CANGIF0.all = 0xFFFFFFFF; //ECAN0INT中断请求线被使能 ECanbRegs.CANGIM.bit.I0EN = 1; EDIS; } void Trancemit_CANb(Uint32 highdata,Uint32 lowdata,Uint32 times) { struct ECAN_REGS ECanbShadow; int i=0; ECanbMboxes.MBOX25.MDL.all = lowdata; ECanbMboxes.MBOX25.MDH.all = highdata; for(i=0; i < times; i++) { ECanbShadow.CANTRS.all = 0; ECanbShadow.CANTRS.bit.TRS25 = 1; // Set TRS for mailbox under test ECanbRegs.CANTRS.all = ECanbShadow.CANTRS.all; do { ECanbShadow.CANTA.all = ECanbRegs.CANTA.all; } while(ECanbShadow.CANTA.bit.TA25 == 0 ); // Wait for TA5 bit to be set.. ECanbShadow.CANTA.all = 0; ECanbShadow.CANTA.bit.TA25 = 1; // Clear TA5 ECanbRegs.CANTA.all = ECanbShadow.CANTA.all; } } void InitEcanbRX() { EALLOW; // 写保护 PieVectTable.ECAN0INTB = &ECANB_RX; //设置ECAN0INTB中断服务程序的入口地址为ECAN0INTB_iSR EDIS; // 禁用写保护 //使能PIE中断 PieCtrlRegs.PIEIER9.bit.INTx7 = 1; //使能CPU中断 IER |= M_INT9; EINT; //开全局中断 ERTM; //开实时中断 } interrupt void ECANB_RX(void) { // volatile Uint32 Rec_l = 0x00000000; // volatile Uint32 Rec_h = 0x00000000; if(ECanbRegs.CANRMP.all != 0) { while(ECanbRegs.CANRMP.all != 0x00010000 ) ; ECanbRegs.CANRMP.all = 0x00010000; //收到的数据在接收邮箱Mbox16 RX_L = ECanbMboxes.MBOX16.MDL.all; RX_H = ECanbMboxes.MBOX16.MDH.all; PieCtrlRegs.PIEACK.bit.ACK9= 1; EINT; } } //main.c /** * main.c */ #include "DSP28x_Project.h" #include "ecan.h" volatile Uint32 RX_L = 0x00000000; volatile Uint32 RX_H = 0x00000000; int main(void) { InitSysCtrl(); Init_GPIOCANb(); DINT; IER = 0x0000; IFR = 0x0000; InitPieCtrl(); InitPieVectTable(); EALLOW; // 写保护 PieVectTable.ECAN0INTB = &ECANB_RX; //设置ECAN0INTB中断服务程序的入口地址为ECAN0INTB_iSR EDIS; // 禁用写保护 InitECANB(); Init_mail_Tx_Rx(); PieCtrlRegs.PIEIER9.bit.INTx7 = 1; //使能CPU中断 IER |= M_INT9; EINT; //开全局中断 ERTM; //开实时中断 InitEcanbRX(); Trancemit_CANb(0x11111111,0x22222222,3); while(1) {} }
这是变量类型