ulp_riscv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "sdkconfig.h"
  10. #include "esp_attr.h"
  11. #include "esp_err.h"
  12. #include "esp_log.h"
  13. #include "esp_private/esp_clk.h"
  14. #include "ulp_riscv.h"
  15. #include "soc/soc.h"
  16. #include "soc/rtc.h"
  17. #include "soc/rtc_cntl_reg.h"
  18. #include "soc/sens_reg.h"
  19. #include "ulp_common.h"
  20. #include "esp_rom_sys.h"
  21. __attribute__((unused)) static const char* TAG = "ulp-riscv";
  22. static esp_err_t ulp_riscv_config_wakeup_source(ulp_riscv_wakeup_source_t wakeup_source)
  23. {
  24. esp_err_t ret = ESP_OK;
  25. switch (wakeup_source) {
  26. case ULP_RISCV_WAKEUP_SOURCE_TIMER:
  27. /* start ULP_TIMER */
  28. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_CTRL_REG, RTC_CNTL_ULP_CP_FORCE_START_TOP);
  29. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  30. break;
  31. case ULP_RISCV_WAKEUP_SOURCE_GPIO:
  32. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_GPIO_WAKEUP_ENA);
  33. break;
  34. default:
  35. ret = ESP_ERR_INVALID_ARG;
  36. }
  37. return ret;
  38. }
  39. esp_err_t ulp_riscv_config_and_run(ulp_riscv_cfg_t* cfg)
  40. {
  41. esp_err_t ret = ESP_OK;
  42. #if CONFIG_IDF_TARGET_ESP32S3
  43. ESP_LOGE(TAG, "ULP temporarily unsupported on ESP32-S3, running sleep + ULP risks causing permanent damage to chip");
  44. abort();
  45. // Fix in-progress: DIG-160
  46. #endif //CONFIG_IDF_TARGET_ESP32S3
  47. #if CONFIG_IDF_TARGET_ESP32S2
  48. /* Reset COCPU when power on. */
  49. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  50. esp_rom_delay_us(20);
  51. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  52. /* The coprocessor cpu trap signal doesnt have a stable reset value,
  53. force ULP-RISC-V clock on to stop RTC_COCPU_TRAP_TRIG_EN from waking the CPU*/
  54. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  55. /* Disable ULP timer */
  56. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  57. /* wait for at least 1 RTC_SLOW_CLK cycle */
  58. esp_rom_delay_us(20);
  59. /* Select RISC-V as the ULP_TIMER trigger target. */
  60. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  61. /* Select ULP-RISC-V to send the DONE signal. */
  62. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE_FORCE);
  63. ret = ulp_riscv_config_wakeup_source(cfg->wakeup_source);
  64. #elif CONFIG_IDF_TARGET_ESP32S3
  65. /* Reset COCPU when power on. */
  66. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  67. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  68. esp_rom_delay_us(20);
  69. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  70. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  71. /* Disable ULP timer */
  72. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  73. /* wait for at least 1 RTC_SLOW_CLK cycle */
  74. esp_rom_delay_us(20);
  75. /* We do not select RISC-V as the Coprocessor here as this could lead to a hang
  76. * in the main CPU. Instead, we reset RTC_CNTL_COCPU_SEL after we have enabled the ULP timer.
  77. *
  78. * IDF-4510
  79. */
  80. //CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  81. /* Select ULP-RISC-V to send the DONE signal */
  82. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE_FORCE);
  83. /* Set the CLKGATE_EN signal */
  84. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLKGATE_EN);
  85. ret = ulp_riscv_config_wakeup_source(cfg->wakeup_source);
  86. /* Select RISC-V as the ULP_TIMER trigger target
  87. * Selecting the RISC-V as the Coprocessor at the end is a workaround
  88. * for the hang issue recorded in IDF-4510.
  89. */
  90. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  91. /* Clear any spurious wakeup trigger interrupts upon ULP startup */
  92. esp_rom_delay_us(20);
  93. REG_WRITE(RTC_CNTL_INT_CLR_REG, RTC_CNTL_COCPU_INT_CLR | RTC_CNTL_COCPU_TRAP_INT_CLR | RTC_CNTL_ULP_CP_INT_CLR);
  94. #endif
  95. return ret;
  96. }
  97. esp_err_t ulp_riscv_run(void)
  98. {
  99. ulp_riscv_cfg_t cfg = ULP_RISCV_DEFAULT_CONFIG();
  100. return ulp_riscv_config_and_run(&cfg);
  101. }
  102. void ulp_riscv_timer_stop(void)
  103. {
  104. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  105. }
  106. void ulp_riscv_timer_resume(void)
  107. {
  108. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  109. }
  110. void ulp_riscv_halt(void)
  111. {
  112. ulp_riscv_timer_stop();
  113. /* suspends the ulp operation*/
  114. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE);
  115. /* Resets the processor */
  116. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  117. }
  118. esp_err_t ulp_riscv_load_binary(const uint8_t* program_binary, size_t program_size_bytes)
  119. {
  120. if (program_binary == NULL) {
  121. return ESP_ERR_INVALID_ARG;
  122. }
  123. if (program_size_bytes > CONFIG_ULP_COPROC_RESERVE_MEM) {
  124. return ESP_ERR_INVALID_SIZE;
  125. }
  126. uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
  127. //Start by clearing memory reserved with zeros, this will also will initialize the bss:
  128. memset(base, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  129. memcpy(base, program_binary, program_size_bytes);
  130. return ESP_OK;
  131. }