Skip to content

Commit

Permalink
Merge branch 'master' into OWL_v1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
IhorNehrutsa authored and IhorNehrutsa committed Nov 29, 2023
2 parents a889428 + e182f38 commit efe599d
Show file tree
Hide file tree
Showing 69 changed files with 836 additions and 365 deletions.
40 changes: 36 additions & 4 deletions docs/library/esp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,35 @@ Functions

.. function:: flash_erase(sector_no)

.. function:: osdebug(level)
.. function:: osdebug(uart_no)

Turn esp os debugging messages on or off.
.. note:: This is the ESP8266 form of this function.

The *level* parameter sets the threshold for the log messages for all esp components.
The log levels are defined as constants:
Change the level of OS serial debug log messages. On boot,
OS serial debug log messages are disabled.

``uart_no`` is the number of the UART peripheral which should receive
OS-level output, or ``None`` to disable OS serial debug log messages.

.. function:: osdebug(uart_no, [level])
:no-index:

.. note:: This is the ESP32 form of this function.

Change the level of OS serial debug log messages. On boot, OS
serial debug log messages are limited to Error output only.

The behaviour of this function depends on the arguments passed to it. The
following combinations are supported:

``osdebug(None)`` restores the default OS debug log message level
(``LOG_ERROR``).

``osdebug(0)`` enables all available OS debug log messages (in the
default build configuration this is ``LOG_INFO``).

``osdebug(0, level)`` sets the OS debug log message level to the
specified value. The log levels are defined as constants:

* ``LOG_NONE`` -- No log output
* ``LOG_ERROR`` -- Critical errors, software module can not recover on its own
Expand All @@ -77,6 +100,15 @@ Functions
* ``LOG_VERBOSE`` -- Bigger chunks of debugging information, or frequent messages
which can potentially flood the output

.. note:: ``LOG_DEBUG`` and ``LOG_VERBOSE`` are not compiled into the
MicroPython binary by default, to save size. A custom build with a
modified "``sdkconfig``" source file is needed to see any output
at these log levels.

.. note:: Log output on ESP32 is automatically suspended in "Raw REPL" mode,
to prevent communications issues. This means OS level logging is never
seen when using ``mpremote run`` and similar tools.

.. function:: set_native_code_location(start, length)

**Note**: ESP8266 only
Expand Down
7 changes: 7 additions & 0 deletions examples/usercmodule/cppexample/example.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
extern "C" {
#include <examplemodule.h>
#include <py/objstr.h>

// Here we implement the function using C++ code, but since it's
// declaration has to be compatible with C everything goes in extern "C" scope.
mp_obj_t cppfunc(mp_obj_t a_obj, mp_obj_t b_obj) {
// The following no-ops are just here to verify the static assertions used in
// the public API all compile with C++.
MP_STATIC_ASSERT_STR_ARRAY_COMPATIBLE;
if (mp_obj_is_type(a_obj, &mp_type_BaseException)) {
}

// Prove we have (at least) C++11 features.
const auto a = mp_obj_get_int(a_obj);
const auto b = mp_obj_get_int(b_obj);
Expand Down
3 changes: 3 additions & 0 deletions extmod/extmod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ SRC_THIRDPARTY_C += $(addprefix $(LWIP_DIR)/,\
core/ipv6/nd6.c \
netif/ethernet.c \
)
ifeq ($(MICROPY_PY_LWIP_LOOPBACK),1)
CFLAGS_EXTMOD += -DLWIP_NETIF_LOOPBACK=1
endif
ifeq ($(MICROPY_PY_LWIP_SLIP),1)
CFLAGS_EXTMOD += -DMICROPY_PY_LWIP_SLIP=1
SRC_THIRDPARTY_C += $(LWIP_DIR)/netif/slipif.c
Expand Down
3 changes: 3 additions & 0 deletions ports/esp32/boards/ARDUINO_NANO_ESP32/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#define MICROPY_HW_BOARD_NAME "Arduino Nano ESP32"
#define MICROPY_HW_MCU_NAME "ESP32S3"

// Network config
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-nano-esp32"

#define MICROPY_PY_MACHINE_DAC (0)

#define MICROPY_HW_I2C0_SCL (12)
Expand Down
4 changes: 4 additions & 0 deletions ports/esp32/boards/ESP32_GENERIC/sdkconfig.d2wd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_PERF=n
CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT=y

# Change maximum log level to error, to reduce firmware size.
CONFIG_LOG_MAXIMUM_LEVEL_ERROR=y
CONFIG_LOG_MAXIMUM_LEVEL_INFO=n

# Disable SPI Ethernet driver to reduce firmware size.
CONFIG_ETH_USE_SPI_ETHERNET=n

Expand Down
13 changes: 11 additions & 2 deletions ports/esp32/boards/sdkconfig.base
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP=y
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y

# Change default log level to "ERROR" (instead of "INFO")
CONFIG_LOG_DEFAULT_LEVEL_INFO=n
CONFIG_LOG_DEFAULT_LEVEL_ERROR=y
CONFIG_LOG_DEFAULT_LEVEL=1

# Set the maximum included log level higher than the default,
# so esp.osdebug() can enable more logging at runtime.
#
# To increase the max log verbosity to Debug or Verbose instead, comment
# CONFIG_LOG_MAXIMUM_LEVEL_INFO=y and uncomment one of the other settings.
#
# If not needed, the next line can be commented entirely to save binary size.
CONFIG_LOG_MAXIMUM_LEVEL_INFO=y
#CONFIG_LOG_MAXIMUM_LEVEL_DEBUG=y
#CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE=y

# Main XTAL Config
# Only on: ESP32
Expand Down
6 changes: 6 additions & 0 deletions ports/esp32/esp32_rmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,18 @@ STATIC mp_obj_t esp32_rmt_write_pulses(size_t n_args, const mp_obj_t *args) {
check_esp_err(rmt_wait_tx_done(self->channel_id, portMAX_DELAY));
}

#if !CONFIG_IDF_TARGET_ESP32S3
check_esp_err(rmt_write_items(self->channel_id, self->items, num_items, false));
#endif

if (self->loop_en) {
check_esp_err(rmt_set_tx_intr_en(self->channel_id, false));
check_esp_err(rmt_set_tx_loop_mode(self->channel_id, true));
}

#if CONFIG_IDF_TARGET_ESP32S3
check_esp_err(rmt_write_items(self->channel_id, self->items, num_items, false));
#endif

return mp_const_none;
}
Expand Down
4 changes: 2 additions & 2 deletions ports/esp32/modesp.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
#include "py/mphal.h"

STATIC mp_obj_t esp_osdebug(size_t n_args, const mp_obj_t *args) {
esp_log_level_t level = LOG_LOCAL_LEVEL;
esp_log_level_t level = LOG_LOCAL_LEVEL; // Maximum available level
if (n_args == 2) {
level = mp_obj_get_int(args[1]);
}
if (args[0] == mp_const_none) {
// Disable logging
// Set logging back to boot default of ESP_LOG_ERROR
esp_log_level_set("*", ESP_LOG_ERROR);
} else {
// Enable logging at the given level
Expand Down
8 changes: 8 additions & 0 deletions ports/esp32/modsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <stdlib.h>
#include <string.h>

#include "py/gc.h"
#include "py/runtime0.h"
#include "py/nlr.h"
#include "py/objlist.h"
Expand Down Expand Up @@ -274,6 +275,13 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type_in, size_t n_args, siz
sock->state = sock->type == SOCK_STREAM ? SOCKET_STATE_NEW : SOCKET_STATE_CONNECTED;

sock->fd = lwip_socket(sock->domain, sock->type, sock->proto);
if (sock->fd < 0 && errno == ENFILE) {
// ESP32 LWIP has a hard socket limit, ENFILE is returned when this is
// reached. Similar to the logic elsewhere for MemoryError, try running
// GC before failing outright.
gc_collect();
sock->fd = lwip_socket(sock->domain, sock->type, sock->proto);
}
if (sock->fd < 0) {
mp_raise_OSError(errno);
}
Expand Down
18 changes: 18 additions & 0 deletions ports/esp32/network_wlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_psram.h"

#ifndef NO_QSTR
#include "mdns.h"
Expand Down Expand Up @@ -206,6 +207,23 @@ void esp_initialise_wifi(void) {
wlan_ap_obj.active = false;

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
#if CONFIG_SPIRAM_IGNORE_NOTFOUND
if (!esp_psram_is_initialized()) {
// If PSRAM failed to initialize, disable "Wi-Fi Cache TX Buffers"
// (default SPIRAM config ESP32_WIFI_CACHE_TX_BUFFER_NUM==32, this is 54,400 bytes of heap)
cfg.cache_tx_buf_num = 0;
cfg.feature_caps &= ~CONFIG_FEATURE_CACHE_TX_BUF_BIT;

// Set some other options back to the non-SPIRAM default values
// to save more RAM.
//
// These can be determined from ESP-IDF components/esp_wifi/Kconfig and the
// WIFI_INIT_CONFIG_DEFAULT macro
cfg.tx_buf_type = 1; // Dynamic, this "magic number" is defined in IDF KConfig
cfg.static_tx_buf_num = 0; // Probably don't need, due to tx_buf_type
cfg.dynamic_tx_buf_num = 32; // ESP-IDF default value (maximum)
}
#endif
ESP_LOGD("modnetwork", "Initializing WiFi");
esp_exceptions(esp_wifi_init(&cfg));
esp_exceptions(esp_wifi_set_storage(WIFI_STORAGE_RAM));
Expand Down
19 changes: 9 additions & 10 deletions ports/esp32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@
#if MICROPY_HW_ENABLE_UART_REPL

#include <stdio.h>
#include "driver/uart.h" // For uart_get_sclk_freq()
#include "hal/uart_hal.h"

// Backwards compatibility for when MICROPY_HW_UART_REPL was a ESP-IDF UART
// driver enum. Only UART_NUM_0 was supported with that version of the driver.
#define UART_NUM_0 0

STATIC void uart_irq_handler(void *arg);

// Declaring the HAL structure on the stack saves a tiny amount of static RAM
Expand All @@ -53,16 +50,18 @@ STATIC void uart_irq_handler(void *arg);

void uart_stdout_init(void) {
uart_hal_context_t repl_hal = REPL_HAL_DEFN();
uint32_t sclk_freq;

#if UART_SCLK_DEFAULT == SOC_MOD_CLK_APB
sclk_freq = APB_CLK_FREQ; // Assumes no frequency scaling
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
uart_sclk_t sclk;
#else
// ESP32-H2 and ESP32-C2, I think
#error "This SoC uses a different default UART SCLK source, code needs updating."
soc_module_clk_t sclk;
#endif
uint32_t sclk_freq;

uart_hal_get_sclk(&repl_hal, &sclk); // To restore SCLK after uart_hal_init() resets it
ESP_ERROR_CHECK(uart_get_sclk_freq(sclk, &sclk_freq));

uart_hal_init(&repl_hal, MICROPY_HW_UART_REPL); // Sets defaults: 8n1, no flow control
uart_hal_set_sclk(&repl_hal, sclk);
uart_hal_set_baudrate(&repl_hal, MICROPY_HW_UART_REPL_BAUD, sclk_freq);
uart_hal_rxfifo_rst(&repl_hal);
uart_hal_txfifo_rst(&repl_hal);
Expand Down
10 changes: 7 additions & 3 deletions ports/renesas-ra/boards/ARDUINO_PORTENTA_C33/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
*/

// MCU config
#define MICROPY_HW_BOARD_NAME "PORTENTA C33"
#define MICROPY_HW_BOARD_NAME "Arduino Portenta C33"
#define MICROPY_HW_MCU_NAME "RA6M5"
#define MICROPY_HW_MCU_SYSCLK 200000000
#define MICROPY_HW_MCU_PCLK 100000000
#define MICROPY_HW_FLASH_FS_LABEL "Portenta C33"

// Network config
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-portenta-c33"

// module config
#define MICROPY_EMIT_THUMB (1)
Expand Down Expand Up @@ -59,8 +63,8 @@ void PORTENTA_C33_board_enter_bootloader(void);
#endif

// I2C
#define MICROPY_HW_I2C2_SCL (pin_P407)
#define MICROPY_HW_I2C2_SDA (pin_P408)
#define MICROPY_HW_I2C0_SCL (pin_P408)
#define MICROPY_HW_I2C0_SDA (pin_P407)

// SPI
#define MICROPY_HW_SPI1_SSL (pin_P104)
Expand Down
3 changes: 3 additions & 0 deletions ports/renesas-ra/mphalport.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include "pin.h"
#include "py/ringbuf.h"

#define MICROPY_PY_PENDSV_ENTER uint32_t atomic_state = raise_irq_pri(IRQ_PRI_PENDSV)
#define MICROPY_PY_PENDSV_EXIT restore_irq_pri(atomic_state)

#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500)

extern const unsigned char mp_hal_status_to_errno_table[4];
Expand Down
11 changes: 2 additions & 9 deletions ports/renesas-ra/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,8 @@

void mp_usbd_port_get_serial_number(char *serial_buf) {
const bsp_unique_id_t *id = R_BSP_UniqueIdGet();
// convert to hex
int hexlen = sizeof(id->unique_id_bytes) * 2;
MP_STATIC_ASSERT(hexlen <= MICROPY_HW_USB_DESC_STR_MAX);
for (int i = 0; i < hexlen; i += 2) {
static const char *hexdig = "0123456789abcdef";
serial_buf[i] = hexdig[id->unique_id_bytes[i / 2] >> 4];
serial_buf[i + 1] = hexdig[id->unique_id_bytes[i / 2] & 0x0f];
}
serial_buf[hexlen] = 0;
MP_STATIC_ASSERT(sizeof(id->unique_id_bytes) * 2 <= MICROPY_HW_USB_DESC_STR_MAX);
mp_usbd_hex_str(serial_buf, id->unique_id_bytes, sizeof(id->unique_id_bytes));
}

#endif
1 change: 1 addition & 0 deletions ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ set(MICROPY_SOURCE_LIB
${MICROPY_DIR}/shared/runtime/mpirq.c
${MICROPY_DIR}/shared/runtime/pyexec.c
${MICROPY_DIR}/shared/runtime/stdout_helpers.c
${MICROPY_DIR}/shared/runtime/softtimer.c
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
${MICROPY_DIR}/shared/timeutils/timeutils.c
${MICROPY_DIR}/shared/tinyusb/mp_cdc_common.c
Expand Down
3 changes: 3 additions & 0 deletions ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#define MICROPY_HW_BOARD_NAME "Arduino Nano RP2040 Connect"
#define MICROPY_HW_FLASH_STORAGE_BYTES (8 * 1024 * 1024)

// Network config
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-nano-rp2040-connect"

// Enable networking.
#define MICROPY_PY_NETWORK (1)

Expand Down
11 changes: 2 additions & 9 deletions ports/rp2/cyw43_configport.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ void cyw43_post_poll_hook(void);
extern volatile int cyw43_has_pending;

static inline void cyw43_yield(void) {
uint32_t my_interrupts = save_and_disable_interrupts();
if (!cyw43_has_pending) {
__WFI();
best_effort_wfe_or_timeout(make_timeout_time_ms(1));
}
restore_interrupts(my_interrupts);
}

static inline void cyw43_delay_us(uint32_t us) {
Expand All @@ -118,12 +116,7 @@ static inline void cyw43_delay_us(uint32_t us) {
}

static inline void cyw43_delay_ms(uint32_t ms) {
uint32_t us = ms * 1000;
int32_t start = mp_hal_ticks_us();
while (mp_hal_ticks_us() - start < us) {
cyw43_yield();
MICROPY_EVENT_POLL_HOOK_FAST;
}
mp_hal_delay_ms(ms);
}

#define CYW43_EVENT_POLL_HOOK MICROPY_EVENT_POLL_HOOK_FAST
Expand Down
2 changes: 2 additions & 0 deletions ports/rp2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "shared/readline/readline.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"
#include "shared/runtime/softtimer.h"
#include "tusb.h"
#include "uart.h"
#include "modmachine.h"
Expand Down Expand Up @@ -212,6 +213,7 @@ int main(int argc, char **argv) {
#if MICROPY_PY_THREAD
mp_thread_deinit();
#endif
soft_timer_deinit();
gc_sweep_all();
mp_deinit();
}
Expand Down
Loading

0 comments on commit efe599d

Please sign in to comment.