2023-12-25 02:32:55 -05:00
|
|
|
/*
|
2024-01-31 04:08:15 -05:00
|
|
|
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
2023-12-25 02:32:55 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2024-04-15 02:12:47 -04:00
|
|
|
#include "soc/clkout_channel.h"
|
|
|
|
#include "hal/assert.h"
|
2023-12-25 02:32:55 -05:00
|
|
|
#include "hal/clk_tree_hal.h"
|
|
|
|
#include "hal/clk_tree_ll.h"
|
2024-04-15 02:12:47 -04:00
|
|
|
#include "hal/gpio_ll.h"
|
2023-12-25 02:32:55 -05:00
|
|
|
#include "hal/log.h"
|
2024-06-07 09:29:44 -04:00
|
|
|
#include "sdkconfig.h"
|
2023-12-25 02:32:55 -05:00
|
|
|
|
|
|
|
uint32_t clk_hal_soc_root_get_freq_mhz(soc_cpu_clk_src_t cpu_clk_src)
|
|
|
|
{
|
|
|
|
switch (cpu_clk_src) {
|
|
|
|
case SOC_CPU_CLK_SRC_XTAL:
|
|
|
|
return clk_hal_xtal_get_freq_mhz();
|
2024-06-14 04:46:48 -04:00
|
|
|
case SOC_CPU_CLK_SRC_PLL_F160M:
|
|
|
|
return CLK_LL_PLL_160M_FREQ_MHZ;
|
|
|
|
case SOC_CPU_CLK_SRC_PLL_F240M:
|
|
|
|
return CLK_LL_PLL_240M_FREQ_MHZ;
|
2023-12-25 02:32:55 -05:00
|
|
|
case SOC_CPU_CLK_SRC_RC_FAST:
|
|
|
|
return SOC_CLK_RC_FAST_FREQ_APPROX / MHZ;
|
|
|
|
default:
|
|
|
|
// Unknown CPU_CLK mux input
|
|
|
|
HAL_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t clk_hal_cpu_get_freq_hz(void)
|
|
|
|
{
|
|
|
|
soc_cpu_clk_src_t source = clk_ll_cpu_get_src();
|
2024-06-14 04:46:48 -04:00
|
|
|
uint32_t divider = clk_ll_cpu_get_divider();
|
2023-12-25 02:32:55 -05:00
|
|
|
return clk_hal_soc_root_get_freq_mhz(source) * MHZ / divider;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t clk_hal_ahb_get_freq_hz(void)
|
|
|
|
{
|
|
|
|
soc_cpu_clk_src_t source = clk_ll_cpu_get_src();
|
2024-06-14 04:46:48 -04:00
|
|
|
uint32_t divider = clk_ll_ahb_get_divider();
|
2023-12-25 02:32:55 -05:00
|
|
|
return clk_hal_soc_root_get_freq_mhz(source) * MHZ / divider;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t clk_hal_apb_get_freq_hz(void)
|
|
|
|
{
|
|
|
|
return clk_hal_ahb_get_freq_hz() / clk_ll_apb_get_divider();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t clk_hal_lp_slow_get_freq_hz(void)
|
|
|
|
{
|
|
|
|
switch (clk_ll_rtc_slow_get_src()) {
|
2024-03-10 22:30:36 -04:00
|
|
|
case SOC_RTC_SLOW_CLK_SRC_RC_SLOW:
|
|
|
|
return SOC_CLK_RC_SLOW_FREQ_APPROX;
|
2023-12-25 02:32:55 -05:00
|
|
|
case SOC_RTC_SLOW_CLK_SRC_XTAL32K:
|
|
|
|
return SOC_CLK_XTAL32K_FREQ_APPROX;
|
|
|
|
case SOC_RTC_SLOW_CLK_SRC_OSC_SLOW:
|
|
|
|
return SOC_CLK_OSC_SLOW_FREQ_APPROX;
|
|
|
|
case SOC_RTC_SLOW_CLK_SRC_RC32K:
|
|
|
|
return SOC_CLK_RC32K_FREQ_APPROX;
|
|
|
|
default:
|
|
|
|
// Unknown RTC_SLOW_CLK mux input
|
|
|
|
HAL_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t clk_hal_xtal_get_freq_mhz(void)
|
|
|
|
{
|
2024-06-07 09:29:44 -04:00
|
|
|
uint32_t freq = clk_ll_xtal_get_freq_mhz();
|
|
|
|
HAL_ASSERT(freq == SOC_XTAL_FREQ_48M || freq == SOC_XTAL_FREQ_40M);
|
|
|
|
return freq;
|
2023-12-25 02:32:55 -05:00
|
|
|
}
|
2024-04-15 02:12:47 -04:00
|
|
|
|
2024-01-22 08:48:12 -05:00
|
|
|
void clk_hal_clock_output_setup(soc_clkout_sig_id_t clk_sig, clock_out_channel_t channel_id)
|
2024-04-15 02:12:47 -04:00
|
|
|
{
|
|
|
|
gpio_ll_set_pin_ctrl(clk_sig, CLKOUT_CHANNEL_MASK(channel_id), CLKOUT_CHANNEL_SHIFT(channel_id));
|
|
|
|
}
|
|
|
|
|
2024-01-22 08:48:12 -05:00
|
|
|
void clk_hal_clock_output_teardown(clock_out_channel_t channel_id)
|
2024-04-15 02:12:47 -04:00
|
|
|
{
|
|
|
|
gpio_ll_set_pin_ctrl(0, CLKOUT_CHANNEL_MASK(channel_id), CLKOUT_CHANNEL_SHIFT(channel_id));
|
|
|
|
}
|