2016-11-29 09:10:31 -05:00
|
|
|
// Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp32/ulp.h"
|
|
|
|
|
|
|
|
#include "soc/soc.h"
|
|
|
|
#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
|
|
|
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
esp_err_t ulp_run(uint32_t entry_point)
|
|
|
|
{
|
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
|
|
|
|
ets_delay_us(10);
|
2016-12-16 07:25:38 -05:00
|
|
|
// set entry point
|
2016-12-07 01:18:10 -05:00
|
|
|
SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_PC_INIT_V, entry_point, SENS_PC_INIT_S);
|
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);
|
|
|
|
// 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);
|
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;
|
|
|
|
}
|
|
|
|
if (load_addr_bytes > CONFIG_ULP_COPROC_RESERVE_MEM) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (load_addr_bytes + program_size_bytes > CONFIG_ULP_COPROC_RESERVE_MEM) {
|
|
|
|
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;
|
|
|
|
}
|