Skip to content

Commit 2c9ab21

Browse files
committed
Merge branch 'refactor/fine_tune_dphy_pll_v5.3' into 'release/v5.3'
feat(mipi): fine tune DPHY PLL clock (v5.3) See merge request espressif/esp-idf!35292
2 parents 369befb + 6f992ac commit 2c9ab21

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

components/esp_hw_support/port/esp32p4/esp_clk_tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ esp_err_t esp_clk_tree_src_get_freq_hz(soc_module_clk_t clk_src, esp_clk_tree_sr
3131
clk_src_freq = clk_hal_xtal_get_freq_mhz() * MHZ;
3232
break;
3333
case SOC_MOD_CLK_PLL_F20M:
34-
clk_src_freq = CLK_LL_PLL_20M_FREQ_MHZ * MHZ;
34+
clk_src_freq = CLK_LL_PLL_480M_FREQ_MHZ / clk_ll_pll_f20m_get_divider() * MHZ;
3535
break;
3636
case SOC_MOD_CLK_PLL_F80M:
3737
clk_src_freq = CLK_LL_PLL_80M_FREQ_MHZ * MHZ;

components/hal/esp32p4/include/hal/clk_tree_ll.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ extern "C" {
3535

3636
#define CLK_LL_PLL_8M_FREQ_MHZ (8)
3737

38-
#define CLK_LL_PLL_20M_FREQ_MHZ (20)
3938
#define CLK_LL_PLL_80M_FREQ_MHZ (80)
4039
#define CLK_LL_PLL_160M_FREQ_MHZ (160)
4140
#define CLK_LL_PLL_240M_FREQ_MHZ (240)
@@ -693,6 +692,27 @@ static inline __attribute__((always_inline)) void clk_ll_pll_f25m_set_divider(ui
693692
HAL_FORCE_MODIFY_U32_REG_FIELD(HP_SYS_CLKRST.ref_clk_ctrl0, reg_ref_25m_clk_div_num, divider - 1);
694693
}
695694

695+
/**
696+
* @brief Set PLL_F20M_CLK divider. freq of PLL_F20M_CLK = freq of SPLL_CLK / divider
697+
*
698+
* @param divider Divider. CLK_DIV_NUM = divider - 1.
699+
*/
700+
static inline __attribute__((always_inline)) void clk_ll_pll_f20m_set_divider(uint32_t divider)
701+
{
702+
HAL_ASSERT(divider >= 1);
703+
HAL_FORCE_MODIFY_U32_REG_FIELD(HP_SYS_CLKRST.ref_clk_ctrl1, reg_ref_20m_clk_div_num, divider - 1);
704+
}
705+
706+
/**
707+
* @brief Get PLL_F20M_CLK divider
708+
*
709+
* @return Divider. Divider = (CLK_DIV_NUM + 1).
710+
*/
711+
static inline __attribute__((always_inline)) uint32_t clk_ll_pll_f20m_get_divider(void)
712+
{
713+
return HAL_FORCE_READ_U32_REG_FIELD(HP_SYS_CLKRST.ref_clk_ctrl1, reg_ref_20m_clk_div_num) + 1;
714+
}
715+
696716
/**
697717
* @brief Select the clock source for RTC_SLOW_CLK
698718
*

components/hal/mipi_dsi_hal.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,20 @@ void mipi_dsi_hal_configure_phy_pll(mipi_dsi_hal_context_t *hal, uint32_t phy_cl
4747
// 5MHz <= f_ref/N <= 40MHz
4848
uint8_t min_N = MAX(1, ref_freq_mhz / 40);
4949
uint8_t max_N = ref_freq_mhz / 5;
50+
uint16_t min_delta = UINT16_MAX;
5051
for (uint8_t n = min_N; n <= max_N; n++) {
5152
uint16_t m = vco_freq_mhz * n / ref_freq_mhz;
5253
// M must be even number
5354
if ((m & 0x01) == 0) {
54-
pll_M = m;
55-
pll_N = n;
56-
break;
55+
uint16_t delta = vco_freq_mhz - ref_freq_mhz * m / n;
56+
if (delta < min_delta) {
57+
min_delta = delta;
58+
pll_M = m;
59+
pll_N = n;
60+
if (min_delta == 0) {
61+
break;
62+
}
63+
}
5764
}
5865
}
5966
HAL_ASSERT(pll_M && pll_N);

components/soc/esp32p4/include/soc/clk_tree_defs.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ typedef enum {
143143
SOC_MOD_CLK_RTC_FAST, /*!< RTC_FAST_CLK can be sourced from XTAL, RC_FAST, or LP_PLL by configuring soc_rtc_fast_clk_src_t */
144144
SOC_MOD_CLK_RTC_SLOW, /*!< RTC_SLOW_CLK can be sourced from RC_SLOW, XTAL32K, or RC32K by configuring soc_rtc_slow_clk_src_t */
145145
// For digital domain: peripherals
146-
SOC_MOD_CLK_PLL_F20M, /*!< PLL_F20M_CLK is derived from SPLL (clock gating + "fixed" divider of 24), it has a fixed frequency of 20MHz */
146+
SOC_MOD_CLK_PLL_F20M, /*!< PLL_F20M_CLK is derived from SPLL (clock gating + default divider 24), its default frequency is 20MHz */
147147
SOC_MOD_CLK_PLL_F25M, /*!< PLL_F25M_CLK is derived from MPLL (clock gating + configurable divider), it will have a frequency of 25MHz */
148-
SOC_MOD_CLK_PLL_F80M, /*!< PLL_F80M_CLK is derived from SPLL (clock gating + "fixed" divider of 6), it has a fixed frequency of 80MHz */
149-
SOC_MOD_CLK_PLL_F160M, /*!< PLL_F160M_CLK is derived from SPLL (clock gating + "fixed" divider of 3), it has a fixed frequency of 160MHz */
150-
SOC_MOD_CLK_PLL_F240M, /*!< PLL_F240M_CLK is derived from SPLL (clock gating + "fixed" divider of 2), it has a fixed frequency of 240MHz */
148+
SOC_MOD_CLK_PLL_F80M, /*!< PLL_F80M_CLK is derived from SPLL (clock gating + default divider 6), its default frequency is 80MHz */
149+
SOC_MOD_CLK_PLL_F160M, /*!< PLL_F160M_CLK is derived from SPLL (clock gating + default divider 3), its default frequency is 160MHz */
150+
SOC_MOD_CLK_PLL_F240M, /*!< PLL_F240M_CLK is derived from SPLL (clock gating + default divider 2), its default frequency is 240MHz */
151151
SOC_MOD_CLK_CPLL, /*!< CPLL is from 40MHz XTAL oscillator frequency multipliers */
152-
SOC_MOD_CLK_SPLL, /*!< SPLL is from 40MHz XTAL oscillator frequency multipliers, it has a "fixed" frequency of 480MHz */
152+
SOC_MOD_CLK_SPLL, /*!< SPLL is from 40MHz XTAL oscillator frequency multipliers, its default frequency is 480MHz */
153153
SOC_MOD_CLK_MPLL, /*!< MPLL is from 40MHz XTAL oscillator frequency multipliers */
154154
SOC_MOD_CLK_XTAL32K, /*!< XTAL32K_CLK comes from the external 32kHz crystal, passing a clock gating to the peripherals */
155155
SOC_MOD_CLK_RC_FAST, /*!< RC_FAST_CLK comes from the internal 20MHz rc oscillator, passing a clock gating to the peripherals */
@@ -720,7 +720,7 @@ typedef enum {
720720
//////////////////////////////////////////////CLOCK OUTPUT///////////////////////////////////////////////////////////
721721
typedef enum {
722722
CLKOUT_SIG_MPLL = 0, /*!< MPLL is from 40MHz XTAL oscillator frequency multipliers */
723-
CLKOUT_SIG_SPLL = 1, /*!< SPLL is from 40MHz XTAL oscillator frequency multipliers, it has a "fixed" frequency of 480MHz */
723+
CLKOUT_SIG_SPLL = 1, /*!< SPLL is from 40MHz XTAL oscillator frequency multipliers, its default frequency is 480MHz */
724724
CLKOUT_SIG_CPLL = 2, /*!< CPLL_CLK is the output of 40MHz crystal oscillator frequency multiplier, can be 320/360/400MHz */
725725
CLKOUT_SIG_XTAL = 3, /*!< External 40MHz crystal */
726726
CLKOUT_SIG_RC_FAST = 4, /*!< Internal 17.5MHz RC oscillator */

0 commit comments

Comments
 (0)