2021-11-18 22:42:01 -05:00
|
|
|
/*
|
2021-12-23 01:12:47 -05:00
|
|
|
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
|
2021-11-18 22:42:01 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-11-29 09:10:31 -05:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2019-06-05 22:57:29 -04:00
|
|
|
#include "sdkconfig.h"
|
2016-11-29 09:10:31 -05:00
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
2021-11-18 22:42:01 -05:00
|
|
|
#include "esp_private/esp_clk.h"
|
2019-06-05 22:57:29 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-29 09:10:31 -05:00
|
|
|
#include "esp32/ulp.h"
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/ulp.h"
|
2020-07-29 01:13:51 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/ulp.h"
|
2019-06-05 22:57:29 -04:00
|
|
|
#endif
|
2016-11-29 09:10:31 -05:00
|
|
|
|
|
|
|
#include "soc/soc.h"
|
2017-04-20 04:13:09 -04:00
|
|
|
#include "soc/rtc.h"
|
2016-11-29 09:10:31 -05:00
|
|
|
#include "soc/rtc_cntl_reg.h"
|
2016-12-07 01:18:10 -05:00
|
|
|
#include "soc/sens_reg.h"
|
2016-11-29 09:10:31 -05:00
|
|
|
|
2022-01-21 04:13:48 -05:00
|
|
|
#include "ulp_common.h"
|
2020-07-21 01:07:34 -04:00
|
|
|
#include "esp_rom_sys.h"
|
2016-11-29 09:10:31 -05:00
|
|
|
|
2023-02-20 08:52:55 -05:00
|
|
|
#include "esp_check.h"
|
|
|
|
#include "esp_private/rtc_ctrl.h"
|
|
|
|
|
2016-11-29 09:10:31 -05:00
|
|
|
typedef struct {
|
2017-01-08 23:38:20 -05:00
|
|
|
uint32_t magic;
|
|
|
|
uint16_t text_offset;
|
|
|
|
uint16_t text_size;
|
|
|
|
uint16_t data_size;
|
|
|
|
uint16_t bss_size;
|
|
|
|
} ulp_binary_header_t;
|
2016-11-29 09:10:31 -05:00
|
|
|
|
2017-01-08 23:38:20 -05:00
|
|
|
#define ULP_BINARY_MAGIC_ESP32 (0x00706c75)
|
2016-11-29 09:10:31 -05:00
|
|
|
|
2017-01-08 23:38:20 -05:00
|
|
|
static const char* TAG = "ulp";
|
2016-11-29 09:10:31 -05:00
|
|
|
|
2023-02-20 08:52:55 -05:00
|
|
|
esp_err_t ulp_isr_register(intr_handler_t fn, void *arg)
|
|
|
|
{
|
|
|
|
ESP_RETURN_ON_FALSE(fn, ESP_ERR_INVALID_ARG, TAG, "ULP ISR is NULL");
|
|
|
|
REG_SET_BIT(RTC_CNTL_INT_ENA_REG, RTC_CNTL_ULP_CP_INT_ENA_M);
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
return rtc_isr_register(fn, arg, RTC_CNTL_SAR_INT_ST_M, 0);
|
|
|
|
#else
|
|
|
|
return rtc_isr_register(fn, arg, RTC_CNTL_ULP_CP_INT_ST_M, 0);
|
|
|
|
#endif /* CONFIG_IDF_TARGET_ESP32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t ulp_isr_deregister(intr_handler_t fn, void *arg)
|
|
|
|
{
|
|
|
|
ESP_RETURN_ON_FALSE(fn, ESP_ERR_INVALID_ARG, TAG, "ULP ISR is NULL");
|
|
|
|
REG_CLR_BIT(RTC_CNTL_INT_ENA_REG, RTC_CNTL_ULP_CP_INT_ENA_M);
|
|
|
|
return rtc_isr_deregister(fn, arg);
|
|
|
|
}
|
|
|
|
|
2016-11-29 09:10:31 -05:00
|
|
|
esp_err_t ulp_run(uint32_t entry_point)
|
|
|
|
{
|
2019-06-13 07:34:01 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-12-16 07:25:38 -05:00
|
|
|
// disable ULP timer
|
|
|
|
CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
|
2017-02-20 02:25:16 -05:00
|
|
|
// wait for at least 1 RTC_SLOW_CLK cycle
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(10);
|
2016-12-16 07:25:38 -05:00
|
|
|
// set entry point
|
2017-04-20 04:13:09 -04:00
|
|
|
REG_SET_FIELD(SENS_SAR_START_FORCE_REG, SENS_PC_INIT, entry_point);
|
2016-12-16 07:25:38 -05:00
|
|
|
// disable force start
|
|
|
|
CLEAR_PERI_REG_MASK(SENS_SAR_START_FORCE_REG, SENS_ULP_CP_FORCE_START_TOP_M);
|
2017-04-20 04:13:09 -04:00
|
|
|
// set time until wakeup is allowed to the smallest possible
|
|
|
|
REG_SET_FIELD(RTC_CNTL_TIMER5_REG, RTC_CNTL_MIN_SLP_VAL, RTC_CNTL_MIN_SLP_VAL_MIN);
|
2016-12-16 07:25:38 -05:00
|
|
|
// make sure voltage is raised when RTC 8MCLK is enabled
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_I2C_FOLW_8M);
|
2017-02-20 02:44:49 -05:00
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_CORE_FOLW_8M);
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_SLEEP_FOLW_8M);
|
2016-12-16 07:25:38 -05:00
|
|
|
// enable ULP timer
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
|
2022-02-17 01:14:34 -05:00
|
|
|
#else
|
|
|
|
/* Reset COCPU when power on. */
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_CTRL_REG, RTC_CNTL_ULP_CP_RESET);
|
|
|
|
esp_rom_delay_us(20);
|
|
|
|
CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_CTRL_REG, RTC_CNTL_ULP_CP_RESET);
|
2019-06-28 08:36:32 -04:00
|
|
|
// disable ULP timer
|
|
|
|
CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
|
|
|
|
// wait for at least 1 RTC_SLOW_CLK cycle
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(10);
|
2020-11-10 02:40:01 -05:00
|
|
|
// set entry point
|
2019-06-28 08:36:32 -04:00
|
|
|
REG_SET_FIELD(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_PC_INIT, entry_point);
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL); // Select ULP_TIMER trigger target for ULP.
|
2020-04-17 15:34:56 -04:00
|
|
|
// start ULP clock gate.
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_CTRL_REG ,RTC_CNTL_ULP_CP_CLK_FO);
|
|
|
|
// ULP FSM sends the DONE signal.
|
2020-11-10 02:40:01 -05:00
|
|
|
CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE_FORCE);
|
2022-02-17 01:14:34 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
/* Set the CLKGATE_EN signal on esp32s3 */
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLKGATE_EN);
|
|
|
|
#endif
|
2019-06-28 08:36:32 -04:00
|
|
|
/* Clear interrupt COCPU status */
|
|
|
|
REG_WRITE(RTC_CNTL_INT_CLR_REG, RTC_CNTL_COCPU_INT_CLR | RTC_CNTL_COCPU_TRAP_INT_CLR | RTC_CNTL_ULP_CP_INT_CLR);
|
|
|
|
// 1: start with timer. wait ULP_TIMER cnt timer.
|
|
|
|
CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_CTRL_REG, RTC_CNTL_ULP_CP_FORCE_START_TOP); // Select ULP_TIMER timer as COCPU trigger source
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN); // Software to turn on the ULP_TIMER timer
|
2019-06-13 07:34:01 -04:00
|
|
|
#endif
|
2016-11-29 09:10:31 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
2017-01-08 23:38:20 -05:00
|
|
|
|
|
|
|
esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size)
|
|
|
|
{
|
|
|
|
size_t program_size_bytes = program_size * sizeof(uint32_t);
|
|
|
|
size_t load_addr_bytes = load_addr * sizeof(uint32_t);
|
|
|
|
|
|
|
|
if (program_size_bytes < sizeof(ulp_binary_header_t)) {
|
|
|
|
return ESP_ERR_INVALID_SIZE;
|
|
|
|
}
|
2022-01-21 04:13:48 -05:00
|
|
|
if (load_addr_bytes > CONFIG_ULP_COPROC_RESERVE_MEM) {
|
2017-01-08 23:38:20 -05:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2022-01-21 04:13:48 -05:00
|
|
|
if (load_addr_bytes + program_size_bytes > CONFIG_ULP_COPROC_RESERVE_MEM) {
|
2017-01-08 23:38:20 -05:00
|
|
|
return ESP_ERR_INVALID_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a copy of a header in case program_binary isn't aligned
|
|
|
|
ulp_binary_header_t header;
|
|
|
|
memcpy(&header, program_binary, sizeof(header));
|
|
|
|
|
|
|
|
if (header.magic != ULP_BINARY_MAGIC_ESP32) {
|
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t total_size = (size_t) header.text_offset + (size_t) header.text_size +
|
|
|
|
(size_t) header.data_size;
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "program_size_bytes: %d total_size: %d offset: %d .text: %d, .data: %d, .bss: %d",
|
|
|
|
program_size_bytes, total_size, header.text_offset,
|
|
|
|
header.text_size, header.data_size, header.bss_size);
|
|
|
|
|
|
|
|
if (total_size != program_size_bytes) {
|
|
|
|
return ESP_ERR_INVALID_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t text_data_size = header.text_size + header.data_size;
|
|
|
|
uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
|
|
|
|
|
|
|
|
memcpy(base + load_addr_bytes, program_binary + header.text_offset, text_data_size);
|
|
|
|
memset(base + load_addr_bytes + text_data_size, 0, header.bss_size);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|