@@ -67,6 +67,18 @@ typedef enum {
6767 ADC_LL_CTRL_DIG = 0 , ///< For ADC1. Select DIG controller.
6868} adc_ll_controller_t ;
6969
70+ typedef struct {
71+ union {
72+ struct {
73+ uint8_t atten : 2 ;
74+ uint8_t channel : 3 ;
75+ uint8_t unit : 1 ;
76+ uint8_t reserved : 2 ;
77+ };
78+ uint8_t val ;
79+ };
80+ } __attribute__((packed )) adc_ll_digi_pattern_table_t ;
81+
7082/*---------------------------------------------------------------
7183 Digital controller setting
7284---------------------------------------------------------------*/
@@ -114,6 +126,28 @@ static inline void adc_ll_digi_set_clk_div(uint32_t div)
114126 HAL_FORCE_MODIFY_U32_REG_FIELD (APB_SARADC .saradc_ctrl , saradc_saradc_sar_clk_div , div );
115127}
116128
129+ /**
130+ * Enable max conversion number detection for digital controller.
131+ * If the number of ADC conversion is equal to the maximum, the conversion is stopped.
132+ *
133+ * @note Only used for bootloader RNG.
134+ * @param enable true: enable; false: disable
135+ */
136+ static inline void adc_ll_digi_convert_limit_enable (bool enable )
137+ {
138+ APB_SARADC .saradc_ctrl2 .saradc_saradc_meas_num_limit = enable ;
139+ }
140+
141+ /**
142+ * Enable output data to DMA from adc digital controller.
143+ *
144+ * @note Only used for bootloader RNG.
145+ */
146+ static inline void adc_ll_digi_dma_enable (void )
147+ {
148+ APB_SARADC .saradc_dma_conf .saradc_apb_adc_trans = 1 ;
149+ }
150+
117151/**
118152 * Sets the number of cycles required for the conversion to complete and wait for the arbiter to stabilize.
119153 *
@@ -140,6 +174,39 @@ static inline void adc_ll_digi_output_invert(adc_unit_t adc_n, bool inv_en)
140174 }
141175}
142176
177+ /**
178+ * Set the interval clock cycle for the digital controller to trigger the measurement.
179+ * Expression: `trigger_meas_freq` = `controller_clk` / 2 / interval.
180+ *
181+ * @note The trigger interval should not be smaller than the sampling time of the SAR ADC.
182+ * @note Only used for bootloader RNG.
183+ * @param cycle The clock cycle (trigger interval) of the measurement. Range: 30 ~ 4095.
184+ */
185+ static inline void adc_ll_digi_set_trigger_interval (uint32_t cycle )
186+ {
187+ APB_SARADC .saradc_ctrl2 .saradc_saradc_timer_target = cycle ;
188+ }
189+
190+ /**
191+ * Enable digital controller timer to trigger the measurement.
192+ *
193+ * @note Only used for bootloader RNG.
194+ */
195+ static inline void adc_ll_digi_trigger_enable (void )
196+ {
197+ APB_SARADC .saradc_ctrl2 .saradc_saradc_timer_en = 1 ;
198+ }
199+
200+ /**
201+ * Disable digital controller timer to trigger the measurement.
202+ *
203+ * @note Only used for bootloader RNG.
204+ */
205+ static inline void adc_ll_digi_trigger_disable (void )
206+ {
207+ APB_SARADC .saradc_ctrl2 .saradc_saradc_timer_en = 0 ;
208+ }
209+
143210/**
144211 * Set ADC digital controller clock division factor. The clock divided from `APLL` or `APB` clock.
145212 * Expression: controller_clk = (APLL or APB) / (div_num + div_a / div_b + 1).
@@ -294,28 +361,28 @@ static inline uint32_t adc_ll_pwdet_get_cct(void)
294361 * @brief Enable the ADC clock
295362 * @param enable true to enable, false to disable
296363 */
297- static inline void adc_ll_enable_bus_clock (bool enable )
364+ static inline void _adc_ll_enable_bus_clock (bool enable )
298365{
299366 SYSTEM .perip_clk_en0 .apb_saradc_clk_en = enable ;
300367}
301368// SYSTEM.perip_clk_en0 is a shared register, so this function must be used in an atomic way
302369#define adc_ll_enable_bus_clock (...) do { \
303370 (void)__DECLARE_RCC_ATOMIC_ENV; \
304- adc_ll_enable_bus_clock (__VA_ARGS__); \
371+ _adc_ll_enable_bus_clock (__VA_ARGS__); \
305372 } while(0)
306373
307374/**
308375 * @brief Reset ADC module
309376 */
310- static inline void adc_ll_reset_register (void )
377+ static inline void _adc_ll_reset_register (void )
311378{
312379 SYSTEM .perip_rst_en0 .apb_saradc_rst = 1 ;
313380 SYSTEM .perip_rst_en0 .apb_saradc_rst = 0 ;
314381}
315382// SYSTEM.perip_rst_en0 is a shared register, so this function must be used in an atomic way
316383#define adc_ll_reset_register (...) do { \
317384 (void)__DECLARE_RCC_ATOMIC_ENV; \
318- adc_ll_reset_register (__VA_ARGS__); \
385+ _adc_ll_reset_register (__VA_ARGS__); \
319386 } while(0)
320387
321388/**
@@ -346,6 +413,70 @@ static inline void adc_ll_set_controller(adc_unit_t adc_n, adc_ll_controller_t c
346413 //Not used on ESP32-C2
347414}
348415
416+ /**
417+ * Set pattern table length for digital controller.
418+ * The pattern table that defines the conversion rules for each SAR ADC. Each table has 8 items, in which channel selection,
419+ * resolution and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
420+ * pattern table one by one. For each controller the scan sequence has at most 8 different rules before repeating itself.
421+ *
422+ * @param adc_n ADC unit.
423+ * @param patt_len Items range: 1 ~ 8.
424+ *
425+ * @note Only used for bootloader RNG.
426+ */
427+ static inline void adc_ll_digi_set_pattern_table_len (adc_unit_t adc_n , uint32_t patt_len )
428+ {
429+ (void )adc_n ;
430+ APB_SARADC .saradc_ctrl .saradc_saradc_sar_patt_len = patt_len - 1 ;
431+ }
432+
433+ /**
434+ * Reset pattern table to default value
435+ *
436+ * @note Only used for bootloader RNG.
437+ */
438+ static inline void adc_ll_digi_reset_pattern_table (void )
439+ {
440+ APB_SARADC .saradc_sar_patt_tab1 .saradc_saradc_sar_patt_tab1 = 0xffffff ;
441+ APB_SARADC .saradc_sar_patt_tab2 .saradc_saradc_sar_patt_tab2 = 0xffffff ;
442+ }
443+
444+ /**
445+ * Set pattern table for digital controller.
446+ * The pattern table that defines the conversion rules for each SAR ADC. Each table has 8 items, in which channel selection,
447+ * resolution and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
448+ * pattern table one by one. For each controller the scan sequence has at most 8 different rules before repeating itself.
449+ *
450+ * @param adc_n ADC unit.
451+ * @param pattern_index Items index. Range: 0 ~ 7.
452+ * @param table Stored conversion rules.
453+ *
454+ * @note Only used for bootloader RNG.
455+ */
456+ static inline void adc_ll_digi_set_pattern_table (adc_unit_t adc_n , uint32_t pattern_index , adc_digi_pattern_config_t table )
457+ {
458+ uint32_t tab ;
459+ uint8_t index = pattern_index / 4 ;
460+ uint8_t offset = (pattern_index % 4 ) * 6 ;
461+ adc_ll_digi_pattern_table_t pattern = {0 };
462+
463+ (void )adc_n ;
464+
465+ pattern .val = (table .atten & 0x3 ) | ((table .channel & 0x7 ) << 2 ) | ((table .unit & 0x1 ) << 5 );
466+
467+ if (index == 0 ) {
468+ tab = APB_SARADC .saradc_sar_patt_tab1 .saradc_saradc_sar_patt_tab1 ; // Read old register value
469+ tab &= (~(0xFC0000 >> offset )); // Clear old data
470+ tab |= ((uint32_t )(pattern .val & 0x3F ) << 18 ) >> offset ; // Fill in the new data
471+ APB_SARADC .saradc_sar_patt_tab1 .saradc_saradc_sar_patt_tab1 = tab ; // Write back
472+ } else {
473+ tab = APB_SARADC .saradc_sar_patt_tab2 .saradc_saradc_sar_patt_tab2 ; // Read old register value
474+ tab &= (~(0xFC0000 >> offset )); // Clear old data
475+ tab |= ((uint32_t )(pattern .val & 0x3F ) << 18 ) >> offset ; // Fill in the new data
476+ APB_SARADC .saradc_sar_patt_tab2 .saradc_saradc_sar_patt_tab2 = tab ; // Write back
477+ }
478+ }
479+
349480/* ADC calibration code. */
350481/**
351482 * @brief Set common calibration configuration. Should be shared with other parts (PWDET).
@@ -405,6 +536,69 @@ static inline void adc_ll_set_calibration_param(adc_unit_t adc_n, uint32_t param
405536 REGI2C_WRITE_MASK (I2C_SAR_ADC , ADC_SAR1_INITIAL_CODE_LOW_ADDR , lsb );
406537}
407538
539+ /**
540+ * Set the SAR DTEST param
541+ *
542+ * @param param DTEST value
543+ */
544+ __attribute__((always_inline ))
545+ static inline void adc_ll_set_dtest_param (uint32_t param )
546+ {
547+ REGI2C_WRITE_MASK (I2C_SAR_ADC , ADC_SARADC_DTEST_RTC_ADDR , param );
548+ }
549+
550+ /**
551+ * Set the SAR ENT param
552+ *
553+ * @param param ENT value
554+ */
555+ __attribute__((always_inline ))
556+ static inline void adc_ll_set_ent_param (uint32_t param )
557+ {
558+ REGI2C_WRITE_MASK (I2C_SAR_ADC , ADC_SARADC_ENT_TSENS_ADDR , param );
559+ }
560+
561+ /**
562+ * Enable/disable the calibration voltage reference for ADC unit.
563+ *
564+ * @param adc_n ADC index number.
565+ * @param en true to enable, false to disable
566+ */
567+ __attribute__((always_inline ))
568+ static inline void adc_ll_enable_calibration_ref (adc_unit_t adc_n , bool en )
569+ {
570+ if (adc_n == ADC_UNIT_1 ) {
571+ REGI2C_WRITE_MASK (I2C_SAR_ADC , ADC_SARADC1_ENCAL_REF_ADDR , en );
572+ } else {
573+ REGI2C_WRITE_MASK (I2C_SAR_ADC , ADC_SARADC2_ENCAL_REF_ADDR , en );
574+ }
575+ }
576+
577+ /**
578+ * Init regi2c SARADC registers
579+ */
580+ __attribute__((always_inline ))
581+ static inline void adc_ll_regi2c_init (void )
582+ {
583+ adc_ll_set_dtest_param (0 );
584+ adc_ll_set_ent_param (0 );
585+ // Config ADC circuit (Analog part) with I2C(HOST ID 0x69) and chose internal voltage as sampling source
586+ adc_ll_enable_calibration_ref (ADC_UNIT_1 , true);
587+ adc_ll_enable_calibration_ref (ADC_UNIT_2 , true);
588+ }
589+
590+ /**
591+ * Deinit regi2c SARADC registers
592+ */
593+ __attribute__((always_inline ))
594+ static inline void adc_ll_regi2c_adc_deinit (void )
595+ {
596+ adc_ll_set_dtest_param (0 );
597+ adc_ll_set_ent_param (0 );
598+ adc_ll_enable_calibration_ref (ADC_UNIT_1 , false);
599+ adc_ll_enable_calibration_ref (ADC_UNIT_2 , false);
600+ }
601+
408602/*---------------------------------------------------------------
409603 Oneshot Read
410604---------------------------------------------------------------*/
0 commit comments