]> granicus.if.org Git - esp-idf/commit
soc/rtc: wait for SLOW_CLK cycle when switching CPU clock
authorIvan Grokhotkov <ivan@espressif.com>
Thu, 26 Oct 2017 10:46:27 +0000 (18:46 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Thu, 26 Oct 2017 11:53:53 +0000 (19:53 +0800)
commit6d4ed4ff6c4ca6e3b3bd4af3c7aeabe33570b540
treef8b4713792ca65dc1b62206a88f197ef05cd3727
parent05a0fbd49b227fad956c5eec90fac5ef397143b1
soc/rtc: wait for SLOW_CLK cycle when switching CPU clock

Previous implementation waited for 20us after setting
RTC_CNTL_SOC_CLK_SEL_XTL register, using ets_delay_us, assuming that
the CPU was running at XTAL frequency. In reality, clock switch happened
on the next RTC_SLOW_CLK cycle, and CPU could be running at the previous
frequency (for example, 240 MHz) until then.
ets_delay_us would wait for 20 us * 40 cycles per us = 800 CPU cycles
(assuming 40 MHz XTAL; even less with a 26 MHz XTAL).
But if CPU was running at 240 MHz, 800 cycles would pass in just 3.3us,
while SLOW_CLK cycle could happen as much as 1/150kHz = 6.7us after
RTC_CNTL_SOC_CLK_SEL_XTL was set. So the software would not actually wait
long enough for the clock switch to happen, and would disable the PLL
while CPU was still clocked from PLL, leading to a halt.

This implementation uses rtc_clk_wait_for_slow_cycle() function to wait
until the clock switch, removing the need to wait for a fixed number of
CPU cycles.
components/soc/esp32/rtc_clk.c