@@ -203,17 +203,35 @@ int I2SClass::_installDriver(){
203203
204204#if SOC_I2S_SUPPORTS_ADC_DAC
205205 if (_mode == I2S_ADC_DAC){
206- esp_i2s::adc_unit_t adc_unit = (esp_i2s::adc_unit_t ) 1 ;
207- esp_i2s::adc1_channel_t adc_channel = (esp_i2s::adc1_channel_t ) 6 ; //
208206 esp_i2s::i2s_set_dac_mode (esp_i2s::I2S_DAC_CHANNEL_BOTH_EN);
209- esp_i2s::i2s_set_adc_mode (adc_unit, adc_channel);
207+ esp_i2s::adc_unit_t adc_unit;
208+ if (!gpioToAdcUnit ((gpio_num_t )_inSdPin, &adc_unit)){
209+ log_e (" pin to adc unit conversion failed" );
210+ return 0 ; // ERR
211+ }
212+ esp_i2s::adc_channel_t adc_channel;
213+ if (!gpioToAdcChannel ((gpio_num_t )_inSdPin, &adc_channel)){
214+ log_e (" pin to adc channel conversion failed" );
215+ return 0 ; // ERR
216+ }
217+ if (ESP_OK != esp_i2s::i2s_set_adc_mode (adc_unit, (esp_i2s::adc1_channel_t )adc_channel)){
218+ log_e (" i2s_set_adc_mode failed" );
219+ end ();
220+ return 0 ; // ERR
221+ }
210222 if (ESP_OK != esp_i2s::i2s_set_pin ((esp_i2s::i2s_port_t ) _deviceIndex, NULL )){
211223 log_e (" i2s_set_pin failed" );
224+ end ();
212225 return 0 ; // ERR
213226 }
214227
215- esp_i2s::adc1_config_width (esp_i2s::ADC_WIDTH_BIT_12);
216- esp_i2s::adc1_config_channel_atten (adc_channel, esp_i2s::ADC_ATTEN_DB_11);
228+ if (adc_unit == esp_i2s::ADC_UNIT_1){
229+ esp_i2s::adc1_config_width (esp_i2s::ADC_WIDTH_BIT_12);
230+ esp_i2s::adc1_config_channel_atten ((esp_i2s::adc1_channel_t )adc_channel, esp_i2s::ADC_ATTEN_DB_11);
231+ }else if (adc_unit == esp_i2s::ADC_UNIT_2){
232+ esp_i2s::adc2_config_channel_atten ((esp_i2s::adc2_channel_t )adc_channel, esp_i2s::ADC_ATTEN_DB_11);
233+ }
234+
217235 esp_i2s::i2s_adc_enable ((esp_i2s::i2s_port_t ) _deviceIndex);
218236 _initialized = true ;
219237 }else // End of ADC/DAC mode
@@ -252,7 +270,7 @@ int I2SClass::begin(int mode, int sampleRate, int bitsPerSample, bool driveClock
252270 }
253271 _driveClock = driveClock;
254272 _mode = mode;
255- _sampleRate = sampleRate;
273+ _sampleRate = ( uint32_t ) sampleRate;
256274 _bitsPerSample = bitsPerSample;
257275
258276 if (_state != I2S_STATE_IDLE && _state != I2S_STATE_DUPLEX) {
@@ -287,11 +305,13 @@ int I2SClass::begin(int mode, int sampleRate, int bitsPerSample, bool driveClock
287305
288306 if (!_installDriver ()){
289307 _initialized = false ;
308+ end ();
290309 return 0 ; // ERR
291310 }
292311
293312 if (!createCallbackTask ()){
294313 _initialized = false ;
314+ end ();
295315 return 0 ; // ERR
296316 }
297317
@@ -455,8 +475,14 @@ void I2SClass::end()
455475 }
456476 _onTransmit = NULL ;
457477 _onReceive = NULL ;
458- vRingbufferDelete (_input_ring_buffer);
459- vRingbufferDelete (_output_ring_buffer);
478+ if (_input_ring_buffer != NULL ){
479+ vRingbufferDelete (_input_ring_buffer);
480+ _input_ring_buffer = NULL ;
481+ }
482+ if (_output_ring_buffer != NULL ){
483+ vRingbufferDelete (_output_ring_buffer);
484+ _output_ring_buffer = NULL ;
485+ }
460486 }else {
461487 log_w (" WARNING: ending I2SClass from callback task not permitted, but attempted!" );
462488 }
@@ -766,3 +792,152 @@ void I2SClass::onDmaTransferComplete(void*)
766792 // TODO set default pins for second module
767793 // I2SClass I2S1(I2S_DEVICE+1, I2S_CLOCK_GENERATOR, PIN_I2S_SD, PIN_I2S_SCK, PIN_I2S_FS); // default - half duplex
768794#endif
795+
796+ int I2SClass::gpioToAdcUnit (gpio_num_t gpio_num, esp_i2s::adc_unit_t * adc_unit){
797+ switch (gpio_num){
798+ #ifdef CONFIG_IDF_TARGET_ESP32
799+ // ADC 1
800+ case GPIO_NUM_36:
801+ case GPIO_NUM_37:
802+ case GPIO_NUM_38:
803+ case GPIO_NUM_39:
804+ case GPIO_NUM_32:
805+ case GPIO_NUM_33:
806+ case GPIO_NUM_34:
807+ case GPIO_NUM_35:
808+ *adc_unit = esp_i2s::ADC_UNIT_1;
809+ return 1 ; // OK
810+
811+ // ADC 2
812+ case GPIO_NUM_0:
813+ log_w (" GPIO 0 for ADC should not be used for dev boards due to external auto program circuits." );
814+ case GPIO_NUM_4:
815+ case GPIO_NUM_2:
816+ case GPIO_NUM_15:
817+ case GPIO_NUM_13:
818+ case GPIO_NUM_12:
819+ case GPIO_NUM_14:
820+ case GPIO_NUM_27:
821+ case GPIO_NUM_25:
822+ case GPIO_NUM_26:
823+ *adc_unit = esp_i2s::ADC_UNIT_2;
824+ return 1 ; // OK
825+ #endif
826+
827+ #ifdef CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
828+ case GPIO_NUM_1:
829+ case GPIO_NUM_2:
830+ case GPIO_NUM_3:
831+ case GPIO_NUM_4:
832+ case GPIO_NUM_5:
833+ case GPIO_NUM_6:
834+ case GPIO_NUM_7:
835+ case GPIO_NUM_8:
836+ case GPIO_NUM_9:
837+ case GPIO_NUM_10:
838+ *adc_unit = esp_i2s::ADC_UNIT_1;
839+ return 1 ; // OK
840+ #endif
841+
842+ #ifdef CONFIG_IDF_TARGET_ESP32S2
843+ case GPIO_NUM_11:
844+ case GPIO_NUM_12:
845+ case GPIO_NUM_13:
846+ case GPIO_NUM_14:
847+ case GPIO_NUM_15:
848+ case GPIO_NUM_16:
849+ case GPIO_NUM_17:
850+ case GPIO_NUM_18:
851+ case GPIO_NUM_19:
852+ case GPIO_NUM_20:
853+ *adc_unit = esp_i2s::ADC_UNIT_2;
854+ return 1 ; // OK
855+ #endif
856+
857+ #ifdef CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
858+ case GPIO_NUM_0:
859+ case GPIO_NUM_1:
860+ case GPIO_NUM_2:
861+ case GPIO_NUM_3:
862+ case GPIO_NUM_4:
863+ *adc_unit = esp_i2s::ADC_UNIT_1;
864+ return 1 ; // OK
865+ case GPIO_NUM_5:
866+ *adc_unit = esp_i2s::ADC_UNIT_2;
867+ return 1 ; // OK
868+ #endif
869+ default :
870+ log_e (" GPIO %d not usable for ADC!" , gpio_num);
871+ log_i (" Please refer to documentation https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html" );
872+ return 0 ; // ERR
873+ }
874+ }
875+
876+ int I2SClass::gpioToAdcChannel (gpio_num_t gpio_num, esp_i2s::adc_channel_t * adc_channel){
877+ switch (gpio_num){
878+ #ifdef CONFIG_IDF_TARGET_ESP32
879+ // ADC 1
880+ case GPIO_NUM_36: *adc_channel = esp_i2s::ADC_CHANNEL_0; return 1 ; // OK
881+ case GPIO_NUM_37: *adc_channel = esp_i2s::ADC_CHANNEL_1; return 1 ; // OK
882+ case GPIO_NUM_38: *adc_channel = esp_i2s::ADC_CHANNEL_2; return 1 ; // OK
883+ case GPIO_NUM_39: *adc_channel = esp_i2s::ADC_CHANNEL_3; return 1 ; // OK
884+ case GPIO_NUM_32: *adc_channel = esp_i2s::ADC_CHANNEL_4; return 1 ; // OK
885+ case GPIO_NUM_33: *adc_channel = esp_i2s::ADC_CHANNEL_5; return 1 ; // OK
886+ case GPIO_NUM_34: *adc_channel = esp_i2s::ADC_CHANNEL_6; return 1 ; // OK
887+ case GPIO_NUM_35: *adc_channel = esp_i2s::ADC_CHANNEL_7; return 1 ; // OK
888+
889+ // ADC 2
890+ case GPIO_NUM_0:
891+ log_w (" GPIO 0 for ADC should not be used for dev boards due to external auto program circuits." );
892+ *adc_channel = esp_i2s::ADC_CHANNEL_1; return 1 ; // OK
893+ case GPIO_NUM_4: *adc_channel = esp_i2s::ADC_CHANNEL_0; return 1 ; // OK
894+ case GPIO_NUM_2: *adc_channel = esp_i2s::ADC_CHANNEL_2; return 1 ; // OK
895+ case GPIO_NUM_15: *adc_channel = esp_i2s::ADC_CHANNEL_3; return 1 ; // OK
896+ case GPIO_NUM_13: *adc_channel = esp_i2s::ADC_CHANNEL_4; return 1 ; // OK
897+ case GPIO_NUM_12: *adc_channel = esp_i2s::ADC_CHANNEL_5; return 1 ; // OK
898+ case GPIO_NUM_14: *adc_channel = esp_i2s::ADC_CHANNEL_6; return 1 ; // OK
899+ case GPIO_NUM_27: *adc_channel = esp_i2s::ADC_CHANNEL_7; return 1 ; // OK
900+ case GPIO_NUM_25: *adc_channel = esp_i2s::ADC_CHANNEL_8; return 1 ; // OK
901+ case GPIO_NUM_26: *adc_channel = esp_i2s::ADC_CHANNEL_9; return 1 ; // OK
902+ #endif
903+
904+ #ifdef CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
905+ case GPIO_NUM_1: *adc_channel = esp_i2s::ADC_CHANNEL_0; return 1 ; // OK
906+ case GPIO_NUM_2: *adc_channel = esp_i2s::ADC_CHANNEL_1; return 1 ; // OK
907+ case GPIO_NUM_3: *adc_channel = esp_i2s::ADC_CHANNEL_2; return 1 ; // OK
908+ case GPIO_NUM_4: *adc_channel = esp_i2s::ADC_CHANNEL_3; return 1 ; // OK
909+ case GPIO_NUM_5: *adc_channel = esp_i2s::ADC_CHANNEL_4; return 1 ; // OK
910+ case GPIO_NUM_6: *adc_channel = esp_i2s::ADC_CHANNEL_5; return 1 ; // OK
911+ case GPIO_NUM_7: *adc_channel = esp_i2s::ADC_CHANNEL_6; return 1 ; // OK
912+ case GPIO_NUM_8: *adc_channel = esp_i2s::ADC_CHANNEL_7; return 1 ; // OK
913+ case GPIO_NUM_9: *adc_channel = esp_i2s::ADC_CHANNEL_8; return 1 ; // OK
914+ case GPIO_NUM_10: *adc_channel = esp_i2s::ADC_CHANNEL_9; return 1 ; // OK
915+ #endif
916+
917+ #ifdef CONFIG_IDF_TARGET_ESP32S2
918+ case GPIO_NUM_11: *adc_channel = esp_i2s::ADC_CHANNEL_0; return 1 ; // OK
919+ case GPIO_NUM_12: *adc_channel = esp_i2s::ADC_CHANNEL_1; return 1 ; // OK
920+ case GPIO_NUM_13: *adc_channel = esp_i2s::ADC_CHANNEL_2; return 1 ; // OK
921+ case GPIO_NUM_14: *adc_channel = esp_i2s::ADC_CHANNEL_3; return 1 ; // OK
922+ case GPIO_NUM_15: *adc_channel = esp_i2s::ADC_CHANNEL_4; return 1 ; // OK
923+ case GPIO_NUM_16: *adc_channel = esp_i2s::ADC_CHANNEL_5; return 1 ; // OK
924+ case GPIO_NUM_17: *adc_channel = esp_i2s::ADC_CHANNEL_6; return 1 ; // OK
925+ case GPIO_NUM_18: *adc_channel = esp_i2s::ADC_CHANNEL_7; return 1 ; // OK
926+ case GPIO_NUM_19: *adc_channel = esp_i2s::ADC_CHANNEL_8; return 1 ; // OK
927+ case GPIO_NUM_20: *adc_channel = esp_i2s::ADC_CHANNEL_9; return 1 ; // OK
928+ #endif
929+
930+ #ifdef CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
931+ case GPIO_NUM_0: *adc_channel = esp_i2s::ADC_CHANNEL_0; return 1 ; // OK
932+ case GPIO_NUM_1: *adc_channel = esp_i2s::ADC_CHANNEL_1; return 1 ; // OK
933+ case GPIO_NUM_2: *adc_channel = esp_i2s::ADC_CHANNEL_2; return 1 ; // OK
934+ case GPIO_NUM_3: *adc_channel = esp_i2s::ADC_CHANNEL_3; return 1 ; // OK
935+ case GPIO_NUM_4: *adc_channel = esp_i2s::ADC_CHANNEL_4; return 1 ; // OK
936+ case GPIO_NUM_5: *adc_channel = esp_i2s::ADC_CHANNEL_0; return 1 ; // OK
937+ #endif
938+ default :
939+ log_e (" GPIO %d not usable for ADC!" , gpio_num);
940+ log_i (" Please refer to documentation https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html" );
941+ return 0 ; // ERR
942+ }
943+ }
0 commit comments