Skip to content

Commit

Permalink
ports/psoc6/machine_i2s.c: WIP starting from mtb examples snippets.
Browse files Browse the repository at this point in the history
Signed-off-by: enriquezgarc <enriquezgarcia.external@infineon.com>
  • Loading branch information
jaenrig-ifx committed Mar 22, 2024
1 parent 8f78bb6 commit 87adad3
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions ports/psoc6/machine_i2s.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cyhal_clock_t hf0_clock;
// 1. memory use (smaller buffer size desirable to reduce memory footprint)
// 2. interrupt frequency (larger buffer size desirable to reduce interrupt frequency)
// The sizeof 1/2 of the DMA buffer must be evenly divisible by the cache line size of 32 bytes.
#define SIZEOF_DMA_BUFFER (256)
#define SIZEOF_DMA_BUFFER (64)
#define SIZEOF_HALF_DMA_BUFFER (SIZEOF_DMA_BUFFER / 2)
#define SIZEOF_HALF_DMA_BUFFER_IN_BYTES (SIZEOF_HALF_DMA_BUFFER * sizeof(uint32_t))

Expand Down Expand Up @@ -210,35 +210,19 @@ static void i2s_dma_from_dmabuf_to_ringbuf(machine_i2s_obj_t *self) {
}

static void i2s_dma_from_ringbuf_to_dmabuf(machine_i2s_obj_t *self) {
uint8_t *dma_buffer_p = (uint8_t *)self->dma_idle_buf_p;
memset(dma_buffer_p, 0, SIZEOF_HALF_DMA_BUFFER_IN_BYTES);
if (ringbuf_available_data(&self->ring_buffer) >= SIZEOF_HALF_DMA_BUFFER_IN_BYTES) {
uint8_t *dma_buffer_p = (uint8_t *)self->dma_idle_buf_p;
// Mono samples duplicate the sample in the right channel
// if (self->mode == MONO) {
// uint8_t bytes_per_sample = (self->bits == 16? 2 : 4);
// for (uint32_t i = 0; i < SIZEOF_HALF_DMA_BUFFER * sizeof(uint32_t); i += (2 * bytes_per_sample)) {
// for (uint8_t b = 0; b < bytes_per_sample; b++) {
// ringbuf_pop(&self->ring_buffer, &dma_buffer_p[i + b]);
// dma_buffer_p[i + b + 2 * bytes_per_sample] = dma_buffer_p[i + b];
// }
// }
// } else if (self->mode == STEREO) {
for (uint32_t i = 0; i < SIZEOF_HALF_DMA_BUFFER_IN_BYTES; i++) {
ringbuf_pop(&self->ring_buffer, &dma_buffer_p[i]);
}
// }
}
}

static void fill_app_buffer(machine_i2s_obj_t *self) {
for (uint16_t i = 0; i < SIZEOF_HALF_DMA_BUFFER_IN_BYTES; i++)
{
// uint32_t value = i | (i + 1) << 8 | (i + 2) << 16 | (i + 3) << 24;
// self->dma_idle_buf_p[i] = value;
// for (uint8_t j = 0; j < 4; j++) {
// ringbuf_push(&self->ring_buffer, i + j);
ringbuf_push(&self->ring_buffer, i);
// }
}
// else {
// memset(dma_buffer_p, 0, SIZEOF_HALF_DMA_BUFFER_IN_BYTES);
// for (uint32_t i = 0; i < SIZEOF_HALF_DMA_BUFFER_IN_BYTES; i++) {
// dma_buffer_p[i] = 0;
// }
// }
}

static inline void i2s_dma_swap_active_dmabuf(machine_i2s_obj_t *self) {
Expand All @@ -256,7 +240,6 @@ static void i2s_dma_tx_event_process(machine_i2s_obj_t *self, cyhal_i2s_event_t

i2s_dma_swap_active_dmabuf(self);
i2s_dma_tx(self);
fill_app_buffer(self);
i2s_dma_from_ringbuf_to_dmabuf(self);
}
}
Expand Down Expand Up @@ -342,6 +325,12 @@ static void i2s_dma_tx_init(machine_i2s_obj_t *self) {
}

static inline void i2s_dma_init_buff(machine_i2s_obj_t *self) {

for (uint32_t i = 0; i < SIZEOF_DMA_BUFFER; i++)
{
self->dma_buffer[i] = 0;
}

self->dma_active_buf_p = self->dma_buffer;
self->dma_idle_buf_p = &self->dma_buffer[SIZEOF_HALF_DMA_BUFFER];
}
Expand Down

0 comments on commit 87adad3

Please sign in to comment.