27 #include "in_compile.h" 28 #include "./hal/hal_power.h" 30 #define ADC_INTR_SAMPLE_DONE 0x00000001UL 31 #define ADC_INTR_FIFO_FULL 0x00000002UL 32 #define ADC_INTR_FIFO_EMPTY 0x00000004UL 33 #define ADC_INTR_FIFO_ALMOST_FULL 0x00000008UL 35 #define ADC_CH_VBAT ADC_CH14 36 #define ADC_CH_TEMP ADC_CH15 37 #define ADC_EFUSE_SAMPLES (16) //c0: 32, e0: 16 39 #define ADC_VBAT_SAMPLES (16) 40 #define ADC_TEMP_SAMPLES (16) 41 #define ADC_CHX_SAMPLES (16) 43 #define ADC_SAMPLE_RETRY (1000) 66 ADC_ERR_INVALID_PARAM = -1,
83 void (*cb)(
void *arg);
87 typedef struct adc_ch_timing {
89 uint8_t sample_dly_en;
90 uint8_t capture_clk_half_duration;
95 static INLINE uint32_t adc_intr_status(
void)
97 return RD_WORD(SADCCTRL_REG_INTR_STATUS);
99 static INLINE uint32_t adc_intr_mask_status(
void)
101 return RD_WORD(SADCCTRL_REG_INTR_MASK_STATUS);
103 static INLINE
void adc_intr_clear(uint32_t reg)
105 WR_WORD(SADCCTRL_REG_INTR_CLEAR, reg);
107 static INLINE
void adc_intr_set(uint32_t reg)
109 WR_WORD(SADCCTRL_REG_INTR_SET, reg);
111 static INLINE
void adc_intr_mask_clear(uint32_t reg)
113 WR_WORD(SADCCTRL_REG_INTR_MASK_CLEAR, reg);
115 static INLINE
void adc_intr_mask_set(uint32_t reg)
117 WR_WORD(SADCCTRL_REG_INTR_MASK_SET, reg);
121 static INLINE
void adc_enable(
void)
123 uint32_t reg = RD_WORD(SADCCTRL_REG_MISC_CTRL);
124 reg &= ~(SADCCTRL_REG_MISC_CTRL_CTL_START|SADCCTRL_REG_MISC_CTRL_CTL_END|SADCCTRL_REG_MISC_CTRL_CTL_RST_INTRCTRL);
125 reg |= SADCCTRL_REG_MISC_CTRL_CTL_ENABLE;
126 WR_WORD(SADCCTRL_REG_MISC_CTRL, reg);
128 static INLINE
void adc_disable(
void)
130 uint32_t reg = RD_WORD(SADCCTRL_REG_MISC_CTRL);
131 reg &= ~(SADCCTRL_REG_MISC_CTRL_CTL_START|SADCCTRL_REG_MISC_CTRL_CTL_END|SADCCTRL_REG_MISC_CTRL_CTL_RST_INTRCTRL);
132 reg &= ~SADCCTRL_REG_MISC_CTRL_CTL_ENABLE;
133 WR_WORD(SADCCTRL_REG_MISC_CTRL, reg);
135 static INLINE
void adc_start(
void)
137 uint32_t reg = RD_WORD(SADCCTRL_REG_MISC_CTRL);
138 reg &= ~(SADCCTRL_REG_MISC_CTRL_CTL_START|SADCCTRL_REG_MISC_CTRL_CTL_END|SADCCTRL_REG_MISC_CTRL_CTL_RST_INTRCTRL);
139 reg |= SADCCTRL_REG_MISC_CTRL_CTL_START;
140 WR_WORD(SADCCTRL_REG_MISC_CTRL, reg);
142 static INLINE
void adc_stop(
void)
144 uint32_t reg = RD_WORD(SADCCTRL_REG_MISC_CTRL);
145 reg &= ~(SADCCTRL_REG_MISC_CTRL_CTL_START|SADCCTRL_REG_MISC_CTRL_CTL_END|SADCCTRL_REG_MISC_CTRL_CTL_RST_INTRCTRL);
147 reg |= SADCCTRL_REG_MISC_CTRL_CTL_END;
148 WR_WORD(SADCCTRL_REG_MISC_CTRL, reg);
150 static INLINE
void adc_reset_interrupt(
void)
152 uint32_t reg = RD_WORD(SADCCTRL_REG_MISC_CTRL);
153 reg &= ~(SADCCTRL_REG_MISC_CTRL_CTL_START|SADCCTRL_REG_MISC_CTRL_CTL_END|SADCCTRL_REG_MISC_CTRL_CTL_RST_INTRCTRL);
155 reg |= SADCCTRL_REG_MISC_CTRL_CTL_RST_INTRCTRL;
156 WR_WORD(SADCCTRL_REG_MISC_CTRL, reg);
164 static INLINE
void adc_use_ana_data_valid(uint8_t val)
166 uint32_t reg = RD_WORD(SADCCTRL_REG_MISC_CTRL);
167 reg &= ~(SADCCTRL_REG_MISC_CTRL_CTL_START|SADCCTRL_REG_MISC_CTRL_CTL_END|SADCCTRL_REG_MISC_CTRL_CTL_RST_INTRCTRL);
169 reg &= ~SADCCTRL_REG_MISC_CTRL_CTL_USE_ANA_DATA_VALID;
171 reg |= SADCCTRL_REG_MISC_CTRL_CTL_USE_ANA_DATA_VALID;
172 WR_WORD(SADCCTRL_REG_MISC_CTRL, reg);
175 static INLINE
void adc_en_always_on(uint8_t en)
177 uint32_t reg = RD_WORD(SADCCTRL_REG_MISC_CTRL);
179 reg &= ~SADCCTRL_REG_MISC_CTRL_CTL_ENABLE_ALWAYS_ON;
181 reg |= SADCCTRL_REG_MISC_CTRL_CTL_ENABLE_ALWAYS_ON;
182 WR_WORD(SADCCTRL_REG_MISC_CTRL, reg);
185 static INLINE
void adc_num_sample(uint16_t val)
187 uint32_t reg = RD_WORD(SADCCTRL_REG_MISC_CTRL);
188 reg &= ~(SADCCTRL_REG_MISC_CTRL_CTL_START|SADCCTRL_REG_MISC_CTRL_CTL_END|SADCCTRL_REG_MISC_CTRL_CTL_RST_INTRCTRL);
189 reg &= ~SADCCTRL_REG_MISC_CTRL_CTL_NUM_SAMPLES;
191 WR_WORD(SADCCTRL_REG_MISC_CTRL, reg);
195 static INLINE
void adc_set_ch_en(uint16_t val)
197 uint32_t reg = RD_WORD(SADCCTRL_REG_CHANNEL_EN_CTRL);
198 reg &= ~SADCCTRL_REG_CHANNEL_EN_CTRL_CTL_CH_EN;
200 WR_WORD(SADCCTRL_REG_CHANNEL_EN_CTRL, reg);
202 static INLINE uint32_t adc_get_ch_en(
void)
204 return RD_WORD(SADCCTRL_REG_CHANNEL_EN_CTRL) & SADCCTRL_REG_CHANNEL_EN_CTRL_CTL_CH_EN;
207 static INLINE
void adc_sample_grp_intv(uint8_t val)
209 uint32_t reg = RD_WORD(SADCCTRL_REG_CHANNEL_EN_CTRL);
210 reg &= ~SADCCTRL_REG_CHANNEL_EN_CTRL_CTL_SAMPLE_GRP_INTERVAL;
211 reg |= val << SADCCTRL_REG_CHANNEL_EN_CTRL_CTL_SAMPLE_GRP_INTERVAL_SHIFT;
212 WR_WORD(SADCCTRL_REG_CHANNEL_EN_CTRL, reg);
214 static INLINE
void adc_disable_grp_intv(uint8_t disable)
216 uint32_t reg = RD_WORD(SADCCTRL_REG_CHANNEL_EN_CTRL);
218 reg &= ~SADCCTRL_REG_CHANNEL_EN_CTRL_CTL_DISABLE_BETWEEN_GRP;
220 reg |= SADCCTRL_REG_CHANNEL_EN_CTRL_CTL_DISABLE_BETWEEN_GRP;
221 WR_WORD(SADCCTRL_REG_CHANNEL_EN_CTRL, reg);
224 static INLINE
void adc_en2start_delay(uint8_t val)
226 uint32_t reg = RD_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL);
227 reg &= ~SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_EN2START_DELAY;
228 reg |= val & SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_EN2START_DELAY_MASK;
229 WR_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL, reg);
231 static INLINE
void adc_ch2start_delay(uint8_t val)
233 uint32_t reg = RD_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL);
234 reg &= ~SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_CH2START_DLY_M1;
235 reg |= (val& SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_CH2START_DLY_M1_MASK) << SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_CH2START_DLY_M1_SHIFT;
236 WR_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL, reg);
239 static INLINE
void adc_conv_clk_half_duration(uint8_t val) {
240 uint32_t reg = RD_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL);
241 reg &= ~SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_CONV_CLK_HLF_DUR;
242 reg |= (val& SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_CONV_CLK_HLF_DUR_MASK) << SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_CONV_CLK_HLF_DUR_SHIFT;
243 WR_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL, reg);
245 static INLINE
void adc_start_to_capture_offset(uint8_t val)
247 uint32_t reg = RD_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL);
248 reg &= ~SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_START_TO_CLK_OFFSET;
249 reg |= (val & SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_START_TO_CLK_OFFSET_MASK) << SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_START_TO_CLK_OFFSET_SHIFT;
250 WR_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL, reg);
252 static INLINE
void adc_ignore_num_clk_before_ready(uint8_t n)
254 uint32_t reg = RD_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL);
255 reg &= ~SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_IGNORE_RDY_NUM_CLK;
256 reg |= (n & SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_IGNORE_RDY_NUM_CLK_MASK) << SADCCTRL_REG_SAMPLE_TIMING_CTRL_CTL_IGNORE_RDY_NUM_CLK_SHIFT;
257 WR_WORD(SADCCTRL_REG_SAMPLE_TIMING_CTRL, reg);
260 static INLINE
void adc_fifo_size(uint16_t reg)
262 WR_WORD(SADCCTRL_REG_FIFO_CTRL, reg);
264 static INLINE
void adc_fifo_almost_full(uint16_t reg)
266 WR_WORD(SADCCTRL_REG_FIFO_ALMOST_FULL_CTRL, reg);
269 static INLINE uint8_t adc_cur_ch()
271 return RD_WORD(SADCCTRL_REG_STATUS)&SADCCTRL_REG_STATUS_STS_CURR_CH;
273 static INLINE uint16_t adc_fifo_cnt()
275 return (RD_WORD(SADCCTRL_REG_STATUS)&SADCCTRL_REG_STATUS_STS_FIFO_CNT)>>4;
277 static INLINE uint8_t adc_fifo_data_ready()
279 return (RD_WORD(SADCCTRL_REG_STATUS)&SADCCTRL_REG_STATUS_STS_FIFO_RDATA_RDY) >> 31u;
282 static INLINE uint16_t adc_status2_curr_grp_cnt_get(
void) {
283 return RD_WORD(SADCCTRL_REG_STATUS2)&SADCCTRL_REG_STATUS2_STS_CURR_GRP_CNT_MASK;
286 static INLINE uint16_t adc_fifo_data()
288 return RD_WORD(SADCCTRL_REG_FIFO_VAL);
291 static INLINE
void adc_force_enable()
293 uint32_t reg = RD_WORD(SADCCTRL_REG_FORCE_CTRL);
294 reg |= SADCCTRL_REG_FORCE_CTRL_CTL_FORCE_EN;
295 WR_WORD(SADCCTRL_REG_FORCE_CTRL, reg);
298 static INLINE
void adc_force_disable()
300 uint32_t reg = RD_WORD(SADCCTRL_REG_FORCE_CTRL);
301 reg &= ~SADCCTRL_REG_FORCE_CTRL_CTL_FORCE_EN;
302 WR_WORD(SADCCTRL_REG_FORCE_CTRL, reg);
305 static INLINE
void adc_force_ch(uint8_t val)
307 uint32_t reg = RD_WORD(SADCCTRL_REG_FORCE_CTRL);
308 reg &= ~SADCCTRL_REG_FORCE_CTRL_CTL_FORCE_CH;
309 reg |= (val & SADCCTRL_REG_FORCE_CTRL_CTL_FORCE_CH_MASK) << SADCCTRL_REG_FORCE_CTRL_CTL_FORCE_CH_SHIFT;
310 WR_WORD(SADCCTRL_REG_FORCE_CTRL, reg);
313 static INLINE uint8_t adc_sts_curr_adc_data_valid(
void)
315 return (RD_WORD(SADCCTRL_REG_FORCE_CTRL) & SADCCTRL_REG_FORCE_CTRL_STS_CURR_ADC_DATA_VALID)>>31u;
318 static INLINE
void adc_force_start(uint8_t val)
320 uint32_t reg = RD_WORD(SADCCTRL_REG_FORCE_CTRL_START);
322 reg &= ~SADCCTRL_REG_FORCE_CTRL_START_CTL_FORCE_START;
324 reg |= SADCCTRL_REG_FORCE_CTRL_START_CTL_FORCE_START;
325 WR_WORD(SADCCTRL_REG_FORCE_CTRL_START, reg);
328 static INLINE uint16_t adc_cur_adc_val()
330 return RD_WORD(SADCCTRL_REG_CURR_ADC_VAL) & SADCCTRL_REG_CURR_ADC_VAL_STS_CUR_ADC_VAL_MASK;
333 static INLINE uint16_t adc_force_adc_val()
335 return (RD_WORD(SADCCTRL_REG_CURR_ADC_VAL) & SADCCTRL_REG_CURR_ADC_VAL_STS_FORCE_ADC_VAL) >> SADCCTRL_REG_CURR_ADC_VAL_STS_FORCE_ADC_VAL_SHIFT;
339 #define ADC_REF_SEL_MASK 0x000000E0UL 351 static INLINE
void adc_ref_sel(
int val)
353 uint32_t reg = RD_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3);
358 WR_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3, reg);
360 static INLINE
void adc_ref_code(uint32_t val)
362 uint32_t reg = RD_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3);
364 reg |= (val&0x1F)<<19;
365 WR_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3, reg);
367 #define ADC_CH_SEL_MASK 0x10UL 368 static INLINE
void adc_ch_sel(uint8_t en, uint32_t ch)
370 uint32_t reg = RD_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3);
378 WR_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3, reg);
381 static INLINE
void adc_clk_edge_set(uint8_t val)
383 uint32_t reg = RD_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3);
389 WR_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3, reg);
392 static INLINE
void adc_offset_cal_en(
int en)
394 uint32_t reg = RD_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3);
400 WR_WORD(SADCCTRL_REG_SENSOR_ADC_REG1TO3, reg);
403 static INLINE
void adc_num_cap_clk(uint32_t addr, uint32_t num)
405 uint32_t reg = RD_WORD(addr);
406 reg &= ~SADCCTRL_REG_CH0_CTL_NUM_CAP_CLK;
407 reg |= (num&SADCCTRL_REG_CH0_CTL_NUM_CAP_CLK_MASK)<<SADCCTRL_REG_CH0_CTL_NUM_CAP_CLK_SHIFT;
411 static INLINE
void adc_cap_clk_hlf_dur(uint32_t addr, uint32_t num)
413 uint32_t reg = RD_WORD(addr);
414 reg &= ~SADCCTRL_REG_CH0_CTL_CAP_CLK_HLF_DUR;
415 reg |= (num&SADCCTRL_REG_CH0_CTL_CAP_CLK_HLF_DUR_MASK)<<SADCCTRL_REG_CH0_CTL_CAP_CLK_HLF_DUR_SHIFT;
int hal_adc_auto_mode_disable_ch(int ch)
Disable channel in auto mode.
internal 1.5v
Definition: hal_adc.h:77
int hal_adc_measure_temp(uint16_t sample_num, float *temp)
Read temperature, this function is more accurate than hal_adc_force_mode_start or hal_adc_auto_mode_s...
adc_ch
Definition: hal_adc.h:45
VBAT.
Definition: hal_adc.h:60
GPIO_2_9.
Definition: hal_adc.h:47
GPIO_2_6.
Definition: hal_adc.h:50
float hal_adc_temp_sample_convert(uint16_t adc_val)
convert temperature sample(channel 15) value to temperature, unit is centigrade
GPIO_2_0.
Definition: hal_adc.h:56
GPIO_3_1.
Definition: hal_adc.h:57
adc_vref
Definition: hal_adc.h:75
GPIO_3_0.
Definition: hal_adc.h:58
ADC_CH_IN.
Definition: hal_adc.h:46
int hal_adc_open(adc_init_t *init)
Open ADC device.
int hal_adc_measure_analog_ch(int ch, float vref, float *vc)
read analog channel voltage.
int hal_adc_auto_mode_cap_clk(int ch, uint32_t cap_clk)
Set capture clock for auto mode.
int hal_adc_auto_mode_start(uint16_t sample_num)
Start auto mode.
temperature sensor
Definition: hal_adc.h:61
GPIO_2_2.
Definition: hal_adc.h:54
GPIO_2_8.
Definition: hal_adc.h:48
GPIO_2_7.
Definition: hal_adc.h:49
GPIO_2_1.
Definition: hal_adc.h:55
int hal_adc_force_mode_enable_ch(int ch)
Enable channel in force mode.
GPIO_2_4.
Definition: hal_adc.h:52
int hal_adc_auto_mode_config(uint8_t grp_intv)
Config auto mode.
float hal_adc_sample_convert(uint16_t adc_val)
convert adc sample value to voltage, unit is mV, vref must be 1.0v
float hal_adc_vbat_sample_convert(uint16_t adc_val)
convert vbat(channel 14) sample value to voltage, unit is mV
int hal_adc_force_mode_cap_clk(uint32_t cap_clk)
Set capture clock for force mode.
void hal_adc_close(void)
Close ADC device.
int hal_adc_force_mode_disable_ch(int ch)
Disable channel in force mode.
PMU internal testpoint.
Definition: hal_adc.h:59
int hal_adc_set_vref(int vref)
Set ADC reference. Default is internal 1.0v.
GPIO_2_3.
Definition: hal_adc.h:53
int hal_adc_auto_mode_enable_ch(int ch, uint16_t *buf, uint16_t buf_sz)
Enable channel in auto mode.
int hal_adc_force_mode_start(int ch, uint16_t *buff, uint16_t sample_num)
Start force mode.
internal 1.0v
Definition: hal_adc.h:76
GPIO_2_5.
Definition: hal_adc.h:51