Announcements

Help us improve the Power & Sensing Selection Guide. Share feedback

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Zrd
Level 1
Level 1
First question asked Welcome!

The  LED0 don not light and i use the device can not receive the message.

//////////////////////////////////////////////////////////

#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"

IFX_ALIGN(4) IfxCpu_syncEvent g_cpuSyncEvent = 0;

#include "Ifx_Types.h"
#include "IfxCan_Can.h"
#include "IfxCan.h"
#include "IfxCpu_Irq.h"
#include "IfxPort.h"
#include "Bsp.h"

#define LED0 &MODULE_P13,0
#define LED1 &MODULE_P13,1
#define LED2 &MODULE_P13,2
#define LED3 &MODULE_P13,3

#define MODULE_CAN0_RAM 0xF0200000
#define NODE0_RAM_OFFSET 0x0
//#define NODE1_RAM_OFFSET 0x1000

#define CAN_MESSAGE_TX_ID0 (uint32)0x1234567
#define CAN_MESSAGE_RX_ID1 (uint32)0x1234561
#define CAN_MESSAGE_RX_ID2 (uint32)0x1234562
#define CAN_MESSAGE_RX_ID3 (uint32)0x1234563

#define ISR_PRIORITY_CAN_TX 2 /* Define the CAN TX interrupt priority */
#define ISR_PRIORITY_CAN_RX 1 /* Define the CAN RX interrupt priority */
#define MAXIMUM_CAN_DATA_PAYLOAD 2 /* Define maximum classical CAN payload in 4-byte words */

IFX_CONST IfxCan_Can_Pins Can00_pins = {
&IfxCan_TXD00_P20_8_OUT, IfxPort_OutputMode_pushPull, // CAN00_TX
&IfxCan_RXD00B_P20_7_IN, IfxPort_InputMode_pullUp, // CAN00_RX
IfxPort_PadDriver_cmosAutomotiveSpeed4
};

typedef struct
{
IfxCan_Can_Config canConfig; /* CAN module configuration structure */
IfxCan_Can canModule; /* CAN module handle */
IfxCan_Can_Node can00Node; /* CAN source node handle data structure */
IfxCan_Can_NodeConfig canNodeConfig; /* CAN node configuration structure */
IfxCan_Filter canFilter; /* CAN filter configuration structure */
IfxCan_Message txMsg; /* Transmitted CAN message structure */
IfxCan_Message rxMsg; /* Received CAN message structure */
uint32 txData[MAXIMUM_CAN_DATA_PAYLOAD]; /* Transmitted CAN data array */
uint32 rxData[MAXIMUM_CAN_DATA_PAYLOAD]; /* Received CAN data array */
} McmcanType;

McmcanType g_mcmcan;

IFX_INTERRUPT(canIsrTxHandler, 0, ISR_PRIORITY_CAN_TX);
void canIsrTxHandler(void)
{
/* Clear the "Transmission Completed" interrupt flag */
IfxCan_Node_clearInterruptFlag(g_mcmcan.can00Node.node, IfxCan_Interrupt_transmissionCompleted);

IfxPort_togglePin(LED0); //transmit indicator
}

IFX_INTERRUPT(canIsrRxHandler, 0, ISR_PRIORITY_CAN_RX);
void canIsrRxHandler(void)
{
/* Clear the "Message stored to Dedicated RX Buffer" interrupt flag */
IfxCan_Node_clearInterruptFlag(g_mcmcan.can00Node.node, IfxCan_Interrupt_messageStoredToDedicatedRxBuffer);
/* Clear the "New Data" flag; as long as the "New Data" flag is set, the respective Rx buffer is
* locked against updates from received matching frames.
*/
IfxCan_Node_clearRxBufferNewDataFlag(g_mcmcan.can00Node.node, g_mcmcan.canFilter.rxBufferOffset);
/* Read the received CAN message */
IfxCan_Can_readMessage(&g_mcmcan.can00Node, &g_mcmcan.rxMsg, g_mcmcan.rxData);

/* Check if the received data matches with the transmitted one */
if((g_mcmcan.rxMsg.messageId == CAN_MESSAGE_RX_ID1)
&& (g_mcmcan.rxData[1] == 0xEFCDAB89))
{
if(g_mcmcan.rxData[0] == 0x67452301) { //send 0x
IfxPort_setPinState(LED1, IfxPort_State_low); //LED ON
} else {
IfxPort_setPinState(LED1, IfxPort_State_high); //LED OFF
}
}

if((g_mcmcan.rxMsg.messageId == CAN_MESSAGE_RX_ID2)
&& (g_mcmcan.rxData[1] == 0xEFCDAB89))
{
if(g_mcmcan.rxData[0] == 0x67452302) {
IfxPort_setPinState(LED2, IfxPort_State_low);
} else {
IfxPort_setPinState(LED2, IfxPort_State_high);
}
}

if((g_mcmcan.rxMsg.messageId == CAN_MESSAGE_RX_ID3)
&& (g_mcmcan.rxData[1] == 0xEFCDAB89))
{
if(g_mcmcan.rxData[0] == 0x67452303) {
IfxPort_setPinState(LED3, IfxPort_State_low);
} else {
IfxPort_setPinState(LED3, IfxPort_State_high);
}
}

}

void initCAN0(void)
{
/*******CAN module configuration and initialization*******/
IfxCan_Can_initModuleConfig(&g_mcmcan.canConfig, &MODULE_CAN0);
IfxCan_Can_initModule(&g_mcmcan.canModule, &g_mcmcan.canConfig);

/*******CAN00 node configuration and initialization*******/
IfxCan_Can_initNodeConfig(&g_mcmcan.canNodeConfig, &g_mcmcan.canModule);

g_mcmcan.canNodeConfig.nodeId = IfxCan_NodeId_0;
g_mcmcan.canNodeConfig.clockSource = IfxCan_ClockSource_both;
g_mcmcan.canNodeConfig.frame.type = IfxCan_FrameType_transmitAndReceive;
g_mcmcan.canNodeConfig.frame.mode = IfxCan_FrameMode_standard; //Classic CAN

g_mcmcan.canNodeConfig.txConfig.txMode = IfxCan_TxMode_dedicatedBuffers;
g_mcmcan.canNodeConfig.txConfig.dedicatedTxBuffersNumber = 255;
g_mcmcan.canNodeConfig.txConfig.txBufferDataFieldSize = IfxCan_DataFieldSize_8;

g_mcmcan.canNodeConfig.rxConfig.rxMode = IfxCan_RxMode_dedicatedBuffers;
g_mcmcan.canNodeConfig.rxConfig.rxBufferDataFieldSize = IfxCan_DataFieldSize_8;

g_mcmcan.canNodeConfig.filterConfig.extendedListSize = 255; //Extended Frame
g_mcmcan.canNodeConfig.filterConfig.messageIdLength = IfxCan_MessageIdLength_extended;

g_mcmcan.canNodeConfig.messageRAM.extendedFilterListStartAddress = 0x100; //Extended Frame
g_mcmcan.canNodeConfig.messageRAM.rxBuffersStartAddress = 0x200;
g_mcmcan.canNodeConfig.messageRAM.txBuffersStartAddress = 0x400;
g_mcmcan.canNodeConfig.messageRAM.baseAddress = MODULE_CAN0_RAM + NODE0_RAM_OFFSET;

g_mcmcan.canNodeConfig.baudRate.baudrate = 500000; //500KBaud

//transmit interrupt
g_mcmcan.canNodeConfig.interruptConfig.transmissionCompletedEnabled = TRUE;
g_mcmcan.canNodeConfig.interruptConfig.traco.priority = ISR_PRIORITY_CAN_TX;
g_mcmcan.canNodeConfig.interruptConfig.traco.interruptLine = IfxCan_InterruptLine_0;
g_mcmcan.canNodeConfig.interruptConfig.traco.typeOfService = IfxSrc_Tos_cpu0;

//receive interrupt
g_mcmcan.canNodeConfig.interruptConfig.messageStoredToDedicatedRxBufferEnabled = TRUE;
g_mcmcan.canNodeConfig.interruptConfig.reint.priority = ISR_PRIORITY_CAN_RX;
g_mcmcan.canNodeConfig.interruptConfig.reint.interruptLine = IfxCan_InterruptLine_1;
g_mcmcan.canNodeConfig.interruptConfig.reint.typeOfService = IfxSrc_Tos_cpu0;

//binding pin
g_mcmcan.canNodeConfig.pins = &Can00_pins;

IfxCan_Can_initNode(&g_mcmcan.can00Node, &g_mcmcan.canNodeConfig);

/*******CAN filter configuration and initialization*******/
g_mcmcan.canFilter.number = 0;
g_mcmcan.canFilter.elementConfiguration = IfxCan_FilterElementConfiguration_storeInRxBuffer;
g_mcmcan.canFilter.id1 = CAN_MESSAGE_RX_ID1;
g_mcmcan.canFilter.rxBufferOffset = IfxCan_RxBufferId_0;
//IfxCan_Can_setStandardFilter(&g_mcmcan.can00Node, &g_mcmcan.canFilter);
IfxCan_Can_setExtendedFilter(&g_mcmcan.can00Node, &g_mcmcan.canFilter);

g_mcmcan.canFilter.number = 1;
g_mcmcan.canFilter.elementConfiguration = IfxCan_FilterElementConfiguration_storeInRxBuffer;
g_mcmcan.canFilter.id1 = CAN_MESSAGE_RX_ID2;
//g_mcmcan.canFilter.id2 = CAN_MESSAGE_RX_ID2;
g_mcmcan.canFilter.rxBufferOffset = IfxCan_RxBufferId_0;
//IfxCan_Can_setStandardFilter(&g_mcmcan.can00Node, &g_mcmcan.canFilter);
IfxCan_Can_setExtendedFilter(&g_mcmcan.can00Node, &g_mcmcan.canFilter);

g_mcmcan.canFilter.number = 2;
g_mcmcan.canFilter.elementConfiguration = IfxCan_FilterElementConfiguration_storeInRxBuffer;
g_mcmcan.canFilter.id1 = CAN_MESSAGE_RX_ID3;
//g_mcmcan.canFilter.id2 = CAN_MESSAGE_RX_ID3;
g_mcmcan.canFilter.rxBufferOffset = IfxCan_RxBufferId_0;
//IfxCan_Can_setStandardFilter(&g_mcmcan.can00Node, &g_mcmcan.canFilter);
IfxCan_Can_setExtendedFilter(&g_mcmcan.can00Node, &g_mcmcan.canFilter);

}

void initLED(void)
{
IfxPort_setPinMode(LED0, IfxPort_Mode_outputPushPullGeneral); /* Initialize LED port pin */
IfxPort_setPinMode(LED1, IfxPort_Mode_outputPushPullGeneral);
IfxPort_setPinMode(LED2, IfxPort_Mode_outputPushPullGeneral);
IfxPort_setPinMode(LED3, IfxPort_Mode_outputPushPullGeneral);
IfxPort_setPinState(LED0, IfxPort_State_high); /* Turn off LED (LED is low-level active) */
IfxPort_setPinState(LED1, IfxPort_State_high);
IfxPort_setPinState(LED2, IfxPort_State_high);
IfxPort_setPinState(LED3, IfxPort_State_high);
}


void core0_main(void)
{
IfxCpu_enableInterrupts();

/* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
* Enable the watchdogs and service them periodically if it is required
*/
IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());

/* Wait for CPU sync event */
IfxCpu_emitEvent(&g_cpuSyncEvent);
IfxCpu_waitEvent(&g_cpuSyncEvent, 1);

initLED();
initTime();
initCAN0();

IfxCan_Can_initMessage(&g_mcmcan.txMsg);
g_mcmcan.txMsg.messageId = CAN_MESSAGE_TX_ID0;
g_mcmcan.txMsg.bufferNumber = 0;
g_mcmcan.txMsg.dataLengthCode = IfxCan_DataLengthCode_8;//8 bytes
g_mcmcan.txMsg.frameMode = IfxCan_FrameMode_standard; //Classic CAN
g_mcmcan.txMsg.messageIdLength=IfxCan_MessageIdLength_extended; //Extended Frame
g_mcmcan.txData[0] = 0x98BADCFE;
g_mcmcan.txData[1] = 0x10325476;
//you will receive: FE DC BA 98 76 54 32 10

while(1)
{
while(IfxCan_Status_notSentBusy ==
IfxCan_Can_sendMessage(&g_mcmcan.can00Node, &g_mcmcan.txMsg, &g_mcmcan.txData[0]) );

waitTime(TimeConst_1s);
}
}

 

 

0 Likes
3 Replies
Di_W
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 250 solutions authored

 Please refer to this example code, and a separated slide send to your email.

0 Likes
Di_W
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 250 solutions authored
//There are two nodes used, one is for transamiting, the other is for receiving.

void initMcmcan(void)
{
    /* ==========================================================================================
     * CAN module configuration and initialization:
     * ==========================================================================================
     *  - load default CAN module configuration into configuration structure
     *  - initialize CAN module with the default configuration
     * ==========================================================================================
     */
    IfxCan_Can_initModuleConfig(&g_mcmcan.canConfig, &MODULE_CAN0);

    IfxCan_Can_initModule(&g_mcmcan.canModule, &g_mcmcan.canConfig);

    /* ==========================================================================================
     * Source CAN node configuration and initialization:
     * ==========================================================================================
     *  - load default CAN node configuration into configuration structure
     *
     *  - set source CAN node in the "Loop-Back" mode (no external pins are used)
     *  - assign source CAN node to CAN node 0
     *
     *  - define the frame to be the transmitting one
     *
     *  - once the transmission is completed, raise the interrupt
     *  - define the transmission complete interrupt priority
     *  - assign the interrupt line 0 to the transmission complete interrupt
     *  - transmission complete interrupt service routine should be serviced by the CPU0
     *
     *  - initialize the source CAN node with the modified configuration
     * ==========================================================================================
     */
    IfxCan_Can_initNodeConfig(&g_mcmcan.canNodeConfig, &g_mcmcan.canModule);

    g_mcmcan.canNodeConfig.busLoopbackEnabled = TRUE;
    g_mcmcan.canNodeConfig.nodeId = IfxCan_NodeId_0;

    g_mcmcan.canNodeConfig.frame.type = IfxCan_FrameType_transmit;

    g_mcmcan.canNodeConfig.interruptConfig.transmissionCompletedEnabled = TRUE;
    g_mcmcan.canNodeConfig.interruptConfig.traco.priority = ISR_PRIORITY_CAN_TX;
    g_mcmcan.canNodeConfig.interruptConfig.traco.interruptLine = IfxCan_InterruptLine_0;
    g_mcmcan.canNodeConfig.interruptConfig.traco.typeOfService = IfxSrc_Tos_cpu0;

    IfxCan_Can_initNode(&g_mcmcan.canSrcNode, &g_mcmcan.canNodeConfig);

    /* ==========================================================================================
     * Destination CAN node configuration and initialization:
     * ==========================================================================================
     *  - load default CAN node configuration into configuration structure
     *
     *  - set destination CAN node in the "Loop-Back" mode (no external pins are used)
     *  - assign destination CAN node to CAN node 1
     *
     *  - define the frame to be the receiving one
     *
     *  - once the message is stored in the dedicated RX buffer, raise the interrupt
     *  - define the receive interrupt priority
     *  - assign the interrupt line 1 to the receive interrupt
     *  - receive interrupt service routine should be serviced by the CPU0
     *
     *  - initialize the destination CAN node with the modified configuration
     * ==========================================================================================
     */
    IfxCan_Can_initNodeConfig(&g_mcmcan.canNodeConfig, &g_mcmcan.canModule);

    g_mcmcan.canNodeConfig.busLoopbackEnabled = TRUE;
    g_mcmcan.canNodeConfig.nodeId = IfxCan_NodeId_1;

    g_mcmcan.canNodeConfig.frame.type = IfxCan_FrameType_receive;

    g_mcmcan.canNodeConfig.interruptConfig.messageStoredToDedicatedRxBufferEnabled = TRUE;
    g_mcmcan.canNodeConfig.interruptConfig.reint.priority = ISR_PRIORITY_CAN_RX;
    g_mcmcan.canNodeConfig.interruptConfig.reint.interruptLine = IfxCan_InterruptLine_1;
    g_mcmcan.canNodeConfig.interruptConfig.reint.typeOfService = IfxSrc_Tos_cpu0;

    IfxCan_Can_initNode(&g_mcmcan.canDstNode, &g_mcmcan.canNodeConfig);

    /* ==========================================================================================
     * CAN filter configuration and initialization:
     * ==========================================================================================
     *  - filter configuration is stored under the filter element number 0
     *  - store received frame in a dedicated RX Buffer
     *  - define the same message ID as defined for the TX message
     *  - assign the filter to the dedicated RX Buffer (RxBuffer0 in this case)
     *
     *  - initialize the standard filter with the modified configuration
     * ==========================================================================================
     */
    g_mcmcan.canFilter.number = 0;
    g_mcmcan.canFilter.elementConfiguration = IfxCan_FilterElementConfiguration_storeInRxBuffer;
    g_mcmcan.canFilter.id1 = CAN_MESSAGE_ID;
    g_mcmcan.canFilter.rxBufferOffset = IfxCan_RxBufferId_0;

    IfxCan_Can_setStandardFilter(&g_mcmcan.canDstNode, &g_mcmcan.canFilter);
}
0 Likes
Di_W
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 250 solutions authored

(1)In your code, TX ID0 =0x1234567,  txData[0]=0x98BADCFE;  txData[1]=0x10325476  Please check if above packet is transmitted.

(2)In your code, please set a breakpoint in canIsrRxHandler(), to check rxMsg.messageId if properly received, if yes, then check if it equals to RX_ID1/RX_ID2/RX_ID3, and STEP IN using debugger to check what content received from CAN bus.

0 Likes