ulp_riscv.c 4.9 KB

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