espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
12.95k stars 7.28k forks source link

twai_transmit(&message, 0); returns error 259 #8953

Open mattia424 opened 7 months ago

mattia424 commented 7 months ago

Board

ESP32-S3-WROOM-1

Device Description

I connected the SN65HVD230DR transceiver

In the last test I found that the problem persists even without anything connected except the logic analyzer on pin 21

Hardware Configuration

GPIO20 and GPIO21 connect to RX and TX of SN65HVD230DR

Version

v2.0.11

IDE Name

Arduino IDE 2.2.1

Operating System

WIN 11

Flash frequency

80MHz

PSRAM enabled

no

Upload speed

921600

Description

I uploaded the sketch to two different devices:

ESP32-S3-WROOM-1 M0N8R8: everything works correctly With the transciver connected to the twai_transmit(&message, 0); it returns 0 while with the transciver disconnected it returns 263

ESP32-S3-WROOM-1 MCN16R2: both with transciver and without the twai_transmit(&message, 0) function; it always returns 259 to me. if I load a simple click on pin 21 with the logic analyzer I see the signal correctly. if I run this sketch it doesn't give me any signal and it always responds with error 259

Before trying with another ESP to avoid it being a chip problem, are there any further tests I can do? is there much difference compared to what is due? Thank you

Sketch

#include <arduino.h>
#include "driver/twai.h"

#define RX_PIN        20
#define TX_PIN        21
//#define CAN_RS        2
#define CAN_REQST_ID  0x7DF

void setup() {
  Serial.begin(115200);
  //pinMode(CAN_RS, INPUT);     // INPUT (high impedance) = slope control mode, OUTPUT = see next line
  //digitalWrite(CAN_RS, LOW);  // LOW = high speed mode, HIGH = low power mode (listen only)
  twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL);  // TWAI_MODE_NORMAL, TWAI_MODE_NO_ACK or TWAI_MODE_LISTEN_ONLY
  twai_timing_config_t t_config  = TWAI_TIMING_CONFIG_500KBITS();
  twai_filter_config_t f_config  = TWAI_FILTER_CONFIG_ACCEPT_ALL();
  twai_driver_install(&g_config, &t_config, &f_config);
  twai_start();
}

void loop() {
  Serial.println(CANsend());
  Serial.println("Message sent!");
  delay(1000);
}

int CANsend (void) {
  twai_message_t message;
  message.identifier = CAN_REQST_ID;
  message.extd = 0;
  message.rtr = 0;
  message.data_length_code = 8;
  message.data[0] = 2;
  message.data[1] = 1;
  message.data[2] = 5;
  message.data[3] = 12;
  message.data[4] = 97;
  message.data[5] = 0;
  message.data[6] = 0;
  message.data[7] = 0; 
  return twai_transmit(&message, 0); 
}

Debug Message

verbose and without errors

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

lbernstone commented 7 months ago

I would assume you are getting an INVALID_ARG error from the driver init. Try different pins or a lower bit rate and see if you can get error 263 (TIMEOUT) instead of 259 (INVALID_STATE). Note that pin 20 is typically one of the USB pins. Are you sure your N32R8 is a WROOM-1, not WROOM-2?

mattia424 commented 6 months ago

The variability that remains is just the different part number of the chip, could it be the revision of the chip? one has the suffix M0 while the other MC.

Another variability is the loading configuration, below you will find a screen Screenshot 2023-12-04 220355

Thank you

lbernstone commented 6 months ago

Try setting USB Mode to USB-Tiny, which should leave the usb pins unattached. Did you try on different pins?

mattia424 commented 6 months ago

Hi, I solved it by finding an RS track that didn't make a false contact under the soldermask