ulp_riscv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_ESP32S2
  43. /* Reset COCPU when power on. */
  44. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  45. esp_rom_delay_us(20);
  46. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  47. /* The coprocessor cpu trap signal doesnt have a stable reset value,
  48. force ULP-RISC-V clock on to stop RTC_COCPU_TRAP_TRIG_EN from waking the CPU*/
  49. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  50. /* Disable ULP timer */
  51. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  52. /* wait for at least 1 RTC_SLOW_CLK cycle */
  53. esp_rom_delay_us(20);
  54. /* Select RISC-V as the ULP_TIMER trigger target. */
  55. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  56. /* Select ULP-RISC-V to send the DONE signal. */
  57. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE_FORCE);
  58. ret = ulp_riscv_config_wakeup_source(cfg->wakeup_source);
  59. #elif CONFIG_IDF_TARGET_ESP32S3
  60. /* Reset COCPU when power on. */
  61. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  62. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  63. esp_rom_delay_us(20);
  64. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  65. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  66. /* Disable ULP timer */
  67. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  68. /* wait for at least 1 RTC_SLOW_CLK cycle */
  69. esp_rom_delay_us(20);
  70. /* We do not select RISC-V as the Coprocessor here as this could lead to a hang
  71. * in the main CPU. Instead, we reset RTC_CNTL_COCPU_SEL after we have enabled the ULP timer.
  72. *
  73. * IDF-4510
  74. */
  75. //CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  76. /* Select ULP-RISC-V to send the DONE signal */
  77. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE_FORCE);
  78. /* Set the CLKGATE_EN signal */
  79. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLKGATE_EN);
  80. ret = ulp_riscv_config_wakeup_source(cfg->wakeup_source);
  81. /* Select RISC-V as the ULP_TIMER trigger target
  82. * Selecting the RISC-V as the Coprocessor at the end is a workaround
  83. * for the hang issue recorded in IDF-4510.
  84. */
  85. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  86. /* Clear any spurious wakeup trigger interrupts upon ULP startup */
  87. esp_rom_delay_us(20);
  88. REG_WRITE(RTC_CNTL_INT_CLR_REG, RTC_CNTL_COCPU_INT_CLR | RTC_CNTL_COCPU_TRAP_INT_CLR | RTC_CNTL_ULP_CP_INT_CLR);
  89. #endif
  90. return ret;
  91. }
  92. esp_err_t ulp_riscv_run(void)
  93. {
  94. ulp_riscv_cfg_t cfg = ULP_RISCV_DEFAULT_CONFIG();
  95. return ulp_riscv_config_and_run(&cfg);
  96. }
  97. void ulp_riscv_timer_stop(void)
  98. {
  99. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  100. }
  101. void ulp_riscv_timer_resume(void)
  102. {
  103. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  104. }
  105. void ulp_riscv_halt(void)
  106. {
  107. ulp_riscv_timer_stop();
  108. /* suspends the ulp operation*/
  109. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE);
  110. /* Resets the processor */
  111. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  112. }
  113. esp_err_t ulp_riscv_load_binary(const uint8_t* program_binary, size_t program_size_bytes)
  114. {
  115. if (program_binary == NULL) {
  116. return ESP_ERR_INVALID_ARG;
  117. }
  118. if (program_size_bytes > CONFIG_ULP_COPROC_RESERVE_MEM) {
  119. return ESP_ERR_INVALID_SIZE;
  120. }
  121. uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
  122. //Start by clearing memory reserved with zeros, this will also will initialize the bss:
  123. #pragma GCC diagnostic push
  124. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  125. #pragma GCC diagnostic ignored "-Warray-bounds"
  126. memset(base, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  127. memcpy(base, program_binary, program_size_bytes);
  128. #pragma GCC diagnostic pop
  129. return ESP_OK;
  130. }