63
63
#define ADC_CONV_LIMIT_EN 0
64
64
#endif
65
65
66
- static adc_digi_convert_mode_t convert_mode = ADC_CONV_SINGLE_UNIT_2 ;
67
- static adc_digi_output_format_t output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1 ;
68
- static uint8_t adc_channel = 0 ;
66
+ static void start_dma (analogbufio_bufferedin_obj_t * self , adc_digi_convert_mode_t * convert_mode , adc_digi_output_format_t * output_format );
67
+ static void stop_dma (analogbufio_bufferedin_obj_t * self );
69
68
70
69
void common_hal_analogbufio_bufferedin_construct (analogbufio_bufferedin_obj_t * self , const mcu_pin_obj_t * pin , uint32_t sample_rate ) {
70
+ self -> pin = pin ;
71
+ self -> sample_rate = sample_rate ;
72
+ }
73
+
74
+ static void start_dma (analogbufio_bufferedin_obj_t * self , adc_digi_convert_mode_t * convert_mode , adc_digi_output_format_t * output_format ) {
71
75
uint16_t adc1_chan_mask = 0 ;
72
76
uint16_t adc2_chan_mask = 0 ;
73
77
74
- output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1 ;
78
+ const mcu_pin_obj_t * pin = self -> pin ;
79
+ uint32_t sample_rate = self -> sample_rate ;
80
+
81
+ * output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1 ;
75
82
if (pin -> adc_index == ADC_UNIT_1 ) {
76
- convert_mode = ADC_CONV_SINGLE_UNIT_1 ;
83
+ * convert_mode = ADC_CONV_SINGLE_UNIT_1 ;
77
84
} else {
78
- convert_mode = ADC_CONV_SINGLE_UNIT_2 ;
85
+ * convert_mode = ADC_CONV_SINGLE_UNIT_2 ;
79
86
}
80
87
81
88
if (pin -> adc_index == NO_ADC || pin -> adc_channel == NO_ADC_CHANNEL ) {
82
89
raise_ValueError_invalid_pin ();
83
90
}
84
91
85
- adc_channel = pin -> adc_channel ;
86
-
87
92
/*
88
93
* Chip version Conversion Mode Output Format Type
89
94
* ESP32 1 TYPE1
@@ -106,14 +111,13 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s
106
111
107
112
#if defined(CONFIG_IDF_TARGET_ESP32C3 )
108
113
/* ESP32C3 only supports alter mode */
109
- convert_mode = ADC_CONV_ALTER_UNIT ;
114
+ * convert_mode = ADC_CONV_ALTER_UNIT ;
110
115
#endif
111
116
112
117
#if defined(CONFIG_IDF_TARGET_ESP32C3 ) || defined(CONFIG_IDF_TARGET_ESP32S3 ) || defined(CONFIG_IDF_TARGET_ESP32H2 )
113
- output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE2 ;
118
+ * output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE2 ;
114
119
#endif
115
120
116
- self -> pin = pin ;
117
121
common_hal_mcu_pin_claim (pin );
118
122
119
123
if (pin -> adc_index == ADC_UNIT_1 ) {
@@ -143,12 +147,12 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s
143
147
.conv_limit_num = 250 ,
144
148
.pattern_num = NUM_ADC_CHANNELS ,
145
149
.sample_freq_hz = sample_rate ,
146
- .conv_mode = convert_mode ,
147
- .format = output_format ,
150
+ .conv_mode = * convert_mode ,
151
+ .format = * output_format ,
148
152
};
149
153
150
154
#if defined(DEBUG_ANALOGBUFIO )
151
- mp_printf (& mp_plat_print ,"conversion_mode:%d, format:%d, conv_limit_en:%d, sample_rate:%d\n" ,convert_mode ,output_format ,ADC_CONV_LIMIT_EN ,sample_rate );
155
+ mp_printf (& mp_plat_print ,"conversion_mode:%d, format:%d, conv_limit_en:%d, sample_rate:%d\n" ,* convert_mode ,* output_format ,ADC_CONV_LIMIT_EN ,sample_rate );
152
156
#endif // DEBUG_ANALOGBUFIO
153
157
154
158
adc_digi_pattern_config_t adc_pattern [NUM_ADC_CHANNELS ] = {0 };
@@ -178,6 +182,13 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s
178
182
}
179
183
}
180
184
185
+ static void stop_dma (analogbufio_bufferedin_obj_t * self ) {
186
+ adc_digi_stop ();
187
+ adc_digi_deinitialize ();
188
+ // Release ADC Pin
189
+ reset_pin_number (self -> pin -> number );
190
+ }
191
+
181
192
bool common_hal_analogbufio_bufferedin_deinited (analogbufio_bufferedin_obj_t * self ) {
182
193
return self -> pin == NULL ;
183
194
}
@@ -186,22 +197,16 @@ void common_hal_analogbufio_bufferedin_deinit(analogbufio_bufferedin_obj_t *self
186
197
if (common_hal_analogbufio_bufferedin_deinited (self )) {
187
198
return ;
188
199
}
189
-
190
- adc_digi_stop ();
191
- adc_digi_deinitialize ();
192
-
193
- // Release ADC Pin
194
- reset_pin_number (self -> pin -> number );
195
200
self -> pin = NULL ;
196
201
}
197
202
198
- static bool check_valid_data (const adc_digi_output_data_t * data ) {
203
+ static bool check_valid_data (const adc_digi_output_data_t * data , const mcu_pin_obj_t * pin , adc_digi_convert_mode_t convert_mode , adc_digi_output_format_t output_format ) {
199
204
unsigned int unit = data -> type2 .unit ;
200
205
if (output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE2 ) {
201
206
if (data -> type2 .channel >= SOC_ADC_CHANNEL_NUM (unit )) {
202
207
return false;
203
208
}
204
- if (adc_channel != data -> type2 .channel ) {
209
+ if (pin -> adc_channel != data -> type2 .channel ) {
205
210
return false;
206
211
}
207
212
} else {
@@ -214,7 +219,7 @@ static bool check_valid_data(const adc_digi_output_data_t *data) {
214
219
if (data -> type1 .channel >= SOC_ADC_CHANNEL_NUM (unit )) {
215
220
return false;
216
221
}
217
- if (adc_channel != data -> type1 .channel ) {
222
+ if (pin -> adc_channel != data -> type1 .channel ) {
218
223
return false;
219
224
}
220
225
#endif
@@ -231,6 +236,14 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
231
236
uint32_t captured_bytes = 0 ;
232
237
esp_err_t ret ;
233
238
uint32_t ret_num = 0 ;
239
+ adc_digi_convert_mode_t convert_mode = ADC_CONV_SINGLE_UNIT_2 ;
240
+ adc_digi_output_format_t output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1 ;
241
+
242
+ if (bytes_per_sample != 2 ) {
243
+ mp_raise_ValueError_varg (translate ("%q must be a array of type 'H'" ), MP_QSTR_buffer );
244
+ }
245
+
246
+ start_dma (self , & convert_mode , & output_format );
234
247
235
248
#if defined(DEBUG_ANALOGBUFIO )
236
249
mp_printf (& mp_plat_print ,"Required bytes: %d\n" ,len );
@@ -243,7 +256,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
243
256
if (ret == ESP_OK ) {
244
257
for (uint32_t i = 0 ; i < ret_num ; i += ADC_RESULT_BYTE ) {
245
258
adc_digi_output_data_t * pResult = (adc_digi_output_data_t * )(void * )& result [i ];
246
- if (check_valid_data (pResult )) {
259
+ if (check_valid_data (pResult , self -> pin , convert_mode , output_format )) {
247
260
if (captured_bytes < len ) {
248
261
uint16_t * pBuffer = (uint16_t * )(void * )& buffer [captured_bytes ];
249
262
if (output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE1 ) {
@@ -256,6 +269,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
256
269
captured_bytes += sizeof (uint16_t );
257
270
captured_samples ++ ;
258
271
} else {
272
+ stop_dma (self );
259
273
return captured_samples ;
260
274
}
261
275
} else {
@@ -265,6 +279,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
265
279
#if defined(DEBUG_ANALOGBUFIO )
266
280
mp_printf (& mp_plat_print ,"Invalid sample received: 0x%x\n" ,pResult -> val );
267
281
#endif // DEBUG_ANALOGBUFIO
282
+ stop_dma (self );
268
283
return captured_samples ;
269
284
#endif
270
285
}
@@ -273,14 +288,18 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
273
288
#if defined(DEBUG_ANALOGBUFIO )
274
289
mp_printf (& mp_plat_print ,"ADC Timeout\n" );
275
290
#endif // DEBUG_ANALOGBUFIO
291
+ stop_dma (self );
276
292
return captured_samples ;
277
293
} else {
278
294
#if defined(DEBUG_ANALOGBUFIO )
279
295
mp_printf (& mp_plat_print ,"adc_digi_read_bytes failed error code:%d\n" ,ret );
280
296
#endif // DEBUG_ANALOGBUFIO
297
+ stop_dma (self );
281
298
return captured_samples ;
282
299
}
283
300
}
301
+
302
+ stop_dma (self );
284
303
#if defined(DEBUG_ANALOGBUFIO )
285
304
mp_printf (& mp_plat_print ,"Captured bytes: %d\n" ,captured_bytes );
286
305
#endif // DEBUG_ANALOGBUFIO
0 commit comments