@@ -142,14 +142,14 @@ static void printFlashInfo(void) {
142142 chip_report_printf (" Page Size : %8lu B (%6.1f KB)\n " , g_rom_flashchip.page_size , b2kb (g_rom_flashchip.page_size ));
143143
144144 // Runtime flash frequency detection from hardware registers
145- uint32_t actual_freq = getFlashFrequencyMHz ();
146- uint8_t source_freq = getFlashSourceFrequencyMHz ();
147- uint8_t divider = getFlashClockDivider ();
145+ uint32_t actual_freq = ESP. getFlashFrequencyMHz ();
146+ uint8_t source_freq = ESP. getFlashSourceFrequencyMHz ();
147+ uint8_t divider = ESP. getFlashClockDivider ();
148148
149149 chip_report_printf (" Bus Speed : %lu MHz\n " , actual_freq);
150150 chip_report_printf (" Flash Frequency : %lu MHz (source: %u MHz, divider: %u)\n " , actual_freq, source_freq, divider);
151151
152- if (isFlashHighPerformanceModeEnabled ()) {
152+ if (ESP. isFlashHighPerformanceModeEnabled ()) {
153153 chip_report_printf (" HPM Mode : Enabled (> 80 MHz)\n " );
154154 }
155155
@@ -341,115 +341,3 @@ void printAfterSetupInfo(void) {
341341 chip_report_printf (" ============ After Setup End =============\n " );
342342 delay (20 ); // allow the print to finish
343343}
344-
345- // ============================================================================
346- // Flash Frequency Runtime Detection
347- // ============================================================================
348-
349- // Define SPI0 base addresses for different chips
350- #if CONFIG_IDF_TARGET_ESP32S3
351- #define FLASH_SPI0_BASE 0x60003000
352- #elif CONFIG_IDF_TARGET_ESP32S2
353- #define FLASH_SPI0_BASE 0x3f402000
354- #elif CONFIG_IDF_TARGET_ESP32C3
355- #define FLASH_SPI0_BASE 0x60002000
356- #elif CONFIG_IDF_TARGET_ESP32C2
357- #define FLASH_SPI0_BASE 0x60002000
358- #elif CONFIG_IDF_TARGET_ESP32C6
359- #define FLASH_SPI0_BASE 0x60003000
360- #elif CONFIG_IDF_TARGET_ESP32H2
361- #define FLASH_SPI0_BASE 0x60003000
362- #elif CONFIG_IDF_TARGET_ESP32
363- #define FLASH_SPI0_BASE 0x3ff42000
364- #else
365- #define FLASH_SPI0_BASE 0x60003000 // Default for new chips
366- #endif
367-
368- // Register offsets
369- #define FLASH_CORE_CLK_SEL_OFFSET 0x80
370- #define FLASH_CLOCK_OFFSET 0x14
371-
372- /* *
373- * @brief Read the source clock frequency from hardware registers
374- * @return Source clock frequency in MHz (80, 120, 160, or 240)
375- */
376- uint8_t getFlashSourceFrequencyMHz (void ) {
377- #if CONFIG_IDF_TARGET_ESP32
378- // ESP32 classic supports 40 MHz and 80 MHz
379- // Note: ESP32 uses the PLL clock (80 MHz) as source and divides it
380- return 80 ; // Always 80 MHz source, divider determines 40/80 MHz
381- #else
382- volatile uint32_t * core_clk_reg = (volatile uint32_t *)(FLASH_SPI0_BASE + FLASH_CORE_CLK_SEL_OFFSET);
383- uint32_t core_clk_sel = (*core_clk_reg) & 0x3 ; // Bits 0-1
384-
385- uint8_t source_freq = 80 ; // Default
386-
387- #if CONFIG_IDF_TARGET_ESP32S3
388- switch (core_clk_sel) {
389- case 0 : source_freq = 80 ; break ;
390- case 1 : source_freq = 120 ; break ;
391- case 2 : source_freq = 160 ; break ;
392- case 3 : source_freq = 240 ; break ;
393- }
394- #elif CONFIG_IDF_TARGET_ESP32S2
395- switch (core_clk_sel) {
396- case 0 : source_freq = 80 ; break ;
397- case 1 : source_freq = 120 ; break ;
398- case 2 : source_freq = 160 ; break ;
399- }
400- #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 || \
401- CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2
402- switch (core_clk_sel) {
403- case 0 : source_freq = 80 ; break ;
404- case 1 : source_freq = 120 ; break ;
405- }
406- #else
407- switch (core_clk_sel) {
408- case 0 : source_freq = 80 ; break ;
409- case 1 : source_freq = 120 ; break ;
410- default : source_freq = 80 ; break ;
411- }
412- #endif
413-
414- return source_freq;
415- #endif
416- }
417-
418- /* *
419- * @brief Read the clock divider from hardware registers
420- * @return Clock divider value (1 = no division, 2 = divide by 2, etc.)
421- */
422- uint8_t getFlashClockDivider (void ) {
423- volatile uint32_t * clock_reg = (volatile uint32_t *)(FLASH_SPI0_BASE + FLASH_CLOCK_OFFSET);
424- uint32_t clock_val = *clock_reg;
425-
426- // Bit 31: if set, clock is 1:1 (no divider)
427- if (clock_val & (1 << 31 )) {
428- return 1 ;
429- }
430-
431- // Bits 16-23: clkdiv_pre
432- uint8_t clkdiv_pre = (clock_val >> 16 ) & 0xFF ;
433- return clkdiv_pre + 1 ;
434- }
435-
436- /* *
437- * @brief Get the actual flash frequency in MHz
438- * @return Flash frequency in MHz
439- */
440- uint32_t getFlashFrequencyMHz (void ) {
441- uint8_t source = getFlashSourceFrequencyMHz ();
442- uint8_t divider = getFlashClockDivider ();
443-
444- if (divider == 0 ) divider = 1 ; // Safety check
445-
446- return source / divider;
447- }
448-
449- /* *
450- * @brief Check if High Performance Mode is enabled
451- * @return true if flash runs > 80 MHz, false otherwise
452- */
453- bool isFlashHighPerformanceModeEnabled (void ) {
454- return getFlashFrequencyMHz () > 80 ;
455- }
0 commit comments