ulp_riscv_utils.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <stdint.h>
  11. #include "ulp_riscv_register_ops.h"
  12. /**
  13. * @brief Wakeup main CPU from sleep or deep sleep.
  14. *
  15. * This raises a software interrupt signal, if the
  16. * main CPU is configured the ULP as a wakeup source
  17. * calling this function will make the main CPU to
  18. * exit from sleep or deep sleep.
  19. */
  20. void ulp_riscv_wakeup_main_processor(void);
  21. /**
  22. * @brief Rescues the cpu from monitor mode
  23. *
  24. * This function cancels the low power mode
  25. * of the ULP-RISC-V, should be called
  26. * every time the co-processor starts.
  27. *
  28. * @note by convenience this function is
  29. * automatically called in startup code.
  30. */
  31. void ulp_riscv_rescue_from_monitor(void);
  32. /**
  33. * @brief Finishes the ULP program and powers down the ULP
  34. * until next wakeup.
  35. *
  36. * @note This function does not return. After called it will
  37. * fully reset the ULP.
  38. *
  39. * @note Returning from main() in the ULP program results on
  40. * calling this function.
  41. *
  42. * @note To stop the ULP from waking up, call ulp_riscv_timer_stop()
  43. * before halting.
  44. *
  45. * This function should be called after the ULP program Finishes
  46. * its processing, it will trigger the timer for the next wakeup,
  47. * put the ULP in monitor mode and triggers a reset.
  48. *
  49. */
  50. void __attribute__((noreturn)) ulp_riscv_halt(void);
  51. #define ulp_riscv_shutdown ulp_riscv_halt
  52. /**
  53. * @brief Stop the ULP timer
  54. *
  55. * @note This will stop the ULP from waking up if halted, but will not abort any program
  56. * currently executing on the ULP.
  57. */
  58. void ulp_riscv_timer_stop(void);
  59. /**
  60. * @brief Resumes the ULP timer
  61. *
  62. * @note This will resume an already configured timer, but does no other configuration
  63. *
  64. */
  65. void ulp_riscv_timer_resume(void);
  66. #define ULP_RISCV_GET_CCOUNT() ({ int __ccount; \
  67. asm volatile("rdcycle %0;" : "=r"(__ccount)); \
  68. __ccount; })
  69. #if CONFIG_IDF_TARGET_ESP32S2
  70. /* These are only approximate default numbers, the default frequency
  71. of the 8M oscillator is 8.5MHz +/- 5%, at the default DCAP setting
  72. */
  73. #define ULP_RISCV_CYCLES_PER_US 8.5
  74. #elif CONFIG_IDF_TARGET_ESP32S3
  75. #define ULP_RISCV_CYCLES_PER_US 17.5
  76. #endif
  77. #define ULP_RISCV_CYCLES_PER_MS ULP_RISCV_CYCLES_PER_US*1000
  78. /**
  79. * @brief Makes the co-processor busy wait for a certain number of cycles
  80. *
  81. * @param cycles Number of cycles to busy wait
  82. */
  83. void ulp_riscv_delay_cycles(uint32_t cycles);
  84. /**
  85. * @brief Clears the GPIO wakeup interrupt bit
  86. *
  87. */
  88. void ulp_riscv_gpio_wakeup_clear(void);
  89. #ifdef __cplusplus
  90. }
  91. #endif