ulp_common.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. /* This file contains definitions that are common between esp32/ulp.h,
  8. esp32s2/ulp.h and esp32s3/ulp.h
  9. */
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**@{*/
  14. #define ESP_ERR_ULP_BASE 0x1200 /*!< Offset for ULP-related error codes */
  15. #define ESP_ERR_ULP_SIZE_TOO_BIG (ESP_ERR_ULP_BASE + 1) /*!< Program doesn't fit into RTC memory reserved for the ULP */
  16. #define ESP_ERR_ULP_INVALID_LOAD_ADDR (ESP_ERR_ULP_BASE + 2) /*!< Load address is outside of RTC memory reserved for the ULP */
  17. #define ESP_ERR_ULP_DUPLICATE_LABEL (ESP_ERR_ULP_BASE + 3) /*!< More than one label with the same number was defined */
  18. #define ESP_ERR_ULP_UNDEFINED_LABEL (ESP_ERR_ULP_BASE + 4) /*!< Branch instructions references an undefined label */
  19. #define ESP_ERR_ULP_BRANCH_OUT_OF_RANGE (ESP_ERR_ULP_BASE + 5) /*!< Branch target is out of range of B instruction (try replacing with BX) */
  20. /**@}*/
  21. union ulp_insn; // Declared in the chip-specific ulp.h header
  22. typedef union ulp_insn ulp_insn_t;
  23. /**
  24. * @brief Resolve all macro references in a program and load it into RTC memory
  25. * @param load_addr address where the program should be loaded, expressed in 32-bit words
  26. * @param program ulp_insn_t array with the program
  27. * @param psize size of the program, expressed in 32-bit words
  28. * @return
  29. * - ESP_OK on success
  30. * - ESP_ERR_NO_MEM if auxiliary temporary structure can not be allocated
  31. * - one of ESP_ERR_ULP_xxx if program is not valid or can not be loaded
  32. */
  33. esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* program, size_t* psize);
  34. /**
  35. * @brief Load ULP program binary into RTC memory
  36. *
  37. * ULP program binary should have the following format (all values little-endian):
  38. *
  39. * 1. MAGIC, (value 0x00706c75, 4 bytes)
  40. * 2. TEXT_OFFSET, offset of .text section from binary start (2 bytes)
  41. * 3. TEXT_SIZE, size of .text section (2 bytes)
  42. * 4. DATA_SIZE, size of .data section (2 bytes)
  43. * 5. BSS_SIZE, size of .bss section (2 bytes)
  44. * 6. (TEXT_OFFSET - 12) bytes of arbitrary data (will not be loaded into RTC memory)
  45. * 7. .text section
  46. * 8. .data section
  47. *
  48. * Linker script in components/ulp/ld/esp32.ulp.ld produces ELF files which
  49. * correspond to this format. This linker script produces binaries with load_addr == 0.
  50. *
  51. * @param load_addr address where the program should be loaded, expressed in 32-bit words
  52. * @param program_binary pointer to program binary
  53. * @param program_size size of the program binary
  54. * @return
  55. * - ESP_OK on success
  56. * - ESP_ERR_INVALID_ARG if load_addr is out of range
  57. * - ESP_ERR_INVALID_SIZE if program_size doesn't match (TEXT_OFFSET + TEXT_SIZE + DATA_SIZE)
  58. * - ESP_ERR_NOT_SUPPORTED if the magic number is incorrect
  59. */
  60. esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size);
  61. /**
  62. * @brief Run the program loaded into RTC memory
  63. * @param entry_point entry point, expressed in 32-bit words
  64. * @return ESP_OK on success
  65. */
  66. esp_err_t ulp_run(uint32_t entry_point);
  67. /**
  68. * @brief Set one of ULP wakeup period values
  69. *
  70. * ULP coprocessor starts running the program when the wakeup timer counts up
  71. * to a given value (called period). There are 5 period values which can be
  72. * programmed into SENS_ULP_CP_SLEEP_CYCx_REG registers, x = 0..4 for ESP32, and
  73. * one period value which can be programmed into RTC_CNTL_ULP_CP_TIMER_1_REG register for ESP32-S2.
  74. * By default, for ESP32, wakeup timer will use the period set into SENS_ULP_CP_SLEEP_CYC0_REG,
  75. * i.e. period number 0. ULP program code can use SLEEP instruction to select
  76. * which of the SENS_ULP_CP_SLEEP_CYCx_REG should be used for subsequent wakeups.
  77. *
  78. * However, please note that SLEEP instruction issued (from ULP program) while the system
  79. * is in deep sleep mode does not have effect, and sleep cycle count 0 is used.
  80. *
  81. * For ESP32-s2 the SLEEP instruction not exist. Instead a WAKE instruction will be used.
  82. *
  83. * @param period_index wakeup period setting number (0 - 4)
  84. * @param period_us wakeup period, us
  85. * @note The ULP FSM requires two clock cycles to wakeup before being able to run the program.
  86. * Then additional 16 cycles are reserved after wakeup waiting until the 8M clock is stable.
  87. * The FSM also requires two more clock cycles to go to sleep after the program execution is halted.
  88. * The minimum wakeup period that may be set up for the ULP
  89. * is equal to the total number of cycles spent on the above internal tasks.
  90. * For a default configuration of the ULP running at 150kHz it makes about 133us.
  91. * @return
  92. * - ESP_OK on success
  93. * - ESP_ERR_INVALID_ARG if period_index is out of range
  94. */
  95. esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us);
  96. #ifdef __cplusplus
  97. }
  98. #endif