From d9baa18411d07e4ebc2a095c92f4b11ed0be0e1a Mon Sep 17 00:00:00 2001 From: Seon Rozenblum Date: Fri, 19 Mar 2021 18:48:34 +1100 Subject: [PATCH 1/4] Renamed TINYPICO board to UM_TINYPICO (as I have new UM_XXX boards coming) Added default hardware SPI 0 pins in mpconfigboard.h Added CONFIG_LWIP_LOCAL_HOSTNAME in sdkconfig.board --- ports/esp32/boards/TINYPICO/mpconfigboard.h | 2 -- ports/esp32/boards/{TINYPICO => UM_TINYPICO}/manifest.py | 0 .../boards/{TINYPICO => UM_TINYPICO}/modules/dotstar.py | 0 .../boards/{TINYPICO => UM_TINYPICO}/modules/tinypico.py | 0 .../boards/{TINYPICO => UM_TINYPICO}/mpconfigboard.cmake | 0 ports/esp32/boards/UM_TINYPICO/mpconfigboard.h | 6 ++++++ .../esp32/boards/{TINYPICO => UM_TINYPICO}/sdkconfig.board | 1 + 7 files changed, 7 insertions(+), 2 deletions(-) delete mode 100644 ports/esp32/boards/TINYPICO/mpconfigboard.h rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/manifest.py (100%) rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/modules/dotstar.py (100%) rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/modules/tinypico.py (100%) rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/mpconfigboard.cmake (100%) create mode 100644 ports/esp32/boards/UM_TINYPICO/mpconfigboard.h rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/sdkconfig.board (72%) diff --git a/ports/esp32/boards/TINYPICO/mpconfigboard.h b/ports/esp32/boards/TINYPICO/mpconfigboard.h deleted file mode 100644 index e63f43ed25a2f..0000000000000 --- a/ports/esp32/boards/TINYPICO/mpconfigboard.h +++ /dev/null @@ -1,2 +0,0 @@ -#define MICROPY_HW_BOARD_NAME "TinyPICO" -#define MICROPY_HW_MCU_NAME "ESP32-PICO-D4" diff --git a/ports/esp32/boards/TINYPICO/manifest.py b/ports/esp32/boards/UM_TINYPICO/manifest.py similarity index 100% rename from ports/esp32/boards/TINYPICO/manifest.py rename to ports/esp32/boards/UM_TINYPICO/manifest.py diff --git a/ports/esp32/boards/TINYPICO/modules/dotstar.py b/ports/esp32/boards/UM_TINYPICO/modules/dotstar.py similarity index 100% rename from ports/esp32/boards/TINYPICO/modules/dotstar.py rename to ports/esp32/boards/UM_TINYPICO/modules/dotstar.py diff --git a/ports/esp32/boards/TINYPICO/modules/tinypico.py b/ports/esp32/boards/UM_TINYPICO/modules/tinypico.py similarity index 100% rename from ports/esp32/boards/TINYPICO/modules/tinypico.py rename to ports/esp32/boards/UM_TINYPICO/modules/tinypico.py diff --git a/ports/esp32/boards/TINYPICO/mpconfigboard.cmake b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.cmake similarity index 100% rename from ports/esp32/boards/TINYPICO/mpconfigboard.cmake rename to ports/esp32/boards/UM_TINYPICO/mpconfigboard.cmake diff --git a/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h new file mode 100644 index 0000000000000..05ee5eaf72a5f --- /dev/null +++ b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h @@ -0,0 +1,6 @@ +#define MICROPY_HW_BOARD_NAME "TinyPICO" +#define MICROPY_HW_MCU_NAME "ESP32-PICO-D4" + +#define MICROPY_HW_SPI1_SCK (18) +#define MICROPY_HW_SPI1_MOSI (23) +#define MICROPY_HW_SPI1_MISO (19) diff --git a/ports/esp32/boards/TINYPICO/sdkconfig.board b/ports/esp32/boards/UM_TINYPICO/sdkconfig.board similarity index 72% rename from ports/esp32/boards/TINYPICO/sdkconfig.board rename to ports/esp32/boards/UM_TINYPICO/sdkconfig.board index dc2c23f674e84..766419c7f81a3 100644 --- a/ports/esp32/boards/TINYPICO/sdkconfig.board +++ b/ports/esp32/boards/UM_TINYPICO/sdkconfig.board @@ -2,3 +2,4 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y CONFIG_SPIRAM_SPEED_80M=y CONFIG_ESP32_REV_MIN_1=y +CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyPICO" From 86f42d6aa6256cc8f59038dc4b05105875c5eab0 Mon Sep 17 00:00:00 2001 From: Seon Rozenblum Date: Mon, 26 Apr 2021 11:33:10 +1000 Subject: [PATCH 2/4] Extra S2 support (And S3 where applicable): - PSRAM - 16MB flash partition - Added correct ADC and DAC pins - Added correct GPIO and IRQ pins - Added S3 area in CMakeLists Added UM_TinyS2 and UM_FeatherS2 boards --- ports/esp32/boards/UM_FEATHERS2/manifest.py | 2 + .../boards/UM_FEATHERS2/modules/dotstar.py | 227 ++++++++++++++++++ .../boards/UM_FEATHERS2/modules/feathers2.py | 98 ++++++++ .../boards/UM_FEATHERS2/mpconfigboard.cmake | 9 + .../esp32/boards/UM_FEATHERS2/mpconfigboard.h | 5 + .../esp32/boards/UM_FEATHERS2/sdkconfig.board | 16 ++ ports/esp32/boards/UM_TINYS2/manifest.py | 2 + .../esp32/boards/UM_TINYS2/modules/tinys2.py | 79 ++++++ .../boards/UM_TINYS2/mpconfigboard.cmake | 8 + ports/esp32/boards/UM_TINYS2/mpconfigboard.h | 9 + ports/esp32/boards/UM_TINYS2/sdkconfig.board | 6 + ports/esp32/boards/sdkconfig.spiram_sx | 15 ++ ports/esp32/machine_adc.c | 18 +- ports/esp32/machine_dac.c | 8 +- ports/esp32/machine_pin.c | 155 ++++++++++-- ports/esp32/machine_touchpad.c | 23 ++ ports/esp32/main.c | 22 ++ ports/esp32/main/CMakeLists.txt | 3 + ports/esp32/modmachine.c | 6 + ports/esp32/mphalport.c | 7 + ports/esp32/partitions-16.csv | 8 + 21 files changed, 706 insertions(+), 20 deletions(-) create mode 100644 ports/esp32/boards/UM_FEATHERS2/manifest.py create mode 100644 ports/esp32/boards/UM_FEATHERS2/modules/dotstar.py create mode 100644 ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py create mode 100644 ports/esp32/boards/UM_FEATHERS2/mpconfigboard.cmake create mode 100644 ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h create mode 100644 ports/esp32/boards/UM_FEATHERS2/sdkconfig.board create mode 100644 ports/esp32/boards/UM_TINYS2/manifest.py create mode 100644 ports/esp32/boards/UM_TINYS2/modules/tinys2.py create mode 100644 ports/esp32/boards/UM_TINYS2/mpconfigboard.cmake create mode 100644 ports/esp32/boards/UM_TINYS2/mpconfigboard.h create mode 100644 ports/esp32/boards/UM_TINYS2/sdkconfig.board create mode 100644 ports/esp32/boards/sdkconfig.spiram_sx create mode 100644 ports/esp32/partitions-16.csv diff --git a/ports/esp32/boards/UM_FEATHERS2/manifest.py b/ports/esp32/boards/UM_FEATHERS2/manifest.py new file mode 100644 index 0000000000000..7ae2ed15d9169 --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/manifest.py @@ -0,0 +1,2 @@ +include("$(PORT_DIR)/boards/manifest.py") +freeze("modules") diff --git a/ports/esp32/boards/UM_FEATHERS2/modules/dotstar.py b/ports/esp32/boards/UM_FEATHERS2/modules/dotstar.py new file mode 100644 index 0000000000000..c362eb11de4bd --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/modules/dotstar.py @@ -0,0 +1,227 @@ +# DotStar strip driver for MicroPython +# +# The MIT License (MIT) +# +# Copyright (c) 2016 Damien P. George (original Neopixel object) +# Copyright (c) 2017 Ladyada +# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries +# Copyright (c) 2019 Matt Trentini (porting back to MicroPython) +# +# 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. + +START_HEADER_SIZE = 4 +LED_START = 0b11100000 # Three "1" bits, followed by 5 brightness bits + +# Pixel color order constants +RGB = (0, 1, 2) +RBG = (0, 2, 1) +GRB = (1, 0, 2) +GBR = (1, 2, 0) +BRG = (2, 0, 1) +BGR = (2, 1, 0) + + +class DotStar: + """ + A sequence of dotstars. + + :param SPI spi: The SPI object to write output to. + :param int n: The number of dotstars in the chain + :param float brightness: Brightness of the pixels between 0.0 and 1.0 + :param bool auto_write: True if the dotstars should immediately change when + set. If False, `show` must be called explicitly. + :param tuple pixel_order: Set the pixel order on the strip - different + strips implement this differently. If you send red, and it looks blue + or green on the strip, modify this! It should be one of the values above + + + Example for TinyPICO: + + .. code-block:: python + + from dotstar import DotStar + from machine import Pin, SPI + + spi = SPI(sck=Pin(12), mosi=Pin(13), miso=Pin(18)) # Configure SPI - note: miso is unused + dotstar = DotStar(spi, 1) + dotstar[0] = (128, 0, 0) # Red + """ + + def __init__(self, spi, n, *, brightness=1.0, auto_write=True, pixel_order=BGR): + self._spi = spi + self._n = n + # Supply one extra clock cycle for each two pixels in the strip. + self.end_header_size = n // 16 + if n % 16 != 0: + self.end_header_size += 1 + self._buf = bytearray(n * 4 + START_HEADER_SIZE + self.end_header_size) + self.end_header_index = len(self._buf) - self.end_header_size + self.pixel_order = pixel_order + # Four empty bytes to start. + for i in range(START_HEADER_SIZE): + self._buf[i] = 0x00 + # Mark the beginnings of each pixel. + for i in range(START_HEADER_SIZE, self.end_header_index, 4): + self._buf[i] = 0xFF + # 0xff bytes at the end. + for i in range(self.end_header_index, len(self._buf)): + self._buf[i] = 0xFF + self._brightness = 1.0 + # Set auto_write to False temporarily so brightness setter does _not_ + # call show() while in __init__. + self.auto_write = False + self.brightness = brightness + self.auto_write = auto_write + + def deinit(self): + """Blank out the DotStars and release the resources.""" + self.auto_write = False + for i in range(START_HEADER_SIZE, self.end_header_index): + if i % 4 != 0: + self._buf[i] = 0 + self.show() + if self._spi: + self._spi.deinit() + + def __enter__(self): + return self + + def __exit__(self, exception_type, exception_value, traceback): + self.deinit() + + def __repr__(self): + return "[" + ", ".join([str(x) for x in self]) + "]" + + def _set_item(self, index, value): + """ + value can be one of three things: + a (r,g,b) list/tuple + a (r,g,b, brightness) list/tuple + a single, longer int that contains RGB values, like 0xFFFFFF + brightness, if specified should be a float 0-1 + + Set a pixel value. You can set per-pixel brightness here, if it's not passed it + will use the max value for pixel brightness value, which is a good default. + + Important notes about the per-pixel brightness - it's accomplished by + PWMing the entire output of the LED, and that PWM is at a much + slower clock than the rest of the LEDs. This can cause problems in + Persistence of Vision Applications + """ + + offset = index * 4 + START_HEADER_SIZE + rgb = value + if isinstance(value, int): + rgb = (value >> 16, (value >> 8) & 0xFF, value & 0xFF) + + if len(rgb) == 4: + brightness = value[3] + # Ignore value[3] below. + else: + brightness = 1 + + # LED startframe is three "1" bits, followed by 5 brightness bits + # then 8 bits for each of R, G, and B. The order of those 3 are configurable and + # vary based on hardware + # same as math.ceil(brightness * 31) & 0b00011111 + # Idea from https://www.codeproject.com/Tips/700780/Fast-floor-ceiling-functions + brightness_byte = 32 - int(32 - brightness * 31) & 0b00011111 + self._buf[offset] = brightness_byte | LED_START + self._buf[offset + 1] = rgb[self.pixel_order[0]] + self._buf[offset + 2] = rgb[self.pixel_order[1]] + self._buf[offset + 3] = rgb[self.pixel_order[2]] + + def __setitem__(self, index, val): + if isinstance(index, slice): + start, stop, step = index.indices(self._n) + length = stop - start + if step != 0: + # same as math.ceil(length / step) + # Idea from https://fizzbuzzer.com/implement-a-ceil-function/ + length = (length + step - 1) // step + if len(val) != length: + raise ValueError("Slice and input sequence size do not match.") + for val_i, in_i in enumerate(range(start, stop, step)): + self._set_item(in_i, val[val_i]) + else: + self._set_item(index, val) + + if self.auto_write: + self.show() + + def __getitem__(self, index): + if isinstance(index, slice): + out = [] + for in_i in range(*index.indices(self._n)): + out.append( + tuple(self._buf[in_i * 4 + (3 - i) + START_HEADER_SIZE] for i in range(3)) + ) + return out + if index < 0: + index += len(self) + if index >= self._n or index < 0: + raise IndexError + offset = index * 4 + return tuple(self._buf[offset + (3 - i) + START_HEADER_SIZE] for i in range(3)) + + def __len__(self): + return self._n + + @property + def brightness(self): + """Overall brightness of the pixel""" + return self._brightness + + @brightness.setter + def brightness(self, brightness): + self._brightness = min(max(brightness, 0.0), 1.0) + if self.auto_write: + self.show() + + def fill(self, color): + """Colors all pixels the given ***color***.""" + auto_write = self.auto_write + self.auto_write = False + for i in range(self._n): + self[i] = color + if auto_write: + self.show() + self.auto_write = auto_write + + def show(self): + """Shows the new colors on the pixels themselves if they haven't already + been autowritten. + + The colors may or may not be showing after this function returns because + it may be done asynchronously.""" + # Create a second output buffer if we need to compute brightness + buf = self._buf + if self.brightness < 1.0: + buf = bytearray(self._buf) + # Four empty bytes to start. + for i in range(START_HEADER_SIZE): + buf[i] = 0x00 + for i in range(START_HEADER_SIZE, self.end_header_index): + buf[i] = self._buf[i] if i % 4 == 0 else int(self._buf[i] * self._brightness) + # Four 0xff bytes at the end. + for i in range(self.end_header_index, len(buf)): + buf[i] = 0xFF + + if self._spi: + self._spi.write(buf) diff --git a/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py b/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py new file mode 100644 index 0000000000000..e12e49ead821c --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py @@ -0,0 +1,98 @@ +# FeatherS2 MicroPython Helper Library +# 2021 Seon Rozenblum - Unexpected Maker +# +# Project home: +# https://feathers2.io +# +# 2021-Mar-21 - v0.1 - Initial implementation + +# Import required libraries +from micropython import const +from machine import Pin, SPI, ADC +import machine, time + +# FeatherS2 Hardware Pin Assignments + +# LDO +LDO2 = const(21) + +# APA102 Dotstar pins +DOTSTAR_CLK = const(45) +DOTSTAR_DATA = const(40) + +# SPI +SPI_MOSI = const(35) +SPI_MISO = const(36) +SPI_CLK = const(37) + +# I2C +I2C_SDA = const(38) +I2C_SCL = const(33) + +# DAC +DAC1 = const(17) +DAC2 = const(18) + +# LED & Ambient Light Sensor +LED = const(13) +AMB_LIGHT = const(4) + +# Helper functions + +# LED & Ambient Light Sensor control +def set_led(state): + l = Pin(LED, Pin.OUT) + l.value(state) + +def toggle_led(state): + l = Pin(LED, Pin.OUT) + l.value(not l.value()) + +# Create ADC and set attenuation and return teh ambient light value from the onboard sensor +def get_amb_light(): + adc = ADC(Pin(AMB_LIGHT)) + adc.atten(ADC.ATTN_11DB) + return adc.read() + +# LDO2 power control +# When we manually turn off the second LDO we also set the DotStar DATA and CLK pins to input to +# prevent parasitic power from lighting the LED even with the LDO off, causing current use. +# The DotStar is a beautiful LED, but parasitic power makes it a terrible choice for battery use :( +def set_ldo2_power(state): + """Set the power for the on-board Dostar to allow no current draw when not needed.""" + # Set the power pin to the inverse of state + ldo2 = Pin(LDO2, Pin.OUT) + ldo2.value(state) + + if state: + Pin(DOTSTAR_CLK, Pin.OUT) + Pin(DOTSTAR_DATA, Pin.OUT)# If power is on, set CLK to be output, otherwise input + else: + Pin(DOTSTAR_CLK, Pin.IN) + Pin(DOTSTAR_DATA, Pin.IN)# If power is on, set CLK to be output, otherwise input + + # A small delay to let the IO change state + time.sleep(0.035) + + +# Dotstar rainbow colour wheel +def dotstar_color_wheel(wheel_pos): + """Color wheel to allow for cycling through the rainbow of RGB colors.""" + wheel_pos = wheel_pos % 255 + + if wheel_pos < 85: + return 255 - wheel_pos * 3, 0, wheel_pos * 3 + elif wheel_pos < 170: + wheel_pos -= 85 + return 0, wheel_pos * 3, 255 - wheel_pos * 3 + else: + wheel_pos -= 170 + return wheel_pos * 3, 255 - wheel_pos * 3, 0 + + +# Go into deep sleep but shut down the APA first to save power +# Use this if you want lowest deep sleep current +def go_deepsleep(t): + """Deep sleep helper that also powers down the on-board Dotstar.""" + set_ldo2_power(False) + machine.deepsleep(t) diff --git a/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.cmake b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.cmake new file mode 100644 index 0000000000000..5e570d513bb47 --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.cmake @@ -0,0 +1,9 @@ +set(IDF_TARGET esp32s2) +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/sdkconfig.spiram_sx + boards/sdkconfig.usb + boards/UM_FEATHERS2/sdkconfig.board +) + +set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) \ No newline at end of file diff --git a/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h new file mode 100644 index 0000000000000..c045adccd9682 --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h @@ -0,0 +1,5 @@ +#define MICROPY_HW_BOARD_NAME "FeatherS2" +#define MICROPY_HW_MCU_NAME "ESP32-S2" + +#define MICROPY_PY_BLUETOOTH (0) +#define MICROPY_HW_ENABLE_SDCARD (0) \ No newline at end of file diff --git a/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board new file mode 100644 index 0000000000000..ec94d4f47a52e --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board @@ -0,0 +1,16 @@ +CONFIG_FLASHMODE_QIO=y +CONFIG_ESPTOOLPY_FLASHFREQ_80M=y +CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y +CONFIG_ESPTOOLPY_AFTER_NORESET=y + +CONFIG_SPIRAM_MEMTEST= + +CONFIG_ESPTOOLPY_FLASHSIZE_4MB= +CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-16.csv" +#CONFIG_USB_AND_UART=y + +# LWIP +CONFIG_LWIP_LOCAL_HOSTNAME="UMFeatherS2" +# end of LWIP diff --git a/ports/esp32/boards/UM_TINYS2/manifest.py b/ports/esp32/boards/UM_TINYS2/manifest.py new file mode 100644 index 0000000000000..7ae2ed15d9169 --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/manifest.py @@ -0,0 +1,2 @@ +include("$(PORT_DIR)/boards/manifest.py") +freeze("modules") diff --git a/ports/esp32/boards/UM_TINYS2/modules/tinys2.py b/ports/esp32/boards/UM_TINYS2/modules/tinys2.py new file mode 100644 index 0000000000000..2a8dbd250011f --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/modules/tinys2.py @@ -0,0 +1,79 @@ +# TinyS2 MicroPython Helper Library +# 2021 Seon Rozenblum - Unexpected Maker +# +# Project home: +# https://tinys2.io +# +# 2021-Apr-10 - v0.1 - Initial implementation + +# Import required libraries +from micropython import const +from machine import Pin, SPI, ADC +import machine, time + +# TinyS2 Hardware Pin Assignments + +# Sense Pins +VBUS_SENSE = const(21) +VBAT_SENSE = const(3) + + +# RGB LED Pins +RGB_DATA = const(1) +RGB_PWR = const(2) + +# SPI +SPI_MOSI = const(35) +SPI_MISO = const(36) +SPI_CLK = const(37) + +# I2C +I2C_SDA = const(8) +I2C_SCL = const(9) + +# DAC +DAC1 = const(17) +DAC2 = const(18) + + +# Helper functions +def set_pixel_power(state): + """Enable or Disable power to the onboard NeoPixel to either show colour, or to reduce power fro deep sleep.""" + Pin(RGB_PWR, Pin.OUT).value(state) + +def get_battery_voltage(): + """ + Returns the current battery voltage. If no battery is connected, returns 4.2V which is the charge voltage + This is an approximation only, but useful to detect if the charge state of the battery is getting low. + """ + adc = ADC(Pin(VBAT_SENSE)) # Assign the ADC pin to read + measuredvbat = adc.read() # Read the value + measuredvbat /= 8192 # divide by 8192 as we are using the default ADC voltage range of 0-1V + measuredvbat *= 4.2 # Multiply by 4.2V, our reference voltage + return round(measuredvbat,2) + +def get_vbus_present(): + """Detect if VBUS (5V) power source is present""" + return (Pin(VBUS_SENSE, Pin.IN).value() == 1) + +# Dotstar rainbow colour wheel +def rgb_color_wheel(wheel_pos): + """Color wheel to allow for cycling through the rainbow of RGB colors.""" + wheel_pos = wheel_pos % 255 + + if wheel_pos < 85: + return 255 - wheel_pos * 3, 0, wheel_pos * 3 + elif wheel_pos < 170: + wheel_pos -= 85 + return 0, wheel_pos * 3, 255 - wheel_pos * 3 + else: + wheel_pos -= 170 + return wheel_pos * 3, 255 - wheel_pos * 3, 0 + + +# Go into deep sleep but shut down the APA first to save power +# Use this if you want lowest deep sleep current +def go_deepsleep(t): + """Deep sleep helper that also powers down the on-board Dotstar.""" + set_pixel_power(False) + machine.deepsleep(t) diff --git a/ports/esp32/boards/UM_TINYS2/mpconfigboard.cmake b/ports/esp32/boards/UM_TINYS2/mpconfigboard.cmake new file mode 100644 index 0000000000000..928f9f8fc35d2 --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/mpconfigboard.cmake @@ -0,0 +1,8 @@ +set(IDF_TARGET esp32s2) +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/sdkconfig.spiram_sx + boards/sdkconfig.usb +) + +set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) diff --git a/ports/esp32/boards/UM_TINYS2/mpconfigboard.h b/ports/esp32/boards/UM_TINYS2/mpconfigboard.h new file mode 100644 index 0000000000000..87d266e58c52a --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/mpconfigboard.h @@ -0,0 +1,9 @@ +#define MICROPY_HW_BOARD_NAME "TinyS2" +#define MICROPY_HW_MCU_NAME "ESP32-S2FN4R2" + +#define MICROPY_PY_BLUETOOTH (0) +#define MICROPY_HW_ENABLE_SDCARD (0) + +#define MICROPY_HW_SPI1_MOSI (35) +#define MICROPY_HW_SPI1_MISO (36) +#define MICROPY_HW_SPI1_SCK (37) diff --git a/ports/esp32/boards/UM_TINYS2/sdkconfig.board b/ports/esp32/boards/UM_TINYS2/sdkconfig.board new file mode 100644 index 0000000000000..48b6749c720db --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/sdkconfig.board @@ -0,0 +1,6 @@ +CONFIG_FLASHMODE_QIO=y +CONFIG_ESPTOOLPY_FLASHFREQ_80M=y +CONFIG_USB_AND_UART=y +# LWIP +CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyS2" +# end of LWIP diff --git a/ports/esp32/boards/sdkconfig.spiram_sx b/ports/esp32/boards/sdkconfig.spiram_sx new file mode 100644 index 0000000000000..dd80eca618a6f --- /dev/null +++ b/ports/esp32/boards/sdkconfig.spiram_sx @@ -0,0 +1,15 @@ +# MicroPython on ESP32-S2 and ESP32-PAD1_subscript_3, ESP IDF configuration with SPIRAM support +CONFIG_ESP32S2_SPIRAM_SUPPORT=y +CONFIG_SPIRAM_TYPE_AUTO=y +CONFIG_DEFAULT_PSRAM_CLK_IO=30 +CONFIG_DEFAULT_PSRAM_CS_IO=26 +# CONFIG_SPIRAM_FETCH_INSTRUCTIONS is not set +# CONFIG_SPIRAM_RODATA is not set +CONFIG_SPIRAM_SPEED_80M=y +# CONFIG_SPIRAM_SPEED_40M=y +CONFIG_SPIRAM=y +CONFIG_SPIRAM_BOOT_INIT=y +CONFIG_SPIRAM_IGNORE_NOTFOUND=y +CONFIG_SPIRAM_USE_MEMMAP=y +CONFIG_SPIRAM_MEMTEST=y +# CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is not set diff --git a/ports/esp32/machine_adc.c b/ports/esp32/machine_adc.c index 4c19d5992bd8a..9638597919e13 100644 --- a/ports/esp32/machine_adc.c +++ b/ports/esp32/machine_adc.c @@ -42,7 +42,9 @@ typedef struct _madc_obj_t { adc1_channel_t adc1_id; } madc_obj_t; + STATIC const madc_obj_t madc_obj[] = { + #if CONFIG_IDF_TARGET_ESP32 {{&machine_adc_type}, GPIO_NUM_36, ADC1_CHANNEL_0}, {{&machine_adc_type}, GPIO_NUM_37, ADC1_CHANNEL_1}, {{&machine_adc_type}, GPIO_NUM_38, ADC1_CHANNEL_2}, @@ -51,6 +53,18 @@ STATIC const madc_obj_t madc_obj[] = { {{&machine_adc_type}, GPIO_NUM_33, ADC1_CHANNEL_5}, {{&machine_adc_type}, GPIO_NUM_34, ADC1_CHANNEL_6}, {{&machine_adc_type}, GPIO_NUM_35, ADC1_CHANNEL_7}, + #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 + {{&machine_adc_type}, GPIO_NUM_1, ADC1_CHANNEL_0}, + {{&machine_adc_type}, GPIO_NUM_2, ADC1_CHANNEL_1}, + {{&machine_adc_type}, GPIO_NUM_3, ADC1_CHANNEL_2}, + {{&machine_adc_type}, GPIO_NUM_4, ADC1_CHANNEL_3}, + {{&machine_adc_type}, GPIO_NUM_5, ADC1_CHANNEL_4}, + {{&machine_adc_type}, GPIO_NUM_6, ADC1_CHANNEL_5}, + {{&machine_adc_type}, GPIO_NUM_7, ADC1_CHANNEL_6}, + {{&machine_adc_type}, GPIO_NUM_8, ADC1_CHANNEL_7}, + {{&machine_adc_type}, GPIO_NUM_9, ADC1_CHANNEL_8}, + {{&machine_adc_type}, GPIO_NUM_10, ADC1_CHANNEL_9}, + #endif }; STATIC uint8_t adc_bit_width; @@ -145,7 +159,7 @@ STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) { case ADC_WIDTH_12Bit: adc_bit_width = 12; break; - #elif CONFIG_IDF_TARGET_ESP32S2 + #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 case ADC_WIDTH_BIT_13: adc_bit_width = 13; break; @@ -175,7 +189,7 @@ STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(ADC_WIDTH_10Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(ADC_WIDTH_11Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(ADC_WIDTH_12Bit) }, - #elif CONFIG_IDF_TARGET_ESP32S2 + #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 { MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(ADC_WIDTH_BIT_13) }, #endif }; diff --git a/ports/esp32/machine_dac.c b/ports/esp32/machine_dac.c index 146ef60aa81d8..d7cb0a2f839d3 100644 --- a/ports/esp32/machine_dac.c +++ b/ports/esp32/machine_dac.c @@ -43,12 +43,12 @@ typedef struct _mdac_obj_t { } mdac_obj_t; STATIC const mdac_obj_t mdac_obj[] = { - #if CONFIG_IDF_TARGET_ESP32 - {{&machine_dac_type}, GPIO_NUM_25, DAC_CHANNEL_1}, - {{&machine_dac_type}, GPIO_NUM_26, DAC_CHANNEL_2}, - #else + #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 {{&machine_dac_type}, GPIO_NUM_17, DAC_CHANNEL_1}, {{&machine_dac_type}, GPIO_NUM_18, DAC_CHANNEL_2}, + #else + {{&machine_dac_type}, GPIO_NUM_25, DAC_CHANNEL_1}, + {{&machine_dac_type}, GPIO_NUM_26, DAC_CHANNEL_2}, #endif }; diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index 8dbdd198490b2..26ddf39aefc69 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -56,6 +56,8 @@ typedef struct _machine_pin_irq_obj_t { } machine_pin_irq_obj_t; STATIC const machine_pin_obj_t machine_pin_obj[] = { + #if CONFIG_IDF_TARGET_ESP32 + {{&machine_pin_type}, GPIO_NUM_0}, {{&machine_pin_type}, GPIO_NUM_1}, {{&machine_pin_type}, GPIO_NUM_2}, @@ -78,17 +80,10 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = { {{&machine_pin_type}, GPIO_NUM_19}, {{NULL}, -1}, {{&machine_pin_type}, GPIO_NUM_21}, - #if CONFIG_IDF_TARGET_ESP32 {{&machine_pin_type}, GPIO_NUM_22}, {{&machine_pin_type}, GPIO_NUM_23}, {{NULL}, -1}, {{&machine_pin_type}, GPIO_NUM_25}, - #else - {{NULL}, -1}, - {{NULL}, -1}, - {{NULL}, -1}, - {{NULL}, -1}, - #endif {{&machine_pin_type}, GPIO_NUM_26}, {{&machine_pin_type}, GPIO_NUM_27}, {{NULL}, -1}, @@ -103,6 +98,90 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = { {{&machine_pin_type}, GPIO_NUM_37}, {{&machine_pin_type}, GPIO_NUM_38}, {{&machine_pin_type}, GPIO_NUM_39}, + +#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 + + {{&machine_pin_type}, GPIO_NUM_0}, + {{&machine_pin_type}, GPIO_NUM_1}, + {{&machine_pin_type}, GPIO_NUM_2}, + {{&machine_pin_type}, GPIO_NUM_3}, + {{&machine_pin_type}, GPIO_NUM_4}, + {{&machine_pin_type}, GPIO_NUM_5}, + {{&machine_pin_type}, GPIO_NUM_6}, + {{&machine_pin_type}, GPIO_NUM_7}, + {{&machine_pin_type}, GPIO_NUM_8}, + {{&machine_pin_type}, GPIO_NUM_9}, + {{&machine_pin_type}, GPIO_NUM_10}, + {{&machine_pin_type}, GPIO_NUM_11}, + {{&machine_pin_type}, GPIO_NUM_12}, + {{&machine_pin_type}, GPIO_NUM_13}, + {{&machine_pin_type}, GPIO_NUM_14}, + {{&machine_pin_type}, GPIO_NUM_15}, + {{&machine_pin_type}, GPIO_NUM_16}, + {{&machine_pin_type}, GPIO_NUM_17}, + {{&machine_pin_type}, GPIO_NUM_18}, +#if CONFIG_USB_CDC_ENABLED + {{NULL}, -1}, // 19 is for native USB D- + {{NULL}, -1}, // 20 is for native USB D- +#else + {{&machine_pin_type}, GPIO_NUM_19}, + {{&machine_pin_type}, GPIO_NUM_20}, +#endif + {{&machine_pin_type}, GPIO_NUM_21}, + {{NULL}, -1}, //Not a pin 22}, + {{NULL}, -1}, //Not a pin 23}, + {{NULL}, -1}, //Not a pin 24}, + {{NULL}, -1}, //Not a pin 25}, + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_26} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_27} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_28} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_29} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_30} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_31} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_32} - FLASH/PSRAM + {{&machine_pin_type}, GPIO_NUM_33}, + {{&machine_pin_type}, GPIO_NUM_34}, + {{&machine_pin_type}, GPIO_NUM_35}, + {{&machine_pin_type}, GPIO_NUM_36}, + {{&machine_pin_type}, GPIO_NUM_37}, + {{&machine_pin_type}, GPIO_NUM_38}, + {{&machine_pin_type}, GPIO_NUM_39}, // MTCLK + {{&machine_pin_type}, GPIO_NUM_40}, // MTDO + {{&machine_pin_type}, GPIO_NUM_41}, // MTDI + {{&machine_pin_type}, GPIO_NUM_42}, // MTMS + {{&machine_pin_type}, GPIO_NUM_43}, // U0TXD + {{&machine_pin_type}, GPIO_NUM_44}, // U0RXD + {{&machine_pin_type}, GPIO_NUM_45}, + {{&machine_pin_type}, GPIO_NUM_46}, + +#else + + {{&machine_pin_type}, GPIO_NUM_0}, + {{&machine_pin_type}, GPIO_NUM_1}, + {{&machine_pin_type}, GPIO_NUM_2}, + {{&machine_pin_type}, GPIO_NUM_3}, + {{&machine_pin_type}, GPIO_NUM_4}, + {{&machine_pin_type}, GPIO_NUM_5}, + {{&machine_pin_type}, GPIO_NUM_6}, + {{&machine_pin_type}, GPIO_NUM_7}, + {{&machine_pin_type}, GPIO_NUM_8}, + {{&machine_pin_type}, GPIO_NUM_9}, + {{&machine_pin_type}, GPIO_NUM_10}, + {{&machine_pin_type}, GPIO_NUM_11}, + {{&machine_pin_type}, GPIO_NUM_12}, + {{&machine_pin_type}, GPIO_NUM_13}, + {{&machine_pin_type}, GPIO_NUM_14}, + {{&machine_pin_type}, GPIO_NUM_15}, + {{&machine_pin_type}, GPIO_NUM_16}, + {{&machine_pin_type}, GPIO_NUM_17}, + {{&machine_pin_type}, GPIO_NUM_18}, + {{&machine_pin_type}, GPIO_NUM_19}, + {{&machine_pin_type}, GPIO_NUM_20}, + {{&machine_pin_type}, GPIO_NUM_21}, + {{&machine_pin_type}, GPIO_NUM_22}, + +#endif + }; // forward declaration @@ -399,6 +478,7 @@ const mp_obj_type_t machine_pin_type = { STATIC const mp_obj_type_t machine_pin_irq_type; STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = { +#if CONFIG_IDF_TARGET_ESP32 {{&machine_pin_irq_type}, GPIO_NUM_0}, {{&machine_pin_irq_type}, GPIO_NUM_1}, {{&machine_pin_irq_type}, GPIO_NUM_2}, @@ -421,17 +501,10 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = { {{&machine_pin_irq_type}, GPIO_NUM_19}, {{NULL}, -1}, {{&machine_pin_irq_type}, GPIO_NUM_21}, - #if CONFIG_IDF_TARGET_ESP32 {{&machine_pin_irq_type}, GPIO_NUM_22}, {{&machine_pin_irq_type}, GPIO_NUM_23}, {{NULL}, -1}, {{&machine_pin_irq_type}, GPIO_NUM_25}, - #else - {{NULL}, -1}, - {{NULL}, -1}, - {{NULL}, -1}, - {{NULL}, -1}, - #endif {{&machine_pin_irq_type}, GPIO_NUM_26}, {{&machine_pin_irq_type}, GPIO_NUM_27}, {{NULL}, -1}, @@ -446,6 +519,60 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = { {{&machine_pin_irq_type}, GPIO_NUM_37}, {{&machine_pin_irq_type}, GPIO_NUM_38}, {{&machine_pin_irq_type}, GPIO_NUM_39}, +#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 + {{&machine_pin_irq_type}, GPIO_NUM_0}, + {{&machine_pin_irq_type}, GPIO_NUM_1}, + {{&machine_pin_irq_type}, GPIO_NUM_2}, + {{&machine_pin_irq_type}, GPIO_NUM_3}, + {{&machine_pin_irq_type}, GPIO_NUM_4}, + {{&machine_pin_irq_type}, GPIO_NUM_5}, + {{&machine_pin_irq_type}, GPIO_NUM_6}, + {{&machine_pin_irq_type}, GPIO_NUM_7}, + {{&machine_pin_irq_type}, GPIO_NUM_8}, + {{&machine_pin_irq_type}, GPIO_NUM_9}, + {{&machine_pin_irq_type}, GPIO_NUM_10}, + {{&machine_pin_irq_type}, GPIO_NUM_11}, + {{&machine_pin_irq_type}, GPIO_NUM_12}, + {{&machine_pin_irq_type}, GPIO_NUM_13}, + {{&machine_pin_irq_type}, GPIO_NUM_14}, + {{&machine_pin_irq_type}, GPIO_NUM_15}, + {{&machine_pin_irq_type}, GPIO_NUM_16}, + {{&machine_pin_irq_type}, GPIO_NUM_17}, + {{&machine_pin_irq_type}, GPIO_NUM_18}, + #if CONFIG_USB_CDC_ENABLED + {{NULL}, -1}, // 19 is for native USB D- + {{NULL}, -1}, // 20 is for native USB D- + #else + {{&machine_pin_irq_type}, GPIO_NUM_19}, + {{&machine_pin_irq_type}, GPIO_NUM_20}, + #endif + {{&machine_pin_irq_type}, GPIO_NUM_21}, + {{NULL}, -1}, //Not a pin 22}, + {{NULL}, -1}, //Not a pin 23}, + {{NULL}, -1}, //Not a pin 24}, + {{NULL}, -1}, //Not a pin 25}, + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_26} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_27} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_28} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_29} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_30} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_31} - FLASH/PSRAM + {{NULL}, -1}, //{{&machine_pin_type}, GPIO_NUM_32} - FLASH/PSRAM + {{&machine_pin_irq_type}, GPIO_NUM_33}, + {{&machine_pin_irq_type}, GPIO_NUM_34}, + {{&machine_pin_irq_type}, GPIO_NUM_35}, + {{&machine_pin_irq_type}, GPIO_NUM_36}, + {{&machine_pin_irq_type}, GPIO_NUM_37}, + {{&machine_pin_irq_type}, GPIO_NUM_38}, + {{&machine_pin_irq_type}, GPIO_NUM_39}, + {{&machine_pin_irq_type}, GPIO_NUM_40}, + {{&machine_pin_irq_type}, GPIO_NUM_41}, + {{&machine_pin_irq_type}, GPIO_NUM_42}, + {{&machine_pin_irq_type}, GPIO_NUM_43}, + {{&machine_pin_irq_type}, GPIO_NUM_44}, + {{&machine_pin_irq_type}, GPIO_NUM_45}, + +#endif }; STATIC mp_obj_t machine_pin_irq_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { diff --git a/ports/esp32/machine_touchpad.c b/ports/esp32/machine_touchpad.c index 335157b154902..168ac16d0eef6 100644 --- a/ports/esp32/machine_touchpad.c +++ b/ports/esp32/machine_touchpad.c @@ -31,7 +31,11 @@ #if CONFIG_IDF_TARGET_ESP32 #include "driver/gpio.h" +#if CONFIG_IDF_TARGET_ESP32 #include "driver/touch_pad.h" +#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 +#include "driver/touch_sensor.h" +#endif typedef struct _mtp_obj_t { mp_obj_base_t base; @@ -39,6 +43,7 @@ typedef struct _mtp_obj_t { touch_pad_t touchpad_id; } mtp_obj_t; +#if CONFIG_IDF_TARGET_ESP32 STATIC const mtp_obj_t touchpad_obj[] = { {{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM0}, {{&machine_touchpad_type}, GPIO_NUM_0, TOUCH_PAD_NUM1}, @@ -51,6 +56,24 @@ STATIC const mtp_obj_t touchpad_obj[] = { {{&machine_touchpad_type}, GPIO_NUM_33, TOUCH_PAD_NUM8}, {{&machine_touchpad_type}, GPIO_NUM_32, TOUCH_PAD_NUM9}, }; +#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 +STATIC const mtp_obj_t touchpad_obj[] = { + {{&machine_touchpad_type}, GPIO_NUM_1, TOUCH_PAD_NUM1}, + {{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2}, + {{&machine_touchpad_type}, GPIO_NUM_3, TOUCH_PAD_NUM3}, + {{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM4}, + {{&machine_touchpad_type}, GPIO_NUM_5, TOUCH_PAD_NUM5}, + {{&machine_touchpad_type}, GPIO_NUM_6, TOUCH_PAD_NUM6}, + {{&machine_touchpad_type}, GPIO_NUM_7, TOUCH_PAD_NUM7}, + {{&machine_touchpad_type}, GPIO_NUM_8, TOUCH_PAD_NUM8}, + {{&machine_touchpad_type}, GPIO_NUM_9, TOUCH_PAD_NUM9}, + {{&machine_touchpad_type}, GPIO_NUM_10, TOUCH_PAD_NUM10}, + {{&machine_touchpad_type}, GPIO_NUM_11, TOUCH_PAD_NUM11}, + {{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM12}, + {{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM13}, + {{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM14}, +}; +#endif STATIC mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 7ca3d84140cf1..c520e6d6d7bb6 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -42,6 +42,8 @@ #include "esp32/spiram.h" #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/spiram.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/spiram.h" #endif #include "py/stackctrl.h" @@ -104,6 +106,20 @@ void mp_task(void *pvParameter) { mp_task_heap = malloc(mp_task_heap_size); break; } + #elif CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT + // Try to use the entire external SPIRAM directly for the heap + size_t mp_task_heap_size; + size_t esp_spiram_size = esp_spiram_get_size(); + void *mp_task_heap = (void *)0x3ff80000 - esp_spiram_size; + // printf("inside CONFIG_ESP32S2_SPIRAM_SUPPORT ram=%d\n", esp_spiram_size); + if ( esp_spiram_size > 0 ) + mp_task_heap_size = esp_spiram_size; + else + { + // No SPIRAM, fallback to normal allocation + mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); + mp_task_heap = malloc(mp_task_heap_size); + } #else // Allocate the uPy heap using malloc and get the largest available region size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); @@ -206,3 +222,9 @@ void *esp_native_code_commit(void *buf, size_t len, void *reloc) { memcpy(p, buf, len); return p; } + +#if CONFIG_IDF_TARGET_ESP32S3 +float __ieee754_sqrtf(float x) { + return x; +} +#endif diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 656045da90a21..1cc30b71ec558 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -128,6 +128,9 @@ if(IDF_TARGET STREQUAL "esp32") elseif(IDF_TARGET STREQUAL "esp32s2") list(APPEND IDF_COMPONENTS esp32s2) list(APPEND IDF_COMPONENTS tinyusb) +elseif(IDF_TARGET STREQUAL "esp32s3") + list(APPEND IDF_COMPONENTS esp32s3) + list(APPEND IDF_COMPONENTS tinyusb) endif() # Register the main IDF component. diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 2eb5cd2feed51..16d98a8bbca4a 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -39,9 +39,15 @@ #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/rtc.h" #include "esp32/clk.h" +// #include "driver/touch_pad.h" #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/rtc.h" #include "esp32s2/clk.h" +// #include "driver/touch_pad.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/rtc.h" +#include "esp32s3/clk.h" +// #include "driver/touch_pad.h" #endif #include "py/obj.h" diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index cf668216df9e3..a0bafb755e781 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -32,7 +32,14 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" + +#if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/uart.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/uart.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/uart.h" +#endif #include "py/obj.h" #include "py/objstr.h" diff --git a/ports/esp32/partitions-16.csv b/ports/esp32/partitions-16.csv new file mode 100644 index 0000000000000..5133185c78b2e --- /dev/null +++ b/ports/esp32/partitions-16.csv @@ -0,0 +1,8 @@ +# Notes: the offset of the partition table itself is set in +# $ESPIDF/components/partition_table/Kconfig.projbuild and the +# offset of the factory/ota_0 partition is set in makeimg.py +# Name, Type, SubType, Offset, Size, Flags +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 0x180000, +vfs, data, fat, 0x200000, 0xD59F80, From 7d19429fced08ff46da9173f32168dca8b08bdce Mon Sep 17 00:00:00 2001 From: Seon Rozenblum Date: Mon, 26 Apr 2021 11:47:05 +1000 Subject: [PATCH 3/4] Renamed 16MB flash partition to correct naming. --- ports/esp32/boards/UM_FEATHERS2/sdkconfig.board | 2 +- ports/esp32/{partitions-16.csv => partitions-16MiB.csv} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename ports/esp32/{partitions-16.csv => partitions-16MiB.csv} (100%) diff --git a/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board index ec94d4f47a52e..ccda7bff68129 100644 --- a/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board +++ b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board @@ -8,7 +8,7 @@ CONFIG_SPIRAM_MEMTEST= CONFIG_ESPTOOLPY_FLASHSIZE_4MB= CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y CONFIG_PARTITION_TABLE_CUSTOM=y -CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-16.csv" +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-16MiB.csv" #CONFIG_USB_AND_UART=y # LWIP diff --git a/ports/esp32/partitions-16.csv b/ports/esp32/partitions-16MiB.csv similarity index 100% rename from ports/esp32/partitions-16.csv rename to ports/esp32/partitions-16MiB.csv From 3b0fac30a127ff3bb7365761405de05c93686749 Mon Sep 17 00:00:00 2001 From: Seon Rozenblum Date: Wed, 28 Apr 2021 11:50:48 +1000 Subject: [PATCH 4/4] Fixed changes @dpgeorge requested --- ports/esp32/boards/sdkconfig.spiram_sx | 4 ---- ports/esp32/machine_dac.c | 8 ++++---- ports/esp32/machine_pin.c | 26 -------------------------- ports/esp32/main.c | 1 - ports/esp32/modmachine.c | 3 --- 5 files changed, 4 insertions(+), 38 deletions(-) diff --git a/ports/esp32/boards/sdkconfig.spiram_sx b/ports/esp32/boards/sdkconfig.spiram_sx index dd80eca618a6f..18a0712cbf70f 100644 --- a/ports/esp32/boards/sdkconfig.spiram_sx +++ b/ports/esp32/boards/sdkconfig.spiram_sx @@ -3,13 +3,9 @@ CONFIG_ESP32S2_SPIRAM_SUPPORT=y CONFIG_SPIRAM_TYPE_AUTO=y CONFIG_DEFAULT_PSRAM_CLK_IO=30 CONFIG_DEFAULT_PSRAM_CS_IO=26 -# CONFIG_SPIRAM_FETCH_INSTRUCTIONS is not set -# CONFIG_SPIRAM_RODATA is not set CONFIG_SPIRAM_SPEED_80M=y -# CONFIG_SPIRAM_SPEED_40M=y CONFIG_SPIRAM=y CONFIG_SPIRAM_BOOT_INIT=y CONFIG_SPIRAM_IGNORE_NOTFOUND=y CONFIG_SPIRAM_USE_MEMMAP=y CONFIG_SPIRAM_MEMTEST=y -# CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is not set diff --git a/ports/esp32/machine_dac.c b/ports/esp32/machine_dac.c index d7cb0a2f839d3..146ef60aa81d8 100644 --- a/ports/esp32/machine_dac.c +++ b/ports/esp32/machine_dac.c @@ -43,12 +43,12 @@ typedef struct _mdac_obj_t { } mdac_obj_t; STATIC const mdac_obj_t mdac_obj[] = { - #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 - {{&machine_dac_type}, GPIO_NUM_17, DAC_CHANNEL_1}, - {{&machine_dac_type}, GPIO_NUM_18, DAC_CHANNEL_2}, - #else + #if CONFIG_IDF_TARGET_ESP32 {{&machine_dac_type}, GPIO_NUM_25, DAC_CHANNEL_1}, {{&machine_dac_type}, GPIO_NUM_26, DAC_CHANNEL_2}, + #else + {{&machine_dac_type}, GPIO_NUM_17, DAC_CHANNEL_1}, + {{&machine_dac_type}, GPIO_NUM_18, DAC_CHANNEL_2}, #endif }; diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index 26ddf39aefc69..868e2212219fa 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -154,32 +154,6 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = { {{&machine_pin_type}, GPIO_NUM_45}, {{&machine_pin_type}, GPIO_NUM_46}, -#else - - {{&machine_pin_type}, GPIO_NUM_0}, - {{&machine_pin_type}, GPIO_NUM_1}, - {{&machine_pin_type}, GPIO_NUM_2}, - {{&machine_pin_type}, GPIO_NUM_3}, - {{&machine_pin_type}, GPIO_NUM_4}, - {{&machine_pin_type}, GPIO_NUM_5}, - {{&machine_pin_type}, GPIO_NUM_6}, - {{&machine_pin_type}, GPIO_NUM_7}, - {{&machine_pin_type}, GPIO_NUM_8}, - {{&machine_pin_type}, GPIO_NUM_9}, - {{&machine_pin_type}, GPIO_NUM_10}, - {{&machine_pin_type}, GPIO_NUM_11}, - {{&machine_pin_type}, GPIO_NUM_12}, - {{&machine_pin_type}, GPIO_NUM_13}, - {{&machine_pin_type}, GPIO_NUM_14}, - {{&machine_pin_type}, GPIO_NUM_15}, - {{&machine_pin_type}, GPIO_NUM_16}, - {{&machine_pin_type}, GPIO_NUM_17}, - {{&machine_pin_type}, GPIO_NUM_18}, - {{&machine_pin_type}, GPIO_NUM_19}, - {{&machine_pin_type}, GPIO_NUM_20}, - {{&machine_pin_type}, GPIO_NUM_21}, - {{&machine_pin_type}, GPIO_NUM_22}, - #endif }; diff --git a/ports/esp32/main.c b/ports/esp32/main.c index c520e6d6d7bb6..5fa0a63991a5f 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -111,7 +111,6 @@ void mp_task(void *pvParameter) { size_t mp_task_heap_size; size_t esp_spiram_size = esp_spiram_get_size(); void *mp_task_heap = (void *)0x3ff80000 - esp_spiram_size; - // printf("inside CONFIG_ESP32S2_SPIRAM_SUPPORT ram=%d\n", esp_spiram_size); if ( esp_spiram_size > 0 ) mp_task_heap_size = esp_spiram_size; else diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 16d98a8bbca4a..bbe7fae0335e4 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -39,15 +39,12 @@ #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/rtc.h" #include "esp32/clk.h" -// #include "driver/touch_pad.h" #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/rtc.h" #include "esp32s2/clk.h" -// #include "driver/touch_pad.h" #elif CONFIG_IDF_TARGET_ESP32S3 #include "esp32s3/rom/rtc.h" #include "esp32s3/clk.h" -// #include "driver/touch_pad.h" #endif #include "py/obj.h"