ulp_riscv.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2020 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 "sdkconfig.h"
  18. #include "esp_attr.h"
  19. #include "esp_err.h"
  20. #include "esp_log.h"
  21. #include "esp32s2/clk.h"
  22. #include "esp32s2/ulp.h"
  23. #include "esp32s2/ulp_riscv.h"
  24. #include "soc/soc.h"
  25. #include "soc/rtc.h"
  26. #include "soc/rtc_cntl_reg.h"
  27. #include "soc/sens_reg.h"
  28. #include "ulp_private.h"
  29. esp_err_t ulp_riscv_run(void)
  30. {
  31. /* Reset COCPU when power on. */
  32. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  33. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  34. ets_delay_us(20);
  35. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  36. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  37. /* Disable ULP timer */
  38. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  39. /* wait for at least 1 RTC_SLOW_CLK cycle */
  40. ets_delay_us(20);
  41. /* Select RISC-V as the ULP_TIMER trigger target. */
  42. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  43. /* Select ULP_TIMER sleep trigger source. 1: REG_COCPU_DONE; 0: ULP END.*/
  44. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE);
  45. /* start ULP_TIMER */
  46. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_CTRL_REG, RTC_CNTL_ULP_CP_FORCE_START_TOP);
  47. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  48. return ESP_OK;
  49. }
  50. esp_err_t ulp_riscv_load_binary(const uint8_t* program_binary, size_t program_size_bytes)
  51. {
  52. if (program_binary == NULL) {
  53. return ESP_ERR_INVALID_ARG;
  54. }
  55. if (program_size_bytes > ULP_RESERVE_MEM) {
  56. return ESP_ERR_INVALID_SIZE;
  57. }
  58. uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
  59. //Start by clearing memory reserved with zeros, this will also will initialize the bss:
  60. memset(base, 0, ULP_RESERVE_MEM);
  61. memcpy(base, program_binary, program_size_bytes);
  62. return ESP_OK;
  63. }