|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 | #include "sdkconfig.h" |
7 | 7 | #include "bootloader_random.h" |
8 | | -#include "soc/soc.h" |
9 | | -#include "soc/pcr_reg.h" |
10 | | -#include "soc/apb_saradc_reg.h" |
11 | | -#include "soc/pmu_reg.h" |
12 | | -#include "hal/regi2c_ctrl.h" |
13 | | -#include "soc/regi2c_saradc.h" |
14 | | -#include "esp_log.h" |
15 | | - |
16 | | -static const uint32_t SAR2_CHANNEL = 9; |
17 | | -static const uint32_t SAR1_CHANNEL = 7; |
18 | | -static const uint32_t PATTERN_BIT_WIDTH = 6; |
19 | | -static const uint32_t SAR1_ATTEN = 3; |
20 | | -static const uint32_t SAR2_ATTEN = 3; |
| 8 | +#include "hal/regi2c_ctrl_ll.h" |
| 9 | +#include "hal/adc_ll.h" |
| 10 | +#include "hal/adc_types.h" |
21 | 11 |
|
22 | 12 | void bootloader_random_enable(void) |
23 | 13 | { |
24 | | - // pull SAR ADC out of reset |
25 | | - REG_SET_BIT(PCR_SARADC_CONF_REG, PCR_SARADC_RST_EN); |
26 | | - REG_CLR_BIT(PCR_SARADC_CONF_REG, PCR_SARADC_RST_EN); |
27 | | - |
28 | | - // enable SAR ADC APB clock |
29 | | - REG_SET_BIT(PCR_SARADC_CONF_REG, PCR_SARADC_REG_CLK_EN); |
30 | | - |
31 | | - // pull APB register out of reset |
32 | | - REG_SET_BIT(PCR_SARADC_CONF_REG, PCR_SARADC_REG_RST_EN); |
33 | | - REG_CLR_BIT(PCR_SARADC_CONF_REG, PCR_SARADC_REG_RST_EN); |
34 | | - |
35 | | - // enable ADC_CTRL_CLK (SAR ADC function clock) |
36 | | - REG_SET_BIT(PCR_SARADC_CLKM_CONF_REG, PCR_SARADC_CLKM_EN); |
37 | | - |
38 | | - // select XTAL clock (40 MHz) source for ADC_CTRL_CLK |
39 | | - REG_SET_FIELD(PCR_SARADC_CLKM_CONF_REG, PCR_SARADC_CLKM_SEL, 0); |
40 | | - |
41 | | - // set the clock divider for ADC_CTRL_CLK to default value (in case it has been changed) |
42 | | - REG_SET_FIELD(PCR_SARADC_CLKM_CONF_REG, PCR_SARADC_CLKM_DIV_NUM, 0); |
| 14 | + adc_ll_reset_register(); |
| 15 | + adc_ll_enable_bus_clock(true); |
| 16 | + adc_ll_enable_func_clock(true); |
| 17 | + adc_ll_digi_clk_sel(ADC_DIGI_CLK_SRC_XTAL); |
| 18 | + adc_ll_digi_controller_clk_div(0, 0, 0); |
43 | 19 |
|
44 | 20 | // some ADC sensor registers are in power group PERIF_I2C and need to be enabled via PMU |
45 | | - SET_PERI_REG_MASK(PMU_RF_PWC_REG, PMU_PERIF_I2C_RSTB); |
46 | | - SET_PERI_REG_MASK(PMU_RF_PWC_REG, PMU_XPD_PERIF_I2C); |
47 | | - |
| 21 | + regi2c_ctrl_ll_i2c_reset_set(); |
| 22 | + regi2c_ctrl_ll_i2c_periph_enable(); |
48 | 23 | // enable analog i2c master clock for RNG runtime |
49 | 24 | ANALOG_CLOCK_ENABLE(); |
50 | 25 |
|
51 | | - // Config ADC circuit (Analog part) with I2C(HOST ID 0x69) and chose internal voltage as sampling source |
52 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_DTEST_RTC_ADDR , 0); |
53 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_RTC_ADDR , 1); |
54 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC1_ENCAL_REF_ADDR, 1); |
55 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC2_ENCAL_REF_ADDR, 1); |
56 | | - |
57 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_HIGH_ADDR, 0x08); |
58 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_LOW_ADDR, 0x66); |
59 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_HIGH_ADDR, 0x08); |
60 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_LOW_ADDR, 0x66); |
61 | | - |
62 | | - // create patterns and set them in pattern table |
63 | | - uint32_t pattern_one = (SAR2_CHANNEL << 2) | SAR2_ATTEN; // we want channel 9 with max attenuation |
64 | | - uint32_t pattern_two = (SAR1_CHANNEL << 2) | SAR1_ATTEN; // we want channel 7 with max attenuation |
65 | | - uint32_t pattern_table = 0 | (pattern_two << 3 * PATTERN_BIT_WIDTH) | pattern_one << 2 * PATTERN_BIT_WIDTH; |
66 | | - REG_WRITE(SARADC_SAR_PATT_TAB1_REG, pattern_table); |
67 | | - |
68 | | - // set pattern length to 2 (APB_SARADC_SAR_PATT_LEN counts from 0) |
69 | | - REG_SET_FIELD(SARADC_CTRL_REG, SARADC_SAR_PATT_LEN, 1); |
70 | | - |
71 | | - // Same as in C3 |
72 | | - REG_SET_FIELD(SARADC_CTRL_REG, SARADC_SAR_CLK_DIV, 15); |
73 | | - |
74 | | - // set timer expiry (timer is ADC_CTRL_CLK) |
75 | | - REG_SET_FIELD(SARADC_CTRL2_REG, SARADC_TIMER_TARGET, 200); |
76 | | - |
77 | | - // enable timer |
78 | | - REG_SET_BIT(SARADC_CTRL2_REG, SARADC_TIMER_EN); |
| 26 | + adc_ll_regi2c_adc_prepare(); |
| 27 | + adc_ll_set_calibration_param(ADC_UNIT_1, 0x866); |
| 28 | + adc_ll_set_calibration_param(ADC_UNIT_2, 0x866); |
| 29 | + |
| 30 | + adc_digi_pattern_config_t pattern_config = {}; |
| 31 | + pattern_config.unit = ADC_UNIT_1; |
| 32 | + pattern_config.atten = ADC_ATTEN_DB_12; |
| 33 | + pattern_config.channel = ADC_CHANNEL_7; |
| 34 | + adc_ll_digi_set_pattern_table(ADC_UNIT_1, 0, pattern_config); |
| 35 | + pattern_config.unit = ADC_UNIT_2; |
| 36 | + pattern_config.atten = ADC_ATTEN_DB_12; |
| 37 | + pattern_config.channel = ADC_CHANNEL_1; |
| 38 | + adc_ll_digi_set_pattern_table(ADC_UNIT_2, 1, pattern_config); |
| 39 | + adc_ll_digi_set_pattern_table_len(ADC_UNIT_1, 2); |
| 40 | + |
| 41 | + adc_ll_digi_set_clk_div(15); |
| 42 | + adc_ll_digi_set_trigger_interval(200); |
| 43 | + adc_ll_digi_trigger_enable(); |
79 | 44 | } |
80 | 45 |
|
81 | 46 | void bootloader_random_disable(void) |
82 | 47 | { |
83 | | - // disable timer |
84 | | - REG_CLR_BIT(SARADC_CTRL2_REG, SARADC_TIMER_EN); |
85 | | - |
86 | | - // Write reset value of this register |
87 | | - REG_WRITE(SARADC_SAR_PATT_TAB1_REG, 0xFFFFFF); |
88 | | - |
89 | | - // Revert ADC I2C configuration and initial voltage source setting |
90 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_HIGH_ADDR, 0x60); |
91 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_LOW_ADDR, 0x0); |
92 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_HIGH_ADDR, 0x60); |
93 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_LOW_ADDR, 0x0); |
94 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_DTEST_RTC_ADDR, 0); |
95 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_RTC_ADDR, 0); |
96 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC1_ENCAL_REF_ADDR, 0); |
97 | | - REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC2_ENCAL_REF_ADDR, 0); |
| 48 | + adc_ll_digi_trigger_disable(); |
| 49 | + adc_ll_digi_reset_pattern_table(); |
| 50 | + adc_ll_set_calibration_param(ADC_UNIT_1, 0x0); |
| 51 | + adc_ll_set_calibration_param(ADC_UNIT_2, 0x0); |
| 52 | + adc_ll_regi2c_adc_reset(); |
98 | 53 |
|
99 | 54 | // disable analog i2c master clock |
100 | 55 | ANALOG_CLOCK_DISABLE(); |
101 | | - |
102 | | - // disable ADC_CTRL_CLK (SAR ADC function clock) |
103 | | - REG_WRITE(PCR_SARADC_CLKM_CONF_REG, 0x00404000); |
104 | | - |
105 | | - // Set PCR_SARADC_CONF_REG to initial state |
106 | | - REG_WRITE(PCR_SARADC_CONF_REG, 0x5); |
| 56 | + adc_ll_digi_controller_clk_div(4, 0, 0); |
| 57 | + adc_ll_digi_clk_sel(ADC_DIGI_CLK_SRC_XTAL); |
107 | 58 | } |
0 commit comments