esp_efuse.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include "esp_err.h"
  18. #include "esp_log.h"
  19. #include "sdkconfig.h"
  20. #if CONFIG_IDF_TARGET_ESP32
  21. #include "esp32/esp_efuse.h"
  22. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  23. #include "esp32s2beta/esp_efuse.h"
  24. #endif
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #define ESP_ERR_EFUSE 0x1600 /*!< Base error code for efuse api. */
  29. #define ESP_OK_EFUSE_CNT (ESP_ERR_EFUSE + 0x01) /*!< OK the required number of bits is set. */
  30. #define ESP_ERR_EFUSE_CNT_IS_FULL (ESP_ERR_EFUSE + 0x02) /*!< Error field is full. */
  31. #define ESP_ERR_EFUSE_REPEATED_PROG (ESP_ERR_EFUSE + 0x03) /*!< Error repeated programming of programmed bits is strictly forbidden. */
  32. #define ESP_ERR_CODING (ESP_ERR_EFUSE + 0x04) /*!< Error while a encoding operation. */
  33. /**
  34. * @brief Structure eFuse field
  35. */
  36. typedef struct {
  37. esp_efuse_block_t efuse_block: 8; /**< Block of eFuse */
  38. uint8_t bit_start; /**< Start bit [0..255] */
  39. uint16_t bit_count; /**< Length of bit field [1..-]*/
  40. } esp_efuse_desc_t;
  41. /**
  42. * @brief Reads bits from EFUSE field and writes it into an array.
  43. *
  44. * The number of read bits will be limited to the minimum value
  45. * from the description of the bits in "field" structure or "dst_size_bits" required size.
  46. * Use "esp_efuse_get_field_size()" function to determine the length of the field.
  47. * @param[in] field A pointer to the structure describing the fields of efuse.
  48. * @param[out] dst A pointer to array that will contain the result of reading.
  49. * @param[in] dst_size_bits The number of bits required to read.
  50. * If the requested number of bits is greater than the field,
  51. * the number will be limited to the field size.
  52. *
  53. * @return
  54. * - ESP_OK: The operation was successfully completed.
  55. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  56. */
  57. esp_err_t esp_efuse_read_field_blob(const esp_efuse_desc_t* field[], void* dst, size_t dst_size_bits);
  58. /**
  59. * @brief Read a single bit eFuse field as a boolean value.
  60. *
  61. * @note The value must exist and must be a single bit wide. If there is any possibility of an error
  62. * in the provided arguments, call esp_efuse_read_field_blob() and check the returned value instead.
  63. *
  64. * @note If assertions are enabled and the parameter is invalid, execution will abort
  65. *
  66. * @param[in] field A pointer to the structure describing the fields of efuse.
  67. * @return
  68. * - true: The field parameter is valid and the bit is set.
  69. * - false: The bit is not set, or the parameter is invalid and assertions are disabled.
  70. *
  71. */
  72. bool esp_efuse_read_field_bit(const esp_efuse_desc_t *field[]);
  73. /**
  74. * @brief Reads bits from EFUSE field and returns number of bits programmed as "1".
  75. *
  76. * If the bits are set not sequentially, they will still be counted.
  77. * @param[in] field A pointer to the structure describing the fields of efuse.
  78. * @param[out] out_cnt A pointer that will contain the number of programmed as "1" bits.
  79. *
  80. * @return
  81. * - ESP_OK: The operation was successfully completed.
  82. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  83. */
  84. esp_err_t esp_efuse_read_field_cnt(const esp_efuse_desc_t* field[], size_t* out_cnt);
  85. /**
  86. * @brief Writes array to EFUSE field.
  87. *
  88. * The number of write bits will be limited to the minimum value
  89. * from the description of the bits in "field" structure or "src_size_bits" required size.
  90. * Use "esp_efuse_get_field_size()" function to determine the length of the field.
  91. * After the function is completed, the writing registers are cleared.
  92. * @param[in] field A pointer to the structure describing the fields of efuse.
  93. * @param[in] src A pointer to array that contains the data for writing.
  94. * @param[in] src_size_bits The number of bits required to write.
  95. *
  96. * @return
  97. * - ESP_OK: The operation was successfully completed.
  98. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  99. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  100. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  101. */
  102. esp_err_t esp_efuse_write_field_blob(const esp_efuse_desc_t* field[], const void* src, size_t src_size_bits);
  103. /**
  104. * @brief Writes a required count of bits as "1" to EFUSE field.
  105. *
  106. * If there are no free bits in the field to set the required number of bits to "1",
  107. * ESP_ERR_EFUSE_CNT_IS_FULL error is returned, the field will not be partially recorded.
  108. * After the function is completed, the writing registers are cleared.
  109. * @param[in] field A pointer to the structure describing the fields of efuse.
  110. * @param[in] cnt Required number of programmed as "1" bits.
  111. *
  112. * @return
  113. * - ESP_OK: The operation was successfully completed.
  114. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  115. * - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
  116. */
  117. esp_err_t esp_efuse_write_field_cnt(const esp_efuse_desc_t* field[], size_t cnt);
  118. /**
  119. * @brief Write a single bit eFuse field to 1
  120. *
  121. * For use with eFuse fields that are a single bit. This function will write the bit to value 1 if
  122. * it is not already set, or does nothing if the bit is already set.
  123. *
  124. * This is equivalent to calling esp_efuse_write_field_cnt() with the cnt parameter equal to 1,
  125. * except that it will return ESP_OK if the field is already set to 1.
  126. *
  127. * @param[in] field Pointer to the structure describing the efuse field.
  128. *
  129. * @return
  130. * - ESP_OK: The operation was successfully completed, or the bit was already set to value 1.
  131. * - ESP_ERR_INVALID_ARG: Error in the passed arugments, including if the efuse field is not 1 bit wide.
  132. */
  133. esp_err_t esp_efuse_write_field_bit(const esp_efuse_desc_t* field[]);
  134. /**
  135. * @brief Sets a write protection for the whole block.
  136. *
  137. * After that, it is impossible to write to this block.
  138. * The write protection does not apply to block 0.
  139. * @param[in] blk Block number of eFuse. (EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3)
  140. *
  141. * @return
  142. * - ESP_OK: The operation was successfully completed.
  143. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  144. * - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
  145. * - ESP_ERR_NOT_SUPPORTED: The block does not support this command.
  146. */
  147. esp_err_t esp_efuse_set_write_protect(esp_efuse_block_t blk);
  148. /**
  149. * @brief Sets a read protection for the whole block.
  150. *
  151. * After that, it is impossible to read from this block.
  152. * The read protection does not apply to block 0.
  153. * @param[in] blk Block number of eFuse. (EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3)
  154. *
  155. * @return
  156. * - ESP_OK: The operation was successfully completed.
  157. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  158. * - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
  159. * - ESP_ERR_NOT_SUPPORTED: The block does not support this command.
  160. */
  161. esp_err_t esp_efuse_set_read_protect(esp_efuse_block_t blk);
  162. /**
  163. * @brief Returns the number of bits used by field.
  164. *
  165. * @param[in] field A pointer to the structure describing the fields of efuse.
  166. *
  167. * @return Returns the number of bits used by field.
  168. */
  169. int esp_efuse_get_field_size(const esp_efuse_desc_t* field[]);
  170. /**
  171. * @brief Returns value of efuse register.
  172. *
  173. * This is a thread-safe implementation.
  174. * Example: EFUSE_BLK2_RDATA3_REG where (blk=2, num_reg=3)
  175. * @param[in] blk Block number of eFuse.
  176. * @param[in] num_reg The register number in the block.
  177. *
  178. * @return Value of register
  179. */
  180. uint32_t esp_efuse_read_reg(esp_efuse_block_t blk, unsigned int num_reg);
  181. /**
  182. * @brief Write value to efuse register.
  183. *
  184. * Apply a coding scheme if necessary.
  185. * This is a thread-safe implementation.
  186. * Example: EFUSE_BLK3_WDATA0_REG where (blk=3, num_reg=0)
  187. * @param[in] blk Block number of eFuse.
  188. * @param[in] num_reg The register number in the block.
  189. * @param[in] val Value to write.
  190. *
  191. * @return
  192. * - ESP_OK: The operation was successfully completed.
  193. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  194. */
  195. esp_err_t esp_efuse_write_reg(esp_efuse_block_t blk, unsigned int num_reg, uint32_t val);
  196. /**
  197. * @brief Return efuse coding scheme for blocks.
  198. *
  199. * Note: The coding scheme is applicable only to 1, 2 and 3 blocks. For 0 block, the coding scheme is always ``NONE``.
  200. *
  201. * @param[in] blk Block number of eFuse.
  202. * @return Return efuse coding scheme for blocks
  203. */
  204. esp_efuse_coding_scheme_t esp_efuse_get_coding_scheme(esp_efuse_block_t blk);
  205. /**
  206. * @brief Read key to efuse block starting at the offset and the required size.
  207. *
  208. * @param[in] blk Block number of eFuse.
  209. * @param[in] dst_key A pointer to array that will contain the result of reading.
  210. * @param[in] offset_in_bits Start bit in block.
  211. * @param[in] size_bits The number of bits required to read.
  212. *
  213. * @return
  214. * - ESP_OK: The operation was successfully completed.
  215. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  216. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  217. */
  218. esp_err_t esp_efuse_read_block(esp_efuse_block_t blk, void* dst_key, size_t offset_in_bits, size_t size_bits);
  219. /**
  220. * @brief Write key to efuse block starting at the offset and the required size.
  221. *
  222. * @param[in] blk Block number of eFuse.
  223. * @param[in] src_key A pointer to array that contains the key for writing.
  224. * @param[in] offset_in_bits Start bit in block.
  225. * @param[in] size_bits The number of bits required to write.
  226. *
  227. * @return
  228. * - ESP_OK: The operation was successfully completed.
  229. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  230. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  231. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits
  232. */
  233. esp_err_t esp_efuse_write_block(esp_efuse_block_t blk, const void* src_key, size_t offset_in_bits, size_t size_bits);
  234. /**
  235. * @brief Returns chip version from efuse
  236. *
  237. * @return chip version
  238. */
  239. uint8_t esp_efuse_get_chip_ver(void);
  240. /**
  241. * @brief Returns chip package from efuse
  242. *
  243. * @return chip package
  244. */
  245. uint32_t esp_efuse_get_pkg_ver(void);
  246. /* @brief Permanently update values written to the efuse write registers
  247. *
  248. * After updating EFUSE_BLKx_WDATAx_REG registers with new values to
  249. * write, call this function to permanently write them to efuse.
  250. *
  251. * @note Setting bits in efuse is permanent, they cannot be unset.
  252. *
  253. * @note Due to this restriction you don't need to copy values to
  254. * Efuse write registers from the matching read registers, bits which
  255. * are set in the read register but unset in the matching write
  256. * register will be unchanged when new values are burned.
  257. *
  258. * @note This function is not threadsafe, if calling code updates
  259. * efuse values from multiple tasks then this is caller's
  260. * responsibility to serialise.
  261. *
  262. * After burning new efuses, the read registers are updated to match
  263. * the new efuse values.
  264. */
  265. void esp_efuse_burn_new_values(void);
  266. /* @brief Reset efuse write registers
  267. *
  268. * Efuse write registers are written to zero, to negate
  269. * any changes that have been staged here.
  270. *
  271. * @note This function is not threadsafe, if calling code updates
  272. * efuse values from multiple tasks then this is caller's
  273. * responsibility to serialise.
  274. */
  275. void esp_efuse_reset(void);
  276. /* @brief Disable BASIC ROM Console via efuse
  277. *
  278. * By default, if booting from flash fails the ESP32 will boot a
  279. * BASIC console in ROM.
  280. *
  281. * Call this function (from bootloader or app) to permanently
  282. * disable the console on this chip.
  283. */
  284. void esp_efuse_disable_basic_rom_console(void);
  285. /* @brief Disable ROM Download Mode via eFuse
  286. *
  287. * Permanently disables the ROM Download Mode feature. Once disabled, if the SoC is booted with
  288. * strapping pins set for ROM Download Mode then an error is printed instead.
  289. *
  290. * @note Not all SoCs support this option. An error will be returned if called on an ESP32
  291. * with a silicon revision lower than 3, as these revisions do not support this option.
  292. *
  293. * @note If ROM Download Mode is already disabled, this function does nothing and returns success.
  294. *
  295. * @return
  296. * - ESP_OK If the eFuse was successfully burned, or had already been burned.
  297. * - ESP_ERR_NOT_SUPPORTED (ESP32 only) This SoC is not capable of disabling UART download mode
  298. * - ESP_ERR_INVALID_STATE (ESP32 only) This eFuse is write protected and cannot be written
  299. */
  300. esp_err_t esp_efuse_disable_rom_download_mode(void);
  301. /* @brief Write random data to efuse key block write registers
  302. *
  303. * @note Caller is responsible for ensuring efuse
  304. * block is empty and not write protected, before calling.
  305. *
  306. * @note Behaviour depends on coding scheme: a 256-bit key is
  307. * generated and written for Coding Scheme "None", a 192-bit key
  308. * is generated, extended to 256-bits by the Coding Scheme,
  309. * and then writtten for 3/4 Coding Scheme.
  310. *
  311. * @note This function does not burn the new values, caller should
  312. * call esp_efuse_burn_new_values() when ready to do this.
  313. *
  314. * @param blk_wdata0_reg Address of the first data write register
  315. * in the block
  316. */
  317. void esp_efuse_write_random_key(uint32_t blk_wdata0_reg);
  318. /* @brief Return secure_version from efuse field.
  319. * @return Secure version from efuse field
  320. */
  321. uint32_t esp_efuse_read_secure_version(void);
  322. /* @brief Check secure_version from app and secure_version and from efuse field.
  323. *
  324. * @param secure_version Secure version from app.
  325. * @return
  326. * - True: If version of app is equal or more then secure_version from efuse.
  327. */
  328. bool esp_efuse_check_secure_version(uint32_t secure_version);
  329. /* @brief Write efuse field by secure_version value.
  330. *
  331. * Update the secure_version value is available if the coding scheme is None.
  332. * Note: Do not use this function in your applications. This function is called as part of the other API.
  333. *
  334. * @param[in] secure_version Secure version from app.
  335. * @return
  336. * - ESP_OK: Successful.
  337. * - ESP_FAIL: secure version of app cannot be set to efuse field.
  338. * - ESP_ERR_NOT_SUPPORTED: Anti rollback is not supported with the 3/4 and Repeat coding scheme.
  339. */
  340. esp_err_t esp_efuse_update_secure_version(uint32_t secure_version);
  341. /* @brief Initializes variables: offset and size to simulate the work of an eFuse.
  342. *
  343. * Note: To simulate the work of an eFuse need to set CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE option
  344. * and to add in the partition.csv file a line `efuse_em, data, efuse, , 0x2000,`.
  345. *
  346. * @param[in] offset The starting address of the partition where the eFuse data will be located.
  347. * @param[in] size The size of the partition.
  348. */
  349. void esp_efuse_init(uint32_t offset, uint32_t size);
  350. /* @brief Set the batch mode of writing fields.
  351. *
  352. * This mode allows you to write the fields in the batch mode.
  353. * If this mode is enabled, esp_efuse_batch_write_commit() must be called
  354. * to actually burn any written efuses.
  355. * In this mode, reading efuse is not possible.
  356. * This mode should be used when burning several efuses at one time.
  357. *
  358. * \code{c}
  359. * // Example of using the batch writing mode.
  360. *
  361. * // set the batch writing mode
  362. * esp_efuse_batch_write_begin();
  363. *
  364. * // use any writing functions as usual
  365. * esp_efuse_write_field_blob(ESP_EFUSE_...);
  366. * esp_efuse_write_field_cnt(ESP_EFUSE_...);
  367. * esp_efuse_set_write_protect(EFUSE_BLKx);
  368. * esp_efuse_write_reg(EFUSE_BLKx, ...);
  369. * esp_efuse_write_block(EFUSE_BLKx, ...);
  370. * ...
  371. *
  372. * // Write all of these fields to the efuse registers
  373. * esp_efuse_batch_write_commit();
  374. *
  375. * \endcode
  376. *
  377. * @return
  378. * - ESP_OK: Successful.
  379. */
  380. esp_err_t esp_efuse_batch_write_begin(void);
  381. /* @brief Reset the batch mode of writing fields.
  382. *
  383. * It will reset the batch writing mode and any written changes.
  384. *
  385. * @return
  386. * - ESP_OK: Successful.
  387. * - ESP_ERR_INVALID_STATE: Tha batch mode was not set.
  388. */
  389. esp_err_t esp_efuse_batch_write_cancel(void);
  390. /* @brief Writes all prepared data for the batch mode.
  391. *
  392. * Must be called to ensure changes are written to the efuse registers.
  393. * After this the batch writing mode will be reset.
  394. *
  395. * @return
  396. * - ESP_OK: Successful.
  397. * - ESP_ERR_INVALID_STATE: The deferred writing mode was not set.
  398. */
  399. esp_err_t esp_efuse_batch_write_commit(void);
  400. inline static bool soc_has_cache_lock_bug(void)
  401. {
  402. return (esp_efuse_get_chip_ver() == 3);
  403. }
  404. #ifdef __cplusplus
  405. }
  406. #endif