esp_efuse_utility.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 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 <string.h>
  11. #include "esp_types.h"
  12. #include "esp_err.h"
  13. #include "esp_efuse.h"
  14. #include "sdkconfig.h"
  15. #include_next "esp_efuse_utility.h"
  16. #define ESP_EFUSE_CHK(ret) \
  17. do \
  18. { \
  19. if( ( err = (ret) ) != ESP_OK ) \
  20. goto err_exit; \
  21. } while( 0 )
  22. /**
  23. * @brief Structure range address by blocks
  24. */
  25. typedef struct {
  26. uint32_t start;
  27. uint32_t end;
  28. } esp_efuse_range_addr_t;
  29. /**
  30. * @brief This is type of function that will handle the efuse field register.
  31. *
  32. * @param[in] num_reg The register number in the block.
  33. * @param[in] efuse_block Block number.
  34. * @param[in] bit_start Start bit in the register.
  35. * @param[in] bit_count The number of bits used in the register.
  36. * @param[in/out] arr A pointer to an array or variable.
  37. * @param[in/out] bits_counter Counter bits.
  38. *
  39. * @return
  40. * - ESP_OK: The operation was successfully completed.
  41. * - other efuse component errors.
  42. */
  43. typedef esp_err_t (*efuse_func_proc_t) (unsigned int num_reg, esp_efuse_block_t efuse_block, int starting_bit_num_in_reg, int num_bits_used_in_reg, void* arr, int* bits_counter);
  44. /**
  45. * @brief This function processes the field by calling the passed function.
  46. *
  47. * This function selects the field, checks the length, and calls the register processing function.
  48. * @param[in] field A pointer to the structure describing the fields of efuse.
  49. * @param[in/out] ptr A pointer to an array that is used to read / write from / to the efuse field.
  50. * @param[in] ptr_size_bits The size of the data in bits for the efuse field. if = 0 then read all field bits.
  51. * @param[in] func_proc This is the function that will handle the efuse fields.
  52. *
  53. * @return
  54. * - ESP_OK: The operation was successfully completed.
  55. * - other efuse component errors.
  56. */
  57. esp_err_t esp_efuse_utility_process(const esp_efuse_desc_t* field[], void* ptr, size_t ptr_size_bits, efuse_func_proc_t func_proc);
  58. /**
  59. * @brief Write register with the required number of "1" bits.
  60. * @param[in/out] cnt The number of bits you need to set in the field.
  61. */
  62. esp_err_t esp_efuse_utility_write_cnt(unsigned int num_reg, esp_efuse_block_t efuse_block, int bit_start, int bit_count, void* cnt, int* bits_counter);
  63. /**
  64. * @brief Fill registers from array for writing.
  65. * @param[in] arr_in A pointer to an array in which the data for the writing.
  66. */
  67. esp_err_t esp_efuse_utility_write_blob(unsigned int num_reg, esp_efuse_block_t efuse_block, int bit_start, int bit_count, void* arr_in, int* bits_counter);
  68. /**
  69. * @brief Count a set bits in register.
  70. * @param[in/out] out_cnt A pointer to size_t variable which will contain the number of "1" bits.
  71. */
  72. esp_err_t esp_efuse_utility_count_once(unsigned int num_reg, esp_efuse_block_t efuse_block, int bit_start, int bit_count, void* out_cnt, int* bits_counter);
  73. /**
  74. * @brief Read efuse register and write this value to array.
  75. * @param[out] arr_out A pointer to array that will contain the result of reading.
  76. */
  77. esp_err_t esp_efuse_utility_fill_buff(unsigned int num_reg, esp_efuse_block_t efuse_block, int bit_start, int bit_count, void* arr_out, int* bits_counter);
  78. /**
  79. * @brief Burn values written to the efuse write registers.
  80. *
  81. * If CONFIG_EFUSE_VIRTUAL is set, writing will not be performed.
  82. * After the function is completed, the writing registers are cleared.
  83. *
  84. * @return
  85. * - ESP_OK: The operation was successfully completed.
  86. * - ESP_FAIL: The operation was not successfully completed.
  87. */
  88. esp_err_t esp_efuse_utility_burn_efuses(void);
  89. /**
  90. * @brief Chip specific operations to perform the burn of values written to the efuse write registers.
  91. *
  92. * @note Use esp_efuse_utility_burn_efuses() to burn efuses.
  93. *
  94. * If CONFIG_EFUSE_VIRTUAL is set, writing will not be performed.
  95. * After the function is completed, the writing registers are cleared.
  96. *
  97. * @return
  98. * - ESP_OK: The operation was successfully completed.
  99. * - ESP_FAIL: The operation was not successfully completed.
  100. */
  101. esp_err_t esp_efuse_utility_burn_chip(void);
  102. /**
  103. * @brief Returns the number of array elements for placing these "bits" in an array with the length of each element equal to "size_of_base".
  104. */
  105. int esp_efuse_utility_get_number_of_items(int bits, int size_of_base);
  106. /**
  107. * @brief Reading efuse register.
  108. */
  109. uint32_t esp_efuse_utility_read_reg(esp_efuse_block_t blk, unsigned int num_reg);
  110. /**
  111. * @brief Writing efuse register with checking of repeated programming of programmed bits.
  112. */
  113. esp_err_t esp_efuse_utility_write_reg(esp_efuse_block_t efuse_block, unsigned int num_reg, uint32_t reg_to_write);
  114. /* @brief Reset efuse write registers
  115. *
  116. * Efuse write registers are written to zero, to negate
  117. * any changes that have been staged here.
  118. */
  119. void esp_efuse_utility_reset(void);
  120. /**
  121. * @brief Fills the virt_blocks array by values from efuse_Rdata.
  122. */
  123. void esp_efuse_utility_update_virt_blocks(void);
  124. /**
  125. * @brief Prints efuse values for all registers.
  126. */
  127. void esp_efuse_utility_debug_dump_blocks(void);
  128. /**
  129. * @brief Prints efuse values for a single block.
  130. * @param[in] num_block Index of efuse block.
  131. * @param[in] from_read Take data from:
  132. * true - read area (already burned efuses),
  133. * false - write area (not yet burned efuses, prepared for burn).
  134. */
  135. void esp_efuse_utility_debug_dump_single_block(int num_block, bool from_read);
  136. /**
  137. * @brief Erase the virt_blocks array.
  138. */
  139. void esp_efuse_utility_erase_virt_blocks(void);
  140. /**
  141. * @brief Apply coding_scheme to write registers.
  142. *
  143. * @return
  144. * - ESP_OK: The operation was successfully completed.
  145. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  146. */
  147. esp_err_t esp_efuse_utility_apply_new_coding_scheme(void);
  148. /**
  149. * @brief Checks eFuse errors in BLOCK0.
  150. *
  151. * @note Refers to ESP32-C3 only.
  152. *
  153. * It does a BLOCK0 check if eFuse EFUSE_ERR_RST_ENABLE is set.
  154. * If BLOCK0 has an error, it prints the error and returns ESP_FAIL, which should be treated as esp_restart.
  155. *
  156. * @return
  157. * - ESP_OK: No errors in BLOCK0.
  158. * - ESP_FAIL: Error in BLOCK0 requiring reboot.
  159. */
  160. esp_err_t esp_efuse_utility_check_errors(void);
  161. /**
  162. * @brief Efuse read operation: copies data from physical efuses to efuse read registers.
  163. */
  164. void esp_efuse_utility_clear_program_registers(void);
  165. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  166. /**
  167. * @brief Writes eFuses to the efuse flash partition.
  168. *
  169. * Used only when CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH is set.
  170. */
  171. void esp_efuse_utility_write_efuses_to_flash(void);
  172. /**
  173. * @brief Loads efuses from efuse flash partition.
  174. *
  175. * Used only when CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH is set.
  176. */
  177. bool esp_efuse_utility_load_efuses_from_flash(void);
  178. /**
  179. * @brief Erase efuse flash partition.
  180. *
  181. * Used only when CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH is set.
  182. */
  183. void esp_efuse_utility_erase_efuses_in_flash(void);
  184. #endif
  185. /**
  186. * @brief Return the address of a particular efuse block's first read register
  187. *
  188. * @param[in] block Index of efuse block to look up
  189. *
  190. * @return a numeric read register address of the first word in the block.
  191. */
  192. uint32_t esp_efuse_utility_get_read_register_address(esp_efuse_block_t block);
  193. /**
  194. * @brief Checks the correctness of burned data in the given block.
  195. *
  196. * @note Internal use. Do not call it.
  197. *
  198. * @param[in] block Index of efuse block.
  199. * @param[in] r_data_len Block length for reading data in bytes (multiple of 4).
  200. *
  201. * @return True - written data are correct.
  202. * False - written data are incorrect.
  203. */
  204. bool esp_efuse_utility_is_correct_written_data(esp_efuse_block_t block, unsigned r_data_len);
  205. #ifdef __cplusplus
  206. }
  207. #endif