Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native wifi API with ESP32S2 support #3326

Merged
merged 33 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c5b8401
First crack at native wifi API
tannewt Jul 29, 2020
1a6f4e0
Scanning WIP. Need to sort out supervisor memory
tannewt Jul 31, 2020
ddcff85
Add debugging. Scanning doesn't crash but returns no results. Need to…
tannewt Aug 1, 2020
c62ab6e
Add ipaddress
tannewt Aug 5, 2020
cdab5e7
Update to latest idf
tannewt Aug 5, 2020
c53a72d
Fix ipaddress import and parse ipv4 strings
tannewt Aug 6, 2020
3860991
Ping work and start to add socketpool
tannewt Aug 11, 2020
7bdd243
Ping works!
tannewt Aug 11, 2020
c9ece21
SocketPool stubbed out
tannewt Aug 11, 2020
eb2c388
HTTP works with my adafruit_requests
tannewt Aug 12, 2020
430530c
SSL works until it runs out of memory
tannewt Aug 13, 2020
b3a449c
Turn on json and enable socket.close
tannewt Aug 14, 2020
1034cc1
Add espidf module.
tannewt Aug 14, 2020
dcc42f6
Remove debug prints
tannewt Aug 15, 2020
9602ee6
Disable wifi debug logging and memory log
tannewt Aug 18, 2020
eb8b42a
Add basic error handling
tannewt Aug 19, 2020
80b15f6
Add error handling w/temp debug logs
tannewt Aug 20, 2020
701e80a
Make socket reads interruptable
tannewt Aug 21, 2020
da61845
Add missing include
tannewt Aug 21, 2020
69fc872
Fix espidf.MemoryError print
tannewt Aug 25, 2020
f3bc712
Fix SocketPool radio check
tannewt Aug 25, 2020
380cbfb
Add hash to Socket so it can be used in dicts
tannewt Aug 25, 2020
8b71e26
Merge remote-tracking branch 'adafruit/main' into native_wifi
tannewt Aug 25, 2020
6fbeb28
Update translations
tannewt Aug 25, 2020
46dc133
Fix stub and doc builds
tannewt Aug 27, 2020
767ca5c
Merge remote-tracking branch 'adafruit/main' into native_wifi
tannewt Aug 27, 2020
fd6e63d
Hopefully final partition scheme.
tannewt Aug 27, 2020
2b71635
Fix esp-idf requirements
tannewt Aug 27, 2020
5d8ac04
-Os espruino_pico to save space
tannewt Aug 27, 2020
0b94638
Changes based on Dan's feedback
tannewt Sep 3, 2020
96cf60f
Merge remote-tracking branch 'adafruit/main' into native_wifi
tannewt Sep 3, 2020
99f5011
Fix heap without PSRAM. Never set heap_size.
tannewt Sep 9, 2020
f3fc7c1
Add safe mode translation
tannewt Sep 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
# directories to ignore when looking for source files.
exclude_patterns = ["**/build*",
".git",
".env",
".venv",
".direnv",
"docs/autoapi",
Expand Down
36 changes: 34 additions & 2 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-08-30 14:38-0400\n"
"POT-Creation-Date: 2020-09-09 14:33-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -273,7 +273,7 @@ msgstr ""
msgid "A hardware interrupt channel is already in use"
msgstr ""

#: shared-bindings/_bleio/Address.c
#: shared-bindings/_bleio/Address.c shared-bindings/ipaddress/IPv4Address.c
#, c-format
msgid "Address must be %d bytes long"
msgstr ""
Expand Down Expand Up @@ -371,6 +371,10 @@ msgstr ""
msgid "Attempted heap allocation when MicroPython VM not running."
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "Authentication failure"
msgstr ""

#: main.c
msgid "Auto-reload is off.\n"
msgstr ""
Expand Down Expand Up @@ -578,6 +582,10 @@ msgid ""
"boot. Press again to exit safe mode.\n"
msgstr ""

#: supervisor/shared/safe_mode.c
msgid "CircuitPython was unable to allocate the heap.\n"
msgstr ""

#: shared-module/bitbangio/SPI.c
msgid "Clock pin init failed."
msgstr ""
Expand Down Expand Up @@ -1221,6 +1229,10 @@ msgstr ""
msgid "No more timers available on this pin."
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "No network with that ssid"
msgstr ""

#: shared-module/touchio/TouchIn.c
msgid "No pulldown on pin; 1Mohm recommended"
msgstr ""
Expand All @@ -1241,6 +1253,10 @@ msgstr ""
msgid "Nordic Soft Device failure assertion."
msgstr ""

#: shared-bindings/ipaddress/IPv4Address.c shared-bindings/ipaddress/__init__.c
msgid "Not a valid IP string"
msgstr ""

#: ports/nrf/common-hal/_bleio/__init__.c
#: shared-bindings/_bleio/CharacteristicBuffer.c
msgid "Not connected"
Expand Down Expand Up @@ -1285,6 +1301,10 @@ msgid ""
"%d bpp given"
msgstr ""

#: shared-bindings/ipaddress/__init__.c
msgid "Only raw int supported for ip"
msgstr ""

#: shared-bindings/audiobusio/PDMIn.c
msgid "Oversample must be multiple of 8."
msgstr ""
Expand Down Expand Up @@ -1481,6 +1501,10 @@ msgstr ""
msgid "Serializer in use"
msgstr ""

#: shared-bindings/ssl/SSLContext.c
msgid "Server side context cannot have hostname"
msgstr ""

#: shared-bindings/nvm/ByteArray.c
msgid "Slice and value different lengths."
msgstr ""
Expand Down Expand Up @@ -1676,6 +1700,10 @@ msgstr ""
msgid "Unexpected nrfx uuid type"
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "Unknown failure"
msgstr ""

#: ports/nrf/common-hal/_bleio/__init__.c
#, c-format
msgid "Unknown gatt error: 0x%04x"
Expand Down Expand Up @@ -1779,6 +1807,10 @@ msgid ""
"To list built-in modules please do `help(\"modules\")`.\n"
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "WiFi password must be between 8 and 63 characters"
msgstr ""

#: ports/nrf/common-hal/_bleio/PacketBuffer.c
msgid "Writes not supported on Characteristic"
msgstr ""
Expand Down
5 changes: 4 additions & 1 deletion ports/esp32s2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
cmake_minimum_required(VERSION 3.13)

set(ENV{IDF_PATH} ${CMAKE_SOURCE_DIR}/esp-idf)
set(COMPONENTS esptool_py soc driver log main)

# The component list here determines what options we get in menuconfig and what the ninja file
# can build.
set(COMPONENTS esptool_py soc driver log main esp-tls mbedtls esp_event esp_netif esp_wifi lwip wpa_supplicant freertos)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(circuitpython)
Expand Down
37 changes: 31 additions & 6 deletions ports/esp32s2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,19 @@ INC += -Iesp-idf/components/freertos/xtensa/include
INC += -Iesp-idf/components/esp32s2/include
INC += -Iesp-idf/components/xtensa/esp32s2/include
INC += -Iesp-idf/components/esp_common/include
INC += -Iesp-idf/components/esp_event/include
INC += -Iesp-idf/components/esp_netif/include
INC += -Iesp-idf/components/esp_ringbuf/include
INC += -Iesp-idf/components/esp_rom/include
INC += -Iesp-idf/components/esp_wifi/include
INC += -Iesp-idf/components/xtensa/include
INC += -Iesp-idf/components/esp_timer/include
INC += -Iesp-idf/components/mbedtls/mbedtls/include
INC += -Iesp-idf/components/mbedtls/port/include/
INC += -Iesp-idf/components/newlib/platform_include
INC += -Iesp-idf/components/lwip/lwip/src/include
INC += -Iesp-idf/components/lwip/port/esp32/include
INC += -Iesp-idf/components/lwip/include/apps/sntp
INC += -Iesp-idf/components/soc/include
INC += -Iesp-idf/components/soc/src/esp32s2/include
INC += -Iesp-idf/components/soc/soc/include
Expand Down Expand Up @@ -125,11 +134,12 @@ LDFLAGS += -L$(BUILD)/esp-idf/esp-idf/esp32s2 \
-Tesp32s2.peripherals.ld \
-Lesp-idf/components/esp_rom/esp32s2/ld \
-Tesp32s2.rom.ld \
-Tesp32s2.rom.api.ld \
-Tesp32s2.rom.libgcc.ld \
-Tesp32s2.rom.newlib-data.ld \
-Tesp32s2.rom.newlib-funcs.ld \
-Tesp32s2.rom.spiflash.ld
LIBS := -lgcc -lc
LIBS := -lgcc -lc -lstdc++

#

Expand All @@ -156,6 +166,7 @@ SRC_C += \
background.c \
fatfs_port.c \
mphalport.c \
bindings/espidf/__init__.c \
boards/$(BOARD)/board.c \
boards/$(BOARD)/pins.c \
modules/$(CIRCUITPY_MODULE).c \
Expand Down Expand Up @@ -238,20 +249,25 @@ $(BUILD)/esp-idf/partition_table/partition-table.bin: $(BUILD)/esp-idf/config/sd

# run menuconfig
menuconfig: $(BUILD)/esp-idf/config
ninja -C $(BUILD)/esp-idf menuconfig
diff --old-line-format= --unchanged-line-format= sdkconfig.defaults $(BUILD)/esp-idf/sdkconfig > boards/$(BOARD)/sdkconfig || true
$(Q)ninja -C $(BUILD)/esp-idf menuconfig
$(Q)diff --old-line-format= --unchanged-line-format= sdkconfig.defaults $(BUILD)/esp-idf/sdkconfig > boards/$(BOARD)/sdkconfig || true

# qstr builds include headers so we need to make sure they are up to date
$(HEADER_BUILD)/qstr.i.last: | $(BUILD)/esp-idf/config/sdkconfig.h

# Order here matters
ESP_IDF_COMPONENTS_LINK = freertos log esp_system esp32s2 bootloader_support pthread esp_timer vfs spi_flash app_update esp_common esp32s2 heap newlib driver xtensa soc esp_ringbuf #
ESP_IDF_COMPONENTS_LINK = freertos log esp_system esp32s2 bootloader_support pthread esp_timer vfs spi_flash app_update esp_common esp32s2 heap newlib driver xtensa soc esp_ringbuf esp_wifi esp_event wpa_supplicant mbedtls efuse nvs_flash esp_netif lwip esp_rom esp-tls

ESP_IDF_COMPONENTS_INCLUDE = driver freertos log soc

INC += $(foreach component, $(ESP_IDF_COMPONENTS_INCLUDE), -Iesp-idf/components/$(component)/include)

ESP_IDF_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
ESP_IDF_WIFI_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_WIFI_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)

MBEDTLS_COMPONENTS_LINK = crypto tls x509
MBEDTLS_COMPONENTS_LINK_EXPANDED = $(foreach component, $(MBEDTLS_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/mbedtls/mbedtls/library/libmbed$(component).a)

BINARY_BLOBS = esp-idf/components/xtensa/esp32s2/libhal.a
BINARY_WIFI_BLOBS = libcoexist.a libcore.a libespnow.a libmesh.a libnet80211.a libpp.a librtc.a libsmartconfig.a libphy.a
BINARY_BLOBS += $(addprefix esp-idf/components/esp_wifi/lib/esp32s2/, $(BINARY_WIFI_BLOBS))
Expand All @@ -265,18 +281,27 @@ all: $(BUILD)/firmware.bin $(BUILD)/firmware.uf2

.PHONY: esp-idf-stamp
esp-idf-stamp: $(BUILD)/esp-idf/config/sdkconfig.h
ninja -C $(BUILD)/esp-idf \
$(Q)ninja -C $(BUILD)/esp-idf \
bootloader/bootloader.bin \
esp-idf/bootloader_support/libbootloader_support.a \
esp-idf/esp-tls/libesp-tls.a \
esp-idf/esp32s2/ld/esp32s2.project.ld \
esp-idf/esp_event/libesp_event.a \
esp-idf/esp_netif/libesp_netif.a \
esp-idf/esp_rom/libesp_rom.a \
esp-idf/esp_system/libesp_system.a \
esp-idf/esp_wifi/libesp_wifi.a \
esp-idf/lwip/liblwip.a \
esp-idf/nvs_flash/libnvs_flash.a \
esp-idf/wpa_supplicant/libwpa_supplicant.a \
esp-idf/mbedtls/libmbedtls.a \
esp-idf/freertos/libfreertos.a \
esp-idf/log/liblog.a \
esp-idf/xtensa/libxtensa.a

$(BUILD)/firmware.elf: $(OBJ) | esp-idf-stamp
$(STEPECHO) "LINK $@"
$(Q)$(CC) -o $@ $(LDFLAGS) $^ $(ESP_IDF_COMPONENTS_EXPANDED) $(BINARY_BLOBS) build-$(BOARD)/esp-idf/esp-idf/newlib/libnewlib.a -u newlib_include_pthread_impl -Wl,--start-group $(LIBS) -Wl,--end-group
$(Q)$(CC) -o $@ $(LDFLAGS) $^ -Wl,--start-group $(ESP_IDF_COMPONENTS_EXPANDED) $(BINARY_BLOBS) $(MBEDTLS_COMPONENTS_LINK_EXPANDED) build-$(BOARD)/esp-idf/esp-idf/newlib/libnewlib.a -Wl,--end-group -u newlib_include_pthread_impl -Wl,--start-group $(LIBS) -Wl,--end-group build-$(BOARD)/esp-idf/esp-idf/pthread/libpthread.a -u __cxx_fatal_exception
# $(Q)$(SIZE) $@ | $(PYTHON3) $(TOP)/tools/build_memory_info.py $(BUILD)/esp-idf/esp-idf/esp32s2/esp32s2_out.ld

$(BUILD)/circuitpython-firmware.bin: $(BUILD)/firmware.elf
Expand Down
110 changes: 110 additions & 0 deletions ports/esp32s2/bindings/espidf/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "py/obj.h"
#include "py/runtime.h"

#include "bindings/espidf/__init__.h"

#include "esp-idf/components/heap/include/esp_heap_caps.h"

//| """Direct access to a few ESP-IDF details. This module *should not* include any functionality
//| that could be implemented by other frameworks. It should only include ESP-IDF specific
//| things."""

//| def heap_caps_get_total_size() -> int:
//| """Return the total size of the ESP-IDF, which includes the CircuitPython heap."""
//| ...
//|

STATIC mp_obj_t espidf_heap_caps_get_total_size(void) {
return MP_OBJ_NEW_SMALL_INT(heap_caps_get_total_size(MALLOC_CAP_8BIT));
}
MP_DEFINE_CONST_FUN_OBJ_0(espidf_heap_caps_get_total_size_obj, espidf_heap_caps_get_total_size);

//| def heap_caps_get_free_size() -> int:
//| """Return total free memory in the ESP-IDF heap."""
//| ...
//|

STATIC mp_obj_t espidf_heap_caps_get_free_size(void) {
return MP_OBJ_NEW_SMALL_INT(heap_caps_get_free_size(MALLOC_CAP_8BIT));
}
MP_DEFINE_CONST_FUN_OBJ_0(espidf_heap_caps_get_free_size_obj, espidf_heap_caps_get_free_size);

//| def heap_caps_get_largest_free_block() -> int:
//| """Return the size of largest free memory block in the ESP-IDF heap."""
//| ...
//|

STATIC mp_obj_t espidf_heap_caps_get_largest_free_block(void) {
return MP_OBJ_NEW_SMALL_INT(heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
}
MP_DEFINE_CONST_FUN_OBJ_0(espidf_heap_caps_get_largest_free_block_obj, espidf_heap_caps_get_largest_free_block);

//| class MemoryError(MemoryError):
//| """Raised when an ESP IDF memory allocation fails."""
//| ...
//|
NORETURN void mp_raise_espidf_MemoryError(void) {
nlr_raise(mp_obj_new_exception(&mp_type_espidf_MemoryError));
}

void espidf_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
bool is_subclass = kind & PRINT_EXC_SUBCLASS;
if (!is_subclass && (k == PRINT_EXC)) {
mp_print_str(print, qstr_str(MP_OBJ_QSTR_VALUE(MP_ROM_QSTR(MP_QSTR_espidf))));
mp_print_str(print, ".");
}
mp_obj_exception_print(print, o_in, kind);
}

const mp_obj_type_t mp_type_espidf_MemoryError = {
{ &mp_type_type },
.name = MP_QSTR_MemoryError,
.print = espidf_exception_print,
.make_new = mp_obj_exception_make_new,
.attr = mp_obj_exception_attr,
.parent = &mp_type_MemoryError,
};

STATIC const mp_rom_map_elem_t espidf_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_espidf) },

{ MP_ROM_QSTR(MP_QSTR_heap_caps_get_total_size), MP_ROM_PTR(&espidf_heap_caps_get_total_size_obj)},
{ MP_ROM_QSTR(MP_QSTR_heap_caps_get_free_size), MP_ROM_PTR(&espidf_heap_caps_get_free_size_obj)},
{ MP_ROM_QSTR(MP_QSTR_heap_caps_get_largest_free_block), MP_ROM_PTR(&espidf_heap_caps_get_largest_free_block_obj)},

{ MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_espidf_MemoryError) },
};

STATIC MP_DEFINE_CONST_DICT(espidf_module_globals, espidf_module_globals_table);

const mp_obj_module_t espidf_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&espidf_module_globals,
};
34 changes: 34 additions & 0 deletions ports/esp32s2/bindings/espidf/__init__.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_ESP32S2_BINDINGS_ESPIDF___INIT___H
#define MICROPY_INCLUDED_ESP32S2_BINDINGS_ESPIDF___INIT___H

extern const mp_obj_type_t mp_type_espidf_MemoryError;

NORETURN void mp_raise_espidf_MemoryError(void);

#endif // MICROPY_INCLUDED_ESP32S2_BINDINGS_ESPIDF___INIT___H
Loading