STM32F407标准库,SPI DMA中断方式收发数据,怎么也调不通,一进DMA收发中断程序死了~(等待方式没有问题),下面是代码,请高手哥哥姐姐帮忙,谢谢!
//STM32F407标准库,SPI DMA中断方式收发数据,怎么也调不通,一进DMA收发中断程序就...
#define SPI1_RX_LEN 2000
#define SPI1_TX_LEN 2000
#define SPI1_DR_ADDR (uint32_t)0x4001300C
unsigned char SPI1_RX_BUFFER[SPI1_RX_LEN]={0};
unsigned char SPI1_TX_BUFFER[SPI1_TX_LEN]={0};
//函数名:初始化SPI接口
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); //使能SPI1时钟
GPIO_InitStructure.GPIO_Pin = SPI1_NSS_Pin; //IO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //标准功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(SPI1_NSS_GPIO_Port, &GPIO_InitStructure); //初始化
SPI1_NSS_Set(); //NSS高
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; //PA5\6\7复用功能输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //PB3复用为 SPI1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //PB4复用为 SPI1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1); //PB5复用为 SPI1
//只针对SPI口初始化
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE); //复位SPI1
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE); //停止复位SPI1
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //串行同步时钟的空闲状态为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //定义波特率预分频的值:波特率预分频值为2:42M;4:21M;8:10.5M;16:5.25M
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
SPI_Init(SPI1, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
spi1_dma_init(41,41); //DMA初始化
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
}
void spi1_dma_init(unsigned short RXSize,unsigned short TXSize)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*DMA2*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
/* DMA RX config */
DMA_InitStructure.DMA_Channel = DMA_Channel_3; //DMA 通道
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI1_DR_ADDR; //外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SPI1_RX_BUFFER; //接收缓冲区(内存中的有一个数组)
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //DMA 传输方向
DMA_InitStructure.DMA_BufferSize = RXSize; //DMA 传输的数量 这个后期还可以再改
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址自增 取消
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址自增 使能
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //传输的 单位 (byte 8bit)
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //传输的 单位 (byte 8bit)
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //普通模式 传输完成一次就自动结束
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //优先级 中等
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //不使用 FIFO
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; //
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream2, &DMA_InitStructure); //初始化
/* DMA TX Config */
DMA_InitStructure.DMA_Channel = DMA_Channel_3; //DMA 通道
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI1_DR_ADDR; //外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SPI1_TX_BUFFER; //接收缓冲区(内存中的有一个数组)
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //DMA传输方向
DMA_InitStructure.DMA_BufferSize = TXSize; //DMA传输的数量 这个后期还可以再改
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址自增 取消
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址自增 使能
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //传输的 单位 (byte 8bit)
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //传输的 单位 (byte 8bit)
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(DMA2_Stream3, &DMA_InitStructure); //初始化
//DMA中断方式发送
DMA_ITConfig(DMA2_Stream3, DMA_IT_TC, ENABLE); //DMA发送中断开
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearITPendingBit(DMA2_Stream3, DMA_IT_TCIF3);
//DMA中断方式接收
DMA_ITConfig(DMA2_Stream2, DMA_IT_TC, ENABLE); //DMA接收中断开
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearITPendingBit(DMA2_Stream2, DMA_IT_TCIF2);
}
//读写数据调用此函数
void spi1_trans(unsigned char *rx_buf, unsigned char *tx_buf, unsigned short length)
{
DMA_Cmd(DMA2_Stream2, DISABLE); //关闭DMA
DMA_Cmd(DMA2_Stream3, DISABLE); //关闭DMA
DMA_SetCurrDataCounter(DMA2_Stream2, (unsigned short)length); //设置DMA的传输参数
DMA_SetCurrDataCounter(DMA2_Stream3, (unsigned short)length); //设置DMA的传输参数
DMA2_Stream2->M0AR = (uint32_t)rx_buf; //设置内存的起始地址
DMA2_Stream3->M0AR = (uint32_t)tx_buf; //设置内存的起始地址
SPI1->DR;
//while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //等待先前的发送完毕
DMA_Cmd(DMA2_Stream2, ENABLE); //打开DMA开始发送
DMA_Cmd(DMA2_Stream3, ENABLE); //打开DMA开始接收
//while( DMA_GetFlagStatus(DMA2_Stream3, DMA_FLAG_TCIF3) == RESET); //等待数据发送完
//while( DMA_GetFlagStatus(DMA2_Stream2, DMA_FLAG_TCIF2) == RESET); //等待
//DMA_Cmd(DMA2_Stream2, DISABLE); //发送完关闭DMA
//DMA_Cmd(DMA2_Stream3, DISABLE); //发送完关闭DMA
//DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_TCIF2); //清楚标志位
//DMA_ClearFlag(DMA2_Stream3, DMA_FLAG_TCIF3); //清楚标志位
}
//发送中断
void DMA2_Stream2_IRQHandler(void)
{
// 清除DMA发送完成标志
if(DMA_GetITStatus(DMA2_Stream2, DMA_IT_TCIF2)!= RESET)
{
// 清除DMA发送完成标志
DMA_ClearITPendingBit(DMA2_Stream2, DMA_IT_TCIF2);
DMA_Cmd(DMA2_Stream2, DISABLE);
u1_printf("S2_TX_IRQ\r\n");
}
// 传输错误中断
if(DMA_GetITStatus(DMA2_Stream2, DMA_IT_TEIF2) != RESET)
{
DMA_ClearITPendingBit(DMA2_Stream2, DMA_IT_TEIF2);
u1_printf("S2_err_TEIF2\r\n");
}
}
//接收中断
void DMA2_Stream3_IRQHandler(void)
{
// 清除DMA发送完成标志
if(DMA_GetITStatus(DMA2_Stream3, DMA_IT_TCIF3)!= RESET)
{
DMA_ClearITPendingBit(DMA2_Stream3, DMA_IT_TCIF3);
DMA_Cmd(DMA2_Stream3, DISABLE);
u1_printf("S3_RX_IRQ\r\n");
}
// 传输错误中断
if(DMA_GetITStatus(DMA2_Stream3, DMA_IT_TEIF3) != RESET)
{
DMA_ClearITPendingBit(DMA2_Stream3, DMA_IT_TEIF3);
u1_printf("S3_err_TEIF3\r\n");
}
}
|