summaryrefslogtreecommitdiff
path: root/shared-bindings/microcontroller
diff options
context:
space:
mode:
Diffstat (limited to 'shared-bindings/microcontroller')
-rw-r--r--shared-bindings/microcontroller/Pin.c22
-rw-r--r--shared-bindings/microcontroller/Pin.h1
-rw-r--r--shared-bindings/microcontroller/Processor.c28
-rw-r--r--shared-bindings/microcontroller/Processor.h4
-rw-r--r--shared-bindings/microcontroller/ResetReason.c77
-rw-r--r--shared-bindings/microcontroller/ResetReason.h45
-rw-r--r--shared-bindings/microcontroller/RunMode.c8
-rw-r--r--shared-bindings/microcontroller/RunMode.h2
-rw-r--r--shared-bindings/microcontroller/__init__.c31
-rw-r--r--shared-bindings/microcontroller/__init__.h5
10 files changed, 190 insertions, 33 deletions
diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c
index 765e602e5..6ba02a0e7 100644
--- a/shared-bindings/microcontroller/Pin.c
+++ b/shared-bindings/microcontroller/Pin.c
@@ -36,10 +36,10 @@
//| class Pin:
//| """Identifies an IO pin on the microcontroller."""
//|
-//| def __init__(self, ):
+//| def __init__(self) -> None:
//| """Identifies an IO pin on the microcontroller. They are fixed by the
//| hardware so they cannot be constructed on demand. Instead, use
-//| `board` or `microcontroller.pin` to reference the desired pin."""
+//| :mod:`board` or :mod:`microcontroller.pin` to reference the desired pin."""
//| ...
//|
@@ -71,7 +71,11 @@ STATIC void mcu_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
qstr name;
get_pin_name(self, &package, &module, &name);
- mp_printf(print, "%q.%q.%q", MP_QSTR_microcontroller, MP_QSTR_pin, name);
+ if (package){
+ mp_printf(print, "%q.%q.%q", package, module, name);
+ } else {
+ mp_printf(print, "%q.%q", module , name);
+ }
}
const mp_obj_type_t mcu_pin_type = {
@@ -101,6 +105,18 @@ mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj) {
return pin;
}
+// Validate every element in the list to be a free pin.
+void validate_list_is_free_pins(qstr what, mcu_pin_obj_t **pins_out, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) {
+ mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq));
+ if (len > max_pins) {
+ mp_raise_ValueError_varg(translate("At most %d %q may be specified (not %d)"), max_pins, what, len);
+ }
+ *count_out = len;
+ for (mp_int_t i=0; i<len; i++) {
+ pins_out[i] = validate_obj_is_free_pin(mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
+ }
+}
+
// Validate that the obj is a free pin or None. Return an mcu_pin_obj_t* or NULL, correspondingly.
mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj) {
if (obj == mp_const_none) {
diff --git a/shared-bindings/microcontroller/Pin.h b/shared-bindings/microcontroller/Pin.h
index bb5256b55..f6659db7d 100644
--- a/shared-bindings/microcontroller/Pin.h
+++ b/shared-bindings/microcontroller/Pin.h
@@ -37,6 +37,7 @@ mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj);
mcu_pin_obj_t *validate_obj_is_pin_or_none(mp_obj_t obj);
mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj);
mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj);
+void validate_list_is_free_pins(qstr what, mcu_pin_obj_t **pins_out, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out);
void assert_pin_free(const mcu_pin_obj_t* pin);
diff --git a/shared-bindings/microcontroller/Processor.c b/shared-bindings/microcontroller/Processor.c
index c4b249124..90cc02fe3 100644
--- a/shared-bindings/microcontroller/Processor.c
+++ b/shared-bindings/microcontroller/Processor.c
@@ -44,13 +44,13 @@
//| print(microcontroller.cpu.temperature)"""
//|
-//| def __init__(self, ):
+//| def __init__(self) -> None:
//| """You cannot create an instance of `microcontroller.Processor`.
//| Use `microcontroller.cpu` to access the sole instance available."""
//| ...
//|
-//| frequency: int = ...
+//| frequency: int
//| """The CPU operating frequency in Hertz. (read-only)"""
//|
STATIC mp_obj_t mcu_processor_get_frequency(mp_obj_t self) {
@@ -67,7 +67,24 @@ const mp_obj_property_t mcu_processor_frequency_obj = {
},
};
-//| temperature: Any = ...
+//| 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)
//|
//| Is `None` if the temperature is not available."""
@@ -87,7 +104,7 @@ const mp_obj_property_t mcu_processor_temperature_obj = {
},
};
-//| uid: Any = ...
+//| uid: bytearray
//| """The unique id (aka serial number) of the chip as a `bytearray`. (read-only)"""
//|
STATIC mp_obj_t mcu_processor_get_uid(mp_obj_t self) {
@@ -106,7 +123,7 @@ const mp_obj_property_t mcu_processor_uid_obj = {
},
};
-//| voltage: Any = ...
+//| voltage: Optional[float]
//| """The input voltage to the microcontroller, as a float. (read-only)
//|
//| Is `None` if the voltage is not available."""
@@ -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 1088112f4..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"
-const mp_obj_type_t mcu_processor_type;
+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..1750e5520
--- /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(microcontroller, mcu_reset_reason);
+
+MAKE_ENUM_TYPE(microcontroller, 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/RunMode.c b/shared-bindings/microcontroller/RunMode.c
index 6db315d8d..a54f40cac 100644
--- a/shared-bindings/microcontroller/RunMode.c
+++ b/shared-bindings/microcontroller/RunMode.c
@@ -29,22 +29,22 @@
//| class RunMode:
//| """run state of the microcontroller"""
//|
-//| def __init__(self, ):
+//| def __init__(self) -> None:
//| """Enum-like class to define the run mode of the microcontroller and
//| CircuitPython."""
//|
-//| NORMAL: Any = ...
+//| NORMAL: RunMode
//| """Run CircuitPython as normal.
//|
//| :type microcontroller.RunMode:"""
//|
-//| SAFE_MODE: Any = ...
+//| SAFE_MODE: RunMode
//| """Run CircuitPython in safe mode. User code will not be run and the
//| file system will be writeable over USB.
//|
//| :type microcontroller.RunMode:"""
//|
-//| BOOTLOADER: Any = ...
+//| BOOTLOADER: RunMode
//| """Run the bootloader.
//|
//| :type microcontroller.RunMode:"""
diff --git a/shared-bindings/microcontroller/RunMode.h b/shared-bindings/microcontroller/RunMode.h
index 5e8b6e646..ce90ab93a 100644
--- a/shared-bindings/microcontroller/RunMode.h
+++ b/shared-bindings/microcontroller/RunMode.h
@@ -35,7 +35,7 @@ typedef enum {
RUNMODE_BOOTLOADER
} mcu_runmode_t;
-const mp_obj_type_t mcu_runmode_type;
+extern const mp_obj_type_t mcu_runmode_type;
typedef struct {
mp_obj_base_t base;
diff --git a/shared-bindings/microcontroller/__init__.c b/shared-bindings/microcontroller/__init__.c
index 88fe9c224..035587c30 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
@@ -47,14 +46,17 @@
//| The `microcontroller` module defines the pins from the perspective of the
//| microcontroller. See `board` for board-specific pin mappings."""
//|
+//| from nvm import ByteArray
+//| from watchdog import WatchDogTimer
+//|
-//| cpu: Processor = ...
+//| cpu: Processor
//| """CPU information and control, such as ``cpu.temperature`` and ``cpu.frequency``
//| (clock frequency).
//| This object is the sole instance of `microcontroller.Processor`."""
//|
-//| def delay_us(delay: Any) -> Any:
+//| def delay_us(delay: int) -> None:
//| """Dedicated delay method used for very short delays. **Do not** do long delays
//| because this stops all other functions from completing. Think of this as an empty
//| ``while`` loop that runs for the specified ``(delay)`` time. If you have other
@@ -72,7 +74,7 @@ STATIC mp_obj_t mcu_delay_us(mp_obj_t delay_obj) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mcu_delay_us_obj, mcu_delay_us);
-//| def disable_interrupts() -> Any:
+//| def disable_interrupts() -> None:
//| """Disable all interrupts. Be very careful, this can stall everything."""
//| ...
//|
@@ -82,7 +84,7 @@ STATIC mp_obj_t mcu_disable_interrupts(void) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_disable_interrupts_obj, mcu_disable_interrupts);
-//| def enable_interrupts() -> Any:
+//| def enable_interrupts() -> None:
//| """Enable the interrupts that were enabled at the last disable."""
//| ...
//|
@@ -92,7 +94,7 @@ STATIC mp_obj_t mcu_enable_interrupts(void) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_enable_interrupts_obj, mcu_enable_interrupts);
-//| def on_next_reset(run_mode: microcontroller.RunMode) -> Any:
+//| def on_next_reset(run_mode: microcontroller.RunMode) -> None:
//| """Configure the run mode used the next time the microcontroller is reset but
//| not powered down.
//|
@@ -117,7 +119,7 @@ STATIC mp_obj_t mcu_on_next_reset(mp_obj_t run_mode_obj) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mcu_on_next_reset_obj, mcu_on_next_reset);
-//| def reset() -> Any:
+//| def reset() -> None:
//| """Reset the microcontroller. After reset, the microcontroller will enter the
//| run mode last set by `on_next_reset`.
//|
@@ -133,22 +135,18 @@ STATIC mp_obj_t mcu_reset(void) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_reset_obj, mcu_reset);
-//| nvm: Any = ...
+//| nvm: Optional[ByteArray]
//| """Available non-volatile memory.
//| This object is the sole instance of `nvm.ByteArray` when available or ``None`` otherwise.
//|
//| :type: nvm.ByteArray or None"""
//|
-//| """:mod:`microcontroller.pin` --- Microcontroller pin names
-//| --------------------------------------------------------
-//|
-//| .. module:: microcontroller.pin
-//| :synopsis: Microcontroller pin names
-//| :platform: SAMD21
-//|
-//| References to pins as named by the microcontroller"""
+//| watchdog: Optional[WatchDogTimer]
+//| """Available watchdog timer.
+//| This object is the sole instance of `watchdog.WatchDogTimer` when available or ``None`` otherwise."""
//|
+
const mp_obj_module_t mcu_pin_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mcu_pin_globals,
@@ -172,6 +170,7 @@ STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = {
#else
{ MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_PTR(&mp_const_none_obj) },
#endif
+ { MP_ROM_QSTR(MP_QSTR_ResetReason), MP_ROM_PTR(&mcu_reset_reason_type) },
{ MP_ROM_QSTR(MP_QSTR_RunMode), MP_ROM_PTR(&mcu_runmode_type) },
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&mcu_pin_type) },
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&mcu_pin_module) },
diff --git a/shared-bindings/microcontroller/__init__.h b/shared-bindings/microcontroller/__init__.h
index 8abdff763..0dafc74c7 100644
--- a/shared-bindings/microcontroller/__init__.h
+++ b/shared-bindings/microcontroller/__init__.h
@@ -1,4 +1,3 @@
-
/*
* This file is part of the MicroPython project, http://micropython.org/
*
@@ -28,11 +27,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);