STM32H753 SAI 范例程序、
时间: 2024-06-12 10:07:14 浏览: 212
STM32H743 基础例程代码
SAI(Serial Audio Interface)是一种串行音频接口,用于在数字音频设备之间传输音频数据。STM32H753系列微控制器具有SAI接口,可用于音频处理和音频数据传输。
以下是STM32H753 SAI的范例程序:
```c
#include "stm32h7xx.h"
// SAI配置参数
#define SAI_CLOCK_DIVIDER 12
#define SAI_FS_DIVIDER 8
#define SAI_FRAME_SIZE 32
#define SAI_DATA_SIZE 16
#define SAI_COMPANDING_MODE SAI_NOCOMPANDING
#define SAI_PROTOCOL SAI_FREE_PROTOCOL
#define SAI_AUDIO_MODE SAI_MODEMASTER_TX
#define SAI_MCK_OUTPUT SAI_MCKDISABLE
#define SAI_SYNCHRONIZATION SAI_ASYNCHRONOUS
// SAI时钟配置
void SAI_ClockConfig(void)
{
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
// 配置PLLSAI1时钟
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SAI1;
PeriphClkInitStruct.PLLSAI1.PLLSAI1N = 37;
PeriphClkInitStruct.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV17;
PeriphClkInitStruct.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_SAI1CLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
// 配置SAI时钟
__HAL_RCC_SAI1_CLK_ENABLE();
__HAL_RCC_SAI1_FORCE_RESET();
__HAL_RCC_SAI1_RELEASE_RESET();
}
// SAI配置
void SAI_Config(void)
{
SAI_HandleTypeDef SAI_Handle = {0};
SAI_InitTypeDef SAI_Init = {0};
// 配置SAI参数
SAI_Init.AudioFrequency = 44100;
SAI_Init.ClockSource = SAI_CLKSOURCE_PLLSAI1;
SAI_Init.ClockDivider = SAI_CLOCK_DIVIDER;
SAI_Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF;
SAI_Init.AudioMode = SAI_AUDIO_MODE;
SAI_Init.DataSize = SAI_DATA_SIZE;
SAI_Init.Protocol = SAI_PROTOCOL;
SAI_Init.TransmitDMA = SAI_DMA_ENABLE;
SAI_Init.CompandingMode = SAI_COMPANDING_MODE;
SAI_Init.TriState = SAI_OUTPUT_NOTRELEASED;
SAI_Init.MckOutput = SAI_MCK_OUTPUT;
SAI_Init.MonoStereoMode = SAI_STEREOMODE;
SAI_Init.Synchro = SAI_SYNCHRONIZATION;
// 配置SAI句柄
SAI_Handle.Instance = SAI1_Block_A;
SAI_Handle.Init = SAI_Init;
HAL_SAI_Init(&SAI_Handle);
// 配置FS和FRAME信号
SAI_FrameInitTypeDef SAI_FrameInit = {0};
SAI_FrameInit.FrameLength = SAI_FRAME_SIZE - 1;
SAI_FrameInit.ActiveFrameLength = SAI_FRAME_SIZE / 2 - 1;
SAI_FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
SAI_FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
SAI_FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
SAI_FrameInit.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
SAI_FrameInit.SlotInit.SlotNumber = 2;
SAI_FrameInit.SlotInit.FirstBitOffset = SAI_FIRSTBIT_MSB;
SAI_FrameInit.SlotInit.SlotActive = SAI_SLOTACTIVE_0 | SAI_SLOTACTIVE_1;
HAL_SAI_FrameInit(&SAI_Handle, &SAI_FrameInit);
}
int main(void)
{
// 初始化HAL库
HAL_Init();
// 配置SAI时钟
SAI_ClockConfig();
// 配置SAI
SAI_Config();
// 启动SAI
HAL_SAI_Start(&SAI_Handle, SAI_MODEMASTER_TX);
while (1)
{
// 主程序
}
}
```
该程序首先配置了SAI的时钟,然后配置了SAI的参数和句柄,最后启动了SAI。在主程序中可以进行音频数据的处理和传输。
阅读全文