ulp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "ulp_private.h"
  33. typedef struct {
  34. uint32_t magic;
  35. uint16_t text_offset;
  36. uint16_t text_size;
  37. uint16_t data_size;
  38. uint16_t bss_size;
  39. } ulp_binary_header_t;
  40. #define ULP_BINARY_MAGIC_ESP32 (0x00706c75)
  41. static const char* TAG = "ulp";
  42. esp_err_t ulp_run(uint32_t entry_point)
  43. {
  44. #if CONFIG_IDF_TARGET_ESP32
  45. // disable ULP timer
  46. CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  47. // wait for at least 1 RTC_SLOW_CLK cycle
  48. ets_delay_us(10);
  49. // set entry point
  50. REG_SET_FIELD(SENS_SAR_START_FORCE_REG, SENS_PC_INIT, entry_point);
  51. // disable force start
  52. CLEAR_PERI_REG_MASK(SENS_SAR_START_FORCE_REG, SENS_ULP_CP_FORCE_START_TOP_M);
  53. // set time until wakeup is allowed to the smallest possible
  54. REG_SET_FIELD(RTC_CNTL_TIMER5_REG, RTC_CNTL_MIN_SLP_VAL, RTC_CNTL_MIN_SLP_VAL_MIN);
  55. // make sure voltage is raised when RTC 8MCLK is enabled
  56. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_I2C_FOLW_8M);
  57. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_CORE_FOLW_8M);
  58. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_SLEEP_FOLW_8M);
  59. // enable ULP timer
  60. SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  61. #endif
  62. return ESP_OK;
  63. }
  64. esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size)
  65. {
  66. size_t program_size_bytes = program_size * sizeof(uint32_t);
  67. size_t load_addr_bytes = load_addr * sizeof(uint32_t);
  68. if (program_size_bytes < sizeof(ulp_binary_header_t)) {
  69. return ESP_ERR_INVALID_SIZE;
  70. }
  71. if (load_addr_bytes > ULP_RESERVE_MEM) {
  72. return ESP_ERR_INVALID_ARG;
  73. }
  74. if (load_addr_bytes + program_size_bytes > ULP_RESERVE_MEM) {
  75. return ESP_ERR_INVALID_SIZE;
  76. }
  77. // Make a copy of a header in case program_binary isn't aligned
  78. ulp_binary_header_t header;
  79. memcpy(&header, program_binary, sizeof(header));
  80. if (header.magic != ULP_BINARY_MAGIC_ESP32) {
  81. return ESP_ERR_NOT_SUPPORTED;
  82. }
  83. size_t total_size = (size_t) header.text_offset + (size_t) header.text_size +
  84. (size_t) header.data_size;
  85. ESP_LOGD(TAG, "program_size_bytes: %d total_size: %d offset: %d .text: %d, .data: %d, .bss: %d",
  86. program_size_bytes, total_size, header.text_offset,
  87. header.text_size, header.data_size, header.bss_size);
  88. if (total_size != program_size_bytes) {
  89. return ESP_ERR_INVALID_SIZE;
  90. }
  91. size_t text_data_size = header.text_size + header.data_size;
  92. uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
  93. memcpy(base + load_addr_bytes, program_binary + header.text_offset, text_data_size);
  94. memset(base + load_addr_bytes + text_data_size, 0, header.bss_size);
  95. return ESP_OK;
  96. }
  97. esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us)
  98. {
  99. #if CONFIG_IDF_TARGET_ESP32
  100. if (period_index > 4) {
  101. return ESP_ERR_INVALID_ARG;
  102. }
  103. uint64_t period_us_64 = period_us;
  104. uint64_t period_cycles = (period_us_64 << RTC_CLK_CAL_FRACT) / esp_clk_slowclk_cal_get();
  105. uint64_t min_sleep_period_cycles = ULP_FSM_PREPARE_SLEEP_CYCLES
  106. + ULP_FSM_WAKEUP_SLEEP_CYCLES
  107. + REG_GET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT);
  108. if (period_cycles < min_sleep_period_cycles) {
  109. period_cycles = 0;
  110. ESP_LOGW(TAG, "Sleep period clipped to minimum of %d cycles", (uint32_t) min_sleep_period_cycles);
  111. } else {
  112. period_cycles -= min_sleep_period_cycles;
  113. }
  114. REG_SET_FIELD(SENS_ULP_CP_SLEEP_CYC0_REG + period_index * sizeof(uint32_t),
  115. SENS_SLEEP_CYCLES_S0, (uint32_t) period_cycles);
  116. #endif
  117. return ESP_OK;
  118. }