24
24
#define _I2S_EVENT_QUEUE_LENGTH 100
25
25
#define _I2S_DMA_BUFFER_SIZE 512 // BUFFER SIZE must be between 8 and 1024
26
26
#define _I2S_DMA_BUFFER_COUNT 8 // BUFFER COUNT must be between 2 and 128
27
-
28
27
#define I2S_INTERFACES_COUNT SOC_I2S_NUM
29
28
30
29
#ifndef I2S_DEVICE
35
34
#define I2S_CLOCK_GENERATOR 0 // does nothing for ESP
36
35
#endif
37
36
38
- #ifndef PIN_I2S_SCK
39
- #define PIN_I2S_SCK 5
40
- #endif
41
-
42
- #ifndef PIN_I2S_FS
43
- #define PIN_I2S_FS 25
44
- #endif
45
-
46
- #ifndef PIN_I2S_SD
47
- #define PIN_I2S_SD 26
48
- #endif
49
-
50
-
51
- #ifndef PIN_I2S_SD_IN
52
- #define PIN_I2S_SD_IN 35 // 35 can be only input pin
53
- #endif
54
-
55
- #ifndef PIN_I2S_SD_OUT
56
- #define PIN_I2S_SD_OUT 26
57
- #endif
58
-
59
37
I2SClass::I2SClass (uint8_t deviceIndex, uint8_t clockGenerator, uint8_t sdPin, uint8_t sckPin, uint8_t fsPin) :
60
38
_deviceIndex(deviceIndex),
61
39
_sdPin(sdPin), // shared data pin
@@ -168,7 +146,6 @@ int I2SClass::begin(int mode, long sampleRate, int bitsPerSample, bool driveCloc
168
146
}
169
147
170
148
if (_state != I2S_STATE_IDLE && _state != I2S_STATE_DUPLEX) {
171
- Serial.println (" ERROR I2SClass::begin invalid state" ); // debug
172
149
return 0 ; // ERR
173
150
}
174
151
@@ -182,7 +159,6 @@ int I2SClass::begin(int mode, long sampleRate, int bitsPerSample, bool driveCloc
182
159
case I2S_LEFT_JUSTIFIED_MODE: // normally this should work, but i don't how to set it up for ESP
183
160
default :
184
161
// invalid mode
185
- Serial.println (" ERROR I2SClass::begin invalid mode" ); // debug
186
162
return 0 ; // ERR
187
163
}
188
164
@@ -203,7 +179,6 @@ int I2SClass::begin(int mode, long sampleRate, int bitsPerSample, bool driveCloc
203
179
204
180
if (_mode == I2S_ADC_DAC){
205
181
if (_bitsPerSample != 16 ){ // ADC/DAC can only work in 16-bit sample mode
206
- Serial.println (" ERROR I2SClass::begin invalid bps for ADC/DAC" ); // debug
207
182
return 0 ; // ERR
208
183
}
209
184
i2s_mode = (esp_i2s::i2s_mode_t )(i2s_mode | esp_i2s::I2S_MODE_DAC_BUILT_IN | esp_i2s::I2S_MODE_ADC_BUILT_IN);
@@ -261,7 +236,6 @@ int I2SClass::begin(int mode, long sampleRate, int bitsPerSample, bool driveCloc
261
236
// xSemaphoreGive(_out_buf_semaphore);
262
237
263
238
if (ESP_OK != esp_i2s::i2s_driver_install ((esp_i2s::i2s_port_t ) _deviceIndex, &i2s_config, _I2S_EVENT_QUEUE_LENGTH, &_i2sEventQueue)){ // Install and start i2s driver
264
- Serial.println (" ERROR I2SClass::begin error installing i2s driver" ); // debug
265
239
return 0 ; // ERR
266
240
}
267
241
@@ -450,6 +424,7 @@ int I2SClass::read(void* buffer, size_t size){
450
424
}
451
425
}
452
426
size_t cpy_size = 0 ;
427
+
453
428
// esp_i2s::i2s_read((esp_i2s::i2s_port_t) _deviceIndex, buffer, size, (size_t*) &read, 0);
454
429
// if(_in_buf_semaphore != NULL && xSemaphoreTake(_in_buf_semaphore, portMAX_DELAY) == pdTRUE){
455
430
if (_in_buf_semaphore != NULL && xSemaphoreTake (_in_buf_semaphore, 10 ) == pdTRUE){
@@ -488,12 +463,16 @@ size_t I2SClass::write(const void *buffer, size_t size)
488
463
}
489
464
size_t bytes_written;
490
465
// esp_i2s::i2s_write((esp_i2s::i2s_port_t) _deviceIndex, buffer, size, &bytes_written, 0);
491
- size_t cpy_size = size <= _buffer_byte_size - _output_buffer_pointer ? size : _buffer_byte_size - _output_buffer_pointer;
492
466
if (_out_buf_semaphore != NULL && xSemaphoreTake (_out_buf_semaphore, portMAX_DELAY) == pdTRUE){
467
+ size_t cpy_size = size <= _buffer_byte_size - _output_buffer_pointer ? size : _buffer_byte_size - _output_buffer_pointer;
493
468
memcpy ((void *)((uint )_outputBuffer+_output_buffer_pointer), buffer, cpy_size);
469
+ _output_buffer_pointer = (_output_buffer_pointer + cpy_size) > _buffer_byte_size ? _output_buffer_pointer : _output_buffer_pointer + cpy_size;
470
+ if (_output_buffer_pointer == _buffer_byte_size){ // when full flush it
471
+ esp_i2s::i2s_write ((esp_i2s::i2s_port_t ) _deviceIndex, _outputBuffer, _output_buffer_pointer, &bytes_written, 0 );
472
+ _output_buffer_pointer = 0 ;
473
+ }
494
474
xSemaphoreGive (_out_buf_semaphore);
495
475
}
496
- _output_buffer_pointer = (_output_buffer_pointer + cpy_size) > _buffer_byte_size ? _output_buffer_pointer : _output_buffer_pointer + cpy_size;
497
476
return bytes_written;
498
477
}
499
478
@@ -593,7 +572,6 @@ void I2SClass::onTransferComplete()
593
572
break ; // from the infinite loop
594
573
}else if (xActivatedMember == _i2sEventQueue){
595
574
xQueueReceive (_i2sEventQueue, &i2s_event, 0 );
596
- // if((i2s_event == esp_i2s::I2S_EVENT_TX_DONE) && (_state == I2S_STATE_DUPLEX || _state == I2S_STATE_TRANSMITTER)){
597
575
if (i2s_event == esp_i2s::I2S_EVENT_TX_DONE){
598
576
size_t bytes_written;
599
577
if (_out_buf_semaphore != NULL && xSemaphoreTake (_out_buf_semaphore, portMAX_DELAY) == pdTRUE){
@@ -602,14 +580,12 @@ void I2SClass::onTransferComplete()
602
580
if (xSemaphoreGive (_out_buf_semaphore) != pdTRUE){
603
581
// We would not expect this call to fail because we must have
604
582
// obtained the semaphore to get here.
605
- }
606
- }
583
+ } // semaphore give
584
+ } // output buffer semaphore
607
585
if (_onTransmit){
608
586
_onTransmit ();
609
587
} // user callback
610
- // }else if(i2s_event == esp_i2s::I2S_EVENT_RX_DONE && (_state == I2S_STATE_RECEIVER || _state == I2S_STATE_DUPLEX)){
611
588
}else if (i2s_event == esp_i2s::I2S_EVENT_RX_DONE){
612
- // if(_in_buf_semaphore != NULL && xSemaphoreTake(_in_buf_semaphore, portMAX_DELAY == pdTRUE)){
613
589
if (_in_buf_semaphore != NULL && xSemaphoreTake (_in_buf_semaphore, 10 ) == pdTRUE){
614
590
esp_i2s::i2s_read ((esp_i2s::i2s_port_t ) _deviceIndex, _inputBuffer, _buffer_byte_size, (size_t *) &_read_available, 0 );
615
591
_input_buffer_pointer = 0 ;
@@ -620,10 +596,10 @@ void I2SClass::onTransferComplete()
620
596
if (_onReceive) {
621
597
_onReceive ();
622
598
} // user callback
623
- } // semaphore
624
- } // if event TX or RX
625
- }
626
- }
599
+ } // input buffer semaphore
600
+ } // RX Done
601
+ } // I2S event
602
+ } // infinite loop
627
603
_callbackTaskHandle = NULL ; // prevent secondary termination to non-existing task
628
604
vTaskDelete (NULL );
629
605
}
0 commit comments