ESP32 (ESP-IDF) “driver/twai.h” CAN 数据传输正常,但使用 CAN/TWAI 接收功能不工作

问题描述 投票:0回答:1

我在 YouTube 上看到有人使用类似代码的视频,对他来说效果很好(YouTube 视频:链接:https://youtu.be/bxzWuIqfn9Y)。他使用了从 ESP-IDF v5.0 中删除的“driver/can.h”,因为 CAN 和 TWAI 是一样的。

更多详情: GitHub 上提出的问题:https://github.com/espressif/esp-idf/issues/10757#issue-1582253298 使用的 IDF 版本:v5.0 和 v4.4.4 使用的操作系统:Windows 用于编程和刷写代码的IDE:VS Code IDE

这是我的代码:

#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "stdio.h"
#include "stdlib.h"
#include "driver/twai.h"

void twai_setup_and_install(){
    //Initialize configuration structures using macro initializers
    twai_general_config_t g_config = {
        .mode = TWAI_MODE_NORMAL,
        .tx_io = GPIO_NUM_5,
        .rx_io = GPIO_NUM_4,
        .clkout_io = TWAI_IO_UNUSED,
        .bus_off_io = TWAI_IO_UNUSED,
        .tx_queue_len = 5,
        .rx_queue_len = 5,
        .alerts_enabled = TWAI_ALERT_NONE,
        .clkout_divider = 0
    };

    twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
    twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

    // Install TWAI driver
    if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
        printf("Driver installed\n");
    } else {
        printf("Failed to install driver\n");
        return;
    }

    // Start TWAI driver
    if (twai_start() == ESP_OK) {
        printf("Driver started\n");
    } else {
        printf("Failed to start driver\n");
        return;
    }
}

void new_message(twai_message_t *message, uint32_t id, uint8_t dlc, uint8_t *data)
{
    
    message->flags = TWAI_MSG_FLAG_NONE;
    message->identifier = id;
    message->data_length_code = dlc;
    for (int i = 0; i < dlc; i++) {
        message->data[i] = data[i];
    }
    printf("Message created\nID: %ld DLC: %d Data:\t", message->identifier, message->data_length_code);
    for (int i = 0; i < message->data_length_code; i++) {
        printf("%d\t", message->data[i]);
    }
    printf("\n");
}

void transmit_message(twai_message_t *message)
{
    if (twai_transmit(message, pdMS_TO_TICKS(1000)) == ESP_OK) {
        printf("Message queued for transmission\n");
    } else {
        printf("Failed to send message\n");
    }
}

void receive_message(twai_message_t *message)
{
    if (twai_receive(message, pdMS_TO_TICKS(1000)) == ESP_OK) {
        printf("Message received:\n");
        printf("ID: %ld DLC: %d Data:\t", message->identifier, message->data_length_code);
        for (int i = 0; i < message->data_length_code; i++) {
            (message->extd)?printf("Extended ID"):printf("Standard ID");
            printf("%d\t", message->data[i]);
        }
    } else {
        printf("Failed to receive message\n");
    }
}

void app_main()
{
    
    twai_setup_and_install();
    twai_message_t message;
    twai_message_t message1;
    // Set the data to send
    uint8_t data[8] = {rand() % 255, rand() % 255, rand() % 255, 
    rand() % 255, rand() % 255, rand() % 255, rand() % 255, rand() % 255};

    while(true){ 
    // Create a new message
    new_message(&message, 0x123, 8, data);

    // Transmit the message to a queue
    transmit_message(&message);

    // Receive the message from the queue
    receive_message(&message1);

    // Wait for 1 second
    vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

}

如果有人能尽快帮助我,我将不胜感激。

真诚的问候。

遵循的步骤:

  • 我在 VS Code 中使用了 ESP-IDF v5.0。
  • 我使用文档来获取发送者和接收者的代码。
  • 我使用经过测试且功能正常的 MCP2515 模块作为我的收发器。
  • Tx 完美广播,但 Rx 不适用于上述代码。

预期产出:

Message received:
ID: 0x123 DLC: 8 Data: 1 2 3 4 5 6 7 8
In my source code, I have implemented random data bits, so data may vary

实际产量:

Failed to receive message

esp32 can-bus esp-idf
1个回答
0
投票

您正在为 ESP32 上的 Twai 控制器使用 twai 驱动程序,对于 MCP2515,您需要使用 MCP_CAN 库通过 SPI 总线接收消息到 ESP32。

© www.soinside.com 2019 - 2024. All rights reserved.