2015-12-30 17:51:08 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013-2015 Damien P. George
|
2016-01-05 14:23:23 -05:00
|
|
|
* Copyright (c) 2016 Paul Sokolovsky
|
2015-12-30 17:51:08 -05:00
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
2016-08-07 09:20:01 -04:00
|
|
|
#include <stdio.h>
|
2015-12-30 17:51:08 -05:00
|
|
|
|
|
|
|
#include "py/obj.h"
|
2016-01-05 14:23:23 -05:00
|
|
|
#include "py/runtime.h"
|
2021-07-09 00:19:15 -04:00
|
|
|
#include "shared/runtime/pyexec.h"
|
2018-06-26 17:03:51 -04:00
|
|
|
|
|
|
|
// This needs to be set before we include the RTOS headers
|
|
|
|
#define USE_US_TIMER 1
|
|
|
|
|
2021-08-11 01:14:46 -04:00
|
|
|
#include "extmod/machine_bitstream.h"
|
2015-12-30 17:51:08 -05:00
|
|
|
#include "extmod/machine_mem.h"
|
2017-01-01 11:17:00 -05:00
|
|
|
#include "extmod/machine_signal.h"
|
2016-05-31 09:06:50 -04:00
|
|
|
#include "extmod/machine_pulse.h"
|
2021-06-23 22:37:50 -04:00
|
|
|
#include "extmod/machine_pwm.h"
|
2016-04-12 08:55:20 -04:00
|
|
|
#include "extmod/machine_i2c.h"
|
2020-09-16 00:09:49 -04:00
|
|
|
#include "extmod/machine_spi.h"
|
2016-11-05 18:30:19 -04:00
|
|
|
#include "modmachine.h"
|
2016-01-05 14:23:23 -05:00
|
|
|
|
2016-05-20 08:30:33 -04:00
|
|
|
#include "xtirq.h"
|
2016-01-05 14:23:23 -05:00
|
|
|
#include "os_type.h"
|
|
|
|
#include "osapi.h"
|
2016-02-05 18:36:17 -05:00
|
|
|
#include "etshal.h"
|
2016-04-21 06:43:37 -04:00
|
|
|
#include "ets_alt_task.h"
|
2016-02-17 10:19:03 -05:00
|
|
|
#include "user_interface.h"
|
2015-12-30 17:51:08 -05:00
|
|
|
|
|
|
|
#if MICROPY_PY_MACHINE
|
|
|
|
|
2016-04-21 06:43:37 -04:00
|
|
|
// #define MACHINE_WAKE_IDLE (0x01)
|
|
|
|
// #define MACHINE_WAKE_SLEEP (0x02)
|
|
|
|
#define MACHINE_WAKE_DEEPSLEEP (0x04)
|
|
|
|
|
2016-08-28 07:48:49 -04:00
|
|
|
extern const mp_obj_type_t esp_wdt_type;
|
|
|
|
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
|
2016-02-17 10:19:03 -05:00
|
|
|
if (n_args == 0) {
|
|
|
|
// get
|
|
|
|
return mp_obj_new_int(system_get_cpu_freq() * 1000000);
|
|
|
|
} else {
|
|
|
|
// set
|
|
|
|
mp_int_t freq = mp_obj_get_int(args[0]) / 1000000;
|
|
|
|
if (freq != 80 && freq != 160) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("frequency can only be either 80Mhz or 160MHz"));
|
2016-02-17 10:19:03 -05:00
|
|
|
}
|
|
|
|
system_update_cpu_freq(freq);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq);
|
|
|
|
|
2016-04-04 17:20:25 -04:00
|
|
|
STATIC mp_obj_t machine_reset(void) {
|
|
|
|
system_restart();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
|
|
|
|
|
2020-03-19 22:40:01 -04:00
|
|
|
STATIC mp_obj_t machine_soft_reset(void) {
|
|
|
|
pyexec_system_exit = PYEXEC_FORCED_EXIT;
|
|
|
|
mp_raise_type(&mp_type_SystemExit);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
|
|
|
|
|
2016-04-15 15:07:28 -04:00
|
|
|
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);
|
|
|
|
|
2016-04-04 17:57:49 -04:00
|
|
|
STATIC mp_obj_t machine_unique_id(void) {
|
|
|
|
uint32_t id = system_get_chip_id();
|
|
|
|
return mp_obj_new_bytes((byte *)&id, sizeof(id));
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);
|
|
|
|
|
2016-08-07 08:51:04 -04:00
|
|
|
STATIC mp_obj_t machine_idle(void) {
|
2016-10-04 14:20:47 -04:00
|
|
|
uint32_t t = mp_hal_ticks_cpu();
|
2016-08-07 08:51:04 -04:00
|
|
|
asm ("waiti 0");
|
2016-10-04 14:20:47 -04:00
|
|
|
t = mp_hal_ticks_cpu() - t;
|
2019-02-27 23:44:37 -05:00
|
|
|
ets_event_poll(); // handle any events after possibly a long wait (eg feed WDT)
|
2016-10-04 14:20:47 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(t);
|
2016-08-07 08:51:04 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle);
|
|
|
|
|
2019-01-29 21:39:11 -05:00
|
|
|
STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) {
|
2019-01-29 22:03:43 -05:00
|
|
|
uint32_t max_us = 0xffffffff;
|
|
|
|
if (n_args == 1) {
|
|
|
|
mp_int_t max_ms = mp_obj_get_int(args[0]);
|
|
|
|
if (max_ms < 0) {
|
|
|
|
max_ms = 0;
|
|
|
|
}
|
|
|
|
max_us = max_ms * 1000;
|
|
|
|
}
|
2019-01-30 08:05:00 -05:00
|
|
|
uint32_t wifi_mode = wifi_get_opmode();
|
2019-01-29 22:03:43 -05:00
|
|
|
uint32_t start = system_get_time();
|
|
|
|
while (system_get_time() - start <= max_us) {
|
|
|
|
ets_event_poll();
|
2019-01-30 08:05:00 -05:00
|
|
|
if (wifi_mode == NULL_MODE) {
|
|
|
|
// Can only idle if the wifi is off
|
|
|
|
asm ("waiti 0");
|
|
|
|
}
|
2019-01-29 22:03:43 -05:00
|
|
|
}
|
2016-08-07 09:20:01 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2019-01-29 21:39:11 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine_lightsleep);
|
2016-08-07 09:20:01 -04:00
|
|
|
|
2019-01-29 21:38:42 -05:00
|
|
|
STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) {
|
2016-04-21 06:43:37 -04:00
|
|
|
// 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) {
|
2020-09-13 22:15:03 -04:00
|
|
|
uint64_t t = pyb_rtc_get_us_since_epoch();
|
2016-04-21 06:43:37 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 21:38:42 -05:00
|
|
|
// if an argument is given then that's the maximum time to sleep for
|
|
|
|
if (n_args == 1) {
|
|
|
|
mp_int_t max_ms = mp_obj_get_int(args[0]);
|
|
|
|
if (max_ms <= 0) {
|
|
|
|
max_ms = 1;
|
|
|
|
}
|
|
|
|
uint32_t max_us = max_ms * 1000;
|
|
|
|
if (sleep_us == 0 || max_us < sleep_us) {
|
|
|
|
sleep_us = max_us;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 11:21:26 -04:00
|
|
|
// prepare for RTC reset at wake up
|
|
|
|
rtc_prepare_deepsleep(sleep_us);
|
2016-04-21 06:43:37 -04:00
|
|
|
// 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;
|
|
|
|
}
|
2019-01-29 21:38:42 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep);
|
2016-04-21 06:43:37 -04:00
|
|
|
|
2019-05-08 09:30:57 -04:00
|
|
|
// These values are from the datasheet
|
|
|
|
#define ESP_TIMER_US_MIN (100)
|
|
|
|
#define ESP_TIMER_US_MAX (0xfffffff)
|
|
|
|
#define ESP_TIMER_MS_MAX (0x689d0)
|
|
|
|
|
2016-01-05 14:23:23 -05:00
|
|
|
typedef struct _esp_timer_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
os_timer_t timer;
|
2019-05-08 09:30:57 -04:00
|
|
|
uint32_t remain_ms; // if non-zero, remaining time to handle large periods
|
|
|
|
uint32_t period_ms; // if non-zero, periodic timer with a large period
|
2016-01-05 14:23:23 -05:00
|
|
|
mp_obj_t callback;
|
|
|
|
} esp_timer_obj_t;
|
|
|
|
|
2019-05-08 09:30:57 -04:00
|
|
|
STATIC void esp_timer_arm_ms(esp_timer_obj_t *self, uint32_t ms, bool repeat) {
|
|
|
|
if (ms <= ESP_TIMER_MS_MAX) {
|
|
|
|
self->remain_ms = 0;
|
|
|
|
self->period_ms = 0;
|
|
|
|
} else {
|
|
|
|
self->remain_ms = ms - ESP_TIMER_MS_MAX;
|
|
|
|
if (repeat) {
|
|
|
|
repeat = false;
|
|
|
|
self->period_ms = ms;
|
|
|
|
} else {
|
|
|
|
self->period_ms = 0;
|
|
|
|
}
|
|
|
|
ms = ESP_TIMER_MS_MAX;
|
|
|
|
}
|
|
|
|
os_timer_arm(&self->timer, ms, repeat);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void esp_timer_arm_us(esp_timer_obj_t *self, uint32_t us, bool repeat) {
|
|
|
|
if (us < ESP_TIMER_US_MIN) {
|
|
|
|
us = ESP_TIMER_US_MIN;
|
|
|
|
}
|
|
|
|
if (us <= ESP_TIMER_US_MAX) {
|
|
|
|
self->remain_ms = 0;
|
|
|
|
self->period_ms = 0;
|
|
|
|
os_timer_arm_us(&self->timer, us, repeat);
|
|
|
|
} else {
|
|
|
|
esp_timer_arm_ms(self, us / 1000, repeat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:23:23 -05:00
|
|
|
const mp_obj_type_t esp_timer_type;
|
|
|
|
|
|
|
|
STATIC void esp_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
esp_timer_obj_t *self = self_in;
|
|
|
|
mp_printf(print, "Timer(%p)", &self->timer);
|
|
|
|
}
|
|
|
|
|
2017-01-04 08:10:42 -05:00
|
|
|
STATIC mp_obj_t esp_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2016-01-05 14:23:23 -05:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
2022-04-22 03:09:15 -04:00
|
|
|
esp_timer_obj_t *tim = mp_obj_malloc(esp_timer_obj_t, &esp_timer_type);
|
2016-01-05 14:23:23 -05:00
|
|
|
return tim;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void esp_timer_cb(void *arg) {
|
|
|
|
esp_timer_obj_t *self = arg;
|
2019-05-08 09:30:57 -04:00
|
|
|
if (self->remain_ms != 0) {
|
|
|
|
// Handle periods larger than the maximum system period
|
|
|
|
uint32_t next_period_ms = self->remain_ms;
|
|
|
|
if (next_period_ms > ESP_TIMER_MS_MAX) {
|
|
|
|
next_period_ms = ESP_TIMER_MS_MAX;
|
|
|
|
}
|
|
|
|
self->remain_ms -= next_period_ms;
|
|
|
|
os_timer_arm(&self->timer, next_period_ms, false);
|
|
|
|
} else {
|
|
|
|
mp_sched_schedule(self->callback, self);
|
|
|
|
if (self->period_ms != 0) {
|
|
|
|
// A periodic timer with a larger period: reschedule it
|
|
|
|
esp_timer_arm_ms(self, self->period_ms, true);
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 14:23:23 -05:00
|
|
|
}
|
|
|
|
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t esp_timer_init_helper(esp_timer_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2018-06-26 17:03:51 -04:00
|
|
|
enum {
|
|
|
|
ARG_mode,
|
|
|
|
ARG_callback,
|
|
|
|
ARG_period,
|
|
|
|
ARG_tick_hz,
|
|
|
|
ARG_freq,
|
|
|
|
};
|
2016-01-05 14:23:23 -05:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
|
|
|
|
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2018-06-26 17:03:51 -04:00
|
|
|
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
|
|
|
|
{ MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
#else
|
|
|
|
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
|
|
|
|
#endif
|
2016-01-05 14:23:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2018-06-26 17:03:51 -04:00
|
|
|
self->callback = args[ARG_callback].u_obj;
|
2016-01-07 11:58:52 -05:00
|
|
|
// Be sure to disarm timer before making any changes
|
|
|
|
os_timer_disarm(&self->timer);
|
2016-01-05 14:23:23 -05:00
|
|
|
os_timer_setfn(&self->timer, esp_timer_cb, self);
|
2018-06-26 17:03:51 -04:00
|
|
|
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
if (args[ARG_freq].u_obj != mp_const_none) {
|
|
|
|
mp_float_t freq = mp_obj_get_float(args[ARG_freq].u_obj);
|
|
|
|
if (freq < 0.001) {
|
2019-05-08 09:30:57 -04:00
|
|
|
esp_timer_arm_ms(self, (mp_int_t)(1000 / freq), args[ARG_mode].u_int);
|
2018-06-26 17:03:51 -04:00
|
|
|
} else {
|
2019-05-08 09:30:57 -04:00
|
|
|
esp_timer_arm_us(self, (mp_int_t)(1000000 / freq), args[ARG_mode].u_int);
|
2018-06-26 17:03:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (args[ARG_freq].u_int != 0xffffffff) {
|
2019-05-08 09:30:57 -04:00
|
|
|
esp_timer_arm_us(self, 1000000 / args[ARG_freq].u_int, args[ARG_mode].u_int);
|
2018-06-26 17:03:51 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
mp_int_t period = args[ARG_period].u_int;
|
|
|
|
mp_int_t hz = args[ARG_tick_hz].u_int;
|
|
|
|
if (hz == 1000) {
|
2019-05-08 09:30:57 -04:00
|
|
|
esp_timer_arm_ms(self, period, args[ARG_mode].u_int);
|
2018-06-26 17:03:51 -04:00
|
|
|
} else if (hz == 1000000) {
|
2019-05-08 09:30:57 -04:00
|
|
|
esp_timer_arm_us(self, period, args[ARG_mode].u_int);
|
2018-06-26 17:03:51 -04:00
|
|
|
} else {
|
|
|
|
// Use a long long to ensure that we don't either overflow or loose accuracy
|
|
|
|
uint64_t period_us = (((uint64_t)period) * 1000000) / hz;
|
|
|
|
if (period_us < 0x80000000ull) {
|
2019-05-08 09:30:57 -04:00
|
|
|
esp_timer_arm_us(self, (mp_int_t)period_us, args[ARG_mode].u_int);
|
2018-06-26 17:03:51 -04:00
|
|
|
} else {
|
2019-05-08 09:30:57 -04:00
|
|
|
esp_timer_arm_ms(self, (mp_int_t)(period_us / 1000), args[ARG_mode].u_int);
|
2018-06-26 17:03:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 14:23:23 -05:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t esp_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
2016-01-05 14:23:23 -05:00
|
|
|
return esp_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp_timer_init_obj, 1, esp_timer_init);
|
|
|
|
|
|
|
|
STATIC mp_obj_t esp_timer_deinit(mp_obj_t self_in) {
|
|
|
|
esp_timer_obj_t *self = self_in;
|
|
|
|
os_timer_disarm(&self->timer);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_timer_deinit_obj, esp_timer_deinit);
|
|
|
|
|
2017-07-30 11:13:01 -04:00
|
|
|
STATIC const mp_rom_map_elem_t esp_timer_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&esp_timer_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&esp_timer_init_obj) },
|
|
|
|
// { MP_ROM_QSTR(MP_QSTR_callback), MP_ROM_PTR(&esp_timer_callback_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(false) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(true) },
|
2016-01-05 14:23:23 -05:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(esp_timer_locals_dict, esp_timer_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t esp_timer_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Timer,
|
|
|
|
.print = esp_timer_print,
|
|
|
|
.make_new = esp_timer_make_new,
|
2017-07-30 11:13:01 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&esp_timer_locals_dict,
|
2016-01-05 14:23:23 -05:00
|
|
|
};
|
|
|
|
|
2016-06-01 12:26:49 -04:00
|
|
|
// this bit is unused in the Xtensa PS register
|
|
|
|
#define ETS_LOOP_ITER_BIT (12)
|
|
|
|
|
2016-05-20 08:30:33 -04:00
|
|
|
STATIC mp_obj_t machine_disable_irq(void) {
|
2016-06-01 12:26:49 -04:00
|
|
|
uint32_t state = disable_irq();
|
|
|
|
state = (state & ~(1 << ETS_LOOP_ITER_BIT)) | (ets_loop_iter_disable << ETS_LOOP_ITER_BIT);
|
|
|
|
ets_loop_iter_disable = 1;
|
|
|
|
return mp_obj_new_int(state);
|
2016-05-20 08:30:33 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq);
|
|
|
|
|
2016-06-01 12:26:49 -04:00
|
|
|
STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) {
|
|
|
|
uint32_t state = mp_obj_get_int(state_in);
|
|
|
|
ets_loop_iter_disable = (state >> ETS_LOOP_ITER_BIT) & 1;
|
|
|
|
enable_irq(state & ~(1 << ETS_LOOP_ITER_BIT));
|
2016-05-20 08:30:33 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq);
|
|
|
|
|
2019-06-23 23:18:43 -04:00
|
|
|
// Custom version of this function that feeds system WDT if necessary
|
|
|
|
mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
|
|
|
|
int nchanges = 2;
|
|
|
|
uint32_t start = system_get_time(); // in microseconds
|
|
|
|
for (;;) {
|
|
|
|
uint32_t dt = system_get_time() - start;
|
|
|
|
|
|
|
|
// Check if pin changed to wanted value
|
|
|
|
if (mp_hal_pin_read(pin) == pulse_level) {
|
|
|
|
if (--nchanges == 0) {
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
pulse_level = 1 - pulse_level;
|
|
|
|
start = system_get_time();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for timeout
|
|
|
|
if (dt >= timeout_us) {
|
|
|
|
return (mp_uint_t)-nchanges;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only feed WDT every now and then, to make sure edge timing is accurate
|
|
|
|
if ((dt & 0xffff) == 0xffff && !ets_loop_dont_feed_sw_wdt) {
|
|
|
|
system_soft_wdt_feed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 17:51:08 -05:00
|
|
|
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },
|
2016-01-05 14:23:23 -05:00
|
|
|
|
2016-02-17 10:19:03 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
|
2016-04-04 17:20:25 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
|
2020-03-19 22:40:01 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
|
2016-04-15 15:07:28 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
|
2016-04-04 17:57:49 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
|
2016-08-07 08:51:04 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) },
|
2019-01-29 21:39:11 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) },
|
2016-04-21 06:43:37 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
|
2016-02-17 10:19:03 -05:00
|
|
|
|
2016-05-20 08:30:33 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
|
|
|
|
|
2021-08-11 01:14:46 -04:00
|
|
|
#if MICROPY_PY_MACHINE_BITSTREAM
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
|
|
|
|
#endif
|
|
|
|
|
2016-05-31 09:06:50 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
|
|
|
|
|
2016-04-21 06:43:37 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
|
2016-01-05 14:23:23 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&esp_timer_type) },
|
2016-08-28 07:48:49 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&esp_wdt_type) },
|
2016-02-03 08:23:16 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pyb_pin_type) },
|
2017-01-01 11:17:00 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
|
2021-06-23 22:37:50 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
|
2019-07-19 22:41:20 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
|
2016-04-06 12:45:52 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
|
2018-05-17 09:37:12 -04:00
|
|
|
#if MICROPY_PY_MACHINE_I2C
|
2020-09-15 23:30:48 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
|
2020-09-16 00:09:49 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
|
2018-05-17 09:37:12 -04:00
|
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_SPI
|
2016-12-07 22:41:58 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hspi_type) },
|
2020-09-16 00:09:49 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
|
2018-05-17 09:37:12 -04:00
|
|
|
#endif
|
2016-04-21 06:43:37 -04:00
|
|
|
|
|
|
|
// wake abilities
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) },
|
|
|
|
|
|
|
|
// reset causes
|
2016-09-07 22:50:38 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(REASON_DEFAULT_RST) },
|
2016-04-21 06:43:37 -04:00
|
|
|
{ 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) },
|
2016-09-04 12:45:58 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(REASON_WDT_RST) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(REASON_SOFT_RESTART) },
|
2015-12-30 17:51:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_machine = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&machine_module_globals,
|
|
|
|
};
|
|
|
|
|
2022-04-20 02:06:22 -04:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_umachine, mp_module_machine, MICROPY_PY_MACHINE);
|
|
|
|
|
2015-12-30 17:51:08 -05:00
|
|
|
#endif // MICROPY_PY_MACHINE
|