Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fda293

Browse files
committed
esp32/machine_i2s: Integrate new I2S IDF driver.
The legacy I2S "shim" is removed and replaced by the new I2S driver. The new driver fixes a bug where mono audio plays only in one channel. Application code size is reduced by 2672 bytes with this change. Tested on ESP32, ESP32+spiram, ESP32-S3 using example code from https://github.com/miketeachman/micropython-i2s-examples Signed-off-by: Mike Teachman <[email protected]>
1 parent 802a88c commit 6fda293

File tree

3 files changed

+156
-160
lines changed

3 files changed

+156
-160
lines changed

extmod/machine_i2s.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ STATIC mp_uint_t machine_i2s_stream_write(mp_obj_t self_in, const void *buf_in,
600600

601601
return size;
602602
} else { // blocking or asyncio mode
603+
603604
mp_buffer_info_t appbuf;
604605
appbuf.buf = (void *)buf_in;
605606
appbuf.len = size;
@@ -608,6 +609,7 @@ STATIC mp_uint_t machine_i2s_stream_write(mp_obj_t self_in, const void *buf_in,
608609
#else
609610
uint32_t num_bytes_written = copy_appbuf_to_dma(self, &appbuf);
610611
#endif
612+
611613
return num_bytes_written;
612614
}
613615
}
@@ -623,6 +625,7 @@ STATIC mp_uint_t machine_i2s_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
623625

624626
if (flags & MP_STREAM_POLL_RD) {
625627
if (self->mode != MICROPY_PY_MACHINE_I2S_CONSTANT_RX) {
628+
626629
*errcode = MP_EPERM;
627630
return MP_STREAM_ERROR;
628631
}
@@ -632,21 +635,14 @@ STATIC mp_uint_t machine_i2s_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
632635
ret |= MP_STREAM_POLL_RD;
633636
}
634637
#else
635-
// check event queue to determine if a DMA buffer has been filled
636-
// (which is an indication that at least one DMA buffer is available to be read)
637-
// note: timeout = 0 so the call is non-blocking
638-
i2s_event_t i2s_event;
639-
if (xQueueReceive(self->i2s_event_queue, &i2s_event, 0)) {
640-
if (i2s_event.type == I2S_EVENT_RX_DONE) {
641-
// getting here means that at least one DMA buffer is now full
642-
// indicating that audio samples can be read from the I2S object
643-
ret |= MP_STREAM_POLL_RD;
644-
}
638+
if (self->dma_buffer_status == DMA_MEMORY_NOT_EMPTY) {
639+
ret |= MP_STREAM_POLL_RD;
645640
}
646641
#endif
647642
}
648643

649644
if (flags & MP_STREAM_POLL_WR) {
645+
650646
if (self->mode != MICROPY_PY_MACHINE_I2S_CONSTANT_TX) {
651647
*errcode = MP_EPERM;
652648
return MP_STREAM_ERROR;
@@ -657,20 +653,14 @@ STATIC mp_uint_t machine_i2s_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
657653
ret |= MP_STREAM_POLL_WR;
658654
}
659655
#else
660-
// check event queue to determine if a DMA buffer has been emptied
661-
// (which is an indication that at least one DMA buffer is available to be written)
662-
// note: timeout = 0 so the call is non-blocking
663-
i2s_event_t i2s_event;
664-
if (xQueueReceive(self->i2s_event_queue, &i2s_event, 0)) {
665-
if (i2s_event.type == I2S_EVENT_TX_DONE) {
666-
// getting here means that at least one DMA buffer is now empty
667-
// indicating that audio samples can be written to the I2S object
668-
ret |= MP_STREAM_POLL_WR;
669-
}
656+
657+
if (self->dma_buffer_status == DMA_MEMORY_NOT_FULL) {
658+
ret |= MP_STREAM_POLL_WR;
670659
}
671660
#endif
672661
}
673662
} else {
663+
674664
*errcode = MP_EINVAL;
675665
ret = MP_STREAM_ERROR;
676666
}

0 commit comments

Comments
 (0)