Finally, it's working! Thanks everyone!
I'm using a spinlock to ensure that only the display or card reader uses the clk pin at once. Still, there are minor glitches in sending data to the display. I had to remove unnecessary switching pin statuses. There is still something to optimise, but it looks good enough.I tried to use a larger buffer_audio split into chunks, to read sd card less often, but visual glitches were even worse. Probably I should dive deeper into sd reader driver, because it's from the board producer, but it's not something for now.
I even gave up for a while and tried to decode an MP3 file stored as a byte array in Flash, but Waves are much easier to use.
I'm using a spinlock to ensure that only the display or card reader uses the clk pin at once. Still, there are minor glitches in sending data to the display. I had to remove unnecessary switching pin statuses. There is still something to optimise, but it looks good enough.
Code:
void draw_buffer(){ uint32_t current_offset=0; spin_lock_t *spi_spinlock = _hardware->get_spinlock(); uint32_t flags = spin_lock_blocking(spi_spinlock); _hardware->write(SD_CS_PIN, 1); _hardware->write(LCD_CS_PIN, 0); _hardware->write(LCD_DC_PIN, 0); _hardware->spi_write_byte(0x2C); _hardware->write(LCD_DC_PIN, 1); spin_unlock(spi_spinlock, flags); while(current_offset<BUFFER_SIZE) { flags = spin_lock_blocking(spi_spinlock); _hardware->write(SD_CS_PIN, 1); _hardware->write(LCD_CS_PIN, 0); dma_channel_set_read_addr(dma_channel,buffer+current_offset,true); dma_channel_wait_for_finish_blocking(dma_channel); current_offset+=chunk_size; spin_unlock(spi_spinlock, flags); }}Code:
void play_wave_file(char *file_name){FIL file;uint8_t header[44];f_res = f_open(&file, file_name, FA_READ);uint br;f_lseek(&file, 0);f_read(&file, header, 44, &br);if (f_res != FR_OK){printf("Loading file failed :(%d)\r\n", f_res);return;}const int buffer_size = 16;int16_t buffer_audio[buffer_size];spin_lock_t *spi_spinlock = _hardware->get_spinlock();while (1){uint32_t flags = spin_lock_blocking(spi_spinlock);f_read(&file, buffer_audio, sizeof(buffer_audio), &br);spin_unlock(spi_spinlock, flags);if (br == 0)break;struct audio_buffer_pool *audio_buffer_pool = _hardware->get_audio_buffer_pool();struct audio_buffer *audio_buf = take_audio_buffer(audio_buffer_pool, true);memcpy(audio_buf->buffer->bytes, buffer_audio, br);audio_buf->sample_count = br / 2;give_audio_buffer(audio_buffer_pool, audio_buf);}f_close(&file);}I even gave up for a while and tried to decode an MP3 file stored as a byte array in Flash, but Waves are much easier to use.
Statistics: Posted by YamiFive — Wed May 14, 2025 9:24 pm