Andreas Valder | 298c072 | 2017-11-20 17:03:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2018 "Andreas Valder" <andreas.valder@serioese.gmbh> |
| 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 "py/runtime.h" |
| 28 | |
Damien George | 66a86a0 | 2021-02-18 21:24:34 +1100 | [diff] [blame] | 29 | #if CONFIG_IDF_TARGET_ESP32 |
| 30 | |
Andreas Valder | 298c072 | 2017-11-20 17:03:38 +0100 | [diff] [blame] | 31 | #include "esp32/ulp.h" |
| 32 | #include "esp_err.h" |
| 33 | |
| 34 | typedef struct _esp32_ulp_obj_t { |
| 35 | mp_obj_base_t base; |
| 36 | } esp32_ulp_obj_t; |
| 37 | |
| 38 | const mp_obj_type_t esp32_ulp_type; |
| 39 | |
| 40 | // singleton ULP object |
| 41 | STATIC const esp32_ulp_obj_t esp32_ulp_obj = {{&esp32_ulp_type}}; |
| 42 | |
| 43 | STATIC mp_obj_t esp32_ulp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 44 | // check arguments |
| 45 | mp_arg_check_num(n_args, n_kw, 0, 0, false); |
| 46 | |
| 47 | // return constant object |
| 48 | return (mp_obj_t)&esp32_ulp_obj; |
| 49 | } |
| 50 | |
| 51 | STATIC mp_obj_t esp32_ulp_set_wakeup_period(mp_obj_t self_in, mp_obj_t period_index_in, mp_obj_t period_us_in) { |
| 52 | mp_uint_t period_index = mp_obj_get_int(period_index_in); |
| 53 | mp_uint_t period_us = mp_obj_get_int(period_us_in); |
| 54 | int _errno = ulp_set_wakeup_period(period_index, period_us); |
| 55 | if (_errno != ESP_OK) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 56 | mp_raise_OSError(_errno); |
Andreas Valder | 298c072 | 2017-11-20 17:03:38 +0100 | [diff] [blame] | 57 | } |
| 58 | return mp_const_none; |
| 59 | } |
| 60 | STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32_ulp_set_wakeup_period_obj, esp32_ulp_set_wakeup_period); |
| 61 | |
| 62 | STATIC mp_obj_t esp32_ulp_load_binary(mp_obj_t self_in, mp_obj_t load_addr_in, mp_obj_t program_binary_in) { |
| 63 | mp_uint_t load_addr = mp_obj_get_int(load_addr_in); |
| 64 | |
| 65 | mp_buffer_info_t bufinfo; |
| 66 | mp_get_buffer_raise(program_binary_in, &bufinfo, MP_BUFFER_READ); |
| 67 | |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 68 | int _errno = ulp_load_binary(load_addr, bufinfo.buf, bufinfo.len / sizeof(uint32_t)); |
Andreas Valder | 298c072 | 2017-11-20 17:03:38 +0100 | [diff] [blame] | 69 | if (_errno != ESP_OK) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 70 | mp_raise_OSError(_errno); |
Andreas Valder | 298c072 | 2017-11-20 17:03:38 +0100 | [diff] [blame] | 71 | } |
| 72 | return mp_const_none; |
| 73 | } |
| 74 | STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32_ulp_load_binary_obj, esp32_ulp_load_binary); |
| 75 | |
| 76 | STATIC mp_obj_t esp32_ulp_run(mp_obj_t self_in, mp_obj_t entry_point_in) { |
| 77 | mp_uint_t entry_point = mp_obj_get_int(entry_point_in); |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 78 | int _errno = ulp_run(entry_point / sizeof(uint32_t)); |
Andreas Valder | 298c072 | 2017-11-20 17:03:38 +0100 | [diff] [blame] | 79 | if (_errno != ESP_OK) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 80 | mp_raise_OSError(_errno); |
Andreas Valder | 298c072 | 2017-11-20 17:03:38 +0100 | [diff] [blame] | 81 | } |
| 82 | return mp_const_none; |
| 83 | } |
| 84 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32_ulp_run_obj, esp32_ulp_run); |
| 85 | |
| 86 | STATIC const mp_rom_map_elem_t esp32_ulp_locals_dict_table[] = { |
| 87 | { MP_ROM_QSTR(MP_QSTR_set_wakeup_period), MP_ROM_PTR(&esp32_ulp_set_wakeup_period_obj) }, |
| 88 | { MP_ROM_QSTR(MP_QSTR_load_binary), MP_ROM_PTR(&esp32_ulp_load_binary_obj) }, |
| 89 | { MP_ROM_QSTR(MP_QSTR_run), MP_ROM_PTR(&esp32_ulp_run_obj) }, |
Jim Mussared | 96008ff | 2019-09-13 23:04:13 +1000 | [diff] [blame] | 90 | { MP_ROM_QSTR(MP_QSTR_RESERVE_MEM), MP_ROM_INT(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM) }, |
Andreas Valder | 298c072 | 2017-11-20 17:03:38 +0100 | [diff] [blame] | 91 | }; |
| 92 | STATIC MP_DEFINE_CONST_DICT(esp32_ulp_locals_dict, esp32_ulp_locals_dict_table); |
| 93 | |
| 94 | const mp_obj_type_t esp32_ulp_type = { |
| 95 | { &mp_type_type }, |
| 96 | .name = MP_QSTR_ULP, |
| 97 | .make_new = esp32_ulp_make_new, |
| 98 | .locals_dict = (mp_obj_t)&esp32_ulp_locals_dict, |
| 99 | }; |
Damien George | 66a86a0 | 2021-02-18 21:24:34 +1100 | [diff] [blame] | 100 | |
| 101 | #endif // CONFIG_IDF_TARGET_ESP32 |