diff --git a/arch/xtensa/src/common/espressif/utils/memory_reserve.h b/arch/xtensa/src/common/espressif/utils/memory_reserve.h new file mode 100644 index 00000000000..a2214059a6e --- /dev/null +++ b/arch/xtensa/src/common/espressif/utils/memory_reserve.h @@ -0,0 +1,85 @@ +/**************************************************************************** + * arch/xtensa/src/common/espressif/utils/memory_reserve.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. + * + ****************************************************************************/ + +#pragma once + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include "sdkconfig.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* Region descriptor holds a description for a particular region of + * memory reserved on this SoC for a particular use (ie not available + * for stack/heap usage.) + */ + +typedef struct +{ + intptr_t start; + intptr_t end; +} soc_reserved_region_t; + +/**************************************************************************** + * Helper Macros/Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: SOC_RESERVE_MEMORY_REGION + * + * Description: + * Macro to reserve a fixed region of RAM (hardcoded addresses) for a + * particular purpose. Usually used to mark out memory addresses needed + * for hardware or ROM code purposes. Not intended for user code which + * can use normal C static allocation instead. + * + * Input Parameters: + * START - Start address to be reserved. + * END - One memory address after the address of the last byte to be + * reserved. + * (ie length of the reserved region is (END - START) in bytes.) + * NAME - Name for the reserved region. Must be a valid variable name, + * unique to this source file. + * + * Returned Value: + * None + * + ****************************************************************************/ + +#define SOC_RESERVE_MEMORY_REGION(START, END, NAME) \ + __attribute__((section(".reserved_memory_address"))) \ + __attribute__((used)) \ + static soc_reserved_region_t reserved_region_##NAME = { START, END }; + +#ifdef __cplusplus +} +#endif diff --git a/arch/xtensa/src/esp32/Kconfig b/arch/xtensa/src/esp32/Kconfig index b3ec5ffb4e4..4b10c21bf05 100644 --- a/arch/xtensa/src/esp32/Kconfig +++ b/arch/xtensa/src/esp32/Kconfig @@ -99,6 +99,10 @@ config ARCH_CHIP_ESP32PICOD4 endchoice # ESP32 Chip Selection +config ESPRESSIF_CHIP_SERIES + string + default "esp32" + config ESP32_SINGLE_CPU bool default n @@ -791,8 +795,10 @@ endmenu # ESP32 Peripheral Selection config ESP32_WIFI_BT_COEXIST bool "Wi-Fi and BT coexist" + default y if ESP32_WIFI && ESP32_BLE default n depends on ESP32_WIFI && ESP32_BLE + select ESP32_WIFI_STA_DISCONNECT_PM menu "Interrupt Configuration" diff --git a/arch/xtensa/src/esp32/Make.defs b/arch/xtensa/src/esp32/Make.defs index d6840af0c13..0bc6d3001e2 100644 --- a/arch/xtensa/src/esp32/Make.defs +++ b/arch/xtensa/src/esp32/Make.defs @@ -117,7 +117,6 @@ endif CHIP_CSRCS += esp32_efuse.c ifeq ($(CONFIG_ESP32_EFUSE),y) -CHIP_CSRCS += esp32_efuse_table.c CHIP_CSRCS += esp32_efuse_lowerhalf.c endif @@ -201,60 +200,6 @@ CHIP_CSRCS += esp32_rtc_lowerhalf.c endif endif -ifeq ($(CONFIG_ESP32_WIRELESS),y) -WIRELESS_DRV_UNPACK = esp-wireless-drivers-3rdparty -WIRELESS_DRV_ID = 45701c0 -WIRELESS_DRV_ZIP = $(WIRELESS_DRV_ID).zip -WIRELESS_DRV_URL = https://github.com/espressif/esp-wireless-drivers-3rdparty/archive - -$(WIRELESS_DRV_ZIP): - $(call DOWNLOAD,$(WIRELESS_DRV_URL),$(WIRELESS_DRV_ZIP),chip/$(WIRELESS_DRV_ZIP)) - -chip/$(WIRELESS_DRV_UNPACK): $(WIRELESS_DRV_ZIP) - $(Q) echo "Unpacking: ESP Wireless Drivers" - $(Q) unzip -oqq chip/$(WIRELESS_DRV_ZIP) -d chip/ - $(Q) mv chip/$(WIRELESS_DRV_UNPACK)-$(WIRELESS_DRV_ID)* chip/$(WIRELESS_DRV_UNPACK) - $(Q) touch chip/$(WIRELESS_DRV_UNPACK) - -ifeq ($(wildcard chip/$(WIRELESS_DRV_UNPACK)/.git),) -context:: chip/$(WIRELESS_DRV_UNPACK) - -distclean:: - $(call DELFILE, chip/$(WIRELESS_DRV_ZIP)) - $(call DELDIR, chip/$(WIRELESS_DRV_UNPACK)) -endif - -INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)esp-wireless-drivers-3rdparty$(DELIM)include -INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)esp-wireless-drivers-3rdparty$(DELIM)include$(DELIM)esp32 - -EXTRA_LIBPATHS += -L $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)esp-wireless-drivers-3rdparty$(DELIM)libs$(DELIM)esp32 -EXTRA_LIBS += -lphy -lrtc - -# Wireless interfaces. - -CHIP_CSRCS += esp32_wireless.c -endif - -ifeq ($(CONFIG_ESP32_WIFI),y) -CHIP_CSRCS += esp32_wlan.c esp32_wifi_utils.c esp32_wifi_adapter.c -EXTRA_LIBS += -lcore -lnet80211 -lpp -lsmartconfig -lespnow -lwpa_supplicant - -ifeq ($(GCCVER),) - export GCCVER := $(shell $(CC) --version | grep gcc | sed -E 's/.* ([0-9]+\.[0-9]+).*/\1/' | cut -d'.' -f1) -endif -ifeq ($(GCCVER),12) - chip/esp32_wifi_adapter.c_CFLAGS += -Wno-maybe-uninitialized -endif -endif - -ifeq ($(CONFIG_ESP32_BLE),y) -CHIP_CSRCS += esp32_ble_adapter.c esp32_ble.c -EXTRA_LIBS += -lbtdm_app - ifeq ($(CONFIG_ESP32_WIFI_BT_COEXIST),y) - EXTRA_LIBS += -lcoexist - endif -endif - ifeq ($(CONFIG_ESP32_OPENETH),y) CHIP_CSRCS += esp32_openeth.c endif @@ -267,11 +212,11 @@ endif ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty ifndef ESP_HAL_3RDPARTY_VERSION - ESP_HAL_3RDPARTY_VERSION = 2fbc8a025275d68833cdfef490377048538de57a + ESP_HAL_3RDPARTY_VERSION = 966f1bbf7d80d3fac9a43f414e864985cbcfd680 endif ifndef ESP_HAL_3RDPARTY_URL - ESP_HAL_3RDPARTY_URL = https://github.com/espressif/esp-hal-3rdparty.git + ESP_HAL_3RDPARTY_URL = https://github.com/espressif/esp-hal-3rdparty.git endif chip/$(ESP_HAL_3RDPARTY_REPO): @@ -284,7 +229,33 @@ chip/$(ESP_HAL_3RDPARTY_REPO): CFLAGS += -Wno-undef -Wno-unused-variable +# Files that require the HAL recipe + +CHIP_SERIES = $(patsubst "%",%,$(CONFIG_ESPRESSIF_CHIP_SERIES)) + include chip/Bootloader.mk +include chip/hal.mk + +include common/espressif/Make.defs + +context:: chip/$(ESP_HAL_3RDPARTY_REPO) +ifeq ($(CONFIG_ESP32_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/bt/controller/lib_esp32 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 + +distclean:: + $(call DELDIR, chip/$(ESP_HAL_3RDPARTY_REPO)) + +ifeq ($(CONFIG_ESP32_WIRELESS),y) +include chip/Wireless.mk +endif + distclean:: $(call DELDIR,chip/$(ESP_HAL_3RDPARTY_REPO)) + +INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)common$(DELIM)espressif diff --git a/arch/xtensa/src/esp32/Wireless.mk b/arch/xtensa/src/esp32/Wireless.mk new file mode 100644 index 00000000000..93ef1b810bb --- /dev/null +++ b/arch/xtensa/src/esp32/Wireless.mk @@ -0,0 +1,253 @@ +############################################################################ +# arch/xtensa/src/esp32/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)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)bt$(DELIM)controller$(DELIM)lib_esp32$(DELIM)$(CHIP_SERIES) +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 += -lrtc -lphy -lcoexist + +# Wireless interfaces. + +CHIP_CSRCS += esp32_wireless.c + +ifeq ($(CONFIG_ESP32_BLE),y) +CHIP_CSRCS += esp32_ble_adapter.c esp32_ble.c +EXTRA_LIBS += -lbtdm_app +endif + +ifeq ($(CONFIG_ESP32_WIFI),y) +CHIP_CSRCS += esp32_wlan.c esp32_wifi_utils.c esp32_wifi_adapter.c +EXTRA_LIBS += -lcore -lnet80211 -lpp + +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="" + +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_ESP_WIFI_ENABLE_SAE_PK),y) +CFLAGS += $(DEFINE_PREFIX)CONFIG_SAE_PK +endif + +ifeq ($(CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA),y) +CFLAGS += $(DEFINE_PREFIX)CONFIG_OWE_STA +endif + +ifeq ($(CONFIG_ESP_WIFI_GCMP_SUPPORT),y) +CFLAGS += $(DEFINE_PREFIX)CONFIG_GCMP +endif + +ifeq ($(CONFIG_ESP_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/xtensa/src/esp32/esp32_ble_adapter.c b/arch/xtensa/src/esp32/esp32_ble_adapter.c index 6c414936e25..f0650665179 100644 --- a/arch/xtensa/src/esp32/esp32_ble_adapter.c +++ b/arch/xtensa/src/esp32/esp32_ble_adapter.c @@ -51,65 +51,79 @@ #include "hardware/esp32_dport.h" #include "hardware/wdev_reg.h" -#include "espidf_wifi.h" #include "xtensa.h" #include "xtensa_attr.h" +#include "utils/memory_reserve.h" #include "esp32_rt_timer.h" -#include "esp32_ble_adapter.h" #include "esp32_wireless.h" #include "esp32_irq.h" #include "esp32_spicache.h" -#ifdef CONFIG_ESP32_WIFI_BT_COEXIST -# include "esp_coexist_internal.h" -#endif +#include "esp_bt.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 "periph_ctrl.h" +#include "rom/ets_sys.h" +#include "soc/soc_caps.h" +#include "xtensa/core-macros.h" +#include "xtensa/xtensa_api.h" +#include "esp_coexist_internal.h" + +#include "esp32_ble_adapter.h" /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -typedef void (*xt_handler)(void *); -typedef void (*coex_func_cb_t)(uint32_t event, int sched_cnt); +/* Bluetooth system and controller config */ -#define XTHAL_SET_INTSET(v) \ -do {\ - int __interrupt = (int)(v);\ - __asm__ __volatile__("wsr.intset %0" :: "a"(__interrupt):"memory");\ -} while(0) +#define BTDM_CFG_BT_DATA_RELEASE (1<<0) +#define BTDM_CFG_HCI_UART (1<<1) +#define BTDM_CFG_CONTROLLER_RUN_APP_CPU (1<<2) +#define BTDM_CFG_SCAN_DUPLICATE_OPTIONS (1<<3) +#define BTDM_CFG_SEND_ADV_RESERVED_SIZE (1<<4) +#define BTDM_CFG_BLE_FULL_SCAN_SUPPORTED (1<<5) -#define MSG_QUEUE_NAME_SIZE 16 +/* Sleep mode */ -#define OSI_FUNCS_TIME_BLOCKING 0xffffffff -#define OSI_VERSION 0x00010002 -#define OSI_MAGIC_VALUE 0xfadebead - -#define BTDM_ASYNC_WAKEUP_REQ_HCI 0 -#define BTDM_ASYNC_WAKEUP_REQ_COEX 1 -#define BTDM_ASYNC_WAKEUP_REQMAX 2 - -#ifdef CONFIG_PM -#define BTDM_MIN_TIMER_UNCERTAINTY_US (1800) +#define BTDM_MODEM_SLEEP_MODE_NONE (0) +#define BTDM_MODEM_SLEEP_MODE_ORIG (1) +#define BTDM_MODEM_SLEEP_MODE_EVED (2) /* sleep mode for BLE controller, used only for internal test. */ /* Low Power Clock Selection */ -#define BTDM_LPCLK_SEL_XTAL (0) -#define BTDM_LPCLK_SEL_XTAL32K (1) -#define BTDM_LPCLK_SEL_RTC_SLOW (2) -#define BTDM_LPCLK_SEL_8M (3) +#define BTDM_LPCLK_SEL_XTAL (0) +#define BTDM_LPCLK_SEL_XTAL32K (1) +#define BTDM_LPCLK_SEL_RTC_SLOW (2) +#define BTDM_LPCLK_SEL_8M (3) /* Sleep and wakeup interval control */ -#define BTDM_MIN_SLEEP_DURATION (24) /* Threshold of interval in half slots to allow to fall into sleep mode */ -#define BTDM_MODEM_WAKE_UP_DELAY (8) /* delay in half slots of modem wake up procedure, including re-enable PHY/RF */ +#define BTDM_MIN_SLEEP_DURATION (12) /* threshold of interval in slots to allow to fall into modem sleep */ +#define BTDM_MODEM_WAKE_UP_DELAY (4) /* delay in slots of modem wake up procedure, including re-enable PHY/RF */ + +#define OSI_FUNCS_TIME_BLOCKING 0xffffffff +#define OSI_VERSION 0x00010004 +#define OSI_MAGIC_VALUE 0xfadebead + +#ifdef CONFIG_PM +# define BTDM_MIN_TIMER_UNCERTAINTY_US (500) #endif -#define BTDM_MODEM_SLEEP_MODE_NONE 0 +#define BTDM_ASYNC_WAKEUP_REQ_HCI 0 +#define BTDM_ASYNC_WAKEUP_REQ_COEX 1 +#define BTDM_ASYNC_WAKEUP_REQ_CTRL_DISA 2 +#define BTDM_ASYNC_WAKEUP_REQMAX 3 -#define ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL 0x20200622 +#define MSG_QUEUE_NAME_SIZE 16 #ifdef CONFIG_ESP32_SPIFLASH -# define BLE_TASK_EVENT_QUEUE_ITEM_SIZE 8 -# define BLE_TASK_EVENT_QUEUE_LEN 8 +# define BLE_TASK_EVENT_QUEUE_ITEM_SIZE 8 +# define BLE_TASK_EVENT_QUEUE_LEN 8 #endif #ifdef CONFIG_ESP32_BLE_INTERRUPT_SAVE_STATUS @@ -118,13 +132,106 @@ do {\ # define NR_IRQSTATE_FLAGS 3 #endif +#define RTC_CLK_CAL_FRACT 19 /* Number of fractional bits in values returned by rtc_clk_cal */ + /**************************************************************************** * Private Types ****************************************************************************/ -/* Number of fractional bits in values returned by rtc_clk_cal */ +/* VHCI function interface */ -#define RTC_CLK_CAL_FRACT 19 +typedef struct vhci_host_callback_s +{ + void (*notify_host_send_available)(void); /* callback used to notify that the host can send packet to controller */ + int (*notify_host_recv)(uint8_t *data, uint16_t len); /* callback used to notify that the controller has a packet to send to the host */ +} vhci_host_callback_t; + +/* Dram region */ + +typedef struct +{ + esp_bt_mode_t mode; + intptr_t start; + intptr_t end; +} btdm_dram_available_region_t; + +struct osi_funcs_s +{ + uint32_t _version; + xt_handler (*_set_isr)(int n, xt_handler f, void *arg); + void (*_ints_on)(unsigned int mask); + void (*_interrupt_disable)(void); + void (*_interrupt_restore)(void); + void (*_task_yield)(void); + void (*_task_yield_from_isr)(void); + void *(*_semphr_create)(uint32_t max, uint32_t init); + void (*_semphr_delete)(void *semphr); + int32_t (*_semphr_take_from_isr)(void *semphr, void *hptw); + int32_t (*_semphr_give_from_isr)(void *semphr, void *hptw); + int32_t (*_semphr_take)(void *semphr, uint32_t block_time_ms); + int32_t (*_semphr_give)(void *semphr); + void *(*_mutex_create)(void); + void (*_mutex_delete)(void *mutex); + int32_t (*_mutex_lock)(void *mutex); + int32_t (*_mutex_unlock)(void *mutex); + void *(* _queue_create)(uint32_t queue_len, uint32_t item_size); + void (* _queue_delete)(void *queue); + int32_t (* _queue_send)(void *queue, void *item, uint32_t block_time_ms); + int32_t (* _queue_send_from_isr)(void *queue, void *item, void *hptw); + int32_t (* _queue_recv)(void *queue, void *item, uint32_t block_time_ms); + int32_t (* _queue_recv_from_isr)(void *queue, void *item, void *hptw); + int32_t (* _task_create)(void *task_func, + const char *name, + uint32_t stack_depth, + void *param, + uint32_t prio, + void *task_handle, + uint32_t core_id); + void (* _task_delete)(void *task_handle); + bool (* _is_in_isr)(void); + int (* _cause_sw_intr_to_core)(int core_id, int intr_no); + void *(* _malloc)(size_t size); + void *(* _malloc_internal)(size_t size); + void (* _free)(void *p); + int32_t (* _read_efuse_mac)(uint8_t mac[6]); + void (* _srand)(unsigned int seed); + int (* _rand)(void); + uint32_t (* _btdm_lpcycles_2_us)(uint32_t cycles); + uint32_t (* _btdm_us_2_lpcycles)(uint32_t us); + bool (* _btdm_sleep_check_duration)(uint32_t *slot_cnt); + void (* _btdm_sleep_enter_phase1)(uint32_t lpcycles); /* called when interrupt is disabled */ + void (* _btdm_sleep_enter_phase2)(void); + void (* _btdm_sleep_exit_phase1)(void); /* called from ISR */ + void (* _btdm_sleep_exit_phase2)(void); /* called from ISR */ + void (* _btdm_sleep_exit_phase3)(void); /* called from task */ + bool (* _coex_bt_wakeup_request)(void); + void (* _coex_bt_wakeup_request_end)(void); + int (* _coex_bt_request)(uint32_t event, + uint32_t latency, + uint32_t duration); + int (* _coex_bt_release)(uint32_t event); + int (* _coex_register_bt_cb)(coex_func_cb_t cb); + uint32_t (* _coex_bb_reset_lock)(void); + void (* _coex_bb_reset_unlock)(uint32_t restore); + int (* _coex_schm_register_btdm_callback)(void *callback); + void (* _coex_schm_status_bit_clear)(uint32_t type, uint32_t status); + void (* _coex_schm_status_bit_set)(uint32_t type, uint32_t status); + uint32_t (* _coex_schm_interval_get)(void); + uint8_t (* _coex_schm_curr_period_get)(void); + void *(* _coex_schm_curr_phase_get)(void); + int (* _coex_wifi_channel_get)(uint8_t *primary, uint8_t *secondary); + int (* _coex_register_wifi_channel_change_callback)(void *cb); + xt_handler (*_set_isr_l3)(int n, xt_handler f, void *arg); + void (*_interrupt_l3_disable)(void); + void (*_interrupt_l3_restore)(void); + void *(* _customer_queue_create)(uint32_t queue_len, uint32_t item_size); + int (* _coex_version_get)(unsigned int *major, + unsigned int *minor, + unsigned int *patch); + uint32_t _magic; +}; + +typedef void (*workitem_handler_t)(void *arg); /* BLE message queue private data */ @@ -143,27 +250,6 @@ struct irq_adpt_s void *arg; /* Interrupt private data */ }; -/* BLE low power control struct */ - -typedef struct btdm_lpcntl_s -{ - bool enable; /* whether low power mode is required */ - bool lpclk_sel; /* low power clock source */ - bool mac_bb_pd; /* whether hardware(MAC, BB) force-power-down is required during sleep */ - bool wakeup_timer_required; /* whether system timer is needed */ - bool no_light_sleep; /* do not allow system to enter light sleep after bluetooth is enabled */ -} btdm_lpcntl_t; - -/* low power control status */ - -typedef struct btdm_lpstat_s -{ - bool pm_lock_released; /* whether power management lock is released */ - bool mac_bb_pd; /* whether hardware(MAC, BB) is powered down */ - bool phy_enabled; /* whether phy is switched on */ - bool wakeup_timer_started; /* whether wakeup timer is started */ -} btdm_lpstat_t; - /* vendor dependent signals to be posted to controller task */ typedef enum @@ -194,139 +280,12 @@ struct bt_sem_s #endif }; -typedef enum -{ - PERIPH_LEDC_MODULE = 0, - PERIPH_UART0_MODULE, - PERIPH_UART1_MODULE, - PERIPH_UART2_MODULE, - PERIPH_I2C0_MODULE, - PERIPH_I2C1_MODULE, - PERIPH_I2S0_MODULE, - PERIPH_I2S1_MODULE, - PERIPH_TIMG0_MODULE, - PERIPH_TIMG1_MODULE, - PERIPH_PWM0_MODULE, - PERIPH_PWM1_MODULE, - PERIPH_PWM2_MODULE, - PERIPH_PWM3_MODULE, - PERIPH_UHCI0_MODULE, - PERIPH_UHCI1_MODULE, - PERIPH_RMT_MODULE, - PERIPH_PCNT_MODULE, - PERIPH_SPI_MODULE, - PERIPH_HSPI_MODULE, - PERIPH_VSPI_MODULE, - PERIPH_SPI_DMA_MODULE, - PERIPH_SDMMC_MODULE, - PERIPH_SDIO_SLAVE_MODULE, - PERIPH_CAN_MODULE, - PERIPH_EMAC_MODULE, - PERIPH_RNG_MODULE, - PERIPH_WIFI_MODULE, - PERIPH_BT_MODULE, - PERIPH_WIFI_BT_COMMON_MODULE, - PERIPH_BT_BASEBAND_MODULE, - PERIPH_BT_LC_MODULE, - PERIPH_AES_MODULE, - PERIPH_SHA_MODULE, - PERIPH_RSA_MODULE, -} periph_module_e; - /* prototype of function to handle vendor dependent signals */ typedef void (*btdm_vnd_ol_task_func_t)(void *param); -/* VHCI function interface */ - -typedef struct vhci_host_callback_s -{ - void (*notify_host_send_available)(void); /* callback used to notify that the host can send packet to controller */ - int (*notify_host_recv)(uint8_t *data, uint16_t len); /* callback used to notify that the controller has a packet to send to the host */ -} vhci_host_callback_t; - -/* DRAM region */ - -typedef struct btdm_dram_available_region_s -{ - esp_bt_mode_t mode; - intptr_t start; - intptr_t end; -} btdm_dram_available_region_t; - typedef void (*osi_intr_handler)(void); -/* BLE OS function */ - -struct osi_funcs_s -{ - uint32_t _version; - xt_handler (*_set_isr)(int n, xt_handler f, void *arg); - void (*_ints_on)(unsigned int mask); - void (*_interrupt_disable)(void); - void (*_interrupt_restore)(void); - void (*_task_yield)(void); - void (*_task_yield_from_isr)(void); - void *(*_semphr_create)(uint32_t max, uint32_t init); - void (*_semphr_delete)(void *semphr); - int32_t (*_semphr_take_from_isr)(void *semphr, void *hptw); - int32_t (*_semphr_give_from_isr)(void *semphr, void *hptw); - int32_t (*_semphr_take)(void *semphr, uint32_t block_time_ms); - int32_t (*_semphr_give)(void *semphr); - void *(*_mutex_create)(void); - void (*_mutex_delete)(void *mutex); - int32_t (*_mutex_lock)(void *mutex); - int32_t (*_mutex_unlock)(void *mutex); - void *(* _queue_create)(uint32_t queue_len, uint32_t item_size); - void (* _queue_delete)(void *queue); - int32_t (* _queue_send)(void *queue, void *item, uint32_t block_time_ms); - int32_t (* _queue_send_from_isr)(void *queue, void *item, void *hptw); - int32_t (* _queue_recv)(void *queue, void *item, uint32_t block_time_ms); - int32_t (* _queue_recv_from_isr)(void *queue, void *item, void *hptw); - int32_t (* _task_create)(void *task_func, - const char *name, - uint32_t stack_depth, - void *param, - uint32_t prio, - void *task_handle, - uint32_t core_id); - void (* _task_delete)(void *task_handle); - bool (* _is_in_isr)(void); - int (* _cause_sw_intr_to_core)(int core_id, int intr_no); - void *(* _malloc)(uint32_t size); - void *(* _malloc_internal)(uint32_t size); - void (* _free)(void *p); - int32_t (* _read_efuse_mac)(uint8_t mac[6]); - void (* _srand)(unsigned int seed); - int (* _rand)(void); - uint32_t (* _btdm_lpcycles_2_us)(uint32_t cycles); - uint32_t (* _btdm_us_2_lpcycles)(uint32_t us); - bool (* _btdm_sleep_check_duration)(uint32_t *slot_cnt); - void (* _btdm_sleep_enter_phase1)(uint32_t lpcycles); /* called when interrupt is disabled */ - void (* _btdm_sleep_enter_phase2)(void); - void (* _btdm_sleep_exit_phase1)(void); /* called from ISR */ - void (* _btdm_sleep_exit_phase2)(void); /* called from ISR */ - void (* _btdm_sleep_exit_phase3)(void); /* called from task */ - bool (* _coex_bt_wakeup_request)(void); - void (* _coex_bt_wakeup_request_end)(void); - int (* _coex_bt_request)(uint32_t event, - uint32_t latency, - uint32_t duration); - int (* _coex_bt_release)(uint32_t event); - int (* _coex_register_bt_cb)(coex_func_cb_t cb); - uint32_t (* _coex_bb_reset_lock)(void); - void (* _coex_bb_reset_unlock)(uint32_t restore); - int (* _coex_schm_register_btdm_callback)(void *callback); - void (* _coex_schm_status_bit_clear)(uint32_t type, uint32_t status); - void (* _coex_schm_status_bit_set)(uint32_t type, uint32_t status); - uint32_t (* _coex_schm_interval_get)(void); - uint8_t (* _coex_schm_curr_period_get)(void); - void *(* _coex_schm_curr_phase_get)(void); - int (* _coex_wifi_channel_get)(uint8_t *primary, uint8_t *secondary); - int (* _coex_register_wifi_channel_change_callback)(void *cb); - uint32_t _magic; -}; - /* List of nested IRQ status flags */ struct irqstate_list_s @@ -336,10 +295,22 @@ struct irqstate_list_s }; /**************************************************************************** - * Private Function + * Private Function Prototypes ****************************************************************************/ -static xt_handler esp_ble_set_isr(int n, xt_handler f, void *arg); +/**************************************************************************** + * Functions to be registered to struct osi_funcs_s + ****************************************************************************/ + +/* Note: Functions name prefixed with `esp_` usually refers to the common + * source code shared between different devices. Avoid creating functions + * with the same prefix. Adding a different prefix, however, would differ + * this source from other devices. So, it's recommended to not use any kind + * of prefix to refer to the SoC. + */ + +static xt_handler ble_set_isr(int n, xt_handler f, void *arg); +static void ints_on(uint32_t mask); static void IRAM_ATTR interrupt_disable(void); static void IRAM_ATTR interrupt_restore(void); static void IRAM_ATTR task_yield_from_isr(void); @@ -347,68 +318,108 @@ static void *semphr_create_wrapper(uint32_t max, uint32_t init); static void semphr_delete_wrapper(void *semphr); static int IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw); static int IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw); -static int semphr_take_wrapper(void *semphr, uint32_t block_time_ms); -static int semphr_give_wrapper(void *semphr); +static int semphr_take_wrapper(void *semphr, uint32_t block_time_ms); +static int semphr_give_wrapper(void *semphr); static void *mutex_create_wrapper(void); static void mutex_delete_wrapper(void *mutex); -static int mutex_lock_wrapper(void *mutex); -static int mutex_unlock_wrapper(void *mutex); -static int IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, +static int mutex_lock_wrapper(void *mutex); +static int mutex_unlock_wrapper(void *mutex); +static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size); +static void queue_delete_wrapper(void *queue); +static int queue_send_wrapper(void *queue, + void *item, + uint32_t block_time_ms); +static int IRAM_ATTR queue_send_from_isr_wrapper(void *queue, + void *item, void *hptw); -static int IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, void *item, +static int queue_recv_wrapper(void *queue, + void *item, + uint32_t block_time_ms); +static int IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, + void *item, void *hptw); -static int task_create_wrapper(void *task_func, const char *name, - uint32_t stack_depth, void *param, - uint32_t prio, void *task_handle, +static int task_create_wrapper(void *task_func, + const char *name, + uint32_t stack_depth, + void *param, + uint32_t prio, + void *task_handle, uint32_t core_id); static void task_delete_wrapper(void *task_handle); static bool IRAM_ATTR is_in_isr_wrapper(void); -static void *malloc_wrapper(size_t size); -static void IRAM_ATTR cause_sw_intr(void *arg); static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no); +static void *malloc_wrapper(size_t size); static void *malloc_internal_wrapper(size_t size); static int IRAM_ATTR read_mac_wrapper(uint8_t mac[6]); static void IRAM_ATTR srand_wrapper(unsigned int seed); static int IRAM_ATTR rand_wrapper(void); static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles); static uint32_t IRAM_ATTR btdm_us_2_lpcycles(uint32_t us); -static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size); -static int queue_send_wrapper(void *queue, void *item, - uint32_t block_time_ms); -static int queue_recv_wrapper(void *queue, void *item, - uint32_t block_time_ms); -static void queue_delete_wrapper(void *queue); -static void esp32_ints_on(uint32_t mask); -static int adapter_coex_register_bt_cb_wrapper(coex_func_cb_t cb); -static int adapter_coex_schm_register_btdm_callback(void *callback); -static int adapter_coex_register_wifi_channel_change_callback(void *cb); -static int adapter_coex_wifi_channel_get(uint8_t *primary, - uint8_t *secondary); -static void adapter_coex_schm_status_bit_clear(uint32_t type, - uint32_t status); -static void adapter_coex_schm_status_bit_set(uint32_t type, uint32_t status); -static uint32_t adapter_coex_schm_interval_get(void); -static uint8_t adapter_coex_schm_curr_period_get(void); -static void *adapter_coex_schm_curr_phase_get(void); - -#ifdef CONFIG_PM -static bool IRAM_ATTR btdm_sleep_check_duration(int32_t *half_slot_cnt); +static bool IRAM_ATTR btdm_sleep_check_duration(uint32_t *slot_cnt); static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles); static void btdm_sleep_enter_phase2_wrapper(void); static void btdm_sleep_exit_phase3_wrapper(void); -#endif - static bool coex_bt_wakeup_request(void); static void coex_bt_wakeup_request_end(void); -static int esp_int_adpt_cb(int irq, void *context, void *arg); +static int coex_bt_request_wrapper(uint32_t event, + uint32_t latency, + uint32_t duration); +static int coex_bt_release_wrapper(uint32_t event); +static int adapter_coex_register_bt_cb_wrapper(coex_func_cb_t cb); +static uint32_t coex_bb_reset_lock_wrapper(void); +static void coex_bb_reset_unlock_wrapper(uint32_t restore); +static int coex_schm_register_btdm_callback_wrapper(void *callback); +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 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_wifi_channel_get_wrapper(uint8_t *primary, + uint8_t *secondary); +static int coex_register_wifi_channel_change_callback_wrapper(void *cb); +static int coex_version_get_wrapper(unsigned int *major, + unsigned int *minor, + unsigned int *patch); + +/**************************************************************************** + * Other functions + ****************************************************************************/ + +static int32_t esp_task_create_pinned_to_core(void *entry, + const char *name, + uint32_t stack_depth, + void *param, + uint32_t prio, + void *task_handle, + uint32_t core_id); +static IRAM_ATTR int32_t esp_queue_send_generic(void *queue, + void *item, + uint32_t ticks, + int prio); +static void esp_update_time(struct timespec *timespec, uint32_t ticks); +static void IRAM_ATTR cause_sw_intr(void *arg); +#ifdef CONFIG_PM +static void btdm_slp_tmr_customer_callback(void * arg); +static void IRAM_ATTR btdm_slp_tmr_callback(void *arg); +#endif +static int IRAM_ATTR esp_int_adpt_cb(int irq, void *context, void *arg); +static void btdm_wakeup_request_callback(void * arg); +static void btdm_controller_mem_init(void); +static uint32_t btdm_config_mask_load(void); +static void bt_controller_deinit_internal(void); +static bool async_wakeup_request(int event); +static void async_wakeup_request_end(int event); /**************************************************************************** * Extern Functions declaration and value ****************************************************************************/ +/* Not for user call, so don't put to include file */ + +/* OSI */ + extern int btdm_osi_funcs_register(void *osi_funcs); -extern void btdm_controller_rom_data_init(void); -extern void coex_bt_high_prio(void); /* Initialise and De-initialise */ @@ -420,6 +431,9 @@ extern void btdm_controller_disable(void); extern uint8_t btdm_controller_get_mode(void); extern const char *btdm_controller_get_compile_version(void); extern void btdm_rf_bb_init_phase2(void); /* shall be called after PHY/RF is enabled */ +extern int btdm_dispatch_work_to_controller(workitem_handler_t callback, + void *arg, + bool blocking); /* Sleep */ @@ -427,14 +441,13 @@ extern void btdm_controller_enable_sleep(bool enable); extern void btdm_controller_set_sleep_mode(uint8_t mode); extern uint8_t btdm_controller_get_sleep_mode(void); extern bool btdm_power_state_active(void); -extern void btdm_wakeup_request(bool request_lock); -extern void btdm_wakeup_request_end(void); +extern void btdm_wakeup_request(void); +extern void btdm_in_wakeup_requesting_set(bool in_wakeup_requesting); /* Low Power Clock */ extern bool btdm_lpclk_select_src(uint32_t sel); extern bool btdm_lpclk_set_div(uint32_t div); -extern int btdm_hci_tl_io_event_post(int event); /* VHCI */ @@ -452,112 +465,41 @@ extern int bredr_txpwr_get(int *min_power_level, int *max_power_level); extern void bredr_sco_datapath_set(uint8_t data_path); extern void btdm_controller_scan_duplicate_list_clear(void); -/* Coexistence */ +/* Shutdown */ -int coex_bt_request_wrapper(uint32_t event, - uint32_t latency, - uint32_t duration); -int coex_bt_release_wrapper(uint32_t event); -uint32_t coex_bb_reset_lock_wrapper(void); -void coex_bb_reset_unlock_wrapper(uint32_t restore); -extern void coex_ble_adv_priority_high_set(bool high); -extern int coex_bt_request(uint32_t event, - uint32_t latency, - uint32_t duration); -extern int coex_bt_release(uint32_t event); -extern int coex_enable(void); -extern int coex_register_bt_cb(coex_func_cb_t cb); -extern int coex_schm_register_btdm_callback(void *callback); -extern int coex_register_wifi_channel_change_callback(void *cb); -extern int coex_wifi_channel_get(uint8_t *primary, - uint8_t *secondary); -extern int coex_register_bt_cb(coex_func_cb_t cb); -extern void coex_bb_reset_unlock(uint32_t restore); -extern uint32_t coex_bb_reset_lock(void); +extern void esp_bt_controller_shutdown(void); extern uint8_t _bss_start_btdm[]; extern uint8_t _bss_end_btdm[]; extern uint8_t _data_start_btdm[]; extern uint8_t _data_end_btdm[]; -extern const uint32_t _data_start_btdm_rom; -extern const uint32_t _data_end_btdm_rom; +extern uint32_t _data_start_btdm_rom; +extern uint32_t _data_end_btdm_rom; -extern uint8_t _bt_bss_start[]; -extern uint8_t _bt_bss_end[]; -extern uint8_t _btdm_bss_start[]; -extern uint8_t _btdm_bss_end[]; -extern uint8_t _bt_data_start[]; -extern uint8_t _bt_data_end[]; -extern uint8_t _btdm_data_start[]; -extern uint8_t _btdm_data_end[]; - -extern uint8_t _bt_tmp_bss_start[]; -extern uint8_t _bt_tmp_bss_end[]; - -void intr_matrix_set(int cpu_no, uint32_t model_num, uint32_t intr_num); +extern uint32_t _bt_bss_start; +extern uint32_t _bt_bss_end; +extern uint32_t _nimble_bss_start; +extern uint32_t _nimble_bss_end; +extern uint32_t _btdm_bss_start; +extern uint32_t _btdm_bss_end; +extern uint32_t _bt_data_start; +extern uint32_t _bt_data_end; +extern uint32_t _nimble_data_start; +extern uint32_t _nimble_data_end; +extern uint32_t _btdm_data_start; +extern uint32_t _btdm_data_end; /**************************************************************************** * Private Data ****************************************************************************/ -/* Controller status */ +/* OSI funcs */ -static DRAM_ATTR esp_bt_controller_status_t btdm_controller_status = - ESP_BT_CONTROLLER_STATUS_IDLE; - -/* low power control struct */ - -static DRAM_ATTR btdm_lpcntl_t g_lp_cntl; - -/* low power status struct */ - -static DRAM_ATTR btdm_lpstat_t g_lp_stat; - -/* measured average low power clock period in micro seconds */ - -static DRAM_ATTR uint32_t g_btdm_lpcycle_us = 0; - -/* number of fractional bit for g_btdm_lpcycle_us */ - -static DRAM_ATTR uint8_t g_btdm_lpcycle_us_frac = 0; - -#ifdef CONFIG_PM -/* semaphore used for blocking VHCI API to wait for controller to wake up */ - -static DRAM_ATTR void * g_wakeup_req_sem = NULL; - -/* wakeup timer */ - -static DRAM_ATTR esp_timer_handle_t g_btdm_slp_tmr; -#endif - -/* BT interrupt private data */ - -static sq_queue_t g_int_flags_free; - -static sq_queue_t g_int_flags_used; - -static struct irqstate_list_s g_int_flags[NR_IRQSTATE_FLAGS]; - -/* Cached queue control variables */ - -#ifdef CONFIG_ESP32_SPIFLASH -static struct esp_queuecache_s g_esp_queuecache[BLE_TASK_EVENT_QUEUE_LEN]; -static uint8_t g_esp_queuecache_buffer[BLE_TASK_EVENT_QUEUE_ITEM_SIZE]; -#endif - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/* BLE OS adapter data */ - -static struct osi_funcs_s g_osi_funcs = +static struct osi_funcs_s g_osi_funcs_ro = { - ._magic = OSI_MAGIC_VALUE, ._version = OSI_VERSION, - ._set_isr = esp_ble_set_isr, - ._ints_on = esp32_ints_on, + ._set_isr = ble_set_isr, + ._ints_on = ints_on, ._interrupt_disable = interrupt_disable, ._interrupt_restore = interrupt_restore, ._task_yield = task_yield_from_isr, @@ -590,38 +532,44 @@ static struct osi_funcs_s g_osi_funcs = ._rand = rand_wrapper, ._btdm_lpcycles_2_us = btdm_lpcycles_2_us, ._btdm_us_2_lpcycles = btdm_us_2_lpcycles, -#ifdef CONFIG_PM ._btdm_sleep_check_duration = btdm_sleep_check_duration, ._btdm_sleep_enter_phase1 = btdm_sleep_enter_phase1_wrapper, ._btdm_sleep_enter_phase2 = btdm_sleep_enter_phase2_wrapper, + ._btdm_sleep_exit_phase1 = NULL, + ._btdm_sleep_exit_phase2 = NULL, ._btdm_sleep_exit_phase3 = btdm_sleep_exit_phase3_wrapper, -#endif ._coex_bt_wakeup_request = coex_bt_wakeup_request, ._coex_bt_wakeup_request_end = coex_bt_wakeup_request_end, ._coex_bt_request = coex_bt_request_wrapper, ._coex_bt_release = coex_bt_release_wrapper, ._coex_register_bt_cb = adapter_coex_register_bt_cb_wrapper, - ._coex_register_wifi_channel_change_callback = - adapter_coex_register_wifi_channel_change_callback, - ._coex_wifi_channel_get = adapter_coex_wifi_channel_get, - ._coex_schm_status_bit_clear = adapter_coex_schm_status_bit_clear, - ._coex_schm_status_bit_set = adapter_coex_schm_status_bit_set, - ._coex_schm_interval_get = adapter_coex_schm_interval_get, - ._coex_schm_curr_period_get = adapter_coex_schm_curr_period_get, - ._coex_schm_curr_phase_get = adapter_coex_schm_curr_phase_get, - ._coex_schm_register_btdm_callback = - adapter_coex_schm_register_btdm_callback, ._coex_bb_reset_lock = coex_bb_reset_lock_wrapper, ._coex_bb_reset_unlock = coex_bb_reset_unlock_wrapper, + ._coex_schm_register_btdm_callback = + coex_schm_register_btdm_callback_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_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_wifi_channel_get = coex_wifi_channel_get_wrapper, + ._coex_register_wifi_channel_change_callback = + coex_register_wifi_channel_change_callback_wrapper, + ._set_isr_l3 = ble_set_isr, + ._interrupt_l3_disable = interrupt_disable, + ._interrupt_l3_restore = interrupt_restore, + ._customer_queue_create = NULL, + ._coex_version_get = coex_version_get_wrapper, + ._magic = OSI_MAGIC_VALUE, }; -/* The mode column will be modified by release function to indicate the - * available region +/* The mode column will be modified by release function to indicate + * the available region. */ -static btdm_dram_available_region_t btdm_dram_available_region[] = +static btdm_dram_available_region_t g_btdm_dram_available_region[] = { - /* following is .data */ + /* The following is .data */ { ESP_BT_MODE_BTDM, @@ -629,7 +577,7 @@ static btdm_dram_available_region_t btdm_dram_available_region[] = SOC_MEM_BT_DATA_END }, - /* following is memory which HW will use */ + /* The following is memory which HW will use */ { ESP_BT_MODE_BTDM, @@ -652,7 +600,7 @@ static btdm_dram_available_region_t btdm_dram_available_region[] = SOC_MEM_BT_EM_BREDR_REAL_END }, - /* following is .bss */ + /* The following is .bss */ { ESP_BT_MODE_BTDM, @@ -666,212 +614,199 @@ static btdm_dram_available_region_t btdm_dram_available_region[] = }, }; -extern void btdm_controller_set_sleep_mode(uint8_t mode); +/* Reserve the full memory region used by Bluetooth Controller. + * Some may be released later at runtime. + */ + +SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_EM_START, + SOC_MEM_BT_EM_BREDR_REAL_END, + rom_bt_em); +SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_BSS_START, + SOC_MEM_BT_BSS_END, + rom_bt_bss); +SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_MISC_START, + SOC_MEM_BT_MISC_END, + rom_bt_misc); +SOC_RESERVE_MEMORY_REGION(SOC_MEM_BT_DATA_START, + SOC_MEM_BT_DATA_END, + rom_bt_data); + +static DRAM_ATTR struct osi_funcs_s *g_osi_funcs_p; + +/* timestamp when PHY/RF was switched on */ + +static DRAM_ATTR int64_t g_time_phy_rf_just_enabled = 0; + +static DRAM_ATTR esp_bt_controller_status_t g_btdm_controller_status = + ESP_BT_CONTROLLER_STATUS_IDLE; + +/* measured average low power clock period in micro seconds */ + +static DRAM_ATTR uint32_t g_btdm_lpcycle_us = 0; + +/* number of fractional bit for g_btdm_lpcycle_us */ + +static DRAM_ATTR uint8_t g_btdm_lpcycle_us_frac = 0; + +#ifdef CONFIG_BTDM_CTRL_MODEM_SLEEP_MODE_ORIG + +/* used low power clock */ + +static DRAM_ATTR uint8_t g_btdm_lpclk_sel; + +#endif /* CONFIG_BTDM_CTRL_MODEM_SLEEP_MODE_ORIG */ + +/* semaphore used for blocking VHCI API to wait for controller to wake up */ + +static DRAM_ATTR struct bt_sem_s * g_wakeup_req_sem = NULL; + +#ifdef CONFIG_PM + +/* wakeup timer */ + +static DRAM_ATTR esp_timer_handle_t g_btdm_slp_tmr; + +static bool g_pm_lock_acquired = true; + +static DRAM_ATTR bool g_btdm_allow_light_sleep; + +#endif + +/* BT interrupt private data */ + +static sq_queue_t g_ble_int_flags_free; + +static sq_queue_t g_ble_int_flags_used; + +static struct irqstate_list_s g_ble_int_flags[NR_IRQSTATE_FLAGS]; + +/* Cached queue control variables */ + +#ifdef CONFIG_ESP32_SPIFLASH +static struct esp_queuecache_s g_esp_queuecache[BLE_TASK_EVENT_QUEUE_LEN]; +static uint8_t g_esp_queuecache_buffer[BLE_TASK_EVENT_QUEUE_ITEM_SIZE]; +#endif /**************************************************************************** - * Private Functions and Public Functions only used by libraries + * Public Data ****************************************************************************/ -static int adapter_coex_register_bt_cb_wrapper(coex_func_cb_t cb) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_register_bt_cb(cb); -#else - return 0; -#endif -} +/**************************************************************************** + * Private Functions + ****************************************************************************/ -static int adapter_coex_schm_register_btdm_callback(void *callback) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_schm_register_btdm_callback(callback); -#else - return 0; -#endif -} - -static int adapter_coex_register_wifi_channel_change_callback(void *cb) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_register_wifi_channel_change_callback(cb); -#else - return -1; -#endif -} - -static int adapter_coex_wifi_channel_get(uint8_t *primary, - uint8_t *secondary) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_wifi_channel_get(primary, secondary); -#else - return -1; -#endif -} - -static void adapter_coex_schm_status_bit_clear(uint32_t type, - uint32_t status) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - coex_schm_status_bit_clear(type, status); -#endif -} - -static void adapter_coex_schm_status_bit_set(uint32_t type, uint32_t status) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - coex_schm_status_bit_set(type, status); -#endif -} - -static uint32_t adapter_coex_schm_interval_get(void) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_schm_interval_get(); -#else - return 0; -#endif -} - -static uint8_t adapter_coex_schm_curr_period_get(void) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_schm_interval_get(); -#else - return 0; -#endif -} - -static void *adapter_coex_schm_curr_phase_get(void) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_schm_curr_phase_get(); -#else - return NULL; -#endif -} +/**************************************************************************** + * Inline Functions + ****************************************************************************/ /**************************************************************************** * Name: esp_errno_trans * * Description: - * Transform from nuttx error code to Wi-Fi adapter error code + * Transform NuttX error code to a boolean value. Returns true if the + * input error code is 0 (no error), and false otherwise. * * Input Parameters: * ret - NuttX error code * * Returned Value: - * Wi-Fi adapter error code + * Boolean value indicating the absence (true) or presence (false) of an + * error * ****************************************************************************/ static inline int32_t esp_errno_trans(int ret) { if (!ret) - { - return true; - } + { + return true; + } else - { - return false; - } + { + return false; + } } /**************************************************************************** - * Name: esp_task_create_pinned_to_core + * Name: esp_bt_power_domain_on * * Description: - * Create task and bind it to target CPU, the task will run when it - * is created + * Power up the Bluetooth module. This function is a wrapper for the + * esp_wifi_bt_power_domain_on function. * * 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 + * None * * Returned Value: - * True if success or false if fail + * None * ****************************************************************************/ -static int32_t esp_task_create_pinned_to_core(void *entry, - const char *name, - uint32_t stack_depth, - void *param, - uint32_t prio, - void *task_handle, - uint32_t core_id) +static inline void esp_bt_power_domain_on(void) { - int pid; -#ifdef CONFIG_SMP - int ret; - cpu_set_t cpuset; -#endif - - DEBUGASSERT(task_handle != NULL); - -#ifdef CONFIG_SMP - ret = sched_lock(); - if (ret) - { - wlerr("Failed to lock scheduler before creating pinned thread\n"); - return false; - } -#endif - - pid = kthread_create(name, prio, stack_depth, entry, - (char * const *)param); - if (pid > 0) - { - if (task_handle) - { - *((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, error %d\n", pid); - } - -#ifdef CONFIG_SMP - ret = sched_unlock(); - if (ret) - { - wlerr("Failed to unlock scheduler after creating pinned thread\n"); - return false; - } -#endif - - return pid > 0; + esp_wifi_bt_power_domain_on(); } /**************************************************************************** - * Name: esp_set_isr + * Name: esp_bt_power_domain_off + * + * Description: + * Power down the Bluetooth module. This function is a wrapper for the + * esp_wifi_bt_power_domain_off function. + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +static inline void esp_bt_power_domain_off(void) +{ + esp_wifi_bt_power_domain_off(); +} + +/**************************************************************************** + * Name: btdm_check_and_init_bb + * + * Description: + * Check and initialize the Bluetooth baseband (BB). If the PHY/RF has + * been switched off since the last Bluetooth baseband initialization, it + * re-initializes the baseband. + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +static inline void btdm_check_and_init_bb(void) +{ + int64_t latest_ts = esp_phy_rf_get_on_ts(); + + if (latest_ts != g_time_phy_rf_just_enabled || + g_time_phy_rf_just_enabled == 0) + { + btdm_rf_bb_init_phase2(); + g_time_phy_rf_just_enabled = latest_ts; + } +} + +/**************************************************************************** + * Functions to be registered to struct osi_funcs_s + ****************************************************************************/ + +/**************************************************************************** + * Name: ble_set_isr * * Description: * Register interrupt function * * Input Parameters: - * n - Interrupt ID + * n - CPU Interrupt ID * f - Interrupt function * arg - Function private data * @@ -880,7 +815,7 @@ static int32_t esp_task_create_pinned_to_core(void *entry, * ****************************************************************************/ -static xt_handler esp_ble_set_isr(int n, xt_handler f, void *arg) +static xt_handler ble_set_isr(int n, xt_handler f, void *arg) { int ret; uint32_t tmp; @@ -921,7 +856,7 @@ static xt_handler esp_ble_set_isr(int n, xt_handler f, void *arg) } /**************************************************************************** - * Name: esp32_ints_on + * Name: ints_on * * Description: * Enable BLE interrupt @@ -934,7 +869,7 @@ static xt_handler esp_ble_set_isr(int n, xt_handler f, void *arg) * ****************************************************************************/ -static void esp32_ints_on(uint32_t mask) +static void ints_on(uint32_t mask) { uint32_t bit; int irq; @@ -943,182 +878,17 @@ static void esp32_ints_on(uint32_t mask) { bit = 1 << i; if (bit & mask) - { - irq = esp32_getirq(0, i); - DEBUGVERIFY(esp32_irq_set_iram_isr(irq)); - up_enable_irq(irq); - wlinfo("Enabled bit %d\n", irq); - } + { + irq = esp32_getirq(0, i); + DEBUGVERIFY(esp32_irq_set_iram_isr(irq)); + up_enable_irq(irq); + wlinfo("Enabled bit %d\n", irq); + } } UNUSED(irq); } -/**************************************************************************** - * Name: is_wifi_clk_peripheral - * - * Description: - * Checks if the peripheral module needs Wi-Fi Clock. - * - * Input Parameters: - * periph - The peripheral module - * - * Returned Value: - * true if it depends on Wi-Fi clock or false otherwise. - * - ****************************************************************************/ - -static bool is_wifi_clk_peripheral(periph_module_e periph) -{ - /* A small subset of peripherals use WIFI_CLK_EN_REG and - * CORE_RST_EN_REG for their clock & reset registers - */ - - switch (periph) - { - case PERIPH_SDMMC_MODULE: - case PERIPH_SDIO_SLAVE_MODULE: - case PERIPH_EMAC_MODULE: - case PERIPH_RNG_MODULE: - case PERIPH_WIFI_MODULE: - case PERIPH_BT_MODULE: - case PERIPH_WIFI_BT_COMMON_MODULE: - case PERIPH_BT_BASEBAND_MODULE: - case PERIPH_BT_LC_MODULE: - return true; - default: - return false; - } -} - -/**************************************************************************** - * Name: get_clk_en_mask - * - * Description: - * Returns the WIFI_BT clock mask case it is BLE peripheral. - * - * Input Parameters: - * periph - The peripheral module - * - * Returned Value: - * The clock peripheral mask. - * - ****************************************************************************/ - -static uint32_t get_clk_en_mask(periph_module_e periph) -{ - switch (periph) - { - case PERIPH_BT_MODULE: - return DPORT_WIFI_CLK_BT_EN_M; - default: - return 0; - } -} - -/**************************************************************************** - * Name: get_rst_en_mask - * - * Description: - * Returns the WIFI_BT reset mask - * - * Input Parameters: - * periph - The peripheral module - * enable - Enable/Disable - * - * Returned Value: - * The reset peripheral mask. - * - ****************************************************************************/ - -static uint32_t get_rst_en_mask(periph_module_e periph, bool enable) -{ - return 0; -} - -/**************************************************************************** - * Name: get_clk_en_reg - * - * Description: - * Returns the WIFI_BT clock register - * - * Input Parameters: - * periph - The peripheral module - * - * Returned Value: - * The clock peripheral register. - * - ****************************************************************************/ - -static uint32_t get_clk_en_reg(periph_module_e periph) -{ - return is_wifi_clk_peripheral(periph) ? DPORT_WIFI_CLK_EN_REG : - DPORT_PERIP_CLK_EN_REG; -} - -/**************************************************************************** - * Name: get_rst_en_reg - * - * Description: - * Returns the WIFI_BT reset register - * - * Input Parameters: - * periph - The peripheral module - * - * Returned Value: - * The reset peripheral register. - * - ****************************************************************************/ - -static uint32_t get_rst_en_reg(periph_module_e periph) -{ - return is_wifi_clk_peripheral(periph) ? DPORT_CORE_RST_EN_REG : - DPORT_PERIP_RST_EN_REG; -} - -/**************************************************************************** - * Name: bt_periph_module_enable - * - * Description: - * Enable the bluetooth module - * - * Input Parameters: - * periph - The peripheral module - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void bt_periph_module_enable(periph_module_e periph) -{ - modifyreg32(get_clk_en_reg(periph), 0, get_clk_en_mask(periph)); - modifyreg32(get_rst_en_reg(periph), get_rst_en_mask(periph, true), 0); -} - -/**************************************************************************** - * Name: esp_int_adpt_cb - * - * Description: - * BT interrupt adapter callback function - * - * Input Parameters: - * arg - interrupt adapter private data - * - * Returned Value: - * NuttX error code - * - ****************************************************************************/ - -static int IRAM_ATTR esp_int_adpt_cb(int irq, void *context, void *arg) -{ - struct irq_adpt_s *adapter = (struct irq_adpt_s *)arg; - - adapter->func(adapter->arg); - - return OK; -} - /**************************************************************************** * Name: interrupt_disable * @@ -1138,13 +908,13 @@ static void IRAM_ATTR interrupt_disable(void) { struct irqstate_list_s *irqstate; - irqstate = (struct irqstate_list_s *)sq_remlast(&g_int_flags_free); + irqstate = (struct irqstate_list_s *)sq_remlast(&g_ble_int_flags_free); - DEBUGASSERT(irqstate != NULL); + ASSERT(irqstate != NULL); irqstate->flags = enter_critical_section(); - sq_addlast((sq_entry_t *)irqstate, &g_int_flags_used); + sq_addlast((sq_entry_t *)irqstate, &g_ble_int_flags_used); } /**************************************************************************** @@ -1166,13 +936,13 @@ static void IRAM_ATTR interrupt_restore(void) { struct irqstate_list_s *irqstate; - irqstate = (struct irqstate_list_s *)sq_remlast(&g_int_flags_used); + irqstate = (struct irqstate_list_s *)sq_remlast(&g_ble_int_flags_used); - DEBUGASSERT(irqstate != NULL); + ASSERT(irqstate != NULL); leave_critical_section(irqstate->flags); - sq_addlast((sq_entry_t *)irqstate, &g_int_flags_free); + sq_addlast((sq_entry_t *)irqstate, &g_ble_int_flags_free); } /**************************************************************************** @@ -1291,6 +1061,7 @@ static int IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw) * * Input Parameters: * semphr - Semaphore data pointer + * hptw - Unused. * * Returned Value: * True if success or false if fail @@ -1319,36 +1090,6 @@ static int IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw) return esp_errno_trans(ret); } -/**************************************************************************** - * Name: esp_update_time - * - * Description: - * Transform ticks to time and add this time to timespec value - * - * Input Parameters: - * ticks - System ticks - * - * Output Parameters: - * timespec - Input timespec data pointer - * - * 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; -} - /**************************************************************************** * Name: semphr_take_wrapper * @@ -1531,6 +1272,1120 @@ static int mutex_unlock_wrapper(void *mutex) return esp_errno_trans(ret); } +/**************************************************************************** + * Name: queue_create_wrapper + * + * Description: + * Create message queue + * + * Input Parameters: + * queue_len - queue message number + * item_size - message size + * + * Returned Value: + * Message queue data pointer + * + ****************************************************************************/ + +static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size) +{ + struct mq_attr attr; + struct mq_adpt_s *mq_adpt; + int ret; + + mq_adpt = kmm_malloc(sizeof(struct mq_adpt_s)); + DEBUGASSERT(mq_adpt); + + 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 %d\n", ret); + kmm_free(mq_adpt); + return NULL; + } + + mq_adpt->msgsize = item_size; + +#ifdef CONFIG_ESP32_SPIFLASH + if (queue_len <= BLE_TASK_EVENT_QUEUE_LEN && + item_size == BLE_TASK_EVENT_QUEUE_ITEM_SIZE) + { + esp_init_queuecache(g_esp_queuecache, + &mq_adpt->mq, + g_esp_queuecache_buffer, + BLE_TASK_EVENT_QUEUE_LEN, + BLE_TASK_EVENT_QUEUE_ITEM_SIZE); + } + else + { + wlerr("Failed to create queue cache." + " Please incresase BLE_TASK_EVENT_QUEUE_LEN to, at least, %d", + queue_len); + return NULL; + } +#endif + + return (void *)mq_adpt; +} + +/**************************************************************************** + * Name: queue_delete_wrapper + * + * Description: + * Delete message queue + * + * Input Parameters: + * queue - Message queue data pointer + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void queue_delete_wrapper(void *queue) +{ + struct mq_adpt_s *mq_adpt = (struct mq_adpt_s *)queue; + + file_mq_close(&mq_adpt->mq); + file_mq_unlink(mq_adpt->name); + kmm_free(mq_adpt); +} + +/**************************************************************************** + * Name: queue_send_wrapper + * + * Description: + * Generic send message to queue within a certain period of time + * + * Input Parameters: + * queue - Message queue data pointer + * item - Item to be sent. + * block_time_ms - Wait time + * + * Returned Value:uint32_t + * True if success or false if fail + * + ****************************************************************************/ + +static int queue_send_wrapper(void *queue, void *item, + uint32_t block_time_ms) +{ + return esp_queue_send_generic(queue, item, block_time_ms, 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 int IRAM_ATTR queue_send_from_isr_wrapper(void *queue, + void *item, + void *hptw) +{ + *((int *)hptw) = false; + return esp_queue_send_generic(queue, item, 0, 0); +} + +/**************************************************************************** + * 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 + * block_time_ms - Wait time + * + * Returned Value: + * True if success or false if fail + * + ****************************************************************************/ + +static int queue_recv_wrapper(void *queue, void *item, + uint32_t block_time_ms) +{ + ssize_t ret; + struct timespec timeout; + unsigned int prio; + struct mq_adpt_s *mq_adpt = (struct mq_adpt_s *)queue; + + if (block_time_ms == 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 %d\n", ret); + return false; + } + + if (block_time_ms) + { + esp_update_time(&timeout, MSEC2TICK(block_time_ms)); + } + + 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: queue_recv_from_isr_wrapper + * + * Description: + * Receive message from queue 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 int IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, + void *item, + void *hptw) +{ + DEBUGPANIC(); + return 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 int task_create_wrapper(void *task_func, const char *name, + uint32_t stack_depth, void *param, + uint32_t prio, void *task_handle, + uint32_t core_id) +{ + return esp_task_create_pinned_to_core(task_func, name, + stack_depth, param, + prio, task_handle, core_id); +} + +/**************************************************************************** + * 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: is_in_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_in_isr_wrapper(void) +{ + return up_interrupt_context(); +} + +/**************************************************************************** + * Name: cause_sw_intr_to_core_wrapper + * + * Description: + * Just a wrapper to cause_sw_intr + * + * Input Parameters: + * core_id - ID of the CPU core, not used. + * intr_no - Number of the software interrupt + * + * Returned Value: + * Always return OK. + * + ****************************************************************************/ + +static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no) +{ + cause_sw_intr((void *)intr_no); + return ESP_OK; +} + +/**************************************************************************** + * Name: malloc_wrapper + * + * Description: + * Malloc buffer + * + * Input Parameters: + * size - buffer size + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void *malloc_wrapper(size_t size) +{ + void * p = NULL; + + p = kmm_malloc(size); + DEBUGASSERT(p); + + return p; +} + +/**************************************************************************** + * Name: malloc_internal_wrapper + * + * Description: + * Malloc buffer in DRAM + * + * Input Parameters: + * szie - buffer size + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void *malloc_internal_wrapper(size_t size) +{ + void * p = NULL; + + p = kmm_malloc(size); + DEBUGASSERT(p); + + return p; +} + +/**************************************************************************** + * Name: read_mac_wrapper + * + * Description: + * Get Mac Address + * + * Input Parameters: + * mac - mac address + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise, -1 (ERROR) is returned. + * + ****************************************************************************/ + +static int IRAM_ATTR read_mac_wrapper(uint8_t mac[6]) +{ + return esp_read_mac(mac, ESP_MAC_BT); +} + +/**************************************************************************** + * Name: srand_wrapper + * + * Description: + * Get random value with seed input. Not implemented. + * + * Input Parameters: + * seed - Value to be used as seed. + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void IRAM_ATTR srand_wrapper(unsigned int seed) +{ + /* empty function */ +} + +/**************************************************************************** + * Name: rand_wrapper + * + * Description: + * Get random value. + * + * Input Parameters: + * None + * + * Returned Value: + * Random value + * + ****************************************************************************/ + +static int IRAM_ATTR rand_wrapper(void) +{ + return esp_random(); +} + +/**************************************************************************** + * Name: btdm_lpcycles_2_us + * + * Description: + * Converts a number of low power clock cycles into a duration in us + * + * Input Parameters: + * cycles - number of CPU cycles + * + * Returned Value: + * us - value equivalent to the CPU cycles in us + * + ****************************************************************************/ + +static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles) +{ + /* The number of lp cycles should not lead to overflow. Thrs: 100s + * clock measurement is conducted + */ + + uint64_t us = (uint64_t)g_btdm_lpcycle_us * cycles; + us = (us + (1 << (g_btdm_lpcycle_us_frac - 1))) >> g_btdm_lpcycle_us_frac; + return (uint32_t)us; +} + +/**************************************************************************** + * Name: btdm_us_2_lpcycles + * + * Description: + * Converts a duration in slots into a number of low power clock cycles. + * + * Input Parameters: + * us - duration in us + * + * Returned Value: + * cycles + * + ****************************************************************************/ + +static uint32_t IRAM_ATTR btdm_us_2_lpcycles(uint32_t us) +{ + /* The number of sleep duration(us) should not lead to overflow. Thrs: 100s + * Compute the sleep duration in us to low power clock cycles, with + * calibration result applied clock measurement is conducted + */ + + uint64_t cycles; + cycles = ((uint64_t)(us) << g_btdm_lpcycle_us_frac) / g_btdm_lpcycle_us; + return (uint32_t)cycles; +} + +/**************************************************************************** + * Name: btdm_sleep_check_duration + * + * Description: + * Wake up in advance considering the delay in enabling PHY/RF. + * + * Input Parameters: + * half_slot_cnt - half slots to allow to fall into modem sleep + * + * Returned Value: + * None + * + ****************************************************************************/ + +static bool IRAM_ATTR btdm_sleep_check_duration(uint32_t *slot_cnt) +{ + if (*slot_cnt < BTDM_MIN_SLEEP_DURATION) + { + return false; + } + + *slot_cnt -= BTDM_MODEM_WAKE_UP_DELAY; + return true; +} + +/**************************************************************************** + * Name: btdm_sleep_enter_phase1_wrapper + * + * Description: + * ESP32 BLE lightsleep callback function. + * + * Input Parameters: + * lpcycles - light sleep cycles + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles) +{ +#ifdef CONFIG_PM + uint32_t us_to_sleep; + uint32_t uncertainty; + + if (g_lp_cntl.wakeup_timer_required == false) + { + return; + } + + /* start a timer to wake up and acquire the pm_lock before sleep awakes */ + + us_to_sleep = btdm_lpcycles_2_us(lpcycles, NULL); + + DEBUGASSERT(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US); + uncertainty = (us_to_sleep >> 11); + + if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US) + { + uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US; + } + + DEBUGASSERT(g_lp_stat.wakeup_timer_started == false); + + if (esp_timer_start_once(g_btdm_slp_tmr, + us_to_sleep - uncertainty) == ESP_OK) + { + g_lp_stat.wakeup_timer_started = true; + } + else + { + wlerr("timer start failed"); + DEBUGPANIC(); + } +#endif +} + +/**************************************************************************** + * Name: btdm_sleep_enter_phase2_wrapper + * + * Description: + * ESP32 BLE lightsleep callback function. + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void btdm_sleep_enter_phase2_wrapper(void) +{ + if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) + { + esp_phy_disable(); +#ifdef CONFIG_PM + if (g_pm_lock_acquired) + { + esp32_pm_lockrelease(); + g_pm_lock_acquired = false; + } +#endif + } + else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) + { + esp_phy_disable(); + + /* pause bluetooth baseband */ + + periph_module_disable(PERIPH_BT_BASEBAND_MODULE); + } +} + +/**************************************************************************** + * Name: btdm_sleep_exit_phase3_wrapper + * + * Description: + * ESP32 BLE lightsleep callback function.. + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +void btdm_sleep_exit_phase3_wrapper(void) +{ +#ifdef CONFIG_PM + if (g_pm_lock_acquired == false) + { + g_pm_lock_acquired = true; + esp32_pm_lockacquire(); + } +#endif + + if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) + { + esp_phy_enable(); + btdm_check_and_init_bb(); +#ifdef CONFIG_PM + esp_timer_stop(g_btdm_slp_tmr); +#endif + } + else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) + { + /* resume bluetooth baseband */ + + periph_module_enable(PERIPH_BT_BASEBAND_MODULE); + esp_phy_enable(); + } +} + +/**************************************************************************** + * Name: coex_bt_wakeup_request + * + * Description: + * Request a Wi-Fi/BLE Coex wakeup request + * + * Input Parameters: + * none + * + * Returned Value: + * true if request lock is needed, false otherwise + * + ****************************************************************************/ + +static bool coex_bt_wakeup_request(void) +{ + return async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_COEX); +} + +/**************************************************************************** + * Name: coex_bt_wakeup_request_end + * + * Description: + * Finish Wi-Fi/BLE Coex wakeup request + * + * Input Parameters: + * none + * + * Returned Value: + * none + * + ****************************************************************************/ + +static void coex_bt_wakeup_request_end(void) +{ + async_wakeup_request_end(BTDM_ASYNC_WAKEUP_REQ_COEX); +} + +/**************************************************************************** + * Name: coex_bt_request_wrapper + * + * Description: + * Bluetooth requests coexistence. + * + * Input Parameters: + * event - Bluetooth event + * latency - Bluetooth will request coexistence after latency + * duration - duration for Bluetooth to request coexistence + * + * Returned Value: + * 0 on success, other values indicate failure + * + ****************************************************************************/ + +static int IRAM_ATTR coex_bt_request_wrapper(uint32_t event, + uint32_t latency, + uint32_t duration) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_bt_request(event, latency, duration); +#else + return 0; +#endif +} + +/**************************************************************************** + * Name: coex_bt_release_wrapper + * + * Description: + * Bluetooth releases coexistence. + * + * Input Parameters: + * event - Bluetooth event + * + * Returned Value: + * 0 on success, other values indicate failure + * + ****************************************************************************/ + +static int IRAM_ATTR coex_bt_release_wrapper(uint32_t event) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_bt_release(event); +#else + return 0; +#endif +} + +/**************************************************************************** + * Name: adapter_coex_register_bt_cb_wrapper + * + * Description: + * Bluetooth registers callback function to coexistence module. + * This function is only used on ESP32. + * + * Input Parameters: + * cb - callback function registered to coexistence module + * + * Returned Value: + * 0 on success, other values indicate failure + * + ****************************************************************************/ + +static int adapter_coex_register_bt_cb_wrapper(coex_func_cb_t cb) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_register_bt_cb(cb); +#else + return 0; +#endif +} + +/**************************************************************************** + * Name: coex_bb_reset_lock_wrapper + * + * Description: + * To acquire the spin-lock used in resetting Bluetooth baseband. + * This function is only used to workaround ESP32 hardware issue. + * + * Input Parameters: + * None + * + * Returned Value: + * Value of the spinlock to be restored + * + ****************************************************************************/ + +static uint32_t IRAM_ATTR coex_bb_reset_lock_wrapper(void) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_bb_reset_lock(); +#else + return 0; +#endif +} + +/**************************************************************************** + * Name: coex_bb_reset_unlock_wrapper + * + * Description: + * To release the spin-lock used in resetting Bluetooth baseband. + * This function is only used to workaround ESP32 hardware issue. + * + * Input Parameters: + * restore - value of the spinlock returned from previous call of + * coex_bb_rest_lock + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void IRAM_ATTR coex_bb_reset_unlock_wrapper(uint32_t restore) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + coex_bb_reset_unlock(restore); +#endif +} + +/**************************************************************************** + * Name: coex_schm_register_btdm_callback_wrapper + * + * Description: + * Register callback for coexistence scheme. + * + * Input Parameters: + * callback - callback function to be registered + * + * Returned Value: + * 0 on success, other values indicate failure + * + ****************************************************************************/ + +static int coex_schm_register_btdm_callback_wrapper(void *callback) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_schm_register_callback(COEX_SCHM_CALLBACK_TYPE_BT, callback); +#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) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + 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) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + coex_schm_status_bit_set(type, status); +#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) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + 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) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_schm_interval_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) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_schm_curr_phase_get(); +#else + return NULL; +#endif +} + +/**************************************************************************** + * Name: coex_wifi_channel_get_wrapper + * + * Description: + * Get WiFi channel from coexistence module. + * + * Input Parameters: + * primary - pointer to value of WiFi primary channel + * secondary - pointer to value of WiFi secondary channel + * + * Returned Value: + * 0 on success, other values indicate failure + * + ****************************************************************************/ + +static int coex_wifi_channel_get_wrapper(uint8_t *primary, + uint8_t *secondary) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_wifi_channel_get(primary, secondary); +#else + return -1; +#endif +} + +/**************************************************************************** + * Name: coex_register_wifi_channel_change_callback_wrapper + * + * Description: + * Bluetooth registers callback function to receive notification when Wi-Fi + * channel changes. + * + * Input Parameters: + * cb - callback function registered to coexistence module + * + * Returned Value: + * 0 on success, other values indicate failure + * + ****************************************************************************/ + +static int coex_register_wifi_channel_change_callback_wrapper(void *cb) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_register_wifi_channel_change_callback(cb); +#else + return -1; +#endif +} + +/**************************************************************************** + * Name: coex_version_get_wrapper + * + * Description: + * Get the version of the coexistence module. + * + * Input Parameters: + * major - pointer to store the major version number + * minor - pointer to store the minor version number + * patch - pointer to store the patch version number + * + * Returned Value: + * 0 on success, -1 on failure + * + ****************************************************************************/ + +static int coex_version_get_wrapper(unsigned int *major, + unsigned int *minor, + unsigned int *patch) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + const char *ver_str = coex_version_get(); + + if (ver_str != NULL) + { + unsigned int _major = 0; + unsigned int _minor = 0; + unsigned int _patch = 0; + + if (sscanf(ver_str, "%u.%u.%u", &_major, &_minor, &_patch) != 3) + { + return -1; + } + + if (major != NULL) + { + *major = _major; + } + + if (minor != NULL) + { + *minor = _minor; + } + + if (patch != NULL) + { + *patch = _patch; + } + + return 0; + } +#endif + + return -1; +} + +/**************************************************************************** + * Other functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp_task_create_pinned_to_core + * + * 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 esp_task_create_pinned_to_core(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 + + DEBUGASSERT(task_handle != NULL); + +#ifdef CONFIG_SMP + ret = sched_lock(); + if (ret) + { + wlerr("Failed to lock scheduler before creating pinned thread\n"); + return false; + } +#endif + + pid = kthread_create(name, prio, stack_depth, entry, + (char * const *)param); + if (pid > 0) + { + if (task_handle) + { + *((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, error %d\n", pid); + } + +#ifdef CONFIG_SMP + ret = sched_unlock(); + if (ret) + { + wlerr("Failed to unlock scheduler after creating pinned thread\n"); + return false; + } +#endif + + return pid > 0; +} + /**************************************************************************** * Name: esp_queue_send_generic * @@ -1603,145 +2458,33 @@ static IRAM_ATTR int32_t esp_queue_send_generic(void *queue, void *item, } /**************************************************************************** - * Name: queue_send_from_isr_wrapper + * Name: esp_update_time * * Description: - * Send message of low priority to queue in ISR within - * a certain period of time + * Transform ticks to time and add this time to timespec value * * Input Parameters: - * queue - Message queue data pointer - * item - Message data pointer - * hptw - Unused + * ticks - System ticks * - * Returned Value: - * True if success or false if fail - * - ****************************************************************************/ - -static int IRAM_ATTR queue_send_from_isr_wrapper(void *queue, - void *item, - void *hptw) -{ - *((int *)hptw) = false; - return esp_queue_send_generic(queue, item, 0, 0); -} - -/**************************************************************************** - * Name: queue_recv_from_isr_wrapper - * - * Description: - * Receive message from queue 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 int IRAM_ATTR queue_recv_from_isr_wrapper(void *queue, - void *item, - void *hptw) -{ - DEBUGPANIC(); - return 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 int task_create_wrapper(void *task_func, const char *name, - uint32_t stack_depth, void *param, - uint32_t prio, void *task_handle, - uint32_t core_id) -{ - return esp_task_create_pinned_to_core(task_func, name, - stack_depth, param, - prio, task_handle, core_id); -} - -/**************************************************************************** - * 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 + * Output Parameters: + * timespec - Input timespec data pointer * * Returned Value: * None * ****************************************************************************/ -static void task_delete_wrapper(void *task_handle) +static void esp_update_time(struct timespec *timespec, uint32_t ticks) { - pid_t pid = (pid_t)((uintptr_t)task_handle); - kthread_delete(pid); -} + uint32_t tmp; -/**************************************************************************** - * Name: is_in_isr_wrapper - * - * Description: - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ + tmp = TICK2SEC(ticks); + timespec->tv_sec += tmp; -static bool IRAM_ATTR is_in_isr_wrapper(void) -{ - return false; -} + ticks -= SEC2TICK(tmp); + tmp = TICK2NSEC(ticks); -/**************************************************************************** - * Name: malloc_wrapper - * - * Description: - * Malloc buffer - * - * Input Parameters: - * size - buffer size - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void *malloc_wrapper(size_t size) -{ - void * p = NULL; - - p = kmm_malloc(size); - DEBUGASSERT(p); - - return p; + timespec->tv_nsec += tmp; } /**************************************************************************** @@ -1766,192 +2509,32 @@ static void IRAM_ATTR cause_sw_intr(void *arg) XTHAL_SET_INTSET((1 << intr_no)); } -/**************************************************************************** - * Name: cause_sw_intr_to_core_wrapper - * - * Description: - * Just a wrapper to cause_sw_intr - * - * Input Parameters: - * core_id - ID of the CPU core, not used. - * intr_no - Number of the software interrupt - * - * Returned Value: - * Always return OK. - * - ****************************************************************************/ - -static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no) -{ - cause_sw_intr((void *)intr_no); - return ESP_OK; -} - -/**************************************************************************** - * Name: malloc_internal_wrapper - * - * Description: - * Malloc buffer in DRAM - * - * Input Parameters: - * szie - buffer size - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void *malloc_internal_wrapper(size_t size) -{ - void * p = NULL; - - p = kmm_malloc(size); - DEBUGASSERT(p); - - return p; -} - -/**************************************************************************** - * Name: read_mac_wrapper - * - * Description: - * Get Mac Address - * - * Input Parameters: - * mac - mac address - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int IRAM_ATTR read_mac_wrapper(uint8_t mac[6]) -{ - return esp_read_mac(mac, ESP_MAC_BT); -} - -/**************************************************************************** - * Name: srand_wrapper - * - * Description: - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void IRAM_ATTR srand_wrapper(unsigned int seed) -{ - /* empty function */ -} - -/**************************************************************************** - * Name: rand_wrapper - * - * Description: - * Get random value. - * - * Input Parameters: - * None - * - * Returned Value: - * Random value - * - ****************************************************************************/ - -static int IRAM_ATTR rand_wrapper(void) -{ - return getreg32(WDEV_RND_REG); -} - -/**************************************************************************** - * Name: btdm_lpcycles_2_us - * - * Description: - * Converts a number of low power clock cycles into a duration in us - * - * Input Parameters: - * cycles - number of CPU cycles - * - * Returned Value: - * us - value equivalent to the CPU cycles - * - ****************************************************************************/ - -static uint32_t IRAM_ATTR btdm_lpcycles_2_us(uint32_t cycles) -{ - uint64_t us = (uint64_t)g_btdm_lpcycle_us * cycles; - us = (us + (1 << (g_btdm_lpcycle_us_frac - 1))) >> g_btdm_lpcycle_us_frac; - return (uint32_t)us; -} - -/**************************************************************************** - * Name: btdm_us_2_lpcycles - * - * Description: - * Converts a duration in half us into a number of low power clock cycles. - * - * Input Parameters: - * us - * - * Returned Value: - * cycles - * - ****************************************************************************/ - -static uint32_t IRAM_ATTR btdm_us_2_lpcycles(uint32_t us) -{ - uint64_t cycles; - cycles = ((uint64_t)(us) << g_btdm_lpcycle_us_frac) / g_btdm_lpcycle_us; - return (uint32_t)cycles; -} - #ifdef CONFIG_PM + /**************************************************************************** - * Name: btdm_sleep_exit_phase0 + * Name: btdm_slp_tmr_customer_callback * * Description: - * Acquire PM lock and stop esp timer. + * Callback function for ESP BLE sleep timer. This function is dispatched + * to the controller from `btdm_slp_tmr_callback`. If the power management + * lock has been released, it acquires the lock again. * * Input Parameters: - * param - wakeup event + * arg - Unused * * Returned Value: * None * ****************************************************************************/ -static void IRAM_ATTR btdm_sleep_exit_phase0(void *param) +static void btdm_slp_tmr_customer_callback(void * arg) { - int event = (int)param; + (void)(arg); - DEBUGASSERT(g_lp_cntl.enable == true); - - if (g_lp_stat.pm_lock_released) + if (g_pm_lock_acquired == false) { + g_pm_lock_acquired = true; esp32_pm_lockacquire(); - g_lp_stat.pm_lock_released = false; - } - - if (event == BTDM_ASYNC_WAKEUP_SRC_VHCI || - event == BTDM_ASYNC_WAKEUP_SRC_DISA) - { - btdm_wakeup_request(); - } - - if (g_lp_cntl.wakeup_timer_required && g_lp_stat.wakeup_timer_started) - { - esp_timer_stop(g_btdm_slp_tmr); - g_lp_stat.wakeup_timer_started = false; - } - - if (event == BTDM_ASYNC_WAKEUP_SRC_VHCI || - event == BTDM_ASYNC_WAKEUP_SRC_DISA) - { - semphr_give_wrapper(g_wakeup_req_sem); } } @@ -1971,171 +2554,79 @@ static void IRAM_ATTR btdm_sleep_exit_phase0(void *param) static void IRAM_ATTR btdm_slp_tmr_callback(void *arg) { - btdm_vnd_offload_post(BTDM_VND_OL_SIG_WAKEUP_TMR, - (void *)BTDM_ASYNC_WAKEUP_SRC_TMR); -} - -/**************************************************************************** - * Name: btdm_sleep_check_duration - * - * Description: - * Wake up in advance considering the delay in enabling PHY/RF. - * - * Input Parameters: - * half_slot_cnt - half slots to allow to fall into modem sleep - * - * Returned Value: - * None - * - ****************************************************************************/ - -static bool IRAM_ATTR btdm_sleep_check_duration(int32_t *half_slot_cnt) -{ - if (*half_slot_cnt < BTDM_MIN_SLEEP_DURATION) - { - return false; - } - - *half_slot_cnt -= BTDM_MODEM_WAKE_UP_DELAY; - return true; -} - -/**************************************************************************** - * Name: btdm_sleep_enter_phase1_wrapper - * - * Description: - * ESP32 BLE lightsleep callback function. - * - * Input Parameters: - * lpcycles - light sleep cycles - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles) -{ - uint32_t us_to_sleep; - uint32_t uncertainty; - - if (g_lp_cntl.wakeup_timer_required == false) - { - return; - } - - /* start a timer to wake up and acquire the pm_lock before sleep awakes */ - - us_to_sleep = btdm_lpcycles_2_us(lpcycles, NULL); - - DEBUGASSERT(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US); - uncertainty = (us_to_sleep >> 11); - - if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US) - { - uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US; - } - - DEBUGASSERT(g_lp_stat.wakeup_timer_started == false); - - if (esp_timer_start_once(g_btdm_slp_tmr, - us_to_sleep - uncertainty) == ESP_OK) - { - g_lp_stat.wakeup_timer_started = true; - } - else - { - wlerr("timer start failed"); - DEBUGPANIC(); - } -} - -/**************************************************************************** - * Name: btdm_sleep_enter_phase2_wrapper - * - * Description: - * ESP32 BLE lightsleep callback function. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void btdm_sleep_enter_phase2_wrapper(void) -{ - if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) - { - if (g_lp_stat.phy_enabled) - { - bt_phy_disable(); - g_lp_stat.phy_enabled = false; - } - else - { - DEBUGPANIC(); - } - - if (g_lp_stat.pm_lock_released == false) - { - esp32_pm_lockrelease(); - g_lp_stat.pm_lock_released = true; - } - } -} - -/**************************************************************************** - * Name: btdm_sleep_exit_phase3_wrapper - * - * Description: - * ESP32 BLE lightsleep callback function.. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void btdm_sleep_exit_phase3_wrapper(void) -{ - if (g_lp_stat.pm_lock_released) - { - esp32_pm_lockacquire(); - g_lp_stat.pm_lock_released = false; - } - - if (btdm_sleep_clock_sync()) - { - wlerr("sleep eco state err\n"); - DEBUGPANIC(); - } - - if (btdm_controller_get_sleep_mode() == ESP_BT_SLEEP_MODE_1) - { - if (g_lp_stat.phy_enabled == false) - { - bt_phy_enable(); - g_lp_stat.phy_enabled = true; - } - } - - if (g_lp_cntl.wakeup_timer_required && g_lp_stat.wakeup_timer_started) - { - esp_timer_stop(g_btdm_slp_tmr); - g_lp_stat.wakeup_timer_started = false; - } + (void)(arg); + btdm_dispatch_work_to_controller(btdm_slp_tmr_customer_callback, + NULL, + true); } #endif +/**************************************************************************** + * Name: esp_int_adpt_cb + * + * Description: + * BT interrupt adapter callback function + * + * Input Parameters: + * irq - Number of the IRQ that generated the interrupt + * context - Interrupt register state save info (not used) + * arg - Argument passed to the interrupt callback + * + * Returned Value: + * OK + * + ****************************************************************************/ + +static int IRAM_ATTR esp_int_adpt_cb(int irq, void *context, void *arg) +{ + struct irq_adpt_s *adapter = (struct irq_adpt_s *)arg; + + adapter->func(adapter->arg); + + return OK; +} + +/**************************************************************************** + * Name: btdm_wakeup_request_callback + * + * Description: + * Callback function for ESP BLE wakeup request. This function is + * dispatched when a wakeup request is received. If the power management + * lock has not been acquired, it acquires the lock. It also stops the + * sleep timer and gives the wakeup request semaphore. + * + * Input Parameters: + * arg - Unused + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void btdm_wakeup_request_callback(void * arg) +{ + (void)(arg); + +#if CONFIG_PM + if (g_pm_lock_acquired == false) + { + g_pm_lock_acquired = true; + esp32_pm_lockacquire(); + } + + esp_timer_stop(g_btdm_slp_tmr); +#endif + btdm_wakeup_request(); + + semphr_give_wrapper(g_wakeup_req_sem); +} + /**************************************************************************** * Name: btdm_controller_mem_init * * Description: * Initialize BT controller to allocate task and other resource. + * * Input Parameters: * None * @@ -2158,272 +2649,23 @@ static void btdm_controller_mem_init(void) /* initial em, .bss section */ - btdm_dram_regions = sizeof(btdm_dram_available_region) + btdm_dram_regions = sizeof(g_btdm_dram_available_region) / sizeof(btdm_dram_available_region_t); for (int i = 1; i < btdm_dram_regions; i++) { - if (btdm_dram_available_region[i].mode != ESP_BT_MODE_IDLE) + if (g_btdm_dram_available_region[i].mode != ESP_BT_MODE_IDLE) { - memset((void *)btdm_dram_available_region[i].start, 0x0, - btdm_dram_available_region[i].end - \ - btdm_dram_available_region[i].start); + memset((void *)g_btdm_dram_available_region[i].start, 0x0, + g_btdm_dram_available_region[i].end - \ + g_btdm_dram_available_region[i].start); wlinfo(".bss initialise [0x%08x] - [0x%08x]\n", - btdm_dram_available_region[i].start, - btdm_dram_available_region[i].end); + g_btdm_dram_available_region[i].start, + g_btdm_dram_available_region[i].end); } } } -/**************************************************************************** - * Name: phy_printf_ble - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * 0 - * - ****************************************************************************/ - -int phy_printf_ble(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - -int coexist_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - -/**************************************************************************** - * Name: bt_phy_disable - * - * Description: - * Disable BT phy. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void bt_phy_disable(void) -{ - esp32_phy_disable(); -} - -/**************************************************************************** - * Name: bt_phy_enable - * - * Description: - * Enable BT phy. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void bt_phy_enable(void) -{ - esp32_phy_enable(); -} - -/**************************************************************************** - * Name: queue_create_wrapper - * - * Description: - * Create message queue - * - * Input Parameters: - * queue_len - queue message number - * item_size - message size - * - * Returned Value: - * Message queue data pointer - * - ****************************************************************************/ - -static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size) -{ - struct mq_attr attr; - struct mq_adpt_s *mq_adpt; - int ret; - - mq_adpt = kmm_malloc(sizeof(struct mq_adpt_s)); - DEBUGASSERT(mq_adpt); - - 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 %d\n", ret); - kmm_free(mq_adpt); - return NULL; - } - - mq_adpt->msgsize = item_size; - -#ifdef CONFIG_ESP32_SPIFLASH - if (queue_len <= BLE_TASK_EVENT_QUEUE_LEN && - item_size == BLE_TASK_EVENT_QUEUE_ITEM_SIZE) - { - esp_init_queuecache(g_esp_queuecache, - &mq_adpt->mq, - g_esp_queuecache_buffer, - BLE_TASK_EVENT_QUEUE_LEN, - BLE_TASK_EVENT_QUEUE_ITEM_SIZE); - } - else - { - wlerr("Failed to create queue cache." - " Please incresase BLE_TASK_EVENT_QUEUE_LEN to, at least, %d", - queue_len); - return NULL; - } -#endif - - return (void *)mq_adpt; -} - -/**************************************************************************** - * Name: queue_send_wrapper - * - * Description: - * Generic send message to queue within a certain period of time - * - * Input Parameters: - * queue - Message queue data pointer - * item - Item to be sent. - * block_time_ms - Wait time - * - * Returned Value:uint32_t - * True if success or false if fail - * - ****************************************************************************/ - -static int queue_send_wrapper(void *queue, void *item, - uint32_t block_time_ms) -{ - return esp_queue_send_generic(queue, item, block_time_ms, 0); -} - -/**************************************************************************** - * 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 - * block_time_ms - Wait time - * - * Returned Value: - * True if success or false if fail - * - ****************************************************************************/ - -static int queue_recv_wrapper(void *queue, void *item, - uint32_t block_time_ms) -{ - ssize_t ret; - struct timespec timeout; - unsigned int prio; - struct mq_adpt_s *mq_adpt = (struct mq_adpt_s *)queue; - - if (block_time_ms == 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 %d\n", ret); - return false; - } - - if (block_time_ms) - { - esp_update_time(&timeout, MSEC2TICK(block_time_ms)); - } - - 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: queue_delete_wrapper - * - * Description: - * Delete message queue - * - * Input Parameters: - * queue - Message queue data pointer - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void queue_delete_wrapper(void *queue) -{ - struct mq_adpt_s *mq_adpt = (struct mq_adpt_s *)queue; - - file_mq_close(&mq_adpt->mq); - file_mq_unlink(mq_adpt->name); - kmm_free(mq_adpt); -} - /**************************************************************************** * Name: btdm_config_mask_load * @@ -2440,372 +2682,77 @@ static void queue_delete_wrapper(void *queue) static uint32_t btdm_config_mask_load(void) { - uint32_t mask = 0x0; + uint32_t mask = 0x0; #ifdef CONFIG_UART_BTH4 - mask |= BTDM_CFG_HCI_UART; + mask |= BTDM_CFG_HCI_UART; #endif -#ifdef CONFIG_ESP32_BLE_RUN_APP_CPU - mask |= BTDM_CFG_CONTROLLER_RUN_APP_CPU; +#ifdef CONFIG_BTDM_CTRL_PINNED_TO_CORE_1 + mask |= BTDM_CFG_CONTROLLER_RUN_APP_CPU; #endif -#ifdef CONFIG_ESP32_BLE_FULL_SCAN - mask |= BTDM_CFG_BLE_FULL_SCAN_SUPPORTED; +#ifdef CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED + mask |= BTDM_CFG_BLE_FULL_SCAN_SUPPORTED; #endif - mask |= BTDM_CFG_SCAN_DUPLICATE_OPTIONS; + mask |= BTDM_CFG_SCAN_DUPLICATE_OPTIONS; - mask |= BTDM_CFG_SEND_ADV_RESERVED_SIZE; + mask |= BTDM_CFG_SEND_ADV_RESERVED_SIZE; - return mask; + return mask; } /**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: esp32_bt_controller_init + * Name: bt_controller_deinit_internal * * Description: - * Init BT controller. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -int esp32_bt_controller_init(void) -{ - esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); - esp_bt_controller_config_t *cfg = &bt_cfg; - int err; - uint32_t btdm_cfg_mask = 0; - int i; - - sq_init(&g_int_flags_free); - sq_init(&g_int_flags_used); - - for (i = 0; i < NR_IRQSTATE_FLAGS; i++) - { - sq_addlast((sq_entry_t *)&g_int_flags[i], &g_int_flags_free); - } - - if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) - { - wlerr("Invalid controller status"); - return ERROR; - } - -#ifdef CONFIG_ESP32_SPIFLASH - if (esp_wireless_init() != OK) - { - return -EIO; - } -#endif - - if (btdm_osi_funcs_register(&g_osi_funcs) != 0) - { - wlerr("Error, probably invalid OSI Functions\n"); - return -EINVAL; - } - - wlinfo("BT controller compile version [%s]\n", - btdm_controller_get_compile_version()); - - /* If all the bt available memory was already released, - * cannot initialize bluetooth controller - */ - - if (btdm_dram_available_region[0].mode == ESP_BT_MODE_IDLE) - { - wlerr("Error, bt available memory was released\n"); - return -EIO; - } - - if (cfg == NULL) - { - wlerr("%s %d\n", __func__, __LINE__); - return -EINVAL; - } - - cfg->controller_task_stack_size = CONFIG_ESP32_BLE_TASK_STACK_SIZE; - cfg->controller_task_prio = CONFIG_ESP32_BLE_TASK_PRIORITY; - - cfg->bt_max_sync_conn = CONFIG_ESP32_BLE_MAX_CONN; - cfg->magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL; - - if (((cfg->mode & ESP_BT_MODE_BLE) && (cfg->ble_max_conn <= 0 || - cfg->ble_max_conn > BTDM_CONTROLLER_BLE_MAX_CONN_LIMIT)) || - ((cfg->mode & ESP_BT_MODE_CLASSIC_BT) && (cfg->bt_max_acl_conn <= 0 || - cfg->bt_max_acl_conn > BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_LIMIT)) || - ((cfg->mode & ESP_BT_MODE_CLASSIC_BT) && - (cfg->bt_max_sync_conn > BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_LIMIT))) - { - wlerr("%s %d\n", __func__, __LINE__); - return -EINVAL; - } - - wlinfo("BT controller compile version [%s]", - btdm_controller_get_compile_version()); - - btdm_controller_mem_init(); - - wlinfo("Memory initialized!\n"); - - bt_periph_module_enable(PERIPH_BT_MODULE); - - /* set default sleep clock cycle and its fractional bits */ - - g_btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT; - g_btdm_lpcycle_us = 2 << (g_btdm_lpcycle_us_frac); - - btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_NONE); - - btdm_cfg_mask = btdm_config_mask_load(); - - wlinfo("Going to call btdm_controller_init\n"); - - if (btdm_controller_init(btdm_cfg_mask, cfg) != 0) - { - wlerr("Failed to initialize the BLE Controller\n"); - err = -ENOMEM; - goto error; - } - - wlinfo("The btdm_controller_init was initialized\n"); - -#ifdef CONFIG_BTDM_COEX_BLE_ADV_HIGH_PRIORITY - coex_ble_adv_priority_high_set(true); -#else - coex_ble_adv_priority_high_set(false); -#endif - - btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; - - return OK; - -error: - return err; -} - -/**************************************************************************** - * Name: esp32_bt_controller_deinit - * - * Description: - * Deinit BT controller. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -int esp32_bt_controller_deinit(void) -{ - if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) - { - return ERROR; - } - - btdm_controller_deinit(); - - if (g_lp_stat.phy_enabled) - { - bt_phy_disable(); - g_lp_stat.phy_enabled = false; - } - else - { - DEBUGPANIC(); - } - -#ifdef CONFIG_PM - /* deinit low power control resources */ - - g_lp_stat.pm_lock_released = false; - - if (g_lp_cntl.wakeup_timer_required) - { - if (g_lp_stat.wakeup_timer_started) - { - esp_timer_stop(g_btdm_slp_tmr); - } - - g_lp_stat.wakeup_timer_started = false; - esp_timer_delete(g_btdm_slp_tmr); - g_btdm_slp_tmr = NULL; - } - - if (g_lp_cntl.enable) - { - btdm_vnd_offload_task_deregister(BTDM_VND_OL_SIG_WAKEUP_TMR); - semphr_delete_wrapper(g_wakeup_req_sem); - g_wakeup_req_sem = NULL; - } -#endif - - btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; - g_btdm_lpcycle_us = 0; - return OK; -} - -/**************************************************************************** - * Name: esp32_bt_controller_disable - * - * Description: - * Disable BT controller. - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -int esp32_bt_controller_disable(void) -{ - if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) - { - return ERROR; - } - - while (!btdm_power_state_active()) - { - nxsig_usleep(1000); /* wait */ - } - - btdm_controller_disable(); - -#ifdef CONFIG_ESP32_WIFI_BT_COEXIST - coex_disable(); -#endif - - btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; - -#ifdef CONFIG_PM - /* disable low power mode */ - - if (g_lp_stat.pm_lock_released == false) - { - esp32_pm_lockrelease(); - g_lp_stat.pm_lock_released = true; - } - else - { - DEBUGPANIC(); - } -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp32_bt_controller_enable - * - * Description: - * Enable BT controller. - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -int esp32_bt_controller_enable(esp_bt_mode_t mode) -{ - int ret = OK; - - if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) - { - return ERROR; - } - - if (mode != btdm_controller_get_mode()) - { - wlerr("invalid mode %d, controller support mode is %d", - mode, btdm_controller_get_mode()); - return ERROR; - } - - bt_phy_enable(); - - btdm_rf_bb_init_phase2(); - - coex_bt_high_prio(); - -#ifdef CONFIG_ESP32_WIFI_BT_COEXIST - coex_enable(); -#endif - -#ifdef CONFIG_PM - /* enable low power mode */ - - esp32_pm_lockacquire(); - g_lp_stat.pm_lock_released = false; - - if (g_lp_cntl.enable) - { - btdm_controller_enable_sleep(true); - } -#endif - - if (g_lp_cntl.enable) - { - btdm_controller_enable_sleep(true); - } - - if (btdm_controller_enable(mode) != 0) - { - ret = ERROR; - goto error; - } - - btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED; - - return ret; - -error: - - /* disable low power mode */ - - btdm_controller_enable_sleep(false); - -#ifdef CONFIG_PM - if (g_lp_stat.pm_lock_released == false) - { - esp32_pm_lockrelease(); - g_lp_stat.pm_lock_released = true; - } -#endif - - return ret; -} - -/**************************************************************************** - * Name: esp32_bt_controller_get_status - * - * Description: - * Returns the status of the BT Controller + * Deinitialize the internal structures of the Bluetooth controller. * * Input Parameters: * None * * Returned Value: - * The current status (type esp_bt_controller_status_t) + * None * ****************************************************************************/ -esp_bt_controller_status_t esp32_bt_controller_get_status(void) +static void bt_controller_deinit_internal(void) { - return btdm_controller_status; + periph_module_disable(PERIPH_BT_MODULE); + +#ifdef CONFIG_PM + + if (g_btdm_slp_tmr != NULL) + { + esp_timer_stop(g_btdm_slp_tmr); + esp_timer_delete(g_btdm_slp_tmr); + g_btdm_slp_tmr = NULL; + } + + g_pm_lock_acquired = false; +#endif + + if (g_wakeup_req_sem) + { + semphr_delete_wrapper(g_wakeup_req_sem); + g_wakeup_req_sem = NULL; + } + + if (g_osi_funcs_p) + { + free(g_osi_funcs_p); + g_osi_funcs_p = NULL; + } + + g_btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; + + g_btdm_lpcycle_us = 0; + btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_NONE); + + esp_bt_power_domain_off(); + + esp_phy_modem_deinit(); } /**************************************************************************** @@ -2830,21 +2777,41 @@ static bool async_wakeup_request(int event) switch (event) { case BTDM_ASYNC_WAKEUP_REQ_HCI: - request_lock = true; + btdm_in_wakeup_requesting_set(true); + + /* No break */ + + case BTDM_ASYNC_WAKEUP_REQ_CTRL_DISA: + if (!btdm_power_state_active()) + { + do_wakeup_request = true; + + btdm_dispatch_work_to_controller(btdm_wakeup_request_callback, + NULL, + true); + semphr_take_wrapper(g_wakeup_req_sem, OSI_FUNCS_TIME_BLOCKING); + } break; case BTDM_ASYNC_WAKEUP_REQ_COEX: - request_lock = false; + if (!btdm_power_state_active()) + { + do_wakeup_request = true; +#if CONFIG_PM + if (g_pm_lock_acquired == false) + { + g_pm_lock_acquired = true; + esp32_pm_lockacquire(); + } + + esp_timer_stop(g_btdm_slp_tmr); +#endif + btdm_wakeup_request(); + } break; default: return false; } - if (!btdm_power_state_active()) - { - do_wakeup_request = true; - btdm_wakeup_request(request_lock); - } - return do_wakeup_request; } @@ -2872,6 +2839,7 @@ static void async_wakeup_request_end(int event) request_lock = true; break; case BTDM_ASYNC_WAKEUP_REQ_COEX: + case BTDM_ASYNC_WAKEUP_REQ_CTRL_DISA: request_lock = false; break; default: @@ -2880,46 +2848,422 @@ static void async_wakeup_request_end(int event) if (request_lock) { - btdm_wakeup_request_end(); + btdm_in_wakeup_requesting_set(false); } } /**************************************************************************** - * Name: coex_bt_wakeup_request + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_bt_controller_init * * Description: - * Request a Wi-Fi/BLE Coex wakeup request + * Init BT controller. * * Input Parameters: - * none + * None * * Returned Value: - * true if request lock is needed, false otherwise + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. * ****************************************************************************/ -static bool coex_bt_wakeup_request(void) +int esp32_bt_controller_init(void) { - return async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_COEX); + esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); + uint32_t btdm_cfg_mask = 0; + int err; + int i; + bool select_src_ret; + bool set_div_ret; +#ifdef CONFIG_PM + esp_timer_create_args_t create_args = + { + .callback = btdm_slp_tmr_callback, + .arg = NULL, + .name = "btSlp" + }; +#endif + + /* If all the bt available memory was already released, + * cannot initialize bluetooth controller + */ + + if (g_btdm_dram_available_region[0].mode == ESP_BT_MODE_IDLE) + { + wlerr("Error, bt available memory was released\n"); + return -EIO; + } + + /* Initialize list of interrupt flags to enable chained critical sections + * to return sucessfully. + */ + + sq_init(&g_ble_int_flags_free); + sq_init(&g_ble_int_flags_used); + + for (i = 0; i < NR_IRQSTATE_FLAGS; i++) + { + sq_addlast((sq_entry_t *)&g_ble_int_flags[i], &g_ble_int_flags_free); + } + +#ifdef CONFIG_ESP32_SPIFLASH + + /* Initialize interfaces that enable BLE ISRs to run during a + * SPI flash operation. + */ + + if (esp_wireless_init() != OK) + { + return -EIO; + } +#endif + + g_osi_funcs_p = + (struct osi_funcs_s *)kmm_malloc(sizeof(struct osi_funcs_s)); + + if (g_osi_funcs_p == NULL) + { + return -ENOMEM; + } + + memcpy(g_osi_funcs_p, &g_osi_funcs_ro, sizeof(struct osi_funcs_s)); + if (btdm_osi_funcs_register(g_osi_funcs_p) != 0) + { + wlerr("Invalid OSI Functions\n"); + return -EINVAL; + } + + if (g_btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) + { + return ESP_ERR_INVALID_STATE; + } + + /* overwrite some parameters */ + + cfg.controller_task_stack_size = CONFIG_ESP32_BLE_TASK_STACK_SIZE; + cfg.controller_task_prio = CONFIG_ESP32_BLE_TASK_PRIORITY; + cfg.bt_max_sync_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF; + cfg.magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL; + + if (((cfg.mode & ESP_BT_MODE_BLE) && (cfg.ble_max_conn <= 0 || + cfg.ble_max_conn > BTDM_CONTROLLER_BLE_MAX_CONN_LIMIT)) || + ((cfg.mode & ESP_BT_MODE_CLASSIC_BT) && (cfg.bt_max_acl_conn <= 0 || + cfg.bt_max_acl_conn > BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_LIMIT)) || + ((cfg.mode & ESP_BT_MODE_CLASSIC_BT) && + (cfg.bt_max_sync_conn > BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_LIMIT))) + { + wlerr("%s %d\n", __func__, __LINE__); + return -EINVAL; + } + + wlinfo("BT controller compile version [%s]", + btdm_controller_get_compile_version()); + + g_wakeup_req_sem = semphr_create_wrapper(1, 0); + if (g_wakeup_req_sem == NULL) + { + err = -ENOMEM; + goto error; + } + + esp_phy_modem_init(); + + esp_bt_power_domain_on(); + + btdm_controller_mem_init(); + + periph_module_enable(PERIPH_BT_MODULE); + +#ifdef CONFIG_PM + g_btdm_allow_light_sleep = false; +#endif + + /* set default sleep clock cycle and its fractional bits */ + + g_btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT; + g_btdm_lpcycle_us = 2 << (g_btdm_lpcycle_us_frac); + +#ifdef CONFIG_BTDM_CTRL_MODEM_SLEEP_MODE_ORIG + g_btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; /* set default value */ +#ifdef CONFIG_BTDM_CTRL_LPCLK_SEL_EXT_32K_XTAL + + /* check whether or not EXT_CRYS is working */ + + if (rtc_clk_slow_src_get() == SOC_RTC_SLOW_CLK_SRC_XTAL32K) + { + g_btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL32K; /* External 32kHz XTAL */ +#ifdef CONFIG_PM + g_btdm_allow_light_sleep = true; +#endif + } + else + { + wlwarn("32.768kHz XTAL not detected, fall back to main XTAL as " + "Bluetooth sleep clock\n" + "light sleep mode will not be able to apply when bluetooth " + "is enabled"); + g_btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; /* set default value */ + } +#else + g_btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; /* set default value */ +#endif + + if (g_btdm_lpclk_sel == BTDM_LPCLK_SEL_XTAL) + { + select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL); + set_div_ret = btdm_lpclk_set_div(esp_clk_xtal_freq() * 2 / MHZ - 1); + assert(select_src_ret && set_div_ret); + g_btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT; + g_btdm_lpcycle_us = 2 << (g_btdm_lpcycle_us_frac); + } + else + { + /* g_btdm_lpclk_sel == BTDM_LPCLK_SEL_XTAL32K */ + + select_src_ret = btdm_lpclk_select_src(BTDM_LPCLK_SEL_XTAL32K); + set_div_ret = btdm_lpclk_set_div(0); + assert(select_src_ret && set_div_ret); + g_btdm_lpcycle_us_frac = RTC_CLK_CAL_FRACT; + g_btdm_lpcycle_us = (RTC_CLK_CAL_FRACT > 15) ? + (1000000 << (RTC_CLK_CAL_FRACT - 15)) : + (1000000 >> (15 - RTC_CLK_CAL_FRACT)); + assert(g_btdm_lpcycle_us != 0); + } + + btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_ORIG); + +#elif CONFIG_BTDM_CTRL_MODEM_SLEEP_MODE_EVED + btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_EVED); + UNUSED(select_src_ret); + UNUSED(set_div_ret); +#else + btdm_controller_set_sleep_mode(BTDM_MODEM_SLEEP_MODE_NONE); + UNUSED(select_src_ret); + UNUSED(set_div_ret); +#endif + +#ifdef CONFIG_PM + if ((err = esp_timer_create(&create_args, &g_btdm_slp_tmr) != OK)) + { + wlerr("Failed to create timer"); + goto error; + } + + g_pm_lock_acquired = true; +#endif + +#if CONFIG_ESP32_WIFI_BT_COEXIST + coex_init(); +#endif + + btdm_cfg_mask = btdm_config_mask_load(); + + wlinfo("Going to call btdm_controller_init\n"); + + if (btdm_controller_init(btdm_cfg_mask, &cfg) != 0) + { + wlerr("Failed to initialize the BLE Controller\n"); + err = -ENOMEM; + goto error; + } + + wlinfo("The btdm_controller_init was initialized\n"); + + g_btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; + + return OK; + +error: + + bt_controller_deinit_internal(); + + return err; } /**************************************************************************** - * Name: coex_bt_wakeup_request_end + * Name: esp32_bt_controller_deinit * * Description: - * Finish Wi-Fi/BLE Coex wakeup request + * Deinit BT controller. * * Input Parameters: - * none + * None * * Returned Value: - * none + * Zero (OK) is returned on success. Otherwise, -1 (ERROR) is returned. * ****************************************************************************/ -static void coex_bt_wakeup_request_end(void) +int esp32_bt_controller_deinit(void) { - async_wakeup_request_end(BTDM_ASYNC_WAKEUP_REQ_COEX); + if (g_btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) + { + return ERROR; + } + + btdm_controller_deinit(); + + bt_controller_deinit_internal(); + + return OK; +} + +/**************************************************************************** + * Name: esp32_bt_controller_enable + * + * Description: + * Enable BT controller. + * + * Input Parameters: + * mode - the mode(BLE/BT/BTDM) to enable. For compatible of API, retain + * this argument. This mode must be equal as the mode in "cfg" of + * esp_bt_controller_init(). + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. + * + ****************************************************************************/ + +int esp32_bt_controller_enable(esp_bt_mode_t mode) +{ + int ret = OK; + + if (g_btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) + { + return ERROR; + } + + if (mode != btdm_controller_get_mode()) + { + wlerr("invalid mode %d, controller support mode is %d", + mode, btdm_controller_get_mode()); + return ERROR; + } + +#ifdef CONFIG_PM + if (g_btdm_allow_light_sleep == false) + { + esp32_pm_lockacquire(); + } + + esp32_pm_lockacquire(); +#endif + + esp_phy_enable(); + +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + coex_enable(); +#endif + + if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) + { + btdm_controller_enable_sleep(true); + } + + /* inititalize bluetooth baseband */ + + btdm_check_and_init_bb(); + + ret = btdm_controller_enable(mode); + if (ret != 0) + { +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + coex_disable(); +#endif + esp_phy_disable(); +#ifdef CONFIG_PM + if (g_btdm_allow_light_sleep == false) + { + esp32_pm_lockrelease(); + } + + esp32_pm_lockrelease(); +#endif + return ERROR; + } + + g_btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED; + + return OK; +} + +/**************************************************************************** + * Name: esp32_bt_controller_disable + * + * Description: + * Disable BT controller. + * + * Input Parameters: + * None + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. + * + ****************************************************************************/ + +int esp32_bt_controller_disable(void) +{ + if (g_btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) + { + return ERROR; + } + + /* disable modem sleep and wake up from sleep mode */ + + if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) + { + btdm_controller_enable_sleep(false); + async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_CTRL_DISA); + while (btdm_power_state_active() == false) + { + nxsig_usleep(1000); + } + } + + btdm_controller_disable(); + +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + coex_disable(); +#endif + + esp_phy_disable(); + g_btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; + +#ifdef CONFIG_PM + if (g_btdm_allow_light_sleep == false) + { + esp32_pm_lockrelease(); + } + + esp32_pm_lockrelease(); +#endif + + return OK; +} + +/**************************************************************************** + * Name: esp32_bt_controller_get_status + * + * Description: + * Returns the status of the BT Controller + * + * Input Parameters: + * None + * + * Returned Value: + * The current status (type esp_bt_controller_status_t) + * + ****************************************************************************/ + +esp_bt_controller_status_t esp32_bt_controller_get_status(void) +{ + return g_btdm_controller_status; } /**************************************************************************** @@ -2938,11 +3282,6 @@ static void coex_bt_wakeup_request_end(void) bool esp32_vhci_host_check_send_available(void) { - if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) - { - return false; - } - return api_vhci_host_check_send_available(); } @@ -2950,10 +3289,11 @@ bool esp32_vhci_host_check_send_available(void) * Name: esp32_vhci_host_send_packet * * Description: - * Host send packet to controller. + * Host send packet to controller. + * * Input Parameters: - * data - the packet pointer - * len - the packet length + * data - the packet pointer + * len - the packet length * * Returned Value: * None @@ -2962,18 +3302,11 @@ bool esp32_vhci_host_check_send_available(void) void esp32_vhci_host_send_packet(uint8_t *data, uint16_t len) { - wlinfo("len: %d\n", len); - for (uint16_t i = 0; i < len; i++) - { - wlinfo("%02x\n", data[i]); - } - - if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) - { - return; - } + async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_HCI); api_vhci_host_send_packet(data, len); + + async_wakeup_request_end(BTDM_ASYNC_WAKEUP_REQ_HCI); } /**************************************************************************** @@ -2993,48 +3326,8 @@ void esp32_vhci_host_send_packet(uint8_t *data, uint16_t len) int esp32_vhci_register_callback(const esp_vhci_host_callback_t *callback) { int ret = ERROR; - if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) - { - return ret; - } ret = api_vhci_host_register_callback( (const vhci_host_callback_t *)callback) == 0 ? 0 : -1; return ret; } - -int IRAM_ATTR coex_bt_request_wrapper(uint32_t event, - uint32_t latency, - uint32_t duration) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_bt_request(event, latency, duration); -#else - return 0; -#endif -} - -int IRAM_ATTR coex_bt_release_wrapper(uint32_t event) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_bt_release(event); -#else - return 0; -#endif -} - -uint32_t IRAM_ATTR coex_bb_reset_lock_wrapper(void) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_bb_reset_lock(); -#else - return 0; -#endif -} - -void IRAM_ATTR coex_bb_reset_unlock_wrapper(uint32_t restore) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - coex_bb_reset_unlock(restore); -#endif -} diff --git a/arch/xtensa/src/esp32/esp32_ble_adapter.h b/arch/xtensa/src/esp32/esp32_ble_adapter.h index 447b1113037..522e75f4635 100644 --- a/arch/xtensa/src/esp32/esp32_ble_adapter.h +++ b/arch/xtensa/src/esp32/esp32_ble_adapter.h @@ -31,45 +31,6 @@ #include "esp_bt.h" -/* Bluetooth system and controller config */ - -#define BTDM_CFG_BT_DATA_RELEASE (1 << 0) -#define BTDM_CFG_HCI_UART (1 << 1) -#define BTDM_CFG_CONTROLLER_RUN_APP_CPU (1 << 2) -#define BTDM_CFG_SCAN_DUPLICATE_OPTIONS (1 << 3) -#define BTDM_CFG_SEND_ADV_RESERVED_SIZE (1 << 4) -#define BTDM_CFG_BLE_FULL_SCAN_SUPPORTED (1 << 5) - -/* Bluetooth memory regions */ - -#define SOC_MEM_BT_DATA_START 0x3ffae6e0 -#define SOC_MEM_BT_DATA_END 0x3ffaff10 -#define SOC_MEM_BT_EM_START 0x3ffb0000 -#define SOC_MEM_BT_EM_END 0x3ffb7cd8 -#define SOC_MEM_BT_EM_BTDM0_START 0x3ffb0000 -#define SOC_MEM_BT_EM_BTDM0_END 0x3ffb09a8 -#define SOC_MEM_BT_EM_BLE_START 0x3ffb09a8 -#define SOC_MEM_BT_EM_BLE_END 0x3ffb1ddc -#define SOC_MEM_BT_EM_BTDM1_START 0x3ffb1ddc -#define SOC_MEM_BT_EM_BTDM1_END 0x3ffb2730 -#define SOC_MEM_BT_EM_BREDR_START 0x3ffb2730 -#define SOC_MEM_BT_EM_BREDR_NO_SYNC_END 0x3ffb6388 /* Not calculate with synchronize connection support */ -#define SOC_MEM_BT_EM_BREDR_END 0x3ffb7cd8 /* Calculate with synchronize connection support */ -#define SOC_MEM_BT_EM_SYNC0_START 0x3ffb6388 -#define SOC_MEM_BT_EM_SYNC0_END 0x3ffb6bf8 -#define SOC_MEM_BT_EM_SYNC1_START 0x3ffb6bf8 -#define SOC_MEM_BT_EM_SYNC1_END 0x3ffb7468 -#define SOC_MEM_BT_EM_SYNC2_START 0x3ffb7468 -#define SOC_MEM_BT_EM_SYNC2_END 0x3ffb7cd8 -#define SOC_MEM_BT_BSS_START 0x3ffb8000 -#define SOC_MEM_BT_BSS_END 0x3ffb9a20 -#define SOC_MEM_BT_MISC_START 0x3ffbdb28 -#define SOC_MEM_BT_MISC_END 0x3ffbdb5c - -#define SOC_MEM_BT_EM_PER_SYNC_SIZE 0x870 - -#define SOC_MEM_BT_EM_BREDR_REAL_END (SOC_MEM_BT_EM_BREDR_NO_SYNC_END + CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF * SOC_MEM_BT_EM_PER_SYNC_SIZE) - /**************************************************************************** * Public Function Prototypes ****************************************************************************/ diff --git a/arch/xtensa/src/esp32/esp32_dac.c b/arch/xtensa/src/esp32/esp32_dac.c index dcbe6450502..5f3b84540ae 100644 --- a/arch/xtensa/src/esp32/esp32_dac.c +++ b/arch/xtensa/src/esp32/esp32_dac.c @@ -31,6 +31,7 @@ #include "esp32_rtc_gpio.h" #include "hardware/esp32_rtc_io.h" #include "hardware/esp32_dport.h" +#include "hardware/esp32_sens.h" /**************************************************************************** * Pre-processor Definitions diff --git a/arch/xtensa/src/esp32/esp32_dac.h b/arch/xtensa/src/esp32/esp32_dac.h index 6c90d4c3da3..52782d756d4 100644 --- a/arch/xtensa/src/esp32/esp32_dac.h +++ b/arch/xtensa/src/esp32/esp32_dac.h @@ -27,8 +27,6 @@ #include #include -#include "hardware/esp32_sens.h" -#include "hardware/esp32_rtc_io.h" /**************************************************************************** * Pre-processor definitions diff --git a/arch/xtensa/src/esp32/esp32_efuse_table.c b/arch/xtensa/src/esp32/esp32_efuse_table.c deleted file mode 100644 index d559459b177..00000000000 --- a/arch/xtensa/src/esp32/esp32_efuse_table.c +++ /dev/null @@ -1,641 +0,0 @@ -/**************************************************************************** - * arch/xtensa/src/esp32/esp32_efuse_table.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 -#include -#include -#include "esp32_efuse.h" - -#define MAX_BLK_LEN 256 - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* The last free bit in the block is counted over the entire file */ - -#define LAST_FREE_BIT_BLK1 MAX_BLK_LEN -#define LAST_FREE_BIT_BLK2 MAX_BLK_LEN -#define LAST_FREE_BIT_BLK3 192 - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -static const efuse_desc_t MAC_FACTORY[] = -{ - { - 72, 8 /* Factory MAC addr [0], */ - }, - { - 64, 8 /* Factory MAC addr [1], */ - }, - { - 56, 8 /* Factory MAC addr [2], */ - }, - { - 48, 8 /* Factory MAC addr [3], */ - }, - { - 40, 8 /* Factory MAC addr [4], */ - }, - { - 32, 8 /* Factory MAC addr [5], */ - }, -}; - -static const efuse_desc_t MAC_FACTORY_CRC[] = -{ - { - 80, 8 /* CRC8 for factory MAC address */ - }, -}; - -static const efuse_desc_t MAC_CUSTOM_CRC[] = -{ - { - 768, 8 /* CRC8 for custom MAC address */ - }, -}; - -static const efuse_desc_t MAC_CUSTOM[] = -{ - { - 776, 48 /* Custom MAC */ - }, -}; - -static const efuse_desc_t MAC_CUSTOM_VER[] = -{ - { - 952, 8 /* Custom MAC version */ - }, -}; - -static const efuse_desc_t SECURE_BOOT_KEY[] = -{ - { - 512, MAX_BLK_LEN /* Security boot key */ - }, -}; - -static const efuse_desc_t ABS_DONE_0[] = -{ - { - 196, 1 /* Secure boot is enabled for bootloader image. - * EFUSE_RD_ABS_DONE_0 - */ - }, -}; - -static const efuse_desc_t ENCRYPT_FLASH_KEY[] = -{ - { - 256, MAX_BLK_LEN /* Flash encrypt key */ - }, -}; - -static const efuse_desc_t ENCRYPT_CONFIG[] = -{ - { - 188, 4 /* Flash encrypt. EFUSE_FLASH_CRYPT_CONFIG_M */ - }, -}; - -static const efuse_desc_t DISABLE_DL_ENCRYPT[] = -{ - { - 199, 1 /* Flash encrypt. Disable UART bootloader - * encryption. EFUSE_DISABLE_DL_ENCRYPT - */ - }, -}; - -static const efuse_desc_t DISABLE_DL_DECRYPT[] = -{ - { - 200, 1 /* Flash encrypt. Disable UART bootloader - * decryption. EFUSE_DISABLE_DL_DECRYPT - */ - }, -}; - -static const efuse_desc_t DISABLE_DL_CACHE[] = -{ - { - 201, 1 /* Flash encrypt. Disable UART bootloader MMU - * cache. EFUSE_DISABLE_DL_CACHE - */ - }, -}; - -static const efuse_desc_t FLASH_CRYPT_CNT[] = -{ - { - 20, 7 /* Flash encrypt. Flash encryption is enabled - * if this field has an odd number of bits set. - * EFUSE_FLASH_CRYPT_CNT - */ - }, -}; - -static const efuse_desc_t DISABLE_JTAG[] = -{ - { - 198, 1 /* Disable JTAG. EFUSE_RD_DISABLE_JTAG */ - }, -}; - -static const efuse_desc_t CONSOLE_DEBUG_DISABLE[] = -{ - { - 194, 1 /* Disable ROM BASIC interpreter fallback. - * EFUSE_RD_CONSOLE_DEBUG_DISABLE - */ - }, -}; - -static const efuse_desc_t UART_DOWNLOAD_DIS[] = -{ - { - 27, 1 /* Disable UART download mode. - * Valid for ESP32 V3 and newer - */ - }, -}; - -static const efuse_desc_t WR_DIS_FLASH_CRYPT_CNT[] = -{ - { - 2, 1 /* Flash encrypt. Write protection - * FLASH_CRYPT_CNT - */ - }, -}; - -static const efuse_desc_t WR_DIS_BLK1[] = -{ - { - 7, 1 /* Flash encrypt. Write protection encryption key. - * EFUSE_WR_DIS_BLK1 - */ - }, -}; - -static const efuse_desc_t WR_DIS_BLK2[] = -{ - { - 8, 1 /* Security boot. Write protection security key. - * EFUSE_WR_DIS_BLK2 - */ - }, -}; - -static const efuse_desc_t WR_DIS_BLK3[] = -{ - { - 9, 1 /* Write protection for EFUSE_BLK3. - * EFUSE_WR_DIS_BLK3 - */ - }, -}; - -static const efuse_desc_t RD_DIS_BLK1[] = -{ - { - 16, 1 /* Flash encrypt. efuse_key_read_protected. - * EFUSE_RD_DIS_BLK1 - */ - }, -}; - -static const efuse_desc_t RD_DIS_BLK2[] = -{ - { - 17, 1 /* Security boot. efuse_key_read_protected. - * EFUSE_RD_DIS_BLK2 - */ - }, -}; - -static const efuse_desc_t RD_DIS_BLK3[] = -{ - { - 18, 1 /* Read protection for EFUSE_BLK3. - * EFUSE_RD_DIS_BLK3 - */ - }, -}; - -static const efuse_desc_t CHIP_VER_DIS_APP_CPU[] = -{ - { - 96, 1 /* EFUSE_RD_CHIP_VER_DIS_APP_CPU */ - }, -}; - -static const efuse_desc_t CHIP_VER_DIS_BT[] = -{ - { - 97, 1 /* EFUSE_RD_CHIP_VER_DIS_BT */ - }, -}; - -static const efuse_desc_t CHIP_VER_PKG[] = -{ - { - 105, 3 /* EFUSE_RD_CHIP_VER_PKG */ - }, -}; - -static const efuse_desc_t CHIP_CPU_FREQ_LOW[] = -{ - { - 108, 1 /* EFUSE_RD_CHIP_CPU_FREQ_LOW */ - }, -}; - -static const efuse_desc_t CHIP_CPU_FREQ_RATED[] = -{ - { - 109, 1 /* EFUSE_RD_CHIP_CPU_FREQ_RATED */ - }, -}; - -static const efuse_desc_t CHIP_VER_REV1[] = -{ - { - 111, 1 /* EFUSE_RD_CHIP_VER_REV1 */ - }, -}; - -static const efuse_desc_t CHIP_VER_REV2[] = -{ - { - 180, 1 /* EFUSE_RD_CHIP_VER_REV2 */ - }, -}; - -static const efuse_desc_t XPD_SDIO_REG[] = -{ - { - 142, 1 /* EFUSE_RD_XPD_SDIO_REG */ - }, -}; - -static const efuse_desc_t SDIO_TIEH[] = -{ - { - 143, 1 /* EFUSE_RD_SDIO_TIEH */ - }, -}; - -static const efuse_desc_t SDIO_FORCE[] = -{ - { - 144, 1 /* EFUSE_RD_XPD_SDIO_FORCE */ - }, -}; - -static const efuse_desc_t ADC_VREF_AND_SDIO_DREF[] = -{ - { - 136, 6 /* EFUSE_RD_ADC_VREF[0..4] or SDIO_DREFH[0 1] */ - }, -}; - -static const efuse_desc_t ADC1_TP_LOW[] = -{ - { - 864, 7 /* TP_REG EFUSE_RD_ADC1_TP_LOW */ - }, -}; - -static const efuse_desc_t ADC2_TP_LOW[] = -{ - { - 880, 7 /* TP_REG EFUSE_RD_ADC2_TP_LOW */ - }, -}; - -static const efuse_desc_t ADC1_TP_HIGH[] = -{ - { - 871, 9 /* TP_REG EFUSE_RD_ADC1_TP_HIGH */ - }, -}; - -static const efuse_desc_t ADC2_TP_HIGH[] = -{ - { - 887, 9 /* TP_REG EFUSE_RD_ADC2_TP_HIGH */ - }, -}; - -static const efuse_desc_t SECURE_VERSION[] = -{ - { - 896, 32 /* Secure version for anti-rollback */ - }, -}; - -/* */ - -const efuse_desc_t *ESP_EFUSE_MAC_FACTORY[] = -{ - &MAC_FACTORY[0], /* Factory MAC addr [0] */ - &MAC_FACTORY[1], /* Factory MAC addr [1] */ - &MAC_FACTORY[2], /* Factory MAC addr [2] */ - &MAC_FACTORY[3], /* Factory MAC addr [3] */ - &MAC_FACTORY[4], /* Factory MAC addr [4] */ - &MAC_FACTORY[5], /* Factory MAC addr [5] */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_MAC_FACTORY_CRC[] = -{ - &MAC_FACTORY_CRC[0], /* CRC8 for factory MAC address */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_MAC_CUSTOM_CRC[] = -{ - &MAC_CUSTOM_CRC[0], /* CRC8 for custom MAC address. */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_MAC_CUSTOM[] = -{ - &MAC_CUSTOM[0], /* Custom MAC */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_MAC_CUSTOM_VER[] = -{ - &MAC_CUSTOM_VER[0], /* Custom MAC version */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_SECURE_BOOT_KEY[] = -{ - &SECURE_BOOT_KEY[0], /* Security boot. Key. - * (length = "None" - 256. - * "3/4" - 192. "REPEAT" - 128) - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_ABS_DONE_0[] = -{ - &ABS_DONE_0[0], /* Secure boot is enabled for bootloader image. - * EFUSE_RD_ABS_DONE_0 - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_ENCRYPT_FLASH_KEY[] = -{ - &ENCRYPT_FLASH_KEY[0], /* Flash encrypt. Key. - * (length = "None" - 256. - * "3/4" - 192. "REPEAT" - 128) - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_ENCRYPT_CONFIG[] = -{ - &ENCRYPT_CONFIG[0], /* Flash encrypt. EFUSE_FLASH_CRYPT_CONFIG_M */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_DISABLE_DL_ENCRYPT[] = -{ - &DISABLE_DL_ENCRYPT[0], /* Flash encrypt. Disable UART bootloader - * encryption. EFUSE_DISABLE_DL_ENCRYPT. - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_DISABLE_DL_DECRYPT[] = -{ - &DISABLE_DL_DECRYPT[0], /* Flash encrypt. Disable UART bootloader - * decryption. EFUSE_DISABLE_DL_DECRYPT. - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_DISABLE_DL_CACHE[] = -{ - &DISABLE_DL_CACHE[0], /* Flash encrypt. Disable UART bootloader - * MMU cache. EFUSE_DISABLE_DL_CACHE. - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_FLASH_CRYPT_CNT[] = -{ - &FLASH_CRYPT_CNT[0], /* Flash encrypt. Flash encryption is enabled - * if this field has an odd number of bits set. - * EFUSE_FLASH_CRYPT_CNT. - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_DISABLE_JTAG[] = -{ - &DISABLE_JTAG[0], /* Disable JTAG. EFUSE_RD_DISABLE_JTAG. */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_CONSOLE_DEBUG_DISABLE[] = -{ - &CONSOLE_DEBUG_DISABLE[0], /* Disable ROM BASIC interpreter fallback. - * EFUSE_RD_CONSOLE_DEBUG_DISABLE. - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_UART_DOWNLOAD_DIS[] = -{ - &UART_DOWNLOAD_DIS[0], /* Disable UART download mode. Valid for - * ESP32 V3 and newer - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT[] = -{ - &WR_DIS_FLASH_CRYPT_CNT[0], /* Flash encrypt. Write protection - * FLASH_CRYPT_CNT - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_WR_DIS_BLK1[] = -{ - &WR_DIS_BLK1[0], /* Flash encrypt. Write protection - * encryption key. EFUSE_WR_DIS_BLK1 */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_WR_DIS_BLK2[] = -{ - &WR_DIS_BLK2[0], /* Security boot. Write protection security - * key. EFUSE_WR_DIS_BLK2 */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_WR_DIS_BLK3[] = -{ - &WR_DIS_BLK3[0], /* Write protection for EFUSE_BLK3. - * EFUSE_WR_DIS_BLK3 - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_RD_DIS_BLK1[] = -{ - &RD_DIS_BLK1[0], /* Flash encrypt. efuse_key_read_protected. - * EFUSE_RD_DIS_BLK1 - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_RD_DIS_BLK2[] = -{ - &RD_DIS_BLK2[0], /* Security boot. efuse_key_read_protected. - * EFUSE_RD_DIS_BLK2 - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_RD_DIS_BLK3[] = -{ - &RD_DIS_BLK3[0], /* Read protection for EFUSE_BLK3. - * EFUSE_RD_DIS_BLK3 - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_CHIP_VER_DIS_APP_CPU[] = -{ - &CHIP_VER_DIS_APP_CPU[0], /* EFUSE_RD_CHIP_VER_DIS_APP_CPU */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_CHIP_VER_DIS_BT[] = -{ - &CHIP_VER_DIS_BT[0], /* EFUSE_RD_CHIP_VER_DIS_BT */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_CHIP_VER_PKG[] = -{ - &CHIP_VER_PKG[0], /* EFUSE_RD_CHIP_VER_PKG */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_CHIP_CPU_FREQ_LOW[] = -{ - &CHIP_CPU_FREQ_LOW[0], /* EFUSE_RD_CHIP_CPU_FREQ_LOW */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_CHIP_CPU_FREQ_RATED[] = -{ - &CHIP_CPU_FREQ_RATED[0], /* EFUSE_RD_CHIP_CPU_FREQ_RATED */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_CHIP_VER_REV1[] = -{ - &CHIP_VER_REV1[0], /* EFUSE_RD_CHIP_VER_REV1 */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_CHIP_VER_REV2[] = -{ - &CHIP_VER_REV2[0], /* EFUSE_RD_CHIP_VER_REV2 */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_XPD_SDIO_REG[] = -{ - &XPD_SDIO_REG[0], /* EFUSE_RD_XPD_SDIO_REG */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_SDIO_TIEH[] = -{ - &SDIO_TIEH[0], /* EFUSE_RD_SDIO_TIEH */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_SDIO_FORCE[] = -{ - &SDIO_FORCE[0], /* EFUSE_RD_SDIO_FORCE */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_ADC_VREF_AND_SDIO_DREF[] = -{ - &ADC_VREF_AND_SDIO_DREF[0], /* EFUSE_RD_ADC_VREF[0..4] or - * SDIO_DREFH[0 1] - */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_ADC1_TP_LOW[] = -{ - &ADC1_TP_LOW[0], /* TP_REG EFUSE_RD_ADC1_TP_LOW */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_ADC2_TP_LOW[] = -{ - &ADC2_TP_LOW[0], /* TP_REG EFUSE_RD_ADC2_TP_LOW */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_ADC1_TP_HIGH[] = -{ - &ADC1_TP_HIGH[0], /* TP_REG EFUSE_RD_ADC1_TP_HIGH */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_ADC2_TP_HIGH[] = -{ - &ADC2_TP_HIGH[0], /* TP_REG EFUSE_RD_ADC2_TP_HIGH */ - NULL -}; - -const efuse_desc_t *ESP_EFUSE_SECURE_VERSION[] = -{ - &SECURE_VERSION[0], /* Secure version for anti-rollback */ - NULL -}; - -/**************************************************************************** - * Private Functions - ****************************************************************************/ diff --git a/arch/xtensa/src/esp32/esp32_irq.c b/arch/xtensa/src/esp32/esp32_irq.c index 9dae25e4957..26004571058 100644 --- a/arch/xtensa/src/esp32/esp32_irq.c +++ b/arch/xtensa/src/esp32/esp32_irq.c @@ -178,8 +178,9 @@ static bool g_non_iram_int_disabled_flag[CONFIG_SMP_NCPUS]; */ static uint32_t g_cpu0_freeints = ESP32_CPUINT_PERIPHSET & - (~ESP32_WIFI_RESERVE_INT & - ~ESP32_BLE_RESERVE_INT); + ~(ESP32_WIFI_RESERVE_INT | + ESP32_BLE_RESERVE_INT); + #ifdef CONFIG_SMP static uint32_t g_cpu1_freeints = ESP32_CPUINT_PERIPHSET; #endif @@ -517,6 +518,8 @@ void up_irqinitialize(void) /* Hard code special cases. */ g_irqmap[XTENSA_IRQ_TIMER0] = IRQ_MKMAP(0, ESP32_CPUINT_TIMER0); + g_irqmap[XTENSA_IRQ_SWINT] = IRQ_MKMAP(0, ESP32_CPUINT_SOFTWARE1); + g_irqmap[XTENSA_IRQ_SWINT] = IRQ_MKMAP(1, ESP32_CPUINT_SOFTWARE1); #ifdef CONFIG_ESP32_WIFI g_irqmap[ESP32_IRQ_MAC] = IRQ_MKMAP(0, ESP32_CPUINT_MAC); @@ -528,9 +531,6 @@ void up_irqinitialize(void) g_irqmap[ESP32_IRQ_RWBLE_IRQ] = IRQ_MKMAP(0, ESP32_PERIPH_RWBLE_IRQ); #endif - g_irqmap[XTENSA_IRQ_SWINT] = IRQ_MKMAP(0, ESP32_CPUINT_SOFTWARE1); - g_irqmap[XTENSA_IRQ_SWINT] = IRQ_MKMAP(1, ESP32_CPUINT_SOFTWARE1); - /* Initialize CPU interrupts */ esp32_cpuint_initialize(); @@ -681,7 +681,7 @@ void up_enable_irq(int irq) /* Enable the CPU interrupt now for internal CPU. */ - xtensa_enable_cpuint(&g_intenable[cpu], 1ul << cpuint); + xtensa_enable_cpuint(&g_intenable[cpu], (1ul << cpuint)); } else { @@ -711,8 +711,8 @@ void up_enable_irq(int irq) DEBUGASSERT(cpu >= 0 && cpu < CONFIG_SMP_NCPUS); - /* Attach the interrupt to the peripheral; the CPU interrupt was - * already enabled when allocated. + /* For peripheral interrupts, attach the interrupt to the peripheral; + * the CPU interrupt was already enabled when allocated. */ int periph = ESP32_IRQ2PERIPH(irq); diff --git a/arch/xtensa/src/esp32/esp32_rng.c b/arch/xtensa/src/esp32/esp32_rng.c index cfecced69c9..4d83b6a5b3d 100644 --- a/arch/xtensa/src/esp32/esp32_rng.c +++ b/arch/xtensa/src/esp32/esp32_rng.c @@ -44,6 +44,8 @@ #include "hardware/wdev_reg.h" #include "esp32_clockconfig.h" +#include "esp_random.h" + #if defined(CONFIG_ESP32_RNG) #if defined(CONFIG_DEV_RANDOM) || defined(CONFIG_DEV_URANDOM_ARCH) @@ -81,42 +83,6 @@ static const struct file_operations g_rngops = * Private functions ****************************************************************************/ -/**************************************************************************** - * Name: esp32_random - ****************************************************************************/ - -uint32_t IRAM_ATTR esp_random(void) -{ - /* The PRNG which implements WDEV_RANDOM register gets 2 bits - * of extra entropy from a hardware randomness source every APB clock cycle - * (provided Wi-Fi or BT are enabled). To make sure entropy is not drained - * faster than it is added, this function needs to wait for at least 16 APB - * clock cycles after reading previous word. This implementation may - * actually wait a bit longer due to extra time spent in arithmetic and - * branch statements. - * - * As a (probably unnecessary) precaution to avoid returning the - * RNG state as-is, the result is XORed with additional - * WDEV_RND_REG reads while waiting. - */ - - uint32_t cpu_to_apb_freq_ratio = esp_clk_cpu_freq() / esp_clk_apb_freq(); - - static uint32_t last_ccount = 0; - uint32_t ccount; - uint32_t result = 0; - - do - { - ccount = XTHAL_GET_CCOUNT(); - result ^= getreg32(WDEV_RND_REG); - } - while (ccount - last_ccount < cpu_to_apb_freq_ratio * 16); - - last_ccount = ccount; - return result ^ getreg32(WDEV_RND_REG); -} - /**************************************************************************** * Name: esp32_rng_read ****************************************************************************/ diff --git a/arch/xtensa/src/esp32/esp32_touch.h b/arch/xtensa/src/esp32/esp32_touch.h index 1f126b88ccf..d48d3b4c61f 100644 --- a/arch/xtensa/src/esp32/esp32_touch.h +++ b/arch/xtensa/src/esp32/esp32_touch.h @@ -29,7 +29,7 @@ #include #include -#include "esp32_touch_lowerhalf.h" +#include "hardware/esp32_touch.h" /**************************************************************************** * Pre-processor Definitions diff --git a/arch/xtensa/src/esp32/esp32_twai.c b/arch/xtensa/src/esp32/esp32_twai.c index 154f6ede5f8..afa4e4b0919 100644 --- a/arch/xtensa/src/esp32/esp32_twai.c +++ b/arch/xtensa/src/esp32/esp32_twai.c @@ -48,6 +48,9 @@ #include "hardware/esp32_dport.h" #include "hardware/esp32_gpio_sigmap.h" +#include "hardware/esp32_twai.h" + +#include "soc/soc.h" #if defined(CONFIG_ESP32_TWAI) diff --git a/arch/xtensa/src/esp32/esp32_twai.h b/arch/xtensa/src/esp32/esp32_twai.h index d1478906806..9ecf0e53040 100644 --- a/arch/xtensa/src/esp32/esp32_twai.h +++ b/arch/xtensa/src/esp32/esp32_twai.h @@ -26,7 +26,6 @@ ****************************************************************************/ #include -#include "hardware/esp32_twai.h" /**************************************************************************** * Pre-processor Definitions diff --git a/arch/xtensa/src/esp32/esp32_wifi_adapter.c b/arch/xtensa/src/esp32/esp32_wifi_adapter.c index 0d2e5066e30..79b7a8e35eb 100644 --- a/arch/xtensa/src/esp32/esp32_wifi_adapter.c +++ b/arch/xtensa/src/esp32/esp32_wifi_adapter.c @@ -57,7 +57,6 @@ #include "xtensa_attr.h" #include "hardware/esp32_dport.h" #include "hardware/esp32_emac.h" -#include "hardware/esp32_soc.h" #include "esp32_irq.h" #include "esp32_wireless.h" #include "esp32_wifi_adapter.h" @@ -69,11 +68,23 @@ # include "esp32_pm.h" #endif -#ifdef CONFIG_ESP32_WIFI_BT_COEXIST -# include "esp_coexist_internal.h" +#ifdef CONFIG_ESP32_BLE +# include "esp32_ble_adapter.h" +# ifdef CONFIG_ESP32_WIFI_BT_COEXIST +# include "esp_coexist_internal.h" +# endif #endif -#include "espidf_wifi.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 "periph_ctrl.h" +#include "rom/ets_sys.h" +#include "soc/soc_caps.h" /**************************************************************************** * Pre-processor Definitions @@ -90,27 +101,28 @@ #define ESP_WIFI_11N_MCS7_HT20_BITRATE 72 #define ESP_WIFI_11N_MCS7_HT40_BITRATE 150 -#define SSID_MAX_LEN (32) -#define PWD_MAX_LEN (64) - #ifndef CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL #define CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL 3 #endif #define DEFAULT_LISTEN_INTERVAL CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL +#define ets_timer _ETSTIMER_ + /* CONFIG_POWER_SAVE_MODEM */ -#if defined(CONFIG_EXAMPLE_POWER_SAVE_MIN_MODEM) +#if defined(CONFIG_ESP32_POWER_SAVE_MIN_MODEM) # define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM -#elif defined(CONFIG_EXAMPLE_POWER_SAVE_MAX_MODEM) +#elif defined(CONFIG_ESP32_POWER_SAVE_MAX_MODEM) # define DEFAULT_PS_MODE WIFI_PS_MAX_MODEM -#elif defined(CONFIG_EXAMPLE_POWER_SAVE_NONE) +#elif defined(CONFIG_ESP32_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 ****************************************************************************/ @@ -126,15 +138,6 @@ enum wifi_sta_state WIFI_STA_STATE_STOP }; -/* Wi-Fi SoftAP state */ - -enum wifi_softap_state -{ - WIFI_SOFTAP_STATE_NULL, - WIFI_SOFTAP_STATE_START, - WIFI_SOFTAP_STATE_STOP -}; - /* Wi-Fi interrupt adapter private data */ struct irq_adpt @@ -160,21 +163,6 @@ struct time_adpt suseconds_t usec; /* Micro second value */ }; -/* Wi-Fi timer private data */ - -struct timer_adpt -{ - struct wdog_s wdog; /* Timer handle */ - struct work_s work; /* Work private data */ - bool repeat; /* Flags indicate if it is cycle */ - uint32_t delay; /* Timeout ticks */ - - /* Timer callback function */ - - void (*func)(void *priv); - void *priv; /* Timer private data */ -}; - /* Wi-Fi event private data */ struct evt_adpt @@ -205,6 +193,12 @@ struct nvs_adpt * Private Function Prototypes ****************************************************************************/ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST +static int semphr_take_from_isr_wrapper(void *semphr, void *hptw); +static int semphr_give_from_isr_wrapper(void *semphr, void *hptw); +static int is_in_isr_wrapper(void); +#endif /* CONFIG_ESP32_WIFI_BT_COEXIST */ + static bool wifi_env_is_chip(void); static void wifi_set_intr(int32_t cpu_no, uint32_t intr_source, uint32_t intr_num, int32_t intr_prio); @@ -222,13 +216,6 @@ static void *esp_semphr_create(uint32_t max, uint32_t init); static void esp_semphr_delete(void *semphr); static int32_t esp_semphr_take(void *semphr, uint32_t block_time_tick); static int32_t esp_semphr_give(void *semphr); - -#ifdef CONFIG_ESP32_WIFI_BT_COEXIST -static int32_t esp_semphr_take_from_isr(void *semphr, void *hptw); -static int32_t esp_semphr_give_from_isr(void *semphr, void *hptw); -static int wifi_is_in_isr(void); -#endif - static void *esp_thread_semphr_get(void); static void *esp_mutex_create(void); static void *esp_recursive_mutex_create(void); @@ -273,6 +260,7 @@ static void *esp_task_get_current_task(void); static int32_t esp_task_get_max_priority(void); static void *esp_malloc(uint32_t size); static void esp_free(void *ptr); +static uint32_t esp_get_free_heap_size(void); static uint32_t esp_rand(void); static void esp_dport_access_stall_other_cpu_start(void); static void esp_dport_access_stall_other_cpu_end(void); @@ -289,6 +277,7 @@ static void wifi_clock_enable(void); static void wifi_clock_disable(void); static void wifi_rtc_enable_iso(void); static void wifi_rtc_disable_iso(void); +static int64_t esp32_timer_get_time(void); static int32_t esp_nvs_set_i8(uint32_t handle, const char *key, int8_t value); static int32_t esp_nvs_get_i8(uint32_t handle, const char *key, @@ -312,9 +301,11 @@ static int32_t esp_nvs_get_blob(uint32_t handle, const char *key, static int32_t esp_nvs_erase_key(uint32_t handle, const char *key); static int32_t esp_get_random(uint8_t *buf, size_t len); static int32_t esp_get_time(void *t); -static void esp_log_writev(uint32_t level, const char *tag, - const char *format, va_list args) - printf_like(3, 0); +static void esp_log_writev_wrapper(uint32_t level, const char *tag, + const char *format, va_list args); +static void esp_log_write_wrapper(uint32_t level, + const char *tag, + const char *format, ...); static void *esp_malloc_internal(size_t size); static void *esp_realloc_internal(void *ptr, size_t size); static void *esp_calloc_internal(size_t n, size_t size); @@ -325,44 +316,30 @@ static void *esp_wifi_calloc(size_t n, size_t size); static void *esp_wifi_zalloc(size_t size); static void *esp_wifi_create_queue(int32_t queue_len, int32_t item_size); static void esp_wifi_delete_queue(void *queue); -static int wifi_coex_init(void); -static void wifi_coex_deinit(void); -static int wifi_coex_enable(void); -static void wifi_coex_disable(void); -static uint32_t esp_coex_status_get(void); -static void esp_coex_condition_set(uint32_t type, bool dissatisfy); -static int32_t esp_coex_wifi_request(uint32_t event, uint32_t latency, - uint32_t duration); -static int32_t esp_coex_wifi_release(uint32_t event); +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 int32_t coex_wifi_request_wrapper(uint32_t event, uint32_t latency, + uint32_t duration); +static int32_t coex_wifi_release_wrapper(uint32_t event); static unsigned long esp_random_ulong(void); -static int wifi_coex_wifi_set_channel(uint8_t primary, uint8_t secondary); -static int wifi_coex_get_event_duration(uint32_t event, - uint32_t *duration); -static int wifi_coex_get_pti(uint32_t event, uint8_t *pti); -static void wifi_coex_clear_schm_status_bit(uint32_t type, - uint32_t status); -static void wifi_coex_set_schm_status_bit(uint32_t type, - uint32_t status); -static int wifi_coex_set_schm_interval(uint32_t interval); -static uint32_t wifi_coex_get_schm_interval(void); -static uint8_t wifi_coex_get_schm_curr_period(void); -static void *wifi_coex_get_schm_curr_phase(void); -static int wifi_coex_set_schm_curr_phase_idx(int idx); -static int wifi_coex_get_schm_curr_phase_idx(void); - -extern void coex_bt_high_prio(void); - -/**************************************************************************** - * Public Functions declaration - ****************************************************************************/ - -int64_t esp_timer_get_time(void); -void esp_fill_random(void *buf, size_t len); -void esp_log_write(uint32_t level, const char *tag, const char *format, ...) - printf_like(3, 4); -uint32_t esp_log_timestamp(void); -uint8_t esp_crc8(const uint8_t *p, uint32_t len); -void intr_matrix_set(int cpu_no, uint32_t model_num, uint32_t intr_num); +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)); /**************************************************************************** * Private Data @@ -375,10 +352,6 @@ 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; -/* Callback function to update Wi-Fi MAC time */ - -wifi_mac_time_update_cb_t g_wifi_mac_time_update_cb; - /* Wi-Fi adapter reference */ static int g_wifi_ref; @@ -427,6 +400,10 @@ static wifi_config_t g_softap_wifi_cfg; static spinlock_t g_lock; +/**************************************************************************** + * Public Data + ****************************************************************************/ + /* Wi-Fi and BT coexistence OS adapter data */ #ifdef CONFIG_ESP32_WIFI_BT_COEXIST @@ -435,30 +412,26 @@ coex_adapter_funcs_t g_coex_adapter_funcs = ._version = COEX_ADAPTER_VERSION, ._spin_lock_create = esp_spin_lock_create, ._spin_lock_delete = esp_spin_lock_delete, - ._int_enable = esp_wifi_int_restore, ._int_disable = esp_wifi_int_disable, + ._int_enable = esp_wifi_int_restore, ._task_yield_from_isr = esp_task_yield_from_isr, ._semphr_create = esp_semphr_create, ._semphr_delete = esp_semphr_delete, - ._semphr_take_from_isr = esp_semphr_take_from_isr, - ._semphr_give_from_isr = esp_semphr_give_from_isr, + ._semphr_take_from_isr = semphr_take_from_isr_wrapper, + ._semphr_give_from_isr = semphr_give_from_isr_wrapper, ._semphr_take = esp_semphr_take, ._semphr_give = esp_semphr_give, - ._is_in_isr = wifi_is_in_isr, + ._is_in_isr = is_in_isr_wrapper, ._malloc_internal = esp_malloc_internal, ._free = esp_free, + ._esp_timer_get_time = esp32_timer_get_time, ._timer_disarm = esp_timer_disarm, ._timer_done = esp32_timer_done, ._timer_setfn = esp_timer_setfn, ._timer_arm_us = esp_timer_arm_us, - ._esp_timer_get_time = esp_timer_get_time, ._magic = COEX_ADAPTER_MAGIC, }; -#endif - -/**************************************************************************** - * Public Data - ****************************************************************************/ +#endif /* CONFIG_ESP32_WIFI_BT_COEXIST */ /* Wi-Fi OS adapter data */ @@ -518,10 +491,10 @@ wifi_osi_funcs_t g_wifi_osi_funcs = esp_dport_access_stall_other_cpu_end, ._wifi_apb80m_request = wifi_apb80m_request, ._wifi_apb80m_release = wifi_apb80m_release, - ._phy_disable = esp32_phy_disable, - ._phy_enable = esp32_phy_enable, - ._phy_common_clock_enable = esp32_phy_enable_clock, - ._phy_common_clock_disable = esp32_phy_disable_clock, + ._phy_disable = esp_phy_disable, + ._phy_enable = esp_phy_enable, + ._phy_common_clock_enable = esp_phy_common_clock_enable, + ._phy_common_clock_disable = esp_phy_common_clock_disable, ._phy_update_country_info = esp32_phy_update_country_info, ._read_mac = esp_wifi_read_mac, ._timer_arm = esp_timer_arm, @@ -534,7 +507,7 @@ wifi_osi_funcs_t g_wifi_osi_funcs = ._wifi_clock_disable = wifi_clock_disable, ._wifi_rtc_enable_iso = wifi_rtc_enable_iso, ._wifi_rtc_disable_iso = wifi_rtc_disable_iso, - ._esp_timer_get_time = esp_timer_get_time, + ._esp_timer_get_time = esp32_timer_get_time, ._nvs_set_i8 = esp_nvs_set_i8, ._nvs_get_i8 = esp_nvs_get_i8, ._nvs_set_u8 = esp_nvs_set_u8, @@ -550,8 +523,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = ._get_random = esp_get_random, ._get_time = esp_get_time, ._random = esp_random_ulong, - ._log_write = esp_log_write, - ._log_writev = esp_log_writev, + ._log_write = esp_log_write_wrapper, + ._log_writev = esp_log_writev_wrapper, ._log_timestamp = esp_log_timestamp, ._malloc_internal = esp_malloc_internal, ._realloc_internal = esp_realloc_internal, @@ -563,25 +536,25 @@ wifi_osi_funcs_t g_wifi_osi_funcs = ._wifi_zalloc = esp_wifi_zalloc, ._wifi_create_queue = esp_wifi_create_queue, ._wifi_delete_queue = esp_wifi_delete_queue, - ._coex_init = wifi_coex_init, - ._coex_deinit = wifi_coex_deinit, - ._coex_enable = wifi_coex_enable, - ._coex_disable = wifi_coex_disable, - ._coex_status_get = esp_coex_status_get, - ._coex_condition_set = esp_coex_condition_set, - ._coex_wifi_request = esp_coex_wifi_request, - ._coex_wifi_release = esp_coex_wifi_release, - ._coex_wifi_channel_set = wifi_coex_wifi_set_channel, - ._coex_event_duration_get = wifi_coex_get_event_duration, - ._coex_pti_get = wifi_coex_get_pti, - ._coex_schm_status_bit_clear = wifi_coex_clear_schm_status_bit, - ._coex_schm_status_bit_set = wifi_coex_set_schm_status_bit, - ._coex_schm_interval_set = wifi_coex_set_schm_interval, - ._coex_schm_interval_get = wifi_coex_get_schm_interval, - ._coex_schm_curr_period_get = wifi_coex_get_schm_curr_period, - ._coex_schm_curr_phase_get = wifi_coex_get_schm_curr_phase, - ._coex_schm_curr_phase_idx_set = wifi_coex_set_schm_curr_phase_idx, - ._coex_schm_curr_phase_idx_get = wifi_coex_get_schm_curr_phase_idx, + ._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, }; @@ -624,7 +597,7 @@ static inline int32_t osi_errno_trans(int ret) } /**************************************************************************** - * Name: osi_errno_trans + * Name: wifi_errno_trans * * Description: * Transform from ESP Wi-Fi error code to NuttX error code @@ -694,10 +667,12 @@ static int32_t wifi_errno_trans(int ret) * Wi-Fi interrupt adapter callback function * * Input Parameters: - * arg - interrupt adapter private data + * irq - Number of the IRQ that generated the interrupt + * context - Interrupt register state save info (not used) + * arg - Argument passed to the interrupt callback * * Returned Value: - * 0 on success + * OK * ****************************************************************************/ @@ -828,7 +803,7 @@ static void esp_set_isr(int32_t n, void *f, void *arg) { wlinfo("irq=%d has been set handler=%p\n", irq, g_irqvector[irq].handler); - return ; + return; } tmp = sizeof(struct irq_adpt); @@ -836,8 +811,8 @@ static void esp_set_isr(int32_t n, void *f, void *arg) if (!adapter) { wlerr("Failed to alloc %d memory\n", tmp); - assert(0); - return ; + PANIC(); + return; } adapter->func = f; @@ -847,8 +822,8 @@ static void esp_set_isr(int32_t n, void *f, void *arg) if (ret) { wlerr("Failed to attach IRQ %d\n", irq); - assert(0); - return ; + PANIC(); + return; } } @@ -933,7 +908,6 @@ static bool IRAM_ATTR wifi_is_from_isr(void) static void *esp_spin_lock_create(void) { -#ifdef CONFIG_SMP spinlock_t *lock; int tmp; @@ -948,11 +922,6 @@ static void *esp_spin_lock_create(void) spin_initialize(lock, SP_UNLOCKED); return lock; -#else - /* If return NULL, code may check fail */ - - return (void *)1; -#endif } /**************************************************************************** @@ -971,11 +940,7 @@ static void *esp_spin_lock_create(void) static void esp_spin_lock_delete(void *lock) { -#ifdef CONFIG_SMP kmm_free(lock); -#else - DEBUGASSERT((int)lock == 1); -#endif } /**************************************************************************** @@ -1029,7 +994,7 @@ static void IRAM_ATTR esp_wifi_int_restore(void *wifi_int_mux, uint32_t tmp) * Name: esp_task_yield_from_isr * * Description: - * Do nothing in NuttX + * Perform a solicited context switch on FreeRTOS. Do nothing in NuttX. * * Input Parameters: * None @@ -1041,7 +1006,6 @@ static void IRAM_ATTR esp_wifi_int_restore(void *wifi_int_mux, uint32_t tmp) static void IRAM_ATTR esp_task_yield_from_isr(void) { - /* Do nothing */ } /**************************************************************************** @@ -1178,70 +1142,6 @@ static int32_t esp_semphr_give(void *semphr) return osi_errno_trans(ret); } -#ifdef CONFIG_ESP32_WIFI_BT_COEXIST - -/**************************************************************************** - * Name: esp_semphr_take_from_isr - * - * Description: - * Try to take semaphore from within an interrupt service routine. - * - * Input Parameters: - * semphr - Semaphore data pointer - * - * Returned Value: - * True if success or false if fail - * - ****************************************************************************/ - -static int32_t IRAM_ATTR esp_semphr_take_from_isr(void *semphr, void *hptw) -{ - *(int *)hptw = 0; - - return osi_errno_trans(nxsem_trywait(semphr)); -} - -/**************************************************************************** - * Name: esp_semphr_give_from_isr - * - * Description: - * Post semaphore from within an interrupt service routine. - * - * Input Parameters: - * semphr - Semaphore data pointer - * - * Returned Value: - * True if success or false if fail - * - ****************************************************************************/ - -static int32_t IRAM_ATTR esp_semphr_give_from_isr(void *semphr, void *hptw) -{ - *(int *)hptw = 0; - - return osi_errno_trans(nxsem_post(semphr)); -} - -/**************************************************************************** - * Name: wifi_is_in_isr - * - * Description: - * Check whether current execution context is of an interrupt service - * routine. - * - * Input Parameters: - * None - * - * Returned Value: - * true if in interrupt or false if not - * - ****************************************************************************/ - -static int IRAM_ATTR wifi_is_in_isr(void) -{ - return up_interrupt_context(); -} -#endif /**************************************************************************** * Name: esp_thread_semphr_get @@ -1637,7 +1537,7 @@ static int32_t esp_queue_send(void *queue, void *item, uint32_t ticks) * Input Parameters: * queue - Message queue data pointer * item - Message data pointer - * hptw - No mean + * hptw - Unused. * * Returned Value: * True if success or false if fail @@ -1646,9 +1546,7 @@ static int32_t esp_queue_send(void *queue, void *item, uint32_t ticks) static int32_t esp_queue_send_from_isr(void *queue, void *item, void *hptw) { - /* Force to set the value to be false */ - - *((int *)hptw) = false; + *(int *)hptw = 0; return esp_queue_send_generic(queue, item, 0, 0); } @@ -1897,9 +1795,15 @@ static int32_t esp_task_create_pinned_to_core(void *entry, int ret; cpu_set_t cpuset; #endif + uint32_t target_prio = prio; - pid = kthread_create(name, prio, stack_depth, entry, - (char * const *)param); + if (target_prio < ESP_MAX_PRIORITIES) + { + target_prio += esp_task_get_max_priority() - ESP_MAX_PRIORITIES; + } + + pid = kthread_create(name, target_prio, stack_depth, entry, + (char * const *)param); if (pid > 0) { if (task_handle != NULL) @@ -2177,6 +2081,7 @@ static int esp_event_id_map(int event_id) id = WIFI_ADPT_EVT_AP_STADISCONNECTED; break; #endif /* ESP32_WLAN_HAS_SOFTAP */ + default: return -1; } @@ -2204,6 +2109,7 @@ static void esp_evt_work_cb(void *arg) irqstate_t flags; struct evt_adpt *evt_adpt; struct wifi_notify *notify; + wifi_ps_type_t ps_type = DEFAULT_PS_MODE; while (1) { @@ -2226,13 +2132,30 @@ static void esp_evt_work_cb(void *arg) #ifdef ESP32_WLAN_HAS_STA case WIFI_ADPT_EVT_STA_START: wlinfo("Wi-Fi sta start\n"); + g_sta_connected = false; - ret = esp_wifi_set_ps(DEFAULT_PS_MODE); + +#ifdef CONFIG_ESP32_BLE + if (esp32_bt_controller_get_status() != + ESP_BT_CONTROLLER_STATUS_IDLE) + { + if (ps_type == WIFI_PS_NONE) + { + ps_type = WIFI_PS_MIN_MODEM; + } + } +#endif + ret = esp_wifi_set_ps(ps_type); + if (ret) { - wlerr("Failed to close PS\n"); + 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) @@ -2280,6 +2203,29 @@ static void esp_evt_work_cb(void *arg) #ifdef ESP32_WLAN_HAS_SOFTAP case WIFI_ADPT_EVT_AP_START: wlinfo("INFO: Wi-Fi softap start\n"); + +#ifdef CONFIG_ESP32_BLE + if (esp32_bt_controller_get_status() != + ESP_BT_CONTROLLER_STATUS_IDLE) + { + if (ps_type == WIFI_PS_NONE) + { + ps_type = WIFI_PS_MIN_MODEM; + } + } +#endif + 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) { @@ -2323,6 +2269,73 @@ static void esp_evt_work_cb(void *arg) } } +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + +/**************************************************************************** + * Name: 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 int IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw) +{ + *(int *)hptw = 0; + + return osi_errno_trans(nxsem_trywait(semphr)); +} + +/**************************************************************************** + * Name: 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 int IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw) +{ + *(int *)hptw = 0; + + return esp_semphr_give(semphr); +} + +/**************************************************************************** + * Name: is_in_isr_wrapper + * + * Description: + * Check current is in interrupt + * + * Input Parameters: + * None + * + * Returned Value: + * true if in interrupt or false if not + * + ****************************************************************************/ + +static int IRAM_ATTR is_in_isr_wrapper(void) +{ + return up_interrupt_context(); +} + +#endif /* CONFIG_ESP32_WIFI_BT_COEXIST */ + /**************************************************************************** * Name: wifi_env_is_chip * @@ -2362,10 +2375,6 @@ static void wifi_set_intr(int32_t cpu_no, uint32_t intr_source, wlinfo("cpu_no=%" PRId32 ", intr_source=%" PRIu32 ", intr_num=%" PRIu32 ", intr_prio=%" PRId32 "\n", cpu_no, intr_source, intr_num, intr_prio); - - /* Force to bind Wi-Fi interrupt to CPU0 */ - - intr_matrix_set(0, intr_source, intr_num); } /**************************************************************************** @@ -2454,7 +2463,7 @@ int32_t esp_event_post(esp_event_base_t event_base, * ****************************************************************************/ -uint32_t esp_get_free_heap_size(void) +static uint32_t esp_get_free_heap_size(void) { struct mallinfo info; @@ -2580,9 +2589,9 @@ static void esp_timer_arm(void *ptimer, uint32_t ms, bool repeat) static void esp_timer_disarm(void *ptimer) { struct ets_timer *ets_timer = (struct ets_timer *)ptimer; - esp_timer_handle_t esp_timer = (esp_timer_handle_t)ets_timer->priv; + esp_timer_handle_t esp_timer = (esp_timer_handle_t)ets_timer->timer_arg; - if (ets_timer->expire == TIMER_INITIALIZED_VAL) + if (ets_timer->timer_expire == TIMER_INITIALIZED_VAL) { esp_timer_stop(esp_timer); } @@ -2605,13 +2614,13 @@ static void esp_timer_disarm(void *ptimer) static void esp32_timer_done(void *ptimer) { struct ets_timer *ets_timer = (struct ets_timer *)ptimer; - esp_timer_handle_t esp_timer = (esp_timer_handle_t)ets_timer->priv; + esp_timer_handle_t esp_timer = (esp_timer_handle_t)ets_timer->timer_arg; - if (ets_timer->expire == TIMER_INITIALIZED_VAL) + if (ets_timer->timer_expire == TIMER_INITIALIZED_VAL) { - ets_timer->expire = 0; + ets_timer->timer_expire = 0; esp_timer_delete(esp_timer); - ets_timer->priv = NULL; + ets_timer->timer_arg = NULL; } } @@ -2637,12 +2646,12 @@ static void esp_timer_setfn(void *ptimer, void *pfunction, void *parg) esp_timer_handle_t esp_timer; struct ets_timer *ets_timer = (struct ets_timer *)ptimer; - if (ets_timer->expire != TIMER_INITIALIZED_VAL) + if (ets_timer->timer_expire != TIMER_INITIALIZED_VAL) { - ets_timer->priv = NULL; + ets_timer->timer_arg = NULL; } - if (ets_timer->priv == NULL) + if (ets_timer->timer_arg == NULL) { const esp_timer_create_args_t create_args = { @@ -2659,8 +2668,8 @@ static void esp_timer_setfn(void *ptimer, void *pfunction, void *parg) } else { - ets_timer->priv = esp_timer; - ets_timer->expire = TIMER_INITIALIZED_VAL; + ets_timer->timer_arg = esp_timer; + ets_timer->timer_expire = TIMER_INITIALIZED_VAL; } } } @@ -2685,9 +2694,9 @@ static void esp_timer_arm_us(void *ptimer, uint32_t us, bool repeat) { int ret; struct ets_timer *ets_timer = (struct ets_timer *)ptimer; - esp_timer_handle_t esp_timer = (esp_timer_handle_t)ets_timer->priv; + esp_timer_handle_t esp_timer = (esp_timer_handle_t)ets_timer->timer_arg; - if (ets_timer->expire == TIMER_INITIALIZED_VAL) + if (ets_timer->timer_expire == TIMER_INITIALIZED_VAL) { esp_timer_stop(esp_timer); if (!repeat) @@ -2724,8 +2733,7 @@ static void esp_timer_arm_us(void *ptimer, uint32_t us, bool repeat) static void wifi_reset_mac(void) { - modifyreg32(DPORT_WIFI_RST_EN_REG, 0, DPORT_MAC_RST_EN); - modifyreg32(DPORT_WIFI_RST_EN_REG, DPORT_MAC_RST_EN, 0); + periph_module_reset(PERIPH_WIFI_MODULE); } /**************************************************************************** @@ -2744,7 +2752,7 @@ static void wifi_reset_mac(void) static void wifi_clock_enable(void) { - modifyreg32(DPORT_WIFI_CLK_EN_REG, 0, DPORT_WIFI_CLK_WIFI_EN_M); + wifi_module_enable(); } /**************************************************************************** @@ -2763,7 +2771,7 @@ static void wifi_clock_enable(void) static void wifi_clock_disable(void) { - modifyreg32(DPORT_WIFI_CLK_EN_REG, DPORT_WIFI_CLK_WIFI_EN_M, 0); + wifi_module_disable(); } /**************************************************************************** @@ -2791,7 +2799,7 @@ static void wifi_rtc_disable_iso(void) } /**************************************************************************** - * Name: esp_timer_get_time + * Name: esp32_timer_get_time * * Description: * Get system time of type int64_t @@ -2804,7 +2812,7 @@ static void wifi_rtc_disable_iso(void) * ****************************************************************************/ -int64_t esp_timer_get_time(void) +int64_t esp32_timer_get_time(void) { return (int64_t)rt_timer_time_us(); } @@ -3092,38 +3100,6 @@ static int32_t esp_nvs_erase_key(uint32_t handle, const char *key) return -1; } -/**************************************************************************** - * Name: esp_fill_random - * - * Description: - * Fill random data int given buffer of given length - * - * Input Parameters: - * buf - buffer pointer - * len - buffer length - * - * Returned Value: - * - ****************************************************************************/ - -void esp_fill_random(void *buf, size_t len) -{ - uint8_t *p = (uint8_t *)buf; - uint32_t tmp; - uint32_t n; - - while (len > 0) - { - tmp = esp_random(); - n = len < 4 ? len : 4; - - memcpy(p, &tmp, n); - - p += n; - len -= n; - } -} - /**************************************************************************** * Name: esp_get_random * @@ -3200,7 +3176,7 @@ static uint32_t esp_rand(void) } /**************************************************************************** - * Name: esp_log_writev + * Name: esp_log_writev_wrapper * * Description: * Output log with by format string and its arguments @@ -3216,34 +3192,29 @@ static uint32_t esp_rand(void) * ****************************************************************************/ -static void esp_log_writev(uint32_t level, const char *tag, - const char *format, va_list args) +static void esp_log_writev_wrapper(uint32_t level, const char *tag, + const char *format, va_list args) { - switch (level) + 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) { -#ifdef CONFIG_DEBUG_WIRELESS_ERROR - case ESP_LOG_ERROR: - vsyslog(LOG_ERR, format, args); - break; -#endif -#ifdef CONFIG_DEBUG_WIRELESS_WARN - case ESP_LOG_WARN: - vsyslog(LOG_WARNING, format, args); - break; -#endif -#ifdef CONFIG_DEBUG_WIRELESS_INFO - case ESP_LOG_INFO: - vsyslog(LOG_INFO, format, args); - break; - default: - vsyslog(LOG_DEBUG, format, args); - break; -#endif + esp_log_writev(level, tag, format, args); } } /**************************************************************************** - * Name: esp_log_write + * Name: esp_log_write_wrapper * * Description: * Output log with by format string and its arguments @@ -3258,33 +3229,29 @@ static void esp_log_writev(uint32_t level, const char *tag, * ****************************************************************************/ -void esp_log_write(uint32_t level, - const char *tag, - const char *format, ...) +static void esp_log_write_wrapper(uint32_t level, + const char *tag, + const char *format, ...) { - va_list list; - va_start(list, format); - esp_log_writev(level, tag, format, list); - va_end(list); -} + esp_log_level_t max_level; -/**************************************************************************** - * Name: esp_log_timestamp - * - * Description: - * Get system time by millim second - * - * Input Parameters: - * None - * - * Returned Value: - * System time - * - ****************************************************************************/ +#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 -uint32_t esp_log_timestamp(void) -{ - return (uint32_t)(esp_timer_get_time() / 1000); + if (level <= max_level) + { + va_list list; + va_start(list, format); + esp_log_writev(level, tag, format, list); + va_end(list); + } } /**************************************************************************** @@ -3583,16 +3550,23 @@ static void esp_wifi_delete_queue(void *queue) } /**************************************************************************** - * Name: wifi_coex_init + * Name: coex_init_wrapper * * Description: - * Don't support + * Init software coexist + * + * Input Parameters: + * none + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. * ****************************************************************************/ -static int wifi_coex_init(void) +static int coex_init_wrapper(void) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_init(); #else return 0; @@ -3600,31 +3574,44 @@ static int wifi_coex_init(void) } /**************************************************************************** - * Name: wifi_coex_deinit + * Name: coex_deinit_wrapper * * Description: - * Don't support + * De-init software coexist + * + * Input Parameters: + * none + * + * Returned Value: + * none * ****************************************************************************/ -static void wifi_coex_deinit(void) +static void coex_deinit_wrapper(void) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST coex_deinit(); #endif } /**************************************************************************** - * Name: wifi_coex_enable + * Name: coex_enable_wrapper * * Description: - * Don't support + * Enable software coexist + * + * Input Parameters: + * none + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. * ****************************************************************************/ -static int wifi_coex_enable(void) +static int coex_enable_wrapper(void) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_enable(); #else return 0; @@ -3632,31 +3619,44 @@ static int wifi_coex_enable(void) } /**************************************************************************** - * Name: wifi_coex_disable + * Name: coex_disable_wrapper * * Description: - * Don't support + * Disable software coexist + * + * Input Parameters: + * none + * + * Returned Value: + * none * ****************************************************************************/ -static void wifi_coex_disable(void) +static void coex_disable_wrapper(void) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST coex_disable(); #endif } /**************************************************************************** - * Name: esp_coex_status_get + * Name: coex_status_get_wrapper * * Description: - * Don't support + * Get software coexist status. + * + * Input Parameters: + * none + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. * ****************************************************************************/ -static uint32_t esp_coex_status_get(void) +static IRAM_ATTR uint32_t coex_status_get_wrapper(void) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_status_get(); #else return 0; @@ -3664,32 +3664,26 @@ static uint32_t esp_coex_status_get(void) } /**************************************************************************** - * Name: esp_coex_condition_set + * Name: coex_wifi_request_wrapper * * Description: - * Don't support + * 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 void esp_coex_condition_set(uint32_t type, bool dissatisfy) +static int32_t coex_wifi_request_wrapper(uint32_t event, uint32_t latency, + uint32_t duration) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - coex_condition_set(type, dissatisfy); -#endif -} - -/**************************************************************************** - * Name: esp_coex_wifi_request - * - * Description: - * Don't support - * - ****************************************************************************/ - -static int32_t esp_coex_wifi_request(uint32_t event, uint32_t latency, - uint32_t duration) -{ -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_wifi_request(event, latency, duration); #else return 0; @@ -3697,33 +3691,48 @@ static int32_t esp_coex_wifi_request(uint32_t event, uint32_t latency, } /**************************************************************************** - * Name: esp_coex_wifi_release + * Name: coex_wifi_release_wrapper * * Description: - * Don't support + * 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 int32_t esp_coex_wifi_release(uint32_t event) +static IRAM_ATTR int32_t coex_wifi_release_wrapper(uint32_t event) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_wifi_release(event); +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_wifi_release(event); #else - return 0; + return 0; #endif } /**************************************************************************** - * Name: wifi_coex_wifi_set_channel + * Name: coex_wifi_channel_set_wrapper * * Description: - * Don't support + * 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 wifi_coex_wifi_set_channel(uint8_t primary, uint8_t secondary) +static int coex_wifi_channel_set_wrapper(uint8_t primary, uint8_t secondary) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_wifi_channel_set(primary, secondary); #else return 0; @@ -3731,16 +3740,25 @@ static int wifi_coex_wifi_set_channel(uint8_t primary, uint8_t secondary) } /**************************************************************************** - * Name: wifi_coex_get_event_duration + * Name: coex_event_duration_get_wrapper * * Description: - * Don't support + * 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 int wifi_coex_get_event_duration(uint32_t event, uint32_t *duration) +static int coex_event_duration_get_wrapper(uint32_t event, + uint32_t *duration) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_event_duration_get(event, duration); #else return 0; @@ -3748,59 +3766,89 @@ static int wifi_coex_get_event_duration(uint32_t event, uint32_t *duration) } /**************************************************************************** - * Name: wifi_coex_get_pti + * Name: coex_pti_get_wrapper * * Description: - * Don't support + * 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 wifi_coex_get_pti(uint32_t event, uint8_t *pti) +static int coex_pti_get_wrapper(uint32_t event, uint8_t *pti) { return 0; } /**************************************************************************** - * Name: wifi_coex_clear_schm_status_bit + * Name: coex_schm_status_bit_clear_wrapper * * Description: - * Don't support + * Clear coexistence status. + * + * Input Parameters: + * type - Coexistence status type + * status - Coexistence status + * + * Returned Value: + * none * ****************************************************************************/ -static void wifi_coex_clear_schm_status_bit(uint32_t type, uint32_t status) +static void coex_schm_status_bit_clear_wrapper(uint32_t type, + uint32_t status) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST coex_schm_status_bit_clear(type, status); #endif } /**************************************************************************** - * Name: wifi_coex_set_schm_status_bit + * Name: coex_schm_status_bit_set_wrapper * * Description: - * Don't support + * Set coexistence status. + * + * Input Parameters: + * type - Coexistence status type + * status - Coexistence status + * + * Returned Value: + * none * ****************************************************************************/ -static void wifi_coex_set_schm_status_bit(uint32_t type, uint32_t status) +static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST coex_schm_status_bit_set(type, status); #endif } /**************************************************************************** - * Name: wifi_coex_set_schm_interval + * Name: coex_schm_interval_set_wrapper * * Description: - * Don't support + * 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 int wifi_coex_set_schm_interval(uint32_t interval) +static IRAM_ATTR int coex_schm_interval_set_wrapper(uint32_t interval) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_schm_interval_set(interval); #else return 0; @@ -3808,16 +3856,22 @@ static int wifi_coex_set_schm_interval(uint32_t interval) } /**************************************************************************** - * Name: wifi_coex_get_schm_interval + * Name: coex_schm_interval_get_wrapper * * Description: - * Don't support + * Get coexistence scheme interval. + * + * Input Parameters: + * none + * + * Returned Value: + * Coexistence scheme interval * ****************************************************************************/ -static uint32_t wifi_coex_get_schm_interval(void) +static uint32_t coex_schm_interval_get_wrapper(void) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_schm_interval_get(); #else return 0; @@ -3825,16 +3879,22 @@ static uint32_t wifi_coex_get_schm_interval(void) } /**************************************************************************** - * Name: wifi_coex_get_schm_curr_period + * Name: coex_schm_curr_period_get_wrapper * * Description: - * Don't support + * Get current coexistence scheme period. + * + * Input Parameters: + * none + * + * Returned Value: + * Coexistence scheme period * ****************************************************************************/ -static uint8_t wifi_coex_get_schm_curr_period(void) +static uint8_t coex_schm_curr_period_get_wrapper(void) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_schm_curr_period_get(); #else return 0; @@ -3842,16 +3902,22 @@ static uint8_t wifi_coex_get_schm_curr_period(void) } /**************************************************************************** - * Name: wifi_coex_get_schm_curr_phase + * Name: coex_schm_curr_phase_get_wrapper * * Description: - * Don't support + * Get current coexistence scheme phase. + * + * Input Parameters: + * none + * + * Returned Value: + * Coexistence scheme phase * ****************************************************************************/ -static void *wifi_coex_get_schm_curr_phase(void) +static void *coex_schm_curr_phase_get_wrapper(void) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST return coex_schm_curr_phase_get(); #else return NULL; @@ -3859,34 +3925,73 @@ static void *wifi_coex_get_schm_curr_phase(void) } /**************************************************************************** - * Name: wifi_coex_set_schm_curr_phase_idx + * Name: coex_register_start_cb_wrapper * * Description: - * Don't support + * 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 wifi_coex_set_schm_curr_phase_idx(int idx) +static int coex_register_start_cb_wrapper(int (* cb)(void)) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_schm_curr_phase_idx_set(idx); +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_register_start_cb(cb); #else return 0; #endif } /**************************************************************************** - * Name: wifi_coex_get_schm_curr_phase_idx + * Name: coex_schm_process_restart_wrapper * * Description: - * Don't support + * 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 wifi_coex_get_schm_curr_phase_idx(void) +static int coex_schm_process_restart_wrapper(void) { -#if defined(CONFIG_ESP32_WIFI_BT_COEXIST) - return coex_schm_curr_phase_idx_get(); +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + 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)) +{ +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + return coex_schm_register_callback(type, cb); #else return 0; #endif @@ -3896,8 +4001,13 @@ static int wifi_coex_get_schm_curr_phase_idx(void) * Name: esp_random_ulong * * Description: - * A simpler wrapper of esp_random. - * Just convert the return value from uint32_t to unsigned long. + * Get random value and convert to unsigned long. + * + * Input Parameters: + * None + * + * Returned Value: + * Random value * ****************************************************************************/ @@ -3912,6 +4022,16 @@ static unsigned long esp_random_ulong(void) * 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 + * ****************************************************************************/ static IRAM_ATTR void esp_wifi_tx_done_cb(uint8_t ifidx, uint8_t *data, @@ -3944,6 +4064,7 @@ static IRAM_ATTR void esp_wifi_tx_done_cb(uint8_t ifidx, uint8_t *data, } #ifdef ESP32_WLAN_HAS_STA + /**************************************************************************** * Name: esp_wifi_auth_trans * @@ -4043,6 +4164,7 @@ static int esp_wifi_cipher_trans(uint32_t wifi_cipher) return cipher_mode; } + #endif /* ESP32_WLAN_HAS_STA */ /**************************************************************************** @@ -4107,6 +4229,37 @@ static int esp_freq_to_channel(uint16_t freq) return 0; } +/**************************************************************************** + * Functions needed by libpp.a + ****************************************************************************/ + +/**************************************************************************** + * Name: pp_printf + * + * Description: + * Output format string and its arguments + * + * Input Parameters: + * format - format string + * + * Returned Value: + * Zero (OK) + * + ****************************************************************************/ + +int pp_printf(const char *format, ...) +{ +#ifdef CONFIG_DEBUG_WIRELESS_INFO + va_list arg; + + va_start(arg, format); + vsyslog(LOG_INFO, format, arg); + va_end(arg); +#endif + + return 0; +} + /**************************************************************************** * Functions needed by libnet80211.a ****************************************************************************/ @@ -4121,7 +4274,7 @@ static int esp_freq_to_channel(uint16_t freq) * format - format string * * Returned Value: - * 0 + * Zero (OK) * ****************************************************************************/ @@ -4139,168 +4292,32 @@ int net80211_printf(const char *format, ...) } /**************************************************************************** - * Functions needed by libmesh.a + * Functions needed by libcoexist.a ****************************************************************************/ /**************************************************************************** - * Name: esp_mesh_send_event_internal + * Name: coexist_printf * * Description: - * Don't support - * - ****************************************************************************/ - -int esp_mesh_send_event_internal(int32_t event_id, - void *event_data, - size_t event_data_size) -{ - return -1; -} - -/**************************************************************************** - * Name: esp_mesh_get_topology - * - * Description: - * Don't support - * - ****************************************************************************/ - -void *esp_mesh_get_topology(void) -{ - return NULL; -} - -/**************************************************************************** - * Functions needed by libwpa_supplicant.a - ****************************************************************************/ - -/**************************************************************************** - * Name: esp_timer_create - * - * Description: - * Create timer with given arguments + * Output format string and its arguments * * Input Parameters: - * create_args - Timer arguments data pointer - * out_handle - Timer handle pointer + * format - format string * * Returned Value: - * 0 if success or -1 if fail + * Zero (OK) * ****************************************************************************/ -int32_t esp_timer_create(const esp_timer_create_args_t *create_args, - esp_timer_handle_t *out_handle) +int coexist_printf(const char *format, ...) { - int ret; - struct rt_timer_args_s rt_timer_args; - struct rt_timer_s *rt_timer; +#ifdef CONFIG_DEBUG_WIRELESS_INFO + va_list arg; - rt_timer_args.arg = create_args->arg; - rt_timer_args.callback = create_args->callback; - - ret = rt_timer_create(&rt_timer_args, &rt_timer); - if (ret) - { - wlerr("Failed to create rt_timer error=%d\n", ret); - return ret; - } - - *out_handle = (esp_timer_handle_t)rt_timer; - - return 0; -} - -/**************************************************************************** - * Name: esp_timer_start_once - * - * Description: - * Start timer with one shot mode - * - * Input Parameters: - * timer - Timer handle pointer - * timeout_us - Timeout value by micro second - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int32_t esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us) -{ - struct rt_timer_s *rt_timer = (struct rt_timer_s *)timer; - - rt_timer_start(rt_timer, timeout_us, false); - - return 0; -} - -/**************************************************************************** - * Name: esp_timer_start_periodic - * - * Description: - * Start timer with periodic mode - * - * Input Parameters: - * timer - Timer handle pointer - * period - Timeout value by micro second - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int32_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period) -{ - struct rt_timer_s *rt_timer = (struct rt_timer_s *)timer; - - rt_timer_start(rt_timer, period, true); - - return 0; -} - -/**************************************************************************** - * Name: esp_timer_stop - * - * Description: - * Stop timer - * - * Input Parameters: - * timer - Timer handle pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int32_t esp_timer_stop(esp_timer_handle_t timer) -{ - struct rt_timer_s *rt_timer = (struct rt_timer_s *)timer; - - rt_timer_stop(rt_timer); - - return 0; -} - -/**************************************************************************** - * Name: esp_timer_delete - * - * Description: - * Delete timer and free resource - * - * Input Parameters: - * timer - Timer handle pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int32_t esp_timer_delete(esp_timer_handle_t timer) -{ - struct rt_timer_s *rt_timer = (struct rt_timer_s *)timer; - - rt_timer_delete(rt_timer); + va_start(arg, format); + vsyslog(LOG_INFO, format, arg); + va_end(arg); +#endif return 0; } @@ -4309,32 +4326,6 @@ int32_t esp_timer_delete(esp_timer_handle_t timer) * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: esp32_wifi_bt_coexist_init - * - * Description: - * Initialize ESP32 Wi-Fi and BT coexistence module. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -#ifdef CONFIG_ESP32_WIFI_BT_COEXIST -int esp32_wifi_bt_coexist_init(void) -{ - coex_dbg_set_log_level(COEX_LOG_INFO); - esp_coex_adapter_register(&g_coex_adapter_funcs); - coex_pre_init(); - - return 0; -} -#endif - /**************************************************************************** * Name: esp_event_send_internal * @@ -4367,77 +4358,6 @@ int32_t esp_event_send_internal(esp_event_base_t event_base, return ret; } -/**************************************************************************** - * Name: esp_wifi_init - * - * Description: - * Initialize Wi-Fi - * - * Input Parameters: - * config - Initialization config parameters - * - * Returned Value: - * 0 if success or others if fail - * - ****************************************************************************/ - -int32_t esp_wifi_init(const wifi_init_config_t *config) -{ - int32_t ret; - - ret = esp_wifi_init_internal(config); - if (ret) - { - wlerr("Failed to initialize Wi-Fi error=%d\n", ret); - return ret; - } - - ret = esp_supplicant_init(); - if (ret) - { - wlerr("Failed to initialize WPA supplicant error=%d\n", ret); - esp_wifi_deinit_internal(); - return ret; - } - - return 0; -} - -/**************************************************************************** - * Name: esp_wifi_deinit - * - * Description: - * Deinitialize Wi-Fi and free resource - * - * Input Parameters: - * None - * - * Returned Value: - * 0 if success or others if fail - * - ****************************************************************************/ - -int32_t esp_wifi_deinit(void) -{ - int ret; - - ret = esp_supplicant_deinit(); - if (ret) - { - wlerr("Failed to deinitialize supplicant\n"); - return ret; - } - - ret = esp_wifi_deinit_internal(); - if (ret != 0) - { - wlerr("Failed to deinitialize Wi-Fi\n"); - return ret; - } - - return ret; -} - /**************************************************************************** * Name: esp_wifi_free_eb * @@ -5043,6 +4963,8 @@ int esp_wifi_sta_essid(struct iwreq *iwr, bool 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) { @@ -5632,7 +5554,8 @@ int esp_wifi_sta_bitrate(struct iwreq *iwr, bool set) return OK; } -#endif //ESP32_WLAN_HAS_STA + +#endif /* ESP32_WLAN_HAS_STA */ /**************************************************************************** * Name: esp_wifi_sta_get_txpower @@ -5714,7 +5637,7 @@ int esp_wifi_sta_txpower(struct iwreq *iwr, bool set) } /**************************************************************************** - * Name: esp_wifi_sta_get_channel_range + * Name: esp_wifi_sta_channel * * Description: * Get station range of channel parameters. @@ -5828,6 +5751,7 @@ int esp_wifi_sta_country(struct iwreq *iwr, bool set) } #ifdef ESP32_WLAN_HAS_STA + /**************************************************************************** * Name: esp_wifi_sta_rssi * @@ -6021,7 +5945,7 @@ errout: * ****************************************************************************/ -int esp_wifi_softap_send_data(void *pbuf, uint32_t len) +int esp_wifi_softap_send_data(void *pbuf, size_t len) { int ret; @@ -6125,7 +6049,7 @@ int esp_wifi_softap_password(struct iwreq *iwr, bool set) DEBUGASSERT(ext != NULL); pdata = ext->key; - len = ext->key_len; + len = ext->key_len; if (set && len > PWD_MAX_LEN) { @@ -6142,6 +6066,7 @@ int esp_wifi_softap_password(struct iwreq *iwr, bool set) /* Clear the password field and copy the user password to it */ memset(wifi_cfg.ap.password, 0x0, PWD_MAX_LEN); + if (ext->alg != IW_ENCODE_ALG_NONE) { memcpy(wifi_cfg.sta.password, pdata, len); @@ -6227,7 +6152,6 @@ int esp_wifi_softap_essid(struct iwreq *iwr, bool set) memset(wifi_cfg.ap.ssid, 0x0, SSID_MAX_LEN); memcpy(wifi_cfg.ap.ssid, pdata, len); wifi_cfg.ap.ssid_len = len; - if (g_softap_started) { ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); @@ -6403,6 +6327,13 @@ int esp_wifi_softap_auth(struct iwreq *iwr, bool set) wifi_cfg.ap.authmode = WIFI_AUTH_WPA2_PSK; break; + case IW_AUTH_WPA_VERSION_WPA3: + wifi_cfg.ap.pmf_cfg.required = true; + wifi_cfg.ap.pmf_cfg.capable = false; + wifi_cfg.ap.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; + wifi_cfg.ap.authmode = WIFI_AUTH_WPA3_PSK; + break; + default: wlerr("Invalid wpa version %" PRId32 "\n", iwr->u.param.value); @@ -6631,7 +6562,32 @@ int esp_wifi_softap_rssi(struct iwreq *iwr, bool set) return -ENOSYS; } -#endif +#endif /* ESP32_WLAN_HAS_SOFTAP */ + +/**************************************************************************** + * Name: esp32_wifi_bt_coexist_init + * + * Description: + * Initialize ESP32 Wi-Fi and BT coexistance module. + * + * Input Parameters: + * None + * + * Returned Value: + * OK on success (positive non-zero values are cmd-specific) + * Negated errno returned on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST +int esp32_wifi_bt_coexist_init(void) +{ + esp_coex_adapter_register(&g_coex_adapter_funcs); + coex_pre_init(); + + return 0; +} +#endif /* CONFIG_ESP32_WIFI_BT_COEXIST */ /**************************************************************************** * Name: esp_wifi_stop_callback diff --git a/arch/xtensa/src/esp32/esp32_wifi_adapter.h b/arch/xtensa/src/esp32/esp32_wifi_adapter.h index 5c0b251196c..309cf3ae288 100644 --- a/arch/xtensa/src/esp32/esp32_wifi_adapter.h +++ b/arch/xtensa/src/esp32/esp32_wifi_adapter.h @@ -61,7 +61,8 @@ extern "C" # define ESP32_WLAN_DEVS 2 #endif -/* Needed to fix coex_adapter_funcs_t definition */ +#define SSID_MAX_LEN (32) +#define PWD_MAX_LEN (64) #define CONFIG_IDF_TARGET_ESP32 1 diff --git a/arch/xtensa/src/esp32/esp32_wifi_utils.c b/arch/xtensa/src/esp32/esp32_wifi_utils.c index 7f10e1c6fe4..200482d2f40 100644 --- a/arch/xtensa/src/esp32/esp32_wifi_utils.c +++ b/arch/xtensa/src/esp32/esp32_wifi_utils.c @@ -35,7 +35,15 @@ #include "esp32_wifi_adapter.h" #include "esp32_wifi_utils.h" #include "esp32_wireless.h" -#include "espidf_wifi.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 "rom/ets_sys.h" +#include "soc/soc_caps.h" /**************************************************************************** * Pre-processor Definitions @@ -53,7 +61,6 @@ #endif #define SCAN_TIME_SEC (5) -#define SSID_LEN (33) /* Maximum number of channels for Wi-Fi 2.4Ghz */ @@ -114,13 +121,13 @@ int esp_wifi_start_scan(struct iwreq *iwr) { struct wifi_scan_result *priv = &g_scan_priv; wifi_scan_config_t *config = NULL; - uint8_t target_ssid[SSID_LEN]; 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)); - memset(target_ssid, 0x0, sizeof(SSID_LEN)); if (iwr == NULL) { wlerr("ERROR: Invalid ioctl cmd.\n"); @@ -132,7 +139,7 @@ int esp_wifi_start_scan(struct iwreq *iwr) return OK; } - config = kmm_malloc(sizeof(wifi_scan_config_t)); + config = kmm_calloc(1, sizeof(wifi_scan_config_t)); if (config == NULL) { wlerr("ERROR: Cannot allocate result buffer\n"); @@ -141,7 +148,7 @@ int esp_wifi_start_scan(struct iwreq *iwr) g_channel_num = 0; memset(g_channel_list, 0x0, CHANNEL_MAX_NUM); - memset(config, 0x0, sizeof(wifi_scan_config_t)); + if (iwr->u.data.pointer && iwr->u.data.length >= sizeof(struct iw_scan_req)) { @@ -153,6 +160,8 @@ int esp_wifi_start_scan(struct iwreq *iwr) { /* 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'; @@ -196,8 +205,6 @@ int esp_wifi_start_scan(struct iwreq *iwr) } esp_wifi_start(); - - esp_wifi_scan_stop(); ret = esp_wifi_scan_start(config, false); if (ret != OK) { @@ -257,18 +264,25 @@ int esp_wifi_get_scan_results(struct iwreq *iwr) if (g_scan_priv.scan_status == ESP_SCAN_RUN) { + irqstate_t irqstate = enter_critical_section(); if (scan_block == false) { 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) + { + return -EINVAL; + } if ((iwr == NULL) || (g_scan_priv.scan_status != ESP_SCAN_DONE)) { @@ -276,7 +290,7 @@ int esp_wifi_get_scan_results(struct iwreq *iwr) goto exit_failed; } - if (!priv->scan_result) + if (priv->scan_result == NULL) { /* Result have already been requested */ @@ -352,19 +366,26 @@ void esp_wifi_scan_event_parse(void) 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_zalloc(bss_total * sizeof(wifi_ap_record_t)); + 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 malloc buffer to print scan results"); + wlerr("ERROR: Failed to calloc buffer to print scan results"); + nxsem_post(&priv->scan_signal); return; } @@ -377,6 +398,7 @@ void esp_wifi_scan_event_parse(void) 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) @@ -421,16 +443,16 @@ void esp_wifi_scan_event_parse(void) /* Copy ESSID */ essid_len = MIN(strlen((const char *) - ap_list_buffer[bss_count].ssid), 32); + 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) + 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->len = ESP_IW_EVENT_SIZE(essid) + essid_len_aligned; iwe->cmd = SIOCGIWESSID; iwe->u.essid.flags = 0; iwe->u.essid.length = essid_len; @@ -442,10 +464,12 @@ void esp_wifi_scan_event_parse(void) 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; + ESP_IW_EVENT_SIZE(essid) + essid_len_aligned; + result_size -= ESP_IW_EVENT_SIZE(essid) + essid_len_aligned; /* Copy link quality info */ @@ -459,7 +483,9 @@ void esp_wifi_scan_event_parse(void) 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; diff --git a/arch/xtensa/src/esp32/esp32_wireless.c b/arch/xtensa/src/esp32/esp32_wireless.c index 109e61aa9a5..ad9cc4201b7 100644 --- a/arch/xtensa/src/esp32/esp32_wireless.c +++ b/arch/xtensa/src/esp32/esp32_wireless.c @@ -26,10 +26,10 @@ #include #include -#include -#include #include #include +#include +#include #include "xtensa.h" #include "hardware/esp32_dport.h" @@ -38,6 +38,13 @@ #include "esp32_irq.h" #include "esp32_partition.h" +#include "esp_private/phy.h" +#ifdef CONFIG_ESP32_WIFI +# include "esp_private/wifi.h" +# include "esp_wpa.h" +#endif +#include "esp_coexist_internal.h" +#include "periph_ctrl.h" #include "esp_phy_init.h" #include "phy_init_data.h" @@ -47,12 +54,6 @@ * Pre-processor Definitions ****************************************************************************/ -#ifdef CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR -# define MAC_ADDR_UNIVERSE_BT_OFFSET 2 -#else -# define MAC_ADDR_UNIVERSE_BT_OFFSET 1 -#endif - /* Software Interrupt */ #define SWI_IRQ ESP32_IRQ_CPU_CPU2 @@ -81,13 +82,16 @@ struct esp_wireless_priv_s static inline void phy_digital_regs_store(void); static inline void phy_digital_regs_load(void); +static int esp_swi_irq(int irq, void *context, void *arg); +#ifdef CONFIG_ESP32_WIFI +static void esp_wifi_set_log_level(void); +#endif /**************************************************************************** * Extern Functions declaration ****************************************************************************/ extern uint8_t esp_crc8(const uint8_t *p, uint32_t len); -extern void coex_bt_high_prio(void); extern void phy_wakeup_init(void); extern void phy_close_rf(void); extern uint8_t phy_dig_reg_backup(bool init, uint32_t *regs); @@ -116,6 +120,10 @@ static uint32_t *g_phy_digital_regs_mem = NULL; static bool g_is_phy_calibrated = false; +/* Private data of the wireless common interface */ + +static struct esp_wireless_priv_s g_esp_wireless_priv; + #ifdef CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION static const char *phy_partion_label = "phy_init"; #endif @@ -144,6 +152,7 @@ static const char *g_phy_type[ESP_PHY_INIT_DATA_TYPE_NUMBER] = static phy_country_to_bin_type_t g_country_code_map_type_table[] = { + {"01", ESP_PHY_INIT_DATA_TYPE_DEFAULT}, {"AT", ESP_PHY_INIT_DATA_TYPE_CE}, {"AU", ESP_PHY_INIT_DATA_TYPE_ACMA}, {"BE", ESP_PHY_INIT_DATA_TYPE_CE}, @@ -192,9 +201,13 @@ static phy_country_to_bin_type_t g_country_code_map_type_table[] = #endif -/* Private data of the wireless common interface */ +/**************************************************************************** + * Public Data + ****************************************************************************/ -static struct esp_wireless_priv_s g_esp_wireless_priv; +/* Callback function to update WiFi MAC time */ + +wifi_mac_time_update_cb_t g_wifi_mac_time_update_cb = NULL; /**************************************************************************** * Private Functions @@ -299,6 +312,41 @@ static int esp_swi_irq(int irq, void *context, void *arg) return OK; } +#ifdef CONFIG_ESP32_WIFI + +/**************************************************************************** + * 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); +} +#endif /* CONFIG_ESP32_WIFI */ + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -326,48 +374,6 @@ uint32_t IRAM_ATTR esp_dport_access_reg_read(uint32_t reg) return getreg32(reg); } -/**************************************************************************** - * Name: phy_enter_critical - * - * Description: - * Enter critical state - * - * Input Parameters: - * None - * - * Returned Value: - * CPU PS value - * - ****************************************************************************/ - -uint32_t IRAM_ATTR phy_enter_critical(void) -{ - irqstate_t flags; - - flags = enter_critical_section(); - - return flags; -} - -/**************************************************************************** - * Name: phy_exit_critical - * - * Description: - * Exit from critical state - * - * Input Parameters: - * level - CPU PS value - * - * Returned Value: - * None - * - ****************************************************************************/ - -void IRAM_ATTR phy_exit_critical(uint32_t level) -{ - leave_critical_section(level); -} - /**************************************************************************** * Name: phy_printf * @@ -395,71 +401,6 @@ int phy_printf(const char *format, ...) return 0; } -/**************************************************************************** - * Name: esp32_phy_enable_clock - * - * Description: - * Enable PHY hardware clock - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp32_phy_enable_clock(void) -{ - irqstate_t flags; - - flags = enter_critical_section(); - - if (g_phy_clk_en_cnt == 0) - { - modifyreg32(DPORT_WIFI_CLK_EN_REG, 0, - DPORT_WIFI_CLK_WIFI_BT_COMMON_M); - } - - g_phy_clk_en_cnt++; - - leave_critical_section(flags); -} - -/**************************************************************************** - * Name: esp32_phy_disable_clock - * - * Description: - * Disable PHY hardware clock - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp32_phy_disable_clock(void) -{ - irqstate_t flags; - - flags = enter_critical_section(); - - if (g_phy_clk_en_cnt > 0) - { - g_phy_clk_en_cnt--; - if (g_phy_clk_en_cnt == 0) - { - modifyreg32(DPORT_WIFI_CLK_EN_REG, - DPORT_WIFI_CLK_WIFI_BT_COMMON_M, - 0); - } - } - - leave_critical_section(flags); -} - #ifdef CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA /**************************************************************************** @@ -515,7 +456,7 @@ static uint8_t phy_find_bin_type_according_country(const char *country) for (i = 0; i < num; i++) { if (memcmp(country, g_country_code_map_type_table[i].cc, - PHY_COUNTRY_CODE_LEN) == 0) + sizeof(g_phy_current_country)) == 0) { phy_init_data_type = g_country_code_map_type_table[i].type; wlinfo("Current country is %c%c, PHY init data type is %s\n", @@ -606,8 +547,10 @@ static int phy_get_multiple_init_data(uint8_t *data, size_t length, return -ENOMEM; } - int ret = esp32_partition_read(phy_partion_label, length, - control_info, sizeof(phy_control_info_data_t)); + int ret = esp32_partition_read(phy_partion_label, + length, + control_info, + sizeof(phy_control_info_data_t)); if (ret != OK) { kmm_free(control_info); @@ -618,8 +561,9 @@ static int phy_get_multiple_init_data(uint8_t *data, size_t length, if ((control_info->check_algorithm) == PHY_CRC_ALGORITHM) { ret = phy_crc_check(control_info->multiple_bin_checksum, - control_info->control_info_checksum, sizeof(phy_control_info_data_t) - - sizeof(control_info->control_info_checksum)); + control_info->control_info_checksum, + sizeof(phy_control_info_data_t) - + sizeof(control_info->control_info_checksum)); if (ret != OK) { kmm_free(control_info); @@ -762,7 +706,7 @@ static int phy_update_init_data(phy_init_data_type_t init_data_type) if (g_current_apply_phy_init_data != g_phy_init_data_type) { ret = esp_phy_apply_phy_init_data(init_data_store + - sizeof(phy_init_magic_pre)); + sizeof(phy_init_magic_pre)); if (ret != OK) { wlerr("ERROR: PHY init data failed to load\n"); @@ -802,7 +746,7 @@ const esp_phy_init_data_t *esp_phy_get_init_data(void) { int ret; size_t length = sizeof(phy_init_magic_pre) + - sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post); + sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post); uint8_t *init_data_store = kmm_malloc(length); if (init_data_store == NULL) { @@ -867,6 +811,7 @@ const esp_phy_init_data_t *esp_phy_get_init_data(void) } #endif + wlinfo("PHY data partition validated\n"); return (const esp_phy_init_data_t *) (init_data_store + sizeof(phy_init_magic_pre)); } @@ -931,84 +876,6 @@ void esp_phy_release_init_data(const esp_phy_init_data_t *init_data) } #endif -/**************************************************************************** - * Name: esp_read_mac - * - * 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 - * - ****************************************************************************/ - -int32_t esp_read_mac(uint8_t *mac, esp_mac_type_t type) -{ - uint32_t regval[2]; - uint8_t *data = (uint8_t *)regval; - uint8_t crc; - int i; - - if (type > ESP_MAC_BT) - { - wlerr("Input type is error=%d\n", type); - return -1; - } - - regval[0] = getreg32(MAC_ADDR0_REG); - regval[1] = getreg32(MAC_ADDR1_REG); - - crc = data[6]; - for (i = 0; i < MAC_LEN; i++) - { - mac[i] = data[5 - i]; - } - - if (crc != esp_crc8(mac, MAC_LEN)) - { - wlerr("Failed to check MAC address CRC\n"); - return -1; - } - - if (type == ESP_MAC_WIFI_SOFTAP) - { -#ifdef CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP - mac[5] += 1; -#else - uint8_t tmp = mac[0]; - for (i = 0; i < 64; i++) - { - mac[0] = tmp | 0x02; - mac[0] ^= i << 2; - - if (mac[0] != tmp) - { - break; - } - } - - if (i >= 64) - { - wlerr("Failed to generate SoftAP MAC\n"); - return -1; - } -#endif - } - - if (type == ESP_MAC_BT) - { -#ifdef CONFIG_ESP_MAC_ADDR_UNIVERSE_BT - mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET; -#endif - } - - return 0; -} - /**************************************************************************** * Name: esp32_phy_update_country_info * @@ -1058,107 +925,134 @@ int esp32_phy_update_country_info(const char *country) } /**************************************************************************** - * Name: esp32_phy_disable + * Name: esp_timer_create * * Description: - * Deinitialize PHY hardware + * Create timer with given arguments * * Input Parameters: - * None + * create_args - Timer arguments data pointer + * out_handle - Timer handle pointer * * Returned Value: - * None + * 0 if success or -1 if fail * ****************************************************************************/ -void esp32_phy_disable(void) +int32_t esp_timer_create(const esp_timer_create_args_t *create_args, + esp_timer_handle_t *out_handle) { - irqstate_t flags; - flags = enter_critical_section(); + int ret; + struct rt_timer_args_s rt_timer_args; + struct rt_timer_s *rt_timer; - g_phy_access_ref--; + rt_timer_args.arg = create_args->arg; + rt_timer_args.callback = create_args->callback; - if (g_phy_access_ref == 0) + ret = rt_timer_create(&rt_timer_args, &rt_timer); + if (ret) { - /* Disable PHY and RF. */ - - phy_close_rf(); - - /* Disable Wi-Fi/BT common peripheral clock. - * Do not disable clock for hardware RNG. - */ - - esp32_phy_disable_clock(); + wlerr("Failed to create rt_timer error=%d\n", ret); + return ret; } - leave_critical_section(flags); + *out_handle = (esp_timer_handle_t)rt_timer; + + return 0; } /**************************************************************************** - * Name: esp32_phy_enable + * Name: esp_timer_start_once * * Description: - * Initialize PHY hardware + * Start timer with one shot mode * * Input Parameters: - * None + * timer - Timer handle pointer + * timeout_us - Timeout value by micro second * * Returned Value: - * None + * 0 if success or -1 if fail * ****************************************************************************/ -void esp32_phy_enable(void) +int32_t esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us) { - static bool debug = false; - irqstate_t flags; - esp_phy_calibration_data_t *cal_data; - if (debug == false) - { - char *phy_version = get_phy_version_str(); - wlinfo("phy_version %s\n", phy_version); - debug = true; - } + struct rt_timer_s *rt_timer = (struct rt_timer_s *)timer; - cal_data = kmm_zalloc(sizeof(esp_phy_calibration_data_t)); - if (!cal_data) - { - wlerr("ERROR: Failed to allocate PHY calibration data buffer."); - abort(); - } + rt_timer_start(rt_timer, timeout_us, false); - flags = enter_critical_section(); + return 0; +} - if (g_phy_access_ref == 0) - { - esp32_phy_enable_clock(); - if (g_is_phy_calibrated == false) - { - const esp_phy_init_data_t *init_data = esp_phy_get_init_data(); - if (init_data == NULL) - { - wlerr("ERROR: Failed to obtain PHY init data"); - abort(); - } +/**************************************************************************** + * Name: esp_timer_start_periodic + * + * Description: + * Start timer with periodic mode + * + * Input Parameters: + * timer - Timer handle pointer + * period - Timeout value by micro second + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ - register_chipv7_phy(init_data, cal_data, PHY_RF_CAL_FULL); - esp_phy_release_init_data(init_data); - g_is_phy_calibrated = true; - } - else - { - phy_wakeup_init(); - phy_digital_regs_load(); - } +int32_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period) +{ + struct rt_timer_s *rt_timer = (struct rt_timer_s *)timer; -#ifdef CONFIG_ESP32_BLE - coex_bt_high_prio(); -#endif - } + rt_timer_start(rt_timer, period, true); - g_phy_access_ref++; - leave_critical_section(flags); - kmm_free(cal_data); + return 0; +} + +/**************************************************************************** + * Name: esp_timer_stop + * + * Description: + * Stop timer + * + * Input Parameters: + * timer - Timer handle pointer + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int32_t esp_timer_stop(esp_timer_handle_t timer) +{ + struct rt_timer_s *rt_timer = (struct rt_timer_s *)timer; + + rt_timer_stop(rt_timer); + + return 0; +} + +/**************************************************************************** + * Name: esp_timer_delete + * + * Description: + * Delete timer and free resource + * + * Input Parameters: + * timer - Timer handle pointer + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int32_t esp_timer_delete(esp_timer_handle_t timer) +{ + struct rt_timer_s *rt_timer = (struct rt_timer_s *)timer; + + rt_timer_delete(rt_timer); + + return 0; } /**************************************************************************** @@ -1397,3 +1291,99 @@ int esp_wireless_deinit(void) return OK; } + +#ifdef CONFIG_ESP32_WIFI + +/**************************************************************************** + * Name: esp_wifi_init + * + * Description: + * Initialize Wi-Fi + * + * Input Parameters: + * config - Initialization config parameters + * + * Returned Value: + * 0 if success or others if fail + * + ****************************************************************************/ + +int32_t esp_wifi_init(const wifi_init_config_t *config) +{ + int32_t ret; + + esp_wifi_power_domain_on(); + +#ifdef CONFIG_ESP32_WIFI_BT_COEXIST + ret = coex_init(); + if (ret) + { + wlerr("ERROR: Failed to initialize coex error=%d\n", ret); + return ret; + } +#endif /* CONFIG_ESP32_WIFI_BT_COEXIST */ + + esp_wifi_set_log_level(); + + ret = esp_wifi_init_internal(config); + if (ret) + { + wlerr("Failed to initialize Wi-Fi error=%d\n", ret); + return ret; + } + +#if CONFIG_MAC_BB_PD + esp_mac_bb_pd_mem_init(); + esp_wifi_internal_set_mac_sleep(true); +#endif + + esp_phy_modem_init(); + + g_wifi_mac_time_update_cb = esp_wifi_internal_update_mac_time; + + ret = esp_supplicant_init(); + if (ret) + { + wlerr("Failed to initialize WPA supplicant error=%d\n", ret); + esp_wifi_deinit_internal(); + return ret; + } + + return 0; +} + +/**************************************************************************** + * Name: esp_wifi_deinit + * + * Description: + * Deinitialize Wi-Fi and free resource + * + * Input Parameters: + * None + * + * Returned Value: + * 0 if success or others if fail + * + ****************************************************************************/ + +int32_t esp_wifi_deinit(void) +{ + int ret; + + ret = esp_supplicant_deinit(); + if (ret) + { + wlerr("Failed to deinitialize supplicant\n"); + return ret; + } + + ret = esp_wifi_deinit_internal(); + if (ret != 0) + { + wlerr("Failed to deinitialize Wi-Fi\n"); + return ret; + } + + return ret; +} +#endif /* CONFIG_ESP32_WIFI */ diff --git a/arch/xtensa/src/esp32/esp32_wireless.h b/arch/xtensa/src/esp32/esp32_wireless.h index 6b7e328cfa7..6c183792682 100644 --- a/arch/xtensa/src/esp32/esp32_wireless.h +++ b/arch/xtensa/src/esp32/esp32_wireless.h @@ -32,8 +32,16 @@ #include #include "xtensa_attr.h" +#include "esp32_rt_timer.h" -#include "espidf_wifi.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 "rom/ets_sys.h" +#include "soc/soc_caps.h" /**************************************************************************** * Pre-processor Definitions @@ -81,87 +89,6 @@ struct esp_queuecache_s * Public Function Prototypes ****************************************************************************/ -/**************************************************************************** - * Name: esp_read_mac - * - * 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 - * - ****************************************************************************/ - -int32_t esp_read_mac(uint8_t *mac, esp_mac_type_t type); - -/**************************************************************************** - * Name: esp32_phy_enable - * - * Description: - * Initialize PHY hardware - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp32_phy_enable(void); - -/**************************************************************************** - * Name: esp32_phy_disable - * - * Description: - * Deinitialize PHY hardware - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp32_phy_disable(void); - -/**************************************************************************** - * Name: esp32_phy_enable_clock - * - * Description: - * Enable PHY clock - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp32_phy_enable_clock(void); - -/**************************************************************************** - * Name: esp32_phy_disable_clock - * - * Description: - * Disable PHY clock - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp32_phy_disable_clock(void); - /**************************************************************************** * Functions needed by libphy.a ****************************************************************************/ @@ -182,38 +109,6 @@ void esp32_phy_disable_clock(void); uint32_t IRAM_ATTR esp_dport_access_reg_read(uint32_t reg); -/**************************************************************************** - * Name: phy_enter_critical - * - * Description: - * Enter critical state - * - * Input Parameters: - * None - * - * Returned Value: - * CPU PS value - * - ****************************************************************************/ - -uint32_t IRAM_ATTR phy_enter_critical(void); - -/**************************************************************************** - * Name: phy_exit_critical - * - * Description: - * Exit from critical state - * - * Input Parameters: - * level - CPU PS value - * - * Returned Value: - * None - * - ****************************************************************************/ - -void IRAM_ATTR phy_exit_critical(uint32_t level); - /**************************************************************************** * Name: phy_printf * @@ -230,6 +125,90 @@ void IRAM_ATTR phy_exit_critical(uint32_t level); int phy_printf(const char *format, ...) printf_like(1, 2); +/**************************************************************************** + * Name: esp_timer_create + * + * Description: + * Create timer with given arguments + * + * Input Parameters: + * create_args - Timer arguments data pointer + * out_handle - Timer handle pointer + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int32_t esp_timer_create(const esp_timer_create_args_t *create_args, + esp_timer_handle_t *out_handle); + +/**************************************************************************** + * Name: esp_timer_start_once + * + * Description: + * Start timer with one shot mode + * + * Input Parameters: + * timer - Timer handle pointer + * timeout_us - Timeout value by micro second + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int32_t esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us); + +/**************************************************************************** + * Name: esp_timer_start_periodic + * + * Description: + * Start timer with periodic mode + * + * Input Parameters: + * timer - Timer handle pointer + * period - Timeout value by micro second + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int32_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period); + +/**************************************************************************** + * Name: esp_timer_stop + * + * Description: + * Stop timer + * + * Input Parameters: + * timer - Timer handle pointer + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int32_t esp_timer_stop(esp_timer_handle_t timer); + +/**************************************************************************** + * Name: esp_timer_delete + * + * Description: + * Delete timer and free resource + * + * Input Parameters: + * timer - Timer handle pointer + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int32_t esp_timer_delete(esp_timer_handle_t timer); + /**************************************************************************** * Name: esp32_phy_update_country_info * diff --git a/arch/xtensa/src/esp32/hal.mk b/arch/xtensa/src/esp32/hal.mk new file mode 100644 index 00000000000..3257f81a468 --- /dev/null +++ b/arch/xtensa/src/esp32/hal.mk @@ -0,0 +1,110 @@ +############################################################################ +# arch/xtensa/src/esp32/hal.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. +# +############################################################################ + +# Include header paths + +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)private_include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)$(CHIP_SERIES)$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)$(CHIP_SERIES)$(DELIM)private_include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_common$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_event$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)include$(DELIM)esp_private +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)include$(DELIM)soc +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)port$(DELIM)$(CHIP_SERIES) +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)port$(DELIM)$(CHIP_SERIES)$(DELIM)private_include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)port$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_phy$(DELIM)$(CHIP_SERIES)$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_phy$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_rom$(DELIM)$(CHIP_SERIES) +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_rom$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_rom$(DELIM)include$(DELIM)$(CHIP_SERIES) +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)include$(DELIM)private +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)public_compat +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_wifi$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)$(CHIP_SERIES)$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)platform_port$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)log +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)log$(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)components$(DELIM)soc$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)xtensa$(DELIM)$(CHIP_SERIES)$(DELIM)include +INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)xtensa$(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 + +# Linker scripts + +ARCHSCRIPT += $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_rom$(DELIM)$(CHIP_SERIES)$(DELIM)ld$(DELIM)$(CHIP_SERIES).rom.api.ld +ARCHSCRIPT += $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_rom$(DELIM)$(CHIP_SERIES)$(DELIM)ld$(DELIM)$(CHIP_SERIES).rom.ld +ARCHSCRIPT += $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)ld$(DELIM)$(CHIP_SERIES).peripherals.ld + +# Source files + +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)src$(DELIM)esp_efuse_api.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)src$(DELIM)esp_efuse_utility.c + +# Please note that the following source file depends on `CONFIG_SOC_EFUSE_KEY_PURPOSE_FIELD` and `CONFIG_SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK` +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)src$(DELIM)efuse_controller$(DELIM)keys$(DELIM)without_key_purposes$(DELIM)three_key_blocks$(DELIM)esp_efuse_api_key.c + +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)$(CHIP_SERIES)$(DELIM)esp_efuse_fields.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)$(CHIP_SERIES)$(DELIM)esp_efuse_table.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)$(CHIP_SERIES)$(DELIM)esp_efuse_utility.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)clk_ctrl_os.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)cpu.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)esp_clk.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)hw_random.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)mac_addr.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)periph_ctrl.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)port$(DELIM)$(CHIP_SERIES)$(DELIM)cpu_region_protect.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)port$(DELIM)$(CHIP_SERIES)$(DELIM)esp_clk_tree.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)port$(DELIM)$(CHIP_SERIES)$(DELIM)rtc_clk.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)port$(DELIM)$(CHIP_SERIES)$(DELIM)rtc_time.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)port$(DELIM)esp_clk_tree_common.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)regi2c_ctrl.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_phy$(DELIM)src$(DELIM)phy_init.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_rom$(DELIM)patches$(DELIM)esp_rom_wdt.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)clk.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)system_internal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)$(CHIP_SERIES)$(DELIM)clk_tree_hal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)$(CHIP_SERIES)$(DELIM)efuse_hal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)brownout_hal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)efuse_hal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gpio_hal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)ledc_hal_iram.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)ledc_hal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)rmt_hal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)timer_hal_iram.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)timer_hal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)uart_hal_iram.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)uart_hal.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)log$(DELIM)log_noos.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)log$(DELIM)log.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)gpio_periph.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)ledc_periph.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)rmt_periph.c + +CFLAGS += ${DEFINE_PREFIX}ESP_PLATFORM=1 diff --git a/arch/xtensa/src/esp32/hardware/esp32_aes.h b/arch/xtensa/src/esp32/hardware/esp32_aes.h index e2ed92d93af..af2714597be 100644 --- a/arch/xtensa/src/esp32/hardware/esp32_aes.h +++ b/arch/xtensa/src/esp32/hardware/esp32_aes.h @@ -23,8 +23,6 @@ /* AES acceleration registers */ -#define DR_REG_AES_BASE (0x3ff01000) - #define AES_START_REG ((DR_REG_AES_BASE) + 0x00) #define AES_IDLE_REG ((DR_REG_AES_BASE) + 0x04) #define AES_MODE_REG ((DR_REG_AES_BASE) + 0x08) diff --git a/arch/xtensa/src/esp32/hardware/esp32_soc.h b/arch/xtensa/src/esp32/hardware/esp32_soc.h index 896c9a7afd6..a1f1ad67a5f 100644 --- a/arch/xtensa/src/esp32/hardware/esp32_soc.h +++ b/arch/xtensa/src/esp32/hardware/esp32_soc.h @@ -33,6 +33,8 @@ #include +#include "soc/soc.h" + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -77,113 +79,6 @@ /* Registers Operation */ -#define ETS_UNCACHED_ADDR(addr) (addr) -#define ETS_CACHED_ADDR(addr) (addr) - -/* Write value to register */ - -#define REG_WRITE(_r, _v) (*(volatile uint32_t *)(_r)) = (_v) - -/* Read value from register */ - -#define REG_READ(_r) (*(volatile uint32_t *)(_r)) - -/* Get bit or get bits from register */ - -#define REG_GET_BIT(_r, _b) (*(volatile uint32_t*)(_r) & (_b)) - -/* Set bit or set bits to register */ - -#define REG_SET_BIT(_r, _b) (*(volatile uint32_t*)(_r) |= (_b)) - -/* Clear bit or clear bits of register */ - -#define REG_CLR_BIT(_r, _b) (*(volatile uint32_t*)(_r) &= ~(_b)) - -/* Set bits of register controlled by mask */ - -#define REG_SET_BITS(_r, _b, _m) (*(volatile uint32_t*)(_r) = (*(volatile uint32_t*)(_r) & ~(_m)) | ((_b) & (_m))) - -/* Get field from register, - * used when _f is not left shifted by _f##_S - */ - -#define REG_GET_FIELD(addr, field) ((getreg32(addr) >> (field##_S)) & (field##_V)) - -/* Set field to register, - * used when _f is not left shifted by _f##_S - */ - -#define REG_SET_FIELD(addr, field, val) (modifyreg32((addr), (field##_M), (((uint32_t) val) & (field##_V)) << (field##_S))) - -/* Set field value from a variable, - * used when _f is not left shifted by _f##_S - */ - -#define VALUE_GET_FIELD(_r, _f) (((_r) >> (_f##_S)) & (_f)) - -/* Get field value from a variable, - * used when _f is left shifted by _f##_S - */ - -#define VALUE_GET_FIELD2(_r, _f) (((_r) & (_f))>> (_f##_S)) - -/* Set field value to a variable, - * used when _f is not left shifted by _f##_S - */ - -#define VALUE_SET_FIELD(_r, _f, _v) ((_r)=(((_r) & ~((_f) << (_f##_S)))|((_v)<<(_f##_S)))) - -/* Set field value to a variable, - * used when _f is left shifted by _f##_S - */ - -#define VALUE_SET_FIELD2(_r, _f, _v) ((_r)=(((_r) & ~(_f))|((_v)<<(_f##_S)))) - -/* Generate a value from a field value, - * used when _f is not left shifted by _f##_S - */ - -#define FIELD_TO_VALUE(_f, _v) (((_v)&(_f))<<_f##_S) - -/* Generate a value from a field value, - * used when _f is left shifted by _f##_S - */ - -#define FIELD_TO_VALUE2(_f, _v) (((_v)<<_f##_S) & (_f)) - -/* Read value from register */ - -#define READ_PERI_REG(addr) (*((volatile uint32_t *)ETS_UNCACHED_ADDR(addr))) - -/* Write value to register */ - -#define WRITE_PERI_REG(addr, val) (*((volatile uint32_t *)ETS_UNCACHED_ADDR(addr))) = (uint32_t)(val) - -/* Clear bits of register controlled by mask */ - -#define CLEAR_PERI_REG_MASK(reg, mask) WRITE_PERI_REG((reg), (READ_PERI_REG(reg)&(~(mask)))) - -/* Set bits of register controlled by mask */ - -#define SET_PERI_REG_MASK(reg, mask) WRITE_PERI_REG((reg), (READ_PERI_REG(reg)|(mask))) - -/* Get bits of register controlled by mask */ - -#define GET_PERI_REG_MASK(reg, mask) (READ_PERI_REG(reg) & (mask)) - -/* Get bits of register controlled by highest bit and lowest bit */ - -#define GET_PERI_REG_BITS(reg, hipos,lowpos) ((READ_PERI_REG(reg)>>(lowpos))&((1<<((hipos)-(lowpos)+1))-1)) - -/* Set bits of register controlled by mask and shift */ - -#define SET_PERI_REG_BITS(reg,bit_map,value,shift) (WRITE_PERI_REG((reg),(READ_PERI_REG(reg)&(~((bit_map)<<(shift))))|(((value) & bit_map)<<(shift)) )) - -/* Get field of register */ - -#define GET_PERI_REG_BITS2(reg, mask,shift) ((READ_PERI_REG(reg)>>(shift))&(mask)) - /* Extract the field from the register and shift it to avoid wrong reading */ #define REG_MASK(_reg, _field) (((_reg) & (_field##_M)) >> (_field##_S)) @@ -194,65 +89,15 @@ /* Periheral Clock */ -#define APB_CLK_FREQ_ROM 26 * 1000000 #define CPU_CLK_FREQ_ROM APB_CLK_FREQ_ROM #define CPU_CLK_FREQ APB_CLK_FREQ -#define APB_CLK_FREQ 80 * 1000000 /* Unit: Hz */ -#define REF_CLK_FREQ (1000000) #define UART_CLK_FREQ APB_CLK_FREQ #define MWDT_CLK_FREQ APB_CLK_FREQ -#define TIMER_CLK_FREQ (80000000 >> 4) /* 80MHz divided by 16 */ #define SPI_CLK_DIV 4 #define TICKS_PER_US_ROM 26 /* CPU is 80MHz */ -#define DR_REG_DPORT_BASE 0x3ff00000 -#define DR_REG_UART_BASE 0x3ff40000 -#define DR_REG_SPI1_BASE 0x3ff42000 -#define DR_REG_SPI0_BASE 0x3ff43000 -#define DR_REG_GPIO_BASE 0x3ff44000 -#define DR_REG_GPIO_SD_BASE 0x3ff44f00 -#define DR_REG_FE2_BASE 0x3ff45000 -#define DR_REG_FE_BASE 0x3ff46000 -#define DR_REG_FRC_TIMER_BASE 0x3ff47000 -#define DR_REG_RTCCNTL_BASE 0x3ff48000 -#define DR_REG_RTCIO_BASE 0x3ff48400 -#define DR_REG_SENS_BASE 0x3ff48800 -#define DR_REG_IO_MUX_BASE 0x3ff49000 -#define DR_REG_EFUSE_BASE 0x3ff5a000 -#define DR_REG_RTCMEM0_BASE 0x3ff61000 -#define DR_REG_RTCMEM1_BASE 0x3ff62000 -#define DR_REG_RTCMEM2_BASE 0x3ff63000 -#define DR_REG_HINF_BASE 0x3ff4b000 -#define DR_REG_UHCI1_BASE 0x3ff4c000 -#define DR_REG_I2S_BASE 0x3ff4f000 -#define DR_REG_UART1_BASE 0x3ff50000 -#define DR_REG_BT_BASE 0x3ff51000 -#define DR_REG_I2C_EXT_BASE 0x3ff53000 -#define DR_REG_UHCI0_BASE 0x3ff54000 -#define DR_REG_SLCHOST_BASE 0x3ff55000 -#define DR_REG_RMT_BASE 0x3ff56000 -#define DR_REG_PCNT_BASE 0x3ff57000 -#define DR_REG_SLC_BASE 0x3ff58000 -#define DR_REG_LEDC_BASE 0x3ff59000 -#define DR_REG_EFUSE_BASE 0x3ff5a000 -#define DR_REG_SPI_ENCRYPT_BASE 0x3ff5b000 -#define DR_REG_NRX_BASE 0x3ff5cc00 -#define DR_REG_BB_BASE 0x3ff5d000 -#define DR_REG_PWM_BASE 0x3ff5e000 -#define DR_REG_TIMERGROUP0_BASE 0x3ff5f000 -#define DR_REG_TIMERGROUP1_BASE 0x3ff60000 -#define DR_REG_SPI2_BASE 0x3ff64000 -#define DR_REG_SPI3_BASE 0x3ff65000 -#define DR_REG_I2C1_EXT_BASE 0x3ff67000 -#define DR_REG_SDMMC_BASE 0x3ff68000 -#define DR_REG_EMAC_BASE 0x3ff69000 -#define DR_REG_TWAI_BASE 0x3ff6b000 -#define DR_REG_CAN_BASE DR_REG_TWAI_BASE -#define DR_REG_PWM1_BASE 0x3ff6c000 -#define DR_REG_I2S1_BASE 0x3ff6d000 -#define DR_REG_UART2_BASE 0x3ff6e000 -#define DR_REG_PWM2_BASE 0x3ff6f000 -#define DR_REG_PWM3_BASE 0x3ff70000 +#define DR_REG_TWAI_BASE DR_REG_CAN_BASE + #define PERIPHS_SPI_ENCRYPT_BASEADDR DR_REG_SPI_ENCRYPT_BASE /* Some AHB addresses can be used instead of DPORT addresses @@ -266,108 +111,14 @@ /* Overall memory map */ -#define SOC_DROM_LOW 0x3f400000 -#define SOC_DROM_HIGH 0x3f800000 -#define SOC_DRAM_LOW 0x3ffae000 -#define SOC_DRAM_HIGH 0x40000000 -#define SOC_IROM_LOW 0x400d0000 -#define SOC_IROM_HIGH 0x40400000 -#define SOC_IROM_MASK_LOW 0x40000000 -#define SOC_IROM_MASK_HIGH 0x40064f00 -#define SOC_CACHE_PRO_LOW 0x40070000 -#define SOC_CACHE_PRO_HIGH 0x40078000 -#define SOC_CACHE_APP_LOW 0x40078000 -#define SOC_CACHE_APP_HIGH 0x40080000 -#define SOC_IRAM_LOW 0x40080000 -#define SOC_IRAM_HIGH 0x400a0000 -#define SOC_RTC_IRAM_LOW 0x400c0000 -#define SOC_RTC_IRAM_HIGH 0x400c2000 -#define SOC_RTC_DRAM_LOW 0x3ff80000 -#define SOC_RTC_DRAM_HIGH 0x3ff82000 -#define SOC_RTC_SLOW_LOW 0x50000000 -#define SOC_RTC_SLOW_HIGH 0x50002000 -#define SOC_EXTRAM_DATA_LOW 0x3f800000 -#define SOC_EXTRAM_DATA_HIGH 0x3fc00000 +#define SOC_RTC_SLOW_LOW SOC_RTC_DATA_LOW +#define SOC_RTC_SLOW_HIGH SOC_RTC_DATA_HIGH /* Virtual address 0 */ #define VADDR0_START_ADDR SOC_DROM_LOW #define VADDR0_END_ADDR (SOC_DROM_HIGH - 1) -/* Interrupt hardware source table - * This table is decided by hardware, don't touch this. - */ - -#define ETS_WIFI_MAC_INTR_SOURCE 0 /* Interrupt of Wi-Fi MAC, level */ -#define ETS_WIFI_MAC_NMI_SOURCE 1 /* Interrupt of Wi-Fi MAC, NMI, use if MAC have bug to fix in NMI */ -#define ETS_WIFI_BB_INTR_SOURCE 2 /* Interrupt of Wi-Fi BB, level, we can do some calibartion */ -#define ETS_BT_MAC_INTR_SOURCE 3 /* will be cancelled */ -#define ETS_BT_BB_INTR_SOURCE 4 /* Interrupt of BT BB, level */ -#define ETS_BT_BB_NMI_SOURCE 5 /* Interrupt of BT BB, NMI, use if BB have bug to fix in NMI */ -#define ETS_RWBT_INTR_SOURCE 6 /* Interrupt of RWBT, level */ -#define ETS_RWBLE_INTR_SOURCE 7 /* Interrupt of RWBLE, level */ -#define ETS_RWBT_NMI_SOURCE 8 /* Interrupt of RWBT, NMI, use if RWBT have bug to fix in NMI */ -#define ETS_RWBLE_NMI_SOURCE 9 /* Interrupt of RWBLE, NMI, use if RWBT have bug to fix in NMI */ -#define ETS_SLC0_INTR_SOURCE 10 /* Interrupt of SLC0, level */ -#define ETS_SLC1_INTR_SOURCE 11 /* Interrupt of SLC1, level */ -#define ETS_UHCI0_INTR_SOURCE 12 /* Interrupt of UHCI0, level */ -#define ETS_UHCI1_INTR_SOURCE 13 /* Interrupt of UHCI1, level */ -#define ETS_TG0_T0_LEVEL_INTR_SOURCE 14 /* Interrupt of TIMER_GROUP0, TIMER0, level, we would like use EDGE for timer if permission */ -#define ETS_TG0_T1_LEVEL_INTR_SOURCE 15 /* Interrupt of TIMER_GROUP0, TIMER1, level, we would like use EDGE for timer if permission */ -#define ETS_TG0_WDT_LEVEL_INTR_SOURCE 16 /* Interrupt of TIMER_GROUP0, WATCHDOG, level */ -#define ETS_TG0_LACT_LEVEL_INTR_SOURCE 17 /* Interrupt of TIMER_GROUP0, LACT, level */ -#define ETS_TG1_T0_LEVEL_INTR_SOURCE 18 /* Interrupt of TIMER_GROUP1, TIMER0, level, we would like use EDGE for timer if permission */ -#define ETS_TG1_T1_LEVEL_INTR_SOURCE 19 /* Interrupt of TIMER_GROUP1, TIMER1, level, we would like use EDGE for timer if permission */ -#define ETS_TG1_WDT_LEVEL_INTR_SOURCE 20 /* Interrupt of TIMER_GROUP1, WATCHDOG, level */ -#define ETS_TG1_LACT_LEVEL_INTR_SOURCE 21 /* Interrupt of TIMER_GROUP1, LACT, level */ -#define ETS_GPIO_INTR_SOURCE 22 /* Interrupt of GPIO, level */ -#define ETS_GPIO_NMI_SOURCE 23 /* Interrupt of GPIO, NMI */ -#define ETS_FROM_CPU_INTR0_SOURCE 24 /* Interrupt0 generated from a CPU, level */ -#define ETS_FROM_CPU_INTR1_SOURCE 25 /* Interrupt1 generated from a CPU, level */ -#define ETS_FROM_CPU_INTR2_SOURCE 26 /* Interrupt2 generated from a CPU, level */ -#define ETS_FROM_CPU_INTR3_SOURCE 27 /* Interrupt3 generated from a CPU, level */ -#define ETS_SPI0_INTR_SOURCE 28 /* Interrupt of SPI0, level, SPI0 is for Cache Access, do not use this */ -#define ETS_SPI1_INTR_SOURCE 29 /* Interrupt of SPI1, level, SPI1 is for flash read/write, do not use this */ -#define ETS_SPI2_INTR_SOURCE 30 /* Interrupt of SPI2, level */ -#define ETS_SPI3_INTR_SOURCE 31 /* Interrupt of SPI3, level */ -#define ETS_I2S0_INTR_SOURCE 32 /* Interrupt of I2S0, level */ -#define ETS_I2S1_INTR_SOURCE 33 /* Interrupt of I2S1, level */ -#define ETS_UART0_INTR_SOURCE 34 /* Interrupt of UART0, level */ -#define ETS_UART1_INTR_SOURCE 35 /* Interrupt of UART1, level */ -#define ETS_UART2_INTR_SOURCE 36 /* Interrupt of UART2, level */ -#define ETS_SDIO_HOST_INTR_SOURCE 37 /* Interrupt of SD/SDIO/MMC HOST, level */ -#define ETS_ETH_MAC_INTR_SOURCE 38 /* Interrupt of ethernet mac, level */ -#define ETS_PWM0_INTR_SOURCE 39 /* Interrupt of PWM0, level, Reserved */ -#define ETS_PWM1_INTR_SOURCE 40 /* Interrupt of PWM1, level, Reserved */ -#define ETS_PWM2_INTR_SOURCE 41 /* Interrupt of PWM2, level */ -#define ETS_PWM3_INTR_SOURCE 42 /* Interruot of PWM3, level */ -#define ETS_LEDC_INTR_SOURCE 43 /* Interrupt of LED PWM, level */ -#define ETS_EFUSE_INTR_SOURCE 44 /* Interrupt of efuse, level, not likely to use */ -#define ETS_CAN_INTR_SOURCE 45 /* Interrupt of can, level */ -#define ETS_RTC_CORE_INTR_SOURCE 46 /* Interrupt of rtc core, level, include rtc watchdog */ -#define ETS_RMT_INTR_SOURCE 47 /* Interrupt of remote controller, level */ -#define ETS_PCNT_INTR_SOURCE 48 /* Interrupt of pulse count, level */ -#define ETS_I2C_EXT0_INTR_SOURCE 49 /* Interrupt of I2C controller1, level */ -#define ETS_I2C_EXT1_INTR_SOURCE 50 /* Interrupt of I2C controller0, level */ -#define ETS_RSA_INTR_SOURCE 51 /* Interrupt of RSA accelerator, level */ -#define ETS_SPI1_DMA_INTR_SOURCE 52 /* Interrupt of SPI1 DMA, SPI1 is for flash read/write, do not use this */ -#define ETS_SPI2_DMA_INTR_SOURCE 53 /* Interrupt of SPI2 DMA, level */ -#define ETS_SPI3_DMA_INTR_SOURCE 54 /* Interrupt of SPI3 DMA, level */ -#define ETS_WDT_INTR_SOURCE 55 /* will be cancelled */ -#define ETS_TIMER1_INTR_SOURCE 56 /* will be cancelled */ -#define ETS_TIMER2_INTR_SOURCE 57 /* will be cancelled */ -#define ETS_TG0_T0_EDGE_INTR_SOURCE 58 /* Interrupt of TIMER_GROUP0, TIMER0, EDGE */ -#define ETS_TG0_T1_EDGE_INTR_SOURCE 59 /* Interrupt of TIMER_GROUP0, TIMER1, EDGE */ -#define ETS_TG0_WDT_EDGE_INTR_SOURCE 60 /* Interrupt of TIMER_GROUP0, WATCH DOG, EDGE */ -#define ETS_TG0_LACT_EDGE_INTR_SOURCE 61 /* Interrupt of TIMER_GROUP0, LACT, EDGE */ -#define ETS_TG1_T0_EDGE_INTR_SOURCE 62 /* Interrupt of TIMER_GROUP1, TIMER0, EDGE */ -#define ETS_TG1_T1_EDGE_INTR_SOURCE 63 /* Interrupt of TIMER_GROUP1, TIMER1, EDGE */ -#define ETS_TG1_WDT_EDGE_INTR_SOURCE 64 /* Interrupt of TIMER_GROUP1, WATCHDOG, EDGE */ -#define ETS_TG1_LACT_EDGE_INTR_SOURCE 65 /* Interrupt of TIMER_GROUP0, LACT, EDGE */ -#define ETS_MMU_IA_INTR_SOURCE 66 /* Interrupt of MMU Invalid Access, LEVEL */ -#define ETS_MPU_IA_INTR_SOURCE 67 /* Interrupt of MPU Invalid Access, LEVEL */ -#define ETS_CACHE_IA_INTR_SOURCE 68 /* Interrupt of Cache Invalied Access, LEVEL */ - #define GPIO_STRAP_REG (DR_REG_GPIO_BASE + 0x0038) /* Interrupt cpu using table */ diff --git a/boards/xtensa/esp32/common/scripts/legacy_sections.ld b/boards/xtensa/esp32/common/scripts/legacy_sections.ld index 705b02fbbd5..c61da8a5b55 100644 --- a/boards/xtensa/esp32/common/scripts/legacy_sections.ld +++ b/boards/xtensa/esp32/common/scripts/legacy_sections.ld @@ -112,6 +112,7 @@ SECTIONS *(.wifirxiram .wifirxiram.*) *(.wifi0iram .wifi0iram.*) + *(.wifiorslpiram .wifiorslpiram.*) *(.wifislpiram .wifislpiram.*) *(.wifislprxiram .wifislprxiram.*) *(.phyiram .phyiram.*) @@ -132,15 +133,39 @@ SECTIONS . = ALIGN (8); _sbss = ABSOLUTE(.); _bss_start = ABSOLUTE(.); + *(EXCLUDE_FILE(*libble_app.a *libbt.a *libbtdm_app.a *libnimble.a) .bss EXCLUDE_FILE(*libble_app.a *libbt.a *libbtdm_app.a *libnimble.a) .bss.*) *(.ext_ram.bss*) + *(EXCLUDE_FILE(*libble_app.a *libbt.a *libbtdm_app.a *libnimble.a) COMMON) + . = ALIGN(4); + _bt_controller_bss_start = ABSOLUTE(.); + *libble_app.a:(.bss .bss.*) + . = ALIGN(4); + _bt_controller_bss_end = ABSOLUTE(.); + . = ALIGN(4); + _bt_controller_common_start = ABSOLUTE(.); + *libble_app.a:(COMMON) + . = ALIGN(4); + _bt_controller_common_end = ABSOLUTE(.); + . = ALIGN(4); _bt_bss_start = ABSOLUTE(.); - *libbt.a:(.bss .bss.* COMMON) - . = ALIGN (4); + *libbt.a:(.bss .bss.*) + . = ALIGN(4); _bt_bss_end = ABSOLUTE(.); + . = ALIGN(4); + _bt_common_start = ABSOLUTE(.); + *libbt.a:(COMMON) + . = ALIGN(4); + _bt_common_end = ABSOLUTE(.); + . = ALIGN(4); _btdm_bss_start = ABSOLUTE(.); - *libbtdm_app.a:(.bss .bss.* COMMON) - . = ALIGN (4); + *libbtdm_app.a:(.bss .bss.*) + . = ALIGN(4); _btdm_bss_end = ABSOLUTE(.); + . = ALIGN(4); + _btdm_common_start = ABSOLUTE(.); + *libbtdm_app.a:(COMMON) + . = ALIGN(4); + _btdm_common_end = ABSOLUTE(.); . = ALIGN (8); *(.dynsbss) *(.sbss) @@ -253,6 +278,13 @@ SECTIONS _srodata = ABSOLUTE(.); *(.rodata) *(.rodata.*) +#ifdef CONFIG_ESP32_WIRELESS + *(.rodata_wlog_verbose.*) + *(.rodata_wlog_debug.*) + *(.rodata_wlog_info.*) + *(.rodata_wlog_warning.*) + *(.rodata_wlog_error.*) +#endif *(.irom1.text) /* catch stray ICACHE_RODATA_ATTR */ *(.gnu.linkonce.r.*) *(.rodata1) @@ -288,6 +320,13 @@ SECTIONS *(.xt_except_desc_end) *(.dynamic) *(.gnu.version_d) + + /* Addresses of memory regions reserved via SOC_RESERVE_MEMORY_REGION() */ + + soc_reserved_memory_region_start = ABSOLUTE(.); + KEEP (*(.reserved_memory_address)) + soc_reserved_memory_region_end = ABSOLUTE(.); + _erodata = ABSOLUTE(.); /* Literals are also RO data. */ _lit4_start = ABSOLUTE(.); diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/blewifi/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/blewifi/defconfig index f04801d0ce3..0cf81f77ba9 100644 --- a/boards/xtensa/esp32/esp32-devkitc/configs/blewifi/defconfig +++ b/boards/xtensa/esp32/esp32-devkitc/configs/blewifi/defconfig @@ -38,7 +38,6 @@ CONFIG_ESP32_SPIFLASH_SPIFFS=y CONFIG_ESP32_STORAGE_MTD_SIZE=0x80000 CONFIG_ESP32_UART0=y CONFIG_ESP32_WIFI=y -CONFIG_ESP32_WIFI_BT_COEXIST=y CONFIG_ESP32_WIFI_STATION_SOFTAP=y CONFIG_EXAMPLES_DHCPD=y CONFIG_EXAMPLE_POWER_SAVE_MIN_MODEM=y diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32_ledc.c b/boards/xtensa/esp32/esp32-devkitc/src/esp32_ledc.c index ed858387004..7f424fddb2e 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32_ledc.c +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32_ledc.c @@ -33,7 +33,6 @@ #include -#include "chip.h" #include "esp32_ledc.h" /**************************************************************************** diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32_twai.c b/boards/xtensa/esp32/esp32-devkitc/src/esp32_twai.c index 59cfc6b7876..d464e236d1e 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32_twai.c +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32_twai.c @@ -30,8 +30,6 @@ #include #include -#include "chip.h" - #include "esp32_twai.h" #include "esp32-devkitc.h"