TI中文支持网
TI专业的中文技术问题搜集分享网站

can通信一直不通

// TI File $Revision: /main/8 $
// Checkin $Date: August 10, 2007 09:04:22 $
//###########################################################################
// Filename: Example_28xEcan_A_to_B_Xmit.c
//
// Description: eCAN-A To eCAN-B TXLOOP – Transmit loop
//
// ASSUMPTIONS:
//
// This program requires the DSP2833x header files.
//
// Both CAN ports of the 2833x DSP need to be connected
// to each other (via CAN transceivers)
//
// eCANA is on GPIO19(CANTXA) and
// GPIO18 (CANRXA)
//
// eCANB is on GPIO20 (CANTXB) and
// GPIO21 (CANRXB)
//
// As supplied, this project is configured for "boot to SARAM"
// operation. The 2833x Boot Mode table is shown below.
// For information on configuring the boot mode of an eZdsp,
// please refer to the documentation included with the eZdsp,
//
// $Boot_Table:
//
// GPIO87 GPIO86 GPIO85 GPIO84
// XA15 XA14 XA13 XA12
// PU PU PU PU
// ==========================================
// 1 1 1 1 Jump to Flash
// 1 1 1 0 SCI-A boot
// 1 1 0 1 SPI-A boot
// 1 1 0 0 I2C-A boot
// 1 0 1 1 eCAN-A boot
// 1 0 1 0 McBSP-A boot
// 1 0 0 1 Jump to XINTF x16
// 1 0 0 0 Jump to XINTF x32
// 0 1 1 1 Jump to OTP
// 0 1 1 0 Parallel GPIO I/O boot
// 0 1 0 1 Parallel XINTF boot
// 0 1 0 0 Jump to SARAM <- "boot to SARAM"
// 0 0 1 1 Branch to check boot mode
// 0 0 1 0 Boot to flash, bypass ADC cal
// 0 0 0 1 Boot to SARAM, bypass ADC cal
// 0 0 0 0 Boot to SCI-A, bypass ADC cal
// Boot_Table_End$
//
// DESCRIPTION:
//
// This example TRANSMITS data to another CAN module using MAILBOX5
// This program could either loop forever or transmit "n" # of times,
// where "n" is the TXCOUNT value.
//
// This example can be used to check CAN-A and CAN-B. Since CAN-B is
// initialized in DSP2833x_ECan.c, it will acknowledge all frames
// transmitted by the node on which this code runs. Both CAN ports of
// the 2833x DSP need to be connected to each other (via CAN transceivers)
//
//###########################################################################
// Original Author: HJ
//
// $TI Release: DSP2833x Header Files V1.10 $
// $Release Date: February 15, 2008 $
//###########################################################################

#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File

#define TXCOUNT 100 // Transmission will take place (TXCOUNT) times..

// Globals for this example
long i,j;
long loopcount = 0;
volatile struct MBOX *Mailbox;
Uint32 ErrorCount;
Uint32 PassCount;
Uint32 MessageReceivedCount;

Uint32 TestMbox1 = 0;
Uint32 TestMbox2 = 0;
Uint32 TestMbox3 = 0;

void mailbox_check(int32 T1, int32 T2, int32 T3)
{
if((T1 !=0x55555555 ) || ( T2 != 0xAAAAAAAA)|| ( T3 != 0x95555555))
{
ErrorCount++;
}
else
{
PassCount++;
}
}

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

// 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();
ErrorCount = 0;
PassCount = 0;
// Step 5. User specific code:

/* Write to the MSGID field */

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

ECanbMboxes.MBOX25.MSGID.all = 0x95555555; // Extended Identifier
/* Configure Mailbox under test as a Transmit mailbox */

ECanaShadow.CANMD.all = ECanaRegs.CANMD.all;
ECanaShadow.CANMD.bit.MD25 = 0;
ECanaRegs.CANMD.all = ECanaShadow.CANMD.all;

ECanbShadow.CANMD.all = ECanbRegs.CANMD.all;
ECanbShadow.CANMD.bit.MD25 = 1;
ECanbRegs.CANMD.all = ECanbShadow.CANMD.all;

/* Enable Mailbox under test */

ECanaShadow.CANME.all = ECanaRegs.CANME.all;
ECanaShadow.CANME.bit.ME25 = 1;
ECanaRegs.CANME.all = ECanaShadow.CANME.all;

ECanbShadow.CANME.all = ECanbRegs.CANME.all;
ECanbShadow.CANME.bit.ME25 = 1;
ECanbRegs.CANME.all = ECanbShadow.CANME.all;

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

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

/* Write to the mailbox RAM field */

ECanaMboxes.MBOX25.MDL.all = 0x55555555;
ECanaMboxes.MBOX25.MDH.all = 0xAAAAAAAA;

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

Mailbox = &ECanbMboxes.MBOX0 + 25; //CANB读数据
TestMbox1 = Mailbox->MDL.all; // = 0x (n is the MBX number)
TestMbox2 = Mailbox->MDH.all; // = 0x (a constant)
TestMbox3 = Mailbox->MSGID.all;// = 0x (n is the MBX number)
mailbox_check(TestMbox1,TestMbox2,TestMbox3); // Checks the received data

loopcount ++;
}
if(ErrorCount == 0)
{
asm(" ESTOP0"); // OK,数据校验正确
}
else
{
asm(" ESTOP0"); // ERROR,
}

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

Alvin Chen:

你回环测试可以吗?debug卡在哪里了?如果不通的话先看一下波特率。

//###########################################################################
//
// FILE:Example_2833xECanBack2Back.c
//
// TITLE:eCAN back to back Example
//
//! \addtogroup f2833x_example_list
//! <h1>eCAN back to back (ecan_back2back)</h1>
//!
//! This example tests eCAN by transmitting data back-to-back at high speed
//! without stopping. The received data is verified. Any error is flagged.
//! MBX0 transmits to MBX16, MBX1 transmits to MBX17 and so on.... \n
//! This program illustrates the use of self-test mode
//!
//! This example uses the self-test mode of the CAN module. i.e. the
//! transmission/reception happens within the module itself (even the required
//! ACKnowldege is generated internally in the module). Therefore, there is no
//! need for a CAN transceiver to run this particular test case and no activity
//! will be seen in the CAN pins/bus. Because everything is internal, there is
//! no need for a 120-ohm termination resistor. Note that a real-world CAN
//! application needs a CAN transceiver and termination resistors on both ends
//! of the bus.
//!
//! \b Watch \b Variables \n
//! - PassCount
//! - ErrorCount
//! - MessageReceivedCount
//
//
//###########################################################################
// $TI Release: F2833x Support Library v2.00.00.00 $
// $Release Date: Thu Dec7 18:50:20 CST 2017 $
// $Copyright:
// Copyright (C) 2009-2017 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions// are met:
////Redistributions of source code must retain the above copyright//notice, this list of conditions and the following disclaimer.
////Redistributions in binary form must reproduce the above copyright
//notice, this list of conditions and the following disclaimer in the//documentation and/or other materials provided with the//distribution.
////Neither the name of Texas Instruments Incorporated nor the names of
//its contributors may be used to endorse or promote products derived
//from this software without specific prior written permission.
//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################//
// Included Files
//
#include "DSP28x_Project.h"// Device Headerfile and Examples Include File//
// Function Prototypes
//
void mailbox_check(int32 T1, int32 T2, int32 T3);
void mailbox_read(int16 i);//
// Globals
//
Uint32ErrorCount;
Uint32PassCount;
Uint32MessageReceivedCount;Uint32TestMbox1 = 0;
Uint32TestMbox2 = 0;
Uint32TestMbox3 = 0;//
// Main
//
void main(void)
{Uint16j;//// eCAN control registers require read/write access using 32-bits.Thus we// will create a set of shadow registers for this example.These shadow// registers will be used to make sure the access is 32-bits and not 16.//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();//// Step 2. Initialize 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//// For this example, configure CAN pins using GPIO regs here// This function is found in DSP2833x_ECan.c//InitECanGpio();//// Step 3. Clear all interrupts and initialize PIE vector table:// Disable CPU interrupts//DINT;//// Initialize 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();//// Step 4. Initialize all the Device Peripherals:// This function is found in DSP2833x_InitPeripherals.c//// InitPeripherals(); // Not required for this example//// Step 5. User specific code, enable interrupts://MessageReceivedCount = 0;ErrorCount = 0;PassCount = 0;//// Initialize eCAN-A module//InitECana();//// Mailboxes can be written to 16-bits or 32-bits at a time// Write to the MSGID field of TRANSMIT mailboxes MBOX0 - 15//ECanaMboxes.MBOX0.MSGID.all = 0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all = 0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all = 0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all = 0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all = 0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all = 0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all = 0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all = 0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all = 0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all = 0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all = 0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all = 0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all = 0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all = 0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all = 0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all = 0x9555AAAF;//// Write to the MSGID field of RECEIVE mailboxes MBOX16 - 31//ECanaMboxes.MBOX16.MSGID.all = 0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all = 0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all = 0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all = 0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all = 0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all = 0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all = 0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all = 0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all = 0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all = 0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all = 0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all = 0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all = 0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all = 0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all = 0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all = 0x9555AAAF;//// Configure Mailboxes 0-15 as Tx, 16-31 as Rx// Since this write is to the entire register (instead of a bit field)// a shadow register is not required.//ECanaRegs.CANMD.all = 0xFFFF0000;//// Enable all Mailboxes// Since this write is to the entire register (instead of a bit field)// a shadow register is not required.//ECanaRegs.CANME.all = 0xFFFFFFFF;//// Specify that 8 bits will be sent/received//ECanaMboxes.MBOX0.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX1.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX2.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX3.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX4.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX5.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX6.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX7.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX8.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX9.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX10.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX11.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX12.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX13.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX14.MSGCTRL.bit.DLC = 8;ECanaMboxes.MBOX15.MSGCTRL.bit.DLC = 8;//// Write to the mailbox RAM field of MBOX0 - 15//ECanaMboxes.MBOX0.MDL.all = 0x9555AAA0;ECanaMboxes.MBOX0.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX1.MDL.all = 0x9555AAA1;ECanaMboxes.MBOX1.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX2.MDL.all = 0x9555AAA2;ECanaMboxes.MBOX2.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX3.MDL.all = 0x9555AAA3;ECanaMboxes.MBOX3.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX4.MDL.all = 0x9555AAA4;ECanaMboxes.MBOX4.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX5.MDL.all = 0x9555AAA5;ECanaMboxes.MBOX5.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX6.MDL.all = 0x9555AAA6;ECanaMboxes.MBOX6.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX7.MDL.all = 0x9555AAA7;ECanaMboxes.MBOX7.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX8.MDL.all = 0x9555AAA8;ECanaMboxes.MBOX8.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX9.MDL.all = 0x9555AAA9;ECanaMboxes.MBOX9.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX10.MDL.all = 0x9555AAAA;ECanaMboxes.MBOX10.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX11.MDL.all = 0x9555AAAB;ECanaMboxes.MBOX11.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX12.MDL.all = 0x9555AAAC;ECanaMboxes.MBOX12.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX13.MDL.all = 0x9555AAAD;ECanaMboxes.MBOX13.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX14.MDL.all = 0x9555AAAE;ECanaMboxes.MBOX14.MDH.all = 0x89ABCDEF;ECanaMboxes.MBOX15.MDL.all = 0x9555AAAF;ECanaMboxes.MBOX15.MDH.all = 0x89ABCDEF;//// Since this write is to the entire register (instead of a bit field)// a shadow register is not required.//EALLOW;ECanaRegs.CANMIM.all = 0xFFFFFFFF;//// Configure the eCAN for self test mode// Enable the enhanced features of the eCAN.//EALLOW;ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;ECanaShadow.CANMC.bit.STM = 1;// Configure CAN for self-test modeECanaRegs.CANMC.all = ECanaShadow.CANMC.all;EDIS;//// Begin transmitting//for(;;){ECanaRegs.CANTRS.all = 0x0000FFFF; //Set TRS for all transmit mailboxeswhile(ECanaRegs.CANTA.all != 0x0000FFFF ){}// Wait for all TAn bits to be setECanaRegs.CANTA.all = 0x0000FFFF;// Clear all TAnMessageReceivedCount++;//// Read from Receive mailboxes and begin checking for data//for(j=16; j<32; j++) // Read & check 16 mailboxes{//// This func reads the indicated mailbox data//mailbox_read(j);//// Checks the received data//mailbox_check(TestMbox1,TestMbox2,TestMbox3);}}
}//
// mailbox_read - This function reads out the contents of the indicated
// by the Mailbox number (MBXnbr).
//
voidmailbox_read(int16 MBXnbr)
{volatile struct MBOX *Mailbox;Mailbox = &ECanaMboxes.MBOX0 + MBXnbr;TestMbox1 = Mailbox->MDL.all;// = 0x9555AAAn (n is the MBX number)TestMbox2 = Mailbox->MDH.all;// = 0x89ABCDEF (a constant)TestMbox3 = Mailbox->MSGID.all;// = 0x9555AAAn (n is the MBX number)
} // MSGID of a rcv MBX is transmitted as the MDL data.//
// mailbox_check -//
voidmailbox_check(int32 T1, int32 T2, int32 T3)
{if((T1 != T3) || ( T2 != 0x89ABCDEF)){ErrorCount++;}else{PassCount++;}
}//
// End of File
//

// TI File $Revision: /main/8 $
// Checkin $Date: August 10, 2007 09:04:22 $
//###########################################################################
// Filename: Example_28xEcan_A_to_B_Xmit.c
//
// Description: eCAN-A To eCAN-B TXLOOP – Transmit loop
//
// ASSUMPTIONS:
//
// This program requires the DSP2833x header files.
//
// Both CAN ports of the 2833x DSP need to be connected
// to each other (via CAN transceivers)
//
// eCANA is on GPIO19(CANTXA) and
// GPIO18 (CANRXA)
//
// eCANB is on GPIO20 (CANTXB) and
// GPIO21 (CANRXB)
//
// As supplied, this project is configured for "boot to SARAM"
// operation. The 2833x Boot Mode table is shown below.
// For information on configuring the boot mode of an eZdsp,
// please refer to the documentation included with the eZdsp,
//
// $Boot_Table:
//
// GPIO87 GPIO86 GPIO85 GPIO84
// XA15 XA14 XA13 XA12
// PU PU PU PU
// ==========================================
// 1 1 1 1 Jump to Flash
// 1 1 1 0 SCI-A boot
// 1 1 0 1 SPI-A boot
// 1 1 0 0 I2C-A boot
// 1 0 1 1 eCAN-A boot
// 1 0 1 0 McBSP-A boot
// 1 0 0 1 Jump to XINTF x16
// 1 0 0 0 Jump to XINTF x32
// 0 1 1 1 Jump to OTP
// 0 1 1 0 Parallel GPIO I/O boot
// 0 1 0 1 Parallel XINTF boot
// 0 1 0 0 Jump to SARAM <- "boot to SARAM"
// 0 0 1 1 Branch to check boot mode
// 0 0 1 0 Boot to flash, bypass ADC cal
// 0 0 0 1 Boot to SARAM, bypass ADC cal
// 0 0 0 0 Boot to SCI-A, bypass ADC cal
// Boot_Table_End$
//
// DESCRIPTION:
//
// This example TRANSMITS data to another CAN module using MAILBOX5
// This program could either loop forever or transmit "n" # of times,
// where "n" is the TXCOUNT value.
//
// This example can be used to check CAN-A and CAN-B. Since CAN-B is
// initialized in DSP2833x_ECan.c, it will acknowledge all frames
// transmitted by the node on which this code runs. Both CAN ports of
// the 2833x DSP need to be connected to each other (via CAN transceivers)
//
//###########################################################################
// Original Author: HJ
//
// $TI Release: DSP2833x Header Files V1.10 $
// $Release Date: February 15, 2008 $
//###########################################################################

#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File

#define TXCOUNT 100 // Transmission will take place (TXCOUNT) times..

// Globals for this example
long i,j;
long loopcount = 0;
volatile struct MBOX *Mailbox;
Uint32 ErrorCount;
Uint32 PassCount;
Uint32 MessageReceivedCount;

Uint32 TestMbox1 = 0;
Uint32 TestMbox2 = 0;
Uint32 TestMbox3 = 0;

void mailbox_check(int32 T1, int32 T2, int32 T3)
{
if((T1 !=0x55555555 ) || ( T2 != 0xAAAAAAAA)|| ( T3 != 0x95555555))
{
ErrorCount++;
}
else
{
PassCount++;
}
}

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

// 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();
ErrorCount = 0;
PassCount = 0;
// Step 5. User specific code:

/* Write to the MSGID field */

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

ECanbMboxes.MBOX25.MSGID.all = 0x95555555; // Extended Identifier
/* Configure Mailbox under test as a Transmit mailbox */

ECanaShadow.CANMD.all = ECanaRegs.CANMD.all;
ECanaShadow.CANMD.bit.MD25 = 0;
ECanaRegs.CANMD.all = ECanaShadow.CANMD.all;

ECanbShadow.CANMD.all = ECanbRegs.CANMD.all;
ECanbShadow.CANMD.bit.MD25 = 1;
ECanbRegs.CANMD.all = ECanbShadow.CANMD.all;

/* Enable Mailbox under test */

ECanaShadow.CANME.all = ECanaRegs.CANME.all;
ECanaShadow.CANME.bit.ME25 = 1;
ECanaRegs.CANME.all = ECanaShadow.CANME.all;

ECanbShadow.CANME.all = ECanbRegs.CANME.all;
ECanbShadow.CANME.bit.ME25 = 1;
ECanbRegs.CANME.all = ECanbShadow.CANME.all;

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

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

/* Write to the mailbox RAM field */

ECanaMboxes.MBOX25.MDL.all = 0x55555555;
ECanaMboxes.MBOX25.MDH.all = 0xAAAAAAAA;

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

Mailbox = &ECanbMboxes.MBOX0 + 25; //CANB读数据
TestMbox1 = Mailbox->MDL.all; // = 0x (n is the MBX number)
TestMbox2 = Mailbox->MDH.all; // = 0x (a constant)
TestMbox3 = Mailbox->MSGID.all;// = 0x (n is the MBX number)
mailbox_check(TestMbox1,TestMbox2,TestMbox3); // Checks the received data

loopcount ++;
}
if(ErrorCount == 0)
{
asm(" ESTOP0"); // OK,数据校验正确
}
else
{
asm(" ESTOP0"); // ERROR,
}

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

Seven Han:请您检查下gpio的配置是否正确。

赞(0)
未经允许不得转载:TI中文支持网 » can通信一直不通
分享到: 更多 (0)