赞
踩
DSP/MCU的CAN接口是指芯片外设中的CAN总线控制器,其输出电平为TTL规格,如果直接将两块芯片的CANRX、CANTX引脚连接,无法实现CAN通讯。SAJ1000、TJA1050芯片是属于物理层芯片,将TTL电平信号转换为符合CAN规范的差分信号,从而实现CAN通讯。
因此,上位机与DSP进行CAN通讯时,相比于基于SCI的串口通讯,需要多加一个收发芯片模块。将DSP/MCU的CANRX引脚接入模块的RX,CANTX接入模块的TX,从而将TTL电平信号转为CAN通讯的差分信号CANH和CANL。
一般的CAN通讯接口对外主要是CANH和CANL两个信号。
当两个具有CAN通讯接口的设备相互通讯时,两边的CANH连接,CANL也连接。高频信号传输时,信号波长相对传输线较短,信号在传输线终端会形成反射波,干扰原信号,所以需要在传输线末端加终端电阻,使信号到达传输线末端后不反射。对于低频信号则不用 CAN总线两端必须连接终端电阻才可以正常工作,终端电阻应该与通讯电缆的阻抗相同,典型值为120欧姆。其作用是匹配总线阻抗,提高数据通信的抗干扰性及可靠行。
当需要利用PC对CAN设备进行调试时,由于PC机通常没有CAN接口,也没有CAN控制器,因此需要CAN转USB模块、CAN转以太网模块等,本质上,该模块内部有一块DSP/MCU,将能够将CAN差分通讯转为PC机能够直接进行的通讯方式,如串口通讯、以太网通讯等。
由于转换模块所用的DSP/MCU中对CAN通讯的处理程序不同,因此,CAN转USB、CAN转以太网模块通常没有统一的PC上位机,针对CAN转化模块,PC上需要根据模块制造商所提供的API,设计相应合适的上位机。
目前选择的CAN转USB模块,实现了将CAN通讯转为串口通讯的功能,直接使用普通的串口调试助手即可调试CAN通讯,后期可以将原有的串口通讯、485通讯协议移植到CAN通讯上。
CAN通讯时,有个概念叫邮箱,一共分为了收发邮箱两类,通过配置确定邮箱为发送还是接收邮箱,每个邮箱也会有分配ID和滤波器,用于识别帧数据是否是发送给了对应的设备。当采用CAN转USB模块时,可以关闭滤波器,实现任意ID的接收。
main函数部分,主要是初始化程序是这样的,这些基本都是调用或者遵照TI官方示例写的
/* Create a shadow register structure for the CAN control registers. This is needed, since, only 32-bit access is allowed to these registers. 16-bit access to these registers could potentially corrupt the register contents. This is especially true while writing to a bit (or group of bits) among bits 16 - 31 */ struct ECAN_REGS ECanaShadow; // Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks // This example function is found in the DSP2833x_SysCtrl.c file. InitSysCtrl(); // Just initalize eCAN pins for this example // This function is in DSP2833x_ECan.c // Step 3. Clear all interrupts and initialize PIE vector table: // Disable CPU interrupts DINT; // Initialize the PIE control registers to their default state. // The default state is all PIE interrupts disabled and flags // are cleared. // This function is found in the DSP2833x_PieCtrl.c file. InitPieCtrl(); // Disable CPU interrupts and clear all CPU interrupt flags: IER = 0x0000; IFR = 0x0000; // Initialize the PIE vector table with pointers to the shell Interrupt // Service Routines (ISR). // This will populate the entire table, even if the interrupt // is not used in this example. This is useful for debug purposes. // The shell ISR routines are found in DSP2833x_DefaultIsr.c. // This function is found in DSP2833x_PieVect.c. InitPieVectTable(); // Interrupts that are used in this example are re-mapped to // ISR functions found within this file. InitECanGpio(); // Step 4. Initialize all the Device Peripherals: // This function is found in DSP2833x_InitPeripherals.c // InitPeripherals(); // Not required for this example // In this case just initalize eCAN-A and eCAN-B // This function is in DSP2833x_ECan.c InitECan(); ErrorCount = 0; PassCount = 0; /* Write to the MSGID field */ ECanaMboxes.MBOX16.MSGID.all = 0x95555554; // Extended Identifier ECanaMboxes.MBOX25.MSGID.all = 0x95555555; // Extended Identifier /* Configure Mailbox under test as a Re/Transmit mailbox */ EALLOW; ECanaShadow.CANMD.all = ECanaRegs.CANMD.all; ECanaShadow.CANMD.bit.MD16 = 1; //rx ECanaShadow.CANMD.bit.MD25 = 0;//tx ECanaRegs.CANMD.all = ECanaShadow.CANMD.all; //without considering id and the data can be received ECanaMboxes.MBOX16.MSGID.bit.AME = 1; ECanaLAMRegs.LAM16.all = 0xFFFFFFFF; /* Enable Mailbox under test */ ECanaShadow.CANME.all = ECanaRegs.CANME.all; ECanaShadow.CANME.bit.ME16 = 1; ECanaShadow.CANME.bit.ME25 = 1; ECanaRegs.CANME.all = ECanaShadow.CANME.all; /* Write to DLC field in Master Control reg */ //ECanaMboxes.MBOX16.MSGCTRL.bit.DLC = 8; ECanaMboxes.MBOX25.MSGCTRL.bit.DLC = 8; ECanaRegs.CANRMP.all = 0xFFFFFC00; /* Write to the mailbox RAM field */ ECanaMboxes.MBOX25.MDL.all = 0x55555555; ECanaMboxes.MBOX25.MDH.all = 0xAAAAAAAA; EDIS; EALLOW; ECanaShadow.CANMIM.all=ECanaRegs.CANMIM.all; ECanaShadow.CANMIM.bit.MIM16=1; ECanaRegs.CANMIM.all=ECanaShadow.CANMIM.all; ECanaShadow.CANMIL.all = ECanaRegs.CANMIL.all; ECanaShadow.CANMIL.all = 0; // 1-32号邮箱中断在中断线0上产生 ECanaRegs.CANMIL.all = ECanaShadow.CANMIL.all; ECanaShadow.CANGIM.all = ECanaRegs.CANGIM.all; ECanaShadow.CANGIM.bit.I0EN = 1 ; // 中断线0使能 ECanaRegs.CANGIM.all = ECanaShadow.CANGIM.all; ECanaShadow.CANMC.all = ECanaRegs.CANMC.all; ECanaShadow.CANMC.bit.STM = 0; // 0-Normal ECanaRegs.CANMC.all = ECanaShadow.CANMC.all; EDIS; EALLOW; // This is needed to write to EALLOW protected registers PieVectTable.ECAN0INTA = &Ecan0ISR;//R-CAN1 接收后中断函数 EDIS; // This is needed to disable write to EALLOW protected registers IER |=M_INT9;// 开CPU中断1~9(必须开放对应的CPU级中断口) PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block PieCtrlRegs.PIEIER9.bit.INTx5=1; //R-CAN0 接收邮箱 EINT;//开总中断 ERTM;//使能实时中断(CPU级的) /* Begin transmitting */ for(i=0; i < TXCOUNT; i++) { ECanaShadow.CANTRS.all = 0; ECanaShadow.CANTRS.bit.TRS25 = 1; // Set TRS for mailbox under test ECanaRegs.CANTRS.all = ECanaShadow.CANTRS.all; do { ECanaShadow.CANTA.all = ECanaRegs.CANTA.all; } while(ECanaShadow.CANTA.bit.TA25 == 0 ); // Wait for TA5 bit to be set..//如果线没有连接,如果线连接错误 ECanaShadow.CANTA.all = 0; ECanaShadow.CANTA.bit.TA25 = 1; // Clear TA5 ECanaRegs.CANTA.all = ECanaShadow.CANTA.all; } while(1) { ECanaShadow.CANRMP.all = ECanaRegs.CANRMP.all; }
这里有少数需要注意是地方就是邮箱滤波器的关闭
//without considering id and the data can be received
ECanaMboxes.MBOX16.MSGID.bit.AME = 1;
ECanaLAMRegs.LAM16.all = 0xFFFFFFFF;
这两句话加在使能邮箱前,才能生效。
最后就是CAN对应的中断了,只需要按照邮箱结构体,依次读取邮箱的低位数据、高位数据和邮箱ID即可。
//
__interrupt void Ecan0ISR(void)//R 接收后进入的中断
{
if(ECanaRegs.CANRMP.all==0x00010000)//RX get after flag and int BOX16
{
ECanaRegs.CANRMP.all = 0x00010000;//clear GMIF16
TestMbox1 = ECanaMboxes.MBOX16.MDL.all;
TestMbox2 = ECanaMboxes.MBOX16.MDH.all;
TestMbox3 = ECanaMboxes.MBOX16.MSGID.all;//从外部接收邮箱16的ID,16为接收邮箱(CANMD)
}
PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。