Skip to content

Commit 5a49ad1

Browse files
I2S.write(sample) needs to block according to the reference implementation
1 parent 169038d commit 5a49ad1

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

libraries/I2S/src/I2S.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,14 @@ int I2SClass::read(void* buffer, size_t size)
275275
{
276276
uint32_t xf_offset, xf_index, xf_count;
277277

278-
if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_RECEIVE))) {
279-
return 0;
278+
if (_state != I2S_STATE_RECEIVE) {
279+
if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_RECEIVE))) {
280+
return 0;
281+
}
282+
283+
_state = I2S_STATE_RECEIVE;
280284
}
281285

282-
_state = I2S_STATE_RECEIVE;
283-
284286
if (!_xf_active)
285287
{
286288
xf_offset = _xf_offset;
@@ -376,24 +378,38 @@ size_t I2SClass::write(const uint8_t *buffer, size_t size)
376378

377379
size_t I2SClass::write(int sample)
378380
{
379-
return write((const void*)&sample, (_width / 8));
381+
return write((int32_t)sample);
380382
}
381383

382384
size_t I2SClass::write(int32_t sample)
383385
{
384-
return write((const void*)&sample, (_width / 8));
386+
if (_state != I2S_STATE_TRANSMIT) {
387+
if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_TRANSMIT))) {
388+
return 0;
389+
}
390+
391+
_state = I2S_STATE_TRANSMIT;
392+
}
393+
394+
while (!write((const void*)&sample, (_width / 8))) {
395+
armv7m_core_yield();
396+
}
397+
398+
return 1;
385399
}
386400

387401
size_t I2SClass::write(const void *buffer, size_t size)
388402
{
389403
uint32_t xf_offset, xf_index, xf_count;
390404

391-
if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_TRANSMIT))) {
392-
return 0;
405+
if (_state != I2S_STATE_TRANSMIT) {
406+
if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_TRANSMIT))) {
407+
return 0;
408+
}
409+
410+
_state = I2S_STATE_TRANSMIT;
393411
}
394412

395-
_state = I2S_STATE_TRANSMIT;
396-
397413
if (_width == 32) { size &= ~3; }
398414
else if (_width == 16) { size &= ~1; }
399415

0 commit comments

Comments
 (0)