Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect CAN Frame ID during Message Transmission #2089

Open
vibhanshuverma02 opened this issue Sep 5, 2024 · 0 comments
Open

Incorrect CAN Frame ID during Message Transmission #2089

vibhanshuverma02 opened this issue Sep 5, 2024 · 0 comments
Labels
bug Something isn't working peripheral:twai TWAI peripheral

Comments

@vibhanshuverma02
Copy link

vibhanshuverma02 commented Sep 5, 2024

I am writing an application for the ESP32 using the CAN protocol. I have used the esp_hal::Twai crate for this implementation. However, I am facing an issue with CAN message transmission. The byte array data for the CAN message has been encoded into different bits, and I have set the CAN Frame ID as zero according to the provided example. However, it is being encoded into another Frame ID, specifically 0x3B9.

Below is the code I used along with the CAN dump output.
Code:

#![no_std]
#![no_main]

const IS_FIRST_SENDER: bool = true;
use esp_backtrace as _;
use embedded_can::nb::Can;

use esp_hal::{
    clock::ClockControl,
    gpio::Io,
    peripherals::Peripherals,
    prelude::*,
    system::SystemControl,
    twai::{self, filter::SingleStandardFilter, EspTwaiFrame, StandardId},
};
use esp_println::println;
use nb::block;

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = SystemControl::new(peripherals.SYSTEM);
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

    let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);

    let can_tx_pin = io.pins.gpio5;
    let can_rx_pin = io.pins.gpio4;

    // The speed of the CAN bus.
    const CAN_BAUDRATE: twai::BaudRate = twai::BaudRate::B500K;
    println!("baud: {}", match CAN_BAUDRATE {
        twai::BaudRate::B500K => "500K",
        twai::BaudRate::B125K => "125K",
        twai::BaudRate::B250K => "250K",
        twai::BaudRate::B1000K => "1000K",
        twai::BaudRate::Custom(_) => todo!(),
    });
    
    let mut can_config = twai::TwaiConfiguration::new(
        peripherals.TWAI0,
        can_tx_pin,
        can_rx_pin,
        &clocks,
        CAN_BAUDRATE,
        None,
    );

    // Define the CAN filter
    // let filter = SingleStandardFilter::new(StandardId::new(0x000).unwrap(), 0x000);
    // can_config.set_filter(filter);

    let mut can = can_config.start();

    loop {
        // Send the CAN message
        let frame = EspTwaiFrame::new(StandardId::new(0x010).unwrap().into(), &[14, 20, 3]).unwrap();
        println!("Frame data: {:?}", frame);

        match block!(can.transmit(&frame)) {
            Ok(_) => println!("Frame transmitted"),
            Err(e) => println!("Failed to transmit frame: {:?}", e),
        }
    }
}

CAN Dump Output:
Screenshot from 2024-09-05 15-37-49

Problem:

The CAN Frame ID is set to 0x010 in the code, but the actual transmission is showing a different Frame ID (0x3B9).
I would like to understand how to correctly transmit the actual CAN message without modifying the Frame ID.

Any help or clarification on how to resolve this issue would be greatly appreciated.

@vibhanshuverma02 vibhanshuverma02 changed the title ncorrect CAN Frame ID during Message Transmission Incorrect CAN Frame ID during Message Transmission Sep 5, 2024
@SergioGasquez SergioGasquez added the peripheral:twai TWAI peripheral label Sep 5, 2024
@MabezDev MabezDev added the bug Something isn't working label Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working peripheral:twai TWAI peripheral
Projects
Status: Todo
Development

No branches or pull requests

3 participants