espressif / esp-hosted-mcu

Apache License 2.0
0 stars 0 forks source link

WiFi buffers allocation (EHM-11) #11

Closed AndyDevLat closed 2 months ago

AndyDevLat commented 2 months ago

I am confused about how and where buffers are allocated when using esp_wifi_remote component: on host or slave? For example, setting like ESP_WIFI_STATIC_RX_BUFFER_NUM is present on slave in WiFi component, but also you can edit it in menuconfig on host side in WiFi -Remote part. But looking into code i don't see where it used (if at all). Also in WiFi section there is 'Host WiFi Enable' checkbox. What it is actually doing? I did not check this box, but WiFi is still working via esp_wifi_remote component. But if i enable this option i can again set all buffers etc. This is really confusing.

mantriyogesh commented 2 months ago

Can you please let us know following:

  1. Final target host type and specification
  2. Final target host OS
  3. Current host expected to use (This might be same as 1 or may differ)
  4. Code base referring and commit
  5. How did you test? Any link that you followed?

I myself am confused, what you wish to do and exactly where did you stuck. above answers could help me in getting a better 'line of treatment'

AndyDevLat commented 2 months ago

Thank you for reply. The question was rather general, in attempt to understand how to use WiFi properly on the host via esp_hosted component. But to be more specific, my host is ESP32-P4 (ESP32-P4-Function-EV-Board) with ESP-IDF master and my slave is ESP32 (ESP32-DevKitM-1), connected via SPI. I have added esp_hosted and esp_wifi_remote components on host and everything is working (WiFi, lwIP etc). But my question is how to properly configure WiFi options (static and dynamic buffers, WPA3 etc). Should i do it on the slave side in WiFi settings? Or should i change them on host side in WiFi -Remote part of menuconfig? Or should i also enable 'Host WiFi Enable' checkbox in host (CONFIG_ESP_HOST_WIFI_ENABLED) and change all setting there? I have not found any documentation regarding CONFIG_ESP_HOST_WIFI_ENABLED and how it actually works. Currently it is not set in my sdkconfig, but esp_wifi_remote still works fine. At the end of the day, i just want to use WiFi and socket APIs on the host (ESP32-P4) via remote ESP32 board, but i don't understand how to configure WiFi options properly.

mantriyogesh commented 2 months ago

But my question is how to properly configure WiFi options (static and dynamic buffers, WPA3 etc). Should i do it on the slave side in WiFi settings?

The ESP-Hosted refers to the actual structures from esp_wifi headers from https://github.com/espressif/esp-idf/tree/master/components/esp_wifi/include

the APIs are same in signature, with exact same structure content. What I mean, there might be slight outdated headers possible at some point, wifi headers move little ahead, for small features etc, but the base structures are rock solid and Hosted uses and regularly sync them, using esp-wifi-remote.

Host on esp_wifi_set_config is going to replace the config at slave using RPC. you can do idf.py menuconfig at example project:

Screenshot 2024-09-03 at 11 38 59 PM

this would configure wifi options at host.

Regarding the component, esp-wifi-remote, you can see registry component and code at, https://github.com/espressif/esp-protocols/blob/master/components/esp_wifi_remote

To understand details how the idf components work, the heads up file is main/idf_compoinent.yml. you can learn about components, how they are loaded automatically at idf-component-manager

AndyDevLat commented 2 months ago

Host on esp_wifi_set_config is going to replace the config at slave using RPC

I understand this, but this API just sends wifi_config_t structure to slave via RPC. What i am asking is who is sending number of static RX buffers (CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM)? This is compilation constant and not part of any API, which can be forwarded to slave by RPC call. Your answer assumes that this number is set just on slave and cannot be changed by host in any way (correct me if I am wrong). But if this is so, what this setting does inside esp_wifi_remote component? I opened Kconfig file of this component and see a lot of WiFi settings, but none of them seem to be used in the source code of the component. If you change CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM on the host side - what it actually changes and what effect it has? Also you did not explain what CONFIG_ESP_HOST_WIFI_ENABLED option does - this is another place where number of static RX buffers can be changed and it is not clear if it is related to esp_hosted component or not?

mantriyogesh commented 2 months ago

CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM

API, esp_wifi_init() has param wifi_init_config_t -> static_rx_buf_num param.

So the static buffers are allocated on calling esp_wifi_init() (not compile time, if you are referring to) This is supported RPC request. Host: RPC_ID__Req_WifiInit Slave: https://github.com/espressif/esp-hosted/blob/8b3918c15ab647bc18dfcbb6e15f1cb390c99ffa/slave/main/slave_control.c#L712

This esp_wifi_init() function is called from the host example, which is higher layer for ESP-Hosted. For example, idf_station_example initialises wifi_init_config_t with CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM

I hope first question is answered.

CONFIG_ESP_HOST_WIFI_ENABLED

is not related to esp-hosted, in fact it is related to alternative solution, https://components.espressif.com/components/espressif/esp-extconn, which needs to be disabled as the symbols for wifi conflict each other.

To allow esp-hosted run smoothly, you need to comment/delete similar lines in main/idf_component.yml file of your example project: https://github.com/espressif/esp-idf/blob/6673376297b921d438790d195014b860eaf8bb17/examples/wifi/iperf/main/idf_component.yml#L10-L13

Apart from this, for high performance, we suggest to amend sdkconfig.defaults. , in this case https://github.com/espressif/esp-idf/blob/master/examples/wifi/iperf/sdkconfig.defaults.esp32p4 such that it would contain:

#### Comment below two lines if present:
# CONFIG_ESP_HOST_WIFI_ENABLED=y
# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y

#### Add Wi-Fi Remote config for better performance:
CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM=16
CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM=64
CONFIG_ESP_WIFI_DYNAMIC_TX_BUFFER_NUM=64
CONFIG_ESP_WIFI_AMPDU_TX_ENABLED=y
CONFIG_ESP_WIFI_TX_BA_WIN=32
CONFIG_ESP_WIFI_AMPDU_RX_ENABLED=y
CONFIG_ESP_WIFI_RX_BA_WIN=32

CONFIG_LWIP_TCP_SND_BUF_DEFAULT=65534
CONFIG_LWIP_TCP_WND_DEFAULT=65534
CONFIG_LWIP_TCP_RECVMBOX_SIZE=64
CONFIG_LWIP_UDP_RECVMBOX_SIZE=64
CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=64

CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_40M=y

CONFIG_LWIP_TCP_SACK_OUT=y
CONFIG_ESP_PKT_STATS=y
AndyDevLat commented 2 months ago

This esp_wifi_init() function is called

Oh, i see now what you mean - all these parameters are initialized implicitly as part of WIFI_INIT_CONFIG_DEFAULT macro:

define WIFI_INIT_CONFIG_DEFAULT() { \

.osi_funcs = &g_wifi_osi_funcs, \
.wpa_crypto_funcs = g_wifi_default_wpa_crypto_funcs, \
.static_rx_buf_num = CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM,\
.dynamic_rx_buf_num = CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM,\
.tx_buf_type = CONFIG_ESP_WIFI_TX_BUFFER_TYPE,\
.static_tx_buf_num = WIFI_STATIC_TX_BUFFER_NUM,\
.dynamic_tx_buf_num = WIFI_DYNAMIC_TX_BUFFER_NUM,\
.rx_mgmt_buf_type = CONFIG_ESP_WIFI_DYNAMIC_RX_MGMT_BUF,\
.rx_mgmt_buf_num = WIFI_RX_MGMT_BUF_NUM_DEF,\
.cache_tx_buf_num = WIFI_CACHE_TX_BUFFER_NUM,\
.csi_enable = WIFI_CSI_ENABLED,\
.ampdu_rx_enable = WIFI_AMPDU_RX_ENABLED,\
.ampdu_tx_enable = WIFI_AMPDU_TX_ENABLED,\
.amsdu_tx_enable = WIFI_AMSDU_TX_ENABLED,\
.nvs_enable = WIFI_NVS_ENABLED,\
.nano_enable = WIFI_NANO_FORMAT_ENABLED,\
.rx_ba_win = WIFI_DEFAULT_RX_BA_WIN,\
.wifi_task_core_id = WIFI_TASK_CORE_ID,\
.beacon_max_len = WIFI_SOFTAP_BEACON_MAX_LEN, \
.mgmt_sbuf_num = WIFI_MGMT_SBUF_NUM, \
.feature_caps = WIFI_FEATURE_CAPS, \
.sta_disconnected_pm = WIFI_STA_DISCONNECTED_PM_ENABLED,  \
.espnow_max_encrypt_num = CONFIG_ESP_WIFI_ESPNOW_MAX_ENCRYPT_NUM, \
.tx_hetb_queue_num = WIFI_TX_HETB_QUEUE_NUM, \
.dump_hesigb_enable = WIFI_DUMP_HESIGB_ENABLED, \
.magic = WIFI_INIT_CONFIG_MAGIC\

}

And this also means that setting these values on slave side has no effect at all, as esp_wifi_init is called there only inside req_wifi_init which calls WIFI_INIT_CONFIG_DEFAULT() too but overrides these values with RPC payload data. In fact, technically these are still compile time values on host side and they are sent to slave. But still, some WiFi parameters depend on the slave settings, eg. wpa_crypto_funcs. Also things like CONFIG_ESP_WIFI_IRAM_OPT have sense only on slave side, correct? They do not affect wifi_init_config_t, but still present in Kconfig of esp_wifi_remote and maybe should be removed? As there is nothing to optimize on host because whole WiFi driver is located on the slave. Same is about CONFIG_ESP_WIFI_WAPI_PSK, CONFIG_ESP_WIFI_SOFTAP_SUPPORT etc which affect compilation of wpa_supplicant on the slave. Maybe they must be removed from Kconfig of esp_wifi_remote to avoid confusion? Anyway, i have more clear understanding what happens under the hood now - thank you!

CONFIG_ESP_HOST_WIFI_ENABLED is not related to esp-hosted, in fact it is related to alternative solution

Oh, thank you for explanation! Now it clears my confusion.