ulp_riscv.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include "esp_rom_sys.h"
  30. esp_err_t ulp_riscv_run(void)
  31. {
  32. /* Reset COCPU when power on. */
  33. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  34. esp_rom_delay_us(20);
  35. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
  36. /* The coprocessor cpu trap signal doesnt have a stable reset value,
  37. force ULP-RISC-V clock on to stop RTC_COCPU_TRAP_TRIG_EN from waking the CPU*/
  38. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_CLK_FO);
  39. /* Disable ULP timer */
  40. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  41. /* wait for at least 1 RTC_SLOW_CLK cycle */
  42. esp_rom_delay_us(20);
  43. /* Select RISC-V as the ULP_TIMER trigger target. */
  44. CLEAR_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SEL);
  45. /* Select ULP-RISC-V to send the DONE signal. */
  46. SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE_FORCE);
  47. /* start ULP_TIMER */
  48. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_CTRL_REG, RTC_CNTL_ULP_CP_FORCE_START_TOP);
  49. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  50. return ESP_OK;
  51. }
  52. esp_err_t ulp_riscv_load_binary(const uint8_t* program_binary, size_t program_size_bytes)
  53. {
  54. if (program_binary == NULL) {
  55. return ESP_ERR_INVALID_ARG;
  56. }
  57. if (program_size_bytes > ULP_RESERVE_MEM) {
  58. return ESP_ERR_INVALID_SIZE;
  59. }
  60. uint8_t* base = (uint8_t*) RTC_SLOW_MEM;
  61. //Start by clearing memory reserved with zeros, this will also will initialize the bss:
  62. memset(base, 0, ULP_RESERVE_MEM);
  63. memcpy(base, program_binary, program_size_bytes);
  64. return ESP_OK;
  65. }