XYZ_WAYY 工程师  2020年11月12日
请教一个SPI-DMA的问题
应用:
STM32F407、SPI做从机接收数据、采用DMA接收,预期 接收到512个数据时产生一个DMA中断,进行数据处理,以下是我的代码,进不了中断,不知道为什么???求解答:
将中断改为SPI1_IRQn时可以进中断,但是数据不对,得到的是一个16位的固定置!
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, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = FPGA_SPI_MISO_PIN;
GPIO_Init(FPGA_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);

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

GPIO_InitStructure.GPIO_Pin = FPGA_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(FPGA_CS_GPIO_PORT, &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, &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(&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)&FPGA_SPI->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, &DMA_InitStructure);

DMA_ITConfig(SPI_DMA_STREAM, DMA_IT_TC, ENABLE);
DMA_Cmd(SPI_DMA_STREAM, ENABLE);
...全文
10531 点赞 收藏 3
写回复
3 条回复

还没有回复,快来抢沙发~

发动态
发帖子
单片机/工控
创建于2007-09-28

1.0w+

社区成员

2.8w+

社区内容

硬件/嵌入开发 单片机/工控
社区公告
暂无公告