ulp.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_ESP32S2
  25. #include "esp32s2/clk.h"
  26. #include "esp32s2/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. #elif defined CONFIG_IDF_TARGET_ESP32S2
  62. // disable ULP timer
  63. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  64. // wait for at least 1 RTC_SLOW_CLK cycle
  65. ets_delay_us(10);
  66. // set entry point
  67. REG_SET_FIELD(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_PC_INIT, entry_point);
  68. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL); // Select ULP_TIMER trigger target for ULP.
  69. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE_FORCE); // Select the value for ULP_TIMER sleep. 1: REG_COCPU_DONE;0: ULP END value.
  70. /* Set the number of cycles of ULP_TIMER sleep, the wait time required to start ULP */
  71. REG_SET_FIELD(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_TIMER_SLP_CYCLE, 100);
  72. /* Clear interrupt COCPU status */
  73. REG_WRITE(RTC_CNTL_INT_CLR_REG, RTC_CNTL_COCPU_INT_CLR | RTC_CNTL_COCPU_TRAP_INT_CLR | RTC_CNTL_ULP_CP_INT_CLR);
  74. // start ULP clock gate.
  75. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_CTRL_REG ,RTC_CNTL_ULP_CP_CLK_FO);
  76. // 1: start with timer. wait ULP_TIMER cnt timer.
  77. 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
  78. 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
  79. #endif
  80. return ESP_OK;
  81. }
  82. esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size)
  83. {
  84. size_t program_size_bytes = program_size * sizeof(uint32_t);
  85. size_t load_addr_bytes = load_addr * sizeof(uint32_t);
  86. if (program_size_bytes < sizeof(ulp_binary_header_t)) {
  87. return ESP_ERR_INVALID_SIZE;
  88. }
  89. if (load_addr_bytes > ULP_RESERVE_MEM) {
  90. return ESP_ERR_INVALID_ARG;
  91. }
  92. if (load_addr_bytes + program_size_bytes > ULP_RESERVE_MEM) {
  93. return ESP_ERR_INVALID_SIZE;
  94. }
  95. // Make a copy of a header in case program_binary isn't aligned
  96. ulp_binary_header_t header;
  97. memcpy(&header, program_binary, sizeof(header));
  98. if (header.magic != ULP_BINARY_MAGIC_ESP32) {
  99. return ESP_ERR_NOT_SUPPORTED;
  100. }
  101. size_t total_size = (size_t) header.text_offset + (size_t) header.text_size +
  102. (size_t) header.data_size;
  103. ESP_LOGD(TAG, "program_size_bytes: %d total_size: %d offset: %d .text: %d, .data: %d, .bss: %d",
  104. program_size_bytes, total_size, header.text_offset,
  105. header.text_size, header.data_size, header.bss_size);
  106. if (total_size != program_size_bytes) {
  107. return ESP_ERR_INVALID_SIZE;
  108. }
  109. size_t text_data_size = header.text_size + header.data_size;
  110. uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
  111. memcpy(base + load_addr_bytes, program_binary + header.text_offset, text_data_size);
  112. memset(base + load_addr_bytes + text_data_size, 0, header.bss_size);
  113. return ESP_OK;
  114. }
  115. esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us)
  116. {
  117. #if CONFIG_IDF_TARGET_ESP32
  118. if (period_index > 4) {
  119. return ESP_ERR_INVALID_ARG;
  120. }
  121. uint64_t period_us_64 = period_us;
  122. uint64_t period_cycles = (period_us_64 << RTC_CLK_CAL_FRACT) / esp_clk_slowclk_cal_get();
  123. uint64_t min_sleep_period_cycles = ULP_FSM_PREPARE_SLEEP_CYCLES
  124. + ULP_FSM_WAKEUP_SLEEP_CYCLES
  125. + REG_GET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT);
  126. if (period_cycles < min_sleep_period_cycles) {
  127. period_cycles = 0;
  128. ESP_LOGW(TAG, "Sleep period clipped to minimum of %d cycles", (uint32_t) min_sleep_period_cycles);
  129. } else {
  130. period_cycles -= min_sleep_period_cycles;
  131. }
  132. REG_SET_FIELD(SENS_ULP_CP_SLEEP_CYC0_REG + period_index * sizeof(uint32_t),
  133. SENS_SLEEP_CYCLES_S0, (uint32_t) period_cycles);
  134. #elif defined CONFIG_IDF_TARGET_ESP32S2
  135. if (period_index > 4) {
  136. return ESP_ERR_INVALID_ARG;
  137. }
  138. uint64_t period_us_64 = period_us;
  139. uint64_t period_cycles = (period_us_64 * 1000) / 90; //COCPU sleep clock is 90KHZ.
  140. REG_SET_FIELD(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_TIMER_SLP_CYCLE, period_cycles);
  141. #endif
  142. return ESP_OK;
  143. }