打印

2块DSP板子进行CAN通信

[复制链接]
2645|11
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
混子黄|  楼主 | 2018-4-19 14:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
混子黄|  楼主 | 2018-4-19 14:33 | 只看该作者
void main()
{

/* 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;

   struct ECAN_REGS ECanbShadow;

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
   InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example

   // Just initalize eCAN pins for this example
   // This function is in DSP2833x_ECan.c
//  InitECanGpio();
   InitECanbGpio();

// 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.

// No interrupts used in this example.

// 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();
  InitECanb();

// Step 5. User specific code:

/* Write to the MSGID field  */

   ECanbMboxes.MBOX25.MSGID.all = 0x95555555; // Extended Identifier

/* Configure Mailbox under test as a Transmit mailbox */

   ECanbShadow.CANMD.all = ECanbRegs.CANMD.all;
   ECanbShadow.CANMD.bit.MD25 = 0;    ///  1: 接收邮箱  0:发送邮箱
   ECanbRegs.CANMD.all = ECanbShadow.CANMD.all;

/* Enable Mailbox under test */

   ECanbShadow.CANME.all = ECanbRegs.CANME.all;
   ECanbShadow.CANME.bit.ME25 = 1;     ///  1:使能    0:  不使能
   ECanbRegs.CANME.all = ECanbShadow.CANME.all;

/* Write to DLC field in Master Control reg */

   ECanbMboxes.MBOX25.MSGCTRL.bit.DLC = 8;

/* Write to the mailbox RAM field */

   ECanbMboxes.MBOX25.MDL.all = 0x12345678;
   ECanbMboxes.MBOX25.MDH.all = 0x98765432;

/* Begin transmitting */

while(1)
{
  // for(i=0; i < TXCOUNT; 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;

      loopcount ++;
    }
        }
    // asm(" ESTOP0");  // Stop here
}

使用特权

评论回复
板凳
chunyang| | 2018-4-19 14:36 | 只看该作者
很显然是程序有错,你该自行检查代码。

使用特权

评论回复
地板
混子黄|  楼主 | 2018-4-19 14:36 | 只看该作者
while(ECanbShadow.CANTA.bit.TA25 == 0 ); 最后会停在这里。

但是我的接受呀板子已经收到数据了,然后不再全速运行,在

ECanbShadow.CANTA.bit.TA25 = 1;                      // Clear TA5

设置一个断点发现进不去了。。

然后复位一下,就又能进去了,然后最后还是会停在这里while(ECanbShadow.CANTA.bit.TA25 == 0 ); 最后会停在这里。

使用特权

评论回复
5
混子黄|  楼主 | 2018-4-19 14:37 | 只看该作者
那些初始化的配置我没有改变,照搬TI的例程。

使用特权

评论回复
6
混子黄|  楼主 | 2018-4-19 14:37 | 只看该作者
接受板子的代码:

void main()
{

/* 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;

   struct ECAN_REGS ECanbShadow;

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
   InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example

   // Just initalize eCAN pins for this example
   // This function is in DSP2833x_ECan.c
   InitECanGpio();

   InitECanbGpio();

// 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.

// No interrupts used in this example.

// 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();

   InitECanb();

// Step 5. User specific code:

/* Write to the MSGID field  */

   ECanbMboxes.MBOX24.MSGID.all = 0x95555555; // Extended Identifier

/* Configure Mailbox under test as a Transmit mailbox */

   ECanbShadow.CANMD.all = ECanbRegs.CANMD.all;
   ECanbShadow.CANMD.bit.MD24 = 1;    ///  1: 接收邮箱  0:发送邮箱
   ECanbRegs.CANMD.all = ECanbShadow.CANMD.all;

/* Enable Mailbox under test */

   ECanbShadow.CANME.all = ECanbRegs.CANME.all;
   ECanbShadow.CANME.bit.ME24 = 1;     ///  1:使能    0:  不使能
   ECanbRegs.CANME.all = ECanbShadow.CANME.all;

/* Write to DLC field in Master Control reg */

   ECanbMboxes.MBOX24.MSGCTRL.bit.DLC = 8;

    ECanbMboxes.MBOX24.MSGCTRL.bit.RTR = 0;

        EALLOW;

        ECanbRegs.CANMIM.all = 0xffffffff;

        ECanbRegs.CANMIL.all = 0;

        ECanbRegs.CANGIF0.all = 0xffffffff;

        ECanbRegs.CANGIM.bit.I0EN = 1;

        EDIS;


        PieCtrlRegs.PIEIER9.bit.INTx7 = 1;

        IER |= M_INT9;

        EINT;

        ERTM;



         for(;;);

}

使用特权

评论回复
7
混子黄|  楼主 | 2018-4-19 14:38 | 只看该作者
中断函数的代码

interrupt void ECAN0INTB_ISR(void)  // eCAN-B
{
  // ECanbShadow.CANRMP.all = ECanbRegs.CANRMP.all;
         while(ECanbRegs.CANRMP.all != 0x01000000 );
                  

                 ECanbRegs.CANRMP.all = 0x01000000;

                 datah = ECanbMboxes.MBOX24.MDH.all;

                 datal = ECanbMboxes.MBOX24.MDL.all;

                 PieCtrlRegs.PIEACK.bit.ACK9 = 1;



  // Insert ISR Code here


  // To receive more interrupts from this PIE group, acknowledge this interrupt
  // PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;

  // Next two lines for debug only to halt the processor here
  // Remove after inserting ISR Code
// asm ("      ESTOP0");
// for(;;);

}

使用特权

评论回复
8
混子黄|  楼主 | 2018-4-19 14:39 | 只看该作者
为什么无法做到一直给接受板子发送数据?

但是复位以后又能重新发送数据了?

使用特权

评论回复
9
chunyang| | 2018-4-19 14:43 | 只看该作者
混子黄 发表于 2018-4-19 14:39
为什么无法做到一直给接受板子发送数据?

但是复位以后又能重新发送数据了? ...

既然代码是抄的,建议你先读懂代码,画出细致的程序流程更容易读懂程序。

使用特权

评论回复
10
混子黄|  楼主 | 2018-4-19 14:46 | 只看该作者
chunyang 发表于 2018-4-19 14:36
很显然是程序有错,你该自行检查代码。

嗯,我在看看。

使用特权

评论回复
11
混子黄|  楼主 | 2018-4-20 10:40 | 只看该作者
。。。。。。。。解决了,每次发送之前先初始化配置就好了。

使用特权

评论回复
12
魏公村樵夫| | 2018-9-5 15:54 | 只看该作者
大佬  我最近也在学习DSP的CAN通讯,我用的是28335,我看你之前发的帖子,我也想用两块DSP实现一个发送一个接受,一直没有成功,我们参考一下你的源程序吗,感激不尽

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

108

主题

1039

帖子

6

粉丝