6363#define ADC_CONV_LIMIT_EN 0
6464#endif
6565
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 );
6968
7069void 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 ) {
7175 uint16_t adc1_chan_mask = 0 ;
7276 uint16_t adc2_chan_mask = 0 ;
7377
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 ;
7582 if (pin -> adc_index == ADC_UNIT_1 ) {
76- convert_mode = ADC_CONV_SINGLE_UNIT_1 ;
83+ * convert_mode = ADC_CONV_SINGLE_UNIT_1 ;
7784 } else {
78- convert_mode = ADC_CONV_SINGLE_UNIT_2 ;
85+ * convert_mode = ADC_CONV_SINGLE_UNIT_2 ;
7986 }
8087
8188 if (pin -> adc_index == NO_ADC || pin -> adc_channel == NO_ADC_CHANNEL ) {
8289 raise_ValueError_invalid_pin ();
8390 }
8491
85- adc_channel = pin -> adc_channel ;
86-
8792 /*
8893 * Chip version Conversion Mode Output Format Type
8994 * ESP32 1 TYPE1
@@ -106,14 +111,13 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s
106111
107112 #if defined(CONFIG_IDF_TARGET_ESP32C3 )
108113 /* ESP32C3 only supports alter mode */
109- convert_mode = ADC_CONV_ALTER_UNIT ;
114+ * convert_mode = ADC_CONV_ALTER_UNIT ;
110115 #endif
111116
112117 #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 ;
114119 #endif
115120
116- self -> pin = pin ;
117121 common_hal_mcu_pin_claim (pin );
118122
119123 if (pin -> adc_index == ADC_UNIT_1 ) {
@@ -143,12 +147,12 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s
143147 .conv_limit_num = 250 ,
144148 .pattern_num = NUM_ADC_CHANNELS ,
145149 .sample_freq_hz = sample_rate ,
146- .conv_mode = convert_mode ,
147- .format = output_format ,
150+ .conv_mode = * convert_mode ,
151+ .format = * output_format ,
148152 };
149153
150154 #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 );
152156 #endif // DEBUG_ANALOGBUFIO
153157
154158 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
178182 }
179183}
180184
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+
181192bool common_hal_analogbufio_bufferedin_deinited (analogbufio_bufferedin_obj_t * self ) {
182193 return self -> pin == NULL ;
183194}
@@ -186,22 +197,16 @@ void common_hal_analogbufio_bufferedin_deinit(analogbufio_bufferedin_obj_t *self
186197 if (common_hal_analogbufio_bufferedin_deinited (self )) {
187198 return ;
188199 }
189-
190- adc_digi_stop ();
191- adc_digi_deinitialize ();
192-
193- // Release ADC Pin
194- reset_pin_number (self -> pin -> number );
195200 self -> pin = NULL ;
196201}
197202
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 ) {
199204 unsigned int unit = data -> type2 .unit ;
200205 if (output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE2 ) {
201206 if (data -> type2 .channel >= SOC_ADC_CHANNEL_NUM (unit )) {
202207 return false;
203208 }
204- if (adc_channel != data -> type2 .channel ) {
209+ if (pin -> adc_channel != data -> type2 .channel ) {
205210 return false;
206211 }
207212 } else {
@@ -214,7 +219,7 @@ static bool check_valid_data(const adc_digi_output_data_t *data) {
214219 if (data -> type1 .channel >= SOC_ADC_CHANNEL_NUM (unit )) {
215220 return false;
216221 }
217- if (adc_channel != data -> type1 .channel ) {
222+ if (pin -> adc_channel != data -> type1 .channel ) {
218223 return false;
219224 }
220225 #endif
@@ -231,6 +236,14 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
231236 uint32_t captured_bytes = 0 ;
232237 esp_err_t ret ;
233238 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 );
234247
235248 #if defined(DEBUG_ANALOGBUFIO )
236249 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
243256 if (ret == ESP_OK ) {
244257 for (uint32_t i = 0 ; i < ret_num ; i += ADC_RESULT_BYTE ) {
245258 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 )) {
247260 if (captured_bytes < len ) {
248261 uint16_t * pBuffer = (uint16_t * )(void * )& buffer [captured_bytes ];
249262 if (output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE1 ) {
@@ -256,6 +269,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
256269 captured_bytes += sizeof (uint16_t );
257270 captured_samples ++ ;
258271 } else {
272+ stop_dma (self );
259273 return captured_samples ;
260274 }
261275 } else {
@@ -265,6 +279,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
265279 #if defined(DEBUG_ANALOGBUFIO )
266280 mp_printf (& mp_plat_print ,"Invalid sample received: 0x%x\n" ,pResult -> val );
267281 #endif // DEBUG_ANALOGBUFIO
282+ stop_dma (self );
268283 return captured_samples ;
269284 #endif
270285 }
@@ -273,14 +288,18 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
273288 #if defined(DEBUG_ANALOGBUFIO )
274289 mp_printf (& mp_plat_print ,"ADC Timeout\n" );
275290 #endif // DEBUG_ANALOGBUFIO
291+ stop_dma (self );
276292 return captured_samples ;
277293 } else {
278294 #if defined(DEBUG_ANALOGBUFIO )
279295 mp_printf (& mp_plat_print ,"adc_digi_read_bytes failed error code:%d\n" ,ret );
280296 #endif // DEBUG_ANALOGBUFIO
297+ stop_dma (self );
281298 return captured_samples ;
282299 }
283300 }
301+
302+ stop_dma (self );
284303 #if defined(DEBUG_ANALOGBUFIO )
285304 mp_printf (& mp_plat_print ,"Captured bytes: %d\n" ,captured_bytes );
286305 #endif // DEBUG_ANALOGBUFIO
0 commit comments