在线时间6 小时
UID364415
ST金币0
蝴蝶豆0
注册时间2007-11-23
初级会员
- 最后登录
- 1970-1-1
|
a0a.1 0b0c
新手请教 在使用STM32F407的DMA向外设SPI2发送数据时 使能DMA的半传输 与 传输完成中断
但是当DMA进入中断处理函数后 DMA的FEIF TCIF HTIF 都被置位
中断进入后发现SXNDTI内的值位8(DMA需要发送的数据个数为16) 在中断中关闭DMA 在半传输的 判断语句中再将DMA打开
结果 中断退出后 很短时间内DMA的FEIF TCIF HTIF 会再次被置位
//配置SPI2 DMA传输
//配置SPI2 DMA传输
void DMA_SPI_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_DMA1,ENABLE); //使能GPIOA的AHB1时钟RCC_APB2Periph_AFIO|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
GPIO_PinAFConfig(GPIO_PORT_KKPJ_SPIX,GPIO_PinSource13, GPIO_AF_SPI2);
GPIO_PinAFConfig( GPIO_PORT_KKPJ_SPIX ,GPIO_PinSource14, GPIO_AF_SPI2);
GPIO_PinAFConfig( GPIO_PORT_KKPJ_SPIX ,GPIO_PinSource15, GPIO_AF_SPI2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIO_PORT_XJ128_SPIX, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//设置SPI1 SPI2
SPI_I2S_DeInit(SPI2);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex ;//;SPI_Direction_1Line_Tx // SPI设置为双线双向全双工 ;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // 设置为主SPI ;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // SPI发送接收8位帧结构 ;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // Low时钟悬空低 ;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // 数据捕获于第二个时钟沿 ;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // 内部NSS信号有SSI位控制 ;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // 波特率预分频值为4 ;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // 数据传输从MSB位开始 ;
SPI_InitStructure.SPI_CRCPolynomial = 7; // 定义了用于CRC值计算的多项式 7。
SPI_Init(SPI2, &SPI_InitStructure); // 初始化外设SPI1寄存器 ;
DMA_InitStructure.DMA_Channel = DMA_Channel_0; //DMA通道4 RM0090手册->165
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(SPI2->DR)); //外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)SPI1_Buffer_Tx; //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //传输方向
DMA_InitStructure.DMA_BufferSize = 0; //设置DMA传输时缓冲区大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DAM外设地址递增模式,因为目前就一个外设,故此处设置为禁止
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //8位数据位宽度
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //DMA_Mode_Circular; //这里必须设置为这个模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA_Priority_VeryHigh; //DMA_Priority_High; //设置优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //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_Stream4, &DMA_InitStructure);
//Enable DMA2 Channel3 Tx Interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级设置,值越大,表示优先级越低
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级设置,值越大,表示优先级越低
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearFlag(DMA1_Stream4,DMA_FLAG_HTIF4); //清除数据传输一半中断标志DMA_FLAG_HTIF3
DMA_ClearFlag(DMA1_Stream4,DMA_FLAG_TCIF4);//清除数据传输完成中断标志
DMA_ITConfig(DMA1_Stream4,DMA_IT_TC | DMA_IT_HT,ENABLE);
//使能 SPI2 DMA传输
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
}
void DMA1_Stream4_IRQHandler(void)
{ u16 ty=3;
DMA1_Stream4 ->CR &= 0xFFFFFFFE; //除能DMA1_Stream3
while( DMA1_Stream4 ->CR & 0x00000001);//确保DMA可以被设置
//等待 SPI 数据寄存器空
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_Cmd(SPI2, DISABLE);
//若数据传输一半
if(DMA_GetFlagStatus( DMA1_Stream4 ,DMA_FLAG_HTIF4))
{
DMA_ClearFlag(KKHG_DMA_Stream,DMA_FLAG_HTIF4);
SPI_Cmd(SPI2, ENABLE);
DMA1_Stream4 ->CR |= (uint32_t)DMA_SxCR_EN;//打开DMA的数据传输
}
if(DMA_GetFlagStatus( DMA1_Stream4 ,DMA_FLAG_TCIF4))
//若数据传输完成 数据传输完成后要关闭DMA传输 若想再一次使用DMA传输就必须 重新写入传输数目
{
DMA_ClearFlag( DMA1_Stream4 ,DMA_FLAG_TCIF4);
}
}
反复搞了几天 实在没辙了 还请 大侠看下
|
|