esp32c3 tcp的发送速度很慢

shiyu1024
Posts: 6
Joined: Fri Dec 29, 2023 1:48 am

esp32c3 tcp的发送速度很慢

Postby shiyu1024 » Sat Mar 16, 2024 9:16 am

我现在使用esp32c3的ap模式,通过spi口接收数据,然后把数据通过wifi tcp连接发送给电脑,我使用examples/wifi/iperf例子测速发现速度可以达到15Mbits/sec。我从spi口接收数据后直接放到Ringbuffer中,然后在另一个线程中读Ringbuffer通过wifi发送,但spi接收速度最高只能120KByte/sec,否则Ringbuffer会爆。我按文档(https://docs.espressif.com/projects/esp ... erformance)中说的修改过一些参数,但完全没有作用,下面的我的代码,请帮我看下有没有问题。谢谢

Code: Select all

void spi_slave_task(void *pvParameters)
{
    esp_err_t ret;

    spi_slave_transaction_t t;
    memset(&t, 0, sizeof(t));

    uint8_t *recvbuf = (uint8_t *)malloc(SPI_RX_MAX * sizeof(uint8_t));
    if(recvbuf == NULL)
    {
        ESP_LOGE(SPI_SLAVE, "Receive buffer memory allocation failed!");
        return;
    }

    while(1) {
        memset(recvbuf, 0x00, SPI_RX_MAX);

        //Set up a transaction of 128 bytes to send/receive
        t.length=SPI_RX_MAX*8;
        t.rx_buffer=recvbuf;

        ret=spi_slave_transmit(RCV_HOST, &t, portMAX_DELAY);

        UBaseType_t res = xRingbufferSend(buf_handle, recvbuf, t.trans_len/8, pdMS_TO_TICKS(10));
        if (res != pdTRUE) {
            ESP_LOGE(SPI_SLAVE, "Failed to send item\n");
        }
    }
}

void wifi_send_task(void *pvParameters)
{
    while(1) {
        size_t item_size;
        char *item = (char *)xRingbufferReceiveUpTo(buf_handle, &item_size, pdMS_TO_TICKS(10), SPI_RX_MAX*2);
        if (item != NULL) {
            // 如果网络连接,通过网络发送数
            if(tcp_is_connect == true) {
                int written = send(tcp_sock, item, item_size, 0);
                if (written < 0) {
                    ESP_LOGE(SPI_SLAVE, "Error occurred during sending: errno %d", errno);
                }
            }
            vRingbufferReturnItem(buf_handle, (void *)item);
        }
    }
}

void spi_slave_task_init(void)
{
    buf_handle = xRingbufferCreate(64*1024, RINGBUF_TYPE_BYTEBUF);
    if (buf_handle == NULL) {
        ESP_LOGE(SPI_SLAVE, "Failed to create ring buffer\n");
    }
    spi_slave_init();
    xTaskCreate(spi_slave_task, "spi_slave", 2048, NULL, 5, NULL);
    xTaskCreate(wifi_send_task, "wifi_send", 2048, NULL, 6, NULL);
}

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: esp32c3 tcp的发送速度很慢

Postby liaifat85 » Sat Mar 16, 2024 1:53 pm

spi_slave_task中使用了spi_slave_transmit函数来接收数据。这个函数在接收完成后会阻塞线程,直到接收到数据为止。这可能会导致你的程序在接收速度较慢时出现问题,因为它会一直等待数据到达,而不会处理其他任务。你可以尝试使用spi_slave_recv函数,它可以非阻塞地接收数据。

shiyu1024
Posts: 6
Joined: Fri Dec 29, 2023 1:48 am

Re: esp32c3 tcp的发送速度很慢

Postby shiyu1024 » Tue Mar 19, 2024 2:29 am

spi的时钟是20M的,spi每35ms接收4096字节的数据,这样spi_slave_transmit只会阻塞约2ms,应该不会影响tcp发送吧?我先改成spi_slave_recv试一下,多谢

Who is online

Users browsing this forum: No registered users and 62 guests