summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-12-05 14:50:58 -0800
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-12-05 14:50:58 -0800
commitb4d36990479888a0be503383367fbd1bd9b6976b (patch)
tree4ad05722e5c6bbde2725b647bbae2fa88f0c0be0 /shared-bindings
parent78db6c32cddabe7ff363a3f934504805a027b217 (diff)
Introduce reset mechanics to microcontroller.
This allows one to configure how a subsequent reset will behave and also trigger a reset. Fixes #350 and fixes #173
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/digitalio/DigitalInOut.c14
-rw-r--r--shared-bindings/digitalio/DigitalInOut.h14
-rw-r--r--shared-bindings/digitalio/Direction.h4
-rw-r--r--shared-bindings/digitalio/DriveMode.h4
-rw-r--r--shared-bindings/digitalio/Pull.h4
-rw-r--r--shared-bindings/microcontroller/RunMode.c90
-rw-r--r--shared-bindings/microcontroller/RunMode.h47
-rw-r--r--shared-bindings/microcontroller/__init__.c44
-rw-r--r--shared-bindings/microcontroller/__init__.h5
9 files changed, 206 insertions, 20 deletions
diff --git a/shared-bindings/digitalio/DigitalInOut.c b/shared-bindings/digitalio/DigitalInOut.c
index ea2ba0b89..008883b8b 100644
--- a/shared-bindings/digitalio/DigitalInOut.c
+++ b/shared-bindings/digitalio/DigitalInOut.c
@@ -124,7 +124,7 @@ STATIC mp_obj_t digitalio_digitalinout_switch_to_output(size_t n_args, const mp_
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- enum digitalio_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL;
+ digitalio_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL;
if (args[ARG_drive_mode].u_rom_obj == &digitalio_drive_mode_open_drain_obj) {
drive_mode = DRIVE_MODE_OPEN_DRAIN;
}
@@ -161,7 +161,7 @@ STATIC mp_obj_t digitalio_digitalinout_switch_to_input(size_t n_args, const mp_o
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- enum digitalio_pull_t pull = PULL_NONE;
+ digitalio_pull_t pull = PULL_NONE;
if (args[ARG_pull].u_rom_obj == &digitalio_pull_up_obj) {
pull = PULL_UP;
}else if (args[ARG_pull].u_rom_obj == &digitalio_pull_down_obj) {
@@ -191,7 +191,7 @@ extern const digitalio_digitalio_direction_obj_t digitalio_digitalio_direction_o
STATIC mp_obj_t digitalio_digitalinout_obj_get_direction(mp_obj_t self_in) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
- enum digitalio_direction_t direction = common_hal_digitalio_digitalinout_get_direction(self);
+ digitalio_direction_t direction = common_hal_digitalio_digitalinout_get_direction(self);
if (direction == DIRECTION_INPUT) {
return (mp_obj_t)&digitalio_direction_input_obj;
}
@@ -262,7 +262,7 @@ STATIC mp_obj_t digitalio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) {
mp_raise_AttributeError("Drive mode not used when direction is input.");
return mp_const_none;
}
- enum digitalio_drive_mode_t drive_mode = common_hal_digitalio_digitalinout_get_drive_mode(self);
+ digitalio_drive_mode_t drive_mode = common_hal_digitalio_digitalinout_get_drive_mode(self);
if (drive_mode == DRIVE_MODE_PUSH_PULL) {
return (mp_obj_t)&digitalio_drive_mode_push_pull_obj;
}
@@ -277,7 +277,7 @@ STATIC mp_obj_t digitalio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_o
mp_raise_AttributeError("Drive mode not used when direction is input.");
return mp_const_none;
}
- enum digitalio_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL;
+ digitalio_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL;
if (drive_mode == &digitalio_drive_mode_open_drain_obj) {
c_drive_mode = DRIVE_MODE_OPEN_DRAIN;
}
@@ -307,7 +307,7 @@ STATIC mp_obj_t digitalio_digitalinout_obj_get_pull(mp_obj_t self_in) {
mp_raise_AttributeError("Pull not used when direction is output.");
return mp_const_none;
}
- enum digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(self);
+ digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(self);
if (pull == PULL_UP) {
return (mp_obj_t)&digitalio_pull_up_obj;
} else if (pull == PULL_DOWN) {
@@ -324,7 +324,7 @@ STATIC mp_obj_t digitalio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t p
mp_raise_AttributeError("Pull not used when direction is output.");
return mp_const_none;
}
- enum digitalio_pull_t pull = PULL_NONE;
+ digitalio_pull_t pull = PULL_NONE;
if (pull_obj == &digitalio_pull_up_obj) {
pull = PULL_UP;
} else if (pull_obj == &digitalio_pull_down_obj) {
diff --git a/shared-bindings/digitalio/DigitalInOut.h b/shared-bindings/digitalio/DigitalInOut.h
index 74970881b..2aaa31b7f 100644
--- a/shared-bindings/digitalio/DigitalInOut.h
+++ b/shared-bindings/digitalio/DigitalInOut.h
@@ -43,14 +43,14 @@ typedef enum {
digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin);
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self);
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t* self);
-void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t* self, enum digitalio_pull_t pull);
-void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, enum digitalio_drive_mode_t drive_mode);
-enum digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t* self);
+void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t* self, digitalio_pull_t pull);
+void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, digitalio_drive_mode_t drive_mode);
+digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_set_value(digitalio_digitalinout_obj_t* self, bool value);
bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t* self);
-void common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t* self, enum digitalio_drive_mode_t drive_mode);
-enum digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t* self);
-void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t* self, enum digitalio_pull_t pull);
-enum digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t* self);
+void common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t* self, digitalio_drive_mode_t drive_mode);
+digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t* self);
+void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t* self, digitalio_pull_t pull);
+digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t* self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H
diff --git a/shared-bindings/digitalio/Direction.h b/shared-bindings/digitalio/Direction.h
index e8fbf181d..d71f48c2e 100644
--- a/shared-bindings/digitalio/Direction.h
+++ b/shared-bindings/digitalio/Direction.h
@@ -29,10 +29,10 @@
#include "py/obj.h"
-enum digitalio_direction_t {
+typedef enum {
DIRECTION_INPUT,
DIRECTION_OUTPUT
-};
+} digitalio_direction_t;
typedef struct {
mp_obj_base_t base;
} digitalio_direction_obj_t;
diff --git a/shared-bindings/digitalio/DriveMode.h b/shared-bindings/digitalio/DriveMode.h
index 959885b1b..47d036b3a 100644
--- a/shared-bindings/digitalio/DriveMode.h
+++ b/shared-bindings/digitalio/DriveMode.h
@@ -29,10 +29,10 @@
#include "py/obj.h"
-enum digitalio_drive_mode_t {
+typedef enum {
DRIVE_MODE_PUSH_PULL,
DRIVE_MODE_OPEN_DRAIN
-};
+} digitalio_drive_mode_t;
typedef struct {
mp_obj_base_t base;
diff --git a/shared-bindings/digitalio/Pull.h b/shared-bindings/digitalio/Pull.h
index 8c88d8673..22fb6cd0e 100644
--- a/shared-bindings/digitalio/Pull.h
+++ b/shared-bindings/digitalio/Pull.h
@@ -29,11 +29,11 @@
#include "py/obj.h"
-enum digitalio_pull_t {
+typedef enum _digitalio_pull_t {
PULL_NONE,
PULL_UP,
PULL_DOWN
-};
+} digitalio_pull_t;
const mp_obj_type_t digitalio_pull_type;
diff --git a/shared-bindings/microcontroller/RunMode.c b/shared-bindings/microcontroller/RunMode.c
new file mode 100644
index 000000000..b8b66a457
--- /dev/null
+++ b/shared-bindings/microcontroller/RunMode.c
@@ -0,0 +1,90 @@
+/*
+ * 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"
+
+//| .. currentmodule:: microcontroller
+//|
+//| :class:`RunMode` -- run state of the microcontroller
+//| =============================================================
+//|
+//| .. class:: mcirocontroller.RunMode
+//|
+//| Enum-like class to define the run mode of the microcontroller and
+//| CircuitPython.
+//|
+//| .. data:: NORMAL
+//|
+//| Run CircuitPython as normal.
+//|
+//| .. data:: SAFE_MODE
+//|
+//| Run CircuitPython in safe mode. User code will not be run and the
+//| file system will be writeable over USB.
+//|
+//| .. data:: BOOTLOADER
+//|
+//| Run the bootloader.
+//|
+const mp_obj_type_t 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_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 (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&mcu_runmode_safe_mode_obj)) {
+ runmode = MP_QSTR_SAFE_MODE;
+ } else if (MP_OBJ_TO_PTR(self_in) ==
+ MP_ROM_PTR(&mcu_runmode_bootloader_obj)) {
+ runmode = MP_QSTR_SAFE_MODE;
+ }
+ 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_t)&mcu_runmode_locals_dict,
+};
diff --git a/shared-bindings/microcontroller/RunMode.h b/shared-bindings/microcontroller/RunMode.h
new file mode 100644
index 000000000..5e8b6e646
--- /dev/null
+++ b/shared-bindings/microcontroller/RunMode.h
@@ -0,0 +1,47 @@
+/*
+ * 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_NORMAL,
+ RUNMODE_SAFE_MODE,
+ RUNMODE_BOOTLOADER
+} mcu_runmode_t;
+
+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_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/shared-bindings/microcontroller/__init__.c b/shared-bindings/microcontroller/__init__.c
index d2567bd65..cc091591f 100644
--- a/shared-bindings/microcontroller/__init__.c
+++ b/shared-bindings/microcontroller/__init__.c
@@ -102,6 +102,47 @@ STATIC mp_obj_t mcu_enable_interrupts(void) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_enable_interrupts_obj, mcu_enable_interrupts);
+//| .. method:: on_next_reset(run_mode)
+//|
+//| 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 == &mcu_runmode_normal_obj) {
+ run_mode = RUNMODE_NORMAL;
+ } else if (run_mode_obj == &mcu_runmode_safe_mode_obj) {
+ run_mode = RUNMODE_SAFE_MODE;
+ } else if (run_mode_obj == &mcu_runmode_bootloader_obj) {
+ run_mode = RUNMODE_BOOTLOADER;
+ } else {
+ mp_raise_ValueError("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);
+
+//| .. method:: reset()
+//|
+//| Reset the microcontroller. After reset, the microcontroller will enter the
+//| run mode last set by `one_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);
+
//| .. attribute:: nvm
//|
//| Available non-volatile memory.
@@ -128,11 +169,14 @@ STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = {
{ 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_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_PTR(&mp_const_none_obj) },
#endif
+ { 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) },
diff --git a/shared-bindings/microcontroller/__init__.h b/shared-bindings/microcontroller/__init__.h
index d43db4bf4..e1487c555 100644
--- a/shared-bindings/microcontroller/__init__.h
+++ b/shared-bindings/microcontroller/__init__.h
@@ -33,11 +33,16 @@
#include "common-hal/microcontroller/Processor.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;
extern const mcu_processor_obj_t common_hal_mcu_processor_obj;