使用stm32f103将音频文件存入nandflash中并且通过i2s驱动wm8960播放的示例代码
时间: 2024-02-03 22:12:52 浏览: 124
stm32 nand flash 驱动代码
4星 · 用户满意度95%
以下是使用STM32F103将音频文件存储到NAND Flash中并通过I2S驱动WM8960播放的示例代码。请注意,此代码仅供参考,您需要根据您的具体情况进行修改和适应。
首先,您需要了解以下几个基本知识:
1. NAND Flash的工作原理和接口
2. STM32F103的GPIO、SPI和I2S模块的使用
3. WM8960的工作原理和寄存器配置
在代码中,我们使用了FatFs文件系统来读取NAND Flash中的音频文件。需要先在代码中配置好FatFs的参数。然后,将音频文件转换为PCM格式,并将其存储在NAND Flash中。
接下来,我们配置STM32F103的GPIO、SPI和I2S模块。需要注意的是,不同的STM32开发板可能会有所不同,您需要根据您的具体情况进行修改。
最后,我们配置WM8960的寄存器,并使用I2S驱动它来播放音频。需要注意的是,WM8960的寄存器配置可能会因不同的应用而有所不同,您需要根据您的需求进行修改。
```
#include "stm32f10x.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_i2s.h"
#include "ff.h"
#define SPI_NAND_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
#define SPI_NAND_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)
#define I2S_WS_PIN GPIO_Pin_12
#define I2S_WS_PORT GPIOB
#define I2S_SCK_PIN GPIO_Pin_13
#define I2S_SCK_PORT GPIOB
#define I2S_SD_PIN GPIO_Pin_15
#define I2S_SD_PORT GPIOB
void SPI_NAND_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
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(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void I2S_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2S_InitTypeDef I2S_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
GPIO_InitStructure.GPIO_Pin = I2S_WS_PIN | I2S_SCK_PIN | I2S_SD_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(I2S_WS_PORT, &GPIO_InitStructure);
RCC_I2SCLKConfig(RCC_I2S2CLKSource_PLLI2S);
RCC_PLLI2SCmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLLI2SRDY) == RESET);
I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_48k;
I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
I2S_Init(SPI2, &I2S_InitStructure);
I2S_Cmd(SPI2, ENABLE);
}
void WM8960_Init(void)
{
// TODO: 配置WM8960的寄存器
}
void PlayAudio(void)
{
FIL file;
FRESULT res;
UINT br;
uint8_t buf[512];
uint16_t pcm;
res = f_open(&file, "audio.wav", FA_READ);
if (res != FR_OK) {
return;
}
SPI_NAND_CS_LOW();
SPI_I2S_SendData(SPI1, 0x00);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
SPI_I2S_SendData(SPI1, 0x0B);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
SPI_NAND_CS_HIGH();
while (1) {
res = f_read(&file, buf, sizeof(buf), &br);
if (res != FR_OK || br == 0) {
break;
}
for (int i = 0; i < br; i += 2) {
pcm = ((uint16_t)buf[i+1] << 8) | buf[i];
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, pcm);
}
}
f_close(&file);
}
int main(void)
{
SPI_NAND_Init();
I2S_Init();
WM8960_Init();
PlayAudio();
while (1) {
}
}
```
阅读全文