jsphuebner / libopeninv

Generic modules that can be used in many projects
GNU Lesser General Public License v3.0
21 stars 21 forks source link

Can not read CAN frame #28

Closed rrryy39 closed 1 week ago

rrryy39 commented 1 week ago

Hi, I am currently can send CAN frames using sym32.send() function but cannot read CAN frames using the function I created.


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
    }
}

``
the can_fifo_pending() always returned 0. Any hint on how to solve this issue? thank you very much
jsphuebner commented 1 week ago

Are you sure your message is received? Please refer to stm32_can.cpp

Please use the openinverter forum for support, not the issues function.