mirror of
https://github.com/espressif/esp-idf.git
synced 2025-10-07 21:47:21 +00:00
feat(adc): support ADC continuous mode on ESP32P4
This commit is contained in:
@@ -10,24 +10,18 @@
|
||||
#include "esp_attr.h"
|
||||
|
||||
#include "soc/adc_periph.h"
|
||||
// #include "soc/ADC_struct.h"
|
||||
// #include "soc/ADC_reg.h"
|
||||
#include "soc/pmu_reg.h"
|
||||
#include "soc/adc_struct.h"
|
||||
#include "soc/clk_tree_defs.h"
|
||||
// #include "soc/pcr_struct.h"
|
||||
#include "soc/hp_sys_clkrst_struct.h"
|
||||
#include "soc/lpperi_struct.h"
|
||||
#include "soc/regi2c_saradc.h"
|
||||
#include "hal/misc.h"
|
||||
#include "hal/assert.h"
|
||||
#include "hal/adc_types.h"
|
||||
#include "hal/adc_types_private.h"
|
||||
#include "hal/regi2c_ctrl.h"
|
||||
#include "hal/sar_ctrl_ll.h"
|
||||
|
||||
#include "soc/regi2c_saradc.h"
|
||||
|
||||
#include "soc/hp_sys_clkrst_struct.h"
|
||||
#include "soc/adc_struct.h"
|
||||
#include "soc/lp_adc_struct.h"
|
||||
#include "soc/lpperi_struct.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -36,6 +30,10 @@ extern "C" {
|
||||
#define ADC_LL_EVENT_ADC1_ONESHOT_DONE BIT(31)
|
||||
#define ADC_LL_EVENT_ADC2_ONESHOT_DONE BIT(30)
|
||||
|
||||
#define LP_ADC_FORCE_XPD_SAR_FSM 0 // Use FSM to control power down
|
||||
#define LP_ADC_FORCE_XPD_SAR_PD 2 // Force power down
|
||||
#define LP_ADC_FORCE_XPD_SAR_PU 3 // Force power up
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
Oneshot
|
||||
---------------------------------------------------------------*/
|
||||
@@ -46,7 +44,20 @@ extern "C" {
|
||||
/*---------------------------------------------------------------
|
||||
DMA
|
||||
---------------------------------------------------------------*/
|
||||
#define ADC_LL_DIGI_DATA_INVERT_DEFAULT(PERIPH_NUM) (0)
|
||||
#define ADC_LL_FSM_RSTB_WAIT_DEFAULT (8)
|
||||
#define ADC_LL_FSM_START_WAIT_DEFAULT (5)
|
||||
#define ADC_LL_FSM_STANDBY_WAIT_DEFAULT (100)
|
||||
#define ADC_LL_SAMPLE_CYCLE_DEFAULT (2)
|
||||
#define ADC_LL_DIGI_SAR_CLK_DIV_DEFAULT (1)
|
||||
|
||||
#define ADC_LL_CLKM_DIV_NUM_DEFAULT 15
|
||||
#define ADC_LL_CLKM_DIV_B_DEFAULT 1
|
||||
#define ADC_LL_CLKM_DIV_A_DEFAULT 0
|
||||
#define ADC_LL_DEFAULT_CONV_LIMIT_EN 0
|
||||
#define ADC_LL_DEFAULT_CONV_LIMIT_NUM 10
|
||||
|
||||
#define ADC_LL_POWER_MANAGE_SUPPORTED 1 //ESP32P4 supported to manage power mode
|
||||
/*---------------------------------------------------------------
|
||||
PWDET (Power Detect)
|
||||
---------------------------------------------------------------*/
|
||||
@@ -56,8 +67,7 @@ typedef enum {
|
||||
ADC_LL_CTRL_RTC = 0, ///< For ADC1 and ADC2. Select RTC controller.
|
||||
ADC_LL_CTRL_ULP = 1, ///< For ADC1 and ADC2. Select ULP controller.
|
||||
ADC_LL_CTRL_DIG = 2, ///< For ADC1 and ADC2. Select DIG controller.
|
||||
ADC_LL_CTRL_PWDET = 3, ///< ???
|
||||
ADC_LL_CTRL_ARB = 4, ///< ???
|
||||
ADC_LL_CTRL_PWDET = 3, ///< For ADC2. Select PWDET controller.
|
||||
} adc_ll_controller_t;
|
||||
|
||||
typedef enum {
|
||||
@@ -94,28 +104,40 @@ typedef struct {
|
||||
};
|
||||
} __attribute__((packed)) adc_ll_digi_pattern_table_t;
|
||||
|
||||
/**
|
||||
* @brief Analyze whether the obtained raw data is correct.
|
||||
* ADC2 use arbiter by default. The arbitration result can be judged by the flag bit in the original data.
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
uint16_t data: 13; /*!<ADC real output data info. Resolution: 13 bit. */
|
||||
uint16_t reserved: 1; /*!<reserved */
|
||||
uint16_t flag: 2; /*!<ADC data flag info.
|
||||
If (flag == 0), The data is valid.
|
||||
If (flag > 0), The data is invalid. */
|
||||
};
|
||||
uint16_t val;
|
||||
};
|
||||
} adc_ll_rtc_output_data_t;
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
Digital controller setting
|
||||
---------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Set adc fsm interval parameter for digital controller. These values are fixed for same platforms.
|
||||
*
|
||||
* @param rst_wait cycles between DIG ADC controller reset ADC sensor and start ADC sensor.
|
||||
* @param start_wait Delay time after open xpd.
|
||||
* @param standby_wait Delay time to close xpd.
|
||||
*/
|
||||
static inline void adc_ll_digi_set_fsm_time(uint32_t rst_wait, uint32_t start_wait, uint32_t standby_wait)
|
||||
{
|
||||
// Internal FSM reset wait time
|
||||
HAL_FORCE_MODIFY_U32_REG_FIELD(ADC.fsm_wait, rstb_wait, rst_wait);
|
||||
// Internal FSM start wait time
|
||||
HAL_FORCE_MODIFY_U32_REG_FIELD(ADC.fsm_wait, xpd_wait, start_wait);
|
||||
// Internal FSM standby wait time
|
||||
HAL_FORCE_MODIFY_U32_REG_FIELD(ADC.fsm_wait, standby_wait, standby_wait);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set adc sample cycle for digital controller.
|
||||
*
|
||||
* @note Normally, please use default value.
|
||||
* @param sample_cycle Cycles between DIG ADC controller start ADC sensor and beginning to receive data from sensor.
|
||||
* Range: 2 ~ 0xFF.
|
||||
*/
|
||||
static inline void adc_ll_set_sample_cycle(uint32_t sample_cycle)
|
||||
{
|
||||
/* Peripheral reg i2c has powered up in rtc_init, write directly */
|
||||
REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_SAMPLE_CYCLE_ADDR, sample_cycle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set SAR ADC module clock division factor.
|
||||
* SAR ADC clock divided from digital controller clock.
|
||||
@@ -128,6 +150,49 @@ static inline void adc_ll_digi_set_clk_div(uint32_t div)
|
||||
HAL_FORCE_MODIFY_U32_REG_FIELD(ADC.ctrl_reg, sar_clk_div, div);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set adc max conversion number for digital controller.
|
||||
* If the number of ADC conversion is equal to the maximum, the conversion is stopped.
|
||||
*
|
||||
* @param meas_num Max conversion number. Range: 0 ~ 255.
|
||||
*/
|
||||
static inline void adc_ll_digi_set_convert_limit_num(uint32_t meas_num)
|
||||
{
|
||||
HAL_FORCE_MODIFY_U32_REG_FIELD(ADC.ctrl2, max_meas_num, meas_num);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable max conversion number detection for digital controller.
|
||||
* If the number of ADC conversion is equal to the maximum, the conversion is stopped.
|
||||
*
|
||||
* @param enable true: enable; false: disable
|
||||
*/
|
||||
static inline void adc_ll_digi_convert_limit_enable(bool enable)
|
||||
{
|
||||
ADC.ctrl2.meas_num_limit = enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set adc conversion mode for digital controller.
|
||||
*
|
||||
* @param mode Conversion mode select.
|
||||
*/
|
||||
static inline void adc_ll_digi_set_convert_mode(adc_ll_digi_convert_mode_t mode)
|
||||
{
|
||||
if (mode == ADC_LL_DIGI_CONV_ONLY_ADC1) {
|
||||
ADC.ctrl_reg.work_mode = 0;
|
||||
ADC.ctrl_reg.sar_sel = 0;
|
||||
} else if (mode == ADC_LL_DIGI_CONV_ONLY_ADC2) {
|
||||
ADC.ctrl_reg.work_mode = 0;
|
||||
ADC.ctrl_reg.sar_sel = 1;
|
||||
} else if (mode == ADC_LL_DIGI_CONV_BOTH_UNIT) {
|
||||
ADC.ctrl_reg.work_mode = 1;
|
||||
} else if (mode == ADC_LL_DIGI_CONV_ALTER_UNIT) {
|
||||
ADC.ctrl_reg.work_mode = 2;
|
||||
}
|
||||
ADC.ctrl_reg.data_sar_sel = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ADC digital controller clock division factor. The clock divided from `APLL` or `APB` clock.
|
||||
* Expression: controller_clk = (APLL or APB) / (div_num + div_a / div_b + 1).
|
||||
@@ -139,8 +204,8 @@ static inline void adc_ll_digi_set_clk_div(uint32_t div)
|
||||
static inline void adc_ll_digi_controller_clk_div(uint32_t div_num, uint32_t div_b, uint32_t div_a)
|
||||
{
|
||||
HAL_FORCE_MODIFY_U32_REG_FIELD(HP_SYS_CLKRST.peri_clk_ctrl23, reg_adc_clk_div_num, div_num);
|
||||
HP_SYS_CLKRST.root_clk_ctrl0.reg_cpu_clk_div_numerator = div_b;
|
||||
HP_SYS_CLKRST.root_clk_ctrl0.reg_cpu_clk_div_denominator = div_a;
|
||||
HP_SYS_CLKRST.peri_clk_ctrl23.reg_adc_clk_div_numerator = div_a;
|
||||
HP_SYS_CLKRST.peri_clk_ctrl23.reg_adc_clk_div_denominator = div_b;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,6 +232,235 @@ static inline void adc_ll_digi_clk_sel(adc_continuous_clk_src_t clk_src)
|
||||
ADC.ctrl_reg.sar_clk_gated = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable clock for ADC digital controller.
|
||||
*/
|
||||
static inline void adc_ll_digi_controller_clk_disable(void)
|
||||
{
|
||||
ADC.ctrl_reg.sar_clk_gated = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset adc digital controller filter.
|
||||
*
|
||||
* @param idx Filter index
|
||||
* @param adc_n ADC unit.
|
||||
*/
|
||||
static inline void adc_ll_digi_filter_reset(adc_digi_iir_filter_t idx, adc_unit_t adc_n)
|
||||
{
|
||||
(void)adc_n;
|
||||
ADC.filter_ctrl0.filter_reset = 1;
|
||||
ADC.filter_ctrl0.filter_reset = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set adc digital controller filter coeff.
|
||||
*
|
||||
* @param idx filter index
|
||||
* @param adc_n adc unit
|
||||
* @param channel adc channel
|
||||
* @param coeff filter coeff
|
||||
*/
|
||||
static inline void adc_ll_digi_filter_set_factor(adc_digi_iir_filter_t idx, adc_unit_t adc_n, adc_channel_t channel, adc_digi_iir_filter_coeff_t coeff)
|
||||
{
|
||||
uint32_t factor_reg_val = 0;
|
||||
switch (coeff) {
|
||||
case ADC_DIGI_IIR_FILTER_COEFF_2:
|
||||
factor_reg_val = 1;
|
||||
break;
|
||||
case ADC_DIGI_IIR_FILTER_COEFF_4:
|
||||
factor_reg_val = 2;
|
||||
break;
|
||||
case ADC_DIGI_IIR_FILTER_COEFF_8:
|
||||
factor_reg_val = 3;
|
||||
break;
|
||||
case ADC_DIGI_IIR_FILTER_COEFF_16:
|
||||
factor_reg_val = 4;
|
||||
break;
|
||||
case ADC_DIGI_IIR_FILTER_COEFF_64:
|
||||
factor_reg_val = 6;
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false);
|
||||
}
|
||||
|
||||
if (idx == ADC_DIGI_IIR_FILTER_0) {
|
||||
ADC.filter_ctrl0.filter_channel0 = ((adc_n + 1) << 3) | (channel & 0x7);
|
||||
ADC.filter_ctrl1.filter_factor0 = factor_reg_val;
|
||||
} else if (idx == ADC_DIGI_IIR_FILTER_1) {
|
||||
ADC.filter_ctrl0.filter_channel1 = ((adc_n + 1) << 3) | (channel & 0x7);
|
||||
ADC.filter_ctrl1.filter_factor1 = factor_reg_val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable adc digital controller filter.
|
||||
* Filtering the ADC data to obtain smooth data at higher sampling rates.
|
||||
*
|
||||
* @param idx filter index
|
||||
* @param adc_n ADC unit
|
||||
* @param enable Enable / Disable
|
||||
*/
|
||||
static inline void adc_ll_digi_filter_enable(adc_digi_iir_filter_t idx, adc_unit_t adc_n, bool enable)
|
||||
{
|
||||
(void)adc_n;
|
||||
if (!enable) {
|
||||
if (idx == ADC_DIGI_IIR_FILTER_0) {
|
||||
ADC.filter_ctrl0.filter_channel0 = 0xF;
|
||||
ADC.filter_ctrl1.filter_factor0 = 0;
|
||||
} else if (idx == ADC_DIGI_IIR_FILTER_1) {
|
||||
ADC.filter_ctrl0.filter_channel1 = 0xF;
|
||||
ADC.filter_ctrl1.filter_factor1 = 0;
|
||||
}
|
||||
}
|
||||
//nothing to do to enable, after adc_ll_digi_filter_set_factor, it's enabled.
|
||||
}
|
||||
|
||||
/**
|
||||
* Set pattern table length for digital controller.
|
||||
* The pattern table that defines the conversion rules for each SAR ADC. Each table has 16 items, in which channel selection,
|
||||
* resolution and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
|
||||
* pattern table one by one. For each controller the scan sequence has at most 16 different rules before repeating itself.
|
||||
*
|
||||
* @param adc_n ADC unit.
|
||||
* @param patt_len Items range: 1 ~ 16.
|
||||
*/
|
||||
static inline void adc_ll_digi_set_pattern_table_len(adc_unit_t adc_n, uint32_t patt_len)
|
||||
{
|
||||
if (adc_n == ADC_UNIT_1) {
|
||||
ADC.ctrl_reg.sar1_patt_len = patt_len - 1;
|
||||
} else { // adc_n == ADC_UNIT_2
|
||||
ADC.ctrl_reg.sar2_patt_len = patt_len - 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set pattern table for digital controller.
|
||||
* The pattern table that defines the conversion rules for each SAR ADC. Each table has 12 items, in which channel selection,
|
||||
* resolution and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
|
||||
* pattern table one by one. For each controller the scan sequence has at most 12 different rules before repeating itself.
|
||||
*
|
||||
* @param adc_n ADC unit.
|
||||
* @param pattern_index Items index. Range: 0 ~ 11.
|
||||
* @param pattern Stored conversion rules.
|
||||
*/
|
||||
static inline void adc_ll_digi_set_pattern_table(adc_unit_t adc_n, uint32_t pattern_index, adc_digi_pattern_config_t table)
|
||||
{
|
||||
uint32_t tab;
|
||||
uint8_t index = pattern_index / 4;
|
||||
uint8_t offset = (pattern_index % 4) * 6;
|
||||
adc_ll_digi_pattern_table_t pattern = {0};
|
||||
|
||||
pattern.val = (table.atten & 0x3) | ((table.channel & 0xF) << 2);
|
||||
if (table.unit == ADC_UNIT_1){
|
||||
tab = ADC.sar1_patt_tab[index].sar1_patt_tab; //Read old register value
|
||||
tab &= (~(0xFC0000 >> offset)); //Clear old data
|
||||
tab |= ((uint32_t)(pattern.val & 0x3F) << 18) >> offset; //Fill in the new data
|
||||
ADC.sar1_patt_tab[index].sar1_patt_tab = tab; //Write back
|
||||
} else {
|
||||
tab = ADC.sar2_patt_tab[index].sar2_patt_tab; //Read old register value
|
||||
tab &= (~(0xFC0000 >> offset)); //clear old data
|
||||
tab |= ((uint32_t)(pattern.val & 0x3F) << 18) >> offset; //Fill in the new data
|
||||
ADC.sar2_patt_tab[index].sar2_patt_tab = tab; //Write back
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the pattern table pointer, then take the measurement rule from table header in next measurement.
|
||||
*
|
||||
* @param adc_n ADC unit.
|
||||
*/
|
||||
static inline void adc_ll_digi_clear_pattern_table(adc_unit_t adc_n)
|
||||
{
|
||||
if (adc_n == ADC_UNIT_1) {
|
||||
ADC.ctrl_reg.sar1_patt_p_clear = 1;
|
||||
ADC.ctrl_reg.sar1_patt_p_clear = 0;
|
||||
} else { // adc_n == ADC_UNIT_2
|
||||
ADC.ctrl_reg.sar2_patt_p_clear = 1;
|
||||
ADC.ctrl_reg.sar2_patt_p_clear = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ADC Digital controller output data invert or not.
|
||||
*
|
||||
* @param adc_n ADC unit.
|
||||
* @param inv_en data invert or not.
|
||||
*/
|
||||
static inline void adc_ll_digi_output_invert(adc_unit_t adc_n, bool inv_en)
|
||||
{
|
||||
if (adc_n == ADC_UNIT_1) {
|
||||
ADC.ctrl2.sar1_inv = inv_en; // Enable / Disable ADC data invert
|
||||
} else { // adc_n == ADC_UNIT_2
|
||||
ADC.ctrl2.sar2_inv = inv_en; // Enable / Disable ADC data invert
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the interval clock cycle for the digital controller to trigger the measurement.
|
||||
* Expression: `trigger_meas_freq` = `controller_clk` / 2 / interval.
|
||||
*
|
||||
* @note The trigger interval should not be smaller than the sampling time of the SAR ADC.
|
||||
* @param cycle The clock cycle (trigger interval) of the measurement. Range: 30 ~ 4095.
|
||||
*/
|
||||
static inline void adc_ll_digi_set_trigger_interval(uint32_t cycle)
|
||||
{
|
||||
ADC.ctrl2.timer_target = cycle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set DMA eof num of adc digital controller.
|
||||
* If the number of measurements reaches `dma_eof_num`, then `dma_in_suc_eof` signal is generated.
|
||||
*
|
||||
* @param num eof num of DMA.
|
||||
*/
|
||||
static inline void adc_ll_digi_dma_set_eof_num(uint32_t num)
|
||||
{
|
||||
HAL_FORCE_MODIFY_U32_REG_FIELD(ADC.dma_conf, apb_adc_eof_num, num);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable output data to DMA from adc digital controller.
|
||||
*/
|
||||
static inline void adc_ll_digi_dma_enable(void)
|
||||
{
|
||||
ADC.dma_conf.apb_adc_trans = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable output data to DMA from adc digital controller.
|
||||
*/
|
||||
static inline void adc_ll_digi_dma_disable(void)
|
||||
{
|
||||
ADC.dma_conf.apb_adc_trans = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset adc digital controller.
|
||||
*/
|
||||
static inline void adc_ll_digi_reset(void)
|
||||
{
|
||||
ADC.dma_conf.apb_adc_reset_fsm = 1;
|
||||
ADC.dma_conf.apb_adc_reset_fsm = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable digital controller timer to trigger the measurement.
|
||||
*/
|
||||
static inline void adc_ll_digi_trigger_enable(void)
|
||||
{
|
||||
ADC.ctrl2.timer_sel = 1;
|
||||
ADC.ctrl2.timer_en = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable digital controller timer to trigger the measurement.
|
||||
*/
|
||||
static inline void adc_ll_digi_trigger_disable(void)
|
||||
{
|
||||
ADC.ctrl2.timer_en = 0;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
PWDET(Power detect) controller setting
|
||||
---------------------------------------------------------------*/
|
||||
@@ -206,6 +500,7 @@ static inline uint32_t adc_ll_pwdet_get_cct(void)
|
||||
static inline void adc_ll_enable_bus_clock(bool enable)
|
||||
{
|
||||
HP_SYS_CLKRST.soc_clk_ctrl2.reg_adc_apb_clk_en = enable;
|
||||
HP_SYS_CLKRST.peri_clk_ctrl23.reg_adc_clk_en = enable;
|
||||
}
|
||||
// HP_SYS_CLKRST.soc_clk_ctrl2 are shared registers, so this function must be used in an atomic way
|
||||
#define adc_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; adc_ll_enable_bus_clock(__VA_ARGS__)
|
||||
@@ -221,6 +516,84 @@ static inline void adc_ll_reset_register(void)
|
||||
// HP_SYS_CLKRST.hp_rst_en2 is a shared register, so this function must be used in an atomic way
|
||||
#define adc_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; adc_ll_reset_register(__VA_ARGS__)
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Set ADC digital controller power management.
|
||||
*
|
||||
* @param adc_n ADC unit.
|
||||
* @param manage Set ADC power status.
|
||||
*/
|
||||
static inline void adc_ll_digi_set_power_manage(adc_unit_t adc_n, adc_ll_power_t manage)
|
||||
{
|
||||
if (adc_n == ADC_UNIT_1) {
|
||||
/* Bit1 0:Fsm 1: SW mode
|
||||
Bit0 0:SW mode power down 1: SW mode power on */
|
||||
if (manage == ADC_LL_POWER_SW_ON) {
|
||||
ADC.ctrl_reg.sar_clk_gated = 1;
|
||||
ADC.ctrl_reg.xpd_sar1_force = LP_ADC_FORCE_XPD_SAR_PU;
|
||||
} else if (manage == ADC_LL_POWER_BY_FSM) {
|
||||
ADC.ctrl_reg.sar_clk_gated = 1;
|
||||
ADC.ctrl_reg.xpd_sar1_force = LP_ADC_FORCE_XPD_SAR_FSM;
|
||||
} else if (manage == ADC_LL_POWER_SW_OFF) {
|
||||
ADC.ctrl_reg.sar_clk_gated = 0;
|
||||
ADC.ctrl_reg.xpd_sar1_force = LP_ADC_FORCE_XPD_SAR_PD;
|
||||
}
|
||||
} else {
|
||||
/* Bit1 0:Fsm 1: SW mode
|
||||
Bit0 0:SW mode power down 1: SW mode power on */
|
||||
if (manage == ADC_LL_POWER_SW_ON) {
|
||||
ADC.ctrl_reg.sar_clk_gated = 1;
|
||||
ADC.ctrl_reg.xpd_sar2_force = LP_ADC_FORCE_XPD_SAR_PU;
|
||||
} else if (manage == ADC_LL_POWER_BY_FSM) {
|
||||
ADC.ctrl_reg.sar_clk_gated = 1;
|
||||
ADC.ctrl_reg.xpd_sar2_force = LP_ADC_FORCE_XPD_SAR_FSM;
|
||||
} else if (manage == ADC_LL_POWER_SW_OFF) {
|
||||
ADC.ctrl_reg.sar_clk_gated = 0;
|
||||
ADC.ctrl_reg.xpd_sar2_force = LP_ADC_FORCE_XPD_SAR_PD;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ADC module power management.
|
||||
*
|
||||
* @param adc_n ADC unit.
|
||||
* @param manage Set ADC power status.
|
||||
*/
|
||||
static inline void adc_ll_set_power_manage(adc_unit_t adc_n, adc_ll_power_t manage)
|
||||
{
|
||||
adc_ll_digi_set_power_manage(adc_n, manage);
|
||||
if (adc_n == ADC_UNIT_1) {
|
||||
/* Bit1 0:Fsm 1: SW mode
|
||||
Bit0 0:SW mode power down 1: SW mode power on */
|
||||
if (manage == ADC_LL_POWER_SW_ON) {
|
||||
LPPERI.clk_en.ck_en_lp_adc = 1;
|
||||
LP_ADC.force_wpd_sar.force_xpd_sar1 = LP_ADC_FORCE_XPD_SAR_PU;
|
||||
} else if (manage == ADC_LL_POWER_BY_FSM) {
|
||||
LPPERI.clk_en.ck_en_lp_adc = 1;
|
||||
LP_ADC.force_wpd_sar.force_xpd_sar1 = LP_ADC_FORCE_XPD_SAR_FSM;
|
||||
} else if (manage == ADC_LL_POWER_SW_OFF) {
|
||||
LP_ADC.force_wpd_sar.force_xpd_sar1 = LP_ADC_FORCE_XPD_SAR_PD;
|
||||
LPPERI.clk_en.ck_en_lp_adc = 0;
|
||||
}
|
||||
} else {
|
||||
/* Bit1 0:Fsm 1: SW mode
|
||||
Bit0 0:SW mode power down 1: SW mode power on */
|
||||
if (manage == ADC_LL_POWER_SW_ON) {
|
||||
LPPERI.clk_en.ck_en_lp_adc = 1;
|
||||
LP_ADC.force_wpd_sar.force_xpd_sar2 = LP_ADC_FORCE_XPD_SAR_PU;
|
||||
} else if (manage == ADC_LL_POWER_BY_FSM) {
|
||||
LPPERI.clk_en.ck_en_lp_adc = 1;
|
||||
LP_ADC.force_wpd_sar.force_xpd_sar2 = LP_ADC_FORCE_XPD_SAR_FSM;
|
||||
} else if (manage == ADC_LL_POWER_SW_OFF) {
|
||||
LP_ADC.force_wpd_sar.force_xpd_sar2 = LP_ADC_FORCE_XPD_SAR_PD;
|
||||
LPPERI.clk_en.ck_en_lp_adc = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set ADC module controller.
|
||||
* There are five SAR ADC controllers:
|
||||
@@ -277,39 +650,6 @@ static inline void adc_ll_set_controller(adc_unit_t adc_n, adc_ll_controller_t c
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ADC2 module controller priority in arbiter.
|
||||
* The arbiter is to improve the use efficiency of ADC2. After the control right is robbed by the high priority,
|
||||
* the low priority controller will read the invalid ADC data, and the validity of the data can be judged by the flag bit in the data.
|
||||
*
|
||||
* @note Only ADC2 support arbiter.
|
||||
* @note The arbiter's working clock is APB_CLK. When the APB_CLK clock drops below 8 MHz, the arbiter must be in shield mode.
|
||||
* @note Default priority: Wi-Fi(2) > RTC(1) > Digital(0);
|
||||
*
|
||||
* @param pri_rtc RTC controller priority. Range: 0 ~ 2.
|
||||
* @param pri_dig Digital controller priority. Range: 0 ~ 2.
|
||||
* @param pri_pwdet Wi-Fi controller priority. Range: 0 ~ 2.
|
||||
*/
|
||||
__attribute__((always_inline))
|
||||
static inline void adc_ll_set_arbiter_priority(uint8_t pri_rtc, uint8_t pri_dig, uint8_t pri_pwdet)
|
||||
{
|
||||
if (pri_rtc != pri_dig && pri_rtc != pri_pwdet && pri_dig != pri_pwdet) {
|
||||
ADC.arb_ctrl.arb_rtc_priority = pri_rtc;
|
||||
ADC.arb_ctrl.arb_apb_priority = pri_dig;
|
||||
ADC.arb_ctrl.arb_wifi_priority = pri_pwdet;
|
||||
}
|
||||
/* Should select highest priority controller. */
|
||||
if (pri_rtc > pri_dig) {
|
||||
ADC.arb_ctrl.arb_apb_force = 0;
|
||||
ADC.arb_ctrl.arb_rtc_force = 1;
|
||||
ADC.arb_ctrl.arb_wifi_force = 0;
|
||||
} else {
|
||||
ADC.arb_ctrl.arb_apb_force = 1;
|
||||
ADC.arb_ctrl.arb_rtc_force = 0;
|
||||
ADC.arb_ctrl.arb_wifi_force = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
Oneshot Read
|
||||
---------------------------------------------------------------*/
|
||||
@@ -436,26 +776,16 @@ static inline uint32_t adc_oneshot_ll_get_raw_result(adc_unit_t adc_n)
|
||||
|
||||
/**
|
||||
* Analyze whether the obtained raw data is correct.
|
||||
* ADC2 can use arbiter. The arbitration result can be judged by the flag bit in the original data.
|
||||
*
|
||||
* @param adc_n ADC unit.
|
||||
* @param raw ADC raw data input (convert value).
|
||||
* @return
|
||||
* - true: raw data is valid
|
||||
* - false: raw data is invalid
|
||||
*/
|
||||
static inline bool adc_oneshot_ll_raw_check_valid(adc_unit_t adc_n, uint32_t raw)
|
||||
{
|
||||
if (adc_n == ADC_UNIT_1) {
|
||||
return true;
|
||||
}
|
||||
adc_ll_rtc_output_data_t *temp = (adc_ll_rtc_output_data_t *)&raw;
|
||||
if (temp->flag == 0) {
|
||||
return true;
|
||||
} else {
|
||||
//Could be ADC_LL_RTC_CTRL_UNSELECTED, ADC_LL_RTC_CTRL_BREAK or ADC_LL_RTC_DATA_FAIL
|
||||
return false;
|
||||
}
|
||||
/* No arbiter, don't need check data */
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -481,8 +811,7 @@ static inline void adc_oneshot_ll_output_invert(adc_unit_t adc_n, bool inv_en)
|
||||
static inline void adc_oneshot_ll_enable(adc_unit_t adc_n)
|
||||
{
|
||||
(void)adc_n;
|
||||
HP_SYS_CLKRST.soc_clk_ctrl2.reg_adc_apb_clk_en = 1;
|
||||
HP_SYS_CLKRST.peri_clk_ctrl23.reg_adc_clk_en = 1;
|
||||
//For compatibility
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -490,8 +819,7 @@ static inline void adc_oneshot_ll_enable(adc_unit_t adc_n)
|
||||
*/
|
||||
static inline void adc_oneshot_ll_disable_all_unit(void)
|
||||
{
|
||||
HP_SYS_CLKRST.soc_clk_ctrl2.reg_adc_apb_clk_en = 0;
|
||||
HP_SYS_CLKRST.peri_clk_ctrl23.reg_adc_clk_en = 0;
|
||||
//For compatibility
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user