summaryrefslogtreecommitdiff
path: root/shared-bindings/microcontroller
diff options
context:
space:
mode:
Diffstat (limited to 'shared-bindings/microcontroller')
-rw-r--r--shared-bindings/microcontroller/Processor.c18
-rw-r--r--shared-bindings/microcontroller/Processor.h2
-rw-r--r--shared-bindings/microcontroller/ResetReason.c77
-rw-r--r--shared-bindings/microcontroller/ResetReason.h45
-rw-r--r--shared-bindings/microcontroller/__init__.c11
-rw-r--r--shared-bindings/microcontroller/__init__.h4
6 files changed, 144 insertions, 13 deletions
diff --git a/shared-bindings/microcontroller/Processor.c b/shared-bindings/microcontroller/Processor.c
index 8c703891d..90cc02fe3 100644
--- a/shared-bindings/microcontroller/Processor.c
+++ b/shared-bindings/microcontroller/Processor.c
@@ -67,6 +67,23 @@ const mp_obj_property_t mcu_processor_frequency_obj = {
},
};
+//| reset_reason: microcontroller.ResetReason
+//| """The reason the microcontroller started up from reset state."""
+//|
+STATIC mp_obj_t mcu_processor_get_reset_reason(mp_obj_t self) {
+ return cp_enum_find(&mcu_reset_reason_type, common_hal_mcu_processor_get_reset_reason());
+}
+
+MP_DEFINE_CONST_FUN_OBJ_1(mcu_processor_get_reset_reason_obj, mcu_processor_get_reset_reason);
+
+const mp_obj_property_t mcu_processor_reset_reason_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&mcu_processor_get_reset_reason_obj, // getter
+ (mp_obj_t)&mp_const_none_obj, // no setter
+ (mp_obj_t)&mp_const_none_obj, // no deleter
+ },
+};
+
//| temperature: Optional[float]
//| """The on-chip temperature, in Celsius, as a float. (read-only)
//|
@@ -128,6 +145,7 @@ const mp_obj_property_t mcu_processor_voltage_obj = {
STATIC const mp_rom_map_elem_t mcu_processor_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&mcu_processor_frequency_obj) },
+ { MP_ROM_QSTR(MP_QSTR_reset_reason), MP_ROM_PTR(&mcu_processor_reset_reason_obj) },
{ MP_ROM_QSTR(MP_QSTR_temperature), MP_ROM_PTR(&mcu_processor_temperature_obj) },
{ MP_ROM_QSTR(MP_QSTR_uid), MP_ROM_PTR(&mcu_processor_uid_obj) },
{ MP_ROM_QSTR(MP_QSTR_voltage), MP_ROM_PTR(&mcu_processor_voltage_obj) },
diff --git a/shared-bindings/microcontroller/Processor.h b/shared-bindings/microcontroller/Processor.h
index 0f520f940..98d479087 100644
--- a/shared-bindings/microcontroller/Processor.h
+++ b/shared-bindings/microcontroller/Processor.h
@@ -30,10 +30,12 @@
#include "py/obj.h"
#include "common-hal/microcontroller/Processor.h"
+#include "shared-bindings/microcontroller/ResetReason.h"
extern const mp_obj_type_t mcu_processor_type;
uint32_t common_hal_mcu_processor_get_frequency(void);
+mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void);
float common_hal_mcu_processor_get_temperature(void);
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]);
float common_hal_mcu_processor_get_voltage(void);
diff --git a/shared-bindings/microcontroller/ResetReason.c b/shared-bindings/microcontroller/ResetReason.c
new file mode 100644
index 000000000..61891934a
--- /dev/null
+++ b/shared-bindings/microcontroller/ResetReason.c
@@ -0,0 +1,77 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
+ *
+ * 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/obj.h"
+#include "py/enum.h"
+
+#include "shared-bindings/microcontroller/ResetReason.h"
+
+MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, POWER_ON, RESET_REASON_POWER_ON);
+MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, BROWNOUT, RESET_REASON_BROWNOUT);
+MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, SOFTWARE, RESET_REASON_SOFTWARE);
+MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, DEEP_SLEEP_ALARM, RESET_REASON_DEEP_SLEEP_ALARM);
+MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, RESET_PIN, RESET_REASON_RESET_PIN);
+MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, WATCHDOG, RESET_REASON_WATCHDOG);
+MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, UNKNOWN, RESET_REASON_UNKNOWN);
+
+//| class ResetReason:
+//| """The reason the microntroller was last reset"""
+//|
+//| POWER_ON: object
+//| """The microntroller was started from power off."""
+//|
+//| BROWNOUT: object
+//| """The microntroller was reset due to too low a voltage."""
+//|
+//| SOFTWARE: object
+//| """The microntroller was reset from software."""
+//|
+//| DEEP_SLEEP_ALARM: object
+//| """The microntroller was reset for deep sleep and restarted by an alarm."""
+//|
+//| RESET_PIN: object
+//| """The microntroller was reset by a signal on its reset pin. The pin might be connected to a reset button."""
+//|
+//| WATCHDOG: object
+//| """The microcontroller was reset by its watchdog timer."""
+//|
+//| UNKNOWN: object
+//| """The microntroller restarted for an unknown reason."""
+//|
+MAKE_ENUM_MAP(mcu_reset_reason) {
+ MAKE_ENUM_MAP_ENTRY(reset_reason, POWER_ON),
+ MAKE_ENUM_MAP_ENTRY(reset_reason, BROWNOUT),
+ MAKE_ENUM_MAP_ENTRY(reset_reason, SOFTWARE),
+ MAKE_ENUM_MAP_ENTRY(reset_reason, DEEP_SLEEP_ALARM),
+ MAKE_ENUM_MAP_ENTRY(reset_reason, RESET_PIN),
+ MAKE_ENUM_MAP_ENTRY(reset_reason, WATCHDOG),
+ MAKE_ENUM_MAP_ENTRY(reset_reason, UNKNOWN),
+};
+STATIC MP_DEFINE_CONST_DICT(mcu_reset_reason_locals_dict, mcu_reset_reason_locals_table);
+
+MAKE_PRINTER(alarm, mcu_reset_reason);
+
+MAKE_ENUM_TYPE(alarm, ResetReason, mcu_reset_reason);
diff --git a/shared-bindings/microcontroller/ResetReason.h b/shared-bindings/microcontroller/ResetReason.h
new file mode 100644
index 000000000..8ed5e4831
--- /dev/null
+++ b/shared-bindings/microcontroller/ResetReason.h
@@ -0,0 +1,45 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
+ *
+ * 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_SHARED_BINDINGS_MCU_RESET_REASON__H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_MCU_RESET_REASON__H
+
+#include "py/obj.h"
+#include "py/enum.h"
+
+typedef enum {
+ RESET_REASON_POWER_ON,
+ RESET_REASON_BROWNOUT,
+ RESET_REASON_SOFTWARE,
+ RESET_REASON_DEEP_SLEEP_ALARM,
+ RESET_REASON_RESET_PIN,
+ RESET_REASON_WATCHDOG,
+ RESET_REASON_UNKNOWN,
+} mcu_reset_reason_t;
+
+extern const mp_obj_type_t mcu_reset_reason_type;
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MCU_RESET_REASON__H
diff --git a/shared-bindings/microcontroller/__init__.c b/shared-bindings/microcontroller/__init__.c
index 2e58bdcc2..8a77d1df5 100644
--- a/shared-bindings/microcontroller/__init__.c
+++ b/shared-bindings/microcontroller/__init__.c
@@ -39,7 +39,6 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Processor.h"
-#include "py/runtime.h"
#include "supervisor/shared/translate.h"
//| """Pin references and cpu functionality
@@ -148,16 +147,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_reset_obj, mcu_reset);
//| This object is the sole instance of `watchdog.WatchDogTimer` when available or ``None`` otherwise."""
//|
-
-//| """:mod:`microcontroller.pin` --- Microcontroller pin names
-//| --------------------------------------------------------
-//|
-//| .. module:: microcontroller.pin
-//| :synopsis: Microcontroller pin names
-//| :platform: SAMD21
-//|
-//| References to pins as named by the microcontroller"""
-//|
const mp_obj_module_t mcu_pin_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mcu_pin_globals,
diff --git a/shared-bindings/microcontroller/__init__.h b/shared-bindings/microcontroller/__init__.h
index 8abdff763..ac71de424 100644
--- a/shared-bindings/microcontroller/__init__.h
+++ b/shared-bindings/microcontroller/__init__.h
@@ -28,11 +28,11 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H
-#include "py/mpconfig.h"
#include "py/obj.h"
+#include "py/mpconfig.h"
#include "common-hal/microcontroller/Processor.h"
-
+#include "shared-bindings/microcontroller/ResetReason.h"
#include "shared-bindings/microcontroller/RunMode.h"
extern void common_hal_mcu_delay_us(uint32_t);