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

riscv processor based esp32 series support(and more) #43

Open
tommasoclini opened this issue Feb 7, 2024 · 7 comments
Open

riscv processor based esp32 series support(and more) #43

tommasoclini opened this issue Feb 7, 2024 · 7 comments

Comments

@tommasoclini
Copy link

tommasoclini commented Feb 7, 2024

When trying to build for esp32c3(or any riscv processor based esp32) tiny_types.c and tiny_types.h don't include the esp32/esp32_hal.inl(or .h, I don't remember).
This can be fixed by adding to the line "#elif defined(__XTENSA__)" " || defined(__riscv)".

Should I create a new branch with this simple fix and do a pull request?

@lexus2k
Copy link
Owner

lexus2k commented Feb 10, 2024

@Tizio1234

The compilation for ESP32 C3 board is successful.
изображение

Are you using some specific board support package?

@tommasoclini
Copy link
Author

I'm not using the Arduino IDE, I'm using esp idf 5.2 directly, other than this, I get a watchdog timer timeout when calling proto.run_tx... for the second time.
Can the idf version be the cause of this?

@lexus2k
Copy link
Owner

lexus2k commented Feb 11, 2024

@Tizio1234 This could be. The library was tested with idf 3.X and 4.X, but not with the 5.X.
Do you have code example, which causes the problem?

@tommasoclini
Copy link
Author

Here's main.cpp:

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"

#include "driver/uart.h"
#include "driver/gpio.h"
#include "esp_log.h"

#include <TinyProtocol.h>

static const char* TAG = "prova tinyproto";

static const char message[] = "hello from tinyproto hdlc";

#define TINY_MULTITHREAD
#define WINDOW_SIZE (7)
#define GENERATED_PACKET_SIZE (64)
#define BUF_SIZE (512)

#define TP_UART_NUM UART_NUM_1
#define TP_UART_BAUDRATE 115200
#define TP_UART_TX GPIO_NUM_27
#define TP_UART_RX GPIO_NUM_26
#define TP_UART_BUF_SIZE 1024

#define TP_TX_BUFF_SIZE 256

tinyproto::FdD proto(tiny_fd_buffer_size_by_mtu(256, WINDOW_SIZE));

static QueueHandle_t uart_queue;

static TaskHandle_t uart_event_task_handle = NULL;
static TaskHandle_t uart_tx_task_handle = NULL;

static void uart_event_task(void *pvParameters){
    uart_event_t event;
    for (;;)
    {
        if (xQueueReceive(uart_queue, &event, portMAX_DELAY))
        {
            if (event.type == UART_DATA)
            {
                char* data = (char*)malloc(event.size);

                uart_read_bytes(TP_UART_NUM, data, event.size, portMAX_DELAY);
                
                int remaining = (int)event.size;
                int read = 0;
                while (remaining > 0)
                {
                    int tmp = proto.run_rx((void*)&(data[read]), remaining);
                    remaining -= tmp;
                    read += tmp;
                }

                free(data);
            }
        }
    }
}

static void uart_tx_task(void *pvParameters){
    void *buff = malloc(TP_TX_BUFF_SIZE);
    if (buff == NULL)
    {
        ESP_LOGE(TAG, "Failed allocating tx buff");
        return;
    }
    
    for (;;)
    {
        int size = proto.run_tx(buff, TP_TX_BUFF_SIZE);
        if (size > 0){
            uart_write_bytes(TP_UART_NUM, buff, size);
        }
        vTaskDelay(pdMS_TO_TICKS(30));
    }
}

void tp_on_receive(void *udata, uint8_t addr, tinyproto::IPacket &pkt){
    char *data = pkt.data();
    size_t size = pkt.availableBytes();

    ESP_LOGI(TAG, "Received message from tp: %.*s", size, data);
}

extern "C" void app_main(void)
{
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };
    //Install UART driver, and get the queue.
    uart_driver_install(TP_UART_NUM, TP_UART_BUF_SIZE * 2, TP_UART_BUF_SIZE * 2, 20, &uart_queue, 0);
    uart_param_config(TP_UART_NUM, &uart_config);

    //Set UART pins (using UART0 default pins ie no changes.)
    uart_set_pin(TP_UART_NUM, TP_UART_TX, TP_UART_RX, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

    proto.enableCheckSum();
    proto.setWindowSize(WINDOW_SIZE);
    proto.setReceiveCallback(tp_on_receive);
    proto.setSendTimeout(100);
    proto.begin();

    xTaskCreate(uart_event_task,
                "uart event task",
                2048,
                NULL,
                12,
                &uart_event_task_handle);
    
    xTaskCreate(uart_tx_task,
                "uart tx task",
                2048,
                NULL,
                10,
                &uart_tx_task_handle);

    sleep(1);

    while (true)
    {
        proto.write(message, sizeof(message));
        sleep(2);
    }
}

I have 2 esp32 connected by an uart with same app(I had similar output with 2 esp32c3), here's the debug uart output, it just goes on like this forever:
ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:7172
load:0x40078000,len:15532
load:0x40080400,len:4
0x40080400: _init at ??:?

load:0x40080404,len:3904
entry 0x40080640
I (29) boot: ESP-IDF v5.2-rc1-4-gcc649ea869 2nd stage bootloader
I (29) boot: compile time Feb 11 2024 22:50:00
I (30) boot: Multicore bootloader
I (35) boot: chip revision: v1.0
I (38) boot.esp32: SPI Speed : 40MHz
I (43) boot.esp32: SPI Mode : DIO
I (47) boot.esp32: SPI Flash Size : 2MB
I (52) boot: Enabling RNG early entropy source...
I (57) boot: Partition Table:
I (61) boot: ## Label Usage Type ST Offset Length
I (68) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (76) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (83) boot: 2 factory factory app 00 00 00010000 00100000
I (91) boot: End of partition table
I (95) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=0defch ( 57084) map
I (123) esp_image: segment 1: paddr=0001df24 vaddr=3ffb0000 size=020f4h ( 8436) load
I (126) esp_image: segment 2: paddr=00020020 vaddr=400d0020 size=18ebch (102076) map
I (164) esp_image: segment 3: paddr=00038ee4 vaddr=3ffb20f4 size=00180h ( 384) load
I (164) esp_image: segment 4: paddr=0003906c vaddr=40080000 size=0de20h ( 56864) load
I (199) boot: Loaded app from partition at offset 0x10000
I (199) boot: Disabling RNG early entropy source...
I (210) cpu_start: Multicore app
I (219) cpu_start: Pro cpu start user code
I (219) cpu_start: cpu freq: 160000000 Hz
I (219) cpu_start: Application information:
I (222) cpu_start: Project name: tinyproto_52
I (228) cpu_start: App version: 1
I (232) cpu_start: Compile time: Feb 11 2024 22:49:55
I (238) cpu_start: ELF file SHA256: 846e14850...
I (243) cpu_start: ESP-IDF: v5.2-rc1-4-gcc649ea869
I (250) cpu_start: Min chip rev: v0.0
I (254) cpu_start: Max chip rev: v3.99
I (259) cpu_start: Chip rev: v1.0
I (264) heap_init: Initializing. RAM available for dynamic allocation:
I (271) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (277) heap_init: At 3FFB2BA8 len 0002D458 (181 KiB): DRAM
I (283) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (290) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (296) heap_init: At 4008DE20 len 000121E0 (72 KiB): IRAM
I (304) spi_flash: detected chip: generic
I (307) spi_flash: flash io: dio
W (311) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (325) main_task: Started on CPU0
I (335) main_task: Calling app_main()
I (335) uart: queue free spaces: 20
E (21895) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
E (21895) task_wdt: - IDLE0 (CPU 0)
E (21895) task_wdt: Tasks currently running:
E (21895) task_wdt: CPU 0: uart event task
E (21895) task_wdt: CPU 1: IDLE1
E (21895) task_wdt: Print CPU 0 (current core) backtrace

Backtrace: 0x400DB32F:0x3FFB0E40 0x400DB6F4:0x3FFB0E60 0x400830B9:0x3FFB0E90 0x400D6FC0:0x3FFB8420 0x400D71E5:0x3FFB8440 0x400D6865:0x3FFB8460 0x400D5E65:0x3FFB8490 0x400D5C74:0x3FFB84B0 0x400871BD:0x3FFB84E0
0x400db32f: task_wdt_timeout_handling at /home/tommaso/esp/v5.2/esp-idf/components/esp_system/task_wdt/task_wdt.c:441
0x400db6f4: task_wdt_isr at /home/tommaso/esp/v5.2/esp-idf/components/esp_system/task_wdt/task_wdt.c:515
0x400830b9: _xt_lowint1 at /home/tommaso/esp/v5.2/esp-idf/components/xtensa/xtensa_vectors.S:1240
0x400d6fc0: hdlc_ll_read_data at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/components/tinyproto/src/proto/hdlc/low_level/hdlc.c:406
0x400d71e5: hdlc_ll_run_rx at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/components/tinyproto/src/proto/hdlc/low_level/hdlc.c:516
0x400d6865: tiny_fd_on_rx_data at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/components/tinyproto/src/proto/fd/tiny_fd.c:841
0x400d5e65: tinyproto::IFd::run_rx(void const*, int) at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/components/tinyproto/src/TinyProtocolFd.cpp:97
0x400d5c74: uart_event_task(void*) at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/main/main.cpp:55
0x400871bd: vPortTaskWrapper at /home/tommaso/esp/v5.2/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:134

E (26895) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
E (26895) task_wdt: - IDLE0 (CPU 0)
E (26895) task_wdt: Tasks currently running:
E (26895) task_wdt: CPU 0: uart event task
E (26895) task_wdt: CPU 1: IDLE1
E (26895) task_wdt: Print CPU 0 (current core) backtrace

Backtrace: 0x400DB32F:0x3FFB0E40 0x400DB6F4:0x3FFB0E60 0x400830B9:0x3FFB0E90 0x400D7091:0x3FFB8420 0x400D71E5:0x3FFB8440 0x400D6865:0x3FFB8460 0x400D5E65:0x3FFB8490 0x400D5C74:0x3FFB84B0 0x400871BD:0x3FFB84E0
0x400db32f: task_wdt_timeout_handling at /home/tommaso/esp/v5.2/esp-idf/components/esp_system/task_wdt/task_wdt.c:441
0x400db6f4: task_wdt_isr at /home/tommaso/esp/v5.2/esp-idf/components/esp_system/task_wdt/task_wdt.c:515
0x400830b9: _xt_lowint1 at /home/tommaso/esp/v5.2/esp-idf/components/xtensa/xtensa_vectors.S:1240
0x400d7091: hdlc_ll_read_end at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/components/tinyproto/src/proto/hdlc/low_level/hdlc.c:447
0x400d71e5: hdlc_ll_run_rx at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/components/tinyproto/src/proto/hdlc/low_level/hdlc.c:516
0x400d6865: tiny_fd_on_rx_data at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/components/tinyproto/src/proto/fd/tiny_fd.c:841
0x400d5e65: tinyproto::IFd::run_rx(void const*, int) at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/components/tinyproto/src/TinyProtocolFd.cpp:97
0x400d5c74: uart_event_task(void*) at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/main/main.cpp:55
0x400871bd: vPortTaskWrapper at /home/tommaso/esp/v5.2/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:134

Even the generator example has this kind of issue, I haven't tested the loopback example though

@tommasoclini tommasoclini changed the title riscv processor based esp32 series support riscv processor based esp32 series support(and more) Feb 11, 2024
@lexus2k
Copy link
Owner

lexus2k commented Feb 18, 2024

In the library there are no blocking calls at hdlc_ll_run_rx at /home/tommaso/esp/workspaces/tinyproto_ws/tinyproto_52/components/tinyproto/src/proto/hdlc/low_level/hdlc.c:516

The only place to lock up is inside tiny_fd_on_rx_data, which calls hdlc_ll_run_rx in the loop. It will exit only if all bytes are processed. It may lock if negative number of bytes is passed.

Could you please try the code from cpp_api branch? Does it give the same result?

@tommasoclini
Copy link
Author

I'm getting the same result even when using cpp_api branch

@lexus2k
Copy link
Owner

lexus2k commented Feb 24, 2024

@Tizio1234

-DTINY_HDLC_DEBUG=1, -DTINY_FD_DEBUG=1, -DTINY_DEBUG=1, -DTINY_LOG_LEVEL_DEFAULT=5 could help to find a root cause.
The FD communication requires both tx and rx threads to run continuously. In your case you use vTaskDelay(pdMS_TO_TICKS(30)); to make a task have a sleep.

There are 2 modes of the library: Single thread mode and Multithread mode.
In single thread mode, the main loop / or main task should execute run_rx(), run_tx() sequentially in the loop.
In multithread mode, you need to have one rx task, and one tx task, calling functions in the loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants