Skip to content

Commit f366160

Browse files
committed
[BACKUP] Latest experiments with internal buffer
1 parent baff65d commit f366160

File tree

6 files changed

+66
-74
lines changed

6 files changed

+66
-74
lines changed

Diff for: libraries/I2S/examples/ADCPlotter/ADCPlotter.ino

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
This example is only for ESP devices
2+
This example is only for ESP devices.
3+
34
This example demonstrates usage of integrated Digital to Analog Converter (DAC)
45
You can display sound wave from audio device, or just measure voltage.
56
@@ -41,20 +42,20 @@ Second option to measure voltage on trimmer / potentiometer has following connec
4142
|
4243
|
4344
V GND
44-
Optional resistor will decrease read value.
45+
Optional resistor will decrease read value.
4546
46-
Steps to run:
47-
1. Select target board:
48-
Tools -> Board -> ESP32 Arduino -> your board
49-
2. Upload sketch
50-
Press upload button (arrow in top left corner)
51-
When you see in console line like this: "Connecting........_____.....__"
52-
On your board press and hold Boot button and press EN button shortly. Now you can release both buttons.
53-
You should see lines like this: "Writing at 0x00010000... (12 %)" with rising percentage on each line.
54-
If this fails, try the board buttons right after pressing upload button, or reconnect the USB cable.
55-
3. Open plotter
56-
Tools -> Serial Plotter
57-
Enjoy
47+
Steps to run:
48+
1. Select target board:
49+
Tools -> Board -> ESP32 Arduino -> your board
50+
2. Upload sketch
51+
Press upload button (arrow in top left corner)
52+
When you see in console line like this: "Connecting........_____.....__"
53+
On your board press and hold Boot button and press EN button shortly. Now you can release both buttons.
54+
You should see lines like this: "Writing at 0x00010000... (12 %)" with rising percentage on each line.
55+
If this fails, try the board buttons right after pressing upload button, or reconnect the USB cable.
56+
3. Open plotter
57+
Tools -> Serial Plotter
58+
Enjoy
5859
5960
Created by Tomas Pilny
6061
on 17th June 2021
@@ -82,5 +83,4 @@ void loop() {
8283
// read a sample
8384
int sample = I2S.read();
8485
Serial.println(sample);
85-
delay(100);
86-
}
86+
}

Diff for: libraries/I2S/examples/Callbacks/Callbacks.ino

+17-16
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ on 23rd June 2021
5151

5252
// If you need to change any of the default pins, simply uncomment chosen line and change the pin number
5353
/*
54-
#define PIN_I2S_SCK 33
55-
#define PIN_I2S_FS 16
56-
#define PIN_I2S_SD 17
57-
#define PIN_I2S_SD_OUT 4
54+
#define PIN_I2S_SCK 5
55+
#define PIN_I2S_FS 25
56+
#define PIN_I2S_SD 35
57+
#define PIN_I2S_SD_OUT 26
5858
*/
5959

6060
#include <I2S.h>
@@ -67,18 +67,21 @@ const int amplitude = 32767; // amplitude of square wave
6767

6868
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
6969

70-
short sample = amplitude; // current sample value
70+
short out_sample = amplitude; // current sample value
7171
int count = 0;
7272

7373
void outputCallback(){
74+
7475
if (count % halfWavelength == 0) {
7576
// invert the sample every half wavelength count multiple to generate square wave
76-
sample = -1 * sample;
77+
out_sample = -1 * out_sample;
7778
}
7879

80+
Serial.printf("write %d\n", out_sample); // only for debug
81+
7982
// write the same sample twice, once for left and once for the right channel
80-
I2S.write(sample);
81-
I2S.write(sample);
83+
I2S.write(out_sample);
84+
I2S.write(out_sample);
8285

8386
// increment the counter for the next sample
8487
count++;
@@ -87,13 +90,11 @@ void outputCallback(){
8790
// code from InputSerialPlotter example
8891
void inputCallback(){
8992
// read a sample
90-
int sample = I2S.read();
93+
int in_sample = I2S.read();
9194

92-
Serial.println(sample); // only for debug
93-
94-
if (sample) {
95+
if (in_sample) {
9596
// if it's non-zero print value to serial
96-
Serial.println(sample);
97+
Serial.println(in_sample);
9798
}
9899
}
99100

@@ -111,7 +112,7 @@ void setup() {
111112
Serial.println("Failed to initialize I2S!");
112113
while (1) delay(100); // do nothing
113114
}
114-
I2S.setAllPins();
115+
I2S.setAllPins(PIN_I2S_SCK, PIN_I2S_FS, PIN_I2S_SD, PIN_I2S_SD_OUT);
115116

116117
// Register our callback functions
117118
I2S.onTransmit(outputCallback); // Function outputCallback will be called each time I2S finishes transmit operation (audio output)
@@ -121,5 +122,5 @@ void setup() {
121122

122123
void loop() {
123124
// loop task remains free for other work
124-
delay(100); // Let the FreeRTOS reset the watchDogTimer
125-
}
125+
delay(10); // Let the FreeRTOS reset the watchDogTimer
126+
}

Diff for: libraries/I2S/examples/InputSerialPlotter/InputSerialPlotter.ino

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ void setup() {
3232
Serial.println("Failed to initialize I2S!");
3333
while (1); // do nothing
3434
}
35-
I2S.setAllPins(27,25,33,26);
3635
}
3736

3837
void loop() {

Diff for: libraries/I2S/examples/SimpleTone/SimpleTone.ino

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
*/
1818

1919
#include <I2S.h>
20-
21-
const int frequency = 1250; // frequency of square wave in Hz
22-
const int amplitude = 32767; // amplitude of square wave
23-
const int sampleRate = 16000; // sample rate in Hz
20+
const int frequency = 440; // frequency of square wave in Hz
21+
const int amplitude = 500; // amplitude of square wave
22+
const int sampleRate = 8000; // sample rate in Hz
2423

2524
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
2625

Diff for: libraries/I2S/src/I2S.cpp

+13-37
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#define _I2S_EVENT_QUEUE_LENGTH 100
2525
#define _I2S_DMA_BUFFER_SIZE 512 // BUFFER SIZE must be between 8 and 1024
2626
#define _I2S_DMA_BUFFER_COUNT 8 // BUFFER COUNT must be between 2 and 128
27-
2827
#define I2S_INTERFACES_COUNT SOC_I2S_NUM
2928

3029
#ifndef I2S_DEVICE
@@ -35,27 +34,6 @@
3534
#define I2S_CLOCK_GENERATOR 0 // does nothing for ESP
3635
#endif
3736

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-
5937
I2SClass::I2SClass(uint8_t deviceIndex, uint8_t clockGenerator, uint8_t sdPin, uint8_t sckPin, uint8_t fsPin) :
6038
_deviceIndex(deviceIndex),
6139
_sdPin(sdPin), // shared data pin
@@ -168,7 +146,6 @@ int I2SClass::begin(int mode, long sampleRate, int bitsPerSample, bool driveCloc
168146
}
169147

170148
if (_state != I2S_STATE_IDLE && _state != I2S_STATE_DUPLEX) {
171-
Serial.println("ERROR I2SClass::begin invalid state"); // debug
172149
return 0; // ERR
173150
}
174151

@@ -182,7 +159,6 @@ int I2SClass::begin(int mode, long sampleRate, int bitsPerSample, bool driveCloc
182159
case I2S_LEFT_JUSTIFIED_MODE: // normally this should work, but i don't how to set it up for ESP
183160
default:
184161
// invalid mode
185-
Serial.println("ERROR I2SClass::begin invalid mode"); // debug
186162
return 0; // ERR
187163
}
188164

@@ -203,7 +179,6 @@ int I2SClass::begin(int mode, long sampleRate, int bitsPerSample, bool driveCloc
203179

204180
if(_mode == I2S_ADC_DAC){
205181
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
207182
return 0; // ERR
208183
}
209184
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
261236
//xSemaphoreGive(_out_buf_semaphore);
262237

263238
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
265239
return 0; // ERR
266240
}
267241

@@ -450,6 +424,7 @@ int I2SClass::read(void* buffer, size_t size){
450424
}
451425
}
452426
size_t cpy_size = 0;
427+
453428
//esp_i2s::i2s_read((esp_i2s::i2s_port_t) _deviceIndex, buffer, size, (size_t*) &read, 0);
454429
//if(_in_buf_semaphore != NULL && xSemaphoreTake(_in_buf_semaphore, portMAX_DELAY) == pdTRUE){
455430
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)
488463
}
489464
size_t bytes_written;
490465
//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;
492466
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;
493468
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+
}
494474
xSemaphoreGive(_out_buf_semaphore);
495475
}
496-
_output_buffer_pointer = (_output_buffer_pointer + cpy_size) > _buffer_byte_size ? _output_buffer_pointer : _output_buffer_pointer + cpy_size;
497476
return bytes_written;
498477
}
499478

@@ -593,7 +572,6 @@ void I2SClass::onTransferComplete()
593572
break; // from the infinite loop
594573
}else if(xActivatedMember == _i2sEventQueue){
595574
xQueueReceive(_i2sEventQueue, &i2s_event, 0);
596-
//if((i2s_event == esp_i2s::I2S_EVENT_TX_DONE) && (_state == I2S_STATE_DUPLEX || _state == I2S_STATE_TRANSMITTER)){
597575
if(i2s_event == esp_i2s::I2S_EVENT_TX_DONE){
598576
size_t bytes_written;
599577
if(_out_buf_semaphore != NULL && xSemaphoreTake(_out_buf_semaphore, portMAX_DELAY) == pdTRUE){
@@ -602,14 +580,12 @@ void I2SClass::onTransferComplete()
602580
if(xSemaphoreGive(_out_buf_semaphore) != pdTRUE){
603581
// We would not expect this call to fail because we must have
604582
// obtained the semaphore to get here.
605-
}
606-
}
583+
} // semaphore give
584+
} // output buffer semaphore
607585
if(_onTransmit){
608586
_onTransmit();
609587
} // user callback
610-
//}else if(i2s_event == esp_i2s::I2S_EVENT_RX_DONE && (_state == I2S_STATE_RECEIVER || _state == I2S_STATE_DUPLEX)){
611588
}else if(i2s_event == esp_i2s::I2S_EVENT_RX_DONE){
612-
//if(_in_buf_semaphore != NULL && xSemaphoreTake(_in_buf_semaphore, portMAX_DELAY == pdTRUE)){
613589
if(_in_buf_semaphore != NULL && xSemaphoreTake(_in_buf_semaphore, 10) == pdTRUE){
614590
esp_i2s::i2s_read((esp_i2s::i2s_port_t) _deviceIndex, _inputBuffer, _buffer_byte_size, (size_t*) &_read_available, 0);
615591
_input_buffer_pointer = 0;
@@ -620,10 +596,10 @@ void I2SClass::onTransferComplete()
620596
if (_onReceive) {
621597
_onReceive();
622598
} // 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
627603
_callbackTaskHandle = NULL; // prevent secondary termination to non-existing task
628604
vTaskDelete(NULL);
629605
}

Diff for: libraries/I2S/src/I2S.h

+17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ namespace esp_i2s {
2525
#include "driver/i2s.h" // ESP specific i2s driver
2626
}
2727

28+
// Default pins
29+
#ifndef PIN_I2S_SCK
30+
#define PIN_I2S_SCK 5
31+
#endif
32+
33+
#ifndef PIN_I2S_FS
34+
#define PIN_I2S_FS 25
35+
#endif
36+
37+
#ifndef PIN_I2S_SD
38+
#define PIN_I2S_SD 35 // Input if used in duplex
39+
#endif
40+
41+
#ifndef PIN_I2S_SD_OUT
42+
#define PIN_I2S_SD_OUT 26
43+
#endif
44+
2845
typedef enum {
2946
I2S_PHILIPS_MODE,
3047
I2S_RIGHT_JUSTIFIED_MODE,

0 commit comments

Comments
 (0)