esp_efuse_fields.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2017-2020 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. #include "esp_efuse.h"
  15. #include "esp_efuse_utility.h"
  16. #include "esp_efuse_table.h"
  17. #include "stdlib.h"
  18. #include "esp_types.h"
  19. #include "esp32c3/rom/efuse.h"
  20. #include "assert.h"
  21. #include "esp_err.h"
  22. #include "esp_log.h"
  23. #include "soc/efuse_periph.h"
  24. #include "bootloader_random.h"
  25. #include "sys/param.h"
  26. const static char *TAG = "efuse";
  27. // Contains functions that provide access to efuse fields which are often used in IDF.
  28. // Returns chip version from efuse
  29. uint8_t esp_efuse_get_chip_ver(void)
  30. {
  31. uint32_t chip_ver = 0;
  32. esp_efuse_read_field_blob(ESP_EFUSE_WAFER_VERSION, &chip_ver, ESP_EFUSE_WAFER_VERSION[0]->bit_count);
  33. return chip_ver;
  34. }
  35. // Returns chip package from efuse
  36. uint32_t esp_efuse_get_pkg_ver(void)
  37. {
  38. uint32_t pkg_ver = 0;
  39. esp_efuse_read_field_blob(ESP_EFUSE_PKG_VERSION, &pkg_ver, ESP_EFUSE_PKG_VERSION[0]->bit_count);
  40. return pkg_ver;
  41. }
  42. esp_err_t esp_efuse_set_rom_log_scheme(esp_efuse_rom_log_scheme_t log_scheme)
  43. {
  44. int cur_log_scheme = 0;
  45. esp_efuse_read_field_blob(ESP_EFUSE_UART_PRINT_CONTROL, &cur_log_scheme, 2);
  46. if (!cur_log_scheme) { // not burned yet
  47. return esp_efuse_write_field_blob(ESP_EFUSE_UART_PRINT_CONTROL, &log_scheme, 2);
  48. } else {
  49. return ESP_ERR_INVALID_STATE;
  50. }
  51. }
  52. void esp_efuse_write_random_key(uint32_t blk_wdata0_reg)
  53. {
  54. uint32_t buf[8];
  55. uint8_t raw[24];
  56. bootloader_fill_random(buf, sizeof(buf));
  57. ESP_LOGV(TAG, "Writing random values to address 0x%08x", blk_wdata0_reg);
  58. for (int i = 0; i < 8; i++) {
  59. ESP_LOGV(TAG, "EFUSE_BLKx_WDATA%d_REG = 0x%08x", i, buf[i]);
  60. REG_WRITE(blk_wdata0_reg + 4 * i, buf[i]);
  61. }
  62. bzero(buf, sizeof(buf));
  63. bzero(raw, sizeof(raw));
  64. }
  65. esp_err_t esp_efuse_disable_rom_download_mode(void)
  66. {
  67. return esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MODE);
  68. }
  69. esp_err_t esp_efuse_enable_rom_secure_download_mode(void)
  70. {
  71. if (esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MODE)) {
  72. return ESP_ERR_INVALID_STATE;
  73. }
  74. return esp_efuse_write_field_bit(ESP_EFUSE_ENABLE_SECURITY_DOWNLOAD);
  75. }