STM32F207(一)驱动:SPI从机+DMA接收和发送

硬件说明

使用STM32F207的SPI3配置为从机并使用DMA方式,SPI介绍网上一大堆这里就不介绍了,话不多说直接开始代码。
引脚说明:

引脚功能复用
GPIOC_12SPI3_MOSIAF6
GPIOC_11SPI3_MISOAF6
GPIOC_10SPI3_SCKAF6
GPIOA_15SPI3_NSSAF6
功能DMA通道
SPI3_RxDMA1 Stream 0 Channel 0 / DMA1 Stream 2 Channel 0
SPI3_TxDMA1 Stream 5 Channel 0 / DMA1 Stream 7 Channel 0

软件驱动

GPIO配置:

GPIO_InitTypeDef   GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

GPIO_InitStructure.GPIO_Pin   =	GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;    
GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure); 

//引脚复用
GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);

SPI3配置:

//SPI初始化
SPI_InitTypeDef    SPI_InitStruct;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);

RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3,ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3,DISABLE);

SPI_InitStruct.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode              = SPI_Mode_Slave;//从机
SPI_InitStruct.SPI_DataSize          = SPI_DataSize_8b; 
SPI_InitStruct.SPI_CPOL              = SPI_CPOL_High;
SPI_InitStruct.SPI_CPHA              = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_NSS               = SPI_NSS_Soft; 
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStruct.SPI_FirstBit          = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial     = 7;
SPI_Init(SPI3, &SPI_InitStruct);   

SPI_Cmd(SPI3, ENABLE);

具体配置功能可以自己查找。

DMA配置:

DMA_InitTypeDef    DMA_InitStructure;
NVIC_InitTypeDef   NVIC_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);

//DMA初始化SPI3_Tx:DAM1 Stream 5 Channel 0
DMA_DeInit(DMA1_Stream5);
while (DMA_GetCmdStatus(DMA1_Stream5) != DISABLE);

DMA_InitStructure.DMA_Channel            = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI3->DR;
DMA_InitStructure.DMA_Memory0BaseAddr    = (uint32_t)&SendH743Data[0];//todo
DMA_InitStructure.DMA_DIR                = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize         = sizeof(SendH743Data);//todo
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_FIFOMode           = DMA_FIFOMode_Disable;         
DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single; 
DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single; 
DMA_Init(DMA1_Stream5, &DMA_InitStructure);

//初始化DMA1_Stream5_IRQn 中断
NVIC_InitStructure.NVIC_IRQChannel                   = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
NVIC_Init(&NVIC_InitStructure);

SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, DISABLE);
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);

DMA_Cmd(DMA1_Stream5, DISABLE);//先关闭DMA

//DMA初始化SPI3_Rx:DMA1 Stream 2 Channel 0
DMA_DeInit(DMA1_Stream2);
while (DMA_GetCmdStatus(DMA1_Stream2) != DISABLE);

DMA_InitStructure.DMA_Channel            = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (DWORD)&SPI3->DR;
DMA_InitStructure.DMA_Memory0BaseAddr    = (uint32_t)&ReceH743Data[0];//todo
DMA_InitStructure.DMA_DIR                = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize         = sizeof(ReceH743Data);//todo
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_FIFOMode           = DMA_FIFOMode_Disable;         
DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream2, &DMA_InitStructure);

//初始化DMA1_Stream2_IRQn 中断
NVIC_InitStructure.NVIC_IRQChannel                   = DMA1_Stream2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
NVIC_Init(&NVIC_InitStructure);

SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Rx, DISABLE);
DMA_ITConfig(DMA1_Stream2, DMA_IT_TC, ENABLE);     
DMA_Cmd(DMA1_Stream2, DISABLE);

说明:
(1)使用DAM1 Stream2 Channel0作为SPI3 Rx的DMA通道;
(2)使用DAM1 Stream5 Channel0作为SPI3 Tx的DMA通道;
(3)DMA接收和发送的DMA_Memory0BaseAddr需要根据自己的缓冲进行引用;
(4)DMA接收和发送的DMA_BufferSize根据自己的协议长度而定,因为SPI没有像Usart那样的空闲中断不能做到接受任意长度的数据,所以可以定义一个比数据更长的缓冲区例如数据长度小于200字节,那么就可以将缓冲区定义为200不够的数据发送0x00即可;
(5)这里使用的通信逻辑是先打开接收DMA,接收完成关闭接收DMA再打开发送DMA,发送完成后再打开接受DMA,如此循环;

======================以上组合就是完整的驱动配置 ======================

配置DMA为接收:

/*
    配置SPI DAM接收模式
*/
void api_SPI3_Enable_RX_DMA(void)
{
    DMA_Cmd(DMA1_Stream5, DISABLE);//关闭SPI3发送DMA:DMA1_Stream5
    SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, DISABLE);
    
    DMA_Cmd(DMA1_Stream2, ENABLE);//打开SPI3接收DMA:DMA1_Stream2
    SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Rx, ENABLE);
    
//    DMA_ClearFlag(DMA1_Stream2,  DMA_FLAG_FEIF2);
//    DMA_ClearFlag(DMA1_Stream2, DMA_FLAG_DMEIF2);
//    DMA_ClearFlag(DMA1_Stream2,  DMA_FLAG_TEIF2);
//    DMA_ClearFlag(DMA1_Stream2,  DMA_FLAG_HTIF2);
//    DMA_ClearFlag(DMA1_Stream2,  DMA_FLAG_TCIF2);
}

配置DMA为发送:

/*
    配置SPI DAM发送模式
*/
void api_SPI3_Enable_TX_DMA(void)
{
    DMA_Cmd(DMA1_Stream2, DISABLE);//关闭SPI3接收DMA:DMA1_Stream2
    SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Rx, DISABLE);
    
    DMA_Cmd(DMA1_Stream5, ENABLE);//打开SPI3发送DMA:DMA1_Stream5
    SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE);
       
//    DMA_ClearFlag(DMA1_Stream5,  DMA_FLAG_FEIF5);
//    DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_DMEIF5);
//    DMA_ClearFlag(DMA1_Stream5,  DMA_FLAG_TEIF5);
//    DMA_ClearFlag(DMA1_Stream5,  DMA_FLAG_HTIF5);
//    DMA_ClearFlag(DMA1_Stream5,  DMA_FLAG_TCIF5);              
}

中断及调用关系

函数调用:

api_InitSPItoARM();			//SPI3驱动初始化(就是之前的驱动初始化)
api_SPI3_Enable_RX_DMA();	//设置DMA为接收模式

中断调用:

//SPI3_Rx:DAM1 Stream 2 Channel 0
void DMA1_Stream2_IRQHandler(void)
{      
    if(DMA_GetFlagStatus(DMA1_Stream2, DMA_FLAG_HTIF2) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream2, DMA_FLAG_HTIF2);        
    }
    
    if(DMA_GetFlagStatus(DMA1_Stream2, DMA_FLAG_TCIF2) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream2, DMA_FLAG_TCIF2);
       
        /* 说明:
         * 1、此处报文应该接收完整,因为已经进入DMA完成中断;
         * 2、在这里添加数据处理函数,例如:DealReceData();
         * 3、接收数据处理完清理接收缓冲区,memset();,并组包要发送的数据,等待主机发送SPI片选信号及时钟信号;
         * 4、设置DMA发送模式;
         */
        
        api_SPI3_Enable_TX_DMA();//接收完成使能SPI3-DMA发送
    }
    
    if(DMA_GetFlagStatus(DMA1_Stream2, DMA_FLAG_FEIF2) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream2, DMA_FLAG_FEIF2);        
    }
    
    if(DMA_GetFlagStatus(DMA1_Stream2, DMA_FLAG_DMEIF2) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream2, DMA_FLAG_DMEIF2);   
    }
    
    if(DMA_GetFlagStatus(DMA1_Stream2, DMA_FLAG_TEIF2) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream2, DMA_FLAG_TEIF2);    
    }
}

//SPI3_Tx:DAM1 Stream 5 Channel 0
void DMA1_Stream5_IRQHandler(void)
{
    if(DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_HTIF5) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_HTIF5);        
    }
  
    if(DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_TCIF5) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5);
    
    	/* 说明:
         * 1、此处报文应该发送完成,因为已经进入DMA完成中断;
         * 2、清理发送缓冲区,memset();
         * 3、设置DMA接受模式;
         */
        
        api_SPI3_Enable_RX_DMA();//发送完成使能SPI3-DMA接收
    }  
     
    if(DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_TEIF5) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TEIF5);        
    }
    
    if(DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_DMEIF5) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_DMEIF5);        
    }
    
    if(DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_FEIF5) != RESET)
    {
        DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_FEIF5);        
    }
}

以上为我使用的完整SPI3+DMA驱动程序,具体数据解析不需要分享,因为每个人要写功能不一,略尽绵薄之力,欢迎大家交流和批评,谢谢!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值