STM32F1 系列 SPI3 使用DMA 发送和 接收

关于STM32F1 的SPI 操作讲解已经很普遍了,这里分享一下STM32F1 系列 SPI3 使用DMA 发送和接收,关于SPI3 DMA发送和 接收大多数所都是在中断中 执行的,其实原理一样都需要死循环,等待数据发送完毕后在执行下一步操作。

// 初始化SPI3
void SPI3_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	SPI_InitTypeDef SPI_InitStructure;
	DMA_InitTypeDef DMA_InitStructure;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //禁止JTAG功能(保留SWD下载口)
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);  

	GPIO_InitStructure.GPIO_Pin =GPIO_Pin_15; //PA15 =1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	PAout(15)=1; 
	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_2;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(SPI3, &SPI_InitStructure);
	//使能DMA发送
	DMA_DeInit(DMA2_Channel2); 
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI3->DR; 
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 
	DMA_InitStructure.DMA_BufferSize = 1024; 
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; 
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 
	DMA_InitStructure.DMA_Mode =   DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; 
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 
	DMA_Init(DMA2_Channel2, &DMA_InitStructure); 
	//使能DMA接收
	DMA_DeInit(DMA2_Channel1);
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI3->DR;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA2_Channel1, &DMA_InitStructure);
	SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Rx, ENABLE);
	SPI_I2S_DMACmd(SPI3,SPI_I2S_DMAReq_Tx,ENABLE);
	SPI_Cmd(SPI3, ENABLE);
}
// 速度设置
void SPI3_SetSpeed(u8 SpeedSet)
{
	SPI3->CR1&=0XFFC7; 
	SPI3->CR1|=SpeedSet;
	SPI_Cmd(SPI3,ENABLE); 
} 
// CPU发送
uint8_t SPI3_ReadWriteByte(u8 TxData)
{		
	while((SPI3->SR&1<<1)==0){;	}
	SPI3->DR=TxData;	 	  		 
	while((SPI3->SR&1<<0)==0){;	}	 
	while(SPI3->SR&(1<<7)){;}
	return SPI3->DR;
}
// SPI3 DMA发送
void DMA_SPI3_TX(uint8_t *buffer,uint16_t len)
{
	DMA2->IFCR |=(0xf<<4);
	DMA2_Channel2->CNDTR=len; //设置要传输的数据长度
	DMA2_Channel2->CMAR=(u32)buffer; //设置RAM缓冲区地址
	DMA2_Channel2->CCR|=0x1;
	while(!(DMA2->ISR&(1<<5))){;}
	while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET){;}
	DMA2_Channel2->CCR &=(uint32_t)~0x1;
}
// SPI3 DMA 接收
void DMA_SPI3_RX(uint8_t *buffer,uint16_t len)
{
	SPI3->CR1|=SPI_Direction_2Lines_RxOnly;
	buffer[0] = SPI3->DR;// 读取一次数据
	DMA2->IFCR |=(0xf<<0);
	DMA2_Channel1->CNDTR=len; //设置要传输的数据长度
	DMA2_Channel1->CMAR=(u32)buffer; //设置RAM缓冲区地址
	DMA2_Channel1->CCR|=0x1;
	while(!(DMA2->ISR&(1<<1))){;}
	DMA2_Channel1->CCR &=(uint32_t)~0x1;
	SPI3->CR1&=~SPI_Direction_2Lines_RxOnly;
}

 

你可能感兴趣的:(stm32)