Home > other >  Ask a question of SPI - DMA
Ask a question of SPI - DMA

Time:11-14

Application:
STM32F407, SPI for receiving data from the machine, using DMA receiving, is expected to receive 512 data generated when a DMA interrupt, data processing, below is my code, into don't interrupt, don't know why??? Answer:
Will interrupt SPI1_IRQn instead to break, but the data is wrong, get is a 16-bit fixed place!
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

SPI_Cmd (FPGA_SPI, DISABLE);
SPI_DeInit (FPGA_SPI);

RCC_AHB1PeriphClockCmd (FPGA_SPI_SCK_GPIO_CLK | FPGA_SPI_MISO_GPIO_CLK | FPGA_SPI_MOSI_GPIO_CLK | FPGA_CS_GPIO_CLK, ENABLE);

RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE);
RCC_AHB1PeriphResetCmd (RCC_AHB1Periph_DMA2, ENABLE);

GPIO_PinAFConfig (FPGA_SPI_SCK_GPIO_PORT FPGA_SPI_SCK_PINSOURCE, FPGA_SPI_SCK_AF);
GPIO_PinAFConfig (FPGA_SPI_MISO_GPIO_PORT FPGA_SPI_MISO_PINSOURCE, FPGA_SPI_MISO_AF);
GPIO_PinAFConfig (FPGA_SPI_MOSI_GPIO_PORT FPGA_SPI_MOSI_PINSOURCE, FPGA_SPI_MOSI_AF);

GPIO_InitStructure. GPIO_Pin=FPGA_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure. GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init (FPGA_SPI_SCK_GPIO_PORT, & amp; GPIO_InitStructure);
GPIO_InitStructure. GPIO_Pin=FPGA_SPI_MISO_PIN;
GPIO_Init (FPGA_SPI_MISO_GPIO_PORT, & amp; GPIO_InitStructure);

GPIO_InitStructure. GPIO_Pin=FPGA_SPI_MOSI_PIN;
GPIO_Init (FPGA_SPI_MOSI_GPIO_PORT, & amp; GPIO_InitStructure);

GPIO_InitStructure. GPIO_Pin=FPGA_CS_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_Init (FPGA_CS_GPIO_PORT, & amp; GPIO_InitStructure);

SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure. SPI_Mode=SPI_Mode_Slave;
SPI_InitStructure. SPI_DataSize=SPI_DataSize_8b;
SPI_InitStructure. SPI_CPOL=SPI_CPOL_Low;
SPI_InitStructure. SPI_CPHA=SPI_CPHA_1Edge;
SPI_InitStructure. SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStructure. SPI_NSS=SPI_NSS_Hard;//SPI_NSS_Soft;
SPI_InitStructure. SPI_CRCPolynomial=7;
SPI_Init (FPGA_SPI, & amp; SPI_InitStructure);

NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);
NVIC_InitStructure. NVIC_IRQChannel=DMA2_Stream2_IRQn;//SPI1_IRQn
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure. NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure. NVIC_IRQChannelCmd=ENABLE;
NVIC_Init (& amp; NVIC_InitStructure);

SPI_DMACmd (FPGA_SPI SPI_DMAReq_Rx, ENABLE);
SPI_Cmd (FPGA_SPI, ENABLE);

While (DMA_GetCmdStatus (SPI_DMA_STREAM)!=DISABLE) {}
DMA_DeInit (SPI_DMA_STREAM);
DMA_InitStructure. DMA_Channel=SPI_DMA_CHANNEL;
DMA_InitStructure. DMA_PeripheralBaseAddr=(u32) & amp; FPGA_SPI - & gt; DR.
DMA_InitStructure. DMA_Memory0BaseAddr=(u32 spi_rv_buf);
DMA_InitStructure. DMA_DIR=DMA_DIR_PeripheralToMemory;
DMA_InitStructure. DMA_BufferSize=SPI_RV_SIZE;
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_High;

DMA_Init (SPI_DMA_STREAM, & amp; DMA_InitStructure);

DMA_ITConfig (SPI_DMA_STREAM DMA_IT_TC, ENABLE);
DMA_Cmd (SPI_DMA_STREAM, ENABLE);

CodePudding user response:

Void DMA2_Stream2_IRQHandler (void) {
If (DMA_GetFlagStatus (SPI_DMA_STREAM, DMA_FLAG_TCIF2)==SET)
{
DMA_ClearITPendingBit (SPI_DMA_STREAM DMA_FLAG_TCIF2);
}
}
  • Related