mirror of
				https://github.com/espressif/esp-idf.git
				synced 2025-11-04 06:11:06 +00:00 
			
		
		
		
	refactor(hal): use hal utils to calculate clock division
This commit is contained in:
		@@ -19,6 +19,7 @@
 | 
			
		||||
#include "soc/i2s_struct.h"
 | 
			
		||||
#include "soc/pcr_struct.h"
 | 
			
		||||
#include "hal/i2s_types.h"
 | 
			
		||||
#include "hal/hal_utils.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
@@ -30,22 +31,12 @@ extern "C" {
 | 
			
		||||
#define I2S_LL_TDM_CH_MASK             (0xffff)
 | 
			
		||||
#define I2S_LL_PDM_BCK_FACTOR          (64)
 | 
			
		||||
 | 
			
		||||
#define I2S_LL_MCLK_DIVIDER_BIT_WIDTH  (9)
 | 
			
		||||
#define I2S_LL_MCLK_DIVIDER_MAX        ((1 << I2S_LL_MCLK_DIVIDER_BIT_WIDTH) - 1)
 | 
			
		||||
#define I2S_LL_CLK_FRAC_DIV_N_MAX  256 // I2S_MCLK = I2S_SRC_CLK / (N + b/a), the N register is 8 bit-width
 | 
			
		||||
#define I2S_LL_CLK_FRAC_DIV_AB_MAX 512 // I2S_MCLK = I2S_SRC_CLK / (N + b/a), the a/b register is 9 bit-width
 | 
			
		||||
 | 
			
		||||
#define I2S_LL_PLL_F160M_CLK_FREQ      (160 * 1000000) // PLL_F160M_CLK: 160MHz
 | 
			
		||||
#define I2S_LL_DEFAULT_PLL_CLK_FREQ     I2S_LL_PLL_F160M_CLK_FREQ    // The default PLL clock frequency while using I2S_CLK_SRC_DEFAULT
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief I2S clock configuration structure
 | 
			
		||||
 * @note Fmclk = Fsclk /(integ+numer/denom)
 | 
			
		||||
 */
 | 
			
		||||
typedef struct {
 | 
			
		||||
    uint16_t integ;     // Integer part of I2S module clock divider
 | 
			
		||||
    uint16_t denom;     // Denominator part of I2S module clock divider
 | 
			
		||||
    uint16_t numer;     // Numerator part of I2S module clock divider
 | 
			
		||||
} i2s_ll_mclk_div_t;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief I2S module general init, enable I2S clock.
 | 
			
		||||
 *
 | 
			
		||||
@@ -313,7 +304,7 @@ static inline void i2s_ll_rx_set_raw_clk_div(i2s_dev_t *hw, uint32_t div_int, ui
 | 
			
		||||
 * @param hw Peripheral I2S hardware instance address.
 | 
			
		||||
 * @param mclk_div The mclk division coefficients
 | 
			
		||||
 */
 | 
			
		||||
static inline void i2s_ll_tx_set_mclk(i2s_dev_t *hw, const i2s_ll_mclk_div_t *mclk_div)
 | 
			
		||||
static inline void i2s_ll_tx_set_mclk(i2s_dev_t *hw, const hal_utils_clk_div_t *mclk_div)
 | 
			
		||||
{
 | 
			
		||||
    /* Workaround for inaccurate clock while switching from a relatively low sample rate to a high sample rate
 | 
			
		||||
     * Set to particular coefficients first then update to the target coefficients,
 | 
			
		||||
@@ -326,13 +317,13 @@ static inline void i2s_ll_tx_set_mclk(i2s_dev_t *hw, const i2s_ll_mclk_div_t *mc
 | 
			
		||||
    uint32_t div_z = 0;
 | 
			
		||||
    uint32_t div_yn1 = 0;
 | 
			
		||||
    /* If any of denominator and numerator is 0, set all the coefficients to 0 */
 | 
			
		||||
    if (mclk_div->denom && mclk_div->numer) {
 | 
			
		||||
        div_yn1 = mclk_div->numer * 2 > mclk_div->denom;
 | 
			
		||||
        div_z = div_yn1 ? mclk_div->denom - mclk_div->numer : mclk_div->numer;
 | 
			
		||||
        div_x = mclk_div->denom / div_z - 1;
 | 
			
		||||
        div_y = mclk_div->denom % div_z;
 | 
			
		||||
    if (mclk_div->denominator && mclk_div->numerator) {
 | 
			
		||||
        div_yn1 = mclk_div->numerator * 2 > mclk_div->denominator;
 | 
			
		||||
        div_z = div_yn1 ? mclk_div->denominator - mclk_div->numerator : mclk_div->numerator;
 | 
			
		||||
        div_x = mclk_div->denominator / div_z - 1;
 | 
			
		||||
        div_y = mclk_div->denominator % div_z;
 | 
			
		||||
    }
 | 
			
		||||
    i2s_ll_tx_set_raw_clk_div(hw, mclk_div->integ, div_x, div_y, div_z, div_yn1);
 | 
			
		||||
    i2s_ll_tx_set_raw_clk_div(hw, mclk_div->integer, div_x, div_y, div_z, div_yn1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -353,7 +344,7 @@ static inline void i2s_ll_rx_set_bck_div_num(i2s_dev_t *hw, uint32_t val)
 | 
			
		||||
 * @param hw Peripheral I2S hardware instance address.
 | 
			
		||||
 * @param mclk_div The mclk division coefficients
 | 
			
		||||
 */
 | 
			
		||||
static inline void i2s_ll_rx_set_mclk(i2s_dev_t *hw, const i2s_ll_mclk_div_t *mclk_div)
 | 
			
		||||
static inline void i2s_ll_rx_set_mclk(i2s_dev_t *hw, const hal_utils_clk_div_t *mclk_div)
 | 
			
		||||
{
 | 
			
		||||
    /* Workaround for inaccurate clock while switching from a relatively low sample rate to a high sample rate
 | 
			
		||||
     * Set to particular coefficients first then update to the target coefficients,
 | 
			
		||||
@@ -366,13 +357,13 @@ static inline void i2s_ll_rx_set_mclk(i2s_dev_t *hw, const i2s_ll_mclk_div_t *mc
 | 
			
		||||
    uint32_t div_z = 0;
 | 
			
		||||
    uint32_t div_yn1 = 0;
 | 
			
		||||
    /* If any of denominator and numerator is 0, set all the coefficients to 0 */
 | 
			
		||||
    if (mclk_div->denom && mclk_div->numer) {
 | 
			
		||||
        div_yn1 = mclk_div->numer * 2 > mclk_div->denom;
 | 
			
		||||
        div_z = div_yn1 ? mclk_div->denom - mclk_div->numer : mclk_div->numer;
 | 
			
		||||
        div_x = mclk_div->denom / div_z - 1;
 | 
			
		||||
        div_y = mclk_div->denom % div_z;
 | 
			
		||||
    if (mclk_div->denominator && mclk_div->numerator) {
 | 
			
		||||
        div_yn1 = mclk_div->numerator * 2 > mclk_div->denominator;
 | 
			
		||||
        div_z = div_yn1 ? mclk_div->denominator - mclk_div->numerator : mclk_div->numerator;
 | 
			
		||||
        div_x = mclk_div->denominator / div_z - 1;
 | 
			
		||||
        div_y = mclk_div->denominator % div_z;
 | 
			
		||||
    }
 | 
			
		||||
    i2s_ll_rx_set_raw_clk_div(hw, mclk_div->integ, div_x, div_y, div_z, div_yn1);
 | 
			
		||||
    i2s_ll_rx_set_raw_clk_div(hw, mclk_div->integer, div_x, div_y, div_z, div_yn1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user