ulp_fsm_common.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "esp_intr_alloc.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**@{*/
  15. #define ESP_ERR_ULP_BASE 0x1200 /*!< Offset for ULP-related error codes */
  16. #define ESP_ERR_ULP_SIZE_TOO_BIG (ESP_ERR_ULP_BASE + 1) /*!< Program doesn't fit into RTC memory reserved for the ULP */
  17. #define ESP_ERR_ULP_INVALID_LOAD_ADDR (ESP_ERR_ULP_BASE + 2) /*!< Load address is outside of RTC memory reserved for the ULP */
  18. #define ESP_ERR_ULP_DUPLICATE_LABEL (ESP_ERR_ULP_BASE + 3) /*!< More than one label with the same number was defined */
  19. #define ESP_ERR_ULP_UNDEFINED_LABEL (ESP_ERR_ULP_BASE + 4) /*!< Branch instructions references an undefined label */
  20. #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) */
  21. /**@}*/
  22. union ulp_insn; // Declared in the chip-specific ulp.h header
  23. typedef union ulp_insn ulp_insn_t;
  24. /**
  25. * @brief Register ULP wakeup signal ISR
  26. *
  27. * @note The ISR routine will only be active if the main CPU is not in deepsleep
  28. *
  29. * @param fn ISR callback function
  30. * @param arg ISR callback function arguments
  31. * @return
  32. * - ESP_OK on success
  33. * - ESP_ERR_INVALID_ARG if callback function is NULL
  34. * - ESP_ERR_NO_MEM if heap memory cannot be allocated for the interrupt
  35. */
  36. esp_err_t ulp_isr_register(intr_handler_t fn, void *arg);
  37. /**
  38. * @brief Deregister ULP wakeup signal ISR
  39. *
  40. * @param fn ISR callback function
  41. * @param arg ISR callback function arguments
  42. * @return
  43. * - ESP_OK on success
  44. * - ESP_ERR_INVALID_ARG if callback function is NULL
  45. * - ESP_ERR_INVALID_STATE if a handler matching both callback function and its arguments isn't registered
  46. */
  47. esp_err_t ulp_isr_deregister(intr_handler_t fn, void *arg);
  48. /**
  49. * @brief Resolve all macro references in a program and load it into RTC memory
  50. * @param load_addr address where the program should be loaded, expressed in 32-bit words
  51. * @param program ulp_insn_t array with the program
  52. * @param psize size of the program, expressed in 32-bit words
  53. * @return
  54. * - ESP_OK on success
  55. * - ESP_ERR_NO_MEM if auxiliary temporary structure can not be allocated
  56. * - one of ESP_ERR_ULP_xxx if program is not valid or can not be loaded
  57. */
  58. esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* program, size_t* psize);
  59. /**
  60. * @brief Load ULP program binary into RTC memory
  61. *
  62. * ULP program binary should have the following format (all values little-endian):
  63. *
  64. * 1. MAGIC, (value 0x00706c75, 4 bytes)
  65. * 2. TEXT_OFFSET, offset of .text section from binary start (2 bytes)
  66. * 3. TEXT_SIZE, size of .text section (2 bytes)
  67. * 4. DATA_SIZE, size of .data section (2 bytes)
  68. * 5. BSS_SIZE, size of .bss section (2 bytes)
  69. * 6. (TEXT_OFFSET - 12) bytes of arbitrary data (will not be loaded into RTC memory)
  70. * 7. .text section
  71. * 8. .data section
  72. *
  73. * Linker script in components/ulp/ld/esp32.ulp.ld produces ELF files which
  74. * correspond to this format. This linker script produces binaries with load_addr == 0.
  75. *
  76. * @param load_addr address where the program should be loaded, expressed in 32-bit words
  77. * @param program_binary pointer to program binary
  78. * @param program_size size of the program binary
  79. * @return
  80. * - ESP_OK on success
  81. * - ESP_ERR_INVALID_ARG if load_addr is out of range
  82. * - ESP_ERR_INVALID_SIZE if program_size doesn't match (TEXT_OFFSET + TEXT_SIZE + DATA_SIZE)
  83. * - ESP_ERR_NOT_SUPPORTED if the magic number is incorrect
  84. */
  85. esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size);
  86. /**
  87. * @brief Run the program loaded into RTC memory
  88. * @param entry_point entry point, expressed in 32-bit words
  89. * @return ESP_OK on success
  90. */
  91. esp_err_t ulp_run(uint32_t entry_point);
  92. #ifdef __cplusplus
  93. }
  94. #endif