So I am using STMG030F6P6TR to play raw audio stored in SPI flash using PT8211 DAC and LM4890S amplifier. I have stored 8 Bit unsigned mono RAW data in 8000HZ in spi flash, retrieved it and pushed the data into the DAC as follows:
void playSong(uint16_t start_address, uint16_t end_address) {
enable_amp;
// Buffer to hold the data
uint16_t buffer2[512] = {0};
uint8_t temp_buffer[256];
// Read a page from SPI flash memory
SPIF_ReadPage(&spif, start_address, temp_buffer, sizeof(temp_buffer), 0);
// Process the temporary buffer to fill buffer with gain applied
for (uint16_t i = 0; i < 256; i++) {
// Apply gain and move 8-bit data to the upper 8 bits of 16-bit
int16_t sample = (int16_t)((temp_buffer[i] - 128))<<8;
// Set stereo output (same value for both left and right channels)
buffer2[2 * i] = (uint16_t)sample;
buffer2[2 * i + 1] = (uint16_t)sample;
}
// Wait until I2S is ready to transmit
while(!buffer_ready);
buffer_ready = false;
// while (HAL_I2S_GetState(&hi2s1) != HAL_I2S_STATE_READY);
// Transmit the processed data via I2S using DMA
HAL_I2S_Transmit_DMA(&hi2s1, buffer2, 512);
}
main(){
while (1) {
if (start_address <= end_address) {
playSong(start_address, end_address);
start_address++;
} else {
disable_amp;
start_address = metadata[43];
end_address = metadata[44];
}
}
}
And I2S setting is as:
hi2s1.Instance = SPI1;
hi2s1.Init.Mode = I2S_MODE_MASTER_TX;
hi2s1.Init.Standard = I2S_STANDARD_LSB;
hi2s1.Init.DataFormat = I2S_DATAFORMAT_16B;
hi2s1.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
hi2s1.Init.AudioFreq = I2S_AUDIOFREQ_8K;
hi2s1.Init.CPOL = I2S_CPOL_HIGH;
but the signal is as follows and I obtain crackling sound here:
and just 4 bit of data is being sent per word select cycle as:
Can someone assist me?