STM32F030F4P6使用SPI1中的DMA方式传送数据,发送不出去,一直卡在 while( DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET ); 不知道为什么???
[mw_shl_code=c,true]//SPI1的初始化,里面调用了DMA1的初始化
void SPI1_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE); //使能GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); //使能SPI时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA
// NSS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //通信方式,全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //模式选择,主机模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //数据帧长度选择,8位
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //时钟极性,空闲态低电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //时钟相位,串行时钟第1个跳变沿采样数据
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //配置NSS引脚为软件模式,一般一主一从的情况下用此模式
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //设置SPI波特率预分频值
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输顺序,MSB在前
SPI_InitStructure.SPI_CRCPolynomial = 7; //设置CRC校验多项式
SPI_Init(SPI1, &SPI_InitStructure);
/* Initialize the FIFO threshold */
SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
//初始化DMA
SPI1_DMA_Init();
SPI_Cmd(SPI1, ENABLE);
}
//DMA1的初始化
//配置SPI1_RX的DMA通道2,SPI1_TX的DMA通道3
void SPI1_DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
//开启DMA1时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//初始化DMA接收通道
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR; //设置外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&SPI1_RX_Buff; //设置DMA存储器地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //设置DMA的传输方向,从外设读
DMA_InitStructure.DMA_BufferSize = 1; //设置DMA在传输时缓冲区的长度,开辟1个连续的DMA存储单元
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式,不执行外设地址增量模式,外设地址不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式,存储器地址增量模式,内存地址自增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //设置DMA在访问外设时每次操作的数据长度,外设数据宽度8bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //设置DMA在访问外设时每次操作的数据长度,存储器数据宽度8bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式,不执行循环操作
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //设置DMA的优先级别,通道优先级非常高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //设置DMA的2个memory中的变量互相访问,非存储器到存储器模式
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
//初始化DMA发送通道
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&SPI1_TX_Buff;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //从存储器读,外设作为DMA的目的端
DMA_InitStructure.DMA_BufferSize = 1;
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_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
//使能通道
DMA_Cmd(DMA1_Channel2, ENABLE);
DMA_Cmd(DMA1_Channel3, ENABLE);
//使能SPI DMA 的发送与接收
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
}
//发送/接受数据
uint8_t SPI1_DMA_SendByte( uint8_t Byte )
{
SPI1_TX_Buff = Byte;
SPI1_RX_Buff = 0;
//重置CNDTR传输数量计数器
DMA1_Channel2->CNDTR = 1 ; //传输数量设置为num个
DMA1_Channel3->CNDTR = 1 ; //传输数量设置为num个
//清除通道2、3标志位
DMA1->IFCR |= (0xff << 4);
//开启通道
DMA1_Channel3->CCR |= 1 << 0 ; //开启DMA通道3
DMA1_Channel2->CCR |= 1 << 0 ; //开启DMA通道2
while( DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET );// 等待通道3发送完成
while( DMA_GetFlagStatus(DMA1_FLAG_TC2) == RESET );
DMA1_Channel3->CCR &= ~( 1 << 0 ) ; //关闭DMA通道3
DMA1_Channel2->CCR &= ~( 1 << 0 ) ; //关闭DMA通道2
return SPI1_RX_Buff;
}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>