ulp_riscv.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "hal/misc.h"
  20. #include "ulp_common.h"
  21. #include "esp_rom_sys.h"
  22. #include "esp_check.h"
  23. #include "esp_private/rtc_ctrl.h"
  24. __attribute__((unused)) static const char* TAG = "ulp-riscv";
  25. esp_err_t ulp_riscv_isr_register(intr_handler_t fn, void *arg, uint32_t mask)
  26. {
  27. /* Verify that the ISR callback is valid */
  28. ESP_RETURN_ON_FALSE(fn, ESP_ERR_INVALID_ARG, TAG, "ULP RISC-V ISR is NULL");
  29. /* Verify that the interrupt bits are valid */
  30. if (!(mask & (RTC_CNTL_COCPU_INT_ST_M | RTC_CNTL_COCPU_TRAP_INT_ST_M))) {
  31. ESP_LOGE(TAG, "Invalid bitmask for ULP RISC-V interrupts");
  32. return ESP_ERR_INVALID_ARG;
  33. }
  34. /* Make sure we enable only the ULP interrupt bits.
  35. * We don't want other RTC interrupts triggering this ISR.
  36. */
  37. mask &= (RTC_CNTL_COCPU_INT_ST_M | RTC_CNTL_COCPU_TRAP_INT_ST_M);
  38. /* Register the RTC ISR */
  39. ESP_RETURN_ON_ERROR(rtc_isr_register(fn, arg, mask, 0), TAG, "rtc_isr_register() failed");
  40. /* Enable the interrupt bits */
  41. SET_PERI_REG_MASK(RTC_CNTL_INT_ENA_REG, mask);
  42. return ESP_OK;
  43. }
  44. esp_err_t ulp_riscv_isr_deregister(intr_handler_t fn, void *arg, uint32_t mask)
  45. {
  46. /* Verify that the ISR callback is valid */
  47. ESP_RETURN_ON_FALSE(fn, ESP_ERR_INVALID_ARG, TAG, "ULP RISC-V ISR is NULL");
  48. /* Verify that the interrupt bits are valid */
  49. if (!(mask & (RTC_CNTL_COCPU_INT_ST_M | RTC_CNTL_COCPU_TRAP_INT_ST_M))) {
  50. ESP_LOGE(TAG, "Invalid bitmask for ULP RISC-V interrupts");
  51. return ESP_ERR_INVALID_ARG;
  52. }
  53. /* Make sure we disable only the ULP interrupt bits */
  54. mask &= (RTC_CNTL_COCPU_INT_ST_M | RTC_CNTL_COCPU_TRAP_INT_ST_M);
  55. /* Disable the interrupt bits */
  56. CLEAR_PERI_REG_MASK(RTC_CNTL_INT_ENA_REG, mask);
  57. /* Deregister the RTC ISR */
  58. ESP_RETURN_ON_ERROR(rtc_isr_deregister(fn, arg), TAG, "rtc_isr_deregister() failed");
  59. return ESP_OK;
  60. }
  61. static esp_err_t ulp_riscv_config_wakeup_source(ulp_riscv_wakeup_source_t wakeup_source)
  62. {
  63. esp_err_t ret = ESP_OK;
  64. switch (wakeup_source) {
  65. case ULP_RISCV_WAKEUP_SOURCE_TIMER:
  66. /* start ULP_TIMER */
  67. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_CTRL_REG, RTC_CNTL_ULP_CP_FORCE_START_TOP);
  68. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  69. break;
  70. case ULP_RISCV_WAKEUP_SOURCE_GPIO:
  71. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_GPIO_WAKEUP_ENA);
  72. break;
  73. default:
  74. ret = ESP_ERR_INVALID_ARG;
  75. }
  76. return ret;
  77. }
  78. esp_err_t ulp_riscv_config_and_run(ulp_riscv_cfg_t* cfg)
  79. {
  80. esp_err_t ret = ESP_OK;
  81. #if CONFIG_IDF_TARGET_ESP32S2
  82. /* Reset COCPU when power on. */
  83. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  84. /* The coprocessor cpu trap signal doesnt have a stable reset value,
  85. force ULP-RISC-V clock on to stop RTC_COCPU_TRAP_TRIG_EN from waking the CPU*/
  86. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  87. /* Disable ULP timer */
  88. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  89. /* wait for at least 1 RTC_SLOW_CLK cycle */
  90. esp_rom_delay_us(20);
  91. /* Select RISC-V as the ULP_TIMER trigger target. */
  92. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  93. /* Select ULP-RISC-V to send the DONE signal. */
  94. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE_FORCE);
  95. ret = ulp_riscv_config_wakeup_source(cfg->wakeup_source);
  96. #elif CONFIG_IDF_TARGET_ESP32S3
  97. /* Reset COCPU when power on. */
  98. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  99. /* The coprocessor cpu trap signal doesnt have a stable reset value,
  100. force ULP-RISC-V clock on to stop RTC_COCPU_TRAP_TRIG_EN from waking the CPU*/
  101. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  102. /* Disable ULP timer */
  103. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  104. /* wait for at least 1 RTC_SLOW_CLK cycle */
  105. esp_rom_delay_us(20);
  106. /* We do not select RISC-V as the Coprocessor here as this could lead to a hang
  107. * in the main CPU. Instead, we reset RTC_CNTL_COCPU_SEL after we have enabled the ULP timer.
  108. *
  109. * IDF-4510
  110. */
  111. //CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  112. /* Select ULP-RISC-V to send the DONE signal */
  113. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE_FORCE);
  114. /* Set the CLKGATE_EN signal */
  115. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLKGATE_EN);
  116. ret = ulp_riscv_config_wakeup_source(cfg->wakeup_source);
  117. /* Select RISC-V as the ULP_TIMER trigger target
  118. * Selecting the RISC-V as the Coprocessor at the end is a workaround
  119. * for the hang issue recorded in IDF-4510.
  120. */
  121. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  122. /* Clear any spurious wakeup trigger interrupts upon ULP startup */
  123. esp_rom_delay_us(20);
  124. REG_WRITE(RTC_CNTL_INT_CLR_REG, RTC_CNTL_COCPU_INT_CLR | RTC_CNTL_COCPU_TRAP_INT_CLR | RTC_CNTL_ULP_CP_INT_CLR);
  125. #endif
  126. return ret;
  127. }
  128. esp_err_t ulp_riscv_run(void)
  129. {
  130. ulp_riscv_cfg_t cfg = ULP_RISCV_DEFAULT_CONFIG();
  131. return ulp_riscv_config_and_run(&cfg);
  132. }
  133. void ulp_riscv_timer_stop(void)
  134. {
  135. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  136. }
  137. void ulp_riscv_timer_resume(void)
  138. {
  139. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  140. }
  141. void ulp_riscv_halt(void)
  142. {
  143. ulp_riscv_timer_stop();
  144. /* suspends the ulp operation*/
  145. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE);
  146. /* Resets the processor */
  147. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  148. }
  149. void ulp_riscv_reset()
  150. {
  151. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT | RTC_CNTL_COCPU_DONE);
  152. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  153. esp_rom_delay_us(20);
  154. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT | RTC_CNTL_COCPU_DONE);
  155. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  156. }
  157. esp_err_t ulp_riscv_load_binary(const uint8_t* program_binary, size_t program_size_bytes)
  158. {
  159. if (program_binary == NULL) {
  160. return ESP_ERR_INVALID_ARG;
  161. }
  162. if (program_size_bytes > CONFIG_ULP_COPROC_RESERVE_MEM) {
  163. return ESP_ERR_INVALID_SIZE;
  164. }
  165. uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
  166. //Start by clearing memory reserved with zeros, this will also will initialize the bss:
  167. hal_memset(base, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  168. hal_memcpy(base, program_binary, program_size_bytes);
  169. return ESP_OK;
  170. }