30
30
31
31
#include "py/mphal.h"
32
32
#include "adc.h"
33
- #include "driver/adc .h"
33
+ #include "esp_adc/adc_oneshot .h"
34
34
35
35
#define ADCBLOCK1 (&madcblock_obj[0])
36
36
#define ADCBLOCK2 (&madcblock_obj[1])
@@ -126,20 +126,8 @@ static const machine_adc_obj_t madc_obj[] = {
126
126
#endif
127
127
};
128
128
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.
132
129
static uint8_t madc_obj_atten [MP_ARRAY_SIZE (madc_obj )];
133
130
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
-
143
131
const machine_adc_obj_t * madc_search_helper (machine_adc_block_obj_t * block , adc_channel_t channel_id , gpio_num_t gpio_id ) {
144
132
for (int i = 0 ; i < MP_ARRAY_SIZE (madc_obj ); i ++ ) {
145
133
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_
152
140
153
141
static void mp_machine_adc_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
154
142
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 ));
171
144
}
172
145
173
146
void 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
182
155
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
183
156
mp_arg_parse_all (n_pos_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
184
157
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 ));
190
164
}
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
+
191
171
}
192
172
193
173
static 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 ) {
194
174
madc_init_helper (self , n_pos_args , pos_args , kw_args );
195
175
}
196
176
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
+
197
184
static 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 ) {
198
185
mp_arg_check_num (n_pos_args , n_kw_args , 1 , MP_OBJ_FUN_ARGS_MAX , true);
199
186
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_
202
189
mp_raise_ValueError (MP_ERROR_TEXT ("invalid pin" ));
203
190
}
204
191
205
- if (self -> block -> width == -1 ) {
206
- madcblock_bits_helper (self -> block , self -> block -> bits );
207
- }
208
-
209
192
mp_map_t kw_args ;
210
193
mp_map_init_fixed_table (& kw_args , n_kw_args , args + n_pos_args );
211
194
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) {
225
208
static mp_int_t mp_machine_adc_read_u16 (machine_adc_obj_t * self ) {
226
209
mp_uint_t raw = madcblock_read_helper (self -> block , self -> channel_id );
227
210
// 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 ) ;
229
212
mp_uint_t u16 = raw << (16 - bits ) | raw >> (2 * bits - 16 );
230
213
return u16 ;
231
214
}
232
215
233
216
static 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 ;
236
231
}
237
232
238
233
static 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 ;
240
248
}
241
249
242
250
static 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 ));
244
253
}
0 commit comments