aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/shared-bindings/microcontroller
diff options
context:
space:
mode:
Diffstat (limited to 'circuitpython/shared-bindings/microcontroller')
-rw-r--r--circuitpython/shared-bindings/microcontroller/Pin.c196
-rw-r--r--circuitpython/shared-bindings/microcontroller/Pin.h57
-rw-r--r--circuitpython/shared-bindings/microcontroller/Processor.c165
-rw-r--r--circuitpython/shared-bindings/microcontroller/Processor.h44
-rw-r--r--circuitpython/shared-bindings/microcontroller/ResetReason.c82
-rw-r--r--circuitpython/shared-bindings/microcontroller/ResetReason.h46
-rw-r--r--circuitpython/shared-bindings/microcontroller/RunMode.c101
-rw-r--r--circuitpython/shared-bindings/microcontroller/RunMode.h50
-rw-r--r--circuitpython/shared-bindings/microcontroller/__init__.c198
-rw-r--r--circuitpython/shared-bindings/microcontroller/__init__.h68
10 files changed, 1007 insertions, 0 deletions
diff --git a/circuitpython/shared-bindings/microcontroller/Pin.c b/circuitpython/shared-bindings/microcontroller/Pin.c
new file mode 100644
index 0000000..e6809d6
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/Pin.c
@@ -0,0 +1,196 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ *
+ * 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 "shared-bindings/board/__init__.h"
+#include "shared-bindings/microcontroller/__init__.h"
+#include "shared-bindings/microcontroller/Pin.h"
+
+#include "py/nlr.h"
+#include "py/obj.h"
+#include "py/runtime.h"
+#include "supervisor/shared/translate.h"
+
+//| class Pin:
+//| """Identifies an IO pin on the microcontroller."""
+//|
+//| 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
+//| :mod:`board` or :mod:`microcontroller.pin` to reference the desired pin."""
+//| ...
+//|
+
+//| def __hash__(self) -> int:
+//| """Returns a hash for the Pin."""
+//| ...
+//|
+// Provided by mp_generic_unary_op().
+
+static void get_pin_name(const mcu_pin_obj_t *self, qstr *package, qstr *module, qstr *name) {
+ const mp_map_t *board_map = &board_module_globals.map;
+ for (uint8_t i = 0; i < board_map->alloc; i++) {
+ if (board_map->table[i].value == MP_OBJ_FROM_PTR(self)) {
+ *package = 0;
+ *module = MP_QSTR_board;
+ *name = MP_OBJ_QSTR_VALUE(board_map->table[i].key);
+ return;
+ }
+ }
+ const mp_map_t *mcu_map = &mcu_pin_globals.map;
+ for (uint8_t i = 0; i < mcu_map->alloc; i++) {
+ if (mcu_map->table[i].value == MP_OBJ_FROM_PTR(self)) {
+ *package = MP_QSTR_microcontroller;
+ *module = MP_QSTR_pin;
+ *name = MP_OBJ_QSTR_VALUE(mcu_map->table[i].key);
+ return;
+ }
+ }
+}
+
+STATIC void mcu_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ mcu_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ qstr package = MP_QSTR_Pin;
+ qstr module;
+ qstr name = MP_QSTR_Pin;
+
+ get_pin_name(self, &package, &module, &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 = {
+ { &mp_type_type },
+ .flags = MP_TYPE_FLAG_EXTENDED,
+ .name = MP_QSTR_Pin,
+ .print = mcu_pin_print,
+ MP_TYPE_EXTENDED_FIELDS(
+ .unary_op = mp_generic_unary_op,
+ )
+};
+
+const mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj) {
+ if (!mp_obj_is_type(obj, &mcu_pin_type)) {
+ mp_raise_TypeError_varg(translate("Expected a %q"), mcu_pin_type.name);
+ }
+ return MP_OBJ_TO_PTR(obj);
+}
+
+// Validate that the obj is a pin or None. Return an mcu_pin_obj_t* or NULL, correspondingly.
+const mcu_pin_obj_t *validate_obj_is_pin_or_none(mp_obj_t obj) {
+ if (obj == mp_const_none) {
+ return NULL;
+ }
+ return validate_obj_is_pin(obj);
+}
+
+const mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj) {
+ const mcu_pin_obj_t *pin = validate_obj_is_pin(obj);
+ assert_pin_free(pin);
+ return pin;
+}
+
+// Validate every element in the list is a unique pin
+void validate_no_duplicate_pins(mp_obj_t seq, qstr arg_name) {
+ const size_t num_pins = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq));
+
+ for (size_t pin_cnt = 0; pin_cnt < num_pins; pin_cnt++) {
+ mp_obj_t pin1_obj = mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(pin_cnt), MP_OBJ_SENTINEL);
+ const mcu_pin_obj_t *pin1 = validate_obj_is_pin(pin1_obj);
+
+ for (size_t pin_cnt_2 = pin_cnt + 1; pin_cnt_2 < num_pins; pin_cnt_2++) {
+ mp_obj_t pin2_obj = mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(pin_cnt_2), MP_OBJ_SENTINEL);
+ const mcu_pin_obj_t *pin2 = validate_obj_is_pin(pin2_obj);
+ if (pin1 == pin2) {
+ mp_raise_TypeError_varg(translate("%q contains duplicate pins"), arg_name);
+ }
+ }
+ }
+}
+
+void validate_no_duplicate_pins_2(mp_obj_t seq1, mp_obj_t seq2, qstr arg_name1, qstr arg_name2) {
+ const size_t num_pins_1 = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq1));
+ const size_t num_pins_2 = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq2));
+
+ validate_no_duplicate_pins(seq1, arg_name1);
+ validate_no_duplicate_pins(seq2, arg_name2);
+
+ for (size_t pin_cnt_1 = 0; pin_cnt_1 < num_pins_1; pin_cnt_1++) {
+ mp_obj_t pin1_obj = mp_obj_subscr(seq1, MP_OBJ_NEW_SMALL_INT(pin_cnt_1), MP_OBJ_SENTINEL);
+ const mcu_pin_obj_t *pin1 = validate_obj_is_pin(pin1_obj);
+
+ for (size_t pin_cnt_2 = 0; pin_cnt_2 < num_pins_2; pin_cnt_2++) {
+ mp_obj_t pin2_obj = mp_obj_subscr(seq2, MP_OBJ_NEW_SMALL_INT(pin_cnt_2), MP_OBJ_SENTINEL);
+ const mcu_pin_obj_t *pin2 = validate_obj_is_pin(pin2_obj);
+ if (pin1 == pin2) {
+ mp_raise_TypeError_varg(translate("%q and %q contain duplicate pins"), arg_name1, arg_name2);
+ }
+ }
+ }
+}
+
+// Validate every element in the list to be a free pin.
+void validate_list_is_free_pins(qstr what, const 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.
+const mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj) {
+ if (obj == mp_const_none) {
+ return NULL;
+ }
+ const mcu_pin_obj_t *pin = validate_obj_is_pin(obj);
+ assert_pin_free(pin);
+ return pin;
+}
+
+void assert_pin_free(const mcu_pin_obj_t *pin) {
+ if (pin != NULL && pin != MP_OBJ_TO_PTR(mp_const_none) && !common_hal_mcu_pin_is_free(pin)) {
+ qstr package;
+ qstr module;
+ qstr name = MP_QSTR_Pin;
+
+ get_pin_name(pin, &package, &module, &name);
+ mp_raise_ValueError_varg(translate("%q in use"), name);
+ }
+}
+
+void validate_pins(qstr what, uint8_t *pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) {
+ const mcu_pin_obj_t *pins[max_pins];
+ validate_list_is_free_pins(what, pins, max_pins, seq, count_out);
+ for (mp_int_t i = 0; i < *count_out; i++) {
+ pin_nos[i] = common_hal_mcu_pin_number(pins[i]);
+ }
+}
diff --git a/circuitpython/shared-bindings/microcontroller/Pin.h b/circuitpython/shared-bindings/microcontroller/Pin.h
new file mode 100644
index 0000000..a7378ea
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/Pin.h
@@ -0,0 +1,57 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ *
+ * 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_MICROCONTROLLER_PIN_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PIN_H
+
+#include "common-hal/microcontroller/Pin.h"
+#include "py/obj.h"
+
+// Type object used in Python. Should be shared between ports.
+extern const mp_obj_type_t mcu_pin_type;
+
+const mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj);
+const mcu_pin_obj_t *validate_obj_is_pin_or_none(mp_obj_t obj);
+const mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj);
+const mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj);
+void validate_no_duplicate_pins(mp_obj_t seq, qstr arg_name);
+void validate_no_duplicate_pins_2(mp_obj_t seq1, mp_obj_t seq2, qstr arg_name1, qstr arg_name2);
+void validate_list_is_free_pins(qstr what, const mcu_pin_obj_t **pins_out, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out);
+void validate_pins(qstr what, uint8_t *pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out);
+
+void assert_pin_free(const mcu_pin_obj_t *pin);
+
+bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin);
+void common_hal_never_reset_pin(const mcu_pin_obj_t *pin);
+void common_hal_reset_pin(const mcu_pin_obj_t *pin);
+uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t *pin);
+void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin);
+void common_hal_mcu_pin_claim_number(uint8_t pin_no);
+void common_hal_mcu_pin_reset_number(uint8_t pin_no);
+
+#define COMMON_HAL_MCU_NO_PIN ((uint8_t)0xff)
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PIN_H
diff --git a/circuitpython/shared-bindings/microcontroller/Processor.c b/circuitpython/shared-bindings/microcontroller/Processor.c
new file mode 100644
index 0000000..ff1b52e
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/Processor.c
@@ -0,0 +1,165 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Dan Halbert 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 "shared-bindings/microcontroller/__init__.h"
+#include "shared-bindings/microcontroller/Processor.h"
+
+#include <math.h>
+#include <stdint.h>
+
+#include "shared-bindings/util.h"
+
+#include "shared/runtime/buffer_helper.h"
+#include "shared/runtime/context_manager_helpers.h"
+#include "py/mperrno.h"
+#include "py/objtype.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+#include "supervisor/shared/translate.h"
+
+
+//| class Processor:
+//| """Microcontroller CPU information and control
+//|
+//| Usage::
+//|
+//| import microcontroller
+//| print(microcontroller.cpu.frequency)
+//| print(microcontroller.cpu.temperature)
+//|
+//| Note that on chips with more than one cpu (such as the RP2040)
+//| microcontroller.cpu will return the value for CPU 0.
+//| To get values from other CPUs use microcontroller.cpus indexed by
+//| the number of the desired cpu. i.e.
+//|
+//| print(microcontroller.cpus[0].temperature)
+//| print(microcontroller.cpus[1].frequency)"""
+//|
+
+//| def __init__(self) -> None:
+//| """You cannot create an instance of `microcontroller.Processor`.
+//| Use `microcontroller.cpu` to access the sole instance available."""
+//| ...
+//|
+
+//| frequency: int
+//| """The CPU operating frequency in Hertz. (read-only)"""
+//|
+
+STATIC mp_obj_t mcu_processor_set_frequency(mp_obj_t self, mp_obj_t freq) {
+ #if CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY
+ uint32_t value_of_freq = (uint32_t)mp_arg_validate_int_min(mp_obj_get_int(freq), 0, MP_QSTR_frequency);
+ common_hal_mcu_processor_set_frequency(self, value_of_freq);
+ #else
+ mp_raise_msg(&mp_type_NotImplementedError,translate("frequency is read-only for this board"));
+ #endif
+ return mp_const_none;
+}
+
+MP_DEFINE_CONST_FUN_OBJ_2(mcu_processor_set_frequency_obj, mcu_processor_set_frequency);
+
+
+STATIC mp_obj_t mcu_processor_get_frequency(mp_obj_t self) {
+ return mp_obj_new_int_from_uint(common_hal_mcu_processor_get_frequency());
+}
+
+MP_DEFINE_CONST_FUN_OBJ_1(mcu_processor_get_frequency_obj, mcu_processor_get_frequency);
+
+MP_PROPERTY_GETSET(mcu_processor_frequency_obj,
+ (mp_obj_t)&mcu_processor_get_frequency_obj,
+ (mp_obj_t)&mcu_processor_set_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);
+
+MP_PROPERTY_GETTER(mcu_processor_reset_reason_obj,
+ (mp_obj_t)&mcu_processor_get_reset_reason_obj);
+
+//| temperature: Optional[float]
+//| """The on-chip temperature, in Celsius, as a float. (read-only)
+//|
+//| Is `None` if the temperature is not available."""
+//|
+STATIC mp_obj_t mcu_processor_get_temperature(mp_obj_t self) {
+ float temperature = common_hal_mcu_processor_get_temperature();
+ return isnan(temperature) ? mp_const_none : mp_obj_new_float(temperature);
+}
+
+MP_DEFINE_CONST_FUN_OBJ_1(mcu_processor_get_temperature_obj, mcu_processor_get_temperature);
+
+MP_PROPERTY_GETTER(mcu_processor_temperature_obj,
+ (mp_obj_t)&mcu_processor_get_temperature_obj);
+
+//| 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) {
+ uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH];
+ common_hal_mcu_processor_get_uid(raw_id);
+ return mp_obj_new_bytearray(sizeof(raw_id), raw_id);
+}
+
+MP_DEFINE_CONST_FUN_OBJ_1(mcu_processor_get_uid_obj, mcu_processor_get_uid);
+
+MP_PROPERTY_GETTER(mcu_processor_uid_obj,
+ (mp_obj_t)&mcu_processor_get_uid_obj);
+
+//| voltage: Optional[float]
+//| """The input voltage to the microcontroller, as a float. (read-only)
+//|
+//| Is `None` if the voltage is not available."""
+//|
+STATIC mp_obj_t mcu_processor_get_voltage(mp_obj_t self) {
+ float voltage = common_hal_mcu_processor_get_voltage();
+ return isnan(voltage) ? mp_const_none : mp_obj_new_float(voltage);
+}
+
+MP_DEFINE_CONST_FUN_OBJ_1(mcu_processor_get_voltage_obj, mcu_processor_get_voltage);
+
+MP_PROPERTY_GETTER(mcu_processor_voltage_obj,
+ (mp_obj_t)&mcu_processor_get_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) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(mcu_processor_locals_dict, mcu_processor_locals_dict_table);
+
+const mp_obj_type_t mcu_processor_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Processor,
+ .locals_dict = (mp_obj_dict_t *)&mcu_processor_locals_dict,
+};
diff --git a/circuitpython/shared-bindings/microcontroller/Processor.h b/circuitpython/shared-bindings/microcontroller/Processor.h
new file mode 100644
index 0000000..9a2f22b
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/Processor.h
@@ -0,0 +1,44 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 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_MICROCONTROLLER_PROCESSOR_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PROCESSOR_H
+
+#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);
+uint32_t common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PROCESSOR_H
diff --git a/circuitpython/shared-bindings/microcontroller/ResetReason.c b/circuitpython/shared-bindings/microcontroller/ResetReason.c
new file mode 100644
index 0000000..905c19f
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/ResetReason.c
@@ -0,0 +1,82 @@
+/*
+ * 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);
+MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, RESCUE_DEBUG, RESET_REASON_RESCUE_DEBUG);
+
+//| 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."""
+//|
+//| RESCUE_DEBUG: object
+//| """The microntroller was reset by the rescue debug port."""
+//|
+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),
+ MAKE_ENUM_MAP_ENTRY(reset_reason, RESCUE_DEBUG),
+};
+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/circuitpython/shared-bindings/microcontroller/ResetReason.h b/circuitpython/shared-bindings/microcontroller/ResetReason.h
new file mode 100644
index 0000000..7abc54c
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/ResetReason.h
@@ -0,0 +1,46 @@
+/*
+ * 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,
+ RESET_REASON_RESCUE_DEBUG,
+} 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/circuitpython/shared-bindings/microcontroller/RunMode.c b/circuitpython/shared-bindings/microcontroller/RunMode.c
new file mode 100644
index 0000000..477168d
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/RunMode.c
@@ -0,0 +1,101 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 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 "shared-bindings/microcontroller/RunMode.h"
+
+//| class RunMode:
+//| """run state of the microcontroller"""
+//|
+//| def __init__(self) -> None:
+//| """Enum-like class to define the run mode of the microcontroller and
+//| CircuitPython."""
+//|
+//| NORMAL: RunMode
+//| """Run CircuitPython as normal.
+//|
+//| :type microcontroller.RunMode:"""
+//|
+//| SAFE_MODE: RunMode
+//| """Run CircuitPython in safe mode. User code will not run and the
+//| file system will be writeable over USB.
+//|
+//| :type microcontroller.RunMode:"""
+//|
+//| UF2: RunMode
+//| """Run the uf2 bootloader.
+//|
+//| :type microcontroller.RunMode:"""
+//|
+//| BOOTLOADER: RunMode
+//| """Run the default bootloader.
+//|
+//| :type microcontroller.RunMode:"""
+//|
+const mp_obj_type_t mcu_runmode_type;
+
+const mcu_runmode_obj_t mcu_runmode_uf2_obj = {
+ { &mcu_runmode_type },
+};
+
+const mcu_runmode_obj_t mcu_runmode_normal_obj = {
+ { &mcu_runmode_type },
+};
+
+const mcu_runmode_obj_t mcu_runmode_safe_mode_obj = {
+ { &mcu_runmode_type },
+};
+
+const mcu_runmode_obj_t mcu_runmode_bootloader_obj = {
+ { &mcu_runmode_type },
+};
+
+STATIC const mp_rom_map_elem_t mcu_runmode_locals_dict_table[] = {
+ {MP_ROM_QSTR(MP_QSTR_UF2), MP_ROM_PTR(&mcu_runmode_uf2_obj)},
+ {MP_ROM_QSTR(MP_QSTR_NORMAL), MP_ROM_PTR(&mcu_runmode_normal_obj)},
+ {MP_ROM_QSTR(MP_QSTR_SAFE_MODE), MP_ROM_PTR(&mcu_runmode_safe_mode_obj)},
+ {MP_ROM_QSTR(MP_QSTR_BOOTLOADER), MP_ROM_PTR(&mcu_runmode_bootloader_obj)},
+};
+STATIC MP_DEFINE_CONST_DICT(mcu_runmode_locals_dict, mcu_runmode_locals_dict_table);
+
+STATIC void mcu_runmode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ qstr runmode = MP_QSTR_NORMAL;
+ if (self_in == MP_ROM_PTR(&mcu_runmode_uf2_obj)) {
+ runmode = MP_QSTR_UF2;
+ } else if (self_in == MP_ROM_PTR(&mcu_runmode_safe_mode_obj)) {
+ runmode = MP_QSTR_SAFE_MODE;
+ } else if (self_in == MP_ROM_PTR(&mcu_runmode_bootloader_obj)) {
+ runmode = MP_QSTR_BOOTLOADER;
+ }
+ mp_printf(print, "%q.%q.%q", MP_QSTR_microcontroller, MP_QSTR_RunMode,
+ runmode);
+}
+
+const mp_obj_type_t mcu_runmode_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_RunMode,
+ .print = mcu_runmode_print,
+ .locals_dict = (mp_obj_dict_t *)&mcu_runmode_locals_dict,
+};
diff --git a/circuitpython/shared-bindings/microcontroller/RunMode.h b/circuitpython/shared-bindings/microcontroller/RunMode.h
new file mode 100644
index 0000000..172256d
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/RunMode.h
@@ -0,0 +1,50 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 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_MICROCONTROLLER_RUNMODE_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_RUNMODE_H
+
+#include "py/obj.h"
+
+typedef enum {
+ RUNMODE_UF2,
+ RUNMODE_NORMAL,
+ RUNMODE_SAFE_MODE,
+ RUNMODE_BOOTLOADER
+} mcu_runmode_t;
+
+extern const mp_obj_type_t mcu_runmode_type;
+
+typedef struct {
+ mp_obj_base_t base;
+} mcu_runmode_obj_t;
+
+extern const mcu_runmode_obj_t mcu_runmode_uf2_obj;
+extern const mcu_runmode_obj_t mcu_runmode_normal_obj;
+extern const mcu_runmode_obj_t mcu_runmode_safe_mode_obj;
+extern const mcu_runmode_obj_t mcu_runmode_bootloader_obj;
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_RUNMODE_H
diff --git a/circuitpython/shared-bindings/microcontroller/__init__.c b/circuitpython/shared-bindings/microcontroller/__init__.c
new file mode 100644
index 0000000..707080c
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/__init__.c
@@ -0,0 +1,198 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ *
+ * 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.
+ */
+
+// Microcontroller contains pin references and microcontroller specific control
+// functions.
+
+#include <stdint.h>
+
+#include "py/obj.h"
+#include "py/runtime.h"
+
+#include "common-hal/microcontroller/Pin.h"
+#include "common-hal/microcontroller/Processor.h"
+
+#include "shared-bindings/microcontroller/__init__.h"
+#include "shared-bindings/microcontroller/Pin.h"
+#include "shared-bindings/microcontroller/Processor.h"
+
+#include "supervisor/shared/translate.h"
+
+//| """Pin references and cpu functionality
+//|
+//| The `microcontroller` module defines the pins and other bare-metal hardware
+//| from the perspective of the microcontroller. See :py:mod:`board` for
+//| board-specific pin mappings."""
+//|
+//| from nvm import ByteArray
+//| from watchdog import WatchDogTimer
+//|
+
+//| cpu: Processor
+//| """CPU information and control, such as ``cpu.temperature`` and ``cpu.frequency``
+//| (clock frequency).
+//| This object is an instance of `microcontroller.Processor`."""
+//|
+
+//| cpus: Processor
+//| """CPU information and control, such as ``cpus[0].temperature`` and ``cpus[1].frequency``
+//| (clock frequency) on chips with more than 1 cpu. The index selects which cpu.
+//| This object is an instance of `microcontroller.Processor`."""
+//|
+
+//| 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
+//| code or peripherals (e.g audio recording) that require specific timing or
+//| processing while you are waiting, explore a different avenue such as using
+//| `time.sleep()`."""
+//| ...
+//|
+STATIC mp_obj_t mcu_delay_us(mp_obj_t delay_obj) {
+ uint32_t delay = mp_obj_get_int(delay_obj);
+
+ common_hal_mcu_delay_us(delay);
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mcu_delay_us_obj, mcu_delay_us);
+
+//| def disable_interrupts() -> None:
+//| """Disable all interrupts. Be very careful, this can stall everything."""
+//| ...
+//|
+STATIC mp_obj_t mcu_disable_interrupts(void) {
+ common_hal_mcu_disable_interrupts();
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_disable_interrupts_obj, mcu_disable_interrupts);
+
+//| def enable_interrupts() -> None:
+//| """Enable the interrupts that were enabled at the last disable."""
+//| ...
+//|
+STATIC mp_obj_t mcu_enable_interrupts(void) {
+ common_hal_mcu_enable_interrupts();
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_enable_interrupts_obj, mcu_enable_interrupts);
+
+//| 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.
+//|
+//| :param ~microcontroller.RunMode run_mode: The next run mode"""
+//| ...
+//|
+STATIC mp_obj_t mcu_on_next_reset(mp_obj_t run_mode_obj) {
+ mcu_runmode_t run_mode;
+ if (run_mode_obj == MP_OBJ_FROM_PTR(&mcu_runmode_uf2_obj)) {
+ run_mode = RUNMODE_UF2;
+ } else if (run_mode_obj == MP_OBJ_FROM_PTR(&mcu_runmode_normal_obj)) {
+ run_mode = RUNMODE_NORMAL;
+ } else if (run_mode_obj == MP_OBJ_FROM_PTR(&mcu_runmode_safe_mode_obj)) {
+ run_mode = RUNMODE_SAFE_MODE;
+ } else if (run_mode_obj == MP_OBJ_FROM_PTR(&mcu_runmode_bootloader_obj)) {
+ run_mode = RUNMODE_BOOTLOADER;
+ } else {
+ mp_raise_ValueError(translate("Invalid run mode."));
+ }
+ common_hal_mcu_on_next_reset(run_mode);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mcu_on_next_reset_obj, mcu_on_next_reset);
+
+//| def reset() -> None:
+//| """Reset the microcontroller. After reset, the microcontroller will enter the
+//| run mode last set by `on_next_reset`.
+//|
+//| .. warning:: This may result in file system corruption when connected to a
+//| host computer. Be very careful when calling this! Make sure the device
+//| "Safely removed" on Windows or "ejected" on Mac OSX and Linux."""
+//| ...
+//|
+STATIC mp_obj_t mcu_reset(void) {
+ common_hal_mcu_reset();
+ // We won't actually get here because we're resetting.
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_reset_obj, mcu_reset);
+
+//| 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"""
+//|
+
+//| 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,
+};
+
+STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_microcontroller) },
+ { MP_ROM_QSTR(MP_QSTR_cpu), MP_ROM_PTR(&common_hal_mcu_processor_obj) },
+ #if CIRCUITPY_PROCESSOR_COUNT > 1
+ { MP_ROM_QSTR(MP_QSTR_cpus), MP_ROM_PTR(&common_hal_multi_processor_obj) },
+ #endif
+ { MP_ROM_QSTR(MP_QSTR_delay_us), MP_ROM_PTR(&mcu_delay_us_obj) },
+ { MP_ROM_QSTR(MP_QSTR_disable_interrupts), MP_ROM_PTR(&mcu_disable_interrupts_obj) },
+ { MP_ROM_QSTR(MP_QSTR_enable_interrupts), MP_ROM_PTR(&mcu_enable_interrupts_obj) },
+ { MP_ROM_QSTR(MP_QSTR_on_next_reset), MP_ROM_PTR(&mcu_on_next_reset_obj) },
+ { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&mcu_reset_obj) },
+ #if CIRCUITPY_NVM && CIRCUITPY_INTERNAL_NVM_SIZE > 0
+ { MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&common_hal_mcu_nvm_obj) },
+ #else
+ { MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_NONE },
+ #endif
+ #if CIRCUITPY_WATCHDOG
+ { MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_PTR(&common_hal_mcu_watchdogtimer_obj) },
+ #else
+ { MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_NONE },
+ #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) },
+ { MP_ROM_QSTR(MP_QSTR_Processor), MP_ROM_PTR(&mcu_processor_type) },
+
+};
+
+STATIC MP_DEFINE_CONST_DICT(mcu_module_globals, mcu_module_globals_table);
+
+const mp_obj_module_t microcontroller_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t *)&mcu_module_globals,
+};
+
+MP_REGISTER_MODULE(MP_QSTR_microcontroller, microcontroller_module, CIRCUITPY_MICROCONTROLLER);
diff --git a/circuitpython/shared-bindings/microcontroller/__init__.h b/circuitpython/shared-bindings/microcontroller/__init__.h
new file mode 100644
index 0000000..e41cce8
--- /dev/null
+++ b/circuitpython/shared-bindings/microcontroller/__init__.h
@@ -0,0 +1,68 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ *
+ * 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_MICROCONTROLLER___INIT___H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H
+
+#include "py/obj.h"
+#include "py/mpconfig.h"
+#include "py/objtuple.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);
+
+extern void common_hal_mcu_disable_interrupts(void);
+extern void common_hal_mcu_enable_interrupts(void);
+
+extern void common_hal_mcu_on_next_reset(mcu_runmode_t runmode);
+extern void common_hal_mcu_reset(void);
+
+extern const mp_obj_dict_t mcu_pin_globals;
+
+#if CIRCUITPY_PROCESSOR_COUNT == 1
+extern const mcu_processor_obj_t common_hal_mcu_processor_obj;
+#elif CIRCUITPY_PROCESSOR_COUNT > 1
+extern const mcu_processor_obj_t common_hal_mcu_processor_obj;
+extern const mp_rom_obj_tuple_t common_hal_multi_processor_obj;
+#else
+#error "Invalid processor count"
+#endif
+
+
+#if CIRCUITPY_NVM && CIRCUITPY_INTERNAL_NVM_SIZE > 0
+#include "common-hal/nvm/ByteArray.h"
+extern const nvm_bytearray_obj_t common_hal_mcu_nvm_obj;
+#endif
+
+#if CIRCUITPY_WATCHDOG
+#include "common-hal/watchdog/WatchDogTimer.h"
+extern watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj;
+#endif
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H