STM32 用I2S和DMA控制CS4344播放WAVE音频详细程序
时间: 2024-02-12 10:08:21 浏览: 258
基于STM32的WAV音频解码和播放.doc
以下是一个使用STM32的I2S和DMA控制CS4344播放WAVE音频的详细程序:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_spi.h"
#include "stm32f4xx_dma.h"
#include "wave.h"
#define AUDIO_BUFFER_SIZE 2048
static uint16_t audio_buffer[AUDIO_BUFFER_SIZE];
static uint32_t audio_buffer_index = 0;
static uint32_t audio_buffer_size = 0;
void RCC_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource15, GPIO_AF_SPI3);
}
void SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_I2S_DeInit(SPI3);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI3, &SPI_InitStructure);
SPI_Cmd(SPI3, ENABLE);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Stream7);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI3->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)audio_buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = AUDIO_BUFFER_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream7, &DMA_InitStructure);
DMA_ITConfig(DMA1_Stream7, DMA_IT_TC, ENABLE);
NVIC_EnableIRQ(DMA1_Stream7_IRQn);
}
void DMA1_Stream7_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_Stream7, DMA_IT_TCIF7) != RESET) {
DMA_ClearITPendingBit(DMA1_Stream7, DMA_IT_TCIF7);
audio_buffer_index += AUDIO_BUFFER_SIZE;
audio_buffer_size -= AUDIO_BUFFER_SIZE;
if (audio_buffer_size < AUDIO_BUFFER_SIZE) {
DMA_Cmd(DMA1_Stream7, DISABLE);
SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, DISABLE);
}
}
}
void CS4344_Init(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
uint16_t reg = 0x0000; // DAC control register
reg |= 0x0001; // Soft reset
reg |= 0x0002; // Power up
reg |= 0x0020; // I2S mode
reg |= 0x0080; // Master mode
reg |= 0x0100; // 24-bit data
reg |= 0x0200; // BCLK is input to DAC
reg |= 0x0800; // MCLK is input to DAC
reg |= 0x1000; // Left channel DAC data is left-justified
reg |= 0x2000; // Right channel DAC data is right-justified
reg |= 0x4000; // Soft mute disable
CS4344_WriteReg(reg);
}
void CS4344_WriteReg(uint16_t reg)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
SPI_I2S_SendData(SPI3, (reg >> 8) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == SET);
SPI_I2S_SendData(SPI3, reg & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == SET);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
void Wave_Reader(uint8_t *data, uint32_t offset, uint32_t size)
{
uint32_t i;
for (i = 0; i < size; i += 2) {
audio_buffer[i / 2] = (uint16_t)data[offset + i + 1] << 8 | (uint16_t)data[offset + i];
}
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
SPI_Configuration();
DMA_Configuration();
CS4344_Init();
uint8_t *wav_data;
uint32_t wav_size;
uint32_t wav_freq;
uint32_t wav_samples;
if (Wave_ReadFile("test.wav", &wav_data, &wav_size, &wav_freq, &wav_samples) != 0) {
// Error reading wave file
while (1);
}
audio_buffer_size = AUDIO_BUFFER_SIZE;
Wave_Reader(wav_data, 44, AUDIO_BUFFER_SIZE * sizeof(uint16_t));
audio_buffer_index = AUDIO_BUFFER_SIZE;
DMA_Cmd(DMA1_Stream7, ENABLE);
SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE);
while (audio_buffer_size > 0) {
// Wait for DMA to finish
}
free(wav_data);
while (1);
}
```
该程序使用DMA和I2S接口将WAVE格式的音频数据发送到CS4344芯片,实现音频播放功能。其中,Wave_Reader函数用于从WAVE文件中读取音频数据并将其转换为16位PCM格式。CS4344_Init函数用于初始化CS4344芯片的各种寄存器设置。DMA1_Stream7_IRQHandler函数用于处理DMA传输完成中断。在main函数中,程序首先读取WAVE文件中的音频数据,然后将其发送到CS4344芯片。在DMA传输完成后,程序通过轮询等待音频数据的发送完成,直到音频数据全部发送完毕。
阅读全文