From 5a49ad1938134b9a4a9e039d853c19ae2689a50b Mon Sep 17 00:00:00 2001 From: Thomas Roell Date: Fri, 24 Feb 2017 18:45:20 -0700 Subject: [PATCH] I2S.write(sample) needs to block according to the reference implementation --- libraries/I2S/src/I2S.cpp | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/libraries/I2S/src/I2S.cpp b/libraries/I2S/src/I2S.cpp index 41e43cdf..37acea6f 100644 --- a/libraries/I2S/src/I2S.cpp +++ b/libraries/I2S/src/I2S.cpp @@ -275,12 +275,14 @@ int I2SClass::read(void* buffer, size_t size) { uint32_t xf_offset, xf_index, xf_count; - if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_RECEIVE))) { - return 0; + if (_state != I2S_STATE_RECEIVE) { + if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_RECEIVE))) { + return 0; + } + + _state = I2S_STATE_RECEIVE; } - _state = I2S_STATE_RECEIVE; - if (!_xf_active) { xf_offset = _xf_offset; @@ -376,24 +378,38 @@ size_t I2SClass::write(const uint8_t *buffer, size_t size) size_t I2SClass::write(int sample) { - return write((const void*)&sample, (_width / 8)); + return write((int32_t)sample); } size_t I2SClass::write(int32_t sample) { - return write((const void*)&sample, (_width / 8)); + if (_state != I2S_STATE_TRANSMIT) { + if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_TRANSMIT))) { + return 0; + } + + _state = I2S_STATE_TRANSMIT; + } + + while (!write((const void*)&sample, (_width / 8))) { + armv7m_core_yield(); + } + + return 1; } size_t I2SClass::write(const void *buffer, size_t size) { uint32_t xf_offset, xf_index, xf_count; - if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_TRANSMIT))) { - return 0; + if (_state != I2S_STATE_TRANSMIT) { + if (!((_state == I2S_STATE_READY) || (_state == I2S_STATE_TRANSMIT))) { + return 0; + } + + _state = I2S_STATE_TRANSMIT; } - _state = I2S_STATE_TRANSMIT; - if (_width == 32) { size &= ~3; } else if (_width == 16) { size &= ~1; }