Просмотр исходного кода

esp32c3: Adds support BROWNOUT reset

KonstantinKondrashov 5 лет назад
Родитель
Сommit
d81a93ae4c

+ 8 - 12
components/esp32c3/Kconfig

@@ -78,9 +78,8 @@ menu "ESP32C3-Specific"
             e.g. GCOV data dump.
 
     config ESP32C3_BROWNOUT_DET
-        # TODO ESP32-C3 IDF-2397
         bool
-        default n
+        default y
         help
             The ESP32-S3 has a built-in brownout detector which can detect if the voltage is lower than
             a specific value. If this happens, it will reset the chip in order to prevent unintended
@@ -93,29 +92,26 @@ menu "ESP32C3-Specific"
         help
             The brownout detector will reset the chip when the supply voltage is approximately
             below this level. Note that there may be some variation of brownout voltage level
-            between each ESP3-S3 chip.
+            between each chip.
 
             #The voltage levels here are estimates, more work needs to be done to figure out the exact voltages
             #of the brownout threshold levels.
         config ESP32C3_BROWNOUT_DET_LVL_SEL_7
-            bool "2.44V"
+            bool "2.51V"
         config ESP32C3_BROWNOUT_DET_LVL_SEL_6
-            bool "2.56V"
+            bool "2.64V"
         config ESP32C3_BROWNOUT_DET_LVL_SEL_5
-            bool "2.67V"
+            bool "2.76V"
         config ESP32C3_BROWNOUT_DET_LVL_SEL_4
-            bool "2.84V"
+            bool "2.92V"
         config ESP32C3_BROWNOUT_DET_LVL_SEL_3
-            bool "2.98V"
+            bool "3.10V"
         config ESP32C3_BROWNOUT_DET_LVL_SEL_2
-            bool "3.19V"
-        config ESP32C3_BROWNOUT_DET_LVL_SEL_1
-            bool "3.30V"
+            bool "3.27V"
     endchoice
 
     config ESP32C3_BROWNOUT_DET_LVL
         int
-        default 1 if ESP32C3_BROWNOUT_DET_LVL_SEL_1
         default 2 if ESP32C3_BROWNOUT_DET_LVL_SEL_2
         default 3 if ESP32C3_BROWNOUT_DET_LVL_SEL_3
         default 4 if ESP32C3_BROWNOUT_DET_LVL_SEL_4

+ 8 - 1
components/esp_common/src/brownout.c

@@ -35,6 +35,10 @@
 #define BROWNOUT_DET_LVL CONFIG_ESP32_BROWNOUT_DET_LVL
 #elif defined(CONFIG_ESP32S2_BROWNOUT_DET_LVL)
 #define BROWNOUT_DET_LVL CONFIG_ESP32S2_BROWNOUT_DET_LVL
+#elif defined(CONFIG_ESP32S3_BROWNOUT_DET_LVL)
+#define BROWNOUT_DET_LVL CONFIG_ESP32S3_BROWNOUT_DET_LVL
+#elif defined(CONFIG_ESP32C3_BROWNOUT_DET_LVL)
+#define BROWNOUT_DET_LVL CONFIG_ESP32C3_BROWNOUT_DET_LVL
 #else
 #define BROWNOUT_DET_LVL 0
 #endif
@@ -45,7 +49,7 @@
 #define BROWNOUT_RESET_EN false
 #endif // SOC_BROWNOUT_RESET_SUPPORTED
 
-
+#ifndef SOC_BROWNOUT_RESET_SUPPORTED
 static void rtc_brownout_isr_handler(void *arg)
 {
     /* Normally RTC ISR clears the interrupt flag after the application-supplied
@@ -61,6 +65,7 @@ static void rtc_brownout_isr_handler(void *arg)
     esp_rom_printf("\r\nBrownout detector was triggered\r\n\r\n");
     esp_restart_noos();
 }
+#endif // not SOC_BROWNOUT_RESET_SUPPORTED
 
 void esp_brownout_init(void)
 {
@@ -74,7 +79,9 @@ void esp_brownout_init(void)
 
     brownout_hal_config(&cfg);
 
+#ifndef SOC_BROWNOUT_RESET_SUPPORTED
     rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M);
 
     brownout_hal_intr_enable(true);
+#endif // not SOC_BROWNOUT_RESET_SUPPORTED
 }

+ 4 - 1
components/esp_system/startup.c

@@ -242,7 +242,10 @@ static void do_core_init(void)
 #endif
     }
 
-#if CONFIG_ESP32_BROWNOUT_DET || CONFIG_ESP32S2_BROWNOUT_DET
+#if CONFIG_ESP32_BROWNOUT_DET   || \
+    CONFIG_ESP32S2_BROWNOUT_DET || \
+    CONFIG_ESP32S3_BROWNOUT_DET || \
+    CONFIG_ESP32C3_BROWNOUT_DET
     // [refactor-todo] leads to call chain rtc_is_register (driver) -> esp_intr_alloc (esp32/esp32s2) ->
     // malloc (newlib) -> heap_caps_malloc (heap), so heap must be at least initialized
     esp_brownout_init();

+ 0 - 2
components/esp_system/test/test_reset_reason.c

@@ -264,7 +264,6 @@ TEST_CASE_MULTIPLE_STAGES("reset reason ESP_RST_WDT after RTC watchdog",
         check_reset_reason_any_wdt);
 
 
-#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3) // TODO ESP32-C3 IDF-2397
 static void do_brownout(void)
 {
     setup_values();
@@ -289,7 +288,6 @@ TEST_CASE_MULTIPLE_STAGES("reset reason ESP_RST_BROWNOUT after brownout event",
         "[reset_reason][ignore][reset="BROWNOUT"]",
         do_brownout,
         check_reset_reason_brownout);
-#endif // TODO ESP32-C3 IDF-2397
 
 
 #ifdef CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY

+ 12 - 3
components/hal/esp32c3/brownout_hal.c

@@ -22,15 +22,24 @@
 
 void brownout_hal_config(const brownout_hal_config_t *cfg)
 {
-    // TODO ESP32-C3 IDF-2397
+    REGI2C_WRITE_MASK(I2C_BOD, I2C_BOD_THRESHOLD, cfg->threshold);
+    typeof(RTCCNTL.brown_out) brown_out_reg = {
+        .close_flash_ena = cfg->flash_power_down,
+        .pd_rf_ena = cfg->rf_power_down,
+        .rst_wait = 0x3ff,
+        .rst_ena = cfg->reset_enabled,
+        .ena = cfg->enabled,
+        .rst_sel = 1,
+    };
+    RTCCNTL.brown_out = brown_out_reg;
 }
 
 void brownout_hal_intr_enable(bool enable)
 {
-    // TODO ESP32-C3 IDF-2397
+    RTCCNTL.int_ena.rtc_brown_out = enable;
 }
 
 void brownout_hal_intr_clear(void)
 {
-    // TODO ESP32-C3 IDF-2397
+    RTCCNTL.int_clr.rtc_brown_out = 1;
 }