Not able recieve CAN frames using stm32_can.cpp

Development and discussion of fast charging systems eg Chademo , CCS etc
Post Reply
rrryy39
Posts: 7
Joined: Fri Jul 26, 2024 3:21 pm

Not able recieve CAN frames using stm32_can.cpp

Post by rrryy39 »

Hi, I am currently can send CAN frames using stm32.send() function but cannot read CAN frames using the function I created inside stm32_can.cpp

Code: Select all

int32_t Stm32Can::CANReadFrame(uint32_t *pCanId, uint8_t *recvData)
{
    uint32_t id = 0;
    bool ext = true, rtr = false;
    uint8_t length = 0, fmi = 0;
    uint32_t data[2] = {0}; // Buffer to store received data (8 bytes)
    uint32_t pending0 = can_fifo_pending(canDev, 0);
    uint32_t pending1 = can_fifo_pending(canDev, 1);

    addToTrace(MOD_HWIF, "fifo pending0:  ", (int16_t)pending0);
    addToTrace(MOD_HWIF, "fifo pending1:  ", [(int16_t)pending1);]
    
    // Ensure that the CAN device (canDev) is initialized and properly configured
    if (canDev == NULL) {
        printf("CAN device is not initialized.\n");
        return -1;
    }

    // Attempt to receive data from FIFO 0
    int pendingMessages = can_receive(canDev, 0, true, &id, &ext, &rtr, &fmi, &length, (uint8_t*)data, 0);
    // addToTrace(MOD_HWIF, "CAN ID:  ", (int32_t)data);
    if (pendingMessages > 0) {
      //  printf("Received data from FIFO 0. CAN_ID: %lu\r\n", id);
         addToTrace(MOD_HWIF, "CAN ID:  ", (int16_t)id);
        // Set CAN ID to the pointer provided
        *pCanId = id;

        // Copy the received data into recvData, ensuring no overflow (recvData should have 8 bytes)
        memcpy(recvData, data, length); // Use 'length' to copy only the required bytes

        // Print additional information for debugging
      //   printf("Data Length: %u\r\n", length);
      //   printf("Extended ID: %s\r\n", ext ? "Yes" : "No");
      //   printf("RTR: %s\r\n", rtr ? "Yes" : "No");
        lastRxTimestamp = rtc_get_counter_val();

        return 0; // Success
    } else {
        // If no messages were pending, print a debug message
        //addToTrace(MOD_HWIF, "No CAN");
        return -1; // Indicate failure to read data
    }
}
I tried to setup filters to allow every can frames to pass in but can_fifo_pending() always returns 0. Any hints of how I can receive CAN frames? Thanks in advance for the help
royhen99
Posts: 261
Joined: Sun Feb 20, 2022 4:23 am
Location: N. Wiltshire. UK
Has thanked: 22 times
Been thanked: 130 times

Re: Not able recieve CAN frames using stm32_can.cpp

Post by royhen99 »

Can you explain what you are trying to do and why you are writing an extra function?
Have you looked at other code that uses libopeninv library?
The usual way to access the receive data is using can callback routine ( or canmap ).

Code: Select all

static bool CanCallback(uint32_t id, uint32_t data[2], uint8_t dlc)
{
// code to check id and use data here
}
User avatar
Bigpie
Posts: 1769
Joined: Wed Apr 10, 2019 8:11 pm
Location: South Yorkshire, UK
Has thanked: 82 times
Been thanked: 422 times

Re: Not able recieve CAN frames using stm32_can.cpp

Post by Bigpie »

Post needs more details if you want help.

What hardware are you trying to run it on?
What are you sending frames with?
Do you have any can logs
Full code would asl help
BMW E91 2006
ZombieVerter
GS450h
Outlander Charger DC/DC
Renault Kangoo 36kWh battery
FOCCCI CCS
rrryy39
Posts: 7
Joined: Fri Jul 26, 2024 3:21 pm

Re: Not able recieve CAN frames using stm32_can.cpp

Post by rrryy39 »

I am working with Foccci 4.2 hardware and integrating it to send and receive custom CAN frames. The code is based on CCS32Clara, and I want to understand how to use the can_receive() function properly.

From what I see in the stm32_can.cpp file, the HandleMessage(int fifo) function is invoked within several interrupt service routines (ISRs), like this:

Code: Select all

/* Interrupt service routines */
extern "C" void usb_lp_can_rx0_isr(void)
{
    Stm32Can::GetInterface(0)->HandleMessage(0);
}

extern "C" void can_rx1_isr()
{
    Stm32Can::GetInterface(0)->HandleMessage(1);
}

extern "C" void can2_rx0_isr()
{
    Stm32Can::GetInterface(1)->HandleMessage(0);
}

extern "C" void can2_rx1_isr()
{
    Stm32Can::GetInterface(1)->HandleMessage(1);
}
However, these ISRs seem to be the only places where HandleMessage() is called, and I don't see where the actual CAN message is being received. In can.c, the can_receive() function appears to handle the actual receiving of messages:

Code: Select all

uint32_t can_receive(uint32_t canport, uint8_t fifo, bool release, uint32_t *id,
                     bool *ext, bool *rtr, uint8_t *fmi, uint8_t *length,
                     uint8_t *data, uint16_t *timestamp);
My questions are:

Does the HandleMessage() function internally call can_receive() or serve as a wrapper for it?
How can I use the can_receive() function directly in my code to implement a custom callback for processing received CAN messages?
How can_receive() is typically integrated into the callback structure for message handling?
Thank you for clarifying how to effectively use can_receive() in this context!
royhen99
Posts: 261
Joined: Sun Feb 20, 2022 4:23 am
Location: N. Wiltshire. UK
Has thanked: 22 times
Been thanked: 130 times

Re: Not able recieve CAN frames using stm32_can.cpp

Post by royhen99 »

I don't understand it well enough to explain how it works but the callback occurs in CanHardware::HandleRx.

There is no need to use can_receive() directly. Can call back is set like this FunctionPointerCallback cb(CanCallback, SetCanFilters); but in ccsclara, I think, the can is all handled using sdo.

How to use mapping and sdo here https://openinverter.org/wiki/CAN_communication.
User avatar
johu
Site Admin
Posts: 6709
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 368 times
Been thanked: 1536 times
Contact:

Re: Not able recieve CAN frames using stm32_can.cpp

Post by johu »

Here's an example of a custom CAN module https://github.com/jsphuebner/stm32-car ... mebbms.cpp

Inherit and implement CanCallback
https://github.com/jsphuebner/stm32-car ... e/mebbms.h
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
Post Reply