summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2020-11-19 00:29:14 -0500
committerDan Halbert <halbert@halwitz.org>2020-11-19 00:29:14 -0500
commit5bb3c321e9f19b657363e2b19a8f5b25c4d5eb49 (patch)
tree7221653786d8179ebd4a686a9deedc6d52f1f66f /ports
parent682054a2169c108ad69e84acdb53fdd26405077e (diff)
parent8ac9b176c961faf7672f081953de6f78ec3005e2 (diff)
merge from main
Diffstat (limited to 'ports')
-rw-r--r--ports/esp32s2/common-hal/microcontroller/__init__.c11
-rw-r--r--ports/esp32s2/common-hal/pulseio/PulseIn.c8
-rw-r--r--ports/esp32s2/common-hal/watchdog/WatchDogMode.c1
-rw-r--r--ports/esp32s2/common-hal/watchdog/WatchDogTimer.c93
-rw-r--r--ports/esp32s2/common-hal/watchdog/WatchDogTimer.h43
-rw-r--r--ports/esp32s2/common-hal/watchdog/__init__.c1
-rw-r--r--ports/esp32s2/common-hal/wifi/Radio.c2
-rw-r--r--ports/esp32s2/common-hal/wifi/__init__.c2
m---------ports/esp32s2/esp-idf0
-rw-r--r--ports/esp32s2/mpconfigport.mk1
-rw-r--r--ports/esp32s2/supervisor/port.c19
-rw-r--r--ports/nrf/common-hal/watchdog/WatchDogTimer.c3
12 files changed, 174 insertions, 10 deletions
diff --git a/ports/esp32s2/common-hal/microcontroller/__init__.c b/ports/esp32s2/common-hal/microcontroller/__init__.c
index ab0e1bfaa..79cb93893 100644
--- a/ports/esp32s2/common-hal/microcontroller/__init__.c
+++ b/ports/esp32s2/common-hal/microcontroller/__init__.c
@@ -91,6 +91,17 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
},
};
+#if CIRCUITPY_WATCHDOG
+// The singleton watchdog.WatchDogTimer object.
+watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
+ .base = {
+ .type = &watchdog_watchdogtimer_type,
+ },
+ .timeout = 0.0f,
+ .mode = WATCHDOGMODE_NONE,
+};
+#endif
+
// This maps MCU pin names to pin objects.
STATIC const mp_rom_map_elem_t mcu_pin_global_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_GPIO0), MP_ROM_PTR(&pin_GPIO0) },
diff --git a/ports/esp32s2/common-hal/pulseio/PulseIn.c b/ports/esp32s2/common-hal/pulseio/PulseIn.c
index f7429ec12..9feeea147 100644
--- a/ports/esp32s2/common-hal/pulseio/PulseIn.c
+++ b/ports/esp32s2/common-hal/pulseio/PulseIn.c
@@ -77,7 +77,9 @@ void pulsein_reset(void) {
for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) {
handles[i] = NULL;
}
- supervisor_disable_tick();
+ if (refcount != 0) {
+ supervisor_disable_tick();
+ }
refcount = 0;
}
@@ -122,8 +124,10 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu
// start RMT RX, and enable ticks so the core doesn't turn off.
rmt_rx_start(channel, true);
- supervisor_enable_tick();
refcount++;
+ if (refcount == 1) {
+ supervisor_enable_tick();
+ }
}
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
diff --git a/ports/esp32s2/common-hal/watchdog/WatchDogMode.c b/ports/esp32s2/common-hal/watchdog/WatchDogMode.c
new file mode 100644
index 000000000..fc4e0e008
--- /dev/null
+++ b/ports/esp32s2/common-hal/watchdog/WatchDogMode.c
@@ -0,0 +1 @@
+// No watchdog module functions.
diff --git a/ports/esp32s2/common-hal/watchdog/WatchDogTimer.c b/ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
new file mode 100644
index 000000000..b51a391b7
--- /dev/null
+++ b/ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
@@ -0,0 +1,93 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * 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 "common-hal/watchdog/WatchDogTimer.h"
+
+#include "shared-bindings/watchdog/__init__.h"
+#include "shared-bindings/microcontroller/__init__.h"
+
+#include "esp_task_wdt.h"
+
+void esp_task_wdt_isr_user_handler(void) {
+ mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception));
+ MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception;
+#if MICROPY_ENABLE_SCHEDULER
+ if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
+ MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
+ }
+#endif
+}
+
+void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
+ if (esp_task_wdt_reset() != ESP_OK) {
+ mp_raise_RuntimeError(translate("watchdog not initialized"));
+ }
+}
+
+void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
+ if (esp_task_wdt_deinit() == ESP_OK) {
+ self->mode = WATCHDOGMODE_NONE;
+ }
+}
+
+void watchdog_reset(void) {
+ common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj);
+}
+
+static void wdt_config(watchdog_watchdogtimer_obj_t *self) {
+ // enable panic hanler in WATCHDOGMODE_RESET mode
+ // initialize Task Watchdog Timer (TWDT)
+ if (esp_task_wdt_init((uint32_t)self->timeout, (self->mode == WATCHDOGMODE_RESET)) != ESP_OK) {
+ mp_raise_RuntimeError(translate("Initialization failed due to lack of memory"));
+ }
+ esp_task_wdt_add(NULL);
+}
+
+mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
+ return self->timeout;
+}
+
+void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) {
+ if ((uint64_t)new_timeout > UINT32_MAX) {
+ mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
+ }
+ if ((uint32_t)self->timeout != (uint32_t)new_timeout) {
+ self->timeout = new_timeout;
+ wdt_config(self);
+ }
+}
+
+watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
+ return self->mode;
+}
+
+void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) {
+ if (self->mode != new_mode) {
+ self->mode = new_mode;
+ wdt_config(self);
+ }
+}
diff --git a/ports/esp32s2/common-hal/watchdog/WatchDogTimer.h b/ports/esp32s2/common-hal/watchdog/WatchDogTimer.h
new file mode 100644
index 000000000..04d9aeee6
--- /dev/null
+++ b/ports/esp32s2/common-hal/watchdog/WatchDogTimer.h
@@ -0,0 +1,43 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * 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.
+ */
+
+#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
+
+#include "py/obj.h"
+#include "shared-bindings/watchdog/WatchDogMode.h"
+#include "shared-bindings/watchdog/WatchDogTimer.h"
+
+struct _watchdog_watchdogtimer_obj_t {
+ mp_obj_base_t base;
+ mp_float_t timeout;
+ watchdog_watchdogmode_t mode;
+};
+
+// This needs to be called in order to disable the watchdog
+void watchdog_reset(void);
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
diff --git a/ports/esp32s2/common-hal/watchdog/__init__.c b/ports/esp32s2/common-hal/watchdog/__init__.c
new file mode 100644
index 000000000..fc4e0e008
--- /dev/null
+++ b/ports/esp32s2/common-hal/watchdog/__init__.c
@@ -0,0 +1 @@
+// No watchdog module functions.
diff --git a/ports/esp32s2/common-hal/wifi/Radio.c b/ports/esp32s2/common-hal/wifi/Radio.c
index 275bae31a..1945da207 100644
--- a/ports/esp32s2/common-hal/wifi/Radio.c
+++ b/ports/esp32s2/common-hal/wifi/Radio.c
@@ -72,6 +72,8 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
return;
}
if (!self->started && enabled) {
+ // esp_wifi_start() would default to soft-AP, thus setting it to station
+ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
self->started = true;
return;
diff --git a/ports/esp32s2/common-hal/wifi/__init__.c b/ports/esp32s2/common-hal/wifi/__init__.c
index 833ef0623..2eb3719b4 100644
--- a/ports/esp32s2/common-hal/wifi/__init__.c
+++ b/ports/esp32s2/common-hal/wifi/__init__.c
@@ -143,8 +143,8 @@ void wifi_reset(void) {
radio->handler_instance_got_ip));
ESP_ERROR_CHECK(esp_wifi_deinit());
esp_netif_destroy(radio->netif);
+ ESP_ERROR_CHECK(esp_event_loop_delete_default());
radio->netif = NULL;
- ESP_ERROR_CHECK(esp_netif_deinit());
}
void ipaddress_ipaddress_to_esp_idf(mp_obj_t ip_address, ip_addr_t* esp_ip_address) {
diff --git a/ports/esp32s2/esp-idf b/ports/esp32s2/esp-idf
-Subproject 8bc19ba893e5544d571a753d82b44a84799b94b
+Subproject d06744f5efc382c61cbad8758107cec308feef0
diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk
index dea5d4dc1..9d6c007ba 100644
--- a/ports/esp32s2/mpconfigport.mk
+++ b/ports/esp32s2/mpconfigport.mk
@@ -27,6 +27,7 @@ CIRCUITPY_NVM = 0
# We don't have enough endpoints to include MIDI.
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_WIFI = 1
+CIRCUITPY_WATCHDOG ?= 1
CIRCUITPY_ESPIDF = 1
ifndef CIRCUITPY_TOUCHIO_USE_NATIVE
diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c
index f25368472..876ad739d 100644
--- a/ports/esp32s2/supervisor/port.c
+++ b/ports/esp32s2/supervisor/port.c
@@ -36,13 +36,14 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
+#include "common-hal/microcontroller/Pin.h"
#include "common-hal/analogio/AnalogOut.h"
#include "common-hal/busio/I2C.h"
#include "common-hal/busio/SPI.h"
#include "common-hal/busio/UART.h"
-#include "common-hal/pwmio/PWMOut.h"
#include "common-hal/pulseio/PulseIn.h"
-#include "common-hal/microcontroller/Pin.h"
+#include "common-hal/pwmio/PWMOut.h"
+#include "common-hal/watchdog/WatchDogTimer.h"
#include "common-hal/wifi/__init__.h"
#include "supervisor/memory.h"
#include "supervisor/shared/tick.h"
@@ -126,6 +127,10 @@ void reset_port(void) {
rtc_reset();
#endif
+#if CIRCUITPY_WATCHDOG
+ watchdog_reset();
+#endif
+
#if CIRCUITPY_WIFI
wifi_reset();
#endif
@@ -186,14 +191,14 @@ uint32_t port_get_saved_word(void) {
}
uint64_t port_get_raw_ticks(uint8_t* subticks) {
- struct timeval tv_now;
- gettimeofday(&tv_now, NULL);
- // convert usec back to ticks
- uint64_t all_subticks = (uint64_t)(tv_now.tv_usec * 2) / 71;
+ // Convert microseconds to subticks of 1/32768 seconds
+ // 32768/1000000 = 64/15625 in lowest terms
+ // this arithmetic overflows after 570 years
+ int64_t all_subticks = esp_timer_get_time() * 512 / 15625;
if (subticks != NULL) {
*subticks = all_subticks % 32;
}
- return (uint64_t)tv_now.tv_sec * 1024L + all_subticks / 32;
+ return all_subticks / 32;
}
// Enable 1/1024 second tick.
diff --git a/ports/nrf/common-hal/watchdog/WatchDogTimer.c b/ports/nrf/common-hal/watchdog/WatchDogTimer.c
index bec0ac473..539b43e76 100644
--- a/ports/nrf/common-hal/watchdog/WatchDogTimer.c
+++ b/ports/nrf/common-hal/watchdog/WatchDogTimer.c
@@ -93,6 +93,9 @@ void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
}
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
+ if (self->mode == WATCHDOGMODE_RESET) {
+ mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
+ }
if (timer) {
timer_free();
}