STM32F401的SPI3使用DMA的初始化程序
时间: 2024-02-16 16:03:40 浏览: 150
SPI 初始化程序
以下是使用STM32F401的SPI3和DMA的初始化程序示例:
```c
#include "stm32f4xx.h"
#include <stdlib.h>
#define SPI_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_15)
#define SPI_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_15)
uint8_t SPI3_Buffer_Tx[256];
uint8_t SPI3_Buffer_Rx[256];
void RCC_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
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(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI3);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
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_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_Init(DMA1_Stream0, &DMA_InitStructure);
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);
}
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_8b;
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_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI3, &SPI_InitStructure);
SPI_Cmd(SPI3, ENABLE);
}
void DMA_SendData(uint8_t* pBuffer, uint32_t BufferSize)
{
DMA_Cmd(DMA1_Stream5, DISABLE);
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)pBuffer;
DMA_InitStructure.DMA_BufferSize = BufferSize;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
DMA_Cmd(DMA1_Stream5, ENABLE);
}
void DMA_ReceiveData(uint8_t* pBuffer, uint32_t BufferSize)
{
DMA_Cmd(DMA1_Stream0, DISABLE);
DMA_DeInit(DMA1_Stream0);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI3->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)pBuffer;
DMA_InitStructure.DMA_BufferSize = BufferSize;
DMA_Init(DMA1_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA1_Stream0, ENABLE);
}
void SPI3_Init(void)
{
RCC_Configuration();
GPIO_Configuration();
SPI_Configuration();
DMA_Configuration();
}
int main(void)
{
SPI3_Init();
while (1)
{
// 发送数据
SPI_CS_LOW();
DMA_SendData(SPI3_Buffer_Tx, 256);
while (DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_TCIF5) == RESET);
DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5);
SPI_CS_HIGH();
// 接收数据
SPI_CS_LOW();
DMA_ReceiveData(SPI3_Buffer_Rx, 256);
while (DMA_GetFlagStatus(DMA1_Stream0, DMA_FLAG_TCIF0) == RESET);
DMA_ClearFlag(DMA1_Stream0, DMA_FLAG_TCIF0);
SPI_CS_HIGH();
}
}
```
以上是SPI3和DMA的初始化程序示例,包括DMA发送和接收数据缓冲区,以及SPI3的初始化和GPIO的配置。注意SPI3和DMA的配置可能需要根据您的具体硬件和应用程序进行微调,但这应该是一个很好的起点。
阅读全文