Skip to content

Commit 06755d9

Browse files
esp32: Support I2S.
Signed-off-by: Vincent1-python <[email protected]>
1 parent b0b1660 commit 06755d9

6 files changed

Lines changed: 184 additions & 9 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(IDF_TARGET esp32s31)
22

33
list(APPEND SDKCONFIG_DEFAULTS
4-
boards/sdkconfig.base
4+
boards/sdkconfig.s31_base
55
boards/sdkconfig.ble
66
boards/sdkconfig.spiram_oct
77
)
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# MicroPython on ESP32, ESP IDF configuration
2+
# The following options override the defaults
3+
4+
CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000
5+
6+
# Compiler options: use -O2 and disable assertions to improve performance
7+
CONFIG_COMPILER_OPTIMIZATION_PERF=y
8+
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y
9+
10+
# Application manager
11+
CONFIG_APP_EXCLUDE_PROJECT_VER_VAR=y
12+
CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR=y
13+
14+
# Bootloader config
15+
CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y
16+
CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP=y
17+
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
18+
19+
# Change default log level to "ERROR" (instead of "INFO")
20+
CONFIG_LOG_DEFAULT_LEVEL_ERROR=y
21+
22+
# Set the maximum included log level higher than the default,
23+
# so esp.osdebug() can enable more logging at runtime.
24+
#
25+
# To increase the max log verbosity to Debug or Verbose instead, comment
26+
# CONFIG_LOG_MAXIMUM_LEVEL_INFO=y and uncomment one of the other settings.
27+
#
28+
# If not needed, the next line can be commented entirely to save binary size.
29+
CONFIG_LOG_MAXIMUM_LEVEL_INFO=y
30+
#CONFIG_LOG_MAXIMUM_LEVEL_DEBUG=y
31+
#CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE=y
32+
33+
# Main XTAL Config
34+
# Only on: ESP32
35+
CONFIG_XTAL_FREQ_AUTO=y
36+
37+
# ESP System Settings
38+
# Only on: ESP32, ESP32S3
39+
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n
40+
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=n
41+
42+
# Power Management
43+
CONFIG_PM_ENABLE=y
44+
45+
# Memory protection
46+
# This is required to allow allocating IRAM
47+
CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=n
48+
49+
# FreeRTOS
50+
CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS=2
51+
CONFIG_FREERTOS_SUPPORT_STATIC_ALLOCATION=y
52+
CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP=n
53+
54+
# UDP
55+
CONFIG_LWIP_PPP_SUPPORT=y
56+
CONFIG_LWIP_PPP_PAP_SUPPORT=y
57+
CONFIG_LWIP_PPP_CHAP_SUPPORT=y
58+
59+
# SSL
60+
# Use 4kiB output buffer instead of default 16kiB
61+
CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y
62+
CONFIG_MBEDTLS_HAVE_TIME_DATE=y
63+
CONFIG_MBEDTLS_PLATFORM_TIME_ALT=y
64+
CONFIG_MBEDTLS_HAVE_TIME=y
65+
66+
# Enable DTLS
67+
CONFIG_MBEDTLS_SSL_PROTO_DTLS=y
68+
69+
# Disable ALPN support as it's not implemented in MicroPython
70+
CONFIG_MBEDTLS_SSL_ALPN=n
71+
72+
# Disable slow or unused EC curves
73+
CONFIG_MBEDTLS_ECP_DP_BP256R1_ENABLED=n
74+
CONFIG_MBEDTLS_ECP_DP_BP384R1_ENABLED=n
75+
CONFIG_MBEDTLS_ECP_DP_BP512R1_ENABLED=n
76+
CONFIG_MBEDTLS_ECP_DP_CURVE25519_ENABLED=n
77+
78+
# Disable certificate bundle as it's not implemented in MicroPython
79+
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
80+
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n
81+
82+
# Enable PSK (pre-shared-key) cipher suites, used by tls.SSLContext PSK support.
83+
CONFIG_MBEDTLS_PSK_MODES=y
84+
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y
85+
CONFIG_MBEDTLS_KEY_EXCHANGE_DHE_PSK=y
86+
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_PSK=y
87+
CONFIG_MBEDTLS_KEY_EXCHANGE_RSA_PSK=y
88+
89+
# Allow mbedTLS to allocate from PSRAM or internal memory
90+
#
91+
# (The ESP-IDF default is internal-only, partly for physical security to prevent
92+
# possible information leakage from unencrypted PSRAM contents on the original
93+
# ESP32 - no PSRAM encryption on that chip. MicroPython doesn't support flash
94+
# encryption and is already storing the Python heap in PSRAM so this isn't a
95+
# significant factor in overall physical security.)
96+
CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
97+
98+
# ULP coprocessor support
99+
# Only on: ESP32, ESP32S2, ESP32S3
100+
CONFIG_ULP_COPROC_ENABLED=y
101+
CONFIG_ULP_COPROC_TYPE_FSM=y
102+
CONFIG_ULP_COPROC_RESERVE_MEM=2040
103+
104+
# For cmake build
105+
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
106+
CONFIG_PARTITION_TABLE_CUSTOM=y
107+
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-16MiBplus.csv"
108+
109+
# To reduce IRAM usage
110+
CONFIG_ESP_WIFI_IRAM_OPT=n
111+
CONFIG_ESP_WIFI_EXTRA_IRAM_OPT=n
112+
CONFIG_ESP_WIFI_RX_IRAM_OPT=n
113+
CONFIG_ESP_WIFI_SLP_IRAM_OPT=n
114+
CONFIG_ESP_PHY_IRAM_OPT=n
115+
CONFIG_I2C_MASTER_ISR_HANDLER_IN_IRAM=n
116+
CONFIG_RMT_TX_ISR_HANDLER_IN_IRAM=n
117+
CONFIG_RMT_RX_ISR_HANDLER_IN_IRAM=n
118+
CONFIG_SPI_MASTER_ISR_IN_IRAM=n
119+
CONFIG_SPI_SLAVE_ISR_IN_IRAM=n
120+
CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR=n
121+
CONFIG_PERIPH_CTRL_FUNC_IN_IRAM=n
122+
123+
# Legacy ADC Calibration Configuration
124+
# Only on: ESP32
125+
CONFIG_ADC_CAL_EFUSE_TP_ENABLE=y
126+
CONFIG_ADC_CAL_EFUSE_VREF_ENABLE=y
127+
CONFIG_ADC_CAL_LUT_ENABLE=y
128+
129+
# UART Configuration
130+
CONFIG_UART_ISR_IN_IRAM=y
131+
132+
# IDF 5 deprecated
133+
CONFIG_PCNT_SUPPRESS_DEPRECATE_WARN=y
134+
CONFIG_TOUCH_SUPPRESS_DEPRECATE_WARN=y
135+
136+
CONFIG_ETH_USE_SPI_ETHERNET=y
137+
CONFIG_ETH_SPI_ETHERNET_W5500=y
138+
CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL=y
139+
CONFIG_ETH_SPI_ETHERNET_DM9051=y
140+
141+
# Using newlib "nano" formatting saves size on SoCs where "nano" formatting
142+
# functions are in ROM. ESP32-C6 (and possibly other new chips) have "full"
143+
# newlib formatting in ROM instead and should override this, check
144+
# ESP_ROM_HAS_NEWLIB_NANO_FORMAT in ESP-IDF.
145+
CONFIG_NEWLIB_NANO_FORMAT=y
146+
147+
# IRAM/DRAM split protection is a memory protection feature on some parts
148+
# that support SOC_CPU_IDRAM_SPLIT_USING_PMP, eg. C2, C5, C6, H2
149+
# Due to limitations in the PMP system this feature breaks native emitters
150+
# so is disabled by default.
151+
CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT=n
152+
153+
# Further limit total sockets in TIME-WAIT when there are many short-lived
154+
# connections.
155+
CONFIG_LWIP_MAX_ACTIVE_TCP=12
156+
157+
# Enable new I2C slave API, and disable conflict check.
158+
CONFIG_I2C_SKIP_LEGACY_CONFLICT_CHECK=y
159+
CONFIG_I2C_ENABLE_SLAVE_DRIVER_VERSION_2=y

ports/esp32/machine_i2s.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
// The size of 240 bytes is an engineering optimum that balances transfer performance with an acceptable use of heap space
5050
#define SIZEOF_TRANSFORM_BUFFER_IN_BYTES (240)
5151

52+
#ifndef SOC_I2S_NUM
53+
#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32S31)
54+
#define SOC_I2S_NUM 2
55+
#elif defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6) || defined(CONFIG_IDF_TARGET_ESP32H2)
56+
#define SOC_I2S_NUM 1
57+
#else
58+
#define SOC_I2S_NUM 2
59+
#endif
60+
#endif
61+
5262
typedef enum {
5363
I2S_TX_TRANSFER,
5464
I2S_RX_TRANSFER,
@@ -69,7 +79,7 @@ typedef enum {
6979

7080
typedef struct _machine_i2s_obj_t {
7181
mp_obj_base_t base;
72-
i2s_port_t i2s_id;
82+
int i2s_id;
7383
i2s_chan_handle_t i2s_chan_handle;
7484
mp_hal_pin_obj_t sck;
7585
mp_hal_pin_obj_t ws;
@@ -101,7 +111,7 @@ static const int8_t i2s_frame_map[NUM_I2S_USER_FORMATS][I2S_RX_FRAME_SIZE_IN_BYT
101111
};
102112

103113
void machine_i2s_init0() {
104-
for (i2s_port_t p = 0; p < I2S_NUM_AUTO; p++) {
114+
for (int p = 0; p < SOC_I2S_NUM; p++) {
105115
MP_STATE_PORT(machine_i2s_obj)[p] = NULL;
106116
}
107117
}
@@ -400,7 +410,7 @@ static void mp_machine_i2s_init_helper(machine_i2s_obj_t *self, mp_arg_val_t *ar
400410
}
401411

402412
static machine_i2s_obj_t *mp_machine_i2s_make_new_instance(mp_int_t i2s_id) {
403-
if (i2s_id < 0 || i2s_id >= I2S_NUM_AUTO) {
413+
if (i2s_id < 0 || i2s_id >= SOC_I2S_NUM) {
404414
mp_raise_ValueError(MP_ERROR_TEXT("invalid id"));
405415
}
406416

ports/esp32/mpconfigport.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,8 @@
157157
#define MICROPY_PY_MACHINE_DAC (SOC_DAC_SUPPORTED)
158158
#endif
159159
#ifndef MICROPY_PY_MACHINE_I2S
160-
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
161-
#define MICROPY_PY_MACHINE_I2S (0)
162-
#else
163160
#define MICROPY_PY_MACHINE_I2S (SOC_I2S_SUPPORTED)
164161
#endif
165-
#endif
166162
#define MICROPY_PY_MACHINE_I2S_INCLUDEFILE "ports/esp32/machine_i2s.c"
167163
#define MICROPY_PY_MACHINE_I2S_FINALISER (1)
168164
#define MICROPY_PY_MACHINE_I2S_CONSTANT_RX (I2S_DIR_RX)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This partition table is for devices with 4MiB or more of flash.
2+
# The first 2MiB is used for bootloader, nvs, phy_init and firmware.
3+
# The remaining flash is for the user filesystem(s).
4+
5+
# Notes: the offset of the partition table itself is set in
6+
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
7+
# Name, Type, SubType, Offset, Size, Flags
8+
nvs, data, nvs, 0x9000, 0x6000,
9+
phy_init, data, phy, 0xf000, 0x1000,
10+
factory, app, factory, 0x10000, 0x230000,

ports/esp32/partitions-4MiBplus.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
# Name, Type, SubType, Offset, Size, Flags
88
nvs, data, nvs, 0x9000, 0x6000,
99
phy_init, data, phy, 0xf000, 0x1000,
10-
factory, app, factory, 0x10000, 0x220000,
10+
factory, app, factory, 0x10000, 0x1F0000,

0 commit comments

Comments
 (0)