Skip to content

Commit 2bb6897

Browse files
committed
I2S cli, fixed read=write pointer
1 parent 926242b commit 2bb6897

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

BoxAudioBuffer.cpp

+40-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
void BoxAudioBuffer::init(uint8_t* buffer, uint16_t size) {
44
_bufferStart = buffer;
5-
_bufferEnd = buffer+size;
5+
_bufferEnd = buffer+size-1;
66
_bufferSize = size;
77

88
_readPointer = _bufferStart;
99
_writePointer = _bufferStart;
1010

11-
Log.info("BoxAudioBuffer init bufferStart=%X, _bufferEnd=%X, _bufferSize=%X, _readPointer=%X, _writePointer=%X", _bufferStart, _bufferEnd, _bufferSize, _readPointer, _writePointer);
11+
_isFull = false;
12+
_isEmpty = true;
13+
}
14+
15+
void BoxAudioBuffer::logState() {
16+
Log.info("BoxAudioBuffer bufferStart=%X, _bufferEnd=%X, _bufferSize=%X, _readPointer=%X, _writePointer=%X", _bufferStart, _bufferEnd, _bufferSize, _readPointer, _writePointer);
1217
Log.info(" getBytesReadable()=%X, getBytesReadableBlock()=%X, getBytesWritable()=%X, getBytesWritableBlock()=%X", getBytesReadable(), getBytesReadableBlock(), getBytesWritable(), getBytesWritableBlock());
1318
}
1419

@@ -17,10 +22,22 @@ bool BoxAudioBuffer::readPointerEqualsWritePointer() {
1722
}
1823

1924
bool BoxAudioBuffer::isEmpty(uint16_t threshold) {
20-
return (getBytesReadable() <= threshold);
25+
if (getBytesReadable() <= threshold) {
26+
if (threshold == 0) {
27+
return _isEmpty;
28+
}
29+
return true;
30+
}
31+
return false;
2132
}
2233
bool BoxAudioBuffer::isFull(uint16_t threshold) {
23-
return (getBytesWritable() <= threshold);
34+
if (getBytesWritable() <= threshold) {
35+
if (threshold == 0) {
36+
return _isFull;
37+
}
38+
return true;
39+
}
40+
return false;
2441
}
2542

2643
uint8_t* BoxAudioBuffer::getReadPointer() {
@@ -31,7 +48,11 @@ void BoxAudioBuffer::updateReadPointer(uint16_t readLength) {
3148
if (readLength < readableBlock) {
3249
_readPointer += readLength;
3350
} else {
34-
_readPointer = _bufferStart + (readLength - readableBlock - 1);
51+
_readPointer = _bufferStart + (readLength - readableBlock);
52+
}
53+
if (readPointerEqualsWritePointer()) {
54+
_isFull = false;
55+
_isEmpty = true;
3556
}
3657
}
3758

@@ -53,36 +74,48 @@ void BoxAudioBuffer::write(uint8_t* buffer, uint16_t size) {
5374
memcpy(_bufferStart, buffer + writableBlock, size - writableBlock);
5475
_writePointer = _bufferStart + (size - writableBlock);
5576
}
77+
if (readPointerEqualsWritePointer()) {
78+
_isEmpty = false;
79+
_isFull = true;
80+
}
5681
isWriting = false;
5782
}
5883

5984

6085
uint16_t BoxAudioBuffer::getBytesReadable() {
86+
if (readPointerEqualsWritePointer() && _isEmpty)
87+
return 0;
6188
return _measureDistance(_writePointer, _readPointer);
6289
}
6390
uint16_t BoxAudioBuffer::getBytesReadableBlock() {
91+
if (readPointerEqualsWritePointer() && _isEmpty)
92+
return 0;
6493
return _measureDistanceBlock(_writePointer, _readPointer);
6594
}
6695

6796
uint16_t BoxAudioBuffer::getBytesWritable() {
97+
if (readPointerEqualsWritePointer() && _isFull)
98+
return 0;
6899
return _measureDistance(_readPointer, _writePointer);
69100
}
70101
uint16_t BoxAudioBuffer::getBytesWritableBlock() {
102+
if (readPointerEqualsWritePointer() && _isFull)
103+
return 0;
71104
return _measureDistanceBlock(_readPointer, _writePointer);
72105
}
73106

74107
uint16_t BoxAudioBuffer::_measureDistance(uint8_t* p1, uint8_t* p2) {
75108
int32_t offset = p1 - p2;
76109

77-
if (offset >= 0)
110+
if (offset > 0)
78111
return offset;
79112

80113
return (_bufferEnd - p2 + 1) + (p1 - _bufferStart);
81114
}
82115
uint16_t BoxAudioBuffer::_measureDistanceBlock(uint8_t* p1, uint8_t* p2) {
83116
int32_t offset = p1 - p2;
84117

85-
if (offset >= 0)
118+
if (offset > 0)
86119
return offset;
87120

88121
return _bufferEnd - p2 + 1;

BoxAudioBuffer.h

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class BoxAudioBuffer {
88
bool isWriting = false;
99

1010
void init(uint8_t* buffer, uint16_t size);
11+
void logState();
1112

1213
bool readPointerEqualsWritePointer();
1314

@@ -29,6 +30,11 @@ class BoxAudioBuffer {
2930
private:
3031
bool _ready = false;
3132

33+
bool
34+
_isFull,
35+
_isEmpty;
36+
37+
3238
uint8_t* _bufferStart;
3339
uint8_t* _bufferEnd;
3440
uint16_t _bufferSize;

BoxCLI.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ void BoxCLI::begin() {
4141

4242
cmdHelp = cli.addSingleArgumentCommand("help");
4343
cmdHelp.setDescription(" Show this screen");
44+
45+
cmdI2S = cli.addCmd("i2s");
46+
cmdI2S.setDescription(" I2S debug information");
4447
}
4548

4649
void BoxCLI::loop() {
@@ -73,6 +76,8 @@ void BoxCLI::parse() {
7376
execRFID();
7477
} else if (lastCmd == cmdLoad) {
7578
execLoad();
79+
} else if (lastCmd == cmdI2S) {
80+
execI2S();
7681
}
7782
}
7883

@@ -290,6 +295,11 @@ void BoxCLI::execLoad() {
290295
}
291296
}
292297

298+
void BoxCLI::execI2S() {
299+
Box.boxDAC.audioBuffer.logState();
300+
Box.boxDAC.logDmaIrqChanges();
301+
}
302+
293303
unsigned long BoxCLI::parseNumber(String numberString) {
294304
const char* num = numberString.c_str();
295305
return parseNumber((char*)num);

BoxCLI.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class BoxCLI : public EnhancedThread {
2222
Command cmdBeep;
2323
Command cmdRFID;
2424
Command cmdLoad;
25+
Command cmdI2S;
2526

2627
void parse();
2728
unsigned long
@@ -34,7 +35,8 @@ class BoxCLI : public EnhancedThread {
3435
execSpiRFID(),
3536
execBeep(),
3637
execRFID(),
37-
execLoad();
38+
execLoad(),
39+
execI2S();
3840
};
3941

4042
#endif

BoxDAC.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void BoxDAC::fillBuffer(uint16_t timeoutMs) {
161161
// Log.info("Playbuffer (nearly) full (%i/%i)", bufferFilled, PLAY_BUFFER_SIZE);
162162
//Log.info("Playbuffer (%i/%i)", bufferFilled, PLAY_BUFFER_SIZE);
163163
while(timeout.isRunning()) {
164-
if (!audioBuffer.readPointerEqualsWritePointer() && audioBuffer.getBytesWritable()<4) //sic! Circual Buffer must not be full!
164+
if (audioBuffer.getBytesWritable()<4) //sic! Circual Buffer must not be full!
165165
break;
166166

167167
if (count % halfWavelength == 0) {
@@ -180,12 +180,15 @@ void BoxDAC::fillBuffer(uint16_t timeoutMs) {
180180
}
181181
if (!ready) {
182182
Log.info("XXXXXXXXXXXXXXXXXXX");
183-
logAudioBuffer();
183+
audioBuffer.logState();
184184
ready = true;
185185
}
186186
}
187187

188188
void BoxDAC::dmaPingPingComplete() {
189+
if (audioBuffer.isWriting)
190+
return;
191+
189192
unsigned long intStatus = MAP_uDMAIntStatus();
190193
unsigned int bytesReadable = audioBuffer.getBytesReadable();
191194
if (bytesReadable > 0)
@@ -203,10 +206,15 @@ void BoxDAC::dmaPingPingComplete() {
203206
unsigned long bufferSize = audioBuffer.getBytesReadableBlock();
204207
unsigned long size = bufferSize > I2S_PACKET_SIZE ? I2S_PACKET_SIZE : bufferSize;
205208
unsigned long elements = size / 2;
209+
210+
if (size == 1) {
211+
audioBuffer.updateReadPointer(1); //Workaround
212+
}
213+
size = elements * 2;
206214

207215
if (MAP_uDMAChannelModeGet(UDMA_CH5_I2S_TX | UDMA_PRI_SELECT) == UDMA_MODE_STOP) {
208216
//if((pControlTable[ulPrimaryIndexRx].ulControl & UDMA_CHCTL_XFERMODE_M) == 0) {
209-
if (elements==0) {
217+
if (elements<4) {
210218
ulPrimaryIndexRxEmpty++;
211219
MAP_uDMAChannelTransferSet(UDMA_CH5_I2S_TX, UDMA_MODE_PINGPONG, (void *)aZeroBuffer, (void *)I2S_TX_DMA_PORT, I2S_PACKET_ELEMENTS);
212220
} else {
@@ -217,7 +225,7 @@ void BoxDAC::dmaPingPingComplete() {
217225
MAP_uDMAChannelEnable(UDMA_CH5_I2S_TX);
218226
} else if (MAP_uDMAChannelModeGet(UDMA_CH5_I2S_TX | UDMA_ALT_SELECT) == UDMA_MODE_STOP) {
219227
//} else if((pControlTable[ulAltIndexRx].ulControl & UDMA_CHCTL_XFERMODE_M) == 0) {
220-
if (elements==0) {
228+
if (elements<0) {
221229
ulAltIndexRxEmpty++;
222230
MAP_uDMAChannelTransferSet(UDMA_CH5_I2S_TX|UDMA_ALT_SELECT, UDMA_MODE_PINGPONG, (void *)aZeroBuffer, (void *)I2S_TX_DMA_PORT, I2S_PACKET_ELEMENTS);
223231
} else {

0 commit comments

Comments
 (0)