ulp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "sdkconfig.h"
  18. #include "esp_attr.h"
  19. #include "esp_err.h"
  20. #include "esp_log.h"
  21. #if CONFIG_IDF_TARGET_ESP32
  22. #include "esp32/clk.h"
  23. #include "esp32/ulp.h"
  24. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  25. #include "esp32s2beta/clk.h"
  26. #include "esp32s2beta/ulp.h"
  27. #endif
  28. #include "soc/soc.h"
  29. #include "soc/rtc.h"
  30. #include "soc/rtc_cntl_reg.h"
  31. #include "soc/sens_reg.h"
  32. typedef struct {
  33. uint32_t magic;
  34. uint16_t text_offset;
  35. uint16_t text_size;
  36. uint16_t data_size;
  37. uint16_t bss_size;
  38. } ulp_binary_header_t;
  39. #define ULP_BINARY_MAGIC_ESP32 (0x00706c75)
  40. static const char* TAG = "ulp";
  41. esp_err_t ulp_run(uint32_t entry_point)
  42. {
  43. // disable ULP timer
  44. CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  45. // wait for at least 1 RTC_SLOW_CLK cycle
  46. ets_delay_us(10);
  47. // set entry point
  48. REG_SET_FIELD(SENS_SAR_START_FORCE_REG, SENS_PC_INIT, entry_point);
  49. // disable force start
  50. CLEAR_PERI_REG_MASK(SENS_SAR_START_FORCE_REG, SENS_ULP_CP_FORCE_START_TOP_M);
  51. // set time until wakeup is allowed to the smallest possible
  52. REG_SET_FIELD(RTC_CNTL_TIMER5_REG, RTC_CNTL_MIN_SLP_VAL, RTC_CNTL_MIN_SLP_VAL_MIN);
  53. // make sure voltage is raised when RTC 8MCLK is enabled
  54. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_I2C_FOLW_8M);
  55. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_CORE_FOLW_8M);
  56. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_SLEEP_FOLW_8M);
  57. // enable ULP timer
  58. SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  59. return ESP_OK;
  60. }
  61. esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size)
  62. {
  63. size_t program_size_bytes = program_size * sizeof(uint32_t);
  64. size_t load_addr_bytes = load_addr * sizeof(uint32_t);
  65. if (program_size_bytes < sizeof(ulp_binary_header_t)) {
  66. return ESP_ERR_INVALID_SIZE;
  67. }
  68. if (load_addr_bytes > CONFIG_ESP32_ULP_COPROC_RESERVE_MEM) {
  69. return ESP_ERR_INVALID_ARG;
  70. }
  71. if (load_addr_bytes + program_size_bytes > CONFIG_ESP32_ULP_COPROC_RESERVE_MEM) {
  72. return ESP_ERR_INVALID_SIZE;
  73. }
  74. // Make a copy of a header in case program_binary isn't aligned
  75. ulp_binary_header_t header;
  76. memcpy(&header, program_binary, sizeof(header));
  77. if (header.magic != ULP_BINARY_MAGIC_ESP32) {
  78. return ESP_ERR_NOT_SUPPORTED;
  79. }
  80. size_t total_size = (size_t) header.text_offset + (size_t) header.text_size +
  81. (size_t) header.data_size;
  82. ESP_LOGD(TAG, "program_size_bytes: %d total_size: %d offset: %d .text: %d, .data: %d, .bss: %d",
  83. program_size_bytes, total_size, header.text_offset,
  84. header.text_size, header.data_size, header.bss_size);
  85. if (total_size != program_size_bytes) {
  86. return ESP_ERR_INVALID_SIZE;
  87. }
  88. size_t text_data_size = header.text_size + header.data_size;
  89. uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
  90. memcpy(base + load_addr_bytes, program_binary + header.text_offset, text_data_size);
  91. memset(base + load_addr_bytes + text_data_size, 0, header.bss_size);
  92. return ESP_OK;
  93. }
  94. esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us)
  95. {
  96. if (period_index > 4) {
  97. return ESP_ERR_INVALID_ARG;
  98. }
  99. uint64_t period_us_64 = period_us;
  100. uint64_t period_cycles = (period_us_64 << RTC_CLK_CAL_FRACT) / esp_clk_slowclk_cal_get();
  101. uint64_t min_sleep_period_cycles = ULP_FSM_PREPARE_SLEEP_CYCLES
  102. + ULP_FSM_WAKEUP_SLEEP_CYCLES
  103. + REG_GET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT);
  104. if (period_cycles < min_sleep_period_cycles) {
  105. period_cycles = 0;
  106. ESP_LOGW(TAG, "Sleep period clipped to minimum of %d cycles", (uint32_t) min_sleep_period_cycles);
  107. } else {
  108. period_cycles -= min_sleep_period_cycles;
  109. }
  110. REG_SET_FIELD(SENS_ULP_CP_SLEEP_CYC0_REG + period_index * sizeof(uint32_t),
  111. SENS_SLEEP_CYCLES_S0, (uint32_t) period_cycles);
  112. return ESP_OK;
  113. }