mirror of
https://github.com/m5stack/M5Stack_MicroPython.git
synced 2026-05-20 10:14:44 -07:00
Added build time option to start REPL in separate thread
Running REPL in thread may solve some issues with uasyncio
and long running or infinite loops
Updated 'machine.UART' module
- fixed bug when wrong parameter was returned to callback functions
- added option to set inverted mode for Rx, Tx, CTS, RTS
- enabled using hw flow controll if CTS and/or RTC pisn are defined
- 'write_break()' function added whitch enits the break signal after data write
Updated 'sys' module
Added 'sys.tz()' to get or set the time zone
The time zone is saved in NVS string and time zone is set on boot
The time zone can be now preserved on reset, power off or deepsleep wake up
Updated 'machine' module
Added SetStackSize() function to set the MicroPython stack size
Added SetHeapSize() function to set the MicroPython heap size
Both heap an stack sizes are set as NVS variables and are read at boot time
to set the sizes, so the sizes may now be different than the compiled ones
Updated 'machine.timer' module
Changed function name 'reshot()' -> 'reshoot()'
This commit is contained in:
@@ -100,6 +100,14 @@ menu "MicroPython"
|
||||
bool "Verbose"
|
||||
endchoice
|
||||
|
||||
config MICROPY_USE_THREADED_REPL
|
||||
bool "Start REPL in separate thread"
|
||||
default n
|
||||
help
|
||||
Start the REPL in its own thread
|
||||
Running REPL in thread may solve some issues with uasyncio
|
||||
and long running or infinite loops
|
||||
|
||||
config MICROPY_RX_BUFFER_SIZE
|
||||
int "RX buffer size"
|
||||
range 256 4096
|
||||
@@ -121,6 +129,7 @@ menu "MicroPython"
|
||||
help
|
||||
Run MicroPython tasks on both cores, if not selected MicroPython tasks are created pinned to one core
|
||||
WARNING: When enabled, it may introduce some issues with uasyncio and threads
|
||||
not recommended for now!
|
||||
|
||||
config MICROPY_TASK_PRIORITY
|
||||
int "Main task priority"
|
||||
@@ -131,9 +140,9 @@ menu "MicroPython"
|
||||
|
||||
config MICROPY_STACK_SIZE
|
||||
int "MicroPython stack size (KB)"
|
||||
range 8 32 if !SPIRAM_SUPPORT
|
||||
range 8 64 if SPIRAM_SUPPORT
|
||||
default 20
|
||||
range 6 32 if !SPIRAM_SUPPORT
|
||||
range 6 64 if SPIRAM_SUPPORT
|
||||
default 16
|
||||
help
|
||||
Set the size of the MicroPython stack in Kbytes.
|
||||
|
||||
|
||||
@@ -50,6 +50,8 @@
|
||||
#define RTC_MEM_INT_SIZE 64
|
||||
#define RTC_MEM_STR_SIZE 2048
|
||||
|
||||
char mpy_time_zone[64] = {'\0'};
|
||||
|
||||
static int RTC_DATA_ATTR rtc_mem_int[RTC_MEM_INT_SIZE] = { 0 };
|
||||
static char RTC_DATA_ATTR rtc_mem_str[RTC_MEM_STR_SIZE] = { 0 };
|
||||
static uint16_t RTC_DATA_ATTR rtc_mem_int_crc;
|
||||
@@ -268,6 +270,29 @@ void sntp_task (void *pvParameters)
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
//--------------------------------------------
|
||||
void tz_fromto_NVS(char *gettzs, char *settzs)
|
||||
{
|
||||
size_t len = 0;
|
||||
char value[64] = {'\0'};
|
||||
if (gettzs) {
|
||||
gettzs[0] = '\0';
|
||||
esp_err_t ret = nvs_get_str(mpy_nvs_handle, "MpyTimeZone", NULL, &len);
|
||||
if ((ret == ESP_OK ) && (len > 0) && (len < 64)) {
|
||||
esp_err_t ret = nvs_get_str(mpy_nvs_handle, "MpyTimeZone", value, &len);
|
||||
if ((ret == ESP_OK ) && (len > 0) && (len < 64)) {
|
||||
if (gettzs) strcpy(gettzs, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (settzs) {
|
||||
esp_err_t esp_err = nvs_set_str(mpy_nvs_handle, "MpyTimeZone", settzs);
|
||||
if (ESP_OK == esp_err) {
|
||||
nvs_commit(mpy_nvs_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------
|
||||
STATIC mp_obj_t mach_rtc_ntp_sync(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
@@ -291,23 +316,30 @@ STATIC mp_obj_t mach_rtc_ntp_sync(size_t n_args, const mp_obj_t *pos_args, mp_ma
|
||||
if ((strlen(srvn) > 3) && (strlen(srvn) < 64)) sprintf(srv_name, "%s", srvn);
|
||||
}
|
||||
|
||||
char tz[64];
|
||||
#ifdef MICROPY_TIMEZONE
|
||||
// ===== Set time zone ======
|
||||
sprintf(tz, "%s", MICROPY_TIMEZONE);
|
||||
if (strlen(mpy_time_zone) == 0) {
|
||||
// Try to get tz from NVS
|
||||
tz_fromto_NVS(mpy_time_zone, NULL);
|
||||
if (strlen(mpy_time_zone) == 0) {
|
||||
#ifdef MICROPY_TIMEZONE
|
||||
// ===== Set default time zone ======
|
||||
snprintf(mpy_time_zone, sizeof(mpy_time_zone)-1, "%s", MICROPY_TIMEZONE);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (args[2].u_obj != mp_const_none) {
|
||||
// get TZ argument
|
||||
const char *tzs = mp_obj_str_get_str(args[2].u_obj);
|
||||
if (strlen(tzs) < 64) {
|
||||
sprintf(tz, "%s", tzs);
|
||||
if ((strlen(tzs) < 2) && (strlen(tzs) < 64)) {
|
||||
sprintf(mpy_time_zone, "%s", tzs);
|
||||
tz_fromto_NVS(NULL, mpy_time_zone);
|
||||
}
|
||||
else {
|
||||
mp_raise_ValueError("tz string length must be 3 - 64");
|
||||
}
|
||||
}
|
||||
setenv("TZ", tz, 1);
|
||||
setenv("TZ", mpy_time_zone, 1);
|
||||
tzset();
|
||||
// ==========================
|
||||
#else
|
||||
tz[0] = '\0';
|
||||
#endif
|
||||
|
||||
|
||||
if (sntp_mutex == NULL) {
|
||||
// Create sntp mutex
|
||||
|
||||
@@ -29,9 +29,11 @@
|
||||
#ifndef MACHRTC_H_
|
||||
#define MACHRTC_H_
|
||||
|
||||
extern char mpy_time_zone[64];
|
||||
extern const mp_obj_type_t mach_rtc_type;
|
||||
extern xSemaphoreHandle sntp_mutex;
|
||||
|
||||
void rtc_init0(void);
|
||||
void tz_fromto_NVS(char *gettzs, char *settzs);
|
||||
|
||||
#endif // MACHRTC_H_
|
||||
|
||||
@@ -670,7 +670,7 @@ STATIC const mp_map_elem_t machine_timer_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_init), (mp_obj_t)&machine_timer_init_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_value), (mp_obj_t)&machine_timer_value_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_events), (mp_obj_t)&machine_timer_events_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reshot), (mp_obj_t)&machine_timer_shot_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reshoot), (mp_obj_t)&machine_timer_shot_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_start), (mp_obj_t)&machine_timer_start_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stop), (mp_obj_t)&machine_timer_pause_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pause), (mp_obj_t)&machine_timer_pause_obj },
|
||||
|
||||
@@ -63,6 +63,7 @@ typedef struct _machine_uart_obj_t {
|
||||
uint32_t *data_cb;
|
||||
uint32_t *pattern_cb;
|
||||
uint32_t *error_cb;
|
||||
uint32_t inverted;
|
||||
uint8_t end_task;
|
||||
uint8_t lineend[3];
|
||||
} machine_uart_obj_t;
|
||||
@@ -149,10 +150,10 @@ static void _sched_callback(mp_obj_t function, int uart, int type, int iarglen,
|
||||
if (!make_carg_entry(carg, 0, MP_SCHED_ENTRY_TYPE_INT, uart, NULL, NULL)) return;
|
||||
if (!make_carg_entry(carg, 1, MP_SCHED_ENTRY_TYPE_INT, type, NULL, NULL)) return;
|
||||
if (sarg) {
|
||||
if (!make_carg_entry(carg, 2, MP_SCHED_ENTRY_TYPE_INT, iarglen, NULL, NULL)) return;
|
||||
if (!make_carg_entry(carg, 2, MP_SCHED_ENTRY_TYPE_STR, iarglen, sarg, NULL)) return;
|
||||
}
|
||||
else {
|
||||
if (!make_carg_entry(carg, 2, MP_SCHED_ENTRY_TYPE_STR, iarglen, sarg, NULL)) return;
|
||||
if (!make_carg_entry(carg, 2, MP_SCHED_ENTRY_TYPE_INT, iarglen, NULL, NULL)) return;
|
||||
}
|
||||
mp_sched_schedule(function, mp_const_none, carg);
|
||||
}
|
||||
@@ -281,9 +282,10 @@ static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
|
||||
{ MP_QSTR_buffer_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 512} },
|
||||
{ MP_QSTR_lineend, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||
{ MP_QSTR_inverted, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_int = -1} },
|
||||
};
|
||||
|
||||
enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx, ARG_rts, ARG_cts, ARG_timeout, ARG_buffer_size, ARG_lineend };
|
||||
enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx, ARG_rts, ARG_cts, ARG_timeout, ARG_buffer_size, ARG_lineend, ARG_inverted };
|
||||
|
||||
//-----------------------------------------------------------------------------------------------
|
||||
STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
@@ -314,10 +316,28 @@ STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_pri
|
||||
lnend_idx++;
|
||||
}
|
||||
}
|
||||
|
||||
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%s, tx=%d, rx=%d, rts=%d, cts=%d, timeout=%u, buf_size=%u, lineend=b'%s')",
|
||||
char inverted[24] = {'\0'};
|
||||
if (self->inverted & (uint32_t)UART_INVERSE_RXD) {
|
||||
if (inverted[0] != '\0') strcat(inverted, ", ");
|
||||
strcat(inverted, "RX");
|
||||
}
|
||||
if (self->inverted & (uint32_t)UART_INVERSE_TXD) {
|
||||
if (inverted[0] != '\0') strcat(inverted, ", ");
|
||||
strcat(inverted, "TX");
|
||||
}
|
||||
if (self->inverted & (uint32_t)UART_INVERSE_CTS) {
|
||||
if (inverted[0] != '\0') strcat(inverted, ", ");
|
||||
strcat(inverted, "CTS");
|
||||
}
|
||||
if (self->inverted & (uint32_t)UART_INVERSE_RTS) {
|
||||
if (inverted[0] != '\0') strcat(inverted, ", ");
|
||||
strcat(inverted, "RTS");
|
||||
}
|
||||
printf("INV %08x, %08x\n", self->inverted, (uint32_t)UART_INVERSE_TXD);
|
||||
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%s, tx=%d, rx=%d, rts=%d, cts=%d, inverted: [%s]\n",
|
||||
self->uart_num+1, baudrate, self->bits, _parity_name[self->parity], _stopbits_name[self->stop],
|
||||
self->tx, self->rx, self->rts, self->cts, self->timeout, self->buffer_size, lnend);
|
||||
self->tx, self->rx, self->rts, self->cts, inverted);
|
||||
mp_printf(print, " timeout=%u, buf_size=%u, lineend=b'%s')", self->timeout, self->buffer_size, lnend);
|
||||
if (self->data_cb) {
|
||||
mp_printf(print, "\n data CB: True, on len: %d", self->data_cb_size);
|
||||
}
|
||||
@@ -420,6 +440,12 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co
|
||||
}
|
||||
}
|
||||
|
||||
// set inverted pins
|
||||
if ((args[ARG_inverted].u_int > -1) && (args[ARG_inverted].u_int != self->inverted)) {
|
||||
self->inverted = args[ARG_inverted].u_int & (UART_INVERSE_RXD | UART_INVERSE_TXD | UART_INVERSE_RTS | UART_INVERSE_CTS);
|
||||
uart_set_line_inverse(self->uart_num+1, self->inverted);
|
||||
}
|
||||
|
||||
// set pins
|
||||
if (((self->tx == -2) && (args[ARG_tx].u_int == UART_PIN_NO_CHANGE)) || ((self->rx == -2) && (args[ARG_rx].u_int == UART_PIN_NO_CHANGE))) {
|
||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Tx&Rx pins must be set: u=machine.UART(uart_num, tx=pin, rx=pin)"));
|
||||
@@ -435,8 +461,16 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co
|
||||
|
||||
if (args[ARG_tx].u_int != UART_PIN_NO_CHANGE) self->tx = args[ARG_tx].u_int;
|
||||
if (args[ARG_rx].u_int != UART_PIN_NO_CHANGE) self->rx = args[ARG_rx].u_int;
|
||||
if (args[ARG_rts].u_int != UART_PIN_NO_CHANGE) self->rts = args[ARG_rts].u_int;
|
||||
if (args[ARG_cts].u_int != UART_PIN_NO_CHANGE) self->cts = args[ARG_cts].u_int;
|
||||
if ((self->rts != args[ARG_rts].u_int) || (self->cts != args[ARG_cts].u_int)) {
|
||||
if (args[ARG_rts].u_int != UART_PIN_NO_CHANGE) self->rts = args[ARG_rts].u_int;
|
||||
if (args[ARG_cts].u_int != UART_PIN_NO_CHANGE) self->cts = args[ARG_cts].u_int;
|
||||
// set flow control
|
||||
int fwc = 0;
|
||||
if (self->rts >= 0) fwc |= UART_HW_FLOWCTRL_RTS;
|
||||
if (self->cts >= 0) fwc |= UART_HW_FLOWCTRL_CTS;
|
||||
// Only when UART_HW_FLOWCTRL_RTS is set, will the rx_thresh value be set.
|
||||
uart_set_hw_flow_ctrl(self->uart_num+1, fwc, UART_FIFO_LEN / 4 * 3);
|
||||
}
|
||||
}
|
||||
|
||||
// set timeout
|
||||
@@ -496,6 +530,7 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
|
||||
self->stop = UART_STOP_BITS_1;
|
||||
self->rts = UART_PIN_NO_CHANGE;
|
||||
self->cts = UART_PIN_NO_CHANGE;
|
||||
self->inverted = UART_INVERSE_DISABLE;
|
||||
self->timeout = 0;
|
||||
self->pattern[0] = 0;
|
||||
self->pattern_len = 0;
|
||||
@@ -773,6 +808,33 @@ STATIC mp_obj_t machine_uart_callback(size_t n_args, const mp_obj_t *pos_args, m
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_uart_callback_obj, 2, machine_uart_callback);
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
STATIC mp_obj_t machine_uart_write_break(size_t n_args, const mp_obj_t *args)
|
||||
{
|
||||
machine_uart_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
||||
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
|
||||
|
||||
// break signal length
|
||||
// unit: one BIT time at current_baudrate
|
||||
int nbreak = nbreak = mp_obj_get_int_truncated(args[2]);
|
||||
if ((nbreak < 1) || (nbreak > 255)) {
|
||||
mp_raise_ValueError("values 1 - 255 are allowed");
|
||||
}
|
||||
|
||||
int len = bufinfo.len;
|
||||
if (n_args == 4) {
|
||||
len = mp_obj_get_int_truncated(args[3]);
|
||||
if ((len < 0) || (len > bufinfo.len)) len = bufinfo.len;;
|
||||
}
|
||||
|
||||
int bytes_written = uart_write_bytes_with_break(self->uart_num+1, (const char*)bufinfo.buf, len, nbreak);
|
||||
|
||||
return MP_OBJ_NEW_SMALL_INT(bytes_written);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_uart_write_break_obj, 3, 4, machine_uart_write_break);
|
||||
|
||||
|
||||
//=================================================================
|
||||
STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
|
||||
@@ -783,6 +845,7 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_write_break), MP_ROM_PTR(&machine_uart_write_break_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_readln), MP_ROM_PTR(&machine_uart_readln_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&machine_uart_flush_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_callback), MP_ROM_PTR(&machine_uart_callback_obj) },
|
||||
@@ -791,6 +854,12 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_CBTYPE_DATA), MP_ROM_INT(UART_CB_TYPE_DATA) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_CBTYPE_PATTERN), MP_ROM_INT(UART_CB_TYPE_PATTERN) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_CBTYPE_ERROR), MP_ROM_INT(UART_CB_TYPE_ERROR) },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_INVERSE_RXD >> 1) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_INVERSE_TXD >> 1) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_INV_CTS), MP_ROM_INT(UART_INVERSE_CTS >> 1) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_INV_RTS), MP_ROM_INT(UART_INVERSE_RTS >> 1) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_INV_NONE), MP_ROM_INT(0) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table);
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
@@ -63,6 +64,8 @@
|
||||
#include "mqtt.h"
|
||||
#endif
|
||||
|
||||
#include "driver/uart.h"
|
||||
#include "rom/uart.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
|
||||
@@ -72,21 +75,16 @@
|
||||
|
||||
#define NVS_NAMESPACE "MPY_NVM"
|
||||
#define MP_TASK_PRIORITY CONFIG_MICROPY_TASK_PRIORITY
|
||||
#define MP_TASK_STACK_SIZE (CONFIG_MICROPY_STACK_SIZE * 1024)
|
||||
#define MP_TASK_HEAP_SIZE (CONFIG_MICROPY_HEAP_SIZE * 1024)
|
||||
#define MP_TASK_STACK_LEN (MP_TASK_STACK_SIZE / sizeof(StackType_t))
|
||||
#define MP_TASK_STACK_LEN 3072
|
||||
|
||||
STATIC TaskHandle_t MainTaskHandle = NULL;
|
||||
|
||||
STATIC StaticTask_t DRAM_ATTR mp_task_tcb;
|
||||
STATIC StackType_t DRAM_ATTR mp_task_stack[MP_TASK_STACK_LEN] __attribute__((aligned (8)));
|
||||
|
||||
STATIC uint8_t *mp_task_heap;
|
||||
static StaticTask_t DRAM_ATTR mp_task_tcb;
|
||||
static StackType_t *mp_task_stack;
|
||||
static uint8_t *mp_task_heap;
|
||||
static int mp_task_stack_len = 4092;
|
||||
|
||||
int MainTaskCore = 0;
|
||||
|
||||
#include "driver/uart.h"
|
||||
#include "rom/uart.h"
|
||||
|
||||
//===============================
|
||||
void mp_task(void *pvParameter) {
|
||||
volatile uint32_t sp = (uint32_t)get_sp();
|
||||
@@ -121,38 +119,34 @@ void mp_task(void *pvParameter) {
|
||||
// -------------------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_MICROPY_USE_TASK_WDT
|
||||
// Enable watchdog for MicroPython main task
|
||||
esp_task_wdt_init(CONFIG_TASK_WDT_TIMEOUT_S, false);
|
||||
esp_task_wdt_add(MainTaskHandle);
|
||||
esp_task_wdt_reset();
|
||||
#endif
|
||||
|
||||
uart_init();
|
||||
|
||||
// Check and open NVS name space
|
||||
if (nvs_open(NVS_NAMESPACE, NVS_READWRITE, &mpy_nvs_handle) != ESP_OK) {
|
||||
mpy_nvs_handle = 0;
|
||||
printf("Error while opening MicroPython NVS name space\n");
|
||||
}
|
||||
#if CONFIG_BOOT_SET_LED >= 0
|
||||
// Deactivate boot led
|
||||
gpio_pad_select_gpio(CONFIG_BOOT_SET_LED);
|
||||
GPIO_OUTPUT_SET(CONFIG_BOOT_SET_LED, CONFIG_BOOT_LED_ON ^ 1);
|
||||
#endif
|
||||
|
||||
// Get and print reset & wakeup reasons
|
||||
// Get and print reset & wakeup reasons
|
||||
mpsleep_init0();
|
||||
|
||||
if (mpsleep_get_reset_cause() != MPSLEEP_DEEPSLEEP_RESET) rtc_init0();
|
||||
|
||||
mp_thread_preinit(&mp_task_stack[0], MP_TASK_STACK_LEN);
|
||||
mp_thread_preinit(&mp_task_stack[0], mp_task_stack_len);
|
||||
|
||||
soft_reset:
|
||||
// Thread init
|
||||
mp_thread_init();
|
||||
|
||||
// Initialize the stack pointer for the main thread
|
||||
mp_stack_set_top((void *)sp);
|
||||
mp_stack_set_limit(MP_TASK_STACK_SIZE - 1024);
|
||||
#ifdef CONFIG_MICROPY_USE_THREADED_REPL
|
||||
mp_stack_set_limit(mp_task_stack_len - 256);
|
||||
#else
|
||||
mp_stack_set_limit(mp_task_stack_len - 1024);
|
||||
#endif
|
||||
|
||||
// initialize the mp heap
|
||||
gc_init(mp_task_heap, mp_task_heap + MP_TASK_HEAP_SIZE);
|
||||
gc_init(mp_task_heap, mp_task_heap + mpy_heap_size);
|
||||
|
||||
mp_init();
|
||||
mp_obj_list_init(mp_sys_path, 0);
|
||||
@@ -168,12 +162,6 @@ soft_reset:
|
||||
// === Mount internal flash file system ===
|
||||
int res = mount_vfs(VFS_NATIVE_TYPE_SPIFLASH, VFS_NATIVE_INTERNAL_MP);
|
||||
|
||||
#if CONFIG_BOOT_SET_LED >= 0
|
||||
// Deactivate boot led
|
||||
gpio_pad_select_gpio(CONFIG_BOOT_SET_LED);
|
||||
GPIO_OUTPUT_SET(CONFIG_BOOT_SET_LED, CONFIG_BOOT_LED_ON ^ 1);
|
||||
#endif
|
||||
|
||||
if (res == 0) {
|
||||
// run boot-up script 'boot.py'
|
||||
pyexec_file("boot.py");
|
||||
@@ -187,7 +175,7 @@ soft_reset:
|
||||
}
|
||||
}
|
||||
}
|
||||
else printf("Error mounting Flash file system\n");
|
||||
else ESP_LOGE("MicroPython", "Error mounting Flash file system");
|
||||
|
||||
// === Print some info ===
|
||||
char sbuff[24] = { 0 };
|
||||
@@ -225,7 +213,11 @@ soft_reset:
|
||||
printf("Wakeup source: %s\n", sbuff);
|
||||
}
|
||||
|
||||
printf(" uPY stack: %d bytes\n", MP_TASK_STACK_LEN-1024);
|
||||
#ifdef CONFIG_MICROPY_USE_THREADED_REPL
|
||||
printf(" REPL stack: %d bytes\n", mpy_repl_stack_size);
|
||||
#else
|
||||
printf(" uPY stack: %d bytes\n", mp_task_stack_len);
|
||||
#endif
|
||||
|
||||
#if CONFIG_SPIRAM_SUPPORT
|
||||
// ## USING SPI RAM FOR HEAP ##
|
||||
@@ -242,32 +234,63 @@ soft_reset:
|
||||
#endif
|
||||
|
||||
// === Main loop ==================================
|
||||
for (;;) {
|
||||
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
|
||||
if (pyexec_raw_repl() != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (pyexec_friendly_repl() != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
MP_THREAD_GIL_EXIT();
|
||||
|
||||
#ifdef CONFIG_MICROPY_USE_TASK_WDT
|
||||
// Enable watchdog for MicroPython main task
|
||||
esp_task_wdt_init(CONFIG_TASK_WDT_TIMEOUT_S, false);
|
||||
esp_task_wdt_add(MainTaskHandle);
|
||||
esp_task_wdt_reset();
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_MICROPY_USE_THREADED_REPL
|
||||
// === Start REPL in separate thread ===
|
||||
pyexec_frozen_module("modREPL.py");
|
||||
#ifdef CONFIG_MICROPY_USE_TASK_WDT
|
||||
if (ReplTaskHandle) {
|
||||
// Enable watchdog for MicroPython main task
|
||||
esp_task_wdt_init(CONFIG_TASK_WDT_TIMEOUT_S, false);
|
||||
esp_task_wdt_add(ReplTaskHandle);
|
||||
esp_task_wdt_reset();
|
||||
}
|
||||
#endif
|
||||
if (!ReplTaskHandle) {
|
||||
printf("!! Error starting threaded REPL, Halted. !!\n");
|
||||
}
|
||||
while (1) {
|
||||
// Main task just waits doing nothing
|
||||
vTaskDelay(CONFIG_TASK_WDT_TIMEOUT_S * 500 / portTICK_RATE_MS);
|
||||
#ifdef CONFIG_MICROPY_USE_TASK_WDT
|
||||
esp_task_wdt_reset();
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
// === Start REPL in main task ===
|
||||
ReplTaskHandle = MainTaskHandle;
|
||||
for (;;) {
|
||||
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
|
||||
if (pyexec_raw_repl() != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (pyexec_friendly_repl() != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// ================================================
|
||||
|
||||
//ToDo: Remember the REPL mode !!
|
||||
//ToDo: Remember the REPL mode (is it needed ?) !!
|
||||
prepareSleepReset(0, "ESP32: soft reboot\r\n");
|
||||
esp_restart();
|
||||
|
||||
goto soft_reset;
|
||||
esp_restart(); // no return !!
|
||||
}
|
||||
|
||||
|
||||
//============================
|
||||
void micropython_entry(void) {
|
||||
ESP_LOGI("MicroPython", "[=== Started ===]");
|
||||
|
||||
void micropython_entry(void)
|
||||
{
|
||||
// === Set esp32 log levels while running MicroPython ===
|
||||
if (CONFIG_MICRO_PY_LOG_LEVEL < CONFIG_LOG_DEFAULT_LEVEL) esp_log_level_set("*", CONFIG_MICRO_PY_LOG_LEVEL);
|
||||
if ((CONFIG_LOG_DEFAULT_LEVEL > ESP_LOG_WARN) && (CONFIG_MICRO_PY_LOG_LEVEL > ESP_LOG_WARN)){
|
||||
@@ -285,8 +308,6 @@ void micropython_entry(void) {
|
||||
else esp_log_level_set("OTA_UPDATE", CONFIG_LOG_DEFAULT_LEVEL);
|
||||
#endif
|
||||
|
||||
nvs_flash_init();
|
||||
|
||||
#ifdef CONFIG_MICROPY_USE_MQTT
|
||||
esp_log_level_set(MQTT_TAG, CONFIG_MQTT_LOG_LEVEL);
|
||||
#endif
|
||||
@@ -294,19 +315,77 @@ void micropython_entry(void) {
|
||||
esp_log_level_set(FTP_TAG, CONFIG_FTPSERVER_LOG_LEVEL);
|
||||
#endif
|
||||
|
||||
nvs_flash_init();
|
||||
|
||||
// === Check and allocate stack ===
|
||||
// Open NVS name space
|
||||
if (nvs_open(NVS_NAMESPACE, NVS_READWRITE, &mpy_nvs_handle) != ESP_OK) {
|
||||
mpy_nvs_handle = 0;
|
||||
ESP_LOGE("MicroPython","Error while opening MicroPython NVS name space");
|
||||
}
|
||||
if (mpy_nvs_handle != 0) {
|
||||
// Get stack size from NVS
|
||||
if (ESP_ERR_NVS_NOT_FOUND == nvs_get_i32(mpy_nvs_handle, "MPY_StackSize", &mpy_repl_stack_size)) {
|
||||
mpy_repl_stack_size = CONFIG_MICROPY_STACK_SIZE * 1024;
|
||||
}
|
||||
else {
|
||||
if ((mpy_repl_stack_size < MPY_MIN_STACK_SIZE) || (mpy_repl_stack_size > MPY_MAX_STACK_SIZE)) {
|
||||
mpy_repl_stack_size = CONFIG_MICROPY_STACK_SIZE * 1024;
|
||||
ESP_LOGE("MicroPython","Wrong Stack size set in NVS: %d (set to configured: %d)", mpy_heap_size, CONFIG_MICROPY_HEAP_SIZE * 1024);
|
||||
}
|
||||
else {
|
||||
ESP_LOGW("MicroPython","Stack size set from NVS: %d (configured: %d)", mpy_repl_stack_size, CONFIG_MICROPY_STACK_SIZE * 1024);
|
||||
}
|
||||
}
|
||||
// restore time zone
|
||||
tz_fromto_NVS(mpy_time_zone, NULL);
|
||||
if (strlen(mpy_time_zone) > 0) {
|
||||
setenv("TZ", mpy_time_zone, 1);
|
||||
tzset();
|
||||
}
|
||||
}
|
||||
mpy_repl_stack_size &= 0x7FFFFFFC;
|
||||
|
||||
#ifdef CONFIG_MICROPY_USE_THREADED_REPL
|
||||
mp_task_stack_len = MP_TASK_STACK_LEN;
|
||||
#else
|
||||
mp_task_stack_len = mpy_repl_stack_size;
|
||||
#endif
|
||||
mp_task_stack = heap_caps_malloc(mp_task_stack_len, MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
|
||||
if (mp_task_stack == NULL) {
|
||||
printf("Error allocating stack, Halted.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// ==== Allocate heap memory ====
|
||||
if (mpy_nvs_handle != 0) {
|
||||
// Get heap size from NVS
|
||||
if (ESP_ERR_NVS_NOT_FOUND == nvs_get_i32(mpy_nvs_handle, "MPY_HeapSize", &mpy_heap_size)) {
|
||||
mpy_heap_size = CONFIG_MICROPY_HEAP_SIZE * 1024;
|
||||
}
|
||||
else {
|
||||
if ((mpy_heap_size < MPY_MIN_HEAP_SIZE) || (mpy_heap_size > MPY_MAX_HEAP_SIZE)) {
|
||||
mpy_heap_size = CONFIG_MICROPY_HEAP_SIZE * 1024;
|
||||
ESP_LOGE("MicroPython","Wrong Heap size set in NVS: %d (set to configured: %d)", mpy_heap_size, CONFIG_MICROPY_HEAP_SIZE * 1024);
|
||||
}
|
||||
else {
|
||||
ESP_LOGW("MicroPython","Heap size set from NVS: %d (configured: %d)", mpy_heap_size, CONFIG_MICROPY_HEAP_SIZE * 1024);
|
||||
}
|
||||
}
|
||||
}
|
||||
mpy_heap_size &= 0x7FFFFFFC;
|
||||
#if CONFIG_SPIRAM_SUPPORT
|
||||
// ## USING SPI RAM FOR HEAP ##
|
||||
#if CONFIG_SPIRAM_USE_CAPS_ALLOC
|
||||
mp_task_heap = heap_caps_malloc(MP_TASK_HEAP_SIZE, MALLOC_CAP_SPIRAM);
|
||||
mp_task_heap = heap_caps_malloc(mpy_heap_size, MALLOC_CAP_SPIRAM);
|
||||
#elif SPIRAM_USE_MEMMAP
|
||||
mp_task_heap = (uint8_t *)0x3f800000;
|
||||
#else
|
||||
mp_task_heap = malloc(MP_TASK_HEAP_SIZE);
|
||||
mp_task_heap = malloc(mpy_heap_size);
|
||||
#endif
|
||||
#else
|
||||
// ## USING DRAM FOR HEAP ##
|
||||
mp_task_heap = malloc(MP_TASK_HEAP_SIZE);
|
||||
mp_task_heap = malloc(mpy_heap_size);
|
||||
#endif
|
||||
|
||||
if (mp_task_heap == NULL) {
|
||||
@@ -318,16 +397,17 @@ void micropython_entry(void) {
|
||||
periph_module_disable(PERIPH_I2C0_MODULE);
|
||||
periph_module_enable(PERIPH_I2C0_MODULE);
|
||||
|
||||
printf("\n[=== MicroPython started ===]\n");
|
||||
// ==== Create and start main MicroPython task ====
|
||||
#if CONFIG_FREERTOS_UNICORE
|
||||
MainTaskCore = 0;
|
||||
MainTaskHandle = xTaskCreateStaticPinnedToCore(&mp_task, "mp_task", MP_TASK_STACK_LEN, NULL, MP_TASK_PRIORITY, &mp_task_stack[0], &mp_task_tcb, 0);
|
||||
MainTaskHandle = xTaskCreateStaticPinnedToCore(&mp_task, "mp_task", mp_task_stack_len, NULL, MP_TASK_PRIORITY, &mp_task_stack[0], &mp_task_tcb, 0);
|
||||
#else
|
||||
MainTaskCore = 1;
|
||||
#if CONFIG_MICROPY_USE_BOTH_CORES
|
||||
MainTaskHandle = xTaskCreateStatic(&mp_task, "mp_task", MP_TASK_STACK_LEN, NULL, MP_TASK_PRIORITY, &mp_task_stack[0], &mp_task_tcb);
|
||||
MainTaskHandle = xTaskCreateStatic(&mp_task, "mp_task", mp_task_stack_len, NULL, MP_TASK_PRIORITY, &mp_task_stack[0], &mp_task_tcb);
|
||||
#else
|
||||
MainTaskHandle = xTaskCreateStaticPinnedToCore(&mp_task, "mp_task", MP_TASK_STACK_LEN, NULL, MP_TASK_PRIORITY, &mp_task_stack[0], &mp_task_tcb, 1);
|
||||
MainTaskHandle = xTaskCreateStaticPinnedToCore(&mp_task, "mp_task", mp_task_stack_len, NULL, MP_TASK_PRIORITY, &mp_task_stack[0], &mp_task_tcb, 1);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -72,6 +72,8 @@
|
||||
nvs_handle mpy_nvs_handle = 0;
|
||||
|
||||
machine_rtc_config_t RTC_DATA_ATTR machine_rtc_config = {0};
|
||||
int mpy_repl_stack_size = CONFIG_MICROPY_STACK_SIZE * 1024;
|
||||
int mpy_heap_size = CONFIG_MICROPY_HEAP_SIZE * 1024;
|
||||
|
||||
|
||||
// === Variables stored in RTC_SLOW_MEM ===
|
||||
@@ -862,6 +864,47 @@ STATIC mp_obj_t mod_machine_set_wdt() {
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_machine_set_wdt_obj, mod_machine_set_wdt);
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
static void _set_stack_heap(char *key, int value, int valmin, int valmax)
|
||||
{
|
||||
checkNVS();
|
||||
|
||||
if ((value != 0) && ((value < valmin) || (value > valmax))) {
|
||||
mp_raise_msg(&mp_type_OSError, "Invalid size");
|
||||
}
|
||||
|
||||
esp_err_t esp_err = nvs_set_i32(mpy_nvs_handle, key, value);
|
||||
if (ESP_OK == esp_err) {
|
||||
nvs_commit(mpy_nvs_handle);
|
||||
}
|
||||
else if (ESP_ERR_NVS_NOT_ENOUGH_SPACE == esp_err || ESP_ERR_NVS_PAGE_FULL == esp_err || ESP_ERR_NVS_NO_FREE_PAGES == esp_err) {
|
||||
mp_raise_msg(&mp_type_OSError, "No space available for NVS variable.");
|
||||
}
|
||||
else if (ESP_ERR_NVS_INVALID_NAME == esp_err || ESP_ERR_NVS_KEY_TOO_LONG == esp_err) {
|
||||
mp_raise_msg(&mp_type_OSError, "NVS Key invalid or too long");
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
STATIC mp_obj_t mod_machine_set_stack_size (mp_obj_t _value)
|
||||
{
|
||||
int value = mp_obj_get_int_truncated(_value);
|
||||
value &= 0x7FFFFFFC;
|
||||
_set_stack_heap("MPY_StackSize", value, MPY_MIN_STACK_SIZE, MPY_MAX_STACK_SIZE);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_machine_set_stack_size_obj, mod_machine_set_stack_size);
|
||||
|
||||
//-----------------------------------------------------------
|
||||
STATIC mp_obj_t mod_machine_set_heap_size (mp_obj_t _value)
|
||||
{
|
||||
int value = mp_obj_get_int_truncated(_value);
|
||||
value &= 0x7FFFFFFC;
|
||||
_set_stack_heap("MPY_HeapSize", value, MPY_MIN_HEAP_SIZE, MPY_MAX_HEAP_SIZE);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_machine_set_heap_size_obj, mod_machine_set_heap_size);
|
||||
|
||||
|
||||
//===============================================================
|
||||
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
|
||||
@@ -882,6 +925,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_wake_description), MP_ROM_PTR(&machine_wake_desc_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_heap_info), MP_ROM_PTR(&machine_heap_info_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stdin_disable), MP_ROM_PTR(&mod_machine_stdin_disable_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_SetStackSize), MP_ROM_PTR(&mod_machine_set_stack_size_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_SetHeapSize), MP_ROM_PTR(&mod_machine_set_heap_size_obj) },
|
||||
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvs_setint), MP_ROM_PTR(&mod_machine_nvs_set_int_obj) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvs_getint), MP_ROM_PTR(&mod_machine_nvs_get_int_obj) },
|
||||
|
||||
@@ -31,11 +31,27 @@
|
||||
#ifndef MICROPY_INCLUDED_ESP32_MODMACHINE_H
|
||||
#define MICROPY_INCLUDED_ESP32_MODMACHINE_H
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "nvs.h"
|
||||
#include "py/obj.h"
|
||||
#include "driver/rtc_io.h"
|
||||
|
||||
#define MPY_MIN_STACK_SIZE (6*1024)
|
||||
#if CONFIG_SPIRAM_SUPPORT
|
||||
#define MPY_MAX_STACK_SIZE (64*1024)
|
||||
#define MPY_MIN_HEAP_SIZE (128*1024)
|
||||
#define MPY_MAX_HEAP_SIZE (3584*1024)
|
||||
#else
|
||||
#define MPY_MAX_STACK_SIZE (32*1024)
|
||||
#define MPY_MIN_HEAP_SIZE (48*1024)
|
||||
#if defined(CONFIG_MICROPY_USE_CURL) && defined(CONFIG_MICROPY_USE_CURL_TLS)
|
||||
#define MPY_MAX_HEAP_SIZE (72*1024)
|
||||
#else
|
||||
#define MPY_MAX_HEAP_SIZE (96*1024)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define EXT1_WAKEUP_ALL_HIGH 2 //!< Wake the chip when all selected GPIOs go high
|
||||
#define EXT1_WAKEUP_MAX_PINS 4
|
||||
|
||||
@@ -76,6 +92,8 @@ extern const mp_obj_type_t machine_onewire_type;
|
||||
extern const mp_obj_type_t machine_ds18x20_type;
|
||||
|
||||
extern nvs_handle mpy_nvs_handle;
|
||||
extern int mpy_repl_stack_size;
|
||||
extern int mpy_heap_size;
|
||||
|
||||
void machine_pins_init(void);
|
||||
void machine_pins_deinit(void);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import _thread
|
||||
import sys
|
||||
|
||||
def thREPL():
|
||||
while True:
|
||||
print(">>> Starting REPL in thread <<<\n")
|
||||
sys.REPL()
|
||||
|
||||
_thread.stack_size(sys.stackSize())
|
||||
REPLthread = _thread.start_new_thread("REPLthread", thREPL, ())
|
||||
_thread.stack_size(4096)
|
||||
@@ -63,6 +63,7 @@ extern TaskHandle_t FtpTaskHandle;
|
||||
extern int MainTaskCore;
|
||||
|
||||
TaskHandle_t MainTaskHandle = NULL;
|
||||
TaskHandle_t ReplTaskHandle = NULL;
|
||||
|
||||
uint8_t main_accept_msg = 1;
|
||||
|
||||
@@ -200,7 +201,13 @@ STATIC void freertos_entry(void *arg) {
|
||||
//------------------------------------------------------------------------------------------------------------------------------
|
||||
TaskHandle_t mp_thread_create_ex(void *(*entry)(void*), void *arg, size_t *stack_size, int priority, char *name, bool same_core)
|
||||
{
|
||||
// store thread entry function into a global variable so we can access it
|
||||
bool is_repl = (strcmp(name, "REPLthread") == 0);
|
||||
if (is_repl) {
|
||||
if (ReplTaskHandle) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "REPL thread already started"));
|
||||
}
|
||||
}
|
||||
// store thread entry function into a global variable so we can access it
|
||||
ext_thread_entry = entry;
|
||||
|
||||
// Check thread stack size
|
||||
@@ -247,6 +254,7 @@ TaskHandle_t mp_thread_create_ex(void *(*entry)(void*), void *arg, size_t *stack
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't create thread"));
|
||||
}
|
||||
|
||||
if (is_repl) ReplTaskHandle = id;
|
||||
// adjust the stack_size to provide room to recover from hitting the limit
|
||||
//*stack_size -= 1024;
|
||||
|
||||
@@ -265,7 +273,8 @@ TaskHandle_t mp_thread_create_ex(void *(*entry)(void*), void *arg, size_t *stack
|
||||
th->waiting = 0;
|
||||
th->deleted = 0;
|
||||
th->notifyed = 0;
|
||||
th->type = THREAD_TYPE_PYTHON;
|
||||
if (is_repl) th->type = THREAD_TYPE_REPL;
|
||||
else th->type = THREAD_TYPE_PYTHON;
|
||||
thread = th;
|
||||
|
||||
mp_thread_mutex_unlock(&thread_mutex);
|
||||
@@ -682,6 +691,23 @@ int mp_thread_list(thread_list_t *list) {
|
||||
int mp_thread_replAcceptMsg(int8_t accept) {
|
||||
int res = main_accept_msg;
|
||||
mp_thread_mutex_lock(&thread_mutex, 1);
|
||||
for (thread_t *th = thread; th != NULL; th = th->next) {
|
||||
if (th->id == xTaskGetCurrentTaskHandle()) {
|
||||
if ((th->id == ReplTaskHandle) && (accept >= 0)) {
|
||||
main_accept_msg = accept & 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
mp_thread_mutex_unlock(&thread_mutex);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
//------------------------------------------
|
||||
int mp_thread_mainAcceptMsg(int8_t accept) {
|
||||
int res = main_accept_msg;
|
||||
mp_thread_mutex_lock(&thread_mutex, 1);
|
||||
for (thread_t *th = thread; th != NULL; th = th->next) {
|
||||
if (th->id == xTaskGetCurrentTaskHandle()) {
|
||||
if ((th->id == MainTaskHandle) && (accept >= 0)) {
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#define THREAD_TYPE_MAIN 1
|
||||
#define THREAD_TYPE_PYTHON 2
|
||||
#define THREAD_TYPE_SERVICE 3
|
||||
#define THREAD_TYPE_REPL 3
|
||||
|
||||
// Reserved thread notification constants
|
||||
#define THREAD_NOTIFY_PAUSE 0x01000000
|
||||
@@ -112,6 +113,9 @@ typedef struct _thread_list_t {
|
||||
threadlistitem_t *threads; // pointer to thread info
|
||||
} thread_list_t;
|
||||
|
||||
extern TaskHandle_t MainTaskHandle;
|
||||
extern TaskHandle_t ReplTaskHandle;
|
||||
|
||||
thread_msg_t thread_messages[MAX_THREAD_MESSAGES];
|
||||
|
||||
uint8_t main_accept_msg;
|
||||
@@ -140,6 +144,7 @@ int mp_thread_getSelfname(char *name);
|
||||
int mp_thread_getname(TaskHandle_t id, char *name);
|
||||
int mp_thread_list(thread_list_t *list);
|
||||
int mp_thread_replAcceptMsg(int8_t accept);
|
||||
int mp_thread_mainAcceptMsg(int8_t accept);
|
||||
|
||||
#ifdef CONFIG_MICROPY_USE_TELNET
|
||||
uintptr_t mp_thread_createTelnetTask(size_t stack_size);
|
||||
|
||||
@@ -24,12 +24,12 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#define MICROPY_GIT_TAG "ESP32_LoBo_v3.2.11"
|
||||
#define MICROPY_GIT_TAG "ESP32_LoBo_v3.2.12"
|
||||
#define MICROPY_GIT_HASH "gbae9709a"
|
||||
#define MICROPY_BUILD_DATE "2018-04-16"
|
||||
#define MICROPY_BUILD_DATE "2018-04-18"
|
||||
#define MICROPY_VERSION_MAJOR (3)
|
||||
#define MICROPY_VERSION_MINOR (2)
|
||||
#define MICROPY_VERSION_MICRO (11)
|
||||
#define MICROPY_VERSION_STRING "3.2.11"
|
||||
#define MICROPY_VERSION_MICRO (12)
|
||||
#define MICROPY_VERSION_STRING "3.2.12"
|
||||
#define MICROPY_CORE_VERSION "59dda71"
|
||||
#define MICROPY_CORE_DATE "2018-04-10"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -374,6 +374,7 @@ raw_repl_reset:
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pyexec_friendly_repl(void) {
|
||||
@@ -499,6 +500,7 @@ friendly_repl_reset:
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // MICROPY_REPL_EVENT_DRIVEN
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include "py/builtin.h"
|
||||
#include "py/objlist.h"
|
||||
@@ -36,6 +38,9 @@
|
||||
#include "py/stream.h"
|
||||
#include "py/smallint.h"
|
||||
#include "py/runtime.h"
|
||||
#include "lib/utils/pyexec.h"
|
||||
#include "modmachine.h"
|
||||
#include "machine_rtc.h"
|
||||
|
||||
#if MICROPY_PY_SYS
|
||||
|
||||
@@ -101,6 +106,7 @@ STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
|
||||
exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]);
|
||||
}
|
||||
nlr_raise(exc);
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
|
||||
|
||||
@@ -158,23 +164,94 @@ STATIC mp_obj_t mp_sys_mpycore() {
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_mpycore_obj, mp_sys_mpycore);
|
||||
|
||||
#ifdef CONFIG_MICROPY_USE_THREADED_REPL
|
||||
//------------------------------------
|
||||
STATIC mp_obj_t mp_sys_exec_repl(void)
|
||||
{
|
||||
for (;;) {
|
||||
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
|
||||
if (pyexec_raw_repl() != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (pyexec_friendly_repl() != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
prepareSleepReset(0, "ESP32: soft reboot\r\n");
|
||||
esp_restart(); // no return !!
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exec_repl_obj, mp_sys_exec_repl);
|
||||
|
||||
//-------------------------------------
|
||||
STATIC mp_obj_t mp_sys_stack_size(void)
|
||||
{
|
||||
return mp_obj_new_int(mpy_repl_stack_size);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_stack_size_obj, mp_sys_stack_size);
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------
|
||||
STATIC mp_obj_t mp_sys_timezone(size_t n_args, const mp_obj_t *args)
|
||||
{
|
||||
if (n_args == 0) {
|
||||
char tzs[64] = {'\0'};
|
||||
if (strlen(mpy_time_zone) == 0) {
|
||||
// Try to get tz from NVS
|
||||
tz_fromto_NVS(tzs, NULL);
|
||||
if (strlen(tzs) > 0) {
|
||||
strcpy(mpy_time_zone, tzs);
|
||||
setenv("TZ", mpy_time_zone, 1);
|
||||
tzset();
|
||||
}
|
||||
}
|
||||
else strcpy(tzs, mpy_time_zone);
|
||||
}
|
||||
else {
|
||||
const char *newtzs = mp_obj_str_get_str(args[0]);
|
||||
if (strcmp(newtzs, mpy_time_zone) != 0) {
|
||||
if ((strlen(newtzs) > 2) && (strlen(newtzs) < 64)) {
|
||||
sprintf(mpy_time_zone, "%s", newtzs);
|
||||
tz_fromto_NVS(NULL, mpy_time_zone);
|
||||
}
|
||||
else {
|
||||
mp_raise_ValueError("tz string length must be 3 - 64");
|
||||
}
|
||||
}
|
||||
setenv("TZ", mpy_time_zone, 1);
|
||||
tzset();
|
||||
}
|
||||
|
||||
return mp_obj_new_str(mpy_time_zone, strlen(mpy_time_zone));
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_timezone_obj, 0, 1, mp_sys_timezone);
|
||||
|
||||
|
||||
//==============================================================
|
||||
STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_mpycore), MP_ROM_PTR(&mp_sys_mpycore_obj) },
|
||||
#ifdef CONFIG_MICROPY_USE_THREADED_REPL
|
||||
{ MP_ROM_QSTR(MP_QSTR_REPL), MP_ROM_PTR(&mp_sys_exec_repl_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stackSize), MP_ROM_PTR(&mp_sys_stack_size_obj) },
|
||||
#endif
|
||||
{ MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_mpycore), MP_ROM_PTR(&mp_sys_mpycore_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_tz), MP_ROM_PTR(&mp_sys_timezone_obj) },
|
||||
#ifdef MICROPY_PY_SYS_PLATFORM
|
||||
{ MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
|
||||
#endif
|
||||
#if MP_ENDIANNESS_LITTLE
|
||||
{ MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
|
||||
#else
|
||||
{ MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
|
||||
#endif
|
||||
|
||||
#if MICROPY_PY_SYS_MAXSIZE
|
||||
@@ -184,37 +261,37 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
|
||||
// to not try to compare sys.maxsize to some literal number (as this
|
||||
// number might not fit in available int size), but instead count number
|
||||
// of "one" bits in sys.maxsize.
|
||||
{ MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
|
||||
#else
|
||||
{ MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if MICROPY_PY_SYS_EXIT
|
||||
{ MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
|
||||
#endif
|
||||
|
||||
#if MICROPY_PY_SYS_STDFILES
|
||||
{ MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
|
||||
#endif
|
||||
|
||||
#if MICROPY_PY_SYS_MODULES
|
||||
{ MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
|
||||
#endif
|
||||
#if MICROPY_PY_SYS_EXC_INFO
|
||||
{ MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
|
||||
#endif
|
||||
#if MICROPY_PY_SYS_GETSIZEOF
|
||||
{ MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Extensions to CPython
|
||||
*/
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
|
||||
|
||||
@@ -36,13 +36,11 @@
|
||||
#include "py/mpthread.h"
|
||||
#include "mphalport.h"
|
||||
|
||||
extern TaskHandle_t MainTaskHandle;
|
||||
|
||||
|
||||
/****************************************************************/
|
||||
// _thread module
|
||||
|
||||
STATIC size_t thread_stack_size = MP_THREAD_DEFAULT_STACK_SIZE;
|
||||
size_t thread_stack_size = MP_THREAD_DEFAULT_STACK_SIZE;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
STATIC mp_obj_t mod_thread_stack_size(size_t n_args, const mp_obj_t *args) {
|
||||
@@ -78,8 +76,8 @@ typedef struct _thread_entry_args_t {
|
||||
mp_obj_t args[];
|
||||
} thread_entry_args_t;
|
||||
|
||||
//--------------------------------------
|
||||
STATIC void *thread_entry(void *args_in)
|
||||
//-------------------------------
|
||||
void *thread_entry(void *args_in)
|
||||
{
|
||||
// Execution begins here for a new thread. We do not have the GIL.
|
||||
|
||||
@@ -312,10 +310,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_thread_isnotified_obj, mod_thread_isnotifie
|
||||
|
||||
//--------------------------------------
|
||||
STATIC mp_obj_t mod_thread_getREPLId() {
|
||||
return mp_obj_new_int_from_uint((uintptr_t)MainTaskHandle);
|
||||
return mp_obj_new_int_from_uint((uintptr_t)ReplTaskHandle);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_getREPLId_obj, mod_thread_getREPLId);
|
||||
|
||||
//--------------------------------------
|
||||
STATIC mp_obj_t mod_thread_getMAINId() {
|
||||
return mp_obj_new_int_from_uint((uintptr_t)MainTaskHandle);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_getMAINId_obj, mod_thread_getMAINId);
|
||||
|
||||
//--------------------------------------
|
||||
STATIC mp_obj_t mod_thread_getnotify() {
|
||||
|
||||
@@ -439,6 +443,7 @@ STATIC mp_obj_t mod_thread_list(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
char th_type[8] = {'\0'};
|
||||
char th_state[16] = {'\0'};
|
||||
if (thr->type == THREAD_TYPE_MAIN) sprintf(th_type, "MAIN");
|
||||
else if (thr->type == THREAD_TYPE_REPL) sprintf(th_type, "REPL");
|
||||
else if (thr->type == THREAD_TYPE_PYTHON) sprintf(th_type, "PYTHON");
|
||||
else if (thr->type == THREAD_TYPE_SERVICE) sprintf(th_type, "SERVICE");
|
||||
else sprintf(th_type, "Unknown");
|
||||
@@ -532,6 +537,19 @@ STATIC mp_obj_t mod_thread_replAcceptMsg(mp_uint_t n_args, const mp_obj_t *args)
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_replAcceptMsg_obj, 0, 1, mod_thread_replAcceptMsg);
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
STATIC mp_obj_t mod_thread_mainAcceptMsg(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
int res = 0;
|
||||
if (n_args == 0) {
|
||||
res = mp_thread_mainAcceptMsg(-1);
|
||||
}
|
||||
else {
|
||||
res = mp_thread_mainAcceptMsg(mp_obj_is_true(args[0]));
|
||||
}
|
||||
return mp_obj_new_bool(res);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_mainAcceptMsg_obj, 0, 1, mod_thread_mainAcceptMsg);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
STATIC mp_obj_t mod_thread_waitnotify(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
uint32_t tmo = portMAX_DELAY;
|
||||
@@ -588,7 +606,9 @@ STATIC const mp_rom_map_elem_t mp_module_thread_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_getnotification), MP_ROM_PTR(&mod_thread_getnotify_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_isnotified), MP_ROM_PTR(&mod_thread_isnotified_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_getReplID), MP_ROM_PTR(&mod_thread_getREPLId_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_getMainID), MP_ROM_PTR(&mod_thread_getMAINId_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_replAcceptMsg), MP_ROM_PTR(&mod_thread_replAcceptMsg_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_mainAcceptMsg), MP_ROM_PTR(&mod_thread_mainAcceptMsg_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_sendmsg), MP_ROM_PTR(&mod_thread_sendmsg_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_getmsg), MP_ROM_PTR(&mod_thread_getmsg_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_list), MP_ROM_PTR(&mod_thread_list_obj) },
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user