14 #include "adc_ctrl_regs.h"
16 static_assert(ADC_CTRL_PARAM_NUM_ADC_CHANNEL == 2,
17 "Expected two ADC Controller channels.");
18 static_assert(ADC_CTRL_PARAM_NUM_ADC_FILTER == 8,
19 "Expected eight ADC Controller filters.");
20 static_assert(ADC_CTRL_ADC_CHN0_FILTER_CTL_MIN_V_FIELD_WIDTH == 10,
21 "Expected channel-0 filter min-voltage field to be 10 bits.");
22 static_assert(ADC_CTRL_ADC_CHN1_FILTER_CTL_MIN_V_FIELD_WIDTH == 10,
23 "Expected channel-1 filter min-voltage field to be 10 bits.");
24 static_assert(ADC_CTRL_ADC_CHN0_FILTER_CTL_MAX_V_FIELD_WIDTH == 10,
25 "Expected channel-0 filter max-voltage field to be 10 bits.");
26 static_assert(ADC_CTRL_ADC_CHN1_FILTER_CTL_MAX_V_FIELD_WIDTH == 10,
27 "Expected channel-1 filter max-voltage field to be 10 bits.");
31 if (adc_ctrl == NULL ||
39 uint32_t en_ctrl_reg = ADC_CTRL_ADC_EN_CTL_REG_RESVAL;
40 uint32_t pd_ctrl_reg = ADC_CTRL_ADC_PD_CTL_REG_RESVAL;
41 uint32_t lp_sample_ctrl_reg = ADC_CTRL_ADC_LP_SAMPLE_CTL_REG_RESVAL;
42 uint32_t np_sample_ctrl_reg = ADC_CTRL_ADC_SAMPLE_CTL_REG_RESVAL;
44 switch (config.
mode) {
47 ADC_CTRL_ADC_PD_CTL_LP_MODE_BIT,
true);
49 pd_ctrl_reg, ADC_CTRL_ADC_PD_CTL_WAKEUP_TIME_FIELD,
52 lp_sample_ctrl_reg, ADC_CTRL_ADC_LP_SAMPLE_CTL_LP_SAMPLE_CNT_FIELD,
57 np_sample_ctrl_reg, ADC_CTRL_ADC_SAMPLE_CTL_NP_SAMPLE_CNT_FIELD,
62 en_ctrl_reg, ADC_CTRL_ADC_EN_CTL_ONESHOT_MODE_BIT,
true);
73 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_EN_CTL_REG_OFFSET,
75 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_PD_CTL_REG_OFFSET,
78 ADC_CTRL_ADC_LP_SAMPLE_CTL_REG_OFFSET,
80 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_SAMPLE_CTL_REG_OFFSET,
90 if (adc_ctrl == NULL || config.
min_voltage > 1023 ||
95 ptrdiff_t filter_ctrl_reg_offset;
101 #define DIF_ADC_CTRL_CHANNEL0_FILTER_CONFIG_CASE_(filter_) \
102 case kDifAdcCtrlFilter##filter_: \
103 filter_ctrl_reg_offset = \
104 ADC_CTRL_ADC_CHN0_FILTER_CTL_##filter_##_REG_OFFSET; \
105 min_voltage_field = \
106 ADC_CTRL_ADC_CHN0_FILTER_CTL_##filter_##_MIN_V_##filter_##_FIELD; \
107 max_voltage_field = \
108 ADC_CTRL_ADC_CHN0_FILTER_CTL_##filter_##_MAX_V_##filter_##_FIELD; \
110 ADC_CTRL_ADC_CHN0_FILTER_CTL_##filter_##_COND_##filter_##_BIT; \
111 enable_bit = ADC_CTRL_ADC_CHN0_FILTER_CTL_##filter_##_EN_##filter_##_BIT; \
114 #define DIF_ADC_CTRL_CHANNEL1_FILTER_CONFIG_CASE_(filter_) \
115 case kDifAdcCtrlFilter##filter_: \
116 filter_ctrl_reg_offset = \
117 ADC_CTRL_ADC_CHN1_FILTER_CTL_##filter_##_REG_OFFSET; \
118 min_voltage_field = \
119 ADC_CTRL_ADC_CHN1_FILTER_CTL_##filter_##_MIN_V_##filter_##_FIELD; \
120 max_voltage_field = \
121 ADC_CTRL_ADC_CHN1_FILTER_CTL_##filter_##_MAX_V_##filter_##_FIELD; \
123 ADC_CTRL_ADC_CHN1_FILTER_CTL_##filter_##_COND_##filter_##_BIT; \
124 enable_bit = ADC_CTRL_ADC_CHN1_FILTER_CTL_##filter_##_EN_##filter_##_BIT; \
128 case kDifAdcCtrlChannel0:
135 case kDifAdcCtrlChannel1:
146 #undef DIF_ADC_CTRL_CHANNEL0_FILTER_CONFIG_CASE_
147 #undef DIF_ADC_CTRL_CHANNEL1_FILTER_CONFIG_CASE_
150 uint32_t filter_ctrl_reg =
158 mmio_region_write32(adc_ctrl->
base_addr, filter_ctrl_reg_offset,
162 uint32_t wakeup_ctrl_reg = mmio_region_read32(
163 adc_ctrl->
base_addr, ADC_CTRL_ADC_WAKEUP_CTL_REG_OFFSET);
166 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_WAKEUP_CTL_REG_OFFSET,
170 uint32_t intr_ctrl_reg =
171 mmio_region_read32(adc_ctrl->
base_addr, ADC_CTRL_ADC_INTR_CTL_REG_OFFSET);
174 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_INTR_CTL_REG_OFFSET,
186 uint32_t en_ctrl_reg =
187 mmio_region_read32(adc_ctrl->
base_addr, ADC_CTRL_ADC_EN_CTL_REG_OFFSET);
191 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_EN_CTL_REG_OFFSET,
199 if (adc_ctrl == NULL || is_enabled == NULL) {
203 uint32_t en_ctrl_reg =
204 mmio_region_read32(adc_ctrl->
base_addr, ADC_CTRL_ADC_EN_CTL_REG_OFFSET);
213 ptrdiff_t *filter_ctrl_reg_offset) {
214 #define DIF_ADC_CTRL_CHANNEL0_FILTER_CTRL_REG_CASE_(filter_) \
215 case kDifAdcCtrlFilter##filter_: \
216 *filter_ctrl_reg_offset = \
217 ADC_CTRL_ADC_CHN0_FILTER_CTL_##filter_##_REG_OFFSET; \
220 #define DIF_ADC_CTRL_CHANNEL1_FILTER_CTRL_REG_CASE_(filter_) \
221 case kDifAdcCtrlFilter##filter_: \
222 *filter_ctrl_reg_offset = \
223 ADC_CTRL_ADC_CHN1_FILTER_CTL_##filter_##_REG_OFFSET; \
227 case kDifAdcCtrlChannel0:
234 case kDifAdcCtrlChannel1:
245 #undef DIF_ADC_CTRL_CHANNEL0_FILTER_CTRL_REG_CASE_
246 #undef DIF_ADC_CTRL_CHANNEL1_FILTER_CTRL_REG_CASE_
254 #define DIF_ADC_CTRL_CHANNEL0_FILTER_ENABLE_CASE_(filter_) \
255 case kDifAdcCtrlFilter##filter_: \
256 *enable_bit = ADC_CTRL_ADC_CHN0_FILTER_CTL_##filter_##_EN_##filter_##_BIT; \
259 #define DIF_ADC_CTRL_CHANNEL1_FILTER_ENABLE_CASE_(filter_) \
260 case kDifAdcCtrlFilter##filter_: \
261 *enable_bit = ADC_CTRL_ADC_CHN1_FILTER_CTL_##filter_##_EN_##filter_##_BIT; \
265 case kDifAdcCtrlChannel0:
272 case kDifAdcCtrlChannel1:
283 #undef DIF_ADC_CTRL_CHANNEL0_FILTER_ENABLE_CASE_
284 #undef DIF_ADC_CTRL_CHANNEL1_FILTER_ENABLE_CASE_
297 ptrdiff_t filter_ctrl_reg_offset;
299 if (!get_filter_offset(channel, filter, &filter_ctrl_reg_offset)) {
302 if (!get_filter_enable_bit(channel, filter, &enable_bit)) {
306 uint32_t filter_ctrl_reg =
307 mmio_region_read32(adc_ctrl->
base_addr, filter_ctrl_reg_offset);
310 mmio_region_write32(adc_ctrl->
base_addr, filter_ctrl_reg_offset,
320 if (adc_ctrl == NULL || is_enabled == NULL) {
324 ptrdiff_t filter_ctrl_reg_offset;
326 if (!get_filter_offset(channel, filter, &filter_ctrl_reg_offset)) {
329 if (!get_filter_enable_bit(channel, filter, &enable_bit)) {
333 uint32_t filter_ctrl_reg =
334 mmio_region_read32(adc_ctrl->
base_addr, filter_ctrl_reg_offset);
344 if (adc_ctrl == NULL || value == NULL) {
350 #define DIF_ADC_CTRL_CHANNEL_TRIG_VALUE_CASE_(channel_) \
351 case kDifAdcCtrlChannel##channel_: \
352 value_reg = mmio_region_read32( \
353 adc_ctrl->base_addr, ADC_CTRL_ADC_CHN_VAL_##channel_##_REG_OFFSET); \
354 *value = (uint16_t)bitfield_field32_read( \
356 ADC_CTRL_ADC_CHN_VAL_##channel_##_ADC_CHN_VALUE_INTR_##channel_##_FIELD); \
365 #undef DIF_ADC_CTRL_CHANNEL_TRIG_VALUE_CASE_
373 if (adc_ctrl == NULL || value == NULL) {
379 #define DIF_ADC_CTRL_CHANNEL_LATEST_VALUE_CASE_(channel_) \
380 case kDifAdcCtrlChannel##channel_: \
381 value_reg = mmio_region_read32( \
382 adc_ctrl->base_addr, ADC_CTRL_ADC_CHN_VAL_##channel_##_REG_OFFSET); \
383 *value = (uint16_t)bitfield_field32_read( \
385 ADC_CTRL_ADC_CHN_VAL_##channel_##_ADC_CHN_VALUE_##channel_##_FIELD); \
394 #undef DIF_ADC_CTRL_CHANNEL_LATEST_VALUE_CASE_
400 if (adc_ctrl == NULL) {
404 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_FSM_RST_REG_OFFSET, 1);
405 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_FSM_RST_REG_OFFSET, 0);
412 if (adc_ctrl == NULL || causes == NULL) {
416 *causes = mmio_region_read32(adc_ctrl->
base_addr,
417 ADC_CTRL_ADC_INTR_STATUS_REG_OFFSET);
424 if (adc_ctrl == NULL ||
status == NULL) {
429 ADC_CTRL_FILTER_STATUS_REG_OFFSET);
441 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_FILTER_STATUS_REG_OFFSET,
445 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_INTR_STATUS_REG_OFFSET,
454 if (adc_ctrl == NULL || filter >= (ADC_CTRL_PARAM_NUM_ADC_FILTER + 1) ||
459 uint32_t wakeup_ctrl_reg = mmio_region_read32(
460 adc_ctrl->
base_addr, ADC_CTRL_ADC_WAKEUP_CTL_REG_OFFSET);
463 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_WAKEUP_CTL_REG_OFFSET,
472 if (adc_ctrl == NULL || filter >= (ADC_CTRL_PARAM_NUM_ADC_FILTER + 1) ||
473 is_enabled == NULL) {
477 uint32_t wakeup_ctrl_reg = mmio_region_read32(
478 adc_ctrl->
base_addr, ADC_CTRL_ADC_WAKEUP_CTL_REG_OFFSET);
493 uint32_t enabled_causes =
494 mmio_region_read32(adc_ctrl->
base_addr, ADC_CTRL_ADC_INTR_CTL_REG_OFFSET);
496 enabled_causes &= ~causes;
498 enabled_causes |= causes;
500 mmio_region_write32(adc_ctrl->
base_addr, ADC_CTRL_ADC_INTR_CTL_REG_OFFSET,
507 uint32_t *enabled_causes) {
508 if (adc_ctrl == NULL || enabled_causes == NULL) {
513 mmio_region_read32(adc_ctrl->
base_addr, ADC_CTRL_ADC_INTR_CTL_REG_OFFSET);
519 uint32_t aon_freq_hz) {
520 if (adc_ctrl == NULL) {