ulp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/ulp.h"
  21. #include "soc/soc.h"
  22. #include "soc/rtc_cntl_reg.h"
  23. #include "soc/sens_reg.h"
  24. #include "sdkconfig.h"
  25. typedef struct {
  26. uint32_t magic;
  27. uint16_t text_offset;
  28. uint16_t text_size;
  29. uint16_t data_size;
  30. uint16_t bss_size;
  31. } ulp_binary_header_t;
  32. #define ULP_BINARY_MAGIC_ESP32 (0x00706c75)
  33. static const char* TAG = "ulp";
  34. esp_err_t ulp_run(uint32_t entry_point)
  35. {
  36. // disable ULP timer
  37. CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  38. // wait for at least 1 RTC_SLOW_CLK cycle
  39. ets_delay_us(10);
  40. // set entry point
  41. SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_PC_INIT_V, entry_point, SENS_PC_INIT_S);
  42. // disable force start
  43. CLEAR_PERI_REG_MASK(SENS_SAR_START_FORCE_REG, SENS_ULP_CP_FORCE_START_TOP_M);
  44. // make sure voltage is raised when RTC 8MCLK is enabled
  45. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_I2C_FOLW_8M);
  46. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_CORE_FOLW_8M);
  47. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BIAS_SLEEP_FOLW_8M);
  48. // enable ULP timer
  49. SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  50. return ESP_OK;
  51. }
  52. esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size)
  53. {
  54. size_t program_size_bytes = program_size * sizeof(uint32_t);
  55. size_t load_addr_bytes = load_addr * sizeof(uint32_t);
  56. if (program_size_bytes < sizeof(ulp_binary_header_t)) {
  57. return ESP_ERR_INVALID_SIZE;
  58. }
  59. if (load_addr_bytes > CONFIG_ULP_COPROC_RESERVE_MEM) {
  60. return ESP_ERR_INVALID_ARG;
  61. }
  62. if (load_addr_bytes + program_size_bytes > CONFIG_ULP_COPROC_RESERVE_MEM) {
  63. return ESP_ERR_INVALID_SIZE;
  64. }
  65. // Make a copy of a header in case program_binary isn't aligned
  66. ulp_binary_header_t header;
  67. memcpy(&header, program_binary, sizeof(header));
  68. if (header.magic != ULP_BINARY_MAGIC_ESP32) {
  69. return ESP_ERR_NOT_SUPPORTED;
  70. }
  71. size_t total_size = (size_t) header.text_offset + (size_t) header.text_size +
  72. (size_t) header.data_size;
  73. ESP_LOGD(TAG, "program_size_bytes: %d total_size: %d offset: %d .text: %d, .data: %d, .bss: %d",
  74. program_size_bytes, total_size, header.text_offset,
  75. header.text_size, header.data_size, header.bss_size);
  76. if (total_size != program_size_bytes) {
  77. return ESP_ERR_INVALID_SIZE;
  78. }
  79. size_t text_data_size = header.text_size + header.data_size;
  80. uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
  81. memcpy(base + load_addr_bytes, program_binary + header.text_offset, text_data_size);
  82. memset(base + load_addr_bytes + text_data_size, 0, header.bss_size);
  83. return ESP_OK;
  84. }