|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2022 microDev |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-bindings/microcontroller/Pin.h" |
| 28 | +#include "shared-bindings/util.h" |
| 29 | +#include "bindings/espulp/ULP.h" |
| 30 | + |
| 31 | +#include "py/runtime.h" |
| 32 | + |
| 33 | +//| class ULP: |
| 34 | +//| def __init__(self): |
| 35 | +//| """The ultra-low-power processor. |
| 36 | +//| |
| 37 | +//| Raises an exception if another ULP has been instantiated. This |
| 38 | +//| ensures that is is only used by one piece of code at a time.""" |
| 39 | +//| ... |
| 40 | +STATIC mp_obj_t espulp_ulp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 41 | + espulp_ulp_obj_t *self = m_new_obj(espulp_ulp_obj_t); |
| 42 | + self->base.type = &espulp_ulp_type; |
| 43 | + common_hal_espulp_ulp_construct(self); |
| 44 | + return MP_OBJ_FROM_PTR(self); |
| 45 | +} |
| 46 | + |
| 47 | +STATIC espulp_ulp_obj_t *get_ulp_obj(mp_obj_t self_in) { |
| 48 | + if (!mp_obj_is_type(self_in, &espulp_ulp_type)) { |
| 49 | + mp_raise_TypeError_varg(translate("Expected a %q"), MP_QSTR_ULP); |
| 50 | + } |
| 51 | + espulp_ulp_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 52 | + if (common_hal_espulp_ulp_deinited(self)) { |
| 53 | + raise_deinited_error(); |
| 54 | + } |
| 55 | + return self; |
| 56 | +} |
| 57 | + |
| 58 | +//| def deinit(self) -> None: |
| 59 | +//| """Deinitialises the ULP and releases it for another program.""" |
| 60 | +//| ... |
| 61 | +STATIC mp_obj_t espulp_ulp_deinit(mp_obj_t self_in) { |
| 62 | + espulp_ulp_obj_t *self = get_ulp_obj(self_in); |
| 63 | + common_hal_espulp_ulp_deinit(self); |
| 64 | + return mp_const_none; |
| 65 | +} |
| 66 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(espulp_ulp_deinit_obj, espulp_ulp_deinit); |
| 67 | + |
| 68 | +//| def __enter__(self) -> ULP: |
| 69 | +//| """No-op used by Context Managers.""" |
| 70 | +//| ... |
| 71 | +// Provided by context manager helper. |
| 72 | + |
| 73 | +//| def __exit__(self) -> None: |
| 74 | +//| """Automatically deinitializes the hardware when exiting a context. See |
| 75 | +//| :ref:`lifetime-and-contextmanagers` for more info.""" |
| 76 | +//| ... |
| 77 | +STATIC mp_obj_t espulp_ulp_obj___exit__(size_t n_args, const mp_obj_t *args) { |
| 78 | + (void)n_args; |
| 79 | + return espulp_ulp_deinit(args[0]); |
| 80 | +} |
| 81 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espulp_ulp___exit___obj, 4, 4, espulp_ulp_obj___exit__); |
| 82 | + |
| 83 | +//| def run( |
| 84 | +//| self, program: ReadableBuffer, *, pins: Sequence[microcontroller.Pin] = () |
| 85 | +//| ) -> None: |
| 86 | +//| """Loads the program into ULP memory and then runs the program. The given pins are |
| 87 | +//| claimed and not reset until `halt()` is called. |
| 88 | +//| |
| 89 | +//| The program will continue to run even when the running Python is halted.""" |
| 90 | +//| ... |
| 91 | +STATIC mp_obj_t espulp_ulp_run(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 92 | + enum { ARG_program, ARG_pins }; |
| 93 | + static const mp_arg_t allowed_args[] = { |
| 94 | + { MP_QSTR_program, MP_ARG_REQUIRED | MP_ARG_OBJ}, |
| 95 | + { MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_tuple} }, |
| 96 | + }; |
| 97 | + |
| 98 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 99 | + espulp_ulp_obj_t *self = get_ulp_obj(pos_args[0]); |
| 100 | + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 101 | + |
| 102 | + mp_buffer_info_t bufinfo; |
| 103 | + mp_get_buffer_raise(args[ARG_program].u_obj, &bufinfo, MP_BUFFER_READ); |
| 104 | + |
| 105 | + mp_obj_t pins_in = args[ARG_pins].u_obj; |
| 106 | + const size_t num_pins = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(pins_in)); |
| 107 | + |
| 108 | + // The ULP only supports 21 pins on the ESP32-S2 and S3. So we can store it |
| 109 | + // as a bitmask in a 32 bit number. The common-hal code does further checks. |
| 110 | + uint32_t pin_mask = 0; |
| 111 | + |
| 112 | + for (mp_uint_t i = 0; i < num_pins; i++) { |
| 113 | + mp_obj_t pin_obj = mp_obj_subscr(pins_in, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL); |
| 114 | + validate_obj_is_free_pin(pin_obj); |
| 115 | + const mcu_pin_obj_t *pin = ((const mcu_pin_obj_t *)pin_obj); |
| 116 | + if (pin->number >= 32) { |
| 117 | + raise_ValueError_invalid_pin(); |
| 118 | + } |
| 119 | + pin_mask |= 1 << pin->number; |
| 120 | + } |
| 121 | + |
| 122 | + common_hal_espulp_ulp_run(self, bufinfo.buf, bufinfo.len, pin_mask); |
| 123 | + return mp_const_none; |
| 124 | +} |
| 125 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(espulp_ulp_run_obj, 2, espulp_ulp_run); |
| 126 | + |
| 127 | +//| def halt(self) -> None: |
| 128 | +//| """Halts the running program and releases the pins given in `run()`.""" |
| 129 | +//| ... |
| 130 | +//| |
| 131 | +STATIC mp_obj_t espulp_ulp_halt(mp_obj_t self_in) { |
| 132 | + common_hal_espulp_ulp_halt(get_ulp_obj(self_in)); |
| 133 | + return mp_const_none; |
| 134 | +} |
| 135 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(espulp_ulp_halt_obj, espulp_ulp_halt); |
| 136 | + |
| 137 | +STATIC const mp_rom_map_elem_t espulp_ulp_locals_table[] = { |
| 138 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&espulp_ulp_deinit_obj) }, |
| 139 | + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) }, |
| 140 | + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&espulp_ulp___exit___obj) }, |
| 141 | + { MP_ROM_QSTR(MP_QSTR_run), MP_ROM_PTR(&espulp_ulp_run_obj) }, |
| 142 | + { MP_ROM_QSTR(MP_QSTR_halt), MP_ROM_PTR(&espulp_ulp_halt_obj) }, |
| 143 | +}; |
| 144 | +STATIC MP_DEFINE_CONST_DICT(espulp_ulp_locals_dict, espulp_ulp_locals_table); |
| 145 | + |
| 146 | +const mp_obj_type_t espulp_ulp_type = { |
| 147 | + { &mp_type_type }, |
| 148 | + .name = MP_QSTR_ULP, |
| 149 | + .make_new = espulp_ulp_make_new, |
| 150 | + .locals_dict = (mp_obj_t)&espulp_ulp_locals_dict, |
| 151 | +}; |
0 commit comments