esp_efuse_utility.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. void esp_efuse_utility_burn_efuses(void);
  85. /**
  86. * @brief Chip specific operations to perform the burn of values written to the efuse write registers.
  87. *
  88. * @note Use esp_efuse_utility_burn_efuses() to burn efuses.
  89. *
  90. * If CONFIG_EFUSE_VIRTUAL is set, writing will not be performed.
  91. * After the function is completed, the writing registers are cleared.
  92. */
  93. void esp_efuse_utility_burn_chip(void);
  94. /**
  95. * @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".
  96. */
  97. int esp_efuse_utility_get_number_of_items(int bits, int size_of_base);
  98. /**
  99. * @brief Reading efuse register.
  100. */
  101. uint32_t esp_efuse_utility_read_reg(esp_efuse_block_t blk, unsigned int num_reg);
  102. /**
  103. * @brief Writing efuse register with checking of repeated programming of programmed bits.
  104. */
  105. esp_err_t esp_efuse_utility_write_reg(unsigned int num_reg, esp_efuse_block_t efuse_block, uint32_t reg_to_write);
  106. /* @brief Reset efuse write registers
  107. *
  108. * Efuse write registers are written to zero, to negate
  109. * any changes that have been staged here.
  110. */
  111. void esp_efuse_utility_reset(void);
  112. /**
  113. * @brief Fills the virt_blocks array by values from efuse_Rdata.
  114. */
  115. void esp_efuse_utility_update_virt_blocks(void);
  116. /**
  117. * @brief Prints efuse values for all registers.
  118. */
  119. void esp_efuse_utility_debug_dump_blocks(void);
  120. /**
  121. * @brief Erase the virt_blocks array.
  122. */
  123. void esp_efuse_utility_erase_virt_blocks(void);
  124. /**
  125. * @brief Apply coding_scheme to write registers.
  126. *
  127. * @return
  128. * - ESP_OK: The operation was successfully completed.
  129. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  130. */
  131. esp_err_t esp_efuse_utility_apply_new_coding_scheme(void);
  132. /**
  133. * @brief Efuse read operation: copies data from physical efuses to efuse read registers.
  134. */
  135. void esp_efuse_utility_clear_program_registers(void);
  136. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  137. /**
  138. * @brief Writes eFuses to the efuse flash partition.
  139. *
  140. * Used only when CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH is set.
  141. */
  142. void esp_efuse_utility_write_efuses_to_flash(void);
  143. /**
  144. * @brief Loads efuses from efuse flash partition.
  145. *
  146. * Used only when CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH is set.
  147. */
  148. bool esp_efuse_utility_load_efuses_from_flash(void);
  149. /**
  150. * @brief Erase efuse flash partition.
  151. *
  152. * Used only when CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH is set.
  153. */
  154. void esp_efuse_utility_erase_efuses_in_flash(void);
  155. #endif
  156. /**
  157. * @brief Return the address of a particular efuse block's first read register
  158. *
  159. * @param[in] block Index of efuse block to look up
  160. *
  161. * @return a numeric read register address of the first word in the block.
  162. */
  163. uint32_t esp_efuse_utility_get_read_register_address(esp_efuse_block_t block);
  164. #ifdef __cplusplus
  165. }
  166. #endif