@@ -117,6 +117,7 @@ int I2SClass::_installDriver(){
117
117
log_e (" (I2S#%d) This chip does not support ADC / DAC mode" , _deviceIndex);
118
118
return 0 ; // ERR
119
119
#endif
120
+ // TODO left/right justified mode is actually stereo, but ignoring the other channel input// left/righ
120
121
}else if (_mode == I2S_PHILIPS_MODE ||
121
122
_mode == I2S_RIGHT_JUSTIFIED_MODE ||
122
123
_mode == I2S_LEFT_JUSTIFIED_MODE){ // End of ADC/DAC mode; start of Normal Philips mode
@@ -128,8 +129,11 @@ int I2SClass::_installDriver(){
128
129
log_w (" (I2S#%d) Original Arduino library does not support 24 bits per sample.\n Keep that in mind if you should switch back to Arduino" , _deviceIndex);
129
130
}
130
131
}else if (_mode == PDM_STEREO_MODE || _mode == PDM_MONO_MODE){ // end of Normal Philips mode; start of PDM mode
131
- #if (SOC_I2S_SUPPORTS_PDM_TX && SOC_I2S_SUPPORTS_PDM_RX)
132
+ #if (SOC_I2S_SUPPORTS_PDM_TX || SOC_I2S_SUPPORTS_PDM_RX)
132
133
i2s_mode = (esp_i2s::i2s_mode_t )(i2s_mode | esp_i2s::I2S_MODE_PDM);
134
+ #ifndef SOC_I2S_SUPPORTS_PDM_RX
135
+ i2s_mode = (esp_i2s::i2s_mode_t )(i2s_mode & ~esp_i2s::I2S_MODE_RX); // remove PRM RX which is not supported on ESP32-C3
136
+ #endif
133
137
#else
134
138
log_e (" (I2S#%d) This chip does not support PDM" , _deviceIndex);
135
139
return 0 ; // ERR
@@ -254,13 +258,13 @@ int I2SClass::begin(int mode, int sampleRate, int bitsPerSample, bool driveClock
254
258
255
259
// There is work in progress on this library.
256
260
if (_bitsPerSample == 16 && _sampleRate > 16000 && driveClock){
257
- log_w (" (I2S#%d) This sample rate is not officially supported - audio might be noisy.\n Try using sample rate below or equal to 16000" , _deviceIndex);
261
+ log_w (" (I2S#%d) The sample rate value %d is not officially supported - audio might be noisy.\n Try using sample rate below or equal to 16000" , _deviceIndex, _sampleRate );
258
262
}
259
263
if (_bitsPerSample != 16 ){
260
264
log_w (" (I2S#%d) This bit-per-sample is not officially supported - audio quality might suffer.\n Try using 16bps, with sample rate below or equal 16000" , _deviceIndex);
261
265
}
262
266
if (_mode != I2S_PHILIPS_MODE){
263
- log_w (" (I2S#%d) This mode is not officially supported - audio quality might suffer.\n At the moment the only supported mode is I2S_PHILIPS_MODE" , _deviceIndex);
267
+ log_w (" (I2S#%d) The mode %s is not officially supported - audio quality might suffer.\n At the moment the only supported mode is I2S_PHILIPS_MODE" , _deviceIndex, i2s_mode_text[_mode] );
264
268
}
265
269
266
270
if (_state != I2S_STATE_IDLE && _state != I2S_STATE_DUPLEX) {
@@ -276,14 +280,24 @@ int I2SClass::begin(int mode, int sampleRate, int bitsPerSample, bool driveClock
276
280
277
281
#if (SOC_I2S_SUPPORTS_ADC && SOC_I2S_SUPPORTS_DAC)
278
282
case ADC_DAC_MODE:
283
+ #else
284
+ log_e (" (I2S#%d) ERROR: ADC/DAC is not supported on this SoC. Change mode or SoC" , _deviceIndex);
285
+ _give_if_top_call ();
286
+ return 0 ; // ERR
279
287
#endif
280
288
289
+ #if defined(SOC_I2S_SUPPORTS_PDM_TX) || defined(SOC_I2S_SUPPORTS_PDM_RX)
281
290
case PDM_STEREO_MODE:
282
291
case PDM_MONO_MODE:
283
292
break ;
293
+ #else
294
+ log_e (" (I2S#%d) ERROR: PDM is not supported on this SoC. Change mode or SoC" , _deviceIndex);
295
+ _give_if_top_call ();
296
+ return 0 ; // ERR
297
+ #endif
284
298
285
299
default : // invalid mode
286
- log_e (" (I2S#%d) ERROR: unknown mode" , _deviceIndex);
300
+ log_e (" (I2S#%d) ERROR: unknown or unsupported mode" , _deviceIndex);
287
301
_give_if_top_call ();
288
302
return 0 ; // ERR
289
303
}
@@ -295,6 +309,7 @@ int I2SClass::begin(int mode, int sampleRate, int bitsPerSample, bool driveClock
295
309
}
296
310
297
311
_buffer_byte_size = _i2s_dma_buffer_size * (_bitsPerSample / 8 ) * _I2S_DMA_BUFFER_COUNT * 2 ;
312
+ log_d (" (I2S#%d) Creating internal buffers. Requested size = %dB\n " , _deviceIndex, _buffer_byte_size);
298
313
_input_ring_buffer = xRingbufferCreate (_buffer_byte_size, RINGBUF_TYPE_BYTEBUF);
299
314
_output_ring_buffer = xRingbufferCreate (_buffer_byte_size, RINGBUF_TYPE_BYTEBUF);
300
315
if (_input_ring_buffer == NULL || _output_ring_buffer == NULL ){
@@ -361,7 +376,6 @@ int I2SClass::setSckPin(int sckPin){
361
376
_take_if_not_holding ();
362
377
_setSckPin (sckPin);
363
378
int ret = _applyPinSetting ();
364
- _applyPinSetting ();
365
379
_give_if_top_call ();
366
380
return ret;
367
381
}
@@ -454,15 +468,17 @@ int I2SClass::setAllPins(int sckPin, int fsPin, int sdPin, int outSdPin, int inS
454
468
int I2SClass::setDuplex (){
455
469
_take_if_not_holding ();
456
470
_state = I2S_STATE_DUPLEX;
471
+ int ret = _applyPinSetting ();
457
472
_give_if_top_call ();
458
- return 1 ;
473
+ return ret ;
459
474
}
460
475
461
476
int I2SClass::setSimplex (){
462
477
_take_if_not_holding ();
463
478
_state = I2S_STATE_IDLE;
479
+ int ret = _applyPinSetting ();
464
480
_give_if_top_call ();
465
- return 1 ;
481
+ return ret ;
466
482
}
467
483
468
484
int I2SClass::isDuplex (){
@@ -707,6 +723,8 @@ size_t I2SClass::write_blocking(const void *buffer, size_t size){
707
723
// non-blocking version of write
708
724
// In case there is not enough space in buffer to write requested size
709
725
// this function will try to flush the buffer and write requested data with 0 time-out
726
+ // The function will return number of successfully written bytes. It is users responsibility
727
+ // to take care of the remaining data.
710
728
size_t I2SClass::write_nonblocking (const void *buffer, size_t size){
711
729
_take_if_not_holding ();
712
730
if (_initialized){
@@ -759,8 +777,8 @@ int I2SClass::peek(){
759
777
_give_if_top_call ();
760
778
return ret;
761
779
}
762
-
763
- // TODO flush everything instead of one buffer
780
+ // Requests data from ring buffer and writes data to I2S module
781
+ // note: it is NOT necessary to call the flush after writes. Buffer is flushed automatically after receiveing enough data.
764
782
void I2SClass::flush (){
765
783
_take_if_not_holding ();
766
784
if (_initialized){
@@ -804,6 +822,11 @@ void I2SClass::onReceive(void(*function)(void)){
804
822
_give_if_top_call ();
805
823
}
806
824
825
+
826
+ // Change buffer size. The unit is in frames.
827
+ // Byte value can be calculated as follows:
828
+ // ByteSize = (bits_per_sample / 8) * number_of_channels * bufferSize
829
+ // Calling this function will automatically restart the driver, which could cause audio output gap.
807
830
int I2SClass::setBufferSize (int bufferSize){
808
831
_take_if_not_holding ();
809
832
int ret = 0 ;
@@ -997,7 +1020,11 @@ void I2SClass::_post_read_data_fix(void *input, size_t *size){
997
1020
((uint16_t *)input)[dst_ptr++] = tmp;
998
1021
}
999
1022
break ;
1000
- default : ; // Do nothing
1023
+ case 24 :
1024
+ // TODO check if need fix and if so, implement it
1025
+ break ;
1026
+ default : ;
1027
+ // Do nothing
1001
1028
} // switch
1002
1029
}
1003
1030
0 commit comments