请教一个SPI-DMA的问题

XYZ_WAYY 2020-11-12 04:40:18
应用:
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);
...全文
10633 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
XYZ_WAYY 2020-11-16
已经找到原因了,不知道怎么回事,在配置的时候CR寄存器的BIT9设置成1了,这一位是外设地址递增模式的设置,现在好了,谢谢
  • 打赏
  • 举报
回复
DannyDeng123 2020-11-15
检查一下这几个参数: NVIC_IRQChannel DMA_MemoryInc DMA_BufferSize 最好的方法是调试看看DMA寄存器,DMA Size 有没有减到0,如果没有到0是不会进TC中断的。
  • 打赏
  • 举报
回复
XYZ_WAYY 2020-11-12
void DMA2_Stream2_IRQHandler(void){ if(DMA_GetFlagStatus(SPI_DMA_STREAM,DMA_FLAG_TCIF2) == SET) { DMA_ClearITPendingBit(SPI_DMA_STREAM,DMA_FLAG_TCIF2); } }
  • 打赏
  • 举报
回复
相关推荐
发帖
单片机/工控

2.7w+

社区成员

硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
帖子事件
创建了帖子
2020-11-12 04:40
社区公告
暂无公告