Files
Boris Lovosevic 7ca7a402f3 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()'
2018-04-18 13:52:42 +02:00

305 lines
10 KiB
C

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2014-2017 Paul Sokolovsky
* Copyright (c) 2018 LoBo (https://github.com/loboris)
*
* 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.
*/
#include "sdkconfig.h"
#include <time.h>
#include <string.h>
#include "py/builtin.h"
#include "py/objlist.h"
#include "py/objtuple.h"
#include "py/objstr.h"
#include "py/objint.h"
#include "py/objtype.h"
#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
#include "esp32/mpversion.h"
// defined per port; type of these is irrelevant, just need pointer
extern struct _mp_dummy_t mp_sys_stdin_obj;
extern struct _mp_dummy_t mp_sys_stdout_obj;
extern struct _mp_dummy_t mp_sys_stderr_obj;
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
#endif
// version - Python language version that this implementation conforms to, as a string
STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
// version_info - Python language version that this implementation conforms to, as a tuple of ints
#define I(n) MP_OBJ_NEW_SMALL_INT(n)
// TODO: CPython is now at 5-element array, but save 2 els so far...
STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}};
// sys.implementation object
// this holds the MicroPython version
STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
{&mp_type_tuple},
3,
{ I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
};
#if MICROPY_PY_ATTRTUPLE
STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
STATIC MP_DEFINE_ATTRTUPLE(
mp_sys_implementation_obj,
impl_fields,
2,
MP_ROM_QSTR(MP_QSTR_micropython),
MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
);
#else
STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
{&mp_type_tuple},
2,
{
MP_ROM_QSTR(MP_QSTR_micropython),
MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
}
};
#endif
#undef I
#ifdef MICROPY_PY_SYS_PLATFORM
// platform - the platform that MicroPython is running on
STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
#endif
// exit([retval]): raise SystemExit, with optional argument given to the exception
STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
mp_obj_t exc;
if (n_args == 0) {
exc = mp_obj_new_exception(&mp_type_SystemExit);
} else {
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);
STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
void *stream_obj = &mp_sys_stdout_obj;
if (n_args > 1) {
stream_obj = MP_OBJ_TO_PTR(args[1]); // XXX may fail
}
mp_print_t print = {stream_obj, mp_stream_write_adaptor};
mp_obj_print_exception(&print, args[0]);
#else
(void)n_args;
mp_obj_print_exception(&mp_plat_print, args[0]);
#endif
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
#if MICROPY_PY_SYS_EXC_INFO
STATIC mp_obj_t mp_sys_exc_info(void) {
mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
if (cur_exc == MP_OBJ_NULL) {
t->items[0] = mp_const_none;
t->items[1] = mp_const_none;
t->items[2] = mp_const_none;
return MP_OBJ_FROM_PTR(t);
}
t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
t->items[1] = cur_exc;
t->items[2] = mp_const_none;
return MP_OBJ_FROM_PTR(t);
}
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
#endif
#if MICROPY_PY_SYS_GETSIZEOF
STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
#endif
STATIC mp_obj_t mp_sys_mpycore() {
mp_obj_t tuple[2];
tuple[0] = mp_obj_new_str(MICROPY_CORE_VERSION, strlen(MICROPY_CORE_VERSION));
tuple[1] = mp_obj_new_str(MICROPY_CORE_DATE, strlen(MICROPY_CORE_DATE));
return mp_obj_new_tuple(2, tuple);
}
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) },
#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) },
#endif
#if MP_ENDIANNESS_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) },
#endif
#if MICROPY_PY_SYS_MAXSIZE
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
// Maximum mp_int_t value is not representable as small int, so we have
// little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
// 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) },
#else
{ 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) },
#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) },
#endif
#if MICROPY_PY_SYS_MODULES
{ 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) },
#endif
#if MICROPY_PY_SYS_GETSIZEOF
{ 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) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
const mp_obj_module_t mp_module_sys = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_sys_globals,
};
#endif