Skip to content

Commit 934cafd

Browse files
committed
SPI1 not SPI0
1 parent 4fcd51a commit 934cafd

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

cores/esp32/Esp.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -547,27 +547,34 @@ uint8_t EspClass::getFlashSourceFrequencyMHz(void) {
547547

548548
/**
549549
* @brief Read the clock divider from hardware using HAL structures
550+
* Based on ESP-IDF HAL implementation:
551+
* - ESP32 classic: Uses SPI1.clock (typedef in spi_flash_ll.h line 52)
552+
* - All modern chips: Use SPIMEM1.clock (typedef in spimem_flash_ll.h)
550553
* @return Clock divider value (1 = no division, 2 = divide by 2, etc.)
551554
*/
552555
uint8_t EspClass::getFlashClockDivider(void) {
553556
#if CONFIG_IDF_TARGET_ESP32
554-
// ESP32 classic: Use SPI0 structure
555-
if (SPI0.clock.clk_equ_sysclk) {
557+
// ESP32 classic: Flash uses SPI1 peripheral (not SPI0)
558+
// See: esp-idf/components/hal/esp32/include/hal/spi_flash_ll.h line 52
559+
if (SPI1.clock.clk_equ_sysclk) {
556560
return 1; // 1:1 clock (no divider)
557561
}
558-
return SPI0.clock.clkcnt_n + 1;
562+
return SPI1.clock.clkcnt_n + 1;
559563
#elif CONFIG_IDF_TARGET_ESP32C5
560-
// ESP32-C5: Uses spi_mem_c_clock_reg_t with mem_ prefix
561-
if (SPIMEM0.clock.mem_clk_equ_sysclk) {
564+
// ESP32-C5: Flash uses SPIMEM1 with mem_ prefixed fields
565+
// See: esp-idf/components/hal/esp32c5/include/hal/spimem_flash_ll.h line 42
566+
if (SPIMEM1.clock.mem_clk_equ_sysclk) {
562567
return 1; // 1:1 clock (no divider)
563568
}
564-
return SPIMEM0.clock.mem_clkcnt_n + 1;
569+
return SPIMEM1.clock.mem_clkcnt_n + 1;
565570
#else
566-
// (S2, S3, C2, C3, C6, H2, P4): Use SPIMEM0 structure without mem_ prefix
567-
if (SPIMEM0.clock.clk_equ_sysclk) {
571+
// (S2, S3, C2, C3, C6, C61, H2, P4): Flash uses SPIMEM1
572+
// See: esp-idf/components/hal/esp32*/include/hal/spimem_flash_ll.h
573+
// Example S3: line 38: typedef typeof(SPIMEM1.clock.val) spimem_flash_ll_clock_reg_t;
574+
if (SPIMEM1.clock.clk_equ_sysclk) {
568575
return 1; // 1:1 clock (no divider)
569576
}
570-
return SPIMEM0.clock.clkcnt_n + 1;
577+
return SPIMEM1.clock.clkcnt_n + 1;
571578
#endif
572579
}
573580

0 commit comments

Comments
 (0)