Skip to content

Commit

Permalink
Added build time option to start REPL in separate thread
Browse files Browse the repository at this point in the history
  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()'
  • Loading branch information
loboris committed Apr 18, 2018
1 parent 09e4111 commit 7ca7a40
Show file tree
Hide file tree
Showing 41 changed files with 1,675 additions and 118 deletions.
15 changes: 12 additions & 3 deletions MicroPython_BUILD/components/micropython/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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.

Expand Down
56 changes: 44 additions & 12 deletions MicroPython_BUILD/components/micropython/esp32/machine_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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[] = {
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions MicroPython_BUILD/components/micropython/esp32/machine_rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
85 changes: 77 additions & 8 deletions MicroPython_BUILD/components/micropython/esp32/machine_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)"));
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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[] = {
Expand All @@ -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) },
Expand All @@ -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);

Expand Down
Loading

0 comments on commit 7ca7a40

Please sign in to comment.