2016-11-03 18:50:59 -04:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2017-08-31 13:48:30 -04:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
|
|
|
#include "common-hal/microcontroller/Processor.h"
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2017-08-31 13:48:30 -04:00
|
|
|
#include "shared-bindings/microcontroller/Processor.h"
|
|
|
|
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Pin references and cpu functionality
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2022-05-12 15:33:11 -04:00
|
|
|
//| 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."""
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| from nvm import ByteArray
|
|
|
|
//| from watchdog import WatchDogTimer
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| cpu: Processor
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """CPU information and control, such as ``cpu.temperature`` and ``cpu.frequency``
|
|
|
|
//| (clock frequency).
|
2021-01-28 17:06:30 -05:00
|
|
|
//| This object is an instance of `microcontroller.Processor`."""
|
|
|
|
|
|
|
|
//| cpus: Processor
|
|
|
|
//| """CPU information and control, such as ``cpus[0].temperature`` and ``cpus[1].frequency``
|
2021-01-29 12:57:36 -05:00
|
|
|
//| (clock frequency) on chips with more than 1 cpu. The index selects which cpu.
|
2021-01-28 17:06:30 -05:00
|
|
|
//| This object is an instance of `microcontroller.Processor`."""
|
2017-08-31 13:48:30 -04:00
|
|
|
|
2020-07-03 16:03:19 -04:00
|
|
|
//| def delay_us(delay: int) -> None:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """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()`."""
|
|
|
|
//| ...
|
2016-11-03 18:50:59 -04:00
|
|
|
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);
|
|
|
|
|
2020-07-03 16:03:19 -04:00
|
|
|
//| def disable_interrupts() -> None:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """Disable all interrupts. Be very careful, this can stall everything."""
|
|
|
|
//| ...
|
2016-11-30 18:08:34 -05:00
|
|
|
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);
|
|
|
|
|
2020-07-03 16:03:19 -04:00
|
|
|
//| def enable_interrupts() -> None:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """Enable the interrupts that were enabled at the last disable."""
|
|
|
|
//| ...
|
2016-11-30 18:08:34 -05:00
|
|
|
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);
|
|
|
|
|
2020-07-03 16:03:19 -04:00
|
|
|
//| def on_next_reset(run_mode: microcontroller.RunMode) -> None:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """Configure the run mode used the next time the microcontroller is reset but
|
|
|
|
//| not powered down.
|
2018-01-02 21:25:41 -05:00
|
|
|
//|
|
2020-05-07 15:59:52 -04:00
|
|
|
//| :param ~microcontroller.RunMode run_mode: The next run mode"""
|
|
|
|
//| ...
|
2018-01-02 21:25:41 -05:00
|
|
|
STATIC mp_obj_t mcu_on_next_reset(mp_obj_t run_mode_obj) {
|
|
|
|
mcu_runmode_t run_mode;
|
2021-08-04 19:27:54 -04:00
|
|
|
if (run_mode_obj == MP_OBJ_FROM_PTR(&mcu_runmode_uf2_obj)) {
|
2021-07-17 14:30:58 -04:00
|
|
|
run_mode = RUNMODE_UF2;
|
2021-08-04 19:27:54 -04:00
|
|
|
} else if (run_mode_obj == MP_OBJ_FROM_PTR(&mcu_runmode_normal_obj)) {
|
2018-01-02 21:25:41 -05:00
|
|
|
run_mode = RUNMODE_NORMAL;
|
2021-08-04 19:27:54 -04:00
|
|
|
} else if (run_mode_obj == MP_OBJ_FROM_PTR(&mcu_runmode_safe_mode_obj)) {
|
2018-01-02 21:25:41 -05:00
|
|
|
run_mode = RUNMODE_SAFE_MODE;
|
2021-08-04 19:27:54 -04:00
|
|
|
} else if (run_mode_obj == MP_OBJ_FROM_PTR(&mcu_runmode_bootloader_obj)) {
|
2018-01-02 21:25:41 -05:00
|
|
|
run_mode = RUNMODE_BOOTLOADER;
|
|
|
|
} else {
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_arg_error_invalid(MP_QSTR_run_mode);
|
2018-01-02 21:25:41 -05:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2020-07-03 16:03:19 -04:00
|
|
|
//| def reset() -> None:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """Reset the microcontroller. After reset, the microcontroller will enter the
|
|
|
|
//| run mode last set by `on_next_reset`.
|
2018-01-02 21:25:41 -05:00
|
|
|
//|
|
2020-05-07 15:59:52 -04:00
|
|
|
//| .. 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."""
|
|
|
|
//| ...
|
2018-01-02 21:25:41 -05:00
|
|
|
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);
|
|
|
|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| nvm: Optional[ByteArray]
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """Available non-volatile memory.
|
|
|
|
//| This object is the sole instance of `nvm.ByteArray` when available or ``None`` otherwise.
|
2017-08-25 16:00:27 -04:00
|
|
|
//|
|
2020-05-07 15:59:52 -04:00
|
|
|
//| :type: nvm.ByteArray or None"""
|
2017-08-25 16:00:27 -04:00
|
|
|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| watchdog: Optional[WatchDogTimer]
|
2020-07-25 04:58:37 -04:00
|
|
|
//| """Available watchdog timer.
|
|
|
|
//| This object is the sole instance of `watchdog.WatchDogTimer` when available or ``None`` otherwise."""
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
const mp_obj_module_t mcu_pin_module = {
|
|
|
|
.base = { &mp_type_module },
|
2021-03-15 09:57:36 -04:00
|
|
|
.globals = (mp_obj_dict_t *)&mcu_pin_globals,
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_microcontroller) },
|
2017-08-31 13:48:30 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_cpu), MP_ROM_PTR(&common_hal_mcu_processor_obj) },
|
2021-03-15 09:57:36 -04:00
|
|
|
#if CIRCUITPY_PROCESSOR_COUNT > 1
|
2021-01-28 17:06:30 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_cpus), MP_ROM_PTR(&common_hal_multi_processor_obj) },
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2016-11-03 18:50:59 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_delay_us), MP_ROM_PTR(&mcu_delay_us_obj) },
|
2016-11-30 18:08:34 -05:00
|
|
|
{ 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) },
|
2018-01-02 21:25:41 -05:00
|
|
|
{ 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) },
|
2021-08-04 19:27:54 -04:00
|
|
|
#if CIRCUITPY_NVM && CIRCUITPY_INTERNAL_NVM_SIZE > 0
|
2017-08-31 13:48:30 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&common_hal_mcu_nvm_obj) },
|
2017-08-25 16:00:27 -04:00
|
|
|
#else
|
2021-05-05 20:51:52 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_NONE },
|
2017-08-25 16:00:27 -04:00
|
|
|
#endif
|
2020-05-21 01:47:23 -04:00
|
|
|
#if CIRCUITPY_WATCHDOG
|
2020-05-22 00:35:29 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_PTR(&common_hal_mcu_watchdogtimer_obj) },
|
2020-05-21 01:47:23 -04:00
|
|
|
#else
|
2021-05-05 20:51:52 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_NONE },
|
2020-05-21 01:47:23 -04:00
|
|
|
#endif
|
2020-12-06 12:00:00 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ResetReason), MP_ROM_PTR(&mcu_reset_reason_type) },
|
2018-01-02 21:25:41 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_RunMode), MP_ROM_PTR(&mcu_runmode_type) },
|
2017-08-25 16:00:27 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&mcu_pin_type) },
|
2016-11-03 18:50:59 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&mcu_pin_module) },
|
2017-08-31 13:48:30 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_Processor), MP_ROM_PTR(&mcu_processor_type) },
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mcu_module_globals, mcu_module_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t microcontroller_module = {
|
|
|
|
.base = { &mp_type_module },
|
2021-03-15 09:57:36 -04:00
|
|
|
.globals = (mp_obj_dict_t *)&mcu_module_globals,
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|
Convert more modules to use MP_REGISTER_MODULE
Convert bitbangio, bitmaptools, _bleio, board, busio, countio, digitalio, framebufferio, frequencyio, gamepadshift, getpass, keypad, math, microcontroller, and msgpack modules to use MP_REGISTER_MODULE.
Related to #5183.
2021-08-26 21:32:44 -04:00
|
|
|
|
|
|
|
MP_REGISTER_MODULE(MP_QSTR_microcontroller, microcontroller_module, CIRCUITPY_MICROCONTROLLER);
|