3030
3131#include "py/mphal.h"
3232#include "adc.h"
33- #include "driver/adc .h"
33+ #include "esp_adc/adc_oneshot .h"
3434
3535#define ADCBLOCK1 (&madcblock_obj[0])
3636#define ADCBLOCK2 (&madcblock_obj[1])
@@ -126,20 +126,8 @@ static const machine_adc_obj_t madc_obj[] = {
126126 #endif
127127};
128128
129- // These values are initialised to 0, which means the corresponding ADC channel is not initialised.
130- // The madc_atten_get/madc_atten_set functions store (atten+1) here so that the uninitialised state
131- // can be distinguished from the initialised state.
132129static uint8_t madc_obj_atten [MP_ARRAY_SIZE (madc_obj )];
133130
134- static inline adc_atten_t madc_atten_get (const machine_adc_obj_t * self ) {
135- uint8_t value = madc_obj_atten [self - & madc_obj [0 ]];
136- return value == 0 ? ADC_ATTEN_MAX : value - 1 ;
137- }
138-
139- static inline void madc_atten_set (const machine_adc_obj_t * self , adc_atten_t atten ) {
140- madc_obj_atten [self - & madc_obj [0 ]] = atten + 1 ;
141- }
142-
143131const machine_adc_obj_t * madc_search_helper (machine_adc_block_obj_t * block , adc_channel_t channel_id , gpio_num_t gpio_id ) {
144132 for (int i = 0 ; i < MP_ARRAY_SIZE (madc_obj ); i ++ ) {
145133 const machine_adc_obj_t * adc = & madc_obj [i ];
@@ -152,22 +140,7 @@ const machine_adc_obj_t *madc_search_helper(machine_adc_block_obj_t *block, adc_
152140
153141static void mp_machine_adc_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
154142 const machine_adc_obj_t * self = MP_OBJ_TO_PTR (self_in );
155- mp_printf (print , "ADC(Pin(%u), atten=%u)" , self -> gpio_id , madc_atten_get (self ));
156- }
157-
158- static void madc_atten_helper (const machine_adc_obj_t * self , mp_int_t atten ) {
159- esp_err_t err = ESP_FAIL ;
160- if (self -> block -> unit_id == ADC_UNIT_1 ) {
161- err = adc1_config_channel_atten (self -> channel_id , atten );
162- } else {
163- #if SOC_ADC_PERIPH_NUM >= 2
164- err = adc2_config_channel_atten (self -> channel_id , atten );
165- #endif
166- }
167- if (err != ESP_OK ) {
168- mp_raise_ValueError (MP_ERROR_TEXT ("invalid atten" ));
169- }
170- madc_atten_set (self , atten );
143+ mp_printf (print , "ADC(Pin(%u), atten=%u)" , self -> gpio_id , mp_machine_adc_atten_get_helper (self ));
171144}
172145
173146void madc_init_helper (const machine_adc_obj_t * self , size_t n_pos_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
@@ -182,18 +155,32 @@ void madc_init_helper(const machine_adc_obj_t *self, size_t n_pos_args, const mp
182155 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
183156 mp_arg_parse_all (n_pos_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
184157
185- mp_int_t atten = args [ARG_atten ].u_int ;
186- if (atten != -1 ) {
187- madc_atten_helper (self , atten );
188- } else if (madc_atten_get (self ) == ADC_ATTEN_MAX ) {
189- madc_atten_helper (self , ADC_ATTEN_DB_0 );
158+
159+ if (!self -> block -> handle ) {
160+ adc_oneshot_unit_init_cfg_t init_config = {
161+ .unit_id = self -> block -> unit_id
162+ };
163+ check_esp_err (adc_oneshot_new_unit (& init_config , & self -> block -> handle ));
190164 }
165+
166+ mp_int_t atten = args [ARG_atten ].u_int ;
167+ mp_machine_adc_atten_set_helper (self , atten != -1 ? atten : ADC_ATTEN_MAX );
168+ mp_machine_adc_block_width_set_helper (self -> block , ADC_WIDTH_MAX );
169+ apply_self_adc_channel_atten (self , mp_machine_adc_atten_get_helper (self ));
170+
191171}
192172
193173static void mp_machine_adc_init_helper (machine_adc_obj_t * self , size_t n_pos_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
194174 madc_init_helper (self , n_pos_args , pos_args , kw_args );
195175}
196176
177+ static void mp_machine_adc_deinit (machine_adc_obj_t * self ) {
178+ if (self -> block -> handle ) {
179+ check_esp_err (adc_oneshot_del_unit (self -> block -> handle ));
180+ self -> block -> handle = NULL ;
181+ }
182+ }
183+
197184static mp_obj_t mp_machine_adc_make_new (const mp_obj_type_t * type , size_t n_pos_args , size_t n_kw_args , const mp_obj_t * args ) {
198185 mp_arg_check_num (n_pos_args , n_kw_args , 1 , MP_OBJ_FUN_ARGS_MAX , true);
199186 gpio_num_t gpio_id = machine_pin_get_id (args [0 ]);
@@ -202,10 +189,6 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_pos_
202189 mp_raise_ValueError (MP_ERROR_TEXT ("invalid pin" ));
203190 }
204191
205- if (self -> block -> width == -1 ) {
206- madcblock_bits_helper (self -> block , self -> block -> bits );
207- }
208-
209192 mp_map_t kw_args ;
210193 mp_map_init_fixed_table (& kw_args , n_kw_args , args + n_pos_args );
211194 madc_init_helper (self , n_pos_args - 1 , args + 1 , & kw_args );
@@ -225,20 +208,46 @@ static mp_int_t mp_machine_adc_read(machine_adc_obj_t *self) {
225208static mp_int_t mp_machine_adc_read_u16 (machine_adc_obj_t * self ) {
226209 mp_uint_t raw = madcblock_read_helper (self -> block , self -> channel_id );
227210 // Scale raw reading to 16 bit value using a Taylor expansion (for 8 <= bits <= 16)
228- mp_int_t bits = self -> block -> bits ;
211+ mp_int_t bits = mp_machine_adc_width_get_helper ( self ) ;
229212 mp_uint_t u16 = raw << (16 - bits ) | raw >> (2 * bits - 16 );
230213 return u16 ;
231214}
232215
233216static mp_int_t mp_machine_adc_read_uv (machine_adc_obj_t * self ) {
234- adc_atten_t atten = madc_atten_get (self );
235- return madcblock_read_uv_helper (self -> block , self -> channel_id , atten );
217+ return madcblock_read_uv_helper (self -> block , self -> channel_id , mp_machine_adc_atten_get_helper (self ));
218+ }
219+
220+ mp_int_t mp_machine_adc_atten_get_helper (const machine_adc_obj_t * self ) {
221+ uint8_t value = madc_obj_atten [self - & madc_obj [0 ]];
222+ return value == 0 ? ADC_ATTEN_MAX : value - 1 ;
223+ }
224+
225+ void mp_machine_adc_atten_set_helper (const machine_adc_obj_t * self , mp_int_t atten ) {
226+ if (atten < ADC_ATTEN_MIN || atten > ADC_ATTEN_MAX ) {
227+ mp_raise_ValueError (MP_ERROR_TEXT ("invalid attenuation" ));
228+ }
229+
230+ madc_obj_atten [self - & madc_obj [0 ]] = atten + 1 ;
236231}
237232
238233static void mp_machine_adc_atten_set (machine_adc_obj_t * self , mp_int_t atten ) {
239- madc_atten_helper (self , atten );
234+ mp_machine_adc_atten_set_helper (self , atten );
235+ apply_self_adc_channel_atten (self , mp_machine_adc_atten_get_helper (self ));
236+ }
237+
238+ mp_int_t mp_machine_adc_width_get_helper (const machine_adc_obj_t * self ) {
239+ return self -> block -> bitwidth ;
240+ }
241+
242+ void mp_machine_adc_block_width_set_helper (machine_adc_block_obj_t * self , mp_int_t width ) {
243+ if (width < ADC_WIDTH_MIN || width > ADC_WIDTH_MAX ) {
244+ mp_raise_ValueError (MP_ERROR_TEXT ("invalid bit-width" ));
245+ }
246+
247+ self -> bitwidth = width ;
240248}
241249
242250static void mp_machine_adc_width_set (machine_adc_obj_t * self , mp_int_t width ) {
243- madcblock_bits_helper (self -> block , width );
251+ mp_machine_adc_block_width_set_helper (self -> block , width );
252+ apply_self_adc_channel_atten (self , mp_machine_adc_atten_get_helper (self ));
244253}
0 commit comments