You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2024/05/14 14:10:55 UTC
(nuttx) 03/06: esp32c6/wifi: Add support for the Wi-Fi in ESP32-C6
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 2c4963dcd6f9785d6d3e5b327e22fd995cbc81a7
Author: Tiago Medicci Serrano <ti...@espressif.com>
AuthorDate: Fri Apr 5 13:59:49 2024 -0300
esp32c6/wifi: Add support for the Wi-Fi in ESP32-C6
This commit introduces support for both station and softAP modes.
---
.../esp32c6/boards/esp32c6-devkitc/index.rst | 41 +
.../esp32c6/boards/esp32c6-devkitm/index.rst | 41 +
Documentation/platforms/risc-v/esp32c6/index.rst | 2 +-
arch/risc-v/src/common/espressif/Kconfig | 258 +
arch/risc-v/src/common/espressif/Make.defs | 60 +-
arch/risc-v/src/common/espressif/Wireless.mk | 245 +
.../src/common/espressif/esp_ets_timer_legacy.c | 263 +
arch/risc-v/src/common/espressif/esp_hr_timer.c | 17 +-
arch/risc-v/src/common/espressif/esp_irq.c | 53 +-
arch/risc-v/src/common/espressif/esp_irq.h | 39 +
arch/risc-v/src/common/espressif/esp_rtc.c | 6 +-
arch/risc-v/src/common/espressif/esp_start.c | 25 +
arch/risc-v/src/common/espressif/esp_wifi_init.c | 465 ++
arch/risc-v/src/common/espressif/esp_wifi_utils.c | 654 +++
arch/risc-v/src/common/espressif/esp_wifi_utils.h | 120 +
arch/risc-v/src/common/espressif/esp_wlan.c | 1731 ++++++
arch/risc-v/src/common/espressif/esp_wlan.h | 177 +
arch/risc-v/src/esp32c6/Make.defs | 7 +
arch/risc-v/src/esp32c6/esp_coex_adapter.c | 578 ++
arch/risc-v/src/esp32c6/esp_wifi_adapter.c | 5569 ++++++++++++++++++++
arch/risc-v/src/esp32c6/esp_wifi_adapter.h | 672 +++
arch/risc-v/src/esp32c6/hal_esp32c6.mk | 265 +-
.../risc-v/esp32c6/common/include/esp_board_wlan.h | 73 +
.../esp32c6/common/scripts/esp32c6_aliases.ld | 1 +
.../common/scripts/esp32c6_simple_boot_sections.ld | 36 +
boards/risc-v/esp32c6/common/src/Make.defs | 4 +
boards/risc-v/esp32c6/common/src/esp_board_wlan.c | 81 +
.../esp32c6-devkitc/configs/sta_softap/defconfig | 84 +
.../esp32c6/esp32c6-devkitc/configs/wifi/defconfig | 80 +
.../esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c | 22 +
.../esp32c6-devkitm/configs/sta_softap/defconfig | 84 +
.../esp32c6/esp32c6-devkitm/configs/wifi/defconfig | 84 +
.../esp32c6/esp32c6-devkitm/src/esp32c6_bringup.c | 22 +
33 files changed, 11700 insertions(+), 159 deletions(-)
diff --git a/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst b/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst
index c83f58d341..af7f93a0f6 100644
--- a/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst
+++ b/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst
@@ -197,6 +197,26 @@ Once booted you can use the following commands to mount the file system::
nsh> mksmartfs /dev/smart0
nsh> mount -t smartfs /dev/smart0 /mnt
+sta_softap
+----------
+
+With this configuration you can run these commands to be able
+to connect your smartphone or laptop to your board::
+
+ nsh> ifup wlan1
+ nsh> dhcpd_start wlan1
+ nsh> wapi psk wlan1 mypasswd 3
+ nsh> wapi essid wlan1 nuttxap 1
+
+In this case, you are creating the access point ``nuttxapp`` in your board and to
+connect to it on your smartphone you will be required to type the password ``mypasswd``
+using WPA2.
+
+.. tip:: Please refer to :ref:`ESP32 Wi-Fi SoftAP Mode <esp32_wi-fi_softap>`
+ for more information.
+
+The ``dhcpd_start`` is necessary to let your board to associate an IP to your smartphone.
+
timer
-----
@@ -255,3 +275,24 @@ To test it, just run the following command::
nsh> wdog -i /dev/watchdogX
Where X is the watchdog instance.
+
+wifi
+----
+
+Enables Wi-Fi support. You can define your credentials this way::
+
+ $ make menuconfig
+ -> Application Configuration
+ -> Network Utilities
+ -> Network initialization (NETUTILS_NETINIT [=y])
+ -> WAPI Configuration
+
+Or if you don't want to keep it saved in the firmware you can do it
+at runtime::
+
+ nsh> wapi psk wlan0 mypasswd 3
+ nsh> wapi essid wlan0 myssid 1
+ nsh> renew wlan0
+
+.. tip:: Please refer to :ref:`ESP32 Wi-Fi Station Mode <esp32_wi-fi_sta>`
+ for more information.
diff --git a/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitm/index.rst b/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitm/index.rst
index 3833abc964..6887ad7045 100644
--- a/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitm/index.rst
+++ b/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitm/index.rst
@@ -197,6 +197,26 @@ Once booted you can use the following commands to mount the file system::
nsh> mksmartfs /dev/smart0
nsh> mount -t smartfs /dev/smart0 /mnt
+sta_softap
+----------
+
+With this configuration you can run these commands to be able
+to connect your smartphone or laptop to your board::
+
+ nsh> ifup wlan1
+ nsh> dhcpd_start wlan1
+ nsh> wapi psk wlan1 mypasswd 3
+ nsh> wapi essid wlan1 nuttxap 1
+
+In this case, you are creating the access point ``nuttxapp`` in your board and to
+connect to it on your smartphone you will be required to type the password ``mypasswd``
+using WPA2.
+
+.. tip:: Please refer to :ref:`ESP32 Wi-Fi SoftAP Mode <esp32_wi-fi_softap>`
+ for more information.
+
+The ``dhcpd_start`` is necessary to let your board to associate an IP to your smartphone.
+
timer
-----
@@ -255,3 +275,24 @@ To test it, just run the following command::
nsh> wdog -i /dev/watchdogX
Where X is the watchdog instance.
+
+wifi
+----
+
+Enables Wi-Fi support. You can define your credentials this way::
+
+ $ make menuconfig
+ -> Application Configuration
+ -> Network Utilities
+ -> Network initialization (NETUTILS_NETINIT [=y])
+ -> WAPI Configuration
+
+Or if you don't want to keep it saved in the firmware you can do it
+at runtime::
+
+ nsh> wapi psk wlan0 mypasswd 3
+ nsh> wapi essid wlan0 myssid 1
+ nsh> renew wlan0
+
+.. tip:: Please refer to :ref:`ESP32 Wi-Fi Station Mode <esp32_wi-fi_sta>`
+ for more information.
diff --git a/Documentation/platforms/risc-v/esp32c6/index.rst b/Documentation/platforms/risc-v/esp32c6/index.rst
index 49d008b25e..f09f1797df 100644
--- a/Documentation/platforms/risc-v/esp32c6/index.rst
+++ b/Documentation/platforms/risc-v/esp32c6/index.rst
@@ -163,7 +163,7 @@ SPIFLASH Yes
Timers Yes
UART Yes
Watchdog Yes
-Wifi No
+Wifi Yes
XTS No
============== =======
diff --git a/arch/risc-v/src/common/espressif/Kconfig b/arch/risc-v/src/common/espressif/Kconfig
index 0273b4a5c3..f247b485c5 100644
--- a/arch/risc-v/src/common/espressif/Kconfig
+++ b/arch/risc-v/src/common/espressif/Kconfig
@@ -339,8 +339,266 @@ config ESP_RMT
format, RMT can be extended to a versatile and general-purpose
transceiver, transmitting or receiving many other types of signals.
+config ESP_WIRELESS
+ bool
+ default n
+ select NET
+ select ARCH_PHY_INTERRUPT
+ select RTC
+ select RTC_DRIVER
+ select ESPRESSIF_HR_TIMER
+
+config ESPRESSIF_WIFI
+ bool "Wi-Fi"
+ depends on ESPRESSIF_ESP32C6
+ default n
+ select ESP_WIRELESS
+ ---help---
+ Enable Wi-Fi support
+
+config ESP_COEX_SW_COEXIST_ENABLE
+ bool "Software WiFi/Bluetooth/IEEE 802.15.4 coexistence"
+ depends on (ESPRESSIF_WIFI && ESPRESSIF_BLE) || \
+ (ESPRESSIF_WIFI && ESP_IEEE802154) || \
+ (ESP_IEEE802154 && ESPRESSIF_BLE)
+ default y
+ ---help---
+ If enabled, WiFi & Bluetooth coexistence is controlled by software rather than hardware.
+ Recommended for heavy traffic scenarios. Both coexistence configuration options are
+ automatically managed, no user intervention is required.
+ If only Bluetooth is used, it is recommended to disable this option to reduce binary file
+ size.
+
endmenu # Peripheral Support
+menu "Wi-Fi Configuration"
+ depends on ESPRESSIF_WIFI
+
+menu "ESP WPA-Supplicant"
+
+config WPA_WAPI_PSK
+ bool "Enable WAPI PSK support"
+ default n
+ ---help---
+ Select this option to enable WAPI-PSK
+ which is a Chinese National Standard Encryption for Wireless LANs (GB 15629.11-2003).
+
+config WPA_SUITE_B_192
+ bool "Enable NSA suite B support with 192-bit key"
+ default n
+ select ESPRESSIF_WIFI_GCMP_SUPPORT
+ select ESPRESSIF_WIFI_GMAC_SUPPORT
+ ---help---
+ Select this option to enable 192-bit NSA suite-B.
+ This is necessary to support WPA3 192-bit security.
+
+config ESP_WPA_DEBUG_PRINT
+ bool "Print debug messages from Espressif's WPA Supplicant"
+ default n
+ ---help---
+ Select this option to print logging information from WPA supplicant,
+ this includes handshake information and key hex dumps depending
+ on the project logging level.
+
+ Enabling this could increase the build size ~60kb
+ depending on the project logging level.
+
+endmenu # ESP WPA-Supplicant
+
+choice ESPRESSIF_WIFI_MODE
+ prompt "ESP Wi-Fi mode"
+ default ESPRESSIF_WIFI_STATION
+
+config ESPRESSIF_WIFI_STATION
+ bool "Station mode"
+
+config ESPRESSIF_WIFI_SOFTAP
+ bool "SoftAP mode"
+
+config ESPRESSIF_WIFI_STATION_SOFTAP
+ bool "Station + SoftAP"
+
+endchoice # ESPRESSIF_WIFI_MODE
+
+config ESPRESSIF_WIFI_ENABLE_WPA3_SAE
+ bool "Enable WPA3-Personal"
+ default y
+ ---help---
+ Select this option to allow the device to establish a WPA3-Personal connection with eligible AP's.
+ PMF (Protected Management Frames) is a prerequisite feature for a WPA3 connection, it needs to be
+ explicitly configured before attempting connection. Please refer to the Wi-Fi Driver API Guide for details.
+
+config ESPRESSIF_WIFI_ENABLE_SAE_PK
+ bool "Enable SAE-PK"
+ depends on ESPRESSIF_WIFI_ENABLE_WPA3_SAE
+ default y
+ ---help---
+ Select this option to enable SAE-PK
+
+config ESPRESSIF_WIFI_SOFTAP_SAE_SUPPORT
+ bool "Enable WPA3 Personal(SAE) SoftAP"
+ default y
+ depends on ESPRESSIF_WIFI_ENABLE_WPA3_SAE
+ depends on ESPRESSIF_WIFI_SOFTAP || ESPRESSIF_WIFI_STATION_SOFTAP
+ ---help---
+ Select this option to enable SAE support in softAP mode.
+
+config ESPRESSIF_WIFI_ENABLE_WPA3_OWE_STA
+ bool "Enable OWE STA"
+ default y
+ ---help---
+ Select this option to allow the device to establish OWE connection with eligible AP's.
+ PMF (Protected Management Frames) is a prerequisite feature for a WPA3 connection, it needs to be
+ explicitly configured before attempting connection. Please refer to the Wi-Fi Driver API Guide for details.
+
+config ESPRESSIF_WIFI_STATIC_RX_BUFFER_NUM
+ int "Max number of WiFi static RX buffers"
+ range 2 25 if !ESPRESSIF_ESP32C6
+ range 2 128 if ESPRESSIF_ESP32C6
+ default 10
+ ---help---
+ Set the number of WiFi static RX buffers. Each buffer takes approximately 1.6KB of RAM.
+ The static rx buffers are allocated when esp_wifi_init is called, they are not freed
+ until esp_wifi_deinit is called.
+
+ WiFi hardware use these buffers to receive all 802.11 frames.
+ A higher number may allow higher throughput but increases memory use. If ESPRESSIF_WIFI_AMPDU_RX_ENABLED
+ is enabled, this value is recommended to set equal or bigger than ESPRESSIF_WIFI_RX_BA_WIN in order to
+ achieve better throughput and compatibility with both stations and APs.
+
+config ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM
+ int "Max number of WiFi dynamic RX buffers"
+ default 32
+ ---help---
+ Set the number of WiFi dynamic RX buffers, 0 means unlimited RX buffers will be allocated
+ (provided sufficient free RAM). The size of each dynamic RX buffer depends on the size of
+ the received data frame.
+
+ For each received data frame, the WiFi driver makes a copy to an RX buffer and then delivers
+ it to the high layer TCP/IP stack. The dynamic RX buffer is freed after the higher layer has
+ successfully received the data frame.
+
+ For some applications, WiFi data frames may be received faster than the application can
+ process them. In these cases we may run out of memory if RX buffer number is unlimited (0).
+
+ If a dynamic RX buffer limit is set, it should be at least the number of static RX buffers.
+
+config ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER_NUM
+ int "Max number of WiFi dynamic TX buffers"
+ range 1 128
+ default 32
+ ---help---
+ Set the number of WiFi dynamic TX buffers. The size of each dynamic TX buffer is not fixed,
+ it depends on the size of each transmitted data frame.
+
+ For each transmitted frame from the higher layer TCP/IP stack, the WiFi driver makes a copy
+ of it in a TX buffer. For some applications, especially UDP applications, the upper layer
+ can deliver frames faster than WiFi layer can transmit. In these cases, we may run out of TX
+ buffers.
+
+config ESPRESSIF_WIFI_AMPDU_TX_ENABLED
+ bool "Wi-Fi TX AMPDU"
+ default y
+ ---help---
+ Select this option to enable AMPDU TX feature
+
+config ESPRESSIF_WIFI_TX_BA_WIN
+ int "WiFi AMPDU TX BA window size"
+ depends on ESPRESSIF_WIFI_AMPDU_TX_ENABLED
+ range 2 32 if !ESPRESSIF_ESP32C6
+ range 2 64 if ESPRESSIF_ESP32C6
+ default 6
+
+config ESPRESSIF_WIFI_AMPDU_RX_ENABLED
+ bool "WiFi AMPDU RX"
+ default y
+ ---help---
+ Select this option to enable AMPDU RX feature
+
+config ESPRESSIF_WIFI_RX_BA_WIN
+ int "WiFi AMPDU RX BA window size"
+ depends on ESPRESSIF_WIFI_AMPDU_RX_ENABLED
+ range 2 32 if !ESPRESSIF_ESP32C6
+ range 2 64 if ESPRESSIF_ESP32C6
+ default 6
+ ---help---
+ Set the size of WiFi Block Ack RX window. Generally a bigger value means higher throughput and better
+ compatibility but more memory. Most of time we should NOT change the default value unless special
+ reason, e.g. test the maximum UDP RX throughput with iperf etc. For iperf test in shieldbox, the
+ recommended value is 9~12. If PSRAM is used and WiFi memory is prefered to allocat in PSRAM first,
+ the default and minimum value should be 16 to achieve better throughput and compatibility with both
+ stations and APs.
+
+config ESPRESSIF_WIFI_GCMP_SUPPORT
+ bool "WiFi GCMP Support(GCMP128 and GCMP256)"
+ default n
+ ---help---
+ Select this option to enable GCMP support. GCMP support is compulsory for WiFi Suite-B support.
+
+config ESPRESSIF_WIFI_GMAC_SUPPORT
+ bool "WiFi GMAC Support(GMAC128 and GMAC256)"
+ default n
+ ---help---
+ Select this option to enable GMAC support. GMAC support is compulsory for WiFi 192-bit certification.
+
+config ESPRESSIF_WIFI_CONNECT_TIMEOUT
+ int "Connect timeout in second"
+ default 10
+ ---help---
+ Max waiting time of connecting to AP.
+
+config ESPRESSIF_WIFI_SCAN_RESULT_SIZE
+ int "Scan result buffer"
+ default 4096
+ ---help---
+ Maximum scan result buffer size.
+
+config ESPRESSIF_WIFI_STA_DISCONNECT_PM
+ bool "Power Management for station when disconnected"
+ default y
+ ---help---
+ Select this option to enable power management for station when disconnected.
+ Chip will do modem-sleep when RF module is not in use anymore.
+
+choice ESP_POWER_SAVE_MODE
+ prompt "Wi-Fi Power save mode"
+ default ESP_POWER_SAVE_MIN_MODEM if ESPRESSIF_WIFI_BT_COEXIST
+ default ESP_POWER_SAVE_NONE
+ ---help---
+ Wi-Fi supports the Modem-sleep mode which refers to the legacy power-saving mode in the IEEE 802.11 protocol.
+ Modem-sleep mode works in station-only mode and the station must connect to the AP first. If the Modem-sleep
+ mode is enabled, station will switch between active and sleep state periodically. In sleep state, RF, PHY and
+ BB are turned off in order to reduce power consumption. Station can keep connection with AP in modem-sleep mode.
+
+ Modem-sleep mode includes minimum and maximum power-saving modes.
+
+ In minimum power-saving mode, station wakes
+ up every DTIM to receive beacon. Broadcast data will not be lost because it is transmitted after DTIM.
+ However, it cannot save much more power if DTIM is short for DTIM is determined by AP.
+
+ In maximum power-saving mode, station wakes up in every listen interval to receive beacon. This listen interval
+ can be set to be longer than the AP DTIM period. Broadcast data may be lost because station may be in sleep
+ state at DTIM time. If listen interval is longer, more power is saved, but broadcast data is more easy to lose.
+ Listen interval can be configured by setting ESPRESSIF_WIFI_LISTEN_INTERVAL.
+
+ ESP_POWER_SAVE_NONE disables Modem-sleep mode entirely. Disabling it increases power consumption, but
+ minimizes the delay in receiving Wi-Fi data in real time. When Modem-sleep mode is enabled, the delay in
+ receiving Wi-Fi data may be the same as the DTIM cycle (minimum power-saving mode) or the listening interval
+ (maximum power-saving mode). Setting ESP_POWER_SAVE_NONE is suitable when high throughput is required.
+
+config ESP_POWER_SAVE_NONE
+ bool "No power save"
+
+config ESP_POWER_SAVE_MIN_MODEM
+ bool "Minimum modem power saving."
+
+config ESP_POWER_SAVE_MAX_MODEM
+ bool "Maximum modem power saving"
+
+endchoice # ESP_POWER_SAVE_MODE
+
+endmenu # ESPRESSIF_WIFI
+
menu "UART Configuration"
depends on ESPRESSIF_UART
diff --git a/arch/risc-v/src/common/espressif/Make.defs b/arch/risc-v/src/common/espressif/Make.defs
index 21cc779d94..7e58bf3d4e 100644
--- a/arch/risc-v/src/common/espressif/Make.defs
+++ b/arch/risc-v/src/common/espressif/Make.defs
@@ -36,59 +36,67 @@ CHIP_CSRCS += esp_lowputc.c esp_serial.c
CHIP_CSRCS += esp_systemreset.c
ifeq ($(CONFIG_SCHED_TICKLESS),y)
- CHIP_CSRCS += esp_tickless.c
+ CHIP_CSRCS += esp_tickless.c
else
- CHIP_CSRCS += esp_timerisr.c
+ CHIP_CSRCS += esp_timerisr.c
endif
ifeq ($(CONFIG_WATCHDOG),y)
- CHIP_CSRCS += esp_wdt.c
+ CHIP_CSRCS += esp_wdt.c
endif
ifneq ($(CONFIG_DEV_RANDOM)$(CONFIG_DEV_URANDOM_ARCH),)
- CHIP_CSRCS += esp_random.c
+ CHIP_CSRCS += esp_random.c
endif
ifeq ($(CONFIG_TIMER),y)
- CHIP_CSRCS += esp_timer.c
+ CHIP_CSRCS += esp_timer.c
endif
ifeq ($(CONFIG_ONESHOT),y)
- CHIP_CSRCS += esp_oneshot.c
+ CHIP_CSRCS += esp_oneshot.c
endif
ifeq ($(CONFIG_RTC),y)
- CHIP_CSRCS += esp_rtc.c
+ CHIP_CSRCS += esp_rtc.c
endif
ifeq ($(CONFIG_ESPRESSIF_HR_TIMER),y)
- CHIP_CSRCS += esp_hr_timer.c
+ CHIP_CSRCS += esp_hr_timer.c esp_ets_timer_legacy.c
endif
ifeq ($(CONFIG_ESPRESSIF_TWAI),y)
- CHIP_CSRCS += esp_twai.c
+ CHIP_CSRCS += esp_twai.c
endif
ifeq ($(CONFIG_ESPRESSIF_LEDC),y)
- CHIP_CSRCS += esp_ledc.c
+ CHIP_CSRCS += esp_ledc.c
endif
ifeq ($(CONFIG_ESPRESSIF_USBSERIAL),y)
- CHIP_CSRCS += esp_usbserial.c
+ CHIP_CSRCS += esp_usbserial.c
endif
ifeq ($(CONFIG_ESP_RMT),y)
- CHIP_CSRCS += esp_rmt.c
- ifeq ($(CONFIG_WS2812_NON_SPI_DRIVER),y)
- CHIP_CSRCS += esp_ws2812.c
- endif
+ CHIP_CSRCS += esp_rmt.c
+ ifeq ($(CONFIG_WS2812_NON_SPI_DRIVER),y)
+ CHIP_CSRCS += esp_ws2812.c
+ endif
endif
ifeq ($(CONFIG_ESPRESSIF_SPIFLASH),y)
- CHIP_CSRCS += esp_spiflash.c
- ifeq ($(CONFIG_ESPRESSIF_MTD),y)
- CHIP_CSRCS += esp_spiflash_mtd.c
- endif
+ CHIP_CSRCS += esp_spiflash.c
+ ifeq ($(CONFIG_ESPRESSIF_MTD),y)
+ CHIP_CSRCS += esp_spiflash_mtd.c
+ endif
+endif
+
+ifeq ($(CONFIG_ESP_WIRELESS),y)
+ ifeq ($(CONFIG_ESPRESSIF_WIFI),y)
+ CHIP_CSRCS += esp_wifi_init.c
+ CHIP_CSRCS += esp_wlan.c
+ CHIP_CSRCS += esp_wifi_utils.c
+ endif
endif
#############################################################################
@@ -99,7 +107,7 @@ endif
ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty
ifndef ESP_HAL_3RDPARTY_VERSION
- ESP_HAL_3RDPARTY_VERSION = 54aca80daef2002b0169e67bcde36f19f59cf6cf
+ ESP_HAL_3RDPARTY_VERSION = 581540ed0a5ce9674da44f9ece6a63d052058985
endif
ifndef ESP_HAL_3RDPARTY_URL
@@ -111,6 +119,13 @@ chip/$(ESP_HAL_3RDPARTY_REPO):
$(Q) git clone --quiet $(ESP_HAL_3RDPARTY_URL) chip/$(ESP_HAL_3RDPARTY_REPO)
$(Q) echo "Espressif HAL for 3rd Party Platforms: ${ESP_HAL_3RDPARTY_VERSION}"
$(Q) git -C chip/$(ESP_HAL_3RDPARTY_REPO) checkout --quiet $(ESP_HAL_3RDPARTY_VERSION)
+ifeq ($(CONFIG_ESP_WIRELESS),y)
+ $(Q) echo "Espressif HAL for 3rd Party Platforms: initializing submodules..."
+ $(Q) git -C chip/$(ESP_HAL_3RDPARTY_REPO) submodule --quiet update --init --depth=1 components/mbedtls/mbedtls components/esp_phy/lib components/esp_wifi/lib components/esp_coex/lib
+ $(Q) git -C chip/$(ESP_HAL_3RDPARTY_REPO)/components/mbedtls/mbedtls reset --quiet --hard
+ $(Q) echo "Applying patches..."
+ $(Q) cd chip/$(ESP_HAL_3RDPARTY_REPO)/components/mbedtls/mbedtls && git apply ../../../nuttx/patches/components/mbedtls/mbedtls/*.patch
+endif
include common$(DELIM)espressif$(DELIM)Bootloader.mk
@@ -124,6 +139,10 @@ CHIP_SERIES = $(patsubst "%",%,$(CONFIG_ESPRESSIF_CHIP_SERIES))
include chip/hal_${CHIP_SERIES}.mk
+ifeq ($(CONFIG_ESP_WIRELESS),y)
+ include common$(DELIM)espressif$(DELIM)Wireless.mk
+endif
+
context:: chip/$(ESP_HAL_3RDPARTY_REPO)
$(call COPYFILE,chip/$(ESP_HAL_3RDPARTY_REPO)/components/soc/$(CHIP_SERIES)/include/soc/gpio_sig_map.h,../include/chip/)
$(call COPYFILE,chip/$(ESP_HAL_3RDPARTY_REPO)/nuttx/$(CHIP_SERIES)/include/irq.h,../include/chip/)
@@ -134,4 +153,5 @@ distclean::
$(call DELDIR,chip/$(ESP_HAL_3RDPARTY_REPO))
INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)common$(DELIM)espressif
+INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)common$(DELIM)espressif$(DELIM)platform_include
VPATH += common$(DELIM)espressif
diff --git a/arch/risc-v/src/common/espressif/Wireless.mk b/arch/risc-v/src/common/espressif/Wireless.mk
new file mode 100644
index 0000000000..a27de857bd
--- /dev/null
+++ b/arch/risc-v/src/common/espressif/Wireless.mk
@@ -0,0 +1,245 @@
+############################################################################
+# arch/risc-v/src/common/espressif/Wireless.mk
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bt$(DELIM)include$(DELIM)$(CHIP_SERIES)$(DELIM)include
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_coex$(DELIM)include
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)include
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)nuttx$(DELIM)$(CHIP_SERIES)$(DELIM)include
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)nuttx$(DELIM)include$(DELIM)esp_wifi
+
+EXTRA_LIBPATHS += -L $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_coex$(DELIM)lib$(DELIM)$(CHIP_SERIES)
+EXTRA_LIBPATHS += -L $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_phy$(DELIM)lib$(DELIM)$(CHIP_SERIES)
+EXTRA_LIBPATHS += -L $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_wifi$(DELIM)lib$(DELIM)$(CHIP_SERIES)
+
+EXTRA_LIBS += -lphy -lcoexist
+ifeq ($(CHIP_SERIES),esp32c6)
+EXTRA_LIBS += -lmesh
+endif
+
+ifeq ($(CONFIG_ESPRESSIF_WIFI),y)
+
+ifeq ($(CONFIG_WPA_WAPI_PSK),y)
+EXTRA_LIBS += -lwapi
+endif
+
+## ESP-IDF's mbedTLS
+
+VPATH += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)mbedtls$(DELIM)mbedtls$(DELIM)library
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)mbedtls$(DELIM)mbedtls$(DELIM)include
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)mbedtls$(DELIM)mbedtls$(DELIM)library
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)mbedtls$(DELIM)port$(DELIM)include
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)nuttx$(DELIM)include$(DELIM)mbedtls
+
+### Define Espressif's configs for mbedTLS
+
+CFLAGS += $(DEFINE_PREFIX)MBEDTLS_CONFIG_FILE="<mbedtls/esp_config.h>"
+
+CHIP_CSRCS += aes.c
+CHIP_CSRCS += aria.c
+CHIP_CSRCS += bignum_core.c
+CHIP_CSRCS += bignum.c
+CHIP_CSRCS += ccm.c
+CHIP_CSRCS += cipher_wrap.c
+CHIP_CSRCS += cipher.c
+CHIP_CSRCS += cmac.c
+CHIP_CSRCS += constant_time.c
+CHIP_CSRCS += ctr_drbg.c
+CHIP_CSRCS += ecp_curves.c
+CHIP_CSRCS += ecp.c
+CHIP_CSRCS += entropy.c
+CHIP_CSRCS += gcm.c
+CHIP_CSRCS += md.c
+CHIP_CSRCS += pkcs5.c
+CHIP_CSRCS += platform_util.c
+CHIP_CSRCS += platform.c
+CHIP_CSRCS += sha1.c
+CHIP_CSRCS += sha256.c
+CHIP_CSRCS += sha512.c
+CHIP_CSRCS += pk.c
+CHIP_CSRCS += pk_wrap.c
+CHIP_CSRCS += pkparse.c
+CHIP_CSRCS += ecdsa.c
+CHIP_CSRCS += asn1parse.c
+CHIP_CSRCS += asn1write.c
+CHIP_CSRCS += rsa.c
+CHIP_CSRCS += md5.c
+CHIP_CSRCS += oid.c
+CHIP_CSRCS += pem.c
+CHIP_CSRCS += hmac_drbg.c
+CHIP_CSRCS += hash_info.c
+CHIP_CSRCS += rsa_alt_helpers.c
+CHIP_CSRCS += ecdh.c
+
+VPATH += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)mbedtls$(DELIM)port
+
+CHIP_CSRCS += esp_hardware.c
+CHIP_CSRCS += esp_mem.c
+CHIP_CSRCS += esp_timing.c
+
+VPATH += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)mbedtls$(DELIM)port$(DELIM)md
+
+CHIP_CSRCS += esp_md.c
+
+## WPA Supplicant
+
+WIFI_WPA_SUPPLICANT = chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)wpa_supplicant
+
+CFLAGS += $(DEFINE_PREFIX)__ets__
+CFLAGS += $(DEFINE_PREFIX)CONFIG_CRYPTO_MBEDTLS
+CFLAGS += $(DEFINE_PREFIX)CONFIG_ECC
+CFLAGS += $(DEFINE_PREFIX)CONFIG_IEEE80211W
+CFLAGS += $(DEFINE_PREFIX)CONFIG_WPA3_SAE
+CFLAGS += $(DEFINE_PREFIX)EAP_PEER_METHOD
+CFLAGS += $(DEFINE_PREFIX)ESP_PLATFORM=1
+CFLAGS += $(DEFINE_PREFIX)ESP_SUPPLICANT
+CFLAGS += $(DEFINE_PREFIX)ESPRESSIF_USE
+CFLAGS += $(DEFINE_PREFIX)IEEE8021X_EAPOL
+CFLAGS += $(DEFINE_PREFIX)USE_WPA2_TASK
+CFLAGS += $(DEFINE_PREFIX)CONFIG_SHA256
+CFLAGS += $(DEFINE_PREFIX)CONFIG_SAE
+
+ifeq ($(CONFIG_ESPRESSIF_WIFI_ENABLE_SAE_PK),y)
+CFLAGS += $(DEFINE_PREFIX)CONFIG_SAE_PK
+endif
+
+ifeq ($(CONFIG_ESPRESSIF_WIFI_ENABLE_WPA3_OWE_STA),y)
+CFLAGS += $(DEFINE_PREFIX)CONFIG_OWE_STA
+endif
+
+ifeq ($(CONFIG_ESPRESSIF_WIFI_GCMP_SUPPORT),y)
+CFLAGS += $(DEFINE_PREFIX)CONFIG_GCMP
+endif
+
+ifeq ($(CONFIG_ESPRESSIF_WIFI_GMAC_SUPPORT),y)
+CFLAGS += $(DEFINE_PREFIX)CONFIG_GMAC
+endif
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)$(WIFI_WPA_SUPPLICANT)$(DELIM)include
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)$(WIFI_WPA_SUPPLICANT)$(DELIM)src
+
+VPATH += $(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)ap
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)$(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)ap
+
+CHIP_CSRCS += ap_config.c
+CHIP_CSRCS += ieee802_11.c
+CHIP_CSRCS += comeback_token.c
+CHIP_CSRCS += pmksa_cache_auth.c
+CHIP_CSRCS += sta_info.c
+CHIP_CSRCS += wpa_auth_ie.c
+CHIP_CSRCS += wpa_auth.c
+
+VPATH += $(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)common
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)$(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)common
+
+CHIP_CSRCS += dragonfly.c
+CHIP_CSRCS += sae.c
+CHIP_CSRCS += wpa_common.c
+CHIP_CSRCS += sae_pk.c
+CHIP_CSRCS += bss.c
+CHIP_CSRCS += scan.c
+CHIP_CSRCS += ieee802_11_common.c
+
+VPATH += $(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)crypto
+
+CHIP_CSRCS += aes-ccm.c
+CHIP_CSRCS += aes-gcm.c
+CHIP_CSRCS += aes-omac1.c
+CHIP_CSRCS += aes-unwrap.c
+CHIP_CSRCS += aes-wrap.c
+CHIP_CSRCS += ccmp.c
+CHIP_CSRCS += crypto_ops.c
+CHIP_CSRCS += des-internal.c
+CHIP_CSRCS += dh_groups.c
+CHIP_CSRCS += rc4.c
+CHIP_CSRCS += sha1-prf.c
+CHIP_CSRCS += sha256-kdf.c
+CHIP_CSRCS += sha256-prf.c
+
+VPATH += $(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)eap_peer
+
+CHIP_CSRCS += chap.c
+CHIP_CSRCS += eap_common.c
+CHIP_CSRCS += eap_mschapv2.c
+CHIP_CSRCS += eap_peap_common.c
+CHIP_CSRCS += eap_peap.c
+CHIP_CSRCS += eap_tls_common.c
+CHIP_CSRCS += eap_tls.c
+CHIP_CSRCS += eap_ttls.c
+CHIP_CSRCS += eap.c
+CHIP_CSRCS += mschapv2.c
+
+VPATH += $(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)rsn_supp
+
+CHIP_CSRCS += pmksa_cache.c
+CHIP_CSRCS += wpa_ie.c
+CHIP_CSRCS += wpa.c
+
+VPATH += $(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)utils
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)$(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)utils
+
+CHIP_CSRCS += base64.c
+CHIP_CSRCS += bitfield.c
+CHIP_CSRCS += common.c
+CHIP_CSRCS += ext_password.c
+CHIP_CSRCS += json.c
+CHIP_CSRCS += uuid.c
+CHIP_CSRCS += wpa_debug.c
+CHIP_CSRCS += wpabuf.c
+
+VPATH += $(WIFI_WPA_SUPPLICANT)$(DELIM)port
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)$(WIFI_WPA_SUPPLICANT)$(DELIM)port$(DELIM)include
+
+CHIP_CSRCS += eloop.c
+CHIP_CSRCS += os_xtensa.c
+
+## ESP Supplicant (Espressif's WPA supplicant extension)
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)$(WIFI_WPA_SUPPLICANT)$(DELIM)esp_supplicant$(DELIM)include
+
+VPATH += $(WIFI_WPA_SUPPLICANT)$(DELIM)esp_supplicant$(DELIM)src
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)$(WIFI_WPA_SUPPLICANT)$(DELIM)esp_supplicant$(DELIM)src
+
+CHIP_CSRCS += esp_common.c
+CHIP_CSRCS += esp_hostap.c
+CHIP_CSRCS += esp_wpa_main.c
+CHIP_CSRCS += esp_wpa2.c
+CHIP_CSRCS += esp_wpa3.c
+CHIP_CSRCS += esp_wpas_glue.c
+CHIP_CSRCS += esp_owe.c
+CHIP_CSRCS += esp_scan.c
+
+VPATH += $(WIFI_WPA_SUPPLICANT)$(DELIM)esp_supplicant$(DELIM)src$(DELIM)crypto
+
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)$(WIFI_WPA_SUPPLICANT)$(DELIM)src$(DELIM)crypto
+
+CHIP_CSRCS += crypto_mbedtls-bignum.c
+CHIP_CSRCS += crypto_mbedtls-ec.c
+CHIP_CSRCS += crypto_mbedtls-rsa.c
+CHIP_CSRCS += crypto_mbedtls.c
+CHIP_CSRCS += tls_mbedtls.c
+CHIP_CSRCS += aes-siv.c
+
+endif
diff --git a/arch/risc-v/src/common/espressif/esp_ets_timer_legacy.c b/arch/risc-v/src/common/espressif/esp_ets_timer_legacy.c
new file mode 100644
index 0000000000..f083778246
--- /dev/null
+++ b/arch/risc-v/src/common/espressif/esp_ets_timer_legacy.c
@@ -0,0 +1,263 @@
+/****************************************************************************
+ * arch/risc-v/src/common/espressif/esp_ets_timer_legacy.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this args for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* ets_timer module implements a set of legacy timer APIs which are
+ * used by the WiFi driver. This is done on top of the esp_hr_timer APIs.
+ * Applications should not use ets_timer functions, as they may change
+ * without notice.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include "sdkconfig.h"
+#include "esp_timer.h"
+#include "rom/ets_sys.h"
+#include "esp_attr.h"
+
+#include "espressif/esp_hr_timer.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* We abuse 'timer_arg' field of ETSTimer structure to hold a
+ * pointer to esp_hr_timer.
+ */
+
+#define ESP_TIMER(p_ets_timer) \
+ ((struct esp_hr_timer_s *) (p_ets_timer)->timer_arg)
+
+/* Override internal name to be compliant with NuttX C Coding Standard */
+
+#define ets_timer ETSTimer
+#define ets_timer_func ETSTimerFunc
+
+/* We abuse 'timer_expire' field of ETSTimer structure to hold a magic value
+ * signifying that the contents of the timer was zeroed out.
+ */
+
+#define TIMER_INITIALIZED_FIELD(p_ets_timer) ((p_ets_timer)->timer_expire)
+#define TIMER_INITIALIZED_VAL 0x12121212
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static bool timer_initialized(ets_timer *ptimer);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: timer_initialized
+ *
+ * Description:
+ * This function checks if the given ETS timer is initialized.
+ *
+ * Input Parameters:
+ * ptimer - Pointer to the ETS timer structure.
+ *
+ * Returned Value:
+ * Returns true if the timer is initialized, false otherwise.
+ *
+ ****************************************************************************/
+
+static bool IRAM_ATTR timer_initialized(ets_timer *ptimer)
+{
+ return TIMER_INITIALIZED_FIELD(ptimer) == TIMER_INITIALIZED_VAL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ets_timer_setfn
+ *
+ * Description:
+ * Set timer callback function and argument.
+ *
+ * Input Parameters:
+ * ptimer - Timer struct pointer
+ * pfunction - Timer callback
+ * parg - Timer callback argument
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void ets_timer_setfn(ets_timer *ptimer,
+ ets_timer_func *pfunction,
+ void *parg)
+{
+ int ret = OK;
+
+ if (!timer_initialized(ptimer))
+ {
+ memset(ptimer, 0, sizeof(*ptimer));
+ TIMER_INITIALIZED_FIELD(ptimer) = TIMER_INITIALIZED_VAL;
+ }
+
+ if (ESP_TIMER(ptimer) == NULL)
+ {
+ struct esp_hr_timer_args_s hr_timer_args;
+ struct esp_hr_timer_s *hr_timers_p;
+
+ hr_timer_args.arg = parg;
+ hr_timer_args.callback = pfunction;
+
+ ret = esp_hr_timer_create(&hr_timer_args, &hr_timers_p);
+
+ if (ret)
+ {
+ tmrerr("Failed to create ets_timer error=%d\n", ret);
+ }
+ else
+ {
+ ptimer->timer_arg = hr_timers_p;
+ }
+ }
+}
+
+/****************************************************************************
+ * Name: ets_timer_arm_us
+ *
+ * Description:
+ * Arms an ETS timer. The timer range is from 640 us to 429496 ms.
+ *
+ * Input Parameters:
+ * ptimer - Pointer to the timer structure.
+ * time_us - Timer value in microseconds. The range is 1 to 429496729.
+ * repeat_flag - Specifies if the timer is periodically repeated.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void IRAM_ATTR ets_timer_arm_us(ets_timer *ptimer,
+ uint32_t time_us,
+ bool repeat_flag)
+{
+ assert(timer_initialized(ptimer));
+
+ esp_hr_timer_stop(ESP_TIMER(ptimer));
+
+ if (!repeat_flag)
+ {
+ esp_hr_timer_start(ESP_TIMER(ptimer), time_us, false);
+ }
+ else
+ {
+ esp_hr_timer_start(ESP_TIMER(ptimer), time_us, true);
+ }
+}
+
+/****************************************************************************
+ * Name: ets_timer_arm
+ *
+ * Description:
+ * Arms an ETS timer. The timer range is from 640 us to 429496 ms.
+ *
+ * Input Parameters:
+ * ptimer - Pointer to the timer structure.
+ * time_us - Timer value in microseconds. The range is 1 to 429496.
+ * repeat_flag - Specifies if the timer is periodically repeated.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void IRAM_ATTR ets_timer_arm(ets_timer *ptimer,
+ uint32_t time_ms,
+ bool repeat_flag)
+{
+ uint64_t time_us = 1000LL * (uint64_t) time_ms;
+
+ assert(timer_initialized(ptimer));
+
+ esp_hr_timer_stop(ESP_TIMER(ptimer));
+
+ if (!repeat_flag)
+ {
+ esp_hr_timer_start(ESP_TIMER(ptimer), time_us, false);
+ }
+ else
+ {
+ esp_hr_timer_start(ESP_TIMER(ptimer), time_us, true);
+ }
+}
+
+/****************************************************************************
+ * Name: ets_timer_done
+ *
+ * Description:
+ * Unset timer callback and argument to NULL.
+ *
+ * Input Parameters:
+ * ptimer - timer data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void ets_timer_done(ets_timer *ptimer)
+{
+ if (timer_initialized(ptimer))
+ {
+ esp_hr_timer_delete(ESP_TIMER(ptimer));
+ ptimer->timer_arg = NULL;
+ TIMER_INITIALIZED_FIELD(ptimer) = 0;
+ }
+}
+
+/****************************************************************************
+ * Name: ets_timer_disarm
+ *
+ * Description:
+ * Disarm an ets timer.
+ *
+ * Input Parameters:
+ * ptimer - timer data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void IRAM_ATTR ets_timer_disarm(ets_timer *ptimer)
+{
+ if (timer_initialized(ptimer))
+ {
+ esp_hr_timer_stop(ESP_TIMER(ptimer));
+ }
+}
diff --git a/arch/risc-v/src/common/espressif/esp_hr_timer.c b/arch/risc-v/src/common/espressif/esp_hr_timer.c
index 3fb7d133fd..e3be4daf9d 100644
--- a/arch/risc-v/src/common/espressif/esp_hr_timer.c
+++ b/arch/risc-v/src/common/espressif/esp_hr_timer.c
@@ -97,6 +97,8 @@ static struct esp_hr_timer_context_s g_hr_timer_context =
.toutsem = SEM_INITIALIZER(0),
};
+static bool g_hr_timer_initialized;
+
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -118,7 +120,7 @@ static struct esp_hr_timer_context_s g_hr_timer_context =
*
****************************************************************************/
-static int esp_hr_timer_thread(int argc, char *argv[])
+IRAM_ATTR static int esp_hr_timer_thread(int argc, char *argv[])
{
struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
@@ -311,8 +313,8 @@ static int IRAM_ATTR esp_hr_timer_isr(int irq, void *context, void *arg)
*
****************************************************************************/
-int esp_hr_timer_create(const struct esp_hr_timer_args_s *args,
- struct esp_hr_timer_s **timer_handle)
+int IRAM_ATTR esp_hr_timer_create(const struct esp_hr_timer_args_s *args,
+ struct esp_hr_timer_s **timer_handle)
{
struct esp_hr_timer_s *timer;
@@ -351,9 +353,9 @@ int esp_hr_timer_create(const struct esp_hr_timer_args_s *args,
*
****************************************************************************/
-void esp_hr_timer_start(struct esp_hr_timer_s *timer,
- uint64_t timeout,
- bool repeat)
+void IRAM_ATTR esp_hr_timer_start(struct esp_hr_timer_s *timer,
+ uint64_t timeout,
+ bool repeat)
{
struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
bool inserted = false;
@@ -436,7 +438,7 @@ void esp_hr_timer_start(struct esp_hr_timer_s *timer,
*
****************************************************************************/
-void esp_hr_timer_stop(struct esp_hr_timer_s *timer)
+void IRAM_ATTR esp_hr_timer_stop(struct esp_hr_timer_s *timer)
{
struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
@@ -666,7 +668,6 @@ void IRAM_ATTR esp_hr_timer_calibration(uint64_t time_us)
int esp_hr_timer_init(void)
{
- static bool g_hr_timer_initialized = false;
struct esp_hr_timer_context_s *priv;
int pid;
diff --git a/arch/risc-v/src/common/espressif/esp_irq.c b/arch/risc-v/src/common/espressif/esp_irq.c
index 5feda8ec25..ca79bb204f 100644
--- a/arch/risc-v/src/common/espressif/esp_irq.c
+++ b/arch/risc-v/src/common/espressif/esp_irq.c
@@ -68,6 +68,7 @@
#define CPUINT_FREE(cpuint) ((cpuint).val = 0)
#define CPUINT_ISENABLED(cpuint) ((cpuint).cpuint_en == 1)
#define CPUINT_ISASSIGNED(cpuint) ((cpuint).assigned == 1)
+#define CPUINT_ISRESERVED(cpuint) ((cpuint).reserved0 == 1)
#define CPUINT_ISFREE(cpuint) (!CPUINT_ISASSIGNED(cpuint))
/* CPU interrupts can be detached from any interrupt source by setting the
@@ -113,7 +114,8 @@ static volatile uint8_t g_irq_map[NR_IRQS];
* devices.
*/
-static uint32_t g_cpuint_freelist = ESP_CPUINT_PERIPHSET;
+static uint32_t g_cpuint_freelist = ESP_CPUINT_PERIPHSET & \
+ ~ESP_WIRELESS_RESERVE_INT;
/* This bitmask has an 1 if the int should be disabled
* when the flash is disabled.
@@ -200,8 +202,7 @@ static int esp_cpuint_alloc(int irq)
DEBUGASSERT(CPUINT_ISFREE(g_cpuint_map[cpuint]));
- CPUINT_ASSIGN(g_cpuint_map[cpuint], irq);
- g_irq_map[irq] = cpuint;
+ esp_set_irq(irq, cpuint);
return cpuint;
}
@@ -680,3 +681,49 @@ void IRAM_ATTR esp_intr_noniram_enable(void)
esp_cpu_intr_enable(non_iram_ints);
leave_critical_section(irqstate);
}
+
+/****************************************************************************
+ * Name: esp_get_irq
+ *
+ * Description:
+ * This function returns the IRQ associated with a CPU interrupt
+ *
+ * Input Parameters:
+ * cpuint - The CPU interrupt associated to the IRQ
+ *
+ * Returned Value:
+ * The IRQ associated with such CPU interrupt or CPUINT_UNASSIGNED if
+ * IRQ is not yet assigned to a CPU interrupt.
+ *
+ ****************************************************************************/
+
+int esp_get_irq(int cpuint)
+{
+ return CPUINT_GETIRQ(g_cpuint_map[cpuint]);
+}
+
+/****************************************************************************
+ * Name: esp_set_irq
+ *
+ * Description:
+ * This function assigns a CPU interrupt to a specific IRQ number. It
+ * updates the mapping between IRQ numbers and CPU interrupts, allowing
+ * the system to correctly route hardware interrupts to the appropriate
+ * handlers. Please note that this function is intended to be used only
+ * when a CPU interrupt is already assigned to an IRQ number. Otherwise,
+ * please check esp_setup_irq.
+ *
+ * Input Parameters:
+ * irq - The IRQ number to be associated with the CPU interrupt.
+ * cpuint - The CPU interrupt to be associated with the IRQ number.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void esp_set_irq(int irq, int cpuint)
+{
+ CPUINT_ASSIGN(g_cpuint_map[cpuint], irq);
+ g_irq_map[irq] = cpuint;
+}
diff --git a/arch/risc-v/src/common/espressif/esp_irq.h b/arch/risc-v/src/common/espressif/esp_irq.h
index 9eb0692d7d..648665430e 100644
--- a/arch/risc-v/src/common/espressif/esp_irq.h
+++ b/arch/risc-v/src/common/espressif/esp_irq.h
@@ -174,6 +174,45 @@ void esp_intr_noniram_disable(void);
void esp_intr_noniram_enable(void);
+/****************************************************************************
+ * Name: esp_get_irq
+ *
+ * Description:
+ * This function returns the IRQ associated with a CPU interrupt
+ *
+ * Input Parameters:
+ * cpuint - The CPU interrupt associated to the IRQ
+ *
+ * Returned Value:
+ * The IRQ associated with such CPU interrupt or CPUINT_UNASSIGNED if
+ * IRQ is not yet assigned to a CPU interrupt.
+ *
+ ****************************************************************************/
+
+int esp_get_irq(int cpuint);
+
+/****************************************************************************
+ * Name: esp_set_irq
+ *
+ * Description:
+ * This function assigns a CPU interrupt to a specific IRQ number. It
+ * updates the mapping between IRQ numbers and CPU interrupts, allowing
+ * the system to correctly route hardware interrupts to the appropriate
+ * handlers. Please note that this function is intended to be used only
+ * when a CPU interrupt is already assigned to an IRQ number. Otherwise,
+ * please check esp_setup_irq.
+ *
+ * Input Parameters:
+ * irq - The IRQ number to be associated with the CPU interrupt.
+ * cpuint - The CPU interrupt to be associated with the IRQ number.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void esp_set_irq(int irq, int cpuint);
+
#undef EXTERN
#if defined(__cplusplus)
}
diff --git a/arch/risc-v/src/common/espressif/esp_rtc.c b/arch/risc-v/src/common/espressif/esp_rtc.c
index 84c56ef5e9..2aba3c9edc 100644
--- a/arch/risc-v/src/common/espressif/esp_rtc.c
+++ b/arch/risc-v/src/common/espressif/esp_rtc.c
@@ -865,7 +865,11 @@ int esp_rtc_driverinit(void)
/* Enable HR-Timer */
- VERIFY(esp_hr_timer_init());
+ ret = esp_hr_timer_init();
+ if (ret < 0)
+ {
+ return ret;
+ }
g_hr_timer_enabled = true;
diff --git a/arch/risc-v/src/common/espressif/esp_start.c b/arch/risc-v/src/common/espressif/esp_start.c
index 7ad3c87da8..96dbc3e37b 100644
--- a/arch/risc-v/src/common/espressif/esp_start.c
+++ b/arch/risc-v/src/common/espressif/esp_start.c
@@ -152,6 +152,8 @@ extern uint8_t _image_drom_size[];
extern int ets_printf(const char *fmt, ...) printf_like(1, 2);
#endif
+extern void cache_set_idrom_mmu_size(uint32_t irom_size, uint32_t drom_size);
+
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
@@ -174,6 +176,11 @@ HDR_ATTR static void (*_entry_point)(void) = __start;
* Public Data
****************************************************************************/
+extern uint8_t _instruction_reserved_start[];
+extern uint8_t _instruction_reserved_end[];
+extern uint8_t _rodata_reserved_start[];
+extern uint8_t _rodata_reserved_end[];
+
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -384,6 +391,11 @@ static int map_rom_segments(uint32_t app_drom_start, uint32_t app_drom_vaddr,
void __esp_start(void)
{
+#ifdef CONFIG_ESP_ROM_NEEDS_SET_CACHE_MMU_SIZE
+ uint32_t _instruction_size;
+ uint32_t cache_mmu_irom_size;
+#endif
+
#ifdef CONFIG_ESPRESSIF_SIMPLE_BOOT
if (bootloader_init() != 0)
{
@@ -410,6 +422,19 @@ void __esp_start(void)
}
#endif
+#if CONFIG_ESP_ROM_NEEDS_SET_CACHE_MMU_SIZE
+ _instruction_size = (uint32_t)&_instruction_reserved_end - \
+ (uint32_t)&_instruction_reserved_start;
+ cache_mmu_irom_size =
+ ((_instruction_size + SPI_FLASH_MMU_PAGE_SIZE - 1) / \
+ SPI_FLASH_MMU_PAGE_SIZE) * sizeof(uint32_t);
+
+ /* Configure the Cache MMU size for instruction and rodata in flash. */
+
+ cache_set_idrom_mmu_size(cache_mmu_irom_size,
+ CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size);
+#endif /* CONFIG_ESP_ROM_NEEDS_SET_CACHE_MMU_SIZE */
+
#ifdef CONFIG_ESPRESSIF_REGION_PROTECTION
/* Configure region protection */
diff --git a/arch/risc-v/src/common/espressif/esp_wifi_init.c b/arch/risc-v/src/common/espressif/esp_wifi_init.c
new file mode 100644
index 0000000000..ba94d12eaf
--- /dev/null
+++ b/arch/risc-v/src/common/espressif/esp_wifi_init.c
@@ -0,0 +1,465 @@
+/****************************************************************************
+ * arch/risc-v/src/common/espressif/esp_wifi_init.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this args for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <assert.h>
+#include <debug.h>
+#include <errno.h>
+
+#include <esp_event.h>
+#include <esp_wifi.h>
+#include "esp_log.h"
+#include "esp_private/wifi.h"
+#include "esp_private/adc_share_hw_ctrl.h"
+#include "esp_private/sleep_modem.h"
+#include "esp_sleep.h"
+#include "esp_private/esp_clk.h"
+#include "esp_wpa.h"
+#include "esp_coexist_internal.h"
+#include "esp_phy_init.h"
+#include "esp_private/phy.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#if (CONFIG_ESPRESSIF_WIFI_RX_BA_WIN > CONFIG_ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM)
+#error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than WIFI_DYNAMIC_RX_BUFFER_NUM!"
+#endif
+
+#if (CONFIG_ESPRESSIF_WIFI_RX_BA_WIN > (CONFIG_ESPRESSIF_WIFI_STATIC_RX_BUFFER_NUM << 1))
+#error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than double of the WIFI_STATIC_RX_BUFFER_NUM!"
+#endif
+
+#if SOC_PM_SUPPORT_PMU_MODEM_STATE
+# define WIFI_BEACON_MONITOR_CONFIG_DEFAULT(ena) { \
+ .enable = (ena), \
+ .loss_timeout = CONFIG_ESP_WIFI_SLP_BEACON_LOST_TIMEOUT, \
+ .loss_threshold = CONFIG_ESP_WIFI_SLP_BEACON_LOST_THRESHOLD, \
+ .delta_intr_early = 0, \
+ .delta_loss_timeout = 0, \
+ .beacon_abort = 1, \
+ .broadcast_wakeup = 1, \
+ .tsf_time_sync_deviation = 5, \
+ .modem_state_consecutive = 10, \
+ .rf_ctrl_wait_cycle = 20 \
+}
+#else
+# define WIFI_BEACON_MONITOR_CONFIG_DEFAULT(ena) { \
+ .enable = (ena), \
+ .loss_timeout = CONFIG_ESP_WIFI_SLP_BEACON_LOST_TIMEOUT, \
+ .loss_threshold = CONFIG_ESP_WIFI_SLP_BEACON_LOST_THRESHOLD, \
+ .delta_intr_early = CONFIG_ESP_WIFI_SLP_PHY_ON_DELTA_EARLY_TIME, \
+ .delta_loss_timeout = CONFIG_ESP_WIFI_SLP_PHY_OFF_DELTA_TIMEOUT_TIME \
+}
+#endif
+
+/* Set additional WiFi features and capabilities */
+
+uint64_t g_wifi_feature_caps =
+#if CONFIG_ESP_WIFI_ENABLE_WPA3_SAE
+ CONFIG_FEATURE_WPA3_SAE_BIT |
+#endif
+#if CONFIG_SPIRAM
+ CONFIG_FEATURE_CACHE_TX_BUF_BIT |
+#endif
+#if CONFIG_ESP_WIFI_FTM_INITIATOR_SUPPORT
+ CONFIG_FEATURE_FTM_INITIATOR_BIT |
+#endif
+#if CONFIG_ESP_WIFI_FTM_RESPONDER_SUPPORT
+ CONFIG_FEATURE_FTM_RESPONDER_BIT |
+#endif
+ 0;
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static void esp_wifi_set_log_level(void);
+static void esp_wifi_config_info(void);
+
+extern uint8_t esp_wifi_get_user_init_flag_internal(void);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+ESP_EVENT_DEFINE_BASE(WIFI_EVENT);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_wifi_set_log_level
+ *
+ * Description:
+ * Sets the log level for the ESP32 WiFi module based on preprocessor
+ * definitions. The log level can be verbose, warning, or error.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void esp_wifi_set_log_level(void)
+{
+ wifi_log_level_t wifi_log_level = WIFI_LOG_NONE;
+
+ /* set WiFi log level */
+
+#if defined(CONFIG_DEBUG_WIRELESS_INFO)
+ wifi_log_level = WIFI_LOG_VERBOSE;
+#elif defined(CONFIG_DEBUG_WIRELESS_WARN)
+ wifi_log_level = WIFI_LOG_WARNING;
+#elif defined(CONFIG_LOG_MAXIMUM_LEVEL)
+ wifi_log_level = WIFI_LOG_ERROR;
+#endif
+
+ esp_wifi_internal_set_log_level(wifi_log_level);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_config_info
+ *
+ * Description:
+ * This function logs the current configuration settings for the Wi-Fi
+ * module. It checks for various configuration options and logs if they
+ * are enabled.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void esp_wifi_config_info(void)
+{
+#ifdef CONFIG_ESPRESSIF_WIFI_RX_BA_WIN
+ wlinfo("rx ba win: %d", CONFIG_ESPRESSIF_WIFI_RX_BA_WIN);
+#endif
+
+#ifdef CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP
+ wlinfo("WiFi/LWIP prefer SPIRAM");
+#endif
+
+#ifdef CONFIG_ESP_WIFI_IRAM_OPT
+ wlinfo("WiFi IRAM OP enabled");
+#endif
+
+#ifdef CONFIG_ESP_WIFI_RX_IRAM_OPT
+ wlinfo("WiFi RX IRAM OP enabled");
+#endif
+
+#ifdef CONFIG_ESP_WIFI_SLP_IRAM_OPT
+ wlinfo("WiFi SLP IRAM OP enabled");
+#endif
+
+#ifdef CONFIG_LWIP_IRAM_OPTIMIZATION
+ wlinfo("LWIP IRAM OP enabled");
+#endif
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+#ifndef CONFIG_ESP_WIFI_FTM_ENABLE
+
+/****************************************************************************
+ * Name: ieee80211_ftm_attach
+ *
+ * Description:
+ * This function initializes and attaches the Fine Timing Measurement (FTM)
+ * capabilities to the IEEE 802.11 Wi-Fi driver. FTM is used for precise
+ * distance measurements between Wi-Fi devices.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void ieee80211_ftm_attach(void)
+{
+ /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */
+}
+#endif
+
+#ifndef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
+
+/****************************************************************************
+ * Name: net80211_softap_funcs_init
+ *
+ * Description:
+ * This function is a placeholder for initializing the SoftAP (Software
+ * Access Point) functionalities of the IEEE 802.11 Wi-Fi driver. It is
+ * only compiled if the CONFIG_ESP_WIFI_SOFTAP_SUPPORT configuration
+ * option is not set.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void net80211_softap_funcs_init(void)
+{
+}
+#endif
+
+#ifndef CONFIG_ESP_WIFI_NAN_ENABLE
+
+/****************************************************************************
+ * Name: nan_start
+ *
+ * Description:
+ * This function is a stub to overwrite a weak link in the Wi-Fi library.
+ * It is used to start the NAN (Neighbor Awareness Networking) service.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Always returns ESP_OK.
+ *
+ ****************************************************************************/
+
+esp_err_t nan_start(void)
+{
+ /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */
+
+ return ESP_OK;
+}
+
+/****************************************************************************
+ * Name: nan_stop
+ *
+ * Description:
+ * This function is a stub to overwrite a weak link in the Wi-Fi library.
+ * It is used to stop the NAN (Neighbor Awareness Networking) service.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Always returns ESP_OK.
+ *
+ ****************************************************************************/
+
+esp_err_t nan_stop(void)
+{
+ /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */
+
+ return ESP_OK;
+}
+
+/****************************************************************************
+ * Name: nan_input
+ *
+ * Description:
+ * This function is a stub to overwrite a weak link in the Wi-Fi library.
+ * It is used to handle input for the NAN (Neighbor Awareness Networking)
+ * service.
+ *
+ * Input Parameters:
+ * p1 - First parameter for the input function.
+ * p2 - Second parameter for the input function.
+ * p3 - Third parameter for the input function.
+ *
+ * Returned Value:
+ * Always returns 0.
+ *
+ ****************************************************************************/
+
+int nan_input(void *p1, int p2, int p3)
+{
+ /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */
+
+ return 0;
+}
+
+/****************************************************************************
+ * Name: nan_sm_handle_event
+ *
+ * Description:
+ * This function is a stub to overwrite a weak link in the Wi-Fi library.
+ * It is used to handle events for the NAN (Neighbor Awareness Networking)
+ * service state machine.
+ *
+ * Input Parameters:
+ * p1 - First parameter for the event handler.
+ * p2 - Second parameter for the event handler.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void nan_sm_handle_event(void *p1, int p2)
+{
+ /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */
+}
+#endif
+
+/****************************************************************************
+ * Name: esp_wifi_deinit
+ *
+ * Description:
+ * Deinitialize Wi-Fi and free resource
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Values: esp_err_t
+ * Zero (OK) is returned or a negative error.
+ *
+ ****************************************************************************/
+
+esp_err_t esp_wifi_deinit(void)
+{
+ esp_err_t err = ESP_OK;
+#ifdef CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT
+ wifi_beacon_monitor_config_t monitor_config;
+#endif
+
+ if (esp_wifi_get_user_init_flag_internal())
+ {
+ wlerr("Wi-Fi not stop");
+ return ESP_ERR_WIFI_NOT_STOPPED;
+ }
+
+ if (esp_wifi_internal_reg_rxcb(WIFI_IF_STA, NULL) != ESP_OK ||
+ esp_wifi_internal_reg_rxcb(WIFI_IF_AP, NULL) != ESP_OK)
+ {
+ wlerr("Failed to unregister Rx callbacks");
+ }
+
+ esp_supplicant_deinit();
+ err = esp_wifi_deinit_internal();
+ if (err != ESP_OK)
+ {
+ wlerr("Failed to deinit Wi-Fi driver (0x%x)", err);
+ return err;
+ }
+
+#ifdef CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT
+ monitor_config = WIFI_BEACON_MONITOR_CONFIG_DEFAULT(false);
+ esp_wifi_beacon_monitor_configure(&monitor_config);
+#endif
+
+#if CONFIG_MAC_BB_PD
+ esp_unregister_mac_bb_pd_callback(pm_mac_sleep);
+ esp_unregister_mac_bb_pu_callback(pm_mac_wakeup);
+#endif
+ esp_wifi_power_domain_off();
+#if CONFIG_MAC_BB_PD
+ esp_wifi_internal_set_mac_sleep(false);
+ esp_mac_bb_pd_mem_deinit();
+#endif
+ esp_phy_modem_deinit();
+
+ return err;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_init
+ *
+ * Description:
+ * Initialize Wi-Fi
+ *
+ * Input Parameters:
+ * config - Initialization config parameters
+ *
+ * Returned Values: esp_err_t
+ * Zero (OK) is returned or a negative error.
+ *
+ ****************************************************************************/
+
+esp_err_t esp_wifi_init(const wifi_init_config_t *config)
+{
+ esp_err_t result = ESP_OK;
+#ifdef CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT
+ wifi_beacon_monitor_config_t monitor_config;
+#endif
+
+ if ((config->feature_caps & CONFIG_FEATURE_CACHE_TX_BUF_BIT) &&
+ (WIFI_CACHE_TX_BUFFER_NUM == 0))
+ {
+ wlerr("Number of WiFi cache TX buffers should not equal 0 when"
+ "enable SPIRAM");
+ return ESP_ERR_NOT_SUPPORTED;
+ }
+
+ esp_wifi_power_domain_on();
+
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ coex_init();
+#endif
+ esp_wifi_set_log_level();
+
+ result = esp_wifi_init_internal(config);
+ if (result == ESP_OK)
+ {
+#if CONFIG_MAC_BB_PD
+ esp_mac_bb_pd_mem_init();
+ esp_wifi_internal_set_mac_sleep(true);
+#endif
+ esp_phy_modem_init();
+
+ result = esp_supplicant_init();
+ if (result != ESP_OK)
+ {
+ esp_err_t deinit_ret;
+
+ wlerr("Failed to init supplicant (0x%x)", result);
+
+ deinit_ret = esp_wifi_deinit();
+ if (deinit_ret != ESP_OK)
+ {
+ wlerr("Failed to deinit Wi-Fi (0x%x)", deinit_ret);
+ }
+
+ return result;
+ }
+ }
+
+#if CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT
+ monitor_config = WIFI_BEACON_MONITOR_CONFIG_DEFAULT(true);
+ esp_wifi_beacon_monitor_configure(&monitor_config);
+#endif
+ adc2_cal_include(); /* This enables the ADC2 calibration constructor at start up */
+
+ esp_wifi_config_info();
+
+ return result;
+}
diff --git a/arch/risc-v/src/common/espressif/esp_wifi_utils.c b/arch/risc-v/src/common/espressif/esp_wifi_utils.c
new file mode 100644
index 0000000000..21019a78f4
--- /dev/null
+++ b/arch/risc-v/src/common/espressif/esp_wifi_utils.c
@@ -0,0 +1,654 @@
+/****************************************************************************
+ * arch/risc-v/src/common/espressif/esp_wifi_utils.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+#include <netinet/arp.h>
+#include <sys/param.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/wireless/wireless.h>
+
+#include "esp_wifi_adapter.h"
+
+#include "esp_log.h"
+#include "esp_mac.h"
+#include "esp_private/phy.h"
+#include "esp_private/wifi.h"
+#include "esp_random.h"
+#include "esp_timer.h"
+#include "esp_wpa.h"
+#include "rom/ets_sys.h"
+#include "soc/soc_caps.h"
+
+#include "esp_wifi_utils.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Helper to get iw_event size */
+
+#define ESP_IW_EVENT_SIZE(field) \
+ (offsetof(struct iw_event, u) + sizeof(((union iwreq_data *)0)->field))
+
+#ifdef CONFIG_ESPRESSIF_WIFI_SCAN_RESULT_SIZE
+# define WIFI_SCAN_RESULT_SIZE CONFIG_ESPRESSIF_WIFI_SCAN_RESULT_SIZE
+#else
+# define WIFI_SCAN_RESULT_SIZE (4096)
+#endif
+
+#define SCAN_TIME_SEC (5)
+
+/* Maximum number of channels for Wi-Fi 2.4Ghz */
+
+#define CHANNEL_MAX_NUM (14)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+enum scan_status_e
+{
+ ESP_SCAN_DISABLED = 0,
+ ESP_SCAN_RUN,
+ ESP_SCAN_DONE
+};
+
+/* Wi-Fi scan result information */
+
+struct wifi_scan_result
+{
+ enum scan_status_e scan_status; /* Scan status */
+ sem_t scan_signal; /* Scan notification signal */
+ uint8_t *scan_result; /* Temp buffer that holds results */
+ unsigned int scan_result_size; /* Current size of temp buffer */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct wifi_scan_result g_scan_priv =
+{
+ .scan_signal = SEM_INITIALIZER(0),
+};
+static uint8_t g_channel_num;
+static uint8_t g_channel_list[CHANNEL_MAX_NUM];
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_wifi_start_scan
+ *
+ * Description:
+ * Scan all available APs.
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_start_scan(struct iwreq *iwr)
+{
+ struct wifi_scan_result *priv = &g_scan_priv;
+ wifi_scan_config_t *config = NULL;
+ struct iw_scan_req *req;
+ int ret = 0;
+ int i;
+ uint8_t target_mac[MAC_LEN];
+ uint8_t target_ssid[SSID_MAX_LEN + 1];
+ memset(target_ssid, 0x0, sizeof(SSID_MAX_LEN + 1));
+
+ if (iwr == NULL)
+ {
+ wlerr("ERROR: Invalid ioctl cmd.\n");
+ return -EINVAL;
+ }
+
+ if (g_scan_priv.scan_status != ESP_SCAN_DISABLED)
+ {
+ return OK;
+ }
+
+ config = kmm_calloc(1, sizeof(wifi_scan_config_t));
+ if (config == NULL)
+ {
+ wlerr("ERROR: Cannot allocate result buffer\n");
+ return -ENOMEM;
+ }
+
+ g_channel_num = 0;
+ memset(g_channel_list, 0x0, CHANNEL_MAX_NUM);
+
+ if (iwr->u.data.pointer &&
+ iwr->u.data.length >= sizeof(struct iw_scan_req))
+ {
+ req = (struct iw_scan_req *)iwr->u.data.pointer;
+ config->scan_type = (req->scan_type == IW_SCAN_TYPE_ACTIVE ?
+ WIFI_SCAN_TYPE_ACTIVE : WIFI_SCAN_TYPE_PASSIVE);
+ if (iwr->u.data.flags & IW_SCAN_THIS_ESSID &&
+ req->essid_len < sizeof(target_ssid))
+ {
+ /* Scan specific ESSID */
+
+ config->show_hidden = true;
+ config->bssid = NULL;
+ memcpy(&target_ssid[0], req->essid, req->essid_len);
+ config->ssid = &target_ssid[0];
+ config->ssid[req->essid_len] = '\0';
+ }
+
+ if (iwr->u.data.flags & IW_SCAN_THIS_FREQ &&
+ req->num_channels > 0)
+ {
+ /* Scan specific channels */
+
+ DEBUGASSERT(req->num_channels <= CHANNEL_MAX_NUM);
+ g_channel_num = req->num_channels;
+ if (req->num_channels == 1)
+ {
+ config->channel = req->channel_list[0].m;
+ }
+ else
+ {
+ for (i = 0; i < req->num_channels; i++)
+ {
+ g_channel_list[i] = req->channel_list[i].m;
+ }
+ }
+ }
+
+ memset(target_mac, 0xff, MAC_LEN);
+ if (memcmp(req->bssid.sa_data, target_mac, MAC_LEN) != 0)
+ {
+ /* Scan specific bssid */
+
+ memcpy(target_mac, req->bssid.sa_data, MAC_LEN);
+ config->bssid = &target_mac[0];
+ }
+ }
+ else
+ {
+ /* Default scan parameters */
+
+ wlinfo("INFO: Use default scan parameters\n");
+ config->scan_type = WIFI_SCAN_TYPE_ACTIVE; /* Active scan */
+ }
+
+ esp_wifi_start();
+ ret = esp_wifi_scan_start(config, false);
+ if (ret != OK)
+ {
+ wlerr("ERROR: Scan error, ret: %d\n", ret);
+ ret = ERROR;
+ }
+ else
+ {
+ /* Allocate buffer to store scan result */
+
+ if (priv->scan_result == NULL)
+ {
+ priv->scan_result = kmm_malloc(WIFI_SCAN_RESULT_SIZE);
+ if (priv->scan_result == NULL)
+ {
+ wlerr("ERROR: Cannot allocate result buffer\n");
+ ret = -ENOMEM;
+ }
+ else
+ {
+ memset(priv->scan_result, 0x0, WIFI_SCAN_RESULT_SIZE);
+ }
+ }
+ }
+
+ if (config)
+ {
+ kmm_free(config);
+ config = NULL;
+ }
+
+ if (ret == OK)
+ {
+ wlinfo("INFO: start scan\n");
+ g_scan_priv.scan_status = ESP_SCAN_RUN;
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_get_scan_results
+ *
+ * Description:
+ * Get scan result
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_get_scan_results(struct iwreq *iwr)
+{
+ int ret = OK;
+ static bool scan_block = false;
+ struct wifi_scan_result *priv = &g_scan_priv;
+
+ if (g_scan_priv.scan_status == ESP_SCAN_RUN)
+ {
+ irqstate_t irqstate = enter_critical_section();
+ if (!scan_block)
+ {
+ scan_block = true;
+ leave_critical_section(irqstate);
+ nxsem_tickwait(&priv->scan_signal, SEC2TICK(SCAN_TIME_SEC));
+ scan_block = false;
+ }
+ else
+ {
+ leave_critical_section(irqstate);
+ ret = -EINVAL;
+ goto exit_failed;
+ }
+ }
+ else if (g_scan_priv.scan_status == ESP_SCAN_DISABLED)
+ {
+ ret = -EINVAL;
+ goto exit_failed;
+ }
+
+ if ((iwr == NULL) || (g_scan_priv.scan_status != ESP_SCAN_DONE))
+ {
+ ret = -EINVAL;
+ goto exit_failed;
+ }
+
+ if (priv->scan_result == NULL)
+ {
+ /* Result have already been requested */
+
+ ret = OK;
+ iwr->u.data.length = 0;
+ goto exit_failed;
+ }
+
+ if (priv->scan_result_size <= 0)
+ {
+ ret = OK;
+ iwr->u.data.length = 0;
+ goto exit_free_buffer;
+ }
+
+ if (iwr->u.data.pointer == NULL ||
+ iwr->u.data.length < priv->scan_result_size)
+ {
+ /* Stat request, return scan_result_size */
+
+ ret = -E2BIG;
+ iwr->u.data.pointer = NULL;
+ iwr->u.data.length = priv->scan_result_size;
+ return ret;
+ }
+
+ /* Copy result to user buffer */
+
+ if (iwr->u.data.length > priv->scan_result_size)
+ {
+ iwr->u.data.length = priv->scan_result_size;
+ }
+
+ memcpy(iwr->u.data.pointer, priv->scan_result, iwr->u.data.length);
+
+exit_free_buffer:
+
+ /* Free scan result buffer */
+
+ kmm_free(priv->scan_result);
+ priv->scan_result = NULL;
+ priv->scan_result_size = 0;
+ g_scan_priv.scan_status = ESP_SCAN_DISABLED;
+
+exit_failed:
+ if (ret < 0)
+ {
+ iwr->u.data.length = 0;
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_scan_event_parse
+ *
+ * Description:
+ * Parse scan information
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void esp_wifi_scan_event_parse(void)
+{
+ struct wifi_scan_result *priv = &g_scan_priv;
+ wifi_ap_record_t *ap_list_buffer = NULL;
+ uint16_t bss_total = 0;
+ uint8_t bss_count = 0;
+ bool parse_done = false;
+
+ if (priv->scan_status != ESP_SCAN_RUN)
+ {
+ return;
+ }
+
+ esp_wifi_scan_get_ap_num(&bss_total);
+ if (bss_total == 0)
+ {
+ priv->scan_status = ESP_SCAN_DONE;
+ wlinfo("INFO: None AP is scanned\n");
+ nxsem_post(&priv->scan_signal);
+ return;
+ }
+
+ ap_list_buffer = kmm_calloc(bss_total, sizeof(wifi_ap_record_t));
+ if (ap_list_buffer == NULL)
+ {
+ priv->scan_status = ESP_SCAN_DONE;
+ wlerr("ERROR: Failed to calloc buffer to print scan results");
+ nxsem_post(&priv->scan_signal);
+ return;
+ }
+
+ if (esp_wifi_scan_get_ap_records(&bss_total,
+ (wifi_ap_record_t *)ap_list_buffer) == OK)
+ {
+ struct iw_event *iwe;
+ unsigned int result_size;
+ size_t essid_len;
+ size_t essid_len_aligned;
+ bool is_target_channel = true;
+ int i;
+
+ for (bss_count = 0; bss_count < bss_total; bss_count++)
+ {
+ if (g_channel_num > 1)
+ {
+ is_target_channel = false;
+ for (i = 0; i < g_channel_num; i++)
+ {
+ if (g_channel_list[i] == ap_list_buffer[bss_count].primary)
+ {
+ is_target_channel = true;
+ break;
+ }
+ }
+ }
+ else
+ {
+ is_target_channel = true;
+ }
+
+ if (is_target_channel)
+ {
+ result_size = WIFI_SCAN_RESULT_SIZE - priv->scan_result_size;
+
+ /* Copy BSSID */
+
+ if (result_size < ESP_IW_EVENT_SIZE(ap_addr))
+ {
+ goto scan_result_full;
+ }
+
+ iwe = (struct iw_event *)
+ &priv->scan_result[priv->scan_result_size];
+ iwe->len = ESP_IW_EVENT_SIZE(ap_addr);
+ iwe->cmd = SIOCGIWAP;
+ memcpy(&iwe->u.ap_addr.sa_data,
+ ap_list_buffer[bss_count].bssid,
+ sizeof(ap_list_buffer[bss_count].bssid));
+ iwe->u.ap_addr.sa_family = ARPHRD_ETHER;
+ priv->scan_result_size += ESP_IW_EVENT_SIZE(ap_addr);
+ result_size -= ESP_IW_EVENT_SIZE(ap_addr);
+
+ /* Copy ESSID */
+
+ essid_len = MIN(strlen((const char *)
+ ap_list_buffer[bss_count].ssid), SSID_MAX_LEN);
+ essid_len_aligned = (essid_len + 3) & -4;
+ if (result_size < ESP_IW_EVENT_SIZE(essid) + essid_len_aligned)
+ {
+ goto scan_result_full;
+ }
+
+ iwe = (struct iw_event *)
+ &priv->scan_result[priv->scan_result_size];
+ iwe->len = ESP_IW_EVENT_SIZE(essid) + essid_len_aligned;
+ iwe->cmd = SIOCGIWESSID;
+ iwe->u.essid.flags = 0;
+ iwe->u.essid.length = essid_len;
+
+ /* Special processing for iw_point, set offset
+ * in pointer field.
+ */
+
+ iwe->u.essid.pointer = (void *)sizeof(iwe->u.essid);
+ memcpy(&iwe->u.essid + 1,
+ ap_list_buffer[bss_count].ssid, essid_len);
+
+ wlinfo("INFO: ssid %s\n", ap_list_buffer[bss_count].ssid);
+
+ priv->scan_result_size +=
+ ESP_IW_EVENT_SIZE(essid) + essid_len_aligned;
+ result_size -= ESP_IW_EVENT_SIZE(essid) + essid_len_aligned;
+
+ /* Copy link quality info */
+
+ if (result_size < ESP_IW_EVENT_SIZE(qual))
+ {
+ goto scan_result_full;
+ }
+
+ iwe = (struct iw_event *)
+ &priv->scan_result[priv->scan_result_size];
+ iwe->len = ESP_IW_EVENT_SIZE(qual);
+ iwe->cmd = IWEVQUAL;
+ iwe->u.qual.qual = 0x00;
+
+ wlinfo("INFO: signal %d\n", ap_list_buffer[bss_count].rssi);
+
+ iwe->u.qual.level = ap_list_buffer[bss_count].rssi;
+ iwe->u.qual.noise = 0x00;
+ iwe->u.qual.updated = IW_QUAL_DBM | IW_QUAL_ALL_UPDATED;
+
+ priv->scan_result_size += ESP_IW_EVENT_SIZE(qual);
+ result_size -= ESP_IW_EVENT_SIZE(qual);
+
+ /* Copy AP mode */
+
+ if (result_size < ESP_IW_EVENT_SIZE(mode))
+ {
+ goto scan_result_full;
+ }
+
+ iwe = (struct iw_event *)
+ &priv->scan_result[priv->scan_result_size];
+ iwe->len = ESP_IW_EVENT_SIZE(mode);
+ iwe->cmd = SIOCGIWMODE;
+ iwe->u.mode = IW_MODE_MASTER;
+ priv->scan_result_size += ESP_IW_EVENT_SIZE(mode);
+ result_size -= ESP_IW_EVENT_SIZE(mode);
+
+ /* Copy AP encryption mode */
+
+ if (result_size < ESP_IW_EVENT_SIZE(data))
+ {
+ goto scan_result_full;
+ }
+
+ iwe = (struct iw_event *)
+ &priv->scan_result[priv->scan_result_size];
+ iwe->len = ESP_IW_EVENT_SIZE(data);
+ iwe->cmd = SIOCGIWENCODE;
+ iwe->u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
+ iwe->u.data.length = 0;
+ iwe->u.essid.pointer = NULL;
+
+ priv->scan_result_size += ESP_IW_EVENT_SIZE(data);
+ result_size -= ESP_IW_EVENT_SIZE(data);
+
+ /* Copy AP channel */
+
+ if (result_size < ESP_IW_EVENT_SIZE(freq))
+ {
+ goto scan_result_full;
+ }
+
+ iwe = (struct iw_event *)
+ &priv->scan_result[priv->scan_result_size];
+ iwe->len = ESP_IW_EVENT_SIZE(freq);
+ iwe->cmd = SIOCGIWFREQ;
+ iwe->u.freq.e = 0;
+ iwe->u.freq.m = ap_list_buffer[bss_count].primary;
+
+ priv->scan_result_size += ESP_IW_EVENT_SIZE(freq);
+ result_size -= ESP_IW_EVENT_SIZE(freq);
+ }
+ }
+
+ parse_done = true;
+ }
+
+scan_result_full:
+
+ /* Continue instead of break to log dropped AP results */
+
+ if (!parse_done)
+ {
+ wlerr("ERROR: No more space in scan_result buffer\n");
+ }
+
+ if (ap_list_buffer)
+ {
+ kmm_free(ap_list_buffer);
+ ap_list_buffer = NULL;
+ }
+
+ priv->scan_status = ESP_SCAN_DONE;
+ nxsem_post(&priv->scan_signal);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_to_errno
+ *
+ * Description:
+ * Transform from ESP Wi-Fi error code to NuttX error code
+ *
+ * Input Parameters:
+ * err - ESP Wi-Fi error code
+ *
+ * Returned Value:
+ * NuttX error code defined in errno.h
+ *
+ ****************************************************************************/
+
+int32_t esp_wifi_to_errno(int err)
+{
+ int ret;
+
+ if (err < ESP_ERR_WIFI_BASE)
+ {
+ /* Unmask component error bits */
+
+ ret = err & 0xfff;
+
+ switch (ret)
+ {
+ case ESP_OK:
+ ret = OK;
+ break;
+ case ESP_ERR_NO_MEM:
+ ret = -ENOMEM;
+ break;
+
+ case ESP_ERR_INVALID_ARG:
+ ret = -EINVAL;
+ break;
+
+ case ESP_ERR_INVALID_STATE:
+ ret = -EIO;
+ break;
+
+ case ESP_ERR_INVALID_SIZE:
+ ret = -EINVAL;
+ break;
+
+ case ESP_ERR_NOT_FOUND:
+ ret = -ENOSYS;
+ break;
+
+ case ESP_ERR_NOT_SUPPORTED:
+ ret = -ENOSYS;
+ break;
+
+ case ESP_ERR_TIMEOUT:
+ ret = -ETIMEDOUT;
+ break;
+
+ case ESP_ERR_INVALID_MAC:
+ ret = -EINVAL;
+ break;
+
+ default:
+ ret = ERROR;
+ break;
+ }
+ }
+ else
+ {
+ ret = ERROR;
+ }
+
+ if (ret != OK)
+ {
+ wlerr("ERROR: %s\n", esp_err_to_name(err));
+ }
+
+ return ret;
+}
diff --git a/arch/risc-v/src/common/espressif/esp_wifi_utils.h b/arch/risc-v/src/common/espressif/esp_wifi_utils.h
new file mode 100644
index 0000000000..738fbbed08
--- /dev/null
+++ b/arch/risc-v/src/common/espressif/esp_wifi_utils.h
@@ -0,0 +1,120 @@
+/****************************************************************************
+ * arch/risc-v/src/common/espressif/esp_wifi_utils.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WIFI_UTILS_H
+#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WIFI_UTILS_H
+
+#include <nuttx/config.h>
+#include <nuttx/net/netdev.h>
+
+#ifndef __ASSEMBLY__
+
+#include <stdint.h>
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_wifi_start_scan
+ *
+ * Description:
+ * Scan all available APs.
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_start_scan(struct iwreq *iwr);
+
+/****************************************************************************
+ * Name: esp_wifi_get_scan_results
+ *
+ * Description:
+ * Get scan result
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_get_scan_results(struct iwreq *iwr);
+
+/****************************************************************************
+ * Name: esp_wifi_scan_event_parse
+ *
+ * Description:
+ * Parse scan information
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void esp_wifi_scan_event_parse(void);
+
+/****************************************************************************
+ * Name: esp_wifi_to_errno
+ *
+ * Description:
+ * Transform from ESP Wi-Fi error code to NuttX error code
+ *
+ * Input Parameters:
+ * err - ESP Wi-Fi error code
+ *
+ * Returned Value:
+ * NuttX error code defined in errno.h
+ *
+ ****************************************************************************/
+
+int32_t esp_wifi_to_errno(int err);
+
+#ifdef __cplusplus
+}
+#endif
+#undef EXTERN
+
+#endif /* __ASSEMBLY__ */
+#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WIFI_UTILS_H */
diff --git a/arch/risc-v/src/common/espressif/esp_wlan.c b/arch/risc-v/src/common/espressif/esp_wlan.c
new file mode 100644
index 0000000000..443b8d525c
--- /dev/null
+++ b/arch/risc-v/src/common/espressif/esp_wlan.c
@@ -0,0 +1,1731 @@
+/****************************************************************************
+ * arch/risc-v/src/common/espressif/esp_wlan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#ifdef CONFIG_ESPRESSIF_WIFI
+
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+#include <arpa/inet.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/crc64.h>
+#include <nuttx/irq.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/net/ip.h>
+#include <nuttx/net/netdev.h>
+#include <nuttx/nuttx.h>
+#include <nuttx/queue.h>
+#include <nuttx/signal.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/wdog.h>
+#include <nuttx/wqueue.h>
+
+#if defined(CONFIG_NET_PKT)
+# include <nuttx/net/pkt.h>
+#endif
+
+#include "esp_systemreset.h"
+#include "esp_wlan.h"
+#include "esp_wifi_utils.h"
+#include "esp_wifi_adapter.h"
+
+#include "esp_attr.h"
+#include "esp_mac.h"
+#include "esp_private/wifi.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* TX timeout = 1 minute */
+
+#define WLAN_TXTOUT (60 * CLK_TCK)
+
+/* Low-priority work queue processes RX/TX */
+
+#define WLAN_WORK LPWORK
+
+/* Ethernet frame:
+ * Resource address : 6 bytes
+ * Destination address: 6 bytes
+ * Type : 2 bytes
+ * Payload : MAX 1500
+ * Checksum : Ignore
+ *
+ * Total size : 1514
+ */
+
+#define WLAN_BUF_SIZE (CONFIG_NET_ETH_PKTSIZE + \
+ CONFIG_NET_LL_GUARDSIZE + \
+ CONFIG_NET_GUARDSIZE)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* WLAN operations */
+
+struct wlan_ops
+{
+ int (*start)(void);
+ int (*send)(void *pdata, size_t n);
+ int (*essid)(struct iwreq *iwr, bool set);
+ int (*bssid)(struct iwreq *iwr, bool set);
+ int (*passwd)(struct iwreq *iwr, bool set);
+ int (*mode)(struct iwreq *iwr, bool set);
+ int (*auth)(struct iwreq *iwr, bool set);
+ int (*freq)(struct iwreq *iwr, bool set);
+ int (*bitrate)(struct iwreq *iwr, bool set);
+ int (*txpower)(struct iwreq *iwr, bool set);
+ int (*channel)(struct iwreq *iwr, bool set);
+ int (*country)(struct iwreq *iwr, bool set);
+ int (*rssi)(struct iwreq *iwr, bool set);
+ int (*connect)(void);
+ int (*disconnect)(void);
+ int (*event)(pid_t pid, struct sigevent *event);
+ int (*stop)(void);
+};
+
+/* The wlan_priv_s encapsulates all state information for a single
+ * hardware interface
+ */
+
+struct wlan_priv_s
+{
+ int ref; /* Reference count */
+
+ bool ifup; /* true:ifup false:ifdown */
+
+ struct wdog_s txtimeout; /* TX timeout timer */
+
+ struct work_s rxwork; /* Send packet work */
+ struct work_s txwork; /* Receive packet work */
+ struct work_s pollwork; /* Poll work */
+ struct work_s toutwork; /* Send packet timeout work */
+
+ const struct wlan_ops *ops; /* WLAN operations */
+
+ /* This holds the information visible to the NuttX network */
+
+ struct net_driver_s dev;
+
+ /* RX packet queue */
+
+ struct iob_queue_s rxb;
+
+ /* TX ready packet queue */
+
+ struct iob_queue_s txb;
+
+ /* Flat buffer swap */
+
+ uint8_t flatbuf[WLAN_BUF_SIZE];
+
+ spinlock_t lock;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Reference count of register Wi-Fi handler */
+
+static uint8_t g_callback_register_ref;
+
+static struct wlan_priv_s g_wlan_priv[ESP_WLAN_DEVS];
+
+#ifdef ESP_WLAN_HAS_STA
+static const struct wlan_ops g_sta_ops =
+{
+ .start = esp_wifi_sta_start,
+ .send = esp_wifi_sta_send_data,
+ .essid = esp_wifi_sta_essid,
+ .bssid = esp_wifi_sta_bssid,
+ .passwd = esp_wifi_sta_password,
+ .mode = esp_wifi_sta_mode,
+ .auth = esp_wifi_sta_auth,
+ .freq = esp_wifi_sta_freq,
+ .bitrate = esp_wifi_sta_bitrate,
+ .txpower = esp_wifi_sta_txpower,
+ .channel = esp_wifi_sta_channel,
+ .country = esp_wifi_sta_country,
+ .rssi = esp_wifi_sta_rssi,
+ .connect = esp_wifi_sta_connect,
+ .disconnect = esp_wifi_sta_disconnect,
+ .event = esp_wifi_notify_subscribe,
+ .stop = esp_wifi_sta_stop
+};
+#endif /* ESP_WLAN_HAS_STA */
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+static const struct wlan_ops g_softap_ops =
+{
+ .start = esp_wifi_softap_start,
+ .send = esp_wifi_softap_send_data,
+ .essid = esp_wifi_softap_essid,
+ .bssid = esp_wifi_softap_bssid,
+ .passwd = esp_wifi_softap_password,
+ .mode = esp_wifi_softap_mode,
+ .auth = esp_wifi_softap_auth,
+ .freq = esp_wifi_softap_freq,
+ .bitrate = esp_wifi_softap_bitrate,
+ .txpower = esp_wifi_softap_txpower,
+ .channel = esp_wifi_softap_channel,
+ .country = esp_wifi_softap_country,
+ .rssi = esp_wifi_softap_rssi,
+ .connect = esp_wifi_softap_connect,
+ .disconnect = esp_wifi_softap_disconnect,
+ .event = esp_wifi_notify_subscribe,
+ .stop = esp_wifi_softap_stop
+};
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+/* Wi-Fi station TX done callback function */
+
+static wifi_tx_done_cb_t g_sta_txdone_cb;
+
+/* Wi-Fi SoftAP TX done callback function */
+
+static wifi_tx_done_cb_t g_softap_txdone_cb;
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Common TX logic */
+
+static void wlan_transmit(struct wlan_priv_s *priv);
+static void wlan_rxpoll(void *arg);
+static int wlan_txpoll(struct net_driver_s *dev);
+static void wlan_dopoll(struct wlan_priv_s *priv);
+
+/* Watchdog timer expirations */
+
+static void wlan_txtimeout_work(void *arg);
+static void wlan_txtimeout_expiry(wdparm_t arg);
+
+/* NuttX callback functions */
+
+static int wlan_ifup(struct net_driver_s *dev);
+static int wlan_ifdown(struct net_driver_s *dev);
+
+static void wlan_txavail_work(void *arg);
+static int wlan_txavail(struct net_driver_s *dev);
+
+#if defined(CONFIG_NET_MCASTGROUP) || defined(CONFIG_NET_ICMPv6)
+static int wlan_addmac(struct net_driver_s *dev, const uint8_t *mac);
+#endif
+
+#ifdef CONFIG_NET_MCASTGROUP
+static int wlan_rmmac(struct net_driver_s *dev, const uint8_t *mac);
+#endif
+
+#ifdef CONFIG_NETDEV_IOCTL
+static int wlan_ioctl(struct net_driver_s *dev, int cmd,
+ unsigned long arg);
+#endif
+
+static void esp_wifi_stop_callback(void);
+static void esp_wifi_free_eb(void *eb);
+#ifdef ESP_WLAN_HAS_STA
+static int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer,
+ uint16_t len,
+ void *eb));
+static void esp_wifi_sta_register_txdone_cb(wifi_tx_done_cb_t cb);
+#endif /* ESP_WLAN_HAS_STA */
+#ifdef ESP_WLAN_HAS_SOFTAP
+static int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer,
+ uint16_t len,
+ void *eb));
+static void esp_wifi_softap_register_txdone_cb(wifi_tx_done_cb_t cb);
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Note:
+ * All TX done/RX done/Error trigger functions are not called from
+ * interrupts, this is much different from ethernet driver, including:
+ * * wlan_rx_done
+ * * wlan_tx_done
+ *
+ * These functions are called in a Wi-Fi private thread. So we just use
+ * mutex/semaphore instead of disable interrupt, if necessary.
+ */
+
+/****************************************************************************
+ * Function: wlan_cache_txpkt_tail
+ *
+ * Description:
+ * Cache packet from dev->d_buf into tail of TX ready queue.
+ *
+ * Input Parameters:
+ * priv - Reference to the driver state structure
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static inline void wlan_cache_txpkt_tail(struct wlan_priv_s *priv)
+{
+ if (priv->dev.d_iob)
+ {
+ iob_tryadd_queue(priv->dev.d_iob, &priv->txb);
+ }
+
+ netdev_iob_clear(&priv->dev);
+}
+
+/****************************************************************************
+ * Function: wlan_recvframe
+ *
+ * Description:
+ * Try to receive RX packet from RX done packet queue.
+ *
+ * Input Parameters:
+ * priv - Reference to the driver state structure
+ *
+ * Returned Value:
+ * RX packet if success or NULl if no packet in queue.
+ *
+ ****************************************************************************/
+
+static struct iob_s *wlan_recvframe(struct wlan_priv_s *priv)
+{
+ struct iob_s *iob;
+
+ iob = iob_remove_queue(&priv->rxb);
+
+ return iob;
+}
+
+/****************************************************************************
+ * Name: wlan_transmit
+ *
+ * Description:
+ * Try to send all TX packets in TX ready queue to Wi-Fi driver. If this
+ * sending fails, then breaks loop and returns.
+ *
+ * Input Parameters:
+ * priv - Reference to the driver state structure
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wlan_transmit(struct wlan_priv_s *priv)
+{
+ uint16_t llhdrlen = NET_LL_HDRLEN(&priv->dev);
+ unsigned int offset = CONFIG_NET_LL_GUARDSIZE - llhdrlen;
+ struct iob_s *iob;
+ int ret;
+
+ while ((iob = iob_peek_queue(&priv->txb)) != NULL)
+ {
+ iob_copyout(priv->flatbuf + llhdrlen, iob, iob->io_pktlen, 0);
+ memcpy(priv->flatbuf, iob->io_data + offset, llhdrlen);
+
+ ret = priv->ops->send(priv->flatbuf, iob->io_pktlen + llhdrlen);
+ if (ret == -ENOMEM)
+ {
+ wd_start(&priv->txtimeout, WLAN_TXTOUT,
+ wlan_txtimeout_expiry, (uint32_t)priv);
+ break;
+ }
+ else
+ {
+ if (ret < 0)
+ {
+ nwarn("WARN: Failed to send pkt, ret: %d\n", ret);
+ }
+
+ iob_remove_queue(&priv->txb);
+
+ /* And free the I/O buffer chain */
+
+ iob_free_chain(iob);
+ }
+ }
+}
+
+/****************************************************************************
+ * Name: wlan_tx_done
+ *
+ * Description:
+ * Wi-Fi TX done callback function. If this is called, it means sending
+ * next packet.
+ *
+ * Input Parameters:
+ * priv - Reference to the driver state structure
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wlan_tx_done(struct wlan_priv_s *priv)
+{
+ wd_cancel(&priv->txtimeout);
+
+ wlan_txavail(&priv->dev);
+}
+
+/****************************************************************************
+ * Function: wlan_rx_done
+ *
+ * Description:
+ * Wi-Fi RX done callback function. If this is called, it means receiving
+ * packet.
+ *
+ * Input Parameters:
+ * priv - Reference to the driver state structure
+ * buffer - Wi-Fi received packet buffer
+ * len - Length of received packet
+ * eb - Wi-Fi receive callback input eb pointer
+ *
+ * Returned Value:
+ * 0 on success or a negated errno on failure
+ *
+ ****************************************************************************/
+
+static int wlan_rx_done(struct wlan_priv_s *priv, void *buffer,
+ uint16_t len, void *eb)
+{
+ struct net_driver_s *dev = &priv->dev;
+ struct iob_s *iob = NULL;
+ irqstate_t flags;
+ int ret = 0;
+
+ if (!priv->ifup)
+ {
+ goto out;
+ }
+
+ if (len > WLAN_BUF_SIZE)
+ {
+ nwarn("ERROR: Wlan receive %d larger than %d\n",
+ len, WLAN_BUF_SIZE);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ if (len > iob_navail(false) * CONFIG_IOB_BUFSIZE)
+ {
+ ret = -ENOBUFS;
+ goto out;
+ }
+
+ iob = iob_tryalloc(false);
+ if (iob == NULL)
+ {
+ ret = -ENOBUFS;
+ goto out;
+ }
+
+ iob_reserve(iob, CONFIG_NET_LL_GUARDSIZE - NET_LL_HDRLEN(dev));
+
+ ret = iob_trycopyin(iob, buffer, len, 0, false);
+ if (ret != len)
+ {
+ ret = -ENOBUFS;
+ goto out;
+ }
+
+ flags = spin_lock_irqsave(&priv->lock);
+ ret = iob_tryadd_queue(iob, &priv->rxb);
+ spin_unlock_irqrestore(&priv->lock, flags);
+
+ if (ret < 0)
+ {
+ ret = -ENOBUFS;
+ goto out;
+ }
+
+out:
+
+ if (eb != NULL)
+ {
+ esp_wifi_free_eb(eb);
+ }
+
+ if (ret != OK && iob != NULL)
+ {
+ iob_free_chain(iob);
+ }
+
+ if (work_available(&priv->rxwork))
+ {
+ work_queue(WLAN_WORK, &priv->rxwork, wlan_rxpoll, priv, 0);
+ }
+
+ wlan_txavail(&priv->dev);
+
+ return ret;
+}
+
+/****************************************************************************
+ * Function: wlan_rxpoll
+ *
+ * Description:
+ * Try to receive packets from RX done queue and pass packets into IP
+ * stack and send packets which is from IP stack if necessary.
+ *
+ * Input Parameters:
+ * priv - Reference to the driver state structure
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wlan_rxpoll(void *arg)
+{
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)arg;
+ struct net_driver_s *dev = &priv->dev;
+ struct eth_hdr_s *eth_hdr;
+ struct iob_s *iob;
+
+ /* Try to send all cached TX packets for TX ack and so on */
+
+ wlan_transmit(priv);
+
+ /* Loop while while iob_remove_queue() successfully retrieves valid
+ * Ethernet frames.
+ */
+
+ net_lock();
+
+ while ((iob = wlan_recvframe(priv)) != NULL)
+ {
+ dev->d_iob = iob;
+ dev->d_len = iob->io_pktlen;
+
+ iob_reserve(iob, CONFIG_NET_LL_GUARDSIZE);
+
+#ifdef CONFIG_NET_PKT
+
+ /* When packet sockets are enabled,
+ * feed the frame into the packet tap.
+ */
+
+ pkt_input(&priv->dev);
+#endif
+
+ eth_hdr = (struct eth_hdr_s *)
+ &dev->d_iob->io_data[CONFIG_NET_LL_GUARDSIZE -
+ NET_LL_HDRLEN(dev)];
+
+ /* We only accept IP packets of the configured type and ARP packets */
+
+#ifdef CONFIG_NET_IPv4
+ if (eth_hdr->type == HTONS(ETHTYPE_IP))
+ {
+ ninfo("IPv4 frame\n");
+
+ /* Receive an IPv4 packet from the network device */
+
+ ipv4_input(&priv->dev);
+
+ /* If the above function invocation resulted in data
+ * that should be sent out on the network,
+ * the field d_len will set to a value > 0.
+ */
+
+ if (priv->dev.d_len > 0)
+ {
+ /* And send the packet */
+
+ wlan_cache_txpkt_tail(priv);
+ }
+ }
+ else
+#endif
+#ifdef CONFIG_NET_IPv6
+ if (eth_hdr->type == HTONS(ETHTYPE_IP6))
+ {
+ ninfo("IPv6 frame\n");
+
+ /* Give the IPv6 packet to the network layer */
+
+ ipv6_input(&priv->dev);
+
+ /* If the above function invocation resulted in data
+ * that should be sent out on the network, the field
+ * d_len will set to a value > 0.
+ */
+
+ if (priv->dev.d_len > 0)
+ {
+ /* And send the packet */
+
+ wlan_cache_txpkt_tail(priv);
+ }
+ }
+ else
+#endif
+#ifdef CONFIG_NET_ARP
+ if (eth_hdr->type == HTONS(ETHTYPE_ARP))
+ {
+ ninfo("ARP frame\n");
+
+ /* Handle ARP packet */
+
+ arp_input(&priv->dev);
+
+ /* If the above function invocation resulted in data
+ * that should be sent out on the network, the field
+ * d_len will set to a value > 0.
+ */
+
+ if (priv->dev.d_len > 0)
+ {
+ wlan_cache_txpkt_tail(priv);
+ }
+ }
+ else
+#endif
+ {
+ ninfo("INFO: Dropped, Unknown type: %04x\n", eth_hdr->type);
+ }
+
+ netdev_iob_release(&priv->dev);
+ }
+
+ /* Try to send all cached TX packets */
+
+ wlan_transmit(priv);
+
+ net_unlock();
+}
+
+/****************************************************************************
+ * Name: wlan_txpoll
+ *
+ * Description:
+ * The transmitter is available, check if the network has any outgoing
+ * packets ready to send. This is a callback from devif_poll().
+ * devif_poll() may be called:
+ *
+ * 1. When the preceding TX packets send times out and the interface is
+ * reset
+ * 2. During normal TX polling
+ *
+ * Input Parameters:
+ * dev - Reference to the NuttX driver state structure
+ *
+ * Returned Value:
+ * OK on success; a negated errno on failure
+ *
+ ****************************************************************************/
+
+static int wlan_txpoll(struct net_driver_s *dev)
+{
+ struct wlan_priv_s *priv = dev->d_private;
+
+ wlan_cache_txpkt_tail(priv);
+ wlan_transmit(priv);
+
+ return OK;
+}
+
+/****************************************************************************
+ * Function: wlan_dopoll
+ *
+ * Description:
+ * The function is called in order to perform an out-of-sequence TX poll.
+ * This is done:
+ *
+ * 1. When new TX data is available (wlan_txavail)
+ * 2. After a TX timeout to restart the sending process
+ * (wlan_txtimeout_expiry).
+ *
+ * Input Parameters:
+ * priv - Reference to the driver state structure
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wlan_dopoll(struct wlan_priv_s *priv)
+{
+ struct net_driver_s *dev = &priv->dev;
+
+ /* Try to let TCP/IP to send all packets to netcard driver */
+
+ while (devif_poll(dev, wlan_txpoll));
+
+ /* Try to send all cached TX packets */
+
+ wlan_transmit(priv);
+}
+
+/****************************************************************************
+ * Function: wlan_txtimeout_work
+ *
+ * Description:
+ * Perform TX timeout related work from the worker thread
+ *
+ * Input Parameters:
+ * arg - The argument passed when work_queue() as called.
+ *
+ * Returned Value:
+ * OK on success
+ *
+ ****************************************************************************/
+
+static void wlan_txtimeout_work(void *arg)
+{
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)arg;
+
+ /* Try to send all cached TX packets */
+
+ wlan_transmit(priv);
+
+ net_lock();
+
+ wlan_ifdown(&priv->dev);
+ wlan_ifup(&priv->dev);
+
+ /* Then poll for new XMIT data */
+
+ wlan_dopoll(priv);
+
+ net_unlock();
+}
+
+/****************************************************************************
+ * Function: wlan_txtimeout_expiry
+ *
+ * Description:
+ * Our TX watchdog timed out. Called from the timer callback handler.
+ * The last TX never completed. Reset the hardware and start again.
+ *
+ * Input Parameters:
+ * arg - The reference of the private driver structure
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wlan_txtimeout_expiry(wdparm_t arg)
+{
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)arg;
+
+ /* Schedule to perform the TX timeout processing on the worker thread. */
+
+ if (work_available(&priv->toutwork))
+ {
+ work_queue(WLAN_WORK, &priv->toutwork, wlan_txtimeout_work, priv, 0);
+ }
+}
+
+/****************************************************************************
+ * Name: wlan_txavail_work
+ *
+ * Description:
+ * Perform an out-of-cycle poll on the worker thread.
+ *
+ * Input Parameters:
+ * arg - Reference to the NuttX driver state structure (cast to void*)
+ *
+ * Returned Value:
+ * None
+ *
+ * Assumptions:
+ * Called on the higher priority worker thread.
+ *
+ ****************************************************************************/
+
+static void wlan_txavail_work(void *arg)
+{
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)arg;
+
+ /* Try to send all cached TX packets even if net is down */
+
+ wlan_transmit(priv);
+
+ /* Lock the network and serialize driver operations if necessary.
+ * NOTE: Serialization is only required in the case where the driver work
+ * is performed on an LP worker thread and where more than one LP worker
+ * thread has been configured.
+ */
+
+ net_lock();
+
+ /* Ignore the notification if the interface is not yet up */
+
+ if (priv->ifup)
+ {
+ /* Poll the network for new XMIT data */
+
+ wlan_dopoll(priv);
+ }
+
+ net_unlock();
+}
+
+/****************************************************************************
+ * Name: wlan_ifup
+ *
+ * Description:
+ * NuttX Callback: Bring up the Ethernet interface when an IP address is
+ * provided
+ *
+ * Input Parameters:
+ * dev - Reference to the NuttX driver state structure
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static int wlan_ifup(struct net_driver_s *dev)
+{
+ int ret;
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private;
+
+#ifdef CONFIG_NET_IPv4
+ ninfo("Bringing up: %u.%u.%u.%u\n",
+ ip4_addr1(dev->d_ipaddr), ip4_addr2(dev->d_ipaddr),
+ ip4_addr3(dev->d_ipaddr), ip4_addr4(dev->d_ipaddr));
+#endif
+#ifdef CONFIG_NET_IPv6
+ ninfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
+ dev->d_ipv6addr[0], dev->d_ipv6addr[1], dev->d_ipv6addr[2],
+ dev->d_ipv6addr[3], dev->d_ipv6addr[4], dev->d_ipv6addr[5],
+ dev->d_ipv6addr[6], dev->d_ipv6addr[7]);
+#endif
+
+ net_lock();
+
+ if (priv->ifup)
+ {
+ net_unlock();
+ return OK;
+ }
+
+ ret = priv->ops->start();
+ if (ret < 0)
+ {
+ net_unlock();
+ nerr("ERROR: Failed to start Wi-Fi ret=%d\n", ret);
+ return ret;
+ }
+
+ IOB_QINIT(&priv->rxb);
+ IOB_QINIT(&priv->txb);
+
+ priv->dev.d_buf = NULL;
+ priv->dev.d_len = 0;
+
+ priv->ifup = true;
+ if (g_callback_register_ref == 0)
+ {
+ ret = esp_register_shutdown_handler(esp_wifi_stop_callback);
+ if (ret < 0)
+ {
+ nwarn("WARN: Failed to register handler ret=%d\n", ret);
+ }
+ }
+
+ ++g_callback_register_ref;
+ net_unlock();
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: wlan_ifdown
+ *
+ * Description:
+ * NuttX Callback: Stop the interface.
+ *
+ * Input Parameters:
+ * dev - Reference to the NuttX driver state structure
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static int wlan_ifdown(struct net_driver_s *dev)
+{
+ int ret;
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private;
+
+ net_lock();
+
+ if (!priv->ifup)
+ {
+ net_unlock();
+ return OK;
+ }
+
+ /* Cancel the TX poll timer and TX timeout timers */
+
+ wd_cancel(&priv->txtimeout);
+
+ /* Mark the device "down" */
+
+ priv->ifup = false;
+
+ iob_free_queue(&priv->rxb);
+ iob_free_queue(&priv->txb);
+
+ ret = priv->ops->stop();
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to stop Wi-Fi ret=%d\n", ret);
+ }
+
+ --g_callback_register_ref;
+ if (g_callback_register_ref == 0)
+ {
+ ret = esp_unregister_shutdown_handler(esp_wifi_stop_callback);
+ if (ret < 0)
+ {
+ nwarn("WARN: Failed to unregister handler ret=%d\n", ret);
+ }
+ }
+
+ net_unlock();
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: wlan_txavail
+ *
+ * Description:
+ * Driver callback invoked when new TX data is available. This is a
+ * stimulus perform an out-of-cycle poll and, thereby, reduce the TX
+ * latency.
+ *
+ * Input Parameters:
+ * dev - Reference to the NuttX driver state structure
+ *
+ * Returned Value:
+ * None
+ *
+ * Assumptions:
+ * Called in normal user mode
+ *
+ ****************************************************************************/
+
+static int wlan_txavail(struct net_driver_s *dev)
+{
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private;
+
+ if (work_available(&priv->txwork))
+ {
+ /* Schedule to serialize the poll on the worker thread. */
+
+ work_queue(WLAN_WORK, &priv->txwork, wlan_txavail_work, priv, 0);
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: wlan_addmac
+ *
+ * Description:
+ * NuttX Callback: Add the specified MAC address to the hardware multicast
+ * address filtering
+ *
+ * Input Parameters:
+ * dev - Reference to the NuttX driver state structure
+ * mac - The MAC address to be added
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_NET_MCASTGROUP) || defined(CONFIG_NET_ICMPv6)
+static int wlan_addmac(struct net_driver_s *dev, const uint8_t *mac)
+{
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private;
+
+ /* Add the MAC address to the hardware multicast routing table */
+
+ return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: wlan_rmmac
+ *
+ * Description:
+ * NuttX Callback: Remove the specified MAC address from the
+ * hardware multicast address filtering
+ *
+ * Input Parameters:
+ * dev - Reference to the NuttX driver state structure
+ * mac - The MAC address to be removed
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NET_MCASTGROUP
+static int wlan_rmmac(struct net_driver_s *dev, const uint8_t *mac)
+{
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private;
+
+ /* Add the MAC address to the hardware multicast routing table */
+
+ return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: wlan_ioctl
+ *
+ * Description:
+ * Handle network IOCTL commands directed to this device.
+ *
+ * Input Parameters:
+ * dev - Reference to the NuttX driver state structure
+ * cmd - The IOCTL command
+ * arg - The argument for the IOCTL command
+ *
+ * Returned Value:
+ * OK on success; Negated errno on failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NETDEV_IOCTL
+static int wlan_ioctl(struct net_driver_s *dev,
+ int cmd,
+ unsigned long arg)
+{
+ int ret;
+ struct iwreq *iwr = (struct iwreq *)arg;
+ struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private;
+ const struct wlan_ops *ops = priv->ops;
+
+ /* Decode and dispatch the driver-specific IOCTL command */
+
+ switch (cmd)
+ {
+#ifdef CONFIG_NETDEV_PHY_IOCTL
+#ifdef CONFIG_ARCH_PHY_INTERRUPT
+ case SIOCMIINOTIFY: /* Set up for PHY event notifications */
+ {
+ struct mii_ioctl_notify_s *req = (struct mii_ioctl_notify_s *)arg;
+ ret = ops->event(req->pid, &req->event);
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to subscribe event\n");
+ }
+ }
+ break;
+#endif
+#endif
+
+ case SIOCSIWENCODEEXT:
+ ret = ops->passwd(iwr, true);
+
+ break;
+
+ case SIOCGIWENCODEEXT:
+ ret = ops->passwd(iwr, false);
+ break;
+
+ case SIOCSIWESSID:
+ if ((iwr->u.essid.flags == IW_ESSID_ON) ||
+ (iwr->u.essid.flags == IW_ESSID_DELAY_ON))
+ {
+ ret = ops->essid(iwr, true);
+ if (ret < 0)
+ {
+ break;
+ }
+
+ if (iwr->u.essid.flags == IW_ESSID_ON)
+ {
+ ret = ops->connect();
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to connect\n");
+ break;
+ }
+ }
+ }
+ else
+ {
+ ret = ops->disconnect();
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to disconnect\n");
+ break;
+ }
+ }
+
+ break;
+
+ case SIOCGIWESSID: /* Get ESSID */
+ ret = ops->essid(iwr, false);
+ break;
+
+ case SIOCSIWAP: /* Set access point MAC addresses */
+ if (iwr->u.ap_addr.sa_data[0] != 0 &&
+ iwr->u.ap_addr.sa_data[1] != 0 &&
+ iwr->u.ap_addr.sa_data[2] != 0)
+ {
+ ret = ops->bssid(iwr, true);
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to set BSSID\n");
+ break;
+ }
+
+ ret = ops->connect();
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to connect\n");
+ break;
+ }
+ }
+ else
+ {
+ ret = ops->disconnect();
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to disconnect\n");
+ break;
+ }
+ }
+
+ break;
+
+ case SIOCGIWAP: /* Get access point MAC addresses */
+ ret = ops->bssid(iwr, false);
+ break;
+
+ case SIOCSIWSCAN:
+ ret = esp_wifi_start_scan(iwr);
+ break;
+
+ case SIOCGIWSCAN:
+ ret = esp_wifi_get_scan_results(iwr);
+ break;
+
+ case SIOCSIWCOUNTRY: /* Set country code */
+ ret = ops->country(iwr, true);
+ break;
+
+ case SIOCGIWSENS: /* Get sensitivity (dBm) */
+ ret = ops->rssi(iwr, false);
+ break;
+
+ case SIOCSIWMODE: /* Set operation mode */
+ ret = ops->mode(iwr, true);
+ break;
+
+ case SIOCGIWMODE: /* Get operation mode */
+ ret = ops->mode(iwr, false);
+ break;
+
+ case SIOCSIWAUTH: /* Set authentication mode params */
+ ret = ops->auth(iwr, true);
+ break;
+
+ case SIOCGIWAUTH: /* Get authentication mode params */
+ ret = ops->auth(iwr, false);
+ break;
+
+ case SIOCSIWFREQ: /* Set channel/frequency (MHz) */
+ ret = ops->freq(iwr, true);
+ break;
+
+ case SIOCGIWFREQ: /* Get channel/frequency (MHz) */
+ ret = ops->freq(iwr, false);
+ break;
+
+ case SIOCSIWRATE: /* Set default bit rate (Mbps) */
+ wlwarn("WARNING: SIOCSIWRATE not implemented\n");
+ ret = -ENOSYS;
+ break;
+
+ case SIOCGIWRATE: /* Get default bit rate (Mbps) */
+ ret = ops->bitrate(iwr, false);
+ break;
+
+ case SIOCSIWTXPOW: /* Set transmit power (dBm) */
+ ret = ops->txpower(iwr, true);
+ break;
+
+ case SIOCGIWTXPOW: /* Get transmit power (dBm) */
+ ret = ops->txpower(iwr, false);
+ break;
+
+ case SIOCGIWRANGE: /* Get range of parameters */
+ ret = ops->channel(iwr, false);
+ break;
+
+ default:
+ nerr("ERROR: Unrecognized IOCTL command: %d\n", cmd);
+ ret = -ENOTTY; /* Special return value for this case */
+ break;
+ }
+
+ return ret;
+}
+#endif /* CONFIG_NETDEV_IOCTL */
+
+/****************************************************************************
+ * Name: esp_net_initialize
+ *
+ * Description:
+ * Initialize the network driver.
+ *
+ * Input Parameters:
+ * devno - The device number.
+ * mac_addr - MAC address.
+ * ops - A pointer to the structure containing the WLAN operations
+ * functions.
+ *
+ * Returned Value:
+ * OK on success; Negated errno on failure.
+ *
+ ****************************************************************************/
+
+static int esp_net_initialize(int devno, uint8_t *mac_addr,
+ const struct wlan_ops *ops)
+{
+ int ret;
+ struct wlan_priv_s *priv;
+ struct net_driver_s *netdev;
+
+ priv = &g_wlan_priv[devno];
+ if (priv->ref)
+ {
+ priv->ref++;
+ return OK;
+ }
+
+ netdev = &priv->dev;
+
+ /* Initialize the driver structure */
+
+ memset(priv, 0, sizeof(struct wlan_priv_s));
+
+ netdev->d_ifup = wlan_ifup; /* I/F down callback */
+ netdev->d_ifdown = wlan_ifdown; /* I/F up (new IP address) callback */
+ netdev->d_txavail = wlan_txavail; /* New TX data callback */
+#ifdef CONFIG_NET_MCASTGROUP
+ netdev->d_addmac = wlan_addmac; /* Add multicast MAC address */
+ netdev->d_rmmac = wlan_rmmac; /* Remove multicast MAC address */
+#endif
+#ifdef CONFIG_NETDEV_IOCTL
+ netdev->d_ioctl = wlan_ioctl; /* Handle network IOCTL commands */
+#endif
+
+ /* Used to recover private state from dev */
+
+ netdev->d_private = (void *)priv;
+
+ memcpy(netdev->d_mac.ether.ether_addr_octet, mac_addr, MAC_LEN);
+
+ ret = netdev_register(netdev, NET_LL_IEEE80211);
+ if (ret < 0)
+ {
+ nerr("ERROR: Initialization of IEEE 802.11 block failed: %d\n", ret);
+ return ret;
+ }
+
+ priv->ops = ops;
+
+ priv->ref++;
+
+ ninfo("INFO: Initialize Wi-Fi adapter No.%d success\n", devno);
+
+ return OK;
+}
+
+/****************************************************************************
+ * Function: wlan_sta_rx_done
+ *
+ * Description:
+ * Wi-Fi station RX done callback function. If this is called, it means
+ * station receiveing packet.
+ *
+ * Input Parameters:
+ * buffer - Wi-Fi received packet buffer
+ * len - Length of received packet
+ * eb - Wi-Fi receive callback input eb pointer
+ *
+ * Returned Value:
+ * 0 on success or a negated errno on failure
+ *
+ ****************************************************************************/
+
+#ifdef ESP_WLAN_HAS_STA
+static int wlan_sta_rx_done(void *buffer, uint16_t len, void *eb)
+{
+ struct wlan_priv_s *priv = &g_wlan_priv[ESP_WLAN_STA_DEVNO];
+
+ return wlan_rx_done(priv, buffer, len, eb);
+}
+
+/****************************************************************************
+ * Name: wlan_sta_tx_done
+ *
+ * Description:
+ * Wi-Fi station TX done callback function. If this is called, it means
+ * station sending next packet.
+ *
+ * Input Parameters:
+ * ifidx - The interface ID that the TX callback has been triggered from.
+ * data - Pointer to the data transmitted.
+ * len - Length of the data transmitted.
+ * status - True if data was transmitted successfully or false if failed.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wlan_sta_tx_done(uint8_t ifidx,
+ uint8_t *data,
+ uint16_t *len,
+ bool status)
+{
+ struct wlan_priv_s *priv = &g_wlan_priv[ESP_WLAN_STA_DEVNO];
+
+ wlan_tx_done(priv);
+}
+#endif /* ESP_WLAN_HAS_STA */
+
+/****************************************************************************
+ * Function: wlan_softap_rx_done
+ *
+ * Description:
+ * Wi-Fi softAP RX done callback function. If this is called, it means
+ * softAP receiveing packet.
+ *
+ * Input Parameters:
+ * buffer - Wi-Fi received packet buffer
+ * len - Length of received packet
+ * eb - Wi-Fi receive callback input eb pointer
+ *
+ * Returned Value:
+ * 0 on success or a negated errno on failure
+ *
+ ****************************************************************************/
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+static int wlan_softap_rx_done(void *buffer, uint16_t len, void *eb)
+{
+ struct wlan_priv_s *priv = &g_wlan_priv[ESP_WLAN_SOFTAP_DEVNO];
+
+ return wlan_rx_done(priv, buffer, len, eb);
+}
+
+/****************************************************************************
+ * Name: wlan_softap_tx_done
+ *
+ * Description:
+ * Wi-Fi softAP TX done callback function. If this is called, it means
+ * softAP sending next packet.
+ *
+ * Input Parameters:
+ * ifidx - The interface ID that the TX callback has been triggered from.
+ * data - Pointer to the data transmitted.
+ * len - Length of the data transmitted.
+ * status - True if data was transmitted successfully or false if failed.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wlan_softap_tx_done(uint8_t ifidx,
+ uint8_t *data,
+ uint16_t *len,
+ bool status)
+{
+ struct wlan_priv_s *priv = &g_wlan_priv[ESP_WLAN_SOFTAP_DEVNO];
+
+ wlan_tx_done(priv);
+}
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+/****************************************************************************
+ * Name: esp_wifi_stop_callback
+ *
+ * Description:
+ * Callback to stop Wi-Fi.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void esp_wifi_stop_callback(void)
+{
+ wlinfo("INFO: Try to stop Wi-Fi\n");
+
+ int ret = esp_wifi_stop();
+ if (ret)
+ {
+ wlerr("ERROR: Failed to stop Wi-Fi ret=%d\n", ret);
+ DEBUGPANIC();
+ }
+}
+
+/****************************************************************************
+ * Name: esp_wifi_free_eb
+ *
+ * Description:
+ * Free Wi-Fi receive callback input eb pointer
+ *
+ * Input Parameters:
+ * eb - Wi-Fi receive callback input eb pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void esp_wifi_free_eb(void *eb)
+{
+ esp_wifi_internal_free_rx_buffer(eb);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_register_recv_cb
+ *
+ * Description:
+ * Register Wi-Fi station receive packet callback function
+ *
+ * Input Parameters:
+ * recv_cb - Receive callback function
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+#ifdef ESP_WLAN_HAS_STA
+static int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer,
+ uint16_t len,
+ void *eb))
+{
+ int ret;
+
+ ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, (wifi_rxcb_t)recv_cb);
+
+ return esp_wifi_to_errno(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_register_txdone_cb
+ *
+ * Description:
+ * Register the station TX done callback function.
+ *
+ * Input Parameters:
+ * cb - The callback function
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void esp_wifi_sta_register_txdone_cb(wifi_tx_done_cb_t cb)
+{
+ g_sta_txdone_cb = cb;
+}
+#endif /* ESP_WLAN_HAS_STA */
+
+/****************************************************************************
+ * Name: esp_wifi_softap_register_recv_cb
+ *
+ * Description:
+ * Register Wi-Fi SoftAP receive packet callback function
+ *
+ * Input Parameters:
+ * recv_cb - Receive callback function
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+static int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer,
+ uint16_t len,
+ void *eb))
+{
+ int ret;
+
+ ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, (wifi_rxcb_t)recv_cb);
+
+ return esp_wifi_to_errno(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_register_txdone_cb
+ *
+ * Description:
+ * Register the SoftAP TX done callback function.
+ *
+ * Input Parameters:
+ * cb - The callback function
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void esp_wifi_softap_register_txdone_cb(wifi_tx_done_cb_t cb)
+{
+ g_softap_txdone_cb = cb;
+}
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_wlan_sta_set_linkstatus
+ *
+ * Description:
+ * Set Wi-Fi station link status
+ *
+ * Parameters:
+ * linkstatus - true Notifies the networking layer about an available
+ * carrier, false Notifies the networking layer about an
+ * disappeared carrier.
+ *
+ * Returned Value:
+ * OK on success; Negated errno on failure.
+ *
+ ****************************************************************************/
+
+#ifdef ESP_WLAN_HAS_STA
+int esp_wlan_sta_set_linkstatus(bool linkstatus)
+{
+ struct wlan_priv_s *priv = &g_wlan_priv[ESP_WLAN_STA_DEVNO];
+
+ if (linkstatus)
+ {
+ netdev_carrier_on(&priv->dev);
+ }
+ else
+ {
+ netdev_carrier_off(&priv->dev);
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_wlan_sta_initialize
+ *
+ * Description:
+ * Initialize the WLAN station netcard driver
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * OK on success; Negated errno on failure.
+ *
+ ****************************************************************************/
+
+int esp_wlan_sta_initialize(void)
+{
+ int ret;
+ uint8_t mac[6];
+
+ ret = esp_wifi_adapter_init();
+ if (ret < 0)
+ {
+ nerr("ERROR: Initialize Wi-Fi adapter error: %d\n", ret);
+ return ret;
+ }
+
+ ret = esp_read_mac(mac, ESP_MAC_WIFI_STA);
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to read MAC address\n");
+ return ret;
+ }
+
+ ninfo("Wi-Fi station MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
+ mac[0], mac[1], mac[2],
+ mac[3], mac[4], mac[5]);
+
+ ret = esp_net_initialize(ESP_WLAN_STA_DEVNO, mac, &g_sta_ops);
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to initialize net\n");
+ return ret;
+ }
+
+ ret = esp_wifi_sta_register_recv_cb(wlan_sta_rx_done);
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to register RX callback\n");
+ return ret;
+ }
+
+ esp_wifi_sta_register_txdone_cb(wlan_sta_tx_done);
+
+ ninfo("INFO: Initialize Wi-Fi station success net\n");
+
+ return OK;
+}
+#endif /* ESP_WLAN_HAS_STA */
+
+/****************************************************************************
+ * Name: esp_wlan_softap_initialize
+ *
+ * Description:
+ * Initialize the ESP32-S3 WLAN softAP netcard driver
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * OK on success; Negated errno on failure.
+ *
+ ****************************************************************************/
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+int esp_wlan_softap_initialize(void)
+{
+ int ret;
+ uint8_t mac[6];
+
+ ret = esp_wifi_adapter_init();
+ if (ret < 0)
+ {
+ nerr("ERROR: Initialize Wi-Fi adapter error: %d\n", ret);
+ return ret;
+ }
+
+ ret = esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP);
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to read MAC address\n");
+ return ret;
+ }
+
+ ninfo("Wi-Fi softAP MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
+ mac[0], mac[1], mac[2],
+ mac[3], mac[4], mac[5]);
+
+ ret = esp_net_initialize(ESP_WLAN_SOFTAP_DEVNO, mac,
+ &g_softap_ops);
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to initialize net\n");
+ return ret;
+ }
+
+ ret = esp_wifi_softap_register_recv_cb(wlan_softap_rx_done);
+ if (ret < 0)
+ {
+ nerr("ERROR: Failed to register RX callback\n");
+ return ret;
+ }
+
+ esp_wifi_softap_register_txdone_cb(wlan_softap_tx_done);
+
+ ninfo("INFO: Initialize Wi-Fi softAP net success\n");
+
+ return OK;
+}
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+/****************************************************************************
+ * Name: esp_wifi_tx_done_cb
+ *
+ * Description:
+ * Wi-Fi TX done callback function.
+ *
+ * Input Parameters:
+ * ifidx - The interface id that the tx callback has been triggered from
+ * data - Pointer to the data transmitted
+ * data_len - Length of the data transmitted
+ * txstatus - True:if the data was transmitted sucessfully False: if data
+ * transmission failed
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void IRAM_ATTR esp_wifi_tx_done_cb(uint8_t ifidx,
+ uint8_t *data,
+ uint16_t *len,
+ bool txstatus)
+{
+#ifdef ESP_WLAN_HAS_STA
+ if (ifidx == ESP_IF_WIFI_STA)
+ {
+ if (g_sta_txdone_cb)
+ {
+ g_sta_txdone_cb(ifidx, data, len, txstatus);
+ }
+ }
+ else
+#endif /* ESP_WLAN_HAS_STA */
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+ if (ifidx == ESP_IF_WIFI_AP)
+ {
+ if (g_softap_txdone_cb)
+ {
+ g_softap_txdone_cb(ifidx, data, len, txstatus);
+ }
+ }
+ else
+#endif /* ESP_WLAN_HAS_SOFTAP */
+ {
+ wlerr("ifidx=%d is error\n", ifidx);
+ }
+}
+
+#endif /* CONFIG_ESPRESSIF_WIFI */
diff --git a/arch/risc-v/src/common/espressif/esp_wlan.h b/arch/risc-v/src/common/espressif/esp_wlan.h
new file mode 100644
index 0000000000..f23c215268
--- /dev/null
+++ b/arch/risc-v/src/common/espressif/esp_wlan.h
@@ -0,0 +1,177 @@
+/****************************************************************************
+ * arch/risc-v/src/common/espressif/esp_wlan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WLAN_H
+#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WLAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#ifndef __ASSEMBLY__
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#if defined(CONFIG_ESPRESSIF_WIFI_STATION)
+# define ESP_WLAN_HAS_STA
+# define ESP_WLAN_STA_DEVNO 0
+# define ESP_WLAN_DEVS 1
+#elif defined(CONFIG_ESPRESSIF_WIFI_SOFTAP)
+# define ESP_WLAN_HAS_SOFTAP
+# define ESP_WLAN_SOFTAP_DEVNO 0
+# define ESP_WLAN_DEVS 1
+#elif defined(CONFIG_ESPRESSIF_WIFI_STATION_SOFTAP)
+# define ESP_WLAN_HAS_STA
+# define ESP_WLAN_HAS_SOFTAP
+# define ESP_WLAN_STA_DEVNO 0
+# define ESP_WLAN_SOFTAP_DEVNO 1
+# define ESP_WLAN_DEVS 2
+#endif
+
+#define MAC_LEN (6)
+
+#ifdef CONFIG_ESPRESSIF_WIFI
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nuttx_err_to_freertos
+ *
+ * Description:
+ * Transform from Nuttx OS error code to FreeRTOS's pdTRUE or pdFALSE.
+ *
+ * Input Parameters:
+ * ret - NuttX error code
+ *
+ * Returned Value:
+ * Wi-Fi adapter error code
+ *
+ ****************************************************************************/
+
+static inline int32_t nuttx_err_to_freertos(int ret)
+{
+ return ret >= 0;
+}
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef ESP_WLAN_HAS_STA
+
+/****************************************************************************
+ * Name: esp_wlan_sta_set_linkstatus
+ *
+ * Description:
+ * Set Wi-Fi station link status
+ *
+ * Parameters:
+ * linkstatus - true Notifies the networking layer about an available
+ * carrier, false Notifies the networking layer about an
+ * disappeared carrier.
+ *
+ * Returned Value:
+ * OK on success; Negated errno on failure.
+ *
+ ****************************************************************************/
+
+int esp_wlan_sta_set_linkstatus(bool linkstatus);
+
+/****************************************************************************
+ * Name: esp_wlan_sta_initialize
+ *
+ * Description:
+ * Initialize the ESP32-S3 WLAN station netcard driver
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * OK on success; Negated errno on failure.
+ *
+ ****************************************************************************/
+
+int esp_wlan_sta_initialize(void);
+#endif /* ESP_WLAN_HAS_STA */
+
+/****************************************************************************
+ * Name: esp_wlan_softap_initialize
+ *
+ * Description:
+ * Initialize the ESP32-S3 WLAN softAP netcard driver
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * OK on success; Negated errno on failure.
+ *
+ ****************************************************************************/
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+int esp_wlan_softap_initialize(void);
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+/****************************************************************************
+ * Name: esp_wifi_tx_done_cb
+ *
+ * Description:
+ * Wi-Fi TX done callback function.
+ *
+ * Input Parameters:
+ * ifidx - The interface id that the tx callback has been triggered from
+ * data - Pointer to the data transmitted
+ * data_len - Length of the data transmitted
+ * txstatus - True:if the data was transmitted sucessfully False: if data
+ * transmission failed
+ *
+ * Returned Value:
+ * none
+ *
+ ****************************************************************************/
+
+void esp_wifi_tx_done_cb(uint8_t ifidx,
+ uint8_t *data,
+ uint16_t *len,
+ bool txstatus);
+
+#endif /* CONFIG_ESPRESSIF_WIFI */
+#ifdef __cplusplus
+}
+#endif
+#undef EXTERN
+
+#endif /* __ASSEMBLY__ */
+#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WLAN_H */
diff --git a/arch/risc-v/src/esp32c6/Make.defs b/arch/risc-v/src/esp32c6/Make.defs
index 13ba12c297..6469f02e57 100644
--- a/arch/risc-v/src/esp32c6/Make.defs
+++ b/arch/risc-v/src/esp32c6/Make.defs
@@ -21,4 +21,11 @@
include common/Make.defs
include common/espressif/Make.defs
+# Wireless interfaces.
+
+ifeq ($(CONFIG_ESPRESSIF_WIFI),y)
+CHIP_CSRCS += esp_coex_adapter.c esp_wifi_adapter.c
+EXTRA_LIBS += -lcore -lnet80211 -lpp
+endif
+
CFLAGS += ${DEFINE_PREFIX}_RETARGETABLE_LOCKING
diff --git a/arch/risc-v/src/esp32c6/esp_coex_adapter.c b/arch/risc-v/src/esp32c6/esp_coex_adapter.c
new file mode 100644
index 0000000000..89b6c3067a
--- /dev/null
+++ b/arch/risc-v/src/esp32c6/esp_coex_adapter.c
@@ -0,0 +1,578 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c6/esp_coex_adapter.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <pthread.h>
+
+#include <nuttx/spinlock.h>
+#include <irq/irq.h>
+#include <nuttx/kmalloc.h>
+
+#include "esp_hr_timer.h"
+#include "esp_wlan.h"
+
+#include "esp_attr.h"
+#include "esp_timer.h"
+#include "soc/rtc.h"
+#include "esp_private/esp_clk.h"
+#include "esp_coexist_adapter.h"
+#include "rom/ets_sys.h"
+#include "soc/soc_caps.h"
+#include "esp_modem_wrapper.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define OSI_FUNCS_TIME_BLOCKING 0xffffffff
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int64_t esp_coex_esp_timer_get_time_wrapper(void);
+static int32_t esp_coex_semphr_take_from_isr_wrapper(void *semphr,
+ void *hptw);
+static int32_t esp_coex_semphr_give_from_isr_wrapper(void *semphr,
+ void *hptw);
+static int esp_coex_is_in_isr_wrapper(void);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+coex_adapter_funcs_t g_coex_adapter_funcs =
+{
+ ._version = COEX_ADAPTER_VERSION,
+ ._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
+ ._semphr_create = esp_coex_common_semphr_create_wrapper,
+ ._semphr_delete = esp_coex_common_semphr_delete_wrapper,
+ ._semphr_take_from_isr = esp_coex_semphr_take_from_isr_wrapper,
+ ._semphr_give_from_isr = esp_coex_semphr_give_from_isr_wrapper,
+ ._semphr_take = esp_coex_common_semphr_take_wrapper,
+ ._semphr_give = esp_coex_common_semphr_give_wrapper,
+ ._is_in_isr = esp_coex_is_in_isr_wrapper,
+ ._malloc_internal = esp_coex_common_malloc_internal_wrapper,
+ ._free = free,
+ ._esp_timer_get_time = esp_coex_esp_timer_get_time_wrapper,
+ ._env_is_chip = esp_coex_common_env_is_chip_wrapper,
+ ._timer_disarm = esp_coex_common_timer_disarm_wrapper,
+ ._timer_done = esp_coex_common_timer_done_wrapper,
+ ._timer_setfn = esp_coex_common_timer_setfn_wrapper,
+ ._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
+ ._magic = COEX_ADAPTER_MAGIC,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_coex_esp_timer_get_time_wrapper
+ *
+ * Description:
+ * This function retrieves the current time of the High Resolution Timer
+ * in microseconds. It is a wrapper around the esp_hr_timer_time_us
+ * function, providing a consistent interface for the coexistence module.
+ *
+ * Input Parameters:
+ * None.
+ *
+ * Returned Value:
+ * The current time of the High Resolution Timer in microseconds, as a
+ * 64-bit integer.
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR int64_t esp_coex_esp_timer_get_time_wrapper(void)
+{
+ return (int64_t)esp_hr_timer_time_us();
+}
+
+/****************************************************************************
+ * Name: esp_coex_semphr_take_from_isr_wrapper
+ *
+ * Description:
+ * Take a semaphore from an ISR
+ *
+ * Input Parameters:
+ * semphr - Semaphore data pointer.
+ * hptw - Unused.
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t IRAM_ATTR esp_coex_semphr_take_from_isr_wrapper(void *semphr,
+ void *hptw)
+{
+ *(int *)hptw = 0;
+
+ return nuttx_err_to_freertos(nxsem_trywait(semphr));
+}
+
+/****************************************************************************
+ * Name: esp_coex_semphr_give_from_isr_wrapper
+ *
+ * Description:
+ * Post semaphore
+ *
+ * Input Parameters:
+ * semphr - Semaphore data pointer
+ * hptw - Unused.
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t IRAM_ATTR esp_coex_semphr_give_from_isr_wrapper(void *semphr,
+ void *hptw)
+{
+ *(int *)hptw = 0;
+
+ return esp_coex_common_semphr_give_wrapper(semphr);
+}
+
+/****************************************************************************
+ * Name: esp_coex_is_in_isr_wrapper
+ *
+ * Description:
+ * This function checks if the current context is an interrupt service
+ * routine (ISR). It is a wrapper around the NuttX up_interrupt_context
+ * function.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Returns 1 if the current context is an ISR, 0 otherwise.
+ *
+ ****************************************************************************/
+
+static int IRAM_ATTR esp_coex_is_in_isr_wrapper(void)
+{
+ return (int)up_interrupt_context();
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_coex_common_env_is_chip_wrapper
+ *
+ * Description:
+ * This function checks if the environment is a chip or FPGA.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Returns true if the environment is a chip, false if it's an FPGA.
+ *
+ ****************************************************************************/
+
+bool IRAM_ATTR esp_coex_common_env_is_chip_wrapper(void)
+{
+#ifdef CONFIG_IDF_ENV_FPGA
+ return false;
+#else
+ return true;
+#endif
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_spin_lock_create_wrapper
+ *
+ * Description:
+ * Create spin lock in SMP mode
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Spin lock data pointer
+ *
+ ****************************************************************************/
+
+void *esp_coex_common_spin_lock_create_wrapper(void)
+{
+ spinlock_t *lock;
+ int tmp;
+
+ tmp = sizeof(*lock);
+ lock = kmm_malloc(tmp);
+ if (!lock)
+ {
+ wlerr("Failed to alloc %d memory\n", tmp);
+ DEBUGPANIC();
+ }
+
+ spin_initialize(lock, SP_UNLOCKED);
+
+ return lock;
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_int_disable_wrapper
+ *
+ * Description:
+ * Enter critical section by disabling interrupts and taking the spin lock
+ * if in SMP mode.
+ *
+ * Input Parameters:
+ * wifi_int_mux - Spin lock data pointer
+ *
+ * Returned Value:
+ * CPU PS value.
+ *
+ ****************************************************************************/
+
+uint32_t IRAM_ATTR esp_coex_common_int_disable_wrapper(void *wifi_int_mux)
+{
+ irqstate_t flags;
+
+ flags = spin_lock_irqsave((spinlock_t *)wifi_int_mux);
+
+ return (uint32_t)flags;
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_int_restore_wrapper
+ *
+ * Description:
+ * Exit from critical section by enabling interrupts and releasing the spin
+ * lock if in SMP mode.
+ *
+ * Input Parameters:
+ * wifi_int_mux - Spin lock data pointer
+ * tmp - CPU PS value.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void IRAM_ATTR esp_coex_common_int_restore_wrapper(void *wifi_int_mux,
+ uint32_t tmp)
+{
+ irqstate_t flags = (irqstate_t)tmp;
+
+ spin_unlock_irqrestore((spinlock_t *)wifi_int_mux, flags);
+}
+
+/****************************************************************************
+ * Name: esp_task_yield_from_isr
+ *
+ * Description:
+ * Perform a solicited context switch on FreeRTOS. Do nothing in NuttX.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void IRAM_ATTR esp_coex_common_task_yield_from_isr_wrapper(void)
+{
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_semphr_create_wrapper
+ *
+ * Description:
+ * Create and initialize semaphore
+ *
+ * Input Parameters:
+ * max - No meanining for NuttX
+ * init - semaphore initialization value
+ *
+ * Returned Value:
+ * Semaphore data pointer
+ *
+ ****************************************************************************/
+
+void *esp_coex_common_semphr_create_wrapper(uint32_t max, uint32_t init)
+{
+ int ret;
+ sem_t *sem;
+ int tmp;
+
+ tmp = sizeof(sem_t);
+ sem = kmm_malloc(tmp);
+ if (!sem)
+ {
+ wlerr("Failed to alloc %d memory\n", tmp);
+ return NULL;
+ }
+
+ ret = nxsem_init(sem, 0, init);
+ if (ret)
+ {
+ wlerr("Failed to initialize sem error=%d\n", ret);
+ kmm_free(sem);
+ return NULL;
+ }
+
+ return sem;
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_semphr_delete_wrapper
+ *
+ * Description:
+ * Delete semaphore
+ *
+ * Input Parameters:
+ * semphr - Semaphore data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void esp_coex_common_semphr_delete_wrapper(void *semphr)
+{
+ sem_t *sem = (sem_t *)semphr;
+
+ nxsem_destroy(sem);
+ kmm_free(sem);
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_semphr_take_wrapper
+ *
+ * Description:
+ * This function attempts to take (wait for) a semaphore within a certain
+ * period of time. It is a wrapper around the NuttX nxsem_wait and
+ * nxsem_tickwait functions, providing error handling and translation
+ * between NuttX and ESP-IDF error codes.
+ *
+ * Input Parameters:
+ * semphr - Pointer to the semaphore data structure.
+ * block_time_tick - The maximum number of system ticks to wait.
+ *
+ * Returned Value:
+ * Returns 0 if the semaphore was successfully taken, or a negative error
+ * code if the operation failed or the timeout expired.
+ *
+ ****************************************************************************/
+
+int32_t esp_coex_common_semphr_take_wrapper(void *semphr,
+ uint32_t block_time_tick)
+{
+ int ret;
+ sem_t *sem = (sem_t *)semphr;
+
+ if (block_time_tick == OSI_FUNCS_TIME_BLOCKING)
+ {
+ ret = nxsem_wait(sem);
+ }
+ else
+ {
+ if (block_time_tick > 0)
+ {
+ ret = nxsem_tickwait(sem, block_time_tick);
+ }
+ else
+ {
+ ret = nxsem_trywait(sem);
+ }
+ }
+
+ if (ret)
+ {
+ wlerr("ERROR: Failed to wait sem in %lu ticks. Error=%d\n",
+ block_time_tick, ret);
+ }
+
+ return nuttx_err_to_freertos(ret);
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_semphr_give_wrapper
+ *
+ * Description:
+ * This function posts (releases) a semaphore. It is a wrapper around the
+ * NuttX nxsem_post function, providing error handling and translation
+ * between NuttX and ESP-IDF error codes.
+ *
+ * Input Parameters:
+ * semphr - Pointer to the semaphore data structure.
+ *
+ * Returned Value:
+ * Returns 0 if the semaphore was successfully posted, or a negative error
+ * code if the operation failed.
+ *
+ ****************************************************************************/
+
+int32_t esp_coex_common_semphr_give_wrapper(void *semphr)
+{
+ int ret;
+ sem_t *sem = (sem_t *)semphr;
+
+ ret = nxsem_post(sem);
+ if (ret)
+ {
+ wlerr("Failed to post sem error=%d\n", ret);
+ }
+
+ return nuttx_err_to_freertos(ret);
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_timer_disarm_wrapper
+ *
+ * Description:
+ * Disable timer
+ *
+ * Input Parameters:
+ * timer - timer data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void IRAM_ATTR esp_coex_common_timer_disarm_wrapper(void *timer)
+{
+ ets_timer_disarm(timer);
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_timer_done_wrapper
+ *
+ * Description:
+ * Disable and free timer
+ *
+ * Input Parameters:
+ * timer - timer data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void esp_coex_common_timer_done_wrapper(void *timer)
+{
+ ets_timer_done(timer);
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_timer_setfn_wrapper
+ *
+ * Description:
+ * Set timer callback function and private data
+ *
+ * Input Parameters:
+ * ptimer - Timer data pointer
+ * pfunction - Callback function
+ * parg - Callback function private data
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void esp_coex_common_timer_setfn_wrapper(void *ptimer,
+ void *pfunction,
+ void *parg)
+{
+ ets_timer_setfn(ptimer, pfunction, parg);
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_timer_arm_us_wrapper
+ *
+ * Description:
+ * Set timer timeout period and repeat flag
+ *
+ * Input Parameters:
+ * ptimer - timer data pointer
+ * us - micro seconds
+ * repeat - true: run cycle, false: run once
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void IRAM_ATTR esp_coex_common_timer_arm_us_wrapper(void *ptimer,
+ uint32_t us,
+ bool repeat)
+{
+ ets_timer_arm_us(ptimer, us, repeat);
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_clk_slowclk_cal_get_wrapper
+ *
+ * Description:
+ * Get the calibration value of RTC slow clock
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * The calibration value obtained using rtc_clk_cal
+ *
+ ****************************************************************************/
+
+uint32_t esp_coex_common_clk_slowclk_cal_get_wrapper(void)
+{
+ /* The bit width of WiFi light sleep clock calibration is 12 while the one
+ * of system is 19. It should shift 19 - 12 = 7.
+ */
+
+ return (esp_clk_slowclk_cal_get() >>
+ (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
+}
+
+/****************************************************************************
+ * Name: esp_coex_common_malloc_internal_wrapper
+ *
+ * Description:
+ * Drivers allocate a block of memory
+ *
+ * Input Parameters:
+ * size - memory size
+ *
+ * Returned Value:
+ * Memory pointer
+ *
+ ****************************************************************************/
+
+IRAM_ATTR void *esp_coex_common_malloc_internal_wrapper(size_t size)
+{
+ return kmm_malloc(size);
+}
diff --git a/arch/risc-v/src/esp32c6/esp_wifi_adapter.c b/arch/risc-v/src/esp32c6/esp_wifi_adapter.c
new file mode 100644
index 0000000000..4ba19299c5
--- /dev/null
+++ b/arch/risc-v/src/esp32c6/esp_wifi_adapter.c
@@ -0,0 +1,5569 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c6/esp_wifi_adapter.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <inttypes.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <debug.h>
+#include <pthread.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <math.h>
+#include <clock/clock.h>
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <irq/irq.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mqueue.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/irq.h>
+#include <nuttx/mutex.h>
+#include <nuttx/kthread.h>
+#include <nuttx/wdog.h>
+#include <nuttx/wqueue.h>
+#include <nuttx/sched.h>
+#include <nuttx/signal.h>
+#include <nuttx/arch.h>
+#include <nuttx/wireless/wireless.h>
+#include <nuttx/tls.h>
+
+#include "esp_irq.h"
+#include "esp_hr_timer.h"
+
+#include "esp_types.h"
+#include "esp_random.h"
+#include "esp_mac.h"
+#include "esp_intr_alloc.h"
+#include "esp_attr.h"
+#include "esp_log.h"
+#include "esp_event.h"
+#include "esp_timer.h"
+#include "esp_private/esp_modem_clock.h"
+#include "esp_private/wifi_os_adapter.h"
+#include "esp_private/wifi.h"
+#include "esp_phy_init.h"
+#include "soc/rtc_cntl_periph.h"
+#include "esp_private/periph_ctrl.h"
+#include "esp_private/esp_clk.h"
+#include "os.h"
+#include "esp_smartconfig.h"
+#include "esp_coexist_internal.h"
+#include "rom/ets_sys.h"
+#include "esp_modem_wrapper.h"
+
+#if SOC_PM_MODEM_RETENTION_BY_REGDMA
+#include "esp_private/esp_regdma.h"
+#include "esp_private/sleep_retention.h"
+#endif
+
+#include "esp_wlan.h"
+#include "esp_wifi_adapter.h"
+#include "esp_wifi_utils.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define PHY_RF_MASK ((1 << PHY_BT_MODULE) | (1 << PHY_WIFI_MODULE))
+
+#define WIFI_CONNECT_TIMEOUT CONFIG_ESPRESSIF_WIFI_CONNECT_TIMEOUT
+
+#define ESP_WIFI_11B_MAX_BITRATE 11
+#define ESP_WIFI_11G_MAX_BITRATE 54
+#define ESP_WIFI_11N_MCS7_HT20_BITRATE 72
+#define ESP_WIFI_11N_MCS7_HT40_BITRATE 150
+
+#ifndef CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL
+#define CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL 3
+#endif
+
+#define DEFAULT_LISTEN_INTERVAL CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL
+
+#define RTC_CLK_CAL_FRACT 19 //!< Number of fractional bits in values returned by rtc_clk_cal
+
+#define ets_timer _ETSTIMER_
+
+/* CONFIG_POWER_SAVE_MODEM */
+
+#if defined(CONFIG_ESP_POWER_SAVE_MIN_MODEM)
+# define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM
+#elif defined(CONFIG_ESP_POWER_SAVE_MAX_MODEM)
+# define DEFAULT_PS_MODE WIFI_PS_MAX_MODEM
+#elif defined(CONFIG_ESP_POWER_SAVE_NONE)
+# define DEFAULT_PS_MODE WIFI_PS_NONE
+#else
+# define DEFAULT_PS_MODE WIFI_PS_NONE
+#endif
+
+#define ESP_MAX_PRIORITIES (25)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* Wi-Fi event ID */
+
+enum wifi_adpt_evt_e
+{
+ WIFI_ADPT_EVT_SCAN_DONE = 0,
+ WIFI_ADPT_EVT_STA_START,
+ WIFI_ADPT_EVT_STA_CONNECT,
+ WIFI_ADPT_EVT_STA_DISCONNECT,
+ WIFI_ADPT_EVT_STA_AUTHMODE_CHANGE,
+ WIFI_ADPT_EVT_STA_STOP,
+ WIFI_ADPT_EVT_AP_START,
+ WIFI_ADPT_EVT_AP_STOP,
+ WIFI_ADPT_EVT_AP_STACONNECTED,
+ WIFI_ADPT_EVT_AP_STADISCONNECTED,
+ WIFI_ADPT_EVT_MAX,
+};
+
+/* Wi-Fi Station state */
+
+enum wifi_sta_state
+{
+ WIFI_STA_STATE_NULL,
+ WIFI_STA_STATE_START,
+ WIFI_STA_STATE_CONNECT,
+ WIFI_STA_STATE_DISCONNECT,
+ WIFI_STA_STATE_STOP
+};
+
+/* Wi-Fi interrupt adapter private data */
+
+struct irq_adpt
+{
+ void (*func)(void *arg); /* Interrupt callback function */
+ void *arg; /* Interrupt private data */
+};
+
+/* Wi-Fi message queue private data */
+
+struct mq_adpt
+{
+ struct file mq; /* Message queue handle */
+ uint32_t msgsize; /* Message size */
+ char name[16]; /* Message queue name */
+};
+
+/* Wi-Fi time private data */
+
+struct time_adpt
+{
+ time_t sec; /* Second value */
+ suseconds_t usec; /* Micro second value */
+};
+
+/* Wi-Fi event private data */
+
+struct evt_adpt
+{
+ sq_entry_t entry; /* Sequence entry */
+ int32_t id; /* Event ID */
+ uint8_t buf[0]; /* Event private data */
+};
+
+/* Wi-Fi event notification private data */
+
+struct wifi_notify
+{
+ bool assigned; /* Flag indicate if it is used */
+ pid_t pid; /* Signal's target thread PID */
+ struct sigevent event; /* Signal event private data */
+ struct sigwork_s work; /* Signal work private data */
+};
+
+/* Wi-Fi NVS private data */
+
+struct nvs_adpt
+{
+ char *index_name;
+};
+
+/* Wi-Fi event callback function */
+
+typedef void (*wifi_evt_cb_t)(void *p);
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Private functions order is defined as:
+ * - A first block containing the functions in the same order as of the
+ * ESP-IDF' corresponding `esp_adapter.c` to ease comparison;
+ * - A second block of auxiliary functions block ordered by ascending;
+ */
+
+/* First block of functions */
+
+static void *wifi_zalloc_wrapper(size_t size);
+static void *wifi_create_queue(int queue_len, int item_size);
+static void wifi_delete_queue(wifi_static_queue_t *queue);
+static void *wifi_create_queue_wrapper(int queue_len, int item_size);
+static void wifi_delete_queue_wrapper(void *queue);
+static void set_intr_wrapper(int32_t cpu_no, uint32_t intr_source,
+ uint32_t intr_num, int32_t intr_prio);
+static void clear_intr_wrapper(uint32_t intr_source, uint32_t intr_num);
+static void set_isr_wrapper(int32_t n, void *f, void *arg);
+static void enable_intr_wrapper(uint32_t intr_mask);
+static void disable_intr_wrapper(uint32_t intr_mask);
+static bool is_from_isr_wrapper(void);
+static void wifi_thread_semphr_free(void *data);
+static void *wifi_thread_semphr_get_wrapper(void);
+static void *recursive_mutex_create_wrapper(void);
+static void *mutex_create_wrapper(void);
+static void mutex_delete_wrapper(void *mutex);
+static int32_t mutex_lock_wrapper(void *mutex);
+static int32_t mutex_unlock_wrapper(void *mutex);
+static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size);
+static int32_t queue_send_wrapper(void *queue,
+ void *item,
+ uint32_t block_time_tick);
+static int32_t queue_send_from_isr_wrapper(void *queue,
+ void *item,
+ void *hptw);
+static int32_t queue_send_to_back_wrapper(void *queue,
+ void *item,
+ uint32_t block_time_tick);
+static int32_t queue_send_to_front_wrapper(void *queue,
+ void *item,
+ uint32_t block_time_tick);
+static int32_t queue_recv_wrapper(void *queue,
+ void *item,
+ uint32_t block_time_tick);
+static uint32_t event_group_wait_bits_wrapper(void *event,
+ uint32_t bits_to_wait_for,
+ int clear_on_exit,
+ int wait_for_all_bits,
+ uint32_t block_time_tick);
+static int32_t task_create_pinned_to_core_wrapper(void *task_func,
+ const char *name,
+ uint32_t stack_depth,
+ void *param,
+ uint32_t prio,
+ void *task_handle,
+ uint32_t core_id);
+static int32_t task_create_wrapper(void *task_func,
+ const char *name,
+ uint32_t stack_depth,
+ void *param,
+ uint32_t prio,
+ void *task_handle);
+static int32_t task_ms_to_tick_wrapper(uint32_t ms);
+static int32_t task_get_max_priority_wrapper(void);
+int32_t esp_event_post_wrapper(const char *event_base,
+ int32_t event_id,
+ void *event_data,
+ size_t event_data_size,
+ uint32_t ticks);
+static void wifi_apb80m_request_wrapper(void);
+static void wifi_apb80m_release_wrapper(void);
+static void timer_arm_wrapper(void *timer, uint32_t tmout, bool repeat);
+static void wifi_reset_mac_wrapper(void);
+static void wifi_clock_enable_wrapper(void);
+static void wifi_clock_disable_wrapper(void);
+static int get_time_wrapper(void *t);
+static void *realloc_internal_wrapper(void *ptr, size_t size);
+static void *calloc_internal_wrapper(size_t n, size_t size);
+static void *zalloc_internal_wrapper(size_t size);
+static int nvs_open_wrapper(const char *name, unsigned int open_mode,
+ uint32_t *out_handle);
+static void esp_log_writev_wrapper(unsigned int level,
+ const char *tag,
+ const char *format,
+ va_list args);
+static void esp_log_write_wrapper(unsigned int level,
+ const char *tag,
+ const char *format, ...);
+static int esp_read_mac_wrapper(uint8_t *mac, unsigned int type);
+static int coex_init_wrapper(void);
+static void coex_deinit_wrapper(void);
+static int coex_enable_wrapper(void);
+static void coex_disable_wrapper(void);
+static uint32_t coex_status_get_wrapper(void);
+static int coex_wifi_request_wrapper(uint32_t event,
+ uint32_t latency,
+ uint32_t duration);
+static int coex_wifi_release_wrapper(uint32_t event);
+static int coex_wifi_channel_set_wrapper(uint8_t primary,
+ uint8_t secondary);
+static int coex_event_duration_get_wrapper(uint32_t event,
+ uint32_t *duration);
+static int coex_pti_get_wrapper(uint32_t event, uint8_t *pti);
+static void coex_schm_status_bit_clear_wrapper(uint32_t type,
+ uint32_t status);
+static void coex_schm_status_bit_set_wrapper(uint32_t type,
+ uint32_t status);
+static int coex_schm_interval_set_wrapper(uint32_t interval);
+static uint32_t coex_schm_interval_get_wrapper(void);
+static uint8_t coex_schm_curr_period_get_wrapper(void);
+static void *coex_schm_curr_phase_get_wrapper(void);
+static int coex_register_start_cb_wrapper(int (* cb)(void));
+static int coex_schm_process_restart_wrapper(void);
+static int coex_schm_register_cb_wrapper(int type, int(*cb)(int));
+static void esp_empty_wrapper(void);
+
+/* Second block of functions
+ * These functions are auxiliary functions that are used by the first block
+ * of functions or software adapters for the Wi-Fi driver
+ */
+
+static int esp_event_id_map(int event_id);
+static void esp_evt_work_cb(void *arg);
+static int esp_freq_to_channel(uint16_t freq);
+static uint32_t esp_get_free_heap_size(void);
+static void *event_group_create_wrapper(void);
+static void event_group_delete_wrapper(void *event);
+static uint32_t event_group_set_bits_wrapper(void *event, uint32_t bits);
+static uint32_t event_group_clear_bits_wrapper(void *event, uint32_t bits);
+static int esp_int_adpt_cb(int irq, void *context, void *arg);
+static int esp_nvs_commit(uint32_t handle);
+static int esp_nvs_erase_key(uint32_t handle, const char *key);
+static int esp_nvs_get_blob(uint32_t handle,
+ const char *key,
+ void *out_value,
+ size_t *length);
+static int esp_nvs_set_blob(uint32_t handle,
+ const char *key,
+ const void *value,
+ size_t length);
+static int esp_nvs_get_i8(uint32_t handle,
+ const char *key,
+ int8_t *out_value);
+static int esp_nvs_set_i8(uint32_t handle, const char *key, int8_t value);
+static int esp_nvs_get_u8(uint32_t handle,
+ const char *key,
+ uint8_t *out_value);
+static int esp_nvs_set_u8(uint32_t handle, const char *key, uint8_t value);
+static int esp_nvs_get_u16(uint32_t handle,
+ const char *key,
+ uint16_t *out_value);
+static int esp_nvs_set_u16(uint32_t handle, const char *key, uint16_t value);
+static void esp_nvs_close(uint32_t handle);
+static void esp_update_time(struct timespec *timespec, uint32_t ticks);
+static int esp_wifi_auth_trans(uint32_t wifi_auth);
+static int esp_wifi_cipher_trans(uint32_t wifi_cipher);
+static int esp_wifi_lock(bool lock);
+static uint32_t queue_msg_waiting_wrapper(void *queue);
+static void task_delay_wrapper(uint32_t tick);
+static void task_delete_wrapper(void *task_handle);
+static void *task_get_current_task_wrapper(void);
+static void vqueue_delete_adapter(void *queue);
+static void vsemaphore_delete_adapter(void *semphr);
+static void *xqueue_create_adapter(uint32_t queue_len, uint32_t item_size);
+static int32_t xqueue_send_adapter(void *queue,
+ void *item,
+ uint32_t ticks,
+ int prio);
+void *xsemaphore_create_counting_adapter(uint32_t max, uint32_t init);
+
+#ifdef CONFIG_PM
+extern void wifi_apb80m_request(void);
+extern void wifi_apb80m_release(void);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Wi-Fi event private data */
+
+static struct work_s g_wifi_evt_work;
+static sq_queue_t g_wifi_evt_queue;
+static struct wifi_notify g_wifi_notify[WIFI_ADPT_EVT_MAX];
+static mutex_t g_wifiexcl_lock = NXMUTEX_INITIALIZER;
+
+/* Wi-Fi adapter reference */
+
+static int g_wifi_ref;
+
+#ifdef ESP_WLAN_HAS_STA
+
+/* If reconnect automatically */
+
+static bool g_sta_reconnect;
+
+/* If Wi-Fi sta starts */
+
+static bool g_sta_started;
+
+/* If Wi-Fi sta connected */
+
+static bool g_sta_connected;
+
+/* Wi-Fi interface configuration */
+
+static wifi_config_t g_sta_wifi_cfg;
+
+#endif /* ESP_WLAN_HAS_STA */
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+
+/* If Wi-Fi SoftAP starts */
+
+static bool g_softap_started;
+
+/* Wi-Fi interface configuration */
+
+static wifi_config_t g_softap_wifi_cfg;
+
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+/* Device specific lock */
+
+static spinlock_t g_lock;
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/* Wi-Fi OS adapter data */
+
+wifi_osi_funcs_t g_wifi_osi_funcs =
+{
+ ._version = ESP_WIFI_OS_ADAPTER_VERSION,
+ ._env_is_chip = esp_coex_common_env_is_chip_wrapper,
+ ._set_intr = set_intr_wrapper,
+ ._clear_intr = clear_intr_wrapper,
+ ._set_isr = set_isr_wrapper,
+ ._ints_on = enable_intr_wrapper,
+ ._ints_off = disable_intr_wrapper,
+ ._is_from_isr = is_from_isr_wrapper,
+ ._spin_lock_create = esp_coex_common_spin_lock_create_wrapper,
+ ._spin_lock_delete = free,
+ ._wifi_int_disable = esp_coex_common_int_disable_wrapper,
+ ._wifi_int_restore = esp_coex_common_int_restore_wrapper,
+ ._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
+ ._semphr_create = esp_coex_common_semphr_create_wrapper,
+ ._semphr_delete = esp_coex_common_semphr_delete_wrapper,
+ ._semphr_take = esp_coex_common_semphr_take_wrapper,
+ ._semphr_give = esp_coex_common_semphr_give_wrapper,
+ ._wifi_thread_semphr_get = wifi_thread_semphr_get_wrapper,
+ ._mutex_create = mutex_create_wrapper,
+ ._recursive_mutex_create = recursive_mutex_create_wrapper,
+ ._mutex_delete = mutex_delete_wrapper,
+ ._mutex_lock = mutex_lock_wrapper,
+ ._mutex_unlock = mutex_unlock_wrapper,
+ ._queue_create = queue_create_wrapper,
+ ._queue_delete = vqueue_delete_adapter,
+ ._queue_send = queue_send_wrapper,
+ ._queue_send_from_isr = queue_send_from_isr_wrapper,
+ ._queue_send_to_back = queue_send_to_back_wrapper,
+ ._queue_send_to_front = queue_send_to_front_wrapper,
+ ._queue_recv = queue_recv_wrapper,
+ ._queue_msg_waiting = queue_msg_waiting_wrapper,
+ ._event_group_create = event_group_create_wrapper,
+ ._event_group_delete = event_group_delete_wrapper,
+ ._event_group_set_bits = event_group_set_bits_wrapper,
+ ._event_group_clear_bits = event_group_clear_bits_wrapper,
+ ._event_group_wait_bits = event_group_wait_bits_wrapper,
+ ._task_create_pinned_to_core = task_create_pinned_to_core_wrapper,
+ ._task_create = task_create_wrapper,
+ ._task_delete = task_delete_wrapper,
+ ._task_delay = task_delay_wrapper,
+ ._task_ms_to_tick = task_ms_to_tick_wrapper,
+ ._task_get_current_task = task_get_current_task_wrapper,
+ ._task_get_max_priority = task_get_max_priority_wrapper,
+ ._malloc = malloc,
+ ._free = free,
+ ._event_post = esp_event_post_wrapper,
+ ._get_free_heap_size = esp_get_free_heap_size,
+ ._rand = esp_random,
+ ._dport_access_stall_other_cpu_start_wrap =
+ esp_empty_wrapper,
+ ._dport_access_stall_other_cpu_end_wrap =
+ esp_empty_wrapper,
+ ._wifi_apb80m_request = wifi_apb80m_request_wrapper,
+ ._wifi_apb80m_release = wifi_apb80m_release_wrapper,
+ ._phy_disable = esp_phy_disable,
+ ._phy_enable = esp_phy_enable,
+ ._phy_update_country_info = esp_phy_update_country_info,
+ ._read_mac = esp_read_mac_wrapper,
+ ._timer_arm = timer_arm_wrapper,
+ ._timer_disarm = esp_coex_common_timer_disarm_wrapper,
+ ._timer_done = esp_coex_common_timer_done_wrapper,
+ ._timer_setfn = esp_coex_common_timer_setfn_wrapper,
+ ._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
+ ._wifi_reset_mac = wifi_reset_mac_wrapper,
+ ._wifi_clock_enable = wifi_clock_enable_wrapper,
+ ._wifi_clock_disable = wifi_clock_disable_wrapper,
+ ._wifi_rtc_enable_iso = esp_empty_wrapper,
+ ._wifi_rtc_disable_iso = esp_empty_wrapper,
+ ._esp_timer_get_time = (int64_t(*)(void))esp_hr_timer_time_us,
+ ._nvs_set_i8 = esp_nvs_set_i8,
+ ._nvs_get_i8 = esp_nvs_get_i8,
+ ._nvs_set_u8 = esp_nvs_set_u8,
+ ._nvs_get_u8 = esp_nvs_get_u8,
+ ._nvs_set_u16 = esp_nvs_set_u16,
+ ._nvs_get_u16 = esp_nvs_get_u16,
+ ._nvs_open = nvs_open_wrapper,
+ ._nvs_close = esp_nvs_close,
+ ._nvs_commit = esp_nvs_commit,
+ ._nvs_set_blob = esp_nvs_set_blob,
+ ._nvs_get_blob = esp_nvs_get_blob,
+ ._nvs_erase_key = esp_nvs_erase_key,
+ ._get_random = os_get_random,
+ ._get_time = get_time_wrapper,
+ ._random = os_random,
+ ._slowclk_cal_get = esp_coex_common_clk_slowclk_cal_get_wrapper,
+ ._log_write = esp_log_write_wrapper,
+ ._log_writev = esp_log_writev_wrapper,
+ ._log_timestamp = esp_log_timestamp,
+ ._malloc_internal = esp_coex_common_malloc_internal_wrapper,
+ ._realloc_internal = realloc_internal_wrapper,
+ ._calloc_internal = calloc_internal_wrapper,
+ ._zalloc_internal = zalloc_internal_wrapper,
+ ._wifi_malloc = wifi_malloc,
+ ._wifi_realloc = wifi_realloc,
+ ._wifi_calloc = wifi_calloc,
+ ._wifi_zalloc = wifi_zalloc_wrapper,
+ ._wifi_create_queue = wifi_create_queue_wrapper,
+ ._wifi_delete_queue = wifi_delete_queue_wrapper,
+ ._coex_init = coex_init_wrapper,
+ ._coex_deinit = coex_deinit_wrapper,
+ ._coex_enable = coex_enable_wrapper,
+ ._coex_disable = coex_disable_wrapper,
+ ._coex_status_get = coex_status_get_wrapper,
+ ._coex_wifi_request = coex_wifi_request_wrapper,
+ ._coex_wifi_release = coex_wifi_release_wrapper,
+ ._coex_wifi_channel_set = coex_wifi_channel_set_wrapper,
+ ._coex_event_duration_get = coex_event_duration_get_wrapper,
+ ._coex_pti_get = coex_pti_get_wrapper,
+ ._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
+ ._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
+ ._coex_schm_interval_set = coex_schm_interval_set_wrapper,
+ ._coex_schm_interval_get = coex_schm_interval_get_wrapper,
+ ._coex_schm_curr_period_get = coex_schm_curr_period_get_wrapper,
+ ._coex_schm_curr_phase_get = coex_schm_curr_phase_get_wrapper,
+ ._coex_register_start_cb = coex_register_start_cb_wrapper,
+ ._coex_schm_process_restart = coex_schm_process_restart_wrapper,
+ ._coex_schm_register_cb = coex_schm_register_cb_wrapper,
+ ._magic = ESP_WIFI_OS_ADAPTER_MAGIC,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Private functions order is defined as:
+ * - A first block containing the functions in the same order as of the
+ * ESP-IDF' corresponding `esp_adapter.c` to ease comparison;
+ * - A second block of auxiliary functions block ordered by ascending;
+ */
+
+/* First block of functions */
+
+/****************************************************************************
+ * Name: wifi_zalloc_wrapper
+ *
+ * Description:
+ * Applications allocate a block of memory and clear it with 0
+ *
+ * Input Parameters:
+ * size - memory size
+ *
+ * Returned Value:
+ * New memory pointer
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR void *wifi_zalloc_wrapper(size_t size)
+{
+ return zalloc(size);
+}
+
+/****************************************************************************
+ * Name: wifi_create_queue
+ *
+ * Description:
+ * Create Wi-Fi static message queue
+ *
+ * Input Parameters:
+ * queue_len - queue message number
+ * item_size - message size
+ *
+ * Returned Value:
+ * Wi-Fi static message queue data pointer
+ *
+ ****************************************************************************/
+
+static void *wifi_create_queue(int queue_len, int item_size)
+{
+ wifi_static_queue_t *wifi_queue;
+
+ wifi_queue = kmm_malloc(sizeof(wifi_static_queue_t));
+ if (!wifi_queue)
+ {
+ wlerr("Failed to kmm_malloc\n");
+ return NULL;
+ }
+
+ wifi_queue->handle = xqueue_create_adapter(queue_len, item_size);
+ if (!wifi_queue->handle)
+ {
+ wlerr("Failed to create queue\n");
+ kmm_free(wifi_queue);
+ return NULL;
+ }
+
+ return wifi_queue;
+}
+
+/****************************************************************************
+ * Name: wifi_delete_queue
+ *
+ * Description:
+ * Delete message queue
+ *
+ * Input Parameters:
+ * queue - Message queue data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wifi_delete_queue(wifi_static_queue_t *queue)
+{
+ if (queue)
+ {
+ vqueue_delete_adapter(queue->handle);
+ kmm_free(queue);
+ }
+}
+
+/****************************************************************************
+ * Name: wifi_create_queue_wrapper
+ *
+ * Description:
+ * This function creates a new queue for Wi-Fi operations. It is a wrapper
+ * around the wifi_create_queue function, providing a consistent interface
+ * for the Wi-Fi module.
+ *
+ * Input Parameters:
+ * queue_len - The maximum number of items that the queue can hold.
+ * item_size - The size of each item in the queue.
+ *
+ * Returned Value:
+ * A pointer to the newly created queue, or NULL if the operation failed.
+ *
+ ****************************************************************************/
+
+static void *wifi_create_queue_wrapper(int queue_len, int item_size)
+{
+ return wifi_create_queue(queue_len, item_size);
+}
+
+/****************************************************************************
+ * Name: wifi_delete_queue_wrapper
+ *
+ * Description:
+ * Delete Wi-Fi static message queue
+ *
+ * Input Parameters:
+ * queue - Wi-Fi static message queue data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wifi_delete_queue_wrapper(void *queue)
+{
+ wifi_delete_queue(queue);
+}
+
+/****************************************************************************
+ * Name: set_intr_wrapper
+ *
+ * Description:
+ * Do nothing
+ *
+ * Input Parameters:
+ * cpu_no - The CPU which the interrupt number belongs.
+ * intr_source - The interrupt hardware source number.
+ * intr_num - The interrupt number CPU.
+ * intr_prio - The interrupt priority.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void set_intr_wrapper(int32_t cpu_no, uint32_t intr_source,
+ uint32_t intr_num, int32_t intr_prio)
+{
+ wlinfo("cpu_no=%" PRId32 ", intr_source=%" PRIu32
+ ", intr_num=%" PRIu32 ", intr_prio=%" PRId32 "\n",
+ cpu_no, intr_source, intr_num, intr_prio);
+
+ esp_route_intr(intr_source, intr_num, intr_prio, ESP_IRQ_TRIGGER_LEVEL);
+ esp_set_irq(ESP_SOURCE2IRQ(intr_source), intr_num);
+}
+
+/****************************************************************************
+ * Name: clear_intr_wrapper
+ *
+ * Description:
+ * This function is intended to clear a specific interrupt. However, this
+ * functionality is not supported in the current implementation.
+ *
+ * Input Parameters:
+ * intr_source - The source of the interrupt.
+ * intr_num - The number of the interrupt.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void IRAM_ATTR clear_intr_wrapper(uint32_t intr_source,
+ uint32_t intr_num)
+{
+}
+
+/****************************************************************************
+ * Name: set_isr_wrapper
+ *
+ * Description:
+ * Register interrupt function
+ *
+ * Input Parameters:
+ * n - CPU interrupt number
+ * f - Interrupt function
+ * arg - Function private data
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void set_isr_wrapper(int32_t n, void *f, void *arg)
+{
+ int ret;
+ uint32_t tmp;
+ struct irq_adpt *adapter;
+ int irq = esp_get_irq(n);
+
+ wlinfo("n=%ld f=%p arg=%p irq=%d\n", n, f, arg, irq);
+
+ if (g_irqvector[irq].handler &&
+ g_irqvector[irq].handler != irq_unexpected_isr)
+ {
+ wlinfo("irq=%d has been set handler=%p\n", irq,
+ g_irqvector[irq].handler);
+ return;
+ }
+
+ tmp = sizeof(struct irq_adpt);
+ adapter = kmm_malloc(tmp);
+ if (!adapter)
+ {
+ wlerr("Failed to alloc %ld memory\n", tmp);
+ PANIC();
+ return;
+ }
+
+ adapter->func = f;
+ adapter->arg = arg;
+
+ ret = irq_attach(irq, esp_int_adpt_cb, adapter);
+ if (ret)
+ {
+ wlerr("Failed to attach IRQ %d\n", irq);
+ PANIC();
+ return;
+ }
+}
+
+/****************************************************************************
+ * Name: enable_intr_wrapper
+ *
+ * Description:
+ * Enable a specific Wi-Fi interrupt.
+ *
+ * Input Parameters:
+ * intr_mask - A mask where the bit corresponding to the interrupt to be
+ * enabled is set.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void enable_intr_wrapper(uint32_t intr_mask)
+{
+ int cpuint = __builtin_ffs(intr_mask) - 1;
+ int irq = esp_get_irq(cpuint);
+
+ wlinfo("intr_mask=%08lx cpuint=%d irq=%d\n", intr_mask, cpuint, irq);
+
+ up_enable_irq(irq);
+}
+
+/****************************************************************************
+ * Name: disable_intr_wrapper
+ *
+ * Description:
+ * Disable a specific Wi-Fi interrupt.
+ *
+ * Input Parameters:
+ * intr_mask - A mask where the bit corresponding to the interrupt to be
+ * disabled is set.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void disable_intr_wrapper(uint32_t intr_mask)
+{
+ int cpuint = __builtin_ffs(intr_mask) - 1;
+ int irq = esp_get_irq(cpuint);
+
+ wlinfo("intr_mask=%08lx cpuint=%d irq=%d\n", intr_mask, cpuint, irq);
+
+ up_disable_irq(irq);
+}
+
+/****************************************************************************
+ * Name: is_from_isr_wrapper
+ *
+ * Description:
+ * Check current is in interrupt
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * true if in interrupt or false if not
+ *
+ ****************************************************************************/
+
+static bool IRAM_ATTR is_from_isr_wrapper(void)
+{
+ return up_interrupt_context();
+}
+
+/****************************************************************************
+ * Name: wifi_thread_semphr_free
+ *
+ * Description:
+ * Delete thread self's semaphore
+ *
+ * Input Parameters:
+ * data - Semaphore data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wifi_thread_semphr_free(void *data)
+{
+ void *sem = (void *)data;
+
+ if (sem)
+ {
+ vsemaphore_delete_adapter(sem);
+ }
+}
+
+/****************************************************************************
+ * Name: wifi_thread_semphr_get_wrapper
+ *
+ * Description:
+ * Get thread self's semaphore
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Semaphore data pointer
+ *
+ ****************************************************************************/
+
+static void *wifi_thread_semphr_get_wrapper(void)
+{
+ static int wifi_task_key = -1;
+ int ret;
+ void *sem;
+
+ if (wifi_task_key < 0)
+ {
+ ret = task_tls_alloc(wifi_thread_semphr_free);
+ if (ret < 0)
+ {
+ wlerr("Failed to create task local key\n");
+ return NULL;
+ }
+
+ wifi_task_key = ret;
+ }
+
+ sem = (void *)task_tls_get_value(wifi_task_key);
+ if (sem == NULL)
+ {
+ sem = xsemaphore_create_counting_adapter(1, 0);
+ if (!sem)
+ {
+ wlerr("Failed to create semaphore\n");
+ return NULL;
+ }
+
+ ret = task_tls_set_value(wifi_task_key, (uintptr_t)sem);
+ if (ret != OK)
+ {
+ wlerr("Failed to save semaphore on task local storage: %d\n", ret);
+ vsemaphore_delete_adapter(sem);
+ return NULL;
+ }
+ }
+
+ return sem;
+}
+
+/****************************************************************************
+ * Name: recursive_mutex_create_wrapper
+ *
+ * Description:
+ * Create recursive mutex
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Recursive mutex data pointer
+ *
+ ****************************************************************************/
+
+static void *recursive_mutex_create_wrapper(void)
+{
+ int ret;
+ pthread_mutex_t *mutex;
+ pthread_mutexattr_t attr;
+ int tmp;
+
+ ret = pthread_mutexattr_init(&attr);
+ if (ret)
+ {
+ wlerr("Failed to initialize attr error=%d\n", ret);
+ return NULL;
+ }
+
+ ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+ if (ret)
+ {
+ wlerr("Failed to set attr type error=%d\n", ret);
+ return NULL;
+ }
+
+ tmp = sizeof(pthread_mutex_t);
+ mutex = kmm_malloc(tmp);
+ if (!mutex)
+ {
+ wlerr("Failed to alloc %d memory\n", tmp);
+ return NULL;
+ }
+
+ ret = pthread_mutex_init(mutex, &attr);
+ if (ret)
+ {
+ wlerr("Failed to initialize mutex error=%d\n", ret);
+ kmm_free(mutex);
+ return NULL;
+ }
+
+ return mutex;
+}
+
+/****************************************************************************
+ * Name: mutex_create_wrapper
+ *
+ * Description:
+ * Create mutex
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Mutex data pointer
+ *
+ ****************************************************************************/
+
+static void *mutex_create_wrapper(void)
+{
+ int ret;
+ pthread_mutex_t *mutex;
+ int tmp;
+
+ tmp = sizeof(pthread_mutex_t);
+ mutex = kmm_malloc(tmp);
+ if (!mutex)
+ {
+ wlerr("Failed to alloc %d memory\n", tmp);
+ return NULL;
+ }
+
+ ret = pthread_mutex_init(mutex, NULL);
+ if (ret)
+ {
+ wlerr("Failed to initialize mutex error=%d\n", ret);
+ kmm_free(mutex);
+ return NULL;
+ }
+
+ return mutex;
+}
+
+/****************************************************************************
+ * Name: mutex_delete_wrapper
+ *
+ * Description:
+ * Delete mutex
+ *
+ * Input Parameters:
+ * mutex - mutex data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void mutex_delete_wrapper(void *mutex)
+{
+ pthread_mutex_t *mutex_data = (pthread_mutex_t *)mutex;
+
+ pthread_mutex_destroy(mutex_data);
+ kmm_free(mutex_data);
+}
+
+/****************************************************************************
+ * Name: mutex_lock_wrapper
+ *
+ * Description:
+ * Lock mutex
+ *
+ * Input Parameters:
+ * mutex - mutex data pointer
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
+{
+ int ret;
+ pthread_mutex_t *mutex_data = (pthread_mutex_t *)mutex;
+
+ ret = pthread_mutex_lock(mutex_data);
+ if (ret)
+ {
+ wlerr("Failed to lock mutex error=%d\n", ret);
+ }
+
+ return nuttx_err_to_freertos(ret);
+}
+
+/****************************************************************************
+ * Name: mutex_unlock_wrapper
+ *
+ * Description:
+ * Unlock mutex
+ *
+ * Input Parameters:
+ * mutex - mutex data pointer
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
+{
+ int ret;
+ pthread_mutex_t *mutex_data = (pthread_mutex_t *)mutex;
+
+ ret = pthread_mutex_unlock(mutex_data);
+ if (ret)
+ {
+ wlerr("Failed to unlock mutex error=%d\n", ret);
+ }
+
+ return nuttx_err_to_freertos(ret);
+}
+
+/****************************************************************************
+ * Name: queue_create_wrapper
+ *
+ * Description:
+ * This function creates a new queue adapter with the specified length and
+ * item size. It is a wrapper around the xqueue_create_adapter function.
+ *
+ * Input Parameters:
+ * queue_len - The maximum number of items that the queue can hold.
+ * item_size - The size of each item in the queue.
+ *
+ * Returned Value:
+ * A pointer to the newly created queue adapter, or NULL if the operation
+ * failed.
+ *
+ ****************************************************************************/
+
+static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
+{
+ return xqueue_create_adapter(queue_len, item_size);
+}
+
+/****************************************************************************
+ * Name: queue_send_wrapper
+ *
+ * Description:
+ * Send message of low priority to queue within a certain period of time
+ *
+ * Input Parameters:
+ * queue - Message queue data pointer
+ * item - Message data pointer
+ * ticks - Wait ticks
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t queue_send_wrapper(void *queue, void *item, uint32_t ticks)
+{
+ return xqueue_send_adapter(queue, item, ticks, 0);
+}
+
+/****************************************************************************
+ * Name: queue_send_from_isr_wrapper
+ *
+ * Description:
+ * Send message of low priority to queue in ISR within
+ * a certain period of time
+ *
+ * Input Parameters:
+ * queue - Message queue data pointer
+ * item - Message data pointer
+ * hptw - Unused.
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue,
+ void *item,
+ void *hptw)
+{
+ *(int *)hptw = 0;
+
+ return xqueue_send_adapter(queue, item, 0, 0);
+}
+
+/****************************************************************************
+ * Name: queue_send_to_back_wrapper
+ *
+ * Description:
+ * Send message of low priority to queue within a certain period of time
+ *
+ * Input Parameters:
+ * queue - Message queue data pointer
+ * item - Message data pointer
+ * ticks - Wait ticks
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t queue_send_to_back_wrapper(void *queue,
+ void *item,
+ uint32_t ticks)
+{
+ return xqueue_send_adapter(queue, item, ticks, 0);
+}
+
+/****************************************************************************
+ * Name: queue_send_to_front_wrapper
+ *
+ * Description:
+ * Send message of high priority to queue within a certain period of time
+ *
+ * Input Parameters:
+ * queue - Message queue data pointer
+ * item - Message data pointer
+ * ticks - Wait ticks
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t queue_send_to_front_wrapper(void *queue,
+ void *item,
+ uint32_t ticks)
+{
+ return xqueue_send_adapter(queue, item, ticks, 1);
+}
+
+/****************************************************************************
+ * Name: queue_recv_wrapper
+ *
+ * Description:
+ * Receive message from queue within a certain period of time
+ *
+ * Input Parameters:
+ * queue - Message queue data pointer
+ * item - Message data pointer
+ * ticks - Wait ticks
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t ticks)
+{
+ ssize_t ret;
+ struct timespec timeout;
+ unsigned int prio;
+ struct mq_adpt *mq_adpt = (struct mq_adpt *)queue;
+
+ if (ticks == OSI_FUNCS_TIME_BLOCKING)
+ {
+ ret = file_mq_receive(&mq_adpt->mq, (char *)item,
+ mq_adpt->msgsize, &prio);
+ if (ret < 0)
+ {
+ wlerr("Failed to receive from mqueue error=%d\n", ret);
+ }
+ }
+ else
+ {
+ ret = clock_gettime(CLOCK_REALTIME, &timeout);
+ if (ret < 0)
+ {
+ wlerr("Failed to get time\n");
+ return false;
+ }
+
+ if (ticks)
+ {
+ esp_update_time(&timeout, ticks);
+ }
+
+ ret = file_mq_timedreceive(&mq_adpt->mq, (char *)item,
+ mq_adpt->msgsize, &prio, &timeout);
+ if (ret < 0)
+ {
+ wlerr("Failed to timedreceive from mqueue error=%d\n",
+ ret);
+ }
+ }
+
+ return ret > 0 ? true : false;
+}
+
+/****************************************************************************
+ * Name: event_group_wait_bits_wrapper
+ *
+ * Description:
+ * Don't support
+ *
+ ****************************************************************************/
+
+static uint32_t event_group_wait_bits_wrapper(void *event,
+ uint32_t bits_to_wait_for,
+ int clear_on_exit,
+ int wait_for_all_bits,
+ uint32_t block_time_tick)
+{
+ DEBUGPANIC();
+
+ return false;
+}
+
+/****************************************************************************
+ * Name: task_create_pinned_to_core_wrapper
+ *
+ * Description:
+ * Create task and bind it to target CPU, the task will run when it
+ * is created
+ *
+ * Input Parameters:
+ * entry - Task entry
+ * name - Task name
+ * stack_depth - Task stack size
+ * param - Task private data
+ * prio - Task priority
+ * task_handle - Task handle pointer which is used to pause, resume
+ * and delete the task
+ * core_id - CPU which the task runs in
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t task_create_pinned_to_core_wrapper(void *entry,
+ const char *name,
+ uint32_t stack_depth,
+ void *param,
+ uint32_t prio,
+ void *task_handle,
+ uint32_t core_id)
+{
+ int pid;
+#ifdef CONFIG_SMP
+ int ret;
+ cpu_set_t cpuset;
+#endif
+ uint32_t target_prio = prio;
+
+ if (target_prio < ESP_MAX_PRIORITIES)
+ {
+ target_prio += task_get_max_priority_wrapper() - ESP_MAX_PRIORITIES;
+ }
+
+ pid = kthread_create(name, target_prio, stack_depth, entry,
+ (char * const *)param);
+ if (pid > 0)
+ {
+ if (task_handle != NULL)
+ {
+ *((int *)task_handle) = pid;
+ }
+
+#ifdef CONFIG_SMP
+ if (core_id < CONFIG_SMP_NCPUS)
+ {
+ CPU_ZERO(&cpuset);
+ CPU_SET(core_id, &cpuset);
+ ret = nxsched_set_affinity(pid, sizeof(cpuset), &cpuset);
+ if (ret)
+ {
+ wlerr("Failed to set affinity error=%d\n", ret);
+ return false;
+ }
+ }
+#endif
+ }
+ else
+ {
+ wlerr("Failed to create task\n");
+ }
+
+ return pid > 0;
+}
+
+/****************************************************************************
+ * Name: task_create_wrapper
+ *
+ * Description:
+ * Create task and the task will run when it is created
+ *
+ * Input Parameters:
+ * entry - Task entry
+ * name - Task name
+ * stack_depth - Task stack size
+ * param - Task private data
+ * prio - Task priority
+ * task_handle - Task handle pointer which is used to pause, resume
+ * and delete the task
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t task_create_wrapper(void *entry,
+ const char *name,
+ uint32_t stack_depth,
+ void *param,
+ uint32_t prio,
+ void *task_handle)
+{
+ return task_create_pinned_to_core_wrapper(entry,
+ name,
+ stack_depth,
+ param,
+ prio,
+ task_handle,
+ UINT32_MAX);
+}
+
+/****************************************************************************
+ * Name: task_ms_to_tick_wrapper
+ *
+ * Description:
+ * This function converts a duration from milliseconds to system ticks.
+ * It is a wrapper around the NuttX MSEC2TICK macro.
+ *
+ * Input Parameters:
+ * ms - The duration in milliseconds.
+ *
+ * Returned Value:
+ * The duration in system ticks.
+ *
+ ****************************************************************************/
+
+static int32_t task_ms_to_tick_wrapper(uint32_t ms)
+{
+ return MSEC2TICK(ms);
+}
+
+/****************************************************************************
+ * Name: task_get_max_priority_wrapper
+ *
+ * Description:
+ * Get OS task maximum priority
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Task maximum priority
+ *
+ ****************************************************************************/
+
+static int32_t task_get_max_priority_wrapper(void)
+{
+ return SCHED_PRIORITY_MAX;
+}
+
+/****************************************************************************
+ * Name: esp_event_post_wrapper
+ *
+ * Description:
+ * Active work queue and let the work to process the cached event
+ *
+ * Input Parameters:
+ * event_base - Event set name
+ * event_id - Event ID
+ * event_data - Event private data
+ * event_data_size - Event data size
+ * ticks - Waiting system ticks
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int32_t esp_event_post_wrapper(const char *event_base,
+ int32_t event_id,
+ void *event_data,
+ size_t event_data_size,
+ uint32_t ticks)
+{
+ size_t size;
+ int32_t id;
+ irqstate_t flags;
+ struct evt_adpt *evt_adpt;
+
+ wlinfo("Event: base=%s id=%ld data=%p data_size=%d ticks=%lu\n",
+ event_base, event_id, event_data, event_data_size, ticks);
+
+ id = esp_event_id_map(event_id);
+ if (id < 0)
+ {
+ wlinfo("No process event %ld\n", event_id);
+ return -1;
+ }
+
+ size = event_data_size + sizeof(struct evt_adpt);
+ evt_adpt = kmm_malloc(size);
+ if (!evt_adpt)
+ {
+ wlerr("Failed to alloc %d memory\n", size);
+ return -1;
+ }
+
+ evt_adpt->id = id;
+ memcpy(evt_adpt->buf, event_data, event_data_size);
+
+ flags = spin_lock_irqsave(&g_lock);
+ sq_addlast(&evt_adpt->entry, &g_wifi_evt_queue);
+ spin_unlock_irqrestore(&g_lock, flags);
+
+ work_queue(LPWORK, &g_wifi_evt_work, esp_evt_work_cb, NULL, 0);
+
+ return 0;
+}
+
+/****************************************************************************
+ * Name: wifi_apb80m_request_wrapper
+ *
+ * Description:
+ * This function acquires the Wi-Fi lock in auto-sleep mode.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void IRAM_ATTR wifi_apb80m_request_wrapper(void)
+{
+#ifdef CONFIG_PM
+ wifi_apb80m_request();
+#endif
+}
+
+/****************************************************************************
+ * Name: wifi_apb80m_release_wrapper
+ *
+ * Description:
+ * This function releases the Wi-Fi lock in auto-sleep mode.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void IRAM_ATTR wifi_apb80m_release_wrapper(void)
+{
+#ifdef CONFIG_PM
+ wifi_apb80m_release();
+#endif
+}
+
+/****************************************************************************
+ * Name: timer_arm_wrapper
+ *
+ * Description:
+ * Set timer timeout period and repeat flag
+ *
+ * Input Parameters:
+ * ptimer - timer data pointer
+ * ms - millim seconds
+ * repeat - true: run cycle, false: run once
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void IRAM_ATTR timer_arm_wrapper(void *ptimer,
+ uint32_t tmout,
+ bool repeat)
+{
+ ets_timer_arm(ptimer, tmout, repeat);
+}
+
+/****************************************************************************
+ * Name: wifi_reset_mac_wrapper
+ *
+ * Description:
+ * Reset Wi-Fi hardware MAC
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wifi_reset_mac_wrapper(void)
+{
+ modem_clock_module_mac_reset(PERIPH_WIFI_MODULE);
+}
+
+/****************************************************************************
+ * Name: wifi_clock_enable_wrapper
+ *
+ * Description:
+ * Enable Wi-Fi clock
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wifi_clock_enable_wrapper(void)
+{
+ wifi_module_enable();
+}
+
+/****************************************************************************
+ * Name: wifi_clock_disable_wrapper
+ *
+ * Description:
+ * Disable Wi-Fi clock
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void wifi_clock_disable_wrapper(void)
+{
+ wifi_module_disable();
+}
+
+/****************************************************************************
+ * Name: get_time_wrapper
+ *
+ * Description:
+ * Get std C time
+ *
+ * Input Parameters:
+ * t - buffer to store time of type timeval
+ *
+ * Returned Value:
+ * Zero (OK) on success; -1 is returned on failure with the errno variable
+ * set appropriately.
+ *
+ ****************************************************************************/
+
+static int get_time_wrapper(void *t)
+{
+ return os_get_time(t);
+}
+
+/****************************************************************************
+ * Name: realloc_internal_wrapper
+ *
+ * Description:
+ * Drivers allocate a block of memory by old memory block
+ *
+ * Input Parameters:
+ * ptr - old memory pointer
+ * size - memory size
+ *
+ * Returned Value:
+ * New memory pointer
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR void *realloc_internal_wrapper(void *ptr, size_t size)
+{
+ return kmm_realloc(ptr, size);
+}
+
+/****************************************************************************
+ * Name: calloc_internal_wrapper
+ *
+ * Description:
+ * Drivers allocate some continuous blocks of memory
+ *
+ * Input Parameters:
+ * n - memory block number
+ * size - memory block size
+ *
+ * Returned Value:
+ * New memory pointer
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR void *calloc_internal_wrapper(size_t n, size_t size)
+{
+ return kmm_calloc(n, size);
+}
+
+/****************************************************************************
+ * Name: zalloc_internal_wrapper
+ *
+ * Description:
+ * Drivers allocate a block of memory and clear it with 0
+ *
+ * Input Parameters:
+ * size - memory size
+ *
+ * Returned Value:
+ * New memory pointer
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR void *zalloc_internal_wrapper(size_t size)
+{
+ return kmm_zalloc(size);
+}
+
+/****************************************************************************
+ * Name: nvs_open_wrapper
+ *
+ * Description:
+ * Create a file system storage data object
+ *
+ * Input Parameters:
+ * name - Storage index
+ * open_mode - Storage mode
+ * out_handle - Storage handle
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int nvs_open_wrapper(const char *name,
+ unsigned int open_mode,
+ uint32_t *out_handle)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_log_writev_wrapper
+ *
+ * Description:
+ * Output log with by format string and its arguments
+ *
+ * Input Parameters:
+ * level - log level, no mean here
+ * tag - log TAG, no mean here
+ * format - format string
+ * args - arguments list
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void esp_log_writev_wrapper(unsigned int level,
+ const char *tag,
+ const char *format,
+ va_list args)
+{
+ esp_log_level_t max_level;
+
+#if defined (CONFIG_DEBUG_WIRELESS_INFO)
+ max_level = ESP_LOG_VERBOSE;
+#elif defined (CONFIG_DEBUG_WIRELESS_WARN)
+ max_level = ESP_LOG_WARN;
+#elif defined (CONFIG_DEBUG_WIRELESS_ERROR)
+ max_level = ESP_LOG_ERROR;
+#else
+ max_level = ESP_LOG_NONE;
+#endif
+
+ if (level <= max_level)
+ {
+ esp_log_writev(level, tag, format, args);
+ }
+}
+
+/****************************************************************************
+ * Name: esp_log_write_wrapper
+ *
+ * Description:
+ * Output log with by format string and its arguments
+ *
+ * Input Parameters:
+ * level - log level, no mean here
+ * tag - log TAG, no mean here
+ * format - format string
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void esp_log_write_wrapper(unsigned int level,
+ const char *tag,
+ const char *format, ...)
+{
+ esp_log_level_t max_level;
+
+#if defined (CONFIG_DEBUG_WIRELESS_INFO)
+ max_level = ESP_LOG_VERBOSE;
+#elif defined (CONFIG_DEBUG_WIRELESS_WARN)
+ max_level = ESP_LOG_WARN;
+#elif defined (CONFIG_DEBUG_WIRELESS_ERROR)
+ max_level = ESP_LOG_ERROR;
+#else
+ max_level = ESP_LOG_NONE;
+#endif
+
+ if (level <= max_level)
+ {
+ va_list list;
+ va_start(list, format);
+ esp_log_writev(level, tag, format, list);
+ va_end(list);
+ }
+}
+
+/****************************************************************************
+ * Name: esp_read_mac_wrapper
+ *
+ * Description:
+ * Read MAC address from efuse
+ *
+ * Input Parameters:
+ * mac - MAC address buffer pointer
+ * type - MAC address type
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_read_mac_wrapper(uint8_t *mac, unsigned int type)
+{
+ return esp_read_mac(mac, type);
+}
+
+/****************************************************************************
+ * Name: coex_init_wrapper
+ *
+ * Description:
+ * Init software coexist
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static int coex_init_wrapper(void)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_init();
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_deinit_wrapper
+ *
+ * Description:
+ * De-init software coexist
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void coex_deinit_wrapper(void)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ coex_deinit();
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_enable_wrapper
+ *
+ * Description:
+ * Enable software coexist
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static int coex_enable_wrapper(void)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_enable();
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_disable_wrapper
+ *
+ * Description:
+ * Disable software coexist
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void coex_disable_wrapper(void)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ coex_disable();
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_status_get_wrapper
+ *
+ * Description:
+ * Get software coexist status.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR uint32_t coex_status_get_wrapper(void)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_status_get();
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_wifi_request_wrapper
+ *
+ * Description:
+ * Request Wi-Fi coexistence.
+ *
+ * Input Parameters:
+ * event - WiFi event
+ * latency - WiFi will request coexistence after latency
+ * duration - duration for WiFi to request coexistence
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static int coex_wifi_request_wrapper(uint32_t event,
+ uint32_t latency,
+ uint32_t duration)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_wifi_request(event, latency, duration);
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_wifi_release_wrapper
+ *
+ * Description:
+ * Release Wi-Fi coexistence.
+ *
+ * Input Parameters:
+ * event - WiFi event
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR int coex_wifi_release_wrapper(uint32_t event)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_wifi_release(event);
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_wifi_channel_set_wrapper
+ *
+ * Description:
+ * Set Wi-Fi channel to coexistence module.
+ *
+ * Input Parameters:
+ * primary - WiFi primary channel
+ * secondary - WiFi secondary channel
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static int coex_wifi_channel_set_wrapper(uint8_t primary, uint8_t secondary)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_wifi_channel_set(primary, secondary);
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_event_duration_get_wrapper
+ *
+ * Description:
+ * Get coexistence event duration.
+ *
+ * Input Parameters:
+ * event - Coexistence event
+ * duration - Coexistence event duration
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR int coex_event_duration_get_wrapper(uint32_t event,
+ uint32_t *duration)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_event_duration_get(event, duration);
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_pti_get_wrapper
+ *
+ * Description:
+ * Get coexistence event priority.
+ *
+ * Input Parameters:
+ * event - Coexistence event
+ * pti - Coexistence event priority
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static int coex_pti_get_wrapper(uint32_t event, uint8_t *pti)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_pti_get(event, pti);
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_schm_status_bit_clear_wrapper
+ *
+ * Description:
+ * Clear coexistence status.
+ *
+ * Input Parameters:
+ * type - Coexistence status type
+ * status - Coexistence status
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void coex_schm_status_bit_clear_wrapper(uint32_t type,
+ uint32_t status)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ coex_schm_status_bit_clear(type, status);
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_schm_status_bit_set_wrapper
+ *
+ * Description:
+ * Set coexistence status.
+ *
+ * Input Parameters:
+ * type - Coexistence status type
+ * status - Coexistence status
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ coex_schm_status_bit_set(type, status);
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_schm_interval_set_wrapper
+ *
+ * Description:
+ * Set coexistence scheme interval.
+ *
+ * Input Parameters:
+ * interval - Coexistence scheme interval
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR int coex_schm_interval_set_wrapper(uint32_t interval)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_schm_interval_set(interval);
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_schm_interval_get_wrapper
+ *
+ * Description:
+ * Get coexistence scheme interval.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Coexistence scheme interval
+ *
+ ****************************************************************************/
+
+static uint32_t coex_schm_interval_get_wrapper(void)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_schm_interval_get();
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_schm_curr_period_get_wrapper
+ *
+ * Description:
+ * Get current coexistence scheme period.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Coexistence scheme period
+ *
+ ****************************************************************************/
+
+static uint8_t coex_schm_curr_period_get_wrapper(void)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_schm_curr_period_get();
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_schm_curr_phase_get_wrapper
+ *
+ * Description:
+ * Get current coexistence scheme phase.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Coexistence scheme phase
+ *
+ ****************************************************************************/
+
+static void *coex_schm_curr_phase_get_wrapper(void)
+{
+#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
+ return coex_schm_curr_phase_get();
+#else
+ return NULL;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_register_start_cb_wrapper
+ *
+ * Description:
+ * Register Wi-Fi callback for coexistence starts.
+ *
+ * Input Parameters:
+ * cb - WiFi callback
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static int coex_register_start_cb_wrapper(int (* cb)(void))
+{
+#if CONFIG_SW_COEXIST_ENABLE
+ return coex_register_start_cb(cb);
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_schm_process_restart_wrapper
+ *
+ * Description:
+ * Restart current coexistence scheme.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static int coex_schm_process_restart_wrapper(void)
+{
+#if CONFIG_SW_COEXIST_ENABLE
+ return coex_schm_process_restart();
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: coex_schm_register_cb_wrapper
+ *
+ * Description:
+ * Register callback for coexistence scheme.
+ *
+ * Input Parameters:
+ * type - callback type
+ * cb - callback
+ *
+ * Returned Value:
+ * Zero (OK) is returned on success. A negated errno value is returned
+ * on failure.
+ *
+ ****************************************************************************/
+
+static int coex_schm_register_cb_wrapper(int type, int(*cb)(int))
+{
+#if CONFIG_SW_COEXIST_ENABLE
+ return coex_schm_register_callback(type, cb);
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: esp_empty_wrapper
+ *
+ * Description:
+ * This function is an empty wrapper, designed to be used where a function
+ * pointer is required but no operation is needed.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void IRAM_ATTR esp_empty_wrapper(void)
+{
+}
+
+/* Second block of functions
+ * These functions are auxiliary functions that are used by the first block
+ * of functions or software adapters for the Wi-Fi driver
+ */
+
+/****************************************************************************
+ * Name: esp_event_id_map
+ *
+ * Description:
+ * Transform from esp-idf event ID to Wi-Fi adapter event ID
+ *
+ * Input Parameters:
+ * event_id - esp-idf event ID
+ *
+ * Returned Value:
+ * Wi-Fi adapter event ID
+ *
+ ****************************************************************************/
+
+static int esp_event_id_map(int event_id)
+{
+ int id;
+
+ switch (event_id)
+ {
+ case WIFI_EVENT_SCAN_DONE:
+ id = WIFI_ADPT_EVT_SCAN_DONE;
+ break;
+
+#ifdef ESP_WLAN_HAS_STA
+ case WIFI_EVENT_STA_START:
+ id = WIFI_ADPT_EVT_STA_START;
+ break;
+
+ case WIFI_EVENT_STA_CONNECTED:
+ id = WIFI_ADPT_EVT_STA_CONNECT;
+ break;
+
+ case WIFI_EVENT_STA_DISCONNECTED:
+ id = WIFI_ADPT_EVT_STA_DISCONNECT;
+ break;
+
+ case WIFI_EVENT_STA_AUTHMODE_CHANGE:
+ id = WIFI_ADPT_EVT_STA_AUTHMODE_CHANGE;
+ break;
+
+ case WIFI_EVENT_STA_STOP:
+ id = WIFI_ADPT_EVT_STA_STOP;
+ break;
+#endif /* ESP_WLAN_HAS_STA */
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+ case WIFI_EVENT_AP_START:
+ id = WIFI_ADPT_EVT_AP_START;
+ break;
+
+ case WIFI_EVENT_AP_STOP:
+ id = WIFI_ADPT_EVT_AP_STOP;
+ break;
+
+ case WIFI_EVENT_AP_STACONNECTED:
+ id = WIFI_ADPT_EVT_AP_STACONNECTED;
+ break;
+
+ case WIFI_EVENT_AP_STADISCONNECTED:
+ id = WIFI_ADPT_EVT_AP_STADISCONNECTED;
+ break;
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+ default:
+ return -1;
+ }
+
+ return id;
+}
+
+/****************************************************************************
+ * Name: esp_evt_work_cb
+ *
+ * Description:
+ * Process the cached event
+ *
+ * Input Parameters:
+ * arg - No mean
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void esp_evt_work_cb(void *arg)
+{
+ int ret;
+ irqstate_t flags;
+ struct evt_adpt *evt_adpt;
+ struct wifi_notify *notify;
+ wifi_ps_type_t ps_type = DEFAULT_PS_MODE;
+
+ while (1)
+ {
+ flags = spin_lock_irqsave(&g_lock);
+ evt_adpt = (struct evt_adpt *)sq_remfirst(&g_wifi_evt_queue);
+ spin_unlock_irqrestore(&g_lock, flags);
+ if (!evt_adpt)
+ {
+ break;
+ }
+
+ esp_wifi_lock(true);
+
+ switch (evt_adpt->id)
+ {
+ case WIFI_ADPT_EVT_SCAN_DONE:
+ esp_wifi_scan_event_parse();
+ break;
+
+#ifdef ESP_WLAN_HAS_STA
+ case WIFI_ADPT_EVT_STA_START:
+ wlinfo("Wi-Fi sta start\n");
+
+ g_sta_connected = false;
+
+ ret = esp_wifi_set_ps(ps_type);
+ if (ret)
+ {
+ wlerr("Failed to set power save type\n");
+ break;
+ }
+ else
+ {
+ wlinfo("INFO: Set ps type=%d\n", ps_type);
+ }
+
+ ret = esp_wifi_get_config(WIFI_IF_STA, &g_sta_wifi_cfg);
+ if (ret)
+ {
+ wlerr("Failed to get Wi-Fi config data ret=%d\n", ret);
+ }
+ break;
+
+ case WIFI_ADPT_EVT_STA_CONNECT:
+ wlinfo("Wi-Fi sta connect\n");
+ g_sta_connected = true;
+ ret = esp_wlan_sta_set_linkstatus(true);
+ if (ret < 0)
+ {
+ wlerr("ERROR: Failed to set Wi-Fi station link status\n");
+ }
+
+ break;
+
+ case WIFI_ADPT_EVT_STA_DISCONNECT:
+ wlinfo("Wi-Fi sta disconnect\n");
+ g_sta_connected = false;
+ ret = esp_wlan_sta_set_linkstatus(false);
+ if (ret < 0)
+ {
+ wlerr("ERROR: Failed to set Wi-Fi station link status\n");
+ }
+
+ if (g_sta_reconnect)
+ {
+ ret = esp_wifi_connect();
+ if (ret)
+ {
+ wlerr("Failed to connect AP error=%d\n", ret);
+ }
+ }
+ break;
+
+ case WIFI_ADPT_EVT_STA_STOP:
+ wlinfo("Wi-Fi sta stop\n");
+ g_sta_connected = false;
+ break;
+#endif /* ESP_WLAN_HAS_STA */
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+ case WIFI_ADPT_EVT_AP_START:
+ wlinfo("INFO: Wi-Fi softap start\n");
+
+ ret = esp_wifi_set_ps(ps_type);
+ if (ret)
+ {
+ wlerr("Failed to set power save type\n");
+ break;
+ }
+ else
+ {
+ wlinfo("INFO: Set ps type=%d\n", ps_type);
+ }
+
+ ret = esp_wifi_get_config(WIFI_IF_AP, &g_softap_wifi_cfg);
+ if (ret)
+ {
+ wlerr("Failed to get Wi-Fi config data ret=%d\n", ret);
+ }
+ break;
+
+ case WIFI_ADPT_EVT_AP_STOP:
+ wlinfo("INFO: Wi-Fi softap stop\n");
+ break;
+
+ case WIFI_ADPT_EVT_AP_STACONNECTED:
+ wlinfo("INFO: Wi-Fi station join\n");
+ break;
+
+ case WIFI_ADPT_EVT_AP_STADISCONNECTED:
+ wlinfo("INFO: Wi-Fi station leave\n");
+ break;
+#endif /* ESP_WLAN_HAS_SOFTAP */
+ default:
+ break;
+ }
+
+ notify = &g_wifi_notify[evt_adpt->id];
+ if (notify->assigned)
+ {
+ notify->event.sigev_value.sival_ptr = evt_adpt->buf;
+
+ ret = nxsig_notification(notify->pid, ¬ify->event,
+ SI_QUEUE, ¬ify->work);
+ if (ret < 0)
+ {
+ wlwarn("nxsig_notification event ID=%ld failed: %d\n",
+ evt_adpt->id, ret);
+ }
+ }
+
+ esp_wifi_lock(false);
+
+ kmm_free(evt_adpt);
+ }
+}
+
+/****************************************************************************
+ * Name: esp_freq_to_channel
+ *
+ * Description:
+ * Converts Wi-Fi frequency to channel.
+ *
+ * Input Parameters:
+ * freq - Wi-Fi frequency
+ *
+ * Returned Value:
+ * Wi-Fi channel
+ *
+ ****************************************************************************/
+
+static int esp_freq_to_channel(uint16_t freq)
+{
+ int channel = 0;
+ if (freq >= 2412 && freq <= 2484)
+ {
+ if (freq == 2484)
+ {
+ channel = 14;
+ }
+ else
+ {
+ channel = freq - 2407;
+ if (channel % 5)
+ {
+ return 0;
+ }
+
+ channel /= 5;
+ }
+
+ return channel;
+ }
+
+ if (freq >= 5005 && freq < 5900)
+ {
+ if (freq % 5)
+ {
+ return 0;
+ }
+
+ channel = (freq - 5000) / 5;
+ return channel;
+ }
+
+ if (freq >= 4905 && freq < 5000)
+ {
+ if (freq % 5)
+ {
+ return 0;
+ }
+
+ channel = (freq - 4000) / 5;
+ return channel;
+ }
+
+ return 0;
+}
+
+/****************************************************************************
+ * Name: esp_get_free_heap_size
+ *
+ * Description:
+ * Get free heap size by byte
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Free heap size
+ *
+ ****************************************************************************/
+
+static uint32_t esp_get_free_heap_size(void)
+{
+ struct mallinfo info;
+
+ info = kmm_mallinfo();
+ return info.fordblks;
+}
+
+/****************************************************************************
+ * Name: event_group_create_wrapper
+ *
+ * Description:
+ * Don't support
+ *
+ ****************************************************************************/
+
+static void *event_group_create_wrapper(void)
+{
+ DEBUGPANIC();
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Name: event_group_delete_wrapper
+ *
+ * Description:
+ * Don't support
+ *
+ ****************************************************************************/
+
+static void event_group_delete_wrapper(void *event)
+{
+ DEBUGPANIC();
+}
+
+/****************************************************************************
+ * Name: event_group_set_bits_wrapper
+ *
+ * Description:
+ * Don't support
+ *
+ ****************************************************************************/
+
+static uint32_t event_group_set_bits_wrapper(void *event, uint32_t bits)
+{
+ DEBUGPANIC();
+
+ return false;
+}
+
+/****************************************************************************
+ * Name: event_group_clear_bits_wrapper
+ *
+ * Description:
+ * Don't support
+ *
+ ****************************************************************************/
+
+static uint32_t event_group_clear_bits_wrapper(void *event, uint32_t bits)
+{
+ DEBUGPANIC();
+
+ return false;
+}
+
+/****************************************************************************
+ * Name: esp_int_adpt_cb
+ *
+ * Description:
+ * This is the callback function for the Wi-Fi interrupt adapter. It
+ * retrieves the adapter from the argument, then calls the function
+ * stored in the adapter with its argument.
+ *
+ * Input Parameters:
+ * irq - The IRQ number that caused this interrupt.
+ * context - The register context at the time of the interrupt.
+ * arg - A pointer to the interrupt adapter's private data.
+ *
+ * Returned Value:
+ * Always returns 0.
+ *
+ ****************************************************************************/
+
+static int esp_int_adpt_cb(int irq, void *context, void *arg)
+{
+ struct irq_adpt *adapter = (struct irq_adpt *)arg;
+
+ adapter->func(adapter->arg);
+
+ return 0;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_commit
+ *
+ * Description:
+ * This function has no practical effect
+ *
+ ****************************************************************************/
+
+static int esp_nvs_commit(uint32_t handle)
+{
+ return 0;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_erase_key
+ *
+ * Description:
+ * Read a block of data from file system
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ * key - Data index
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_nvs_erase_key(uint32_t handle, const char *key)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_get_blob
+ *
+ * Description:
+ * Read a block of data from file system
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ * key - Data index
+ * out_value - Read buffer pointer
+ * length - Buffer length
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_nvs_get_blob(uint32_t handle,
+ const char *key,
+ void *out_value,
+ size_t *length)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_set_blob
+ *
+ * Description:
+ * Save a block of data into file system
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ * key - Data index
+ * value - Stored buffer pointer
+ * length - Buffer length
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_nvs_set_blob(uint32_t handle,
+ const char *key,
+ const void *value,
+ size_t length)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_get_i8
+ *
+ * Description:
+ * Read data of type int8_t from file system
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ * key - Data index
+ * out_value - Read buffer pointer
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_nvs_get_i8(uint32_t handle,
+ const char *key,
+ int8_t *out_value)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_set_i8
+ *
+ * Description:
+ * Save data of type int8_t into file system
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ * key - Data index
+ * value - Stored data
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_nvs_set_i8(uint32_t handle,
+ const char *key,
+ int8_t value)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_get_u8
+ *
+ * Description:
+ * Read data of type uint8_t from file system
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ * key - Data index
+ * out_value - Read buffer pointer
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_nvs_get_u8(uint32_t handle,
+ const char *key,
+ uint8_t *out_value)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_set_u8
+ *
+ * Description:
+ * Save data of type uint8_t into file system
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ * key - Data index
+ * value - Stored data
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_nvs_set_u8(uint32_t handle,
+ const char *key,
+ uint8_t value)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_get_u16
+ *
+ * Description:
+ * Read data of type uint16_t from file system
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ * key - Data index
+ * out_value - Read buffer pointer
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_nvs_get_u16(uint32_t handle,
+ const char *key,
+ uint16_t *out_value)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_set_u16
+ *
+ * Description:
+ * Save data of type uint16_t into file system
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ * key - Data index
+ * value - Stored data
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static int esp_nvs_set_u16(uint32_t handle,
+ const char *key,
+ uint16_t value)
+{
+ DEBUGPANIC();
+
+ return -1;
+}
+
+/****************************************************************************
+ * Name: esp_nvs_close
+ *
+ * Description:
+ * Close storage data object and free resource
+ *
+ * Input Parameters:
+ * handle - NVS handle
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+static void esp_nvs_close(uint32_t handle)
+{
+ DEBUGPANIC();
+}
+
+/****************************************************************************
+ * Name: esp_update_time
+ *
+ * Description:
+ * Transform ticks to time and add this time to timespec value
+ *
+ * Input Parameters:
+ * timespec - Input timespec data pointer
+ * ticks - System ticks
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void esp_update_time(struct timespec *timespec, uint32_t ticks)
+{
+ uint32_t tmp;
+
+ tmp = TICK2SEC(ticks);
+ timespec->tv_sec += tmp;
+
+ ticks -= SEC2TICK(tmp);
+ tmp = TICK2NSEC(ticks);
+
+ timespec->tv_nsec += tmp;
+}
+
+#ifdef ESP_WLAN_HAS_STA
+
+/****************************************************************************
+ * Name: esp_wifi_auth_trans
+ *
+ * Description:
+ * Converts a ESP32-C6 authenticate mode values to WEXT authenticate mode.
+ *
+ * Input Parameters:
+ * wifi_auth - ESP32-C6 authenticate mode
+ *
+ * Returned Value:
+ * authenticate mode
+ *
+ ****************************************************************************/
+
+static int esp_wifi_auth_trans(uint32_t wifi_auth)
+{
+ int auth_mode = IW_AUTH_WPA_VERSION_DISABLED;
+
+ switch (wifi_auth)
+ {
+ case WIFI_AUTH_OPEN:
+ auth_mode = IW_AUTH_WPA_VERSION_DISABLED;
+ break;
+
+ case WIFI_AUTH_WPA_PSK:
+ auth_mode = IW_AUTH_WPA_VERSION_WPA;
+ break;
+
+ case WIFI_AUTH_WPA2_PSK:
+ case WIFI_AUTH_WPA_WPA2_PSK:
+ auth_mode = IW_AUTH_WPA_VERSION_WPA2;
+ break;
+
+ case WIFI_AUTH_WPA3_PSK:
+ case WIFI_AUTH_WPA2_WPA3_PSK:
+ auth_mode = IW_AUTH_WPA_VERSION_WPA3;
+ break;
+
+ default:
+ wlerr("Failed to transfer wireless authmode: %ld", wifi_auth);
+ break;
+ }
+
+ return auth_mode;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_cipher_trans
+ *
+ * Description:
+ * Converts a ESP32-C6 cipher type values to WEXT cipher type values.
+ *
+ * Input Parameters:
+ * wifi_cipher - ESP32-C6 cipher type
+ *
+ * Returned Value:
+ * cipher type
+ *
+ ****************************************************************************/
+
+static int esp_wifi_cipher_trans(uint32_t wifi_cipher)
+{
+ int cipher_mode = IW_AUTH_CIPHER_NONE;
+
+ switch (wifi_cipher)
+ {
+ case WIFI_CIPHER_TYPE_NONE:
+ cipher_mode = IW_AUTH_CIPHER_NONE;
+ break;
+
+ case WIFI_CIPHER_TYPE_WEP40:
+ cipher_mode = IW_AUTH_CIPHER_WEP40;
+ break;
+
+ case WIFI_CIPHER_TYPE_WEP104:
+ cipher_mode = IW_AUTH_CIPHER_WEP104;
+ break;
+
+ case WIFI_CIPHER_TYPE_TKIP:
+ cipher_mode = IW_AUTH_CIPHER_TKIP;
+ break;
+
+ case WIFI_CIPHER_TYPE_CCMP:
+ case WIFI_CIPHER_TYPE_TKIP_CCMP:
+ cipher_mode = IW_AUTH_CIPHER_CCMP;
+ break;
+
+ case WIFI_CIPHER_TYPE_AES_CMAC128:
+ cipher_mode = IW_AUTH_CIPHER_AES_CMAC;
+ break;
+
+ default:
+ wlerr("Failed to transfer wireless authmode: %ld",
+ wifi_cipher);
+ break;
+ }
+
+ return cipher_mode;
+}
+
+#endif /* ESP_WLAN_HAS_STA */
+
+/****************************************************************************
+ * Name: esp_wifi_lock
+ *
+ * Description:
+ * Lock or unlock the event process
+ *
+ * Input Parameters:
+ * lock - true: Lock event process, false: unlock event process
+ *
+ * Returned Value:
+ * The result of lock or unlock the event process
+ *
+ ****************************************************************************/
+
+static int esp_wifi_lock(bool lock)
+{
+ int ret;
+
+ if (lock)
+ {
+ ret = nxmutex_lock(&g_wifiexcl_lock);
+ if (ret < 0)
+ {
+ wlinfo("Failed to lock Wi-Fi ret=%d\n", ret);
+ }
+ }
+ else
+ {
+ ret = nxmutex_unlock(&g_wifiexcl_lock);
+ if (ret < 0)
+ {
+ wlinfo("Failed to unlock Wi-Fi ret=%d\n", ret);
+ }
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: queue_msg_waiting_wrapper
+ *
+ * Description:
+ * Get message number in the message queue
+ *
+ * Input Parameters:
+ * queue - Message queue data pointer
+ *
+ * Returned Value:
+ * Message number
+ *
+ ****************************************************************************/
+
+static uint32_t queue_msg_waiting_wrapper(void *queue)
+{
+ int ret;
+ struct mq_attr attr;
+ struct mq_adpt *mq_adpt = (struct mq_adpt *)queue;
+
+ ret = file_mq_getattr(&mq_adpt->mq, &attr);
+ if (ret < 0)
+ {
+ wlerr("Failed to get attr from mqueue error=%d\n", ret);
+ return 0;
+ }
+
+ return attr.mq_curmsgs;
+}
+
+/****************************************************************************
+ * Name: task_delay_wrapper
+ *
+ * Description:
+ * Current task wait for some ticks
+ *
+ * Input Parameters:
+ * tick - Waiting ticks
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void task_delay_wrapper(uint32_t tick)
+{
+ useconds_t us = TICK2USEC(tick);
+
+ nxsig_usleep(us);
+}
+
+/****************************************************************************
+ * Name: task_delete_wrapper
+ *
+ * Description:
+ * Delete the target task
+ *
+ * Input Parameters:
+ * task_handle - Task handle pointer which is used to pause, resume
+ * and delete the task
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void task_delete_wrapper(void *task_handle)
+{
+ pid_t pid = (pid_t)((uintptr_t)task_handle);
+
+ kthread_delete(pid);
+}
+
+/****************************************************************************
+ * Name: task_get_current_task_wrapper
+ *
+ * Description:
+ * This function gets the current task's PID and returns it as a void
+ * pointer. This is a wrapper around the NuttX function nxsched_getpid.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * The current task's PID as a void pointer.
+ *
+ ****************************************************************************/
+
+static void *task_get_current_task_wrapper(void)
+{
+ pid_t pid = nxsched_getpid();
+
+ return (void *)((uintptr_t)pid);
+}
+
+/****************************************************************************
+ * Name: vqueue_delete_adapter
+ *
+ * Description:
+ * This function deletes a queue adapter. It closes the message queue,
+ * unlinks it, and then frees the memory allocated for the queue adapter.
+ *
+ * Input Parameters:
+ * queue - A pointer to the queue adapter to be deleted.
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void vqueue_delete_adapter(void *queue)
+{
+ struct mq_adpt *mq_adpt = (struct mq_adpt *)queue;
+
+ file_mq_close(&mq_adpt->mq);
+ file_mq_unlink(mq_adpt->name);
+ kmm_free(mq_adpt);
+}
+
+/****************************************************************************
+ * Name: vsemaphore_delete_adapter
+ *
+ * Description:
+ * Delete semaphore
+ *
+ * Input Parameters:
+ * semphr - Semaphore data pointer
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+static void vsemaphore_delete_adapter(void *semphr)
+{
+ sem_t *sem = (sem_t *)semphr;
+
+ nxsem_destroy(sem);
+ kmm_free(sem);
+}
+
+/****************************************************************************
+ * Name: xqueue_create_adapter
+ *
+ * Description:
+ * Create message queue
+ *
+ * Input Parameters:
+ * queue_len - queue message number
+ * item_size - message size
+ *
+ * Returned Value:
+ * Message queue data pointer
+ *
+ ****************************************************************************/
+
+static void *xqueue_create_adapter(uint32_t queue_len, uint32_t item_size)
+{
+ struct mq_attr attr;
+ struct mq_adpt *mq_adpt;
+ int ret;
+
+ mq_adpt = kmm_malloc(sizeof(struct mq_adpt));
+ if (!mq_adpt)
+ {
+ wlerr("Failed to kmm_malloc\n");
+ return NULL;
+ }
+
+ snprintf(mq_adpt->name, sizeof(mq_adpt->name),
+ "/tmp/%p", mq_adpt);
+
+ attr.mq_maxmsg = queue_len;
+ attr.mq_msgsize = item_size;
+ attr.mq_curmsgs = 0;
+ attr.mq_flags = 0;
+
+ ret = file_mq_open(&mq_adpt->mq, mq_adpt->name,
+ O_RDWR | O_CREAT, 0644, &attr);
+ if (ret < 0)
+ {
+ wlerr("Failed to create mqueue\n");
+ kmm_free(mq_adpt);
+ return NULL;
+ }
+
+ mq_adpt->msgsize = item_size;
+
+ return (void *)mq_adpt;
+}
+
+/****************************************************************************
+ * Name: xqueue_send_adapter
+ *
+ * Description:
+ * Generic send message to queue within a certain period of time
+ *
+ * Input Parameters:
+ * queue - Message queue data pointer
+ * item - Message data pointer
+ * ticks - Wait ticks
+ * prio - Message priority
+ *
+ * Returned Value:
+ * True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t xqueue_send_adapter(void *queue,
+ void *item,
+ uint32_t ticks,
+ int prio)
+{
+ int ret;
+ struct timespec timeout;
+ struct mq_adpt *mq_adpt = (struct mq_adpt *)queue;
+
+ if (ticks == OSI_FUNCS_TIME_BLOCKING || ticks == 0)
+ {
+ /* Wi-Fi interrupt function will call this adapter function to send
+ * message to message queue, so here we should call kernel API
+ * instead of application API
+ */
+
+ ret = file_mq_send(&mq_adpt->mq, (const char *)item,
+ mq_adpt->msgsize, prio);
+ if (ret < 0)
+ {
+ wlerr("Failed to send message to mqueue error=%d\n",
+ ret);
+ }
+ }
+ else
+ {
+ ret = clock_gettime(CLOCK_REALTIME, &timeout);
+ if (ret < 0)
+ {
+ wlerr("Failed to get time\n");
+ return false;
+ }
+
+ if (ticks)
+ {
+ esp_update_time(&timeout, ticks);
+ }
+
+ ret = file_mq_timedsend(&mq_adpt->mq, (const char *)item,
+ mq_adpt->msgsize, prio, &timeout);
+ if (ret < 0)
+ {
+ wlerr("Failed to timedsend message to mqueue error=%d\n",
+ ret);
+ }
+ }
+
+ return nuttx_err_to_freertos(ret);
+}
+
+/****************************************************************************
+ * Name: xsemaphore_create_counting_adapter
+ *
+ * Description:
+ * Create and initialize semaphore
+ *
+ * Input Parameters:
+ * max - No meanining for NuttX
+ * init - semaphore initialization value
+ *
+ * Returned Value:
+ * Semaphore data pointer
+ *
+ ****************************************************************************/
+
+void *xsemaphore_create_counting_adapter(uint32_t max, uint32_t init)
+{
+ int ret;
+ sem_t *sem;
+ int tmp;
+
+ tmp = sizeof(sem_t);
+ sem = kmm_malloc(tmp);
+ if (!sem)
+ {
+ wlerr("Failed to alloc %d memory\n", tmp);
+ return NULL;
+ }
+
+ ret = nxsem_init(sem, 0, init);
+ if (ret)
+ {
+ wlerr("Failed to initialize sem error=%d\n", ret);
+ kmm_free(sem);
+ return NULL;
+ }
+
+ return sem;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: wifi_malloc
+ *
+ * Description:
+ * Applications allocate a block of memory
+ *
+ * Input Parameters:
+ * size - memory size
+ *
+ * Returned Value:
+ * Memory pointer
+ *
+ ****************************************************************************/
+
+IRAM_ATTR void *wifi_malloc(size_t size)
+{
+ return malloc(size);
+}
+
+/****************************************************************************
+ * Name: wifi_realloc
+ *
+ * Description:
+ * Applications allocate a block of memory by old memory block
+ *
+ * Input Parameters:
+ * ptr - old memory pointer
+ * size - memory size
+ *
+ * Returned Value:
+ * New memory pointer
+ *
+ ****************************************************************************/
+
+IRAM_ATTR void *wifi_realloc(void *ptr, size_t size)
+{
+ return realloc(ptr, size);
+}
+
+/****************************************************************************
+ * Name: wifi_calloc
+ *
+ * Description:
+ * Applications allocate some continuous blocks of memory
+ *
+ * Input Parameters:
+ * n - memory block number
+ * size - memory block size
+ *
+ * Returned Value:
+ * New memory pointer
+ *
+ ****************************************************************************/
+
+IRAM_ATTR void *wifi_calloc(size_t n, size_t size)
+{
+ return calloc(n, size);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_notify_subscribe
+ *
+ * Description:
+ * Enable event notification
+ *
+ * Input Parameters:
+ * pid - Task PID
+ * event - Signal event data pointer
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_notify_subscribe(pid_t pid, struct sigevent *event)
+{
+ int id;
+ struct wifi_notify *notify;
+ int ret = -1;
+
+ wlinfo("PID=%d event=%p\n", pid, event);
+
+ esp_wifi_lock(true);
+
+ if (event->sigev_notify == SIGEV_SIGNAL)
+ {
+ id = esp_event_id_map(event->sigev_signo);
+ if (id < 0)
+ {
+ wlerr("No process event %d\n", event->sigev_signo);
+ }
+ else
+ {
+ notify = &g_wifi_notify[id];
+
+ if (notify->assigned)
+ {
+ wlerr("sigev_signo %d has subscribed\n",
+ event->sigev_signo);
+ }
+ else
+ {
+ if (pid == 0)
+ {
+ pid = nxsched_getpid();
+ wlinfo("Actual PID=%d\n", pid);
+ }
+
+ notify->pid = pid;
+ notify->event = *event;
+ notify->assigned = true;
+
+ ret = 0;
+ }
+ }
+ }
+ else if (event->sigev_notify == SIGEV_NONE)
+ {
+ id = esp_event_id_map(event->sigev_signo);
+ if (id < 0)
+ {
+ wlerr("No process event %d\n", event->sigev_signo);
+ }
+ else
+ {
+ notify = &g_wifi_notify[id];
+
+ if (!notify->assigned)
+ {
+ wlerr("sigev_signo %d has not subscribed\n",
+ event->sigev_signo);
+ }
+ else
+ {
+ notify->assigned = false;
+
+ ret = 0;
+ }
+ }
+ }
+ else
+ {
+ wlerr("sigev_notify %d is invalid\n", event->sigev_signo);
+ }
+
+ esp_wifi_lock(false);
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_adapter_init
+ *
+ * Description:
+ * Initialize ESP32-C6 Wi-Fi adapter
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * 0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_adapter_init(void)
+{
+ int ret;
+ wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
+
+ esp_wifi_lock(true);
+
+ if (g_wifi_ref)
+ {
+ wlinfo("Wi-Fi adapter is already initialized\n");
+ g_wifi_ref++;
+ esp_wifi_lock(false);
+ return OK;
+ }
+
+ sq_init(&g_wifi_evt_queue);
+
+ wifi_cfg.nvs_enable = 0;
+
+#ifdef CONFIG_ESPRESSIF_WIFI_AMPDU_TX_ENABLED
+ wifi_cfg.ampdu_tx_enable = 1;
+#else
+ wifi_cfg.ampdu_tx_enable = 0;
+#endif
+
+#ifdef CONFIG_ESPRESSIF_WIFI_AMPDU_RX_ENABLED
+ wifi_cfg.ampdu_rx_enable = 1;
+#else
+ wifi_cfg.ampdu_rx_enable = 0;
+#endif
+
+#ifdef CONFIG_ESPRESSIF_WIFI_STA_DISCONNECT_PM
+ wifi_cfg.sta_disconnected_pm = true;
+#else
+ wifi_cfg.sta_disconnected_pm = false;
+#endif
+
+ wifi_cfg.rx_ba_win = CONFIG_ESPRESSIF_WIFI_TX_BA_WIN;
+ wifi_cfg.static_rx_buf_num = CONFIG_ESPRESSIF_WIFI_STATIC_RX_BUFFER_NUM;
+ wifi_cfg.dynamic_rx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM;
+ wifi_cfg.dynamic_tx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER_NUM;
+
+ ret = esp_wifi_init(&wifi_cfg);
+ if (ret)
+ {
+ wlerr("Failed to initialize Wi-Fi error=%d\n", ret);
+ ret = esp_wifi_to_errno(ret);
+ goto errout_init_wifi;
+ }
+
+ ret = esp_wifi_set_tx_done_cb(esp_wifi_tx_done_cb);
+ if (ret)
+ {
+ wlerr("Failed to register TX done callback ret=%d\n", ret);
+ ret = esp_wifi_to_errno(ret);
+ goto errout_init_txdone;
+ }
+
+ g_wifi_ref++;
+
+ wlinfo("OK to initialize Wi-Fi adapter\n");
+
+ esp_wifi_lock(false);
+
+ return OK;
+
+errout_init_txdone:
+ esp_wifi_deinit();
+errout_init_wifi:
+ esp_wifi_lock(false);
+
+ return ret;
+}
+
+/****************************************************************************
+ * Station functions
+ ****************************************************************************/
+
+#ifdef ESP_WLAN_HAS_STA
+
+/****************************************************************************
+ * Name: esp_wifi_sta_start
+ *
+ * Description:
+ * Start Wi-Fi station.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_start(void)
+{
+ int ret;
+ wifi_mode_t mode;
+
+ esp_wifi_lock(true);
+
+ ret = esp_wifi_stop();
+ if (ret)
+ {
+ wlinfo("Failed to stop Wi-Fi ret=%d\n", ret);
+ }
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+ if (g_softap_started)
+ {
+ mode = WIFI_MODE_APSTA;
+ }
+ else
+#endif /* ESP_WLAN_HAS_SOFTAP */
+ {
+ mode = WIFI_MODE_STA;
+ }
+
+ ret = esp_wifi_set_mode(mode);
+ if (ret)
+ {
+ wlerr("Failed to set Wi-Fi mode=%d ret=%d\n", mode, ret);
+ ret = esp_wifi_to_errno(ret);
+ goto errout;
+ }
+
+ ret = esp_wifi_start();
+ if (ret)
+ {
+ wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret);
+ ret = esp_wifi_to_errno(ret);
+ goto errout;
+ }
+
+ g_sta_started = true;
+
+ wlinfo("OK to start Wi-Fi station\n");
+
+errout:
+ esp_wifi_lock(false);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_stop
+ *
+ * Description:
+ * Stop Wi-Fi station.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_stop(void)
+{
+ int ret;
+
+ esp_wifi_lock(true);
+
+ ret = esp_wifi_stop();
+ if (ret)
+ {
+ wlinfo("Failed to stop Wi-Fi ret=%d\n", ret);
+ }
+
+ g_sta_started = false;
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+ if (g_softap_started)
+ {
+ ret = esp_wifi_set_mode(WIFI_MODE_AP);
+ if (ret)
+ {
+ wlerr("Failed to set Wi-Fi AP mode ret=%d\n", ret);
+ ret = esp_wifi_to_errno(ret);
+ goto errout;
+ }
+
+ ret = esp_wifi_start();
+ if (ret)
+ {
+ wlerr("Failed to start Wi-Fi AP ret=%d\n", ret);
+ ret = esp_wifi_to_errno(ret);
+ goto errout;
+ }
+ }
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+ wlinfo("OK to stop Wi-Fi station\n");
+
+#ifdef ESP_WLAN_HAS_SOFTAP
+errout:
+#endif /* ESP_WLAN_HAS_SOFTAP */
+
+ esp_wifi_lock(false);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_send_data
+ *
+ * Description:
+ * Use Wi-Fi station interface to send 802.3 frame
+ *
+ * Input Parameters:
+ * pbuf - Packet buffer pointer
+ * len - Packet length
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_send_data(void *pbuf, size_t len)
+{
+ int ret;
+
+ ret = esp_wifi_internal_tx(WIFI_IF_STA, pbuf, len);
+
+ return esp_wifi_to_errno(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_set_password
+ *
+ * Description:
+ * Set/Get Wi-Fi station password
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_password(struct iwreq *iwr, bool set)
+{
+ int ret;
+ int size;
+ wifi_config_t wifi_cfg;
+ struct iw_encode_ext *ext = iwr->u.encoding.pointer;
+ uint8_t *pdata;
+ uint8_t len;
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+ char buf[PWD_MAX_LEN + 1];
+#endif
+
+ DEBUGASSERT(ext != NULL);
+
+ pdata = ext->key;
+
+ wifi_cfg = g_sta_wifi_cfg;
+
+ if (set)
+ {
+ len = ext->key_len;
+ if (len > PWD_MAX_LEN)
+ {
+ return -EINVAL;
+ }
+
+ memset(wifi_cfg.sta.password, 0x0, PWD_MAX_LEN);
+
+ if (ext->alg != IW_ENCODE_ALG_NONE)
+ {
+ memcpy(wifi_cfg.sta.password, pdata, len);
+ }
+
+ wifi_cfg.sta.pmf_cfg.capable = true;
+
+ if (g_sta_connected)
+ {
+ ret = esp_wifi_sta_disconnect();
+ if (ret)
+ {
+ wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+
+ ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
+ if (ret)
+ {
+ wlerr("Failed to set Wi-Fi config data ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ ret = esp_wifi_sta_connect();
+ if (ret)
+ {
+ wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+ }
+
+ g_sta_wifi_cfg = wifi_cfg;
+ }
+ else
+ {
+ len = iwr->u.encoding.length - sizeof(*ext);
+ size = strnlen((char *)wifi_cfg.sta.password, PWD_MAX_LEN);
+ if (len < size)
+ {
+ return -EINVAL;
+ }
+ else
+ {
+ ext->key_len = size;
+ memcpy(pdata, wifi_cfg.sta.password, ext->key_len);
+ }
+
+ if (g_sta_connected)
+ {
+ wifi_ap_record_t ap_info;
+
+ ret = esp_wifi_sta_get_ap_info(&ap_info);
+ if (ret)
+ {
+ wlerr("Failed to get AP record ret=%d", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ switch (ap_info.pairwise_cipher)
+ {
+ case WIFI_CIPHER_TYPE_NONE:
+ ext->alg = IW_ENCODE_ALG_NONE;
+ break;
+
+ case WIFI_CIPHER_TYPE_WEP40:
+ case WIFI_CIPHER_TYPE_WEP104:
+ ext->alg = IW_ENCODE_ALG_WEP;
+ break;
+
+ case WIFI_CIPHER_TYPE_TKIP:
+ ext->alg = IW_ENCODE_ALG_TKIP;
+ break;
+
+ case WIFI_CIPHER_TYPE_CCMP:
+ case WIFI_CIPHER_TYPE_TKIP_CCMP:
+ ext->alg = IW_ENCODE_ALG_CCMP;
+ break;
+
+ case WIFI_CIPHER_TYPE_AES_CMAC128:
+ ext->alg = IW_ENCODE_ALG_AES_CMAC;
+ break;
+
+ default:
+ wlerr("Failed to transfer wireless authmode: %d",
+ ap_info.pairwise_cipher);
+ return -EIO;
+ }
+ }
+ }
+
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+ memcpy(buf, pdata, len);
+ buf[len] = 0;
+ wlinfo("Wi-Fi station password=%s len=%d\n", buf, len);
+#endif
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_essid
+ *
+ * Description:
+ * Set/Get Wi-Fi station ESSID
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_essid(struct iwreq *iwr, bool set)
+{
+ int ret;
+ int size;
+ wifi_config_t wifi_cfg;
+ struct iw_point *essid = &iwr->u.essid;
+ uint8_t *pdata;
+ uint8_t len;
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+ char buf[SSID_MAX_LEN + 1];
+#endif
+
+ DEBUGASSERT(essid != NULL);
+
+ pdata = essid->pointer;
+ len = essid->length;
+
+ if (set && len > SSID_MAX_LEN)
+ {
+ return -EINVAL;
+ }
+
+ wifi_cfg = g_sta_wifi_cfg;
+
+ if (set)
+ {
+ memset(wifi_cfg.sta.ssid, 0x0, SSID_MAX_LEN);
+ memcpy(wifi_cfg.sta.ssid, pdata, len);
+ memset(wifi_cfg.sta.sae_h2e_identifier, 0x0, SAE_H2E_IDENTIFIER_LEN);
+ wifi_cfg.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH;
+
+ if (g_sta_connected)
+ {
+ ret = esp_wifi_sta_disconnect();
+ if (ret)
+ {
+ wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+
+ ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
+ if (ret)
+ {
+ wlerr("Failed to set Wi-Fi config data ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ ret = esp_wifi_sta_connect();
+ if (ret)
+ {
+ wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+ }
+
+ g_sta_wifi_cfg = wifi_cfg;
+ }
+ else
+ {
+ size = strnlen((char *)wifi_cfg.sta.ssid, SSID_MAX_LEN);
+ if (len < size)
+ {
+ return -EINVAL;
+ }
+ else
+ {
+ len = size;
+ memcpy(pdata, wifi_cfg.sta.ssid, len);
+ }
+
+ if (g_sta_connected)
+ {
+ essid->flags = IW_ESSID_ON;
+ }
+ else
+ {
+ essid->flags = IW_ESSID_OFF;
+ }
+ }
+
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+ memcpy(buf, pdata, len);
+ buf[len] = 0;
+ wlinfo("Wi-Fi station ssid=%s len=%d\n", buf, len);
+#endif
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_bssid
+ *
+ * Description:
+ * Set/Get Wi-Fi station BSSID
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_bssid(struct iwreq *iwr, bool set)
+{
+ int ret;
+ wifi_config_t wifi_cfg;
+ struct sockaddr *sockaddr;
+ char *pdata;
+
+ sockaddr = &iwr->u.ap_addr;
+ pdata = sockaddr->sa_data;
+
+ wifi_cfg = g_sta_wifi_cfg;
+
+ if (set)
+ {
+ wifi_cfg.sta.bssid_set = true;
+ memcpy(wifi_cfg.sta.bssid, pdata, MAC_LEN);
+
+ if (g_sta_connected)
+ {
+ ret = esp_wifi_sta_disconnect();
+ if (ret)
+ {
+ wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+
+ ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
+ if (ret)
+ {
+ wlerr("Failed to set Wi-Fi config data ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ ret = esp_wifi_sta_connect();
+ if (ret)
+ {
+ wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+ }
+
+ g_sta_wifi_cfg = wifi_cfg;
+ }
+ else
+ {
+ memcpy(pdata, wifi_cfg.sta.bssid, MAC_LEN);
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_connect
+ *
+ * Description:
+ * Trigger Wi-Fi station connection action
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_connect(void)
+{
+ int ret;
+ uint32_t ticks;
+
+ esp_wifi_lock(true);
+
+ if (g_sta_connected)
+ {
+ wlinfo("Wi-Fi has connected AP\n");
+ esp_wifi_lock(false);
+ return OK;
+ }
+
+ g_sta_reconnect = true;
+
+ ret = esp_wifi_set_config(WIFI_IF_STA, &g_sta_wifi_cfg);
+ if (ret)
+ {
+ wlerr("Failed to set Wi-Fi config data ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ ret = esp_wifi_connect();
+ if (ret)
+ {
+ wlerr("Failed to connect ret=%d\n", ret);
+ ret = esp_wifi_to_errno(ret);
+ goto errout;
+ }
+
+ esp_wifi_lock(false);
+
+ ticks = SEC2TICK(WIFI_CONNECT_TIMEOUT);
+ do
+ {
+ if (g_sta_connected)
+ {
+ break;
+ }
+
+ task_delay_wrapper(1);
+ }
+ while (ticks--);
+
+ if (!g_sta_connected)
+ {
+ g_sta_reconnect = false;
+ wlinfo("Failed to connect to AP\n");
+ return -1;
+ }
+
+ return OK;
+
+errout:
+ g_sta_reconnect = false;
+ esp_wifi_lock(false);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_disconnect
+ *
+ * Description:
+ * Trigger Wi-Fi station disconnection action
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_disconnect(void)
+{
+ int ret;
+
+ esp_wifi_lock(true);
+
+ g_sta_reconnect = false;
+
+ ret = esp_wifi_disconnect();
+ if (ret)
+ {
+ wlerr("Failed to disconnect ret=%d\n", ret);
+ ret = esp_wifi_to_errno(ret);
+ }
+ else
+ {
+ wlinfo("OK to disconnect Wi-Fi station\n");
+ }
+
+ esp_wifi_lock(false);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_mode
+ *
+ * Description:
+ * Set/Get Wi-Fi Station mode code.
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_mode(struct iwreq *iwr, bool set)
+{
+ if (set == false)
+ {
+ iwr->u.mode = IW_MODE_INFRA;
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_auth
+ *
+ * Description:
+ * Set/Get station authentication mode params.
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_auth(struct iwreq *iwr, bool set)
+{
+ int ret;
+ int cmd;
+ wifi_config_t wifi_cfg;
+ wifi_ap_record_t ap_info;
+
+ wifi_cfg = g_sta_wifi_cfg;
+
+ if (set)
+ {
+ cmd = iwr->u.param.flags & IW_AUTH_INDEX;
+ switch (cmd)
+ {
+ case IW_AUTH_WPA_VERSION:
+ {
+ switch (iwr->u.param.value)
+ {
+ case IW_AUTH_WPA_VERSION_DISABLED:
+ wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN;
+ break;
+
+ case IW_AUTH_WPA_VERSION_WPA:
+ wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA_PSK;
+ break;
+
+ case IW_AUTH_WPA_VERSION_WPA2:
+ wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
+ break;
+
+ case IW_AUTH_WPA_VERSION_WPA3:
+ wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA3_PSK;
+ break;
+
+ default:
+ wlerr("Invalid wpa version %" PRId32 "\n",
+ iwr->u.param.value);
+ return -EINVAL;
+ }
+ }
+
+ break;
+ case IW_AUTH_CIPHER_PAIRWISE:
+ case IW_AUTH_CIPHER_GROUP:
+ {
+ switch (iwr->u.param.value)
+ {
+ case IW_AUTH_CIPHER_NONE:
+ wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN;
+ break;
+
+ case IW_AUTH_CIPHER_WEP40:
+ case IW_AUTH_CIPHER_WEP104:
+ wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WEP;
+ break;
+
+ case IW_AUTH_CIPHER_TKIP:
+ case IW_AUTH_CIPHER_CCMP:
+ case IW_AUTH_CIPHER_AES_CMAC:
+ break;
+
+ default:
+ wlerr("Invalid cipher mode %" PRId32 "\n",
+ iwr->u.param.value);
+ return -EINVAL;
+ }
+ }
+
+ break;
+ case IW_AUTH_KEY_MGMT:
+ case IW_AUTH_TKIP_COUNTERMEASURES:
+ case IW_AUTH_DROP_UNENCRYPTED:
+ case IW_AUTH_80211_AUTH_ALG:
+ case IW_AUTH_WPA_ENABLED:
+ case IW_AUTH_RX_UNENCRYPTED_EAPOL:
+ case IW_AUTH_ROAMING_CONTROL:
+ case IW_AUTH_PRIVACY_INVOKED:
+ default:
+ wlerr("Unknown cmd %d\n", cmd);
+ return -EINVAL;
+ }
+
+ size_t password_len = strlen((const char *)wifi_cfg.sta.password);
+ wifi_auth_mode_t authmode = wifi_cfg.sta.threshold.authmode;
+
+ if (g_sta_connected &&
+ ((password_len > 0 && authmode != WIFI_AUTH_OPEN) ||
+ (password_len == 0 && authmode == WIFI_AUTH_OPEN)))
+ {
+ ret = esp_wifi_sta_disconnect();
+ if (ret)
+ {
+ wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+
+ ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
+ if (ret)
+ {
+ wlerr("Failed to set Wi-Fi config data ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ ret = esp_wifi_sta_connect();
+ if (ret)
+ {
+ wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+ }
+
+ g_sta_wifi_cfg = wifi_cfg;
+ }
+ else
+ {
+ if (g_sta_connected == false)
+ {
+ return -ENOTCONN;
+ }
+
+ ret = esp_wifi_sta_get_ap_info(&ap_info);
+ if (ret)
+ {
+ wlerr("Failed to get AP record ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ cmd = iwr->u.param.flags & IW_AUTH_INDEX;
+ switch (cmd)
+ {
+ case IW_AUTH_WPA_VERSION:
+ iwr->u.param.value = esp_wifi_auth_trans(ap_info.authmode);
+ break;
+
+ case IW_AUTH_CIPHER_PAIRWISE:
+ iwr->u.param.value =
+ esp_wifi_cipher_trans(ap_info.pairwise_cipher);
+ break;
+
+ case IW_AUTH_CIPHER_GROUP:
+ iwr->u.param.value = esp_wifi_cipher_trans(ap_info.group_cipher);
+ break;
+
+ case IW_AUTH_KEY_MGMT:
+ case IW_AUTH_TKIP_COUNTERMEASURES:
+ case IW_AUTH_DROP_UNENCRYPTED:
+ case IW_AUTH_80211_AUTH_ALG:
+ case IW_AUTH_WPA_ENABLED:
+ case IW_AUTH_RX_UNENCRYPTED_EAPOL:
+ case IW_AUTH_ROAMING_CONTROL:
+ case IW_AUTH_PRIVACY_INVOKED:
+ default:
+ wlerr("Unknown cmd %d\n", cmd);
+ return -ENOSYS;
+ }
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_freq
+ *
+ * Description:
+ * Set/Get station frequency.
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_freq(struct iwreq *iwr, bool set)
+{
+ int ret;
+
+ if (set && (iwr->u.freq.flags == IW_FREQ_FIXED))
+ {
+ wifi_config_t wifi_cfg = g_sta_wifi_cfg;
+
+ wifi_cfg.sta.channel = esp_freq_to_channel(iwr->u.freq.m);
+
+ if (g_sta_connected)
+ {
+ ret = esp_wifi_sta_disconnect();
+ if (ret)
+ {
+ wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+
+ ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
+ if (ret)
+ {
+ wlerr("Failed to set Wi-Fi config data ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ ret = esp_wifi_sta_connect();
+ if (ret)
+ {
+ wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret);
+ return ret;
+ }
+ }
+
+ g_sta_wifi_cfg = wifi_cfg;
+ }
+ else
+ {
+ if (g_sta_connected)
+ {
+ wifi_ap_record_t ap_info;
+
+ ret = esp_wifi_sta_get_ap_info(&ap_info);
+ if (ret)
+ {
+ wlerr("Failed to get AP record ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ iwr->u.freq.flags = IW_FREQ_FIXED;
+ iwr->u.freq.e = 0;
+ iwr->u.freq.m = 2407 + 5 * ap_info.primary;
+ }
+ else
+ {
+ iwr->u.freq.flags = IW_FREQ_AUTO;
+ iwr->u.freq.e = 0;
+ iwr->u.freq.m = 2412;
+ }
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_bitrate
+ *
+ * Description:
+ * Get station default bit rate (Mbps).
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_bitrate(struct iwreq *iwr, bool set)
+{
+ int ret;
+ wifi_ap_record_t ap_info;
+
+ if (set)
+ {
+ return -ENOSYS;
+ }
+ else
+ {
+ if (g_sta_connected == false)
+ {
+ iwr->u.bitrate.fixed = IW_FREQ_AUTO;
+ return OK;
+ }
+
+ ret = esp_wifi_sta_get_ap_info(&ap_info);
+ if (ret)
+ {
+ wlerr("Failed to get AP record ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ iwr->u.bitrate.fixed = IW_FREQ_FIXED;
+ if (ap_info.phy_11n)
+ {
+ if (ap_info.second)
+ {
+ iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT40_BITRATE;
+ }
+ else
+ {
+ iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT20_BITRATE;
+ }
+ }
+ else if (ap_info.phy_11g)
+ {
+ iwr->u.bitrate.value = ESP_WIFI_11G_MAX_BITRATE;
+ }
+ else if (ap_info.phy_11b)
+ {
+ iwr->u.bitrate.value = ESP_WIFI_11B_MAX_BITRATE;
+ }
+ else
+ {
+ return -EIO;
+ }
+ }
+
+ return OK;
+}
+
+#endif /* ESP_WLAN_HAS_STA */
+
+/****************************************************************************
+ * Name: esp_wifi_sta_get_txpower
+ *
+ * Description:
+ * Get station transmit power (dBm).
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_txpower(struct iwreq *iwr, bool set)
+{
+ int ret;
+ int8_t power;
+ double power_dbm;
+
+ if (set)
+ {
+ if (iwr->u.txpower.flags == IW_TXPOW_RELATIVE)
+ {
+ power = (int8_t)iwr->u.txpower.value;
+ }
+ else
+ {
+ if (iwr->u.txpower.flags == IW_TXPOW_MWATT)
+ {
+ power_dbm = ceil(10 * log10(iwr->u.txpower.value));
+ }
+ else
+ {
+ power_dbm = iwr->u.txpower.value;
+ }
+
+ power = (int8_t)(power_dbm * 4);
+ }
+
+ /* The value set by this API will be mapped to the max_tx_power
+ * of the structure wifi_country_t variable. Param power unit is
+ * 0.25dBm, range is [8, 84] corresponding to 2dBm - 20dBm.
+ * Relationship between set value and actual value.
+ * As follows: {set value range, actual value} =
+ * {{[8, 19],8}, {[20, 27],20}, {[28, 33],28},
+ * {[34, 43],34}, {[44, 51],44}, {[52, 55],52},
+ * {[56, 59],56}, {[60, 65],60}, {[66, 71],66},
+ * {[72, 79],72}, {[80, 84],80}}.
+ */
+
+ if (power < 8 || power > 84)
+ {
+ wlerr("Failed to set transmit power =%d\n", power);
+ return -ENOSYS;
+ }
+
+ esp_wifi_set_max_tx_power(power);
+ return OK;
+ }
+ else
+ {
+ ret = esp_wifi_get_max_tx_power(&power);
+ if (ret)
+ {
+ wlerr("Failed to get transmit power ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ iwr->u.txpower.disabled = 0;
+ iwr->u.txpower.flags = IW_TXPOW_DBM;
+ iwr->u.txpower.value = power / 4;
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_channel
+ *
+ * Description:
+ * Get station range of channel parameters.
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_channel(struct iwreq *iwr, bool set)
+{
+ int ret;
+ int k;
+ wifi_country_t country;
+ struct iw_range *range;
+
+ if (set)
+ {
+ return -ENOSYS;
+ }
+ else
+ {
+ ret = esp_wifi_get_country(&country);
+ if (ret)
+ {
+ wlerr("Failed to get country info ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+
+ range = (struct iw_range *)iwr->u.data.pointer;
+ range->num_frequency = country.nchan;
+ for (k = 1; k <= range->num_frequency; k++)
+ {
+ range->freq[k - 1].i = k;
+ range->freq[k - 1].e = 0;
+ range->freq[k - 1].m = 2407 + 5 * k;
+ }
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_country
+ *
+ * Description:
+ * Configure country info.
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_country(struct iwreq *iwr, bool set)
+{
+ int ret;
+ char *country_code;
+ wifi_country_t country;
+
+ if (set)
+ {
+ memset(&country, 0x00, sizeof(wifi_country_t));
+ country.schan = 1;
+ country.policy = 0;
+
+ country_code = (char *)iwr->u.data.pointer;
+ if (strlen(country_code) != 2)
+ {
+ wlerr("Invalid input arguments\n");
+ return -EINVAL;
+ }
+
+ if (strncmp(country_code, "US", 3) == 0 ||
+ strncmp(country_code, "CA", 3) == 0)
+ {
+ country.nchan = 11;
+ }
+ else if(strncmp(country_code, "JP", 3) == 0)
+ {
+ country.nchan = 14;
+ }
+ else
+ {
+ country.nchan = 13;
+ }
+
+ memcpy(country.cc, country_code, 2);
+ ret = esp_wifi_set_country(&country);
+ if (ret)
+ {
+ wlerr("Failed to Configure country ret=%d\n", ret);
+ return esp_wifi_to_errno(ret);
+ }
+ }
+ else
+ {
+ return -ENOSYS;
+ }
+
+ return OK;
+}
+
+#ifdef ESP_WLAN_HAS_STA
+
+/****************************************************************************
+ * Name: esp_wifi_sta_rssi
+ *
+ * Description:
+ * Get Wi-Fi sensitivity (dBm).
+ *
+ * Input Parameters:
+ * iwr - The argument of the ioctl cmd
+ * set - true: set data; false: get data
+ *
+ * Returned Value:
+ * OK on success (positive non-zero values are cmd-specific)
+ * Negated errno returned on failure.
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_rssi(struct iwreq *iwr, bool set)
+{
+ int ret;
+ wifi_ap_record_t ap_info;
+
+ if (set)
+ {
+ return -ENOSYS;
+ }
+ else
+ {
+ if (g_sta_connected == false)
+ {
+ iwr->u.sens.value = 128;
+ return OK;
+ }
+
+ ret = esp_wifi_sta_get_ap_info(&ap_info);
+ if (ret)
+ {
... 2385 lines suppressed ...