@@ -44,6 +44,7 @@ class AnalogConfig : public AudioBaseInfo {
44
44
int mode_internal;
45
45
int port_no = I2S_NUM_0; // Analog input and output only supports 0!
46
46
bool use_apll = false ;
47
+ bool uninstall_driver_on_end = true ;
47
48
48
49
AnalogConfig () {
49
50
sample_rate = 44100 ;
@@ -185,81 +186,97 @@ class AnalogAudioStream : public AudioStreamX {
185
186
}
186
187
}
187
188
189
+ // / Reopen with last config
190
+ bool begin () override {
191
+ return begin (adc_config);
192
+ }
193
+
188
194
// / starts the DAC
189
195
bool begin (AnalogConfig cfg) {
190
196
LOGI (LOG_METHOD);
191
197
cfg.logInfo ();
192
- port_no = (i2s_port_t ) cfg.port_no ;
193
-
194
- adc_config = cfg;
195
- i2s_config_t i2s_config = {
196
- .mode = (i2s_mode_t )cfg.mode_internal ,
197
- .sample_rate = cfg.sample_rate ,
198
- .bits_per_sample = (i2s_bits_per_sample_t )cfg.bits_per_sample ,
199
- .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
200
- .communication_format = I2S_COMM_FORMAT_I2S_MSB,
201
- .intr_alloc_flags = 0 ,
202
- .dma_buf_count = cfg.dma_buf_count ,
203
- .dma_buf_len = cfg.dma_buf_len ,
204
- .use_apll = cfg.use_apll ,
205
- .tx_desc_auto_clear = false
206
- // // .fixed_mclk = 0
207
- };
208
-
209
-
210
- // setup config
211
- if (i2s_driver_install (port_no, &i2s_config, 0 , nullptr )!=ESP_OK){
212
- LOGE ( " %s - %s" , __func__, " i2s_driver_install" );
213
- return false ;
214
- }
215
-
216
- // clear i2s buffer
217
- if (i2s_zero_dma_buffer (port_no)!=ESP_OK) {
218
- LOGE ( " %s - %s" , __func__, " i2s_zero_dma_buffer" );
219
- return false ;
220
- }
221
198
222
- switch (cfg.mode ) {
223
- case RX_MODE:
224
- LOGI (" RX_MODE" );
199
+ if (!is_driver_installed){
200
+ port_no = (i2s_port_t ) cfg.port_no ;
201
+
202
+ adc_config = cfg;
203
+ i2s_config_t i2s_config = {
204
+ .mode = (i2s_mode_t )cfg.mode_internal ,
205
+ .sample_rate = cfg.sample_rate ,
206
+ .bits_per_sample = (i2s_bits_per_sample_t )cfg.bits_per_sample ,
207
+ .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
208
+ .communication_format = I2S_COMM_FORMAT_I2S_MSB,
209
+ .intr_alloc_flags = 0 ,
210
+ .dma_buf_count = cfg.dma_buf_count ,
211
+ .dma_buf_len = cfg.dma_buf_len ,
212
+ .use_apll = cfg.use_apll ,
213
+ .tx_desc_auto_clear = false
214
+ // // .fixed_mclk = 0
215
+ };
216
+
217
+
218
+ // setup config
219
+ if (i2s_driver_install (port_no, &i2s_config, 0 , nullptr )!=ESP_OK){
220
+ LOGE ( " %s - %s" , __func__, " i2s_driver_install" );
221
+ return false ;
222
+ }
225
223
226
- if (i2s_set_adc_mode (cfg.adc_unit [0 ], cfg.adc_channel [0 ])!=ESP_OK) {
227
- LOGE ( " %s - %s" , __func__, " i2s_driver_install" );
228
- return false ;
229
- }
224
+ // record driver as installed
225
+ is_driver_installed = true ;
230
226
231
- if (cfg.channels >1 ){
232
- if (i2s_set_adc_mode (cfg.adc_unit [1 ], cfg.adc_channel [1 ])!=ESP_OK) {
227
+ // clear i2s buffer
228
+ if (i2s_zero_dma_buffer (port_no)!=ESP_OK) {
229
+ LOGE ( " %s - %s" , __func__, " i2s_zero_dma_buffer" );
230
+ return false ;
231
+ }
232
+
233
+ switch (cfg.mode ) {
234
+ case RX_MODE:
235
+ LOGI (" RX_MODE" );
236
+
237
+ if (i2s_set_adc_mode (cfg.adc_unit [0 ], cfg.adc_channel [0 ])!=ESP_OK) {
233
238
LOGE ( " %s - %s" , __func__, " i2s_driver_install" );
234
239
return false ;
235
240
}
236
- }
237
241
238
- // enable the ADC
239
- if (i2s_adc_enable (port_no)!=ESP_OK) {
240
- LOGE ( " %s - %s" , __func__, " i2s_adc_enable" );
241
- return false ;
242
- }
243
-
244
- // if (adc1_config_channel_atten(ADC1_CHANNEL_6,ADC_ATTEN_DB_11)!=ESP_OK){
245
- // LOGE( "%s - %s", __func__, "adc1_config_channel_atten");
246
- // }
247
- // if (adc1_config_channel_atten(ADC1_CHANNEL_7,ADC_ATTEN_DB_11)!=ESP_OK){
248
- // LOGE( "%s - %s", __func__, "adc1_config_channel_atten");
249
- // }
250
- break ;
242
+ if (cfg.channels >1 ){
243
+ if (i2s_set_adc_mode (cfg.adc_unit [1 ], cfg.adc_channel [1 ])!=ESP_OK) {
244
+ LOGE ( " %s - %s" , __func__, " i2s_driver_install" );
245
+ return false ;
246
+ }
247
+ }
251
248
252
- case TX_MODE:
253
- LOGI ( " TX_MODE " );
254
- if ( i2s_set_pin (port_no, nullptr )!=ESP_OK) LOGE (" i2s_set_pin " );
255
- if ( i2s_set_dac_mode ( I2S_DAC_CHANNEL_BOTH_EN )!= ESP_OK) LOGE ( " i2s_set_dac_mode " ) ;
256
- break ;
249
+ // enable the ADC
250
+ if ( i2s_adc_enable (port_no)!=ESP_OK) {
251
+ LOGE ( " %s - %s " , __func__, " i2s_adc_enable " );
252
+ return false ;
253
+ }
257
254
258
- default :
259
- LOGE ( " Unsupported MODE: %d" , cfg.mode );
260
- return false ;
261
- break ;
255
+ // if (adc1_config_channel_atten(ADC1_CHANNEL_6,ADC_ATTEN_DB_11)!=ESP_OK){
256
+ // LOGE( "%s - %s", __func__, "adc1_config_channel_atten");
257
+ // }
258
+ // if (adc1_config_channel_atten(ADC1_CHANNEL_7,ADC_ATTEN_DB_11)!=ESP_OK){
259
+ // LOGE( "%s - %s", __func__, "adc1_config_channel_atten");
260
+ // }
261
+ break ;
262
+
263
+ case TX_MODE:
264
+ LOGI (" TX_MODE" );
265
+ if (i2s_set_pin (port_no, nullptr ) != ESP_OK) LOGE (" i2s_set_pin" );
266
+ if (i2s_set_dac_mode ( I2S_DAC_CHANNEL_BOTH_EN) != ESP_OK) LOGE (" i2s_set_dac_mode" );
267
+ break ;
262
268
269
+ default :
270
+ LOGE ( " Unsupported MODE: %d" , cfg.mode );
271
+ return false ;
272
+ break ;
273
+
274
+ }
275
+ } else {
276
+ i2s_start (port_no);
277
+ if (adc_config.mode == RX_MODE){
278
+ i2s_adc_enable (port_no);
279
+ }
263
280
}
264
281
active = true ;
265
282
return true ;
@@ -268,8 +285,18 @@ class AnalogAudioStream : public AudioStreamX {
268
285
// / stops the I2C and unistalls the driver
269
286
void end () override {
270
287
LOGI (__func__);
271
- i2s_adc_disable (port_no);
272
- i2s_driver_uninstall (port_no);
288
+ i2s_zero_dma_buffer (port_no);
289
+
290
+ // close ADC
291
+ if (adc_config.mode == RX_MODE){
292
+ i2s_adc_disable (port_no);
293
+ }
294
+ if (adc_config.uninstall_driver_on_end ){
295
+ i2s_driver_uninstall (port_no);
296
+ is_driver_installed = false ;
297
+ } else {
298
+ i2s_stop (port_no);
299
+ }
273
300
active = false ;
274
301
}
275
302
@@ -327,6 +354,7 @@ class AnalogAudioStream : public AudioStreamX {
327
354
AnalogConfig adc_config;
328
355
i2s_port_t port_no;
329
356
bool active = false ;
357
+ bool is_driver_installed = false ;
330
358
size_t result=0 ;
331
359
332
360
0 commit comments