ulp.c 4.5 KB

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