xtensa/esp32: Add ESP32 WiFi adapter and driver

This commit is contained in:
Dong Heng 2020-08-31 15:39:05 +08:00 committed by Masayuki Ishikawa
parent 0345b1edf7
commit a0b84ae53e
12 changed files with 6396 additions and 2 deletions

View File

@ -18,6 +18,12 @@ config ARCH_CHIP_ESP32
select ARCH_HAVE_SDRAM
select ARCH_HAVE_RESET
select ARCH_TOOLCHAIN_GNU
select LIBC_ARCH_MEMCPY
select LIBC_ARCH_MEMCHR
select LIBC_ARCH_MEMCMP
select LIBC_ARCH_MEMCCMP
select LIBC_ARCH_MEMMOVE
select LIBC_ARCH_MEMSET
---help---
The ESP32 is a dual-core system from Espressif with two Harvard
architecture Xtensa LX6 CPUs. All embedded memory, external memory

View File

@ -256,6 +256,9 @@ config ESP32_WIRELESS
bool "Wireless"
default n
depends on EXPERIMENTAL
select NET
select ARCH_PHY_INTERRUPT
select ESP32_RNG
---help---
No yet implemented
@ -628,4 +631,38 @@ config ESP32_ETH_PHY_ADDR
endmenu # ESP32_EMAC
menu "WiFi configuration"
depends on ESP32_WIRELESS
config ESP32_WIFI_CONNECT_TIMEOUT
int "Connect timeout by second"
default 10
help
Max waiting time of connecting to AP.
config ESP32_WIFI_SAVE_PARAM
bool "Save WiFi Parameters"
default n
help
If you enable this option, WiFi adapter parameters will be saved
into the file system instead of computing them each time.
These parameters mainly contains:
- SSID
- Password
- BSSID
- PMK(compute when connecting)
- Author mode
- MAC address
- WiFi hardware configuration parameters
config ESP32_WIFI_FS_MOUNTPT
string "Save WiFi Parameters"
default "/mnt/esp/wifi"
depends on ESP32_WIFI_SAVE_PARAM
help
Mount point of WiFi storage file system.
endmenu # ESP32_WIRELESS
endif # ARCH_CHIP_ESP32

View File

@ -150,8 +150,10 @@ CHIP_CSRCS += esp32_modtext.c
CMN_ASRCS += xtensa_loadstore.S
endif
ifeq ($(CONFIG_ESP32_WIRELESS),y)
WIRELESS_DRV_UNPACK = esp-wireless-drivers-3rdparty
WIRELESS_DRV_ZIP = master.zip
WIRELESS_DRV_ID = 5cb6561
WIRELESS_DRV_ZIP = $(WIRELESS_DRV_ID).zip
WIRELESS_DRV_URL = https://github.com/espressif/esp-wireless-drivers-3rdparty/archive
$(WIRELESS_DRV_ZIP):
@ -161,7 +163,7 @@ $(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)-master chip/$(WIRELESS_DRV_UNPACK)
$(Q) mv chip/$(WIRELESS_DRV_UNPACK)-$(WIRELESS_DRV_ID)* chip/$(WIRELESS_DRV_UNPACK)
$(Q) touch chip/$(WIRELESS_DRV_UNPACK)
context:: chip/$(WIRELESS_DRV_UNPACK)
@ -170,3 +172,14 @@ clean_context::
$(call DELFILE, chip/$(WIRELESS_DRV_ZIP))
$(call DELDIR, chip/$(WIRELESS_DRV_UNPACK))
INCLUDES += $(shell $(INCDIR) "$(CC)" $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)esp-wireless-drivers-3rdparty$(DELIM)include)
CHIP_CSRCS += esp32_wlan.c esp32_wifi_adapter.c
EXTRA_LIBPATHS += -L $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)esp-wireless-drivers-3rdparty$(DELIM)libs
EXTRA_LIBS += -lcore -lrtc -lnet80211 -lpp -lsmartconfig -lcoexist -lespnow -lphy -lwpa_supplicant
# Due to some Wi-Fi related libraries, the option is need to avoid linking too much
# unused functions.
LDFLAGS += --gc-sections
endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,223 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_wifi_adapter.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __ARCH_XTENSA_SRC_ESP32_ESP32_WIFI_ADAPTER_H
#define __ARCH_XTENSA_SRC_ESP32_ESP32_WIFI_ADAPTER_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* WiFi event ID */
enum wifi_adpt_evt_e
{
WIFI_ADPT_EVT_STA_START = 0,
WIFI_ADPT_EVT_STA_CONNECT,
WIFI_ADPT_EVT_STA_DISCONNECT,
WIFI_ADPT_EVT_STA_AUTHMODE_CHANGE,
WIFI_ADPT_EVT_STA_STOP,
WIFI_ADPT_EVT_MAX,
};
/* WiFi event callback function */
typedef void (*wifi_evt_cb_t)(void *p);
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: esp_wifi_adapter_init
*
* Description:
* Initialize ESP32 WiFi adapter
*
* Input Parameters:
* None
*
* Returned Value:
* 0 if success or -1 if fail
*
****************************************************************************/
int esp_wifi_adapter_init(void);
/****************************************************************************
* Name: esp_wifi_notify_subscribe
*
* Description:
* Enable event notification
*
* Input Parameters:
* pid - Task PID
* event - Signal event data pointer
*
* Returned Value:
* 0 if success or -1 if fail
*
****************************************************************************/
int esp_wifi_notify_subscribe(pid_t pid, FAR struct sigevent *event);
/****************************************************************************
* Name: esp_wifi_sta_send_data
*
* Description:
* Use WiFi station interface to send 802.3 frame
*
* Input Parameters:
* pbuf - Packet buffer pointer
* len - Packet length
*
* Returned Value:
* 0 if success or others if fail
*
****************************************************************************/
int esp_wifi_sta_send_data(void *pbuf, uint32_t len);
/****************************************************************************
* Name: esp_wifi_sta_register_recv_cb
*
* Description:
* Regitser WiFi receive packet callback function
*
* Input Parameters:
* input_cb - Receive callback function
*
* Returned Value:
* 0 if success or others if fail
*
****************************************************************************/
int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer,
uint16_t len,
void *eb));
/****************************************************************************
* Name: esp_wifi_sta_read_mac
*
* Description:
* Read station interface MAC address from efuse
*
* Input Parameters:
* mac - MAC address buffer pointer
*
* Returned Value:
* 0 if success or -1 if fail
*
****************************************************************************/
int esp_wifi_sta_read_mac(uint8_t *mac);
/****************************************************************************
* Name: esp_wifi_free_eb
*
* Description:
* Free WiFi receive callback input eb pointer
*
* Input Parameters:
* eb - WiFi receive callback input eb pointer
*
* Returned Value:
* None
*
****************************************************************************/
void esp_wifi_free_eb(void *eb);
/****************************************************************************
* Name: esp_wifi_set_password
*
* Description:
* Set WiFi password
*
* Input Parameters:
* pdata - Password buffer pointer
* len - Password length
*
* Returned Value:
* 0 if success or -1 if fail
*
****************************************************************************/
int esp_wifi_set_password(const uint8_t *pdata, uint8_t len);
/****************************************************************************
* Name: esp_wifi_set_ssid
*
* Description:
* Set WiFi SSID
*
* Input Parameters:
* pdata - SSID buffer pointer
* len - SSID length
*
* Returned Value:
* 0 if success or -1 if fail
*
****************************************************************************/
int esp_wifi_set_ssid(const uint8_t *pdata, uint8_t len);
/****************************************************************************
* Name: esp_wifi_connect_internal
*
* Description:
* Trigger WiFi connection action
*
* Input Parameters:
* None
*
* Returned Value:
* 0 if success or -1 if fail
*
****************************************************************************/
int esp_wifi_connect_internal(void);
#ifdef __cplusplus
}
#endif
#undef EXTERN
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_XTENSA_SRC_ESP32_ESP32_WIFI_ADAPTER_H */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,72 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_wlan.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __ARCH_XTENSA_SRC_ESP32_ESP32_WLAN_H
#define __ARCH_XTENSA_SRC_ESP32_ESP32_WLAN_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
#ifdef CONFIG_ESP32_WIRELESS
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: esp32_wlan_initialize
*
* Description:
* Initialize the esp32 wlan driver
*
* Input Parameters:
* None
*
* Returned Value:
* OK on success; Negated errno on failure.
*
* Assumptions:
*
****************************************************************************/
int esp32_wlan_initialize(void);
#endif /* CONFIG_ESP32_SPI */
#ifdef __cplusplus
}
#endif
#undef EXTERN
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_XTENSA_SRC_ESP32_ESP32_WLAN_H */

View File

@ -1193,6 +1193,31 @@
#define DPORT_WIFI_CLK_EN_V 0xFFFFFFFF
#define DPORT_WIFI_CLK_EN_S 0
/* Mask for all Wifi clock bits - 1, 2, 10 */
#define DPORT_WIFI_CLK_WIFI_EN 0x00000406
#define DPORT_WIFI_CLK_WIFI_EN_M ((DPORT_WIFI_CLK_WIFI_EN_V)<<(DPORT_WIFI_CLK_WIFI_EN_S))
#define DPORT_WIFI_CLK_WIFI_EN_V 0x406
#define DPORT_WIFI_CLK_WIFI_EN_S 0
/* Mask for all Bluetooth clock bits - 11, 16, 17 */
#define DPORT_WIFI_CLK_BT_EN 0x61
#define DPORT_WIFI_CLK_BT_EN_M ((DPORT_WIFI_CLK_BT_EN_V)<<(DPORT_WIFI_CLK_BT_EN_S))
#define DPORT_WIFI_CLK_BT_EN_V 0x61
#define DPORT_WIFI_CLK_BT_EN_S 11
/* Mask for clock bits used by both WIFI and Bluetooth */
#define DPORT_WIFI_CLK_WIFI_BT_COMMON_M 0x000003c9
/* bluetooth baseband bit11 */
#define DPORT_BT_BASEBAND_EN BIT(11)
/* bluetooth LC bit16 and bit17 */
#define DPORT_BT_LC_EN (BIT(16)|BIT(17))
#define DPORT_WIFI_RST_EN_REG (DR_REG_DPORT_BASE + 0x0D0)
/* DPORT_WIFI_RST : R/W ;bitpos:[31:0] ;default: 32'h0 ; */

View File

@ -0,0 +1,82 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
# CONFIG_NSH_CMDPARMS is not set
CONFIG_ARCH="xtensa"
CONFIG_ARCH_BOARD="esp32-core"
CONFIG_ARCH_BOARD_ESP32CORE=y
CONFIG_ARCH_CHIP="esp32"
CONFIG_ARCH_CHIP_ESP32=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_DEBUG_ASSERTIONS=y
CONFIG_DEBUG_ERROR=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_WARN=y
CONFIG_DEBUG_WIRELESS=y
CONFIG_DEBUG_WIRELESS_ERROR=y
CONFIG_DEBUG_WIRELESS_WARN=y
CONFIG_DRIVERS_IEEE80211=y
CONFIG_DRIVERS_WIRELESS=y
CONFIG_ESP32_MTD_SIZE=0x80000
CONFIG_ESP32_SPIFLASH=y
CONFIG_ESP32_UART0=y
CONFIG_ESP32_WIFI_SAVE_PARAM=y
CONFIG_ESP32_WIRELESS=y
CONFIG_EXPERIMENTAL=y
CONFIG_FS_PROCFS=y
CONFIG_FS_SPIFFS=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INTELHEX_BINARY=y
CONFIG_MAX_TASKS=16
CONFIG_MM_REGIONS=2
CONFIG_NETDB_DNSCLIENT=y
CONFIG_NETDEV_LATEINIT=y
CONFIG_NETDEV_PHY_IOCTL=y
CONFIG_NETDEV_WIRELESS_IOCTL=y
CONFIG_NETUTILS_DHCPC=y
CONFIG_NET_BROADCAST=y
CONFIG_NET_ICMP=y
CONFIG_NET_ICMP_SOCKET=y
CONFIG_NET_SOCKOPTS=y
CONFIG_NET_TCP=y
CONFIG_NET_UDP=y
CONFIG_NFILE_DESCRIPTORS=8
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_PTHREAD_MUTEX_TYPES=y
CONFIG_RAM_SIZE=114688
CONFIG_RAM_START=0x20000000
CONFIG_RAW_BINARY=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_SDCLONE_DISABLE=y
CONFIG_SIG_DEFAULT=y
CONFIG_SPI=y
CONFIG_SPIFFS_NAME_MAX=48
CONFIG_START_DAY=6
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2011
CONFIG_SYSTEM_NSH=y
CONFIG_SYSTEM_PING=y
CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_USER_ENTRYPOINT="nsh_main"
CONFIG_WIRELESS=y
CONFIG_WIRELESS_WAPI=y
CONFIG_WIRELESS_WAPI_CMDTOOL=y
CONFIG_WIRELESS_WAPI_STACKSIZE=4096

View File

@ -59,6 +59,8 @@ SECTIONS
*libpp.a:(.literal .text .literal.* .text.*)
*libhal.a:(.literal .text .literal.* .text.*)
*libarch.a:esp32_spiflash.*(.literal .text .literal.* .text.*)
*(.wifirxiram .wifirxiram.*)
*(.wifirxiram .wifi0iram.*)
_iram_text_end = ABSOLUTE(.);
/* Module text area starts at the end of iram0_0_seg */

View File

@ -1850,3 +1850,6 @@ PROVIDE ( g_rom_spiflash_dummy_len_plus = 0x3ffae290 );
PROVIDE ( esp_rom_spiflash_read_user_cmd = 0x400621b0 );
PROVIDE ( esp_rom_spiflash_write_encrypted_enable = 0x40062df4 );
PROVIDE ( esp_rom_spiflash_prepare_encrypted_data = 0x40062e1c );
PROVIDE ( esp_rom_printf = ets_printf );
PROVIDE ( esp_rom_delay_us = ets_delay_us );

View File

@ -39,6 +39,12 @@
#include <nuttx/config.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <syslog.h>
@ -47,9 +53,14 @@
#ifdef CONFIG_TIMER
# include <nuttx/timers/timer.h>
#endif
#include <syslog.h>
#include <sys/errno.h>
#include "esp32-core.h"
#include "esp32_wlan.h"
#include "esp32_spiflash.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -88,6 +99,38 @@
*
****************************************************************************/
#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM
static int esp32_init_wifi_storage(void)
{
int ret;
const char *path = "/dev/mtdblock1";
FAR struct mtd_dev_s *mtd_part;
mtd_part = esp32_spiflash_alloc_mtdpart();
if (!mtd_part)
{
syslog(LOG_ERR, "ERROR: Failed to alloc MTD partition of SPI Flash\n");
return -1;
}
ret = register_mtddriver(path, mtd_part, 0777, NULL);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: Failed to regitser MTD: %d\n", ret);
return -1;
}
ret = mount(path, CONFIG_ESP32_WIFI_FS_MOUNTPT, "spiffs", 0, NULL);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", errno);
return ret;
}
return 0;
}
#endif
int esp32_bringup(void)
{
int ret;
@ -113,6 +156,33 @@ int esp32_bringup(void)
#ifdef CONFIG_ESP32_SPIFLASH
ret = esp32_spiflash_init();
if (ret)
{
syslog(LOG_ERR, "ERROR: Failed to initialize SPI Flash\n");
return ret;
}
#endif
#ifdef CONFIG_ESP32_WIRELESS
#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM
ret = esp32_init_wifi_storage();
if (ret)
{
syslog(LOG_ERR, "ERROR: Failed to initialize WiFi storage\n");
return ret;
}
#endif
#ifdef CONFIG_NET
ret = esp32_wlan_initialize();
if (ret)
{
syslog(LOG_ERR, "ERROR: Failed to initialize WiFi\n");
return ret;
}
#endif
#endif
#ifdef CONFIG_TIMER