From a525493e40eb4d89e805737bfa36b39d35221efb Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 12 Apr 2016 13:55:20 +0100 Subject: esp8266: Switch from using custom I2C driver to generic extmod one. --- esp8266/modmachine.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'esp8266/modmachine.c') diff --git a/esp8266/modmachine.c b/esp8266/modmachine.c index 96f9845dc1..8fb88fafd7 100644 --- a/esp8266/modmachine.c +++ b/esp8266/modmachine.c @@ -30,6 +30,7 @@ #include "py/obj.h" #include "py/runtime.h" #include "extmod/machine_mem.h" +#include "extmod/machine_i2c.h" #include "utils.h" #include "modpyb.h" @@ -159,7 +160,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&pyb_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) }, }; -- cgit v1.2.3 From 050e645ef2380d14a2833edb509d4f7498f6d035 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 15 Apr 2016 22:07:28 +0300 Subject: esp8266/modmachine: Add reset_cause() function. --- esp8266/modmachine.c | 6 ++++++ esp8266/qstrdefsport.h | 1 + 2 files changed, 7 insertions(+) (limited to 'esp8266/modmachine.c') diff --git a/esp8266/modmachine.c b/esp8266/modmachine.c index 8fb88fafd7..83fb8d3504 100644 --- a/esp8266/modmachine.c +++ b/esp8266/modmachine.c @@ -64,6 +64,11 @@ STATIC mp_obj_t machine_reset(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); +STATIC mp_obj_t machine_reset_cause(void) { + return MP_OBJ_NEW_SMALL_INT(system_get_rst_info()->reason); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); + STATIC mp_obj_t machine_unique_id(void) { uint32_t id = system_get_chip_id(); return mp_obj_new_bytes((byte*)&id, sizeof(id)); @@ -153,6 +158,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, + { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&esp_timer_type) }, diff --git a/esp8266/qstrdefsport.h b/esp8266/qstrdefsport.h index cb5168016b..34f7452069 100644 --- a/esp8266/qstrdefsport.h +++ b/esp8266/qstrdefsport.h @@ -214,6 +214,7 @@ Q(time) // machine Q(reset) +Q(reset_cause) Q(Timer) Q(callback) Q(deinit) -- cgit v1.2.3 From 32d7cf6e441660caed00620309d1faa4b7df5dd4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 Apr 2016 11:43:37 +0100 Subject: esp8266: Implement basic deep-sleep capabilities. Use the machine.deepsleep() function to enter the sleep mode. Use the RTC to configure the alarm to wake the device. Basic use is the following: import machine # configure RTC's ALARM0 to wake device from deep sleep rtc = machine.RTC() rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP) # do other things # ... # set ALARM0's alarm to wake after 10 seconds rtc.alarm(rtc.ALARM0, 10000) # enter deep-sleep state (system is reset upon waking) machine.deepsleep() To detect if the system woke from a deep sleep use: if machine.reset_cause() == machine.DEEPSLEEP_RESET: print('woke from deep sleep') --- esp8266/modmachine.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ esp8266/modpybrtc.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ esp8266/modpybrtc.h | 3 +++ 3 files changed, 101 insertions(+) (limited to 'esp8266/modmachine.c') diff --git a/esp8266/modmachine.c b/esp8266/modmachine.c index 83fb8d3504..c75f6e528d 100644 --- a/esp8266/modmachine.c +++ b/esp8266/modmachine.c @@ -33,14 +33,20 @@ #include "extmod/machine_i2c.h" #include "utils.h" #include "modpyb.h" +#include "modpybrtc.h" #include "os_type.h" #include "osapi.h" #include "etshal.h" +#include "ets_alt_task.h" #include "user_interface.h" #if MICROPY_PY_MACHINE +//#define MACHINE_WAKE_IDLE (0x01) +//#define MACHINE_WAKE_SLEEP (0x02) +#define MACHINE_WAKE_DEEPSLEEP (0x04) + STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) { if (n_args == 0) { // get @@ -75,6 +81,40 @@ STATIC mp_obj_t machine_unique_id(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); +STATIC mp_obj_t machine_deepsleep(void) { + // default to sleep forever + uint32_t sleep_us = 0; + + // see if RTC.ALARM0 should wake the device + if (pyb_rtc_alarm0_wake & MACHINE_WAKE_DEEPSLEEP) { + uint64_t t = pyb_rtc_get_us_since_2000(); + if (pyb_rtc_alarm0_expiry <= t) { + sleep_us = 1; // alarm already expired so wake immediately + } else { + uint64_t delta = pyb_rtc_alarm0_expiry - t; + if (delta <= 0xffffffff) { + // sleep for the desired time + sleep_us = delta; + } else { + // overflow, just set to maximum sleep time + sleep_us = 0xffffffff; + } + } + } + + // put the device in a deep-sleep state + system_deep_sleep_set_option(0); // default power down mode; TODO check this + system_deep_sleep(sleep_us); + + for (;;) { + // we must not return + ets_loop_iter(); + } + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_deepsleep_obj, machine_deepsleep); + typedef struct _esp_timer_obj_t { mp_obj_base_t base; os_timer_t timer; @@ -160,7 +200,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, + { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) }, + { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&esp_timer_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pyb_pin_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&pyb_pwm_type) }, @@ -168,6 +210,14 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) }, + + // wake abilities + { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) }, + + // reset causes + { MP_ROM_QSTR(MP_QSTR_PWR_ON_RESET), MP_ROM_INT(REASON_EXT_SYS_RST) }, + { MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(REASON_EXT_SYS_RST) }, + { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_ROM_INT(REASON_DEEP_SLEEP_AWAKE) }, }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); diff --git a/esp8266/modpybrtc.c b/esp8266/modpybrtc.c index 594c34b157..e62dc88175 100644 --- a/esp8266/modpybrtc.c +++ b/esp8266/modpybrtc.c @@ -49,6 +49,10 @@ typedef struct _pyb_rtc_obj_t { // singleton RTC object STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}}; +// ALARM0 state +uint32_t pyb_rtc_alarm0_wake; // see MACHINE_WAKE_xxx constants +uint64_t pyb_rtc_alarm0_expiry; // in microseconds + void mp_hal_rtc_init(void) { uint32_t magic; @@ -61,6 +65,10 @@ void mp_hal_rtc_init(void) { system_rtc_mem_write(MEM_CAL_ADDR, &cal, sizeof(cal)); system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta)); } + + // reset ALARM0 state + pyb_rtc_alarm0_wake = 0; + pyb_rtc_alarm0_expiry = 0; } STATIC mp_obj_t pyb_rtc_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { @@ -177,9 +185,49 @@ STATIC mp_obj_t pyb_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_memory_obj, 1, 2, pyb_rtc_memory); +STATIC mp_obj_t pyb_rtc_alarm(mp_obj_t self_in, mp_obj_t alarm_id, mp_obj_t time_in) { + (void)self_in; // unused + + // check we want alarm0 + if (mp_obj_get_int(alarm_id) != 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid alarm")); + } + + // set expiry time (in microseconds) + pyb_rtc_alarm0_expiry = pyb_rtc_get_us_since_2000() + mp_obj_get_int(time_in) * 1000; + + return mp_const_none; + +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_rtc_alarm_obj, pyb_rtc_alarm); + +STATIC mp_obj_t pyb_rtc_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_trigger, ARG_wake }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_trigger, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_wake, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // check we want alarm0 + if (args[ARG_trigger].u_int != 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid alarm")); + } + + // set the wake value + pyb_rtc_alarm0_wake = args[ARG_wake].u_int; + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_rtc_irq_obj, 1, pyb_rtc_irq); + STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_memory), (mp_obj_t)&pyb_rtc_memory_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_alarm), (mp_obj_t)&pyb_rtc_alarm_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_irq), (mp_obj_t)&pyb_rtc_irq_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ALARM0), MP_OBJ_NEW_SMALL_INT(0) }, }; STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table); diff --git a/esp8266/modpybrtc.h b/esp8266/modpybrtc.h index 2a982d38fa..b4ca780712 100644 --- a/esp8266/modpybrtc.h +++ b/esp8266/modpybrtc.h @@ -24,6 +24,9 @@ * THE SOFTWARE. */ +extern uint32_t pyb_rtc_alarm0_wake; +extern uint64_t pyb_rtc_alarm0_expiry; + void pyb_rtc_set_us_since_2000(uint64_t nowus); uint64_t pyb_rtc_get_us_since_2000(); -- cgit v1.2.3 From 6d103b65482d8dc1d688fcfd59b3400ebfbe0f0f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 25 Apr 2016 19:28:12 +0300 Subject: py: Move call_function_*_protected() functions to py/ for reuse. They almost certainly needed by any C code which calls Python callbacks. --- esp8266/Makefile | 1 - esp8266/modesp.c | 1 - esp8266/modmachine.c | 3 +-- esp8266/modpybpin.c | 3 +-- esp8266/utils.c | 50 -------------------------------------------------- esp8266/utils.h | 29 ----------------------------- py/py.mk | 1 + py/runtime.h | 3 +++ py/runtime_utils.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 56 insertions(+), 85 deletions(-) delete mode 100644 esp8266/utils.c delete mode 100644 esp8266/utils.h create mode 100644 py/runtime_utils.c (limited to 'esp8266/modmachine.c') diff --git a/esp8266/Makefile b/esp8266/Makefile index c49fdd2c25..6db8003ed8 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -76,7 +76,6 @@ SRC_C = \ moduos.c \ modmachine.c \ modonewire.c \ - utils.c \ ets_alt_task.c \ $(BUILD)/frozen.c \ fatfs_port.c \ diff --git a/esp8266/modesp.c b/esp8266/modesp.c index fd54ae0704..a6038ab39d 100644 --- a/esp8266/modesp.c +++ b/esp8266/modesp.c @@ -40,7 +40,6 @@ #include "user_interface.h" #include "espconn.h" #include "spi_flash.h" -#include "utils.h" #include "espneopixel.h" #include "modpyb.h" diff --git a/esp8266/modmachine.c b/esp8266/modmachine.c index c75f6e528d..c3c9494ac8 100644 --- a/esp8266/modmachine.c +++ b/esp8266/modmachine.c @@ -31,7 +31,6 @@ #include "py/runtime.h" #include "extmod/machine_mem.h" #include "extmod/machine_i2c.h" -#include "utils.h" #include "modpyb.h" #include "modpybrtc.h" @@ -137,7 +136,7 @@ STATIC mp_obj_t esp_timer_make_new(const mp_obj_type_t *type, mp_uint_t n_args, STATIC void esp_timer_cb(void *arg) { esp_timer_obj_t *self = arg; - call_function_1_protected(self->callback, self); + mp_call_function_1_protected(self->callback, self); } STATIC mp_obj_t esp_timer_init_helper(esp_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { diff --git a/esp8266/modpybpin.c b/esp8266/modpybpin.c index a65911cbd1..ca2b87d935 100644 --- a/esp8266/modpybpin.c +++ b/esp8266/modpybpin.c @@ -37,7 +37,6 @@ #include "py/runtime.h" #include "py/gc.h" #include "modpyb.h" -#include "utils.h" #define GET_TRIGGER(phys_port) \ GPIO_PIN_INT_TYPE_GET(GPIO_REG_READ(GPIO_PIN_ADDR(phys_port))) @@ -105,7 +104,7 @@ void pin_intr_handler(uint32_t status) { if (status & 1) { mp_obj_t handler = MP_STATE_PORT(pin_irq_handler)[p]; if (handler != MP_OBJ_NULL) { - call_function_1_protected(handler, MP_OBJ_FROM_PTR(&pyb_pin_obj[p])); + mp_call_function_1_protected(handler, MP_OBJ_FROM_PTR(&pyb_pin_obj[p])); } } } diff --git a/esp8266/utils.c b/esp8266/utils.c deleted file mode 100644 index b2bdcffbe5..0000000000 --- a/esp8266/utils.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2015 Josef Gajdusek - * Copyright (c) 2015 Paul Sokolovsky - * - * 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 "py/runtime.h" -#include "py/obj.h" -#include "py/nlr.h" - -void call_function_1_protected(mp_obj_t fun, mp_obj_t arg) { - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - mp_call_function_1(fun, arg); - nlr_pop(); - } else { - mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); - } -} - -void call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) { - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - mp_call_function_2(fun, arg1, arg2); - nlr_pop(); - } else { - mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); - } -} diff --git a/esp8266/utils.h b/esp8266/utils.h deleted file mode 100644 index c6a4f1f3e6..0000000000 --- a/esp8266/utils.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2015 Josef Gajdusek - * Copyright (c) 2015 Paul Sokolovsky - * - * 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. - */ - -void call_function_1_protected(mp_obj_t fun, mp_obj_t arg); -void call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2); diff --git a/py/py.mk b/py/py.mk index c6360599f6..74aaf4510c 100644 --- a/py/py.mk +++ b/py/py.mk @@ -100,6 +100,7 @@ PY_O_BASENAME = \ parsenum.o \ emitglue.o \ runtime.o \ + runtime_utils.o \ nativeglue.o \ stackctrl.o \ argcheck.o \ diff --git a/py/runtime.h b/py/runtime.h index 1b58f4728f..3e325a31b3 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -95,6 +95,9 @@ mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2); mp_obj_t mp_call_function_n_kw(mp_obj_t fun, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args); mp_obj_t mp_call_method_n_kw(mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args); mp_obj_t mp_call_method_n_kw_var(bool have_self, mp_uint_t n_args_n_kw, const mp_obj_t *args); +// Call function and catch/dump exception - for Python callbacks from C code +void mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg); +void mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2); typedef struct _mp_call_args_t { mp_obj_t fun; diff --git a/py/runtime_utils.c b/py/runtime_utils.c new file mode 100644 index 0000000000..0b0aa4e50e --- /dev/null +++ b/py/runtime_utils.c @@ -0,0 +1,50 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2015 Josef Gajdusek + * Copyright (c) 2015 Paul Sokolovsky + * + * 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 "py/runtime.h" +#include "py/obj.h" +#include "py/nlr.h" + +void mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg) { + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_call_function_1(fun, arg); + nlr_pop(); + } else { + mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); + } +} + +void mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) { + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_call_function_2(fun, arg1, arg2); + nlr_pop(); + } else { + mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); + } +} -- cgit v1.2.3