|
| 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/coproc/__init__.h" |
| 28 | + |
| 29 | +#include "py/runtime.h" |
| 30 | + |
| 31 | +#if defined(CONFIG_IDF_TARGET_ESP32S2) |
| 32 | +#include "esp32s2/ulp.h" |
| 33 | +#include "esp32s2/ulp_riscv.h" |
| 34 | +#elif defined(CONFIG_IDF_TARGET_ESP32S3) |
| 35 | +#include "esp32s3/ulp.h" |
| 36 | +#include "esp32s3/ulp_riscv.h" |
| 37 | +#endif |
| 38 | + |
| 39 | +// To-do idf v5.0: remove following include |
| 40 | +#include "soc/rtc_cntl_reg.h" |
| 41 | + |
| 42 | +void common_hal_coproc_run(coproc_coproc_obj_t *self) { |
| 43 | + if (ulp_riscv_load_binary(self->buf, self->buf_len) != ESP_OK) { |
| 44 | + mp_raise_RuntimeError(translate("Firmware is too big")); |
| 45 | + } |
| 46 | + m_free(self->buf); |
| 47 | + self->buf = (uint8_t *)RTC_SLOW_MEM; |
| 48 | + ulp_riscv_run(); |
| 49 | +} |
| 50 | + |
| 51 | +void common_hal_coproc_halt(coproc_coproc_obj_t *self) { |
| 52 | + self->buf = (uint8_t *)m_malloc(self->buf_len, false); |
| 53 | + memcpy(self->buf, (uint8_t *)RTC_SLOW_MEM, self->buf_len); |
| 54 | + |
| 55 | + // To-do idf v5.0: use following functions |
| 56 | + // ulp_riscv_timer_stop(); |
| 57 | + // ulp_riscv_halt(); |
| 58 | + |
| 59 | + // stop the ulp timer |
| 60 | + CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN); |
| 61 | + // suspends the ulp operation |
| 62 | + SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE); |
| 63 | + // Resets the processor |
| 64 | + SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN); |
| 65 | +} |
| 66 | + |
| 67 | +mp_obj_t common_hal_coproc_memory(coproc_coproc_obj_t *self) { |
| 68 | + return (self->coproc_memory) ? MP_OBJ_FROM_PTR(self->coproc_memory) : mp_const_none; |
| 69 | +} |
0 commit comments