espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.7k stars 7.29k forks source link

Connection lost to esp32s3 via USB/JTAG while using TWAI driver (IDFGH-10143) #11414

Open 5ami opened 1 year ago

5ami commented 1 year ago

Answers checklist.

IDF version.

IDF V5.0

Operating System used.

Windows

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

PowerShell

Development Kit.

esp32-s3-DevKitC-1 v1.1 & custom board

Power Supply used.

USB

What is the expected behavior?

I have custom esp32s3 board, That doesn't contain a reset and boot button or a UART port. I simply lost connection to it via USB/JTAG port while flashing a modified code of TWAI driver example using the self-test example .

To understand the issue, I tired the same code on a esp32-s3-DevKitC-1 v1.1, and it resulted in the same behavior (lost connection to it via USB/JTAG port)

I Expected the program to run and return an error log, or keeps resetting.

What is the actual behavior?

After flashing the modified (defected) software, Windows can not recognize the device giving USB device not recognized msg.

image image

The device will be recognized again if you flash the correct software through UART port. or by putting the board to download mode.

The question is, what does actually happens to prevent the board from connecting through the JTAG/USB port ?

I want to avoid this scenario in our custom board that doesn't include a UART USB port or a reset button to put the chip into download mode, hence losing all connection to custom board.

my initial guess that this due to some efuse that get emulated in way to disable the JTAG/USB port

Steps to reproduce.

  1. Compile and flash this modified version of the TWAI self-test code in esp-idf v5.0 example
  2. The modification is simply commenting out xSemaphoreTake(rx_sem, portMAX_DELAY); and xSemaphoreTake(tx_sem, portMAX_DELAY); from the original example.

/* TWAI Self Test Example

This example code is in the Public Domain (or CC0 licensed, at your option.)

Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */

include

include

include "freertos/FreeRTOS.h"

include "freertos/task.h"

include "freertos/semphr.h"

include "esp_err.h"

include "esp_log.h"

include "driver/twai.h"

/ --------------------- Definitions and static variables ------------------ /

//Example Configurations

define NO_OF_MSGS 10

define TX_GPIO_NUM 3

define RX_GPIO_NUM 8

define TX_TASK_PRIO 4 //Sending task priority

define RX_TASK_PRIO 5 //Receiving task priority

define CTRL_TSK_PRIO 6 //Control task priority

define MSG_ID 0x555 //11 bit standard format ID

define EXAMPLE_TAG "TWAI Self Test"

static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS(); //Filter all other IDs except MSG_ID static const twai_filter_config_t f_config = {.acceptance_code = (MSG_ID << 21), .acceptance_mask = ~(TWAI_STD_ID_MASK << 21), .single_filter = true}; //Set to NO_ACK mode due to self testing with single module static const twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NO_ACK);

static SemaphoreHandle_t tx_sem; static SemaphoreHandle_t rx_sem; static SemaphoreHandle_t ctrl_sem; static SemaphoreHandle_t done_sem;

/ --------------------------- Tasks and Functions -------------------------- /

static void twai_transmit_task(void *arg) { twai_message_t tx_msg = {.data_length_code = 1, .identifier = MSG_ID, .self = 1}; //xSemaphoreTake(tx_sem, portMAX_DELAY); for (int i = 0; i < NO_OF_MSGS; i++) { ESP_LOGI(EXAMPLE_TAG, "Msg Sent - Data = %d", i); //Transmit messages using self reception request tx_msg.data[0] = i; ESP_ERROR_CHECK(twai_transmit(&tx_msg, portMAX_DELAY)); vTaskDelay(pdMS_TO_TICKS(10)); } vTaskDelete(NULL); }

static void twai_receive_task(void *arg) { twai_message_t rx_message; //xSemaphoreTake(rx_sem, portMAX_DELAY); for (int i = 0; i < NO_OF_MSGS; i++) { //Receive message and print message data ESP_ERROR_CHECK(twai_receive(&rx_message, portMAX_DELAY)); ESP_LOGI(EXAMPLE_TAG, "Msg received - Data = %d", rx_message.data[0]); } //Indicate to control task all messages received for this iteration xSemaphoreGive(ctrl_sem); vTaskDelete(NULL); }

static void twai_control_task(void *arg) { xSemaphoreTake(ctrl_sem, portMAX_DELAY); //Start TWAI Driver for this iteration ESP_ERROR_CHECK(twai_start()); ESP_LOGI(EXAMPLE_TAG, "Driver started");

//Trigger TX and RX tasks to start transmitting/receiving
xSemaphoreGive(rx_sem);
xSemaphoreGive(tx_sem);
xSemaphoreTake(ctrl_sem, portMAX_DELAY);    //Wait for TX and RX tasks to finish iteration

ESP_ERROR_CHECK(twai_stop());               //Stop the TWAI Driver
ESP_LOGI(EXAMPLE_TAG, "Driver stopped");
vTaskDelay(pdMS_TO_TICKS(100));             //Delay then start next iteration

xSemaphoreGive(done_sem);
vTaskDelete(NULL);

}

void app_main(void) { //Create tasks and synchronization primitives tx_sem = xSemaphoreCreateBinary(); rx_sem = xSemaphoreCreateBinary(); ctrl_sem = xSemaphoreCreateBinary(); done_sem = xSemaphoreCreateBinary();

xTaskCreatePinnedToCore(twai_control_task, "TWAI_ctrl", 4096, NULL, CTRL_TSK_PRIO, NULL, tskNO_AFFINITY);
xTaskCreatePinnedToCore(twai_receive_task, "TWAI_rx", 4096, NULL, RX_TASK_PRIO, NULL, tskNO_AFFINITY);
xTaskCreatePinnedToCore(twai_transmit_task, "TWAI_tx", 4096, NULL, TX_TASK_PRIO, NULL, tskNO_AFFINITY);

//Install TWAI driver
ESP_ERROR_CHECK(twai_driver_install(&g_config, &t_config, &f_config));
ESP_LOGI(EXAMPLE_TAG, "Driver installed");

//Start control task
xSemaphoreGive(ctrl_sem);
//Wait for all iterations and tasks to complete running
xSemaphoreTake(done_sem, portMAX_DELAY);

//Uninstall TWAI driver
ESP_ERROR_CHECK(twai_driver_uninstall());
ESP_LOGI(EXAMPLE_TAG, "Driver uninstalled");

//Cleanup
vSemaphoreDelete(tx_sem);
vSemaphoreDelete(rx_sem);
vSemaphoreDelete(ctrl_sem);
vQueueDelete(done_sem);

}

Debug Logs.

No response

More Information.

No response

igrr commented 1 year ago

To understand the issue, I tired the same code on a esp32-s3-DevKitC-1 v1.1, and it resulted in the same behavior (lost connection to it via USB/JTAG port)

When the board enters this state, can you try to connect the USB cable to the other USB connector (CP2102) instead, and see what output you get in the logs?

5ami commented 1 year ago

Hi Igrr, I get the flowing logs image

5ami commented 1 year ago

Can this issue be because I am using pins 43 and 44 (which are UART0 TX RX ) with the CANbus transceiver ?