本帖最后由 lisn1234 于 2013-6-6 21:33 编辑
请教高手一个问题:
最近在用STM32F407ZG的片子,用到了SPI的DMA模式,来采集外部数据的。
原来是用103的芯片,程序可以运行,才改的407,配置什么的都有小的改动(固件库不太一样了)
但是不知道是不是设置有问题,我的程序总是到了
while(DMA_GetFlagStatus(DMA2_Stream2,DMA_FLAG_TCIF2)==RESET);
这一行停住,不会继续往下运行
代码如下,还请大家帮忙看看,小弟鞠躬了~
void rt_hw_adc_init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* DMA2 clock source enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
/* SPI1、GPIOA clock source enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
/*SPI1 port_init*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure SPI1 pins: NSS ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT ;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_Enable; //启用ADC128S102
/* SPI1 Configuration (Master, 16 MBaud) --------------------------------*/
SPI_I2S_DeInit(SPI1);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
DMA_DeInit(DMA2_Stream2);//只用到了SPI1_RX
// DMA_StructInit( &DMA_InitStructure);
DMA_InitStructure.DMA_BufferSize = SPI1_BufferSize;
DMA_InitStructure.DMA_Channel = DMA_Channel_3;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&SPI1_Buffer_Rx;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI1_DR_Address;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_Init(DMA2_Stream2, &DMA_InitStructure);
/* Enable DMA2 Stream2 */
DMA_Cmd(DMA2_Stream2,ENABLE);
/* Enable SPI1 Rx DMA Request */
SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Rx,ENABLE);
DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_FEIF2 | DMA_FLAG_DMEIF2 | DMA_FLAG_TEIF2 | DMA_FLAG_HTIF2 | DMA_FLAG_TCIF2);
}
/*******************************************************************************/
void adc(void)
{
rt_hw_adc_init();
while(1)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,ADC_CHANNEL3);
re = rt_mq_send(&mq3, &SPI1_Buffer_Rx[2], 2);
//Waiting for the DMA2 transfer complete flag to be setted
while(DMA_GetFlagStatus(DMA2_Stream2,DMA_FLAG_TCIF2)==RESET);//就是总卡在这里
DMA_ClearFlag(DMA2_Stream2,DMA_FLAG_TCIF2);rt_kprintf("flag cleared!\r\n");
rt_thread_delay( 45 );
}
}
下面是103芯片运行成功的代码
static void rt_hw_adc_init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* DMA1 clock source enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* SPI1、GPIOA and AFIO clock source enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 |RCC_APB2Periph_GPIOA
|RCC_APB2Periph_AFIO,ENABLE);
/*SPI1 port_init*/
/* Configure SPI1 pins: SCK, NSS and MOSI ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure SPI1 pins: NSS ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_Enable; //启用ADC128S102
/* SPI1 Configuration (Master, 4.5 MBaud) --------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
/*DMA1 Channel2 Configuration*/
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&SPI1_Buffer_Rx;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = SPI1_BufferSize;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
/* Enable DMA1 Channel2 */
DMA_Cmd(DMA1_Channel2,ENABLE);
/* Enable SPI1 Rx DMA Request */
SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Rx,ENABLE);
}
void adc(void)
{
rt_hw_adc_init();
while(1)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,ADC_CHANNEL3);
re = rt_mq_send(&mq3, &SPI1_Buffer_Rx[2], 2);
//Waiting for the DMA1 transfer complete flag to be setted
while(!DMA_GetFlagStatus(DMA1_FLAG_TC2));
DMA_ClearFlag(DMA1_FLAG_TC2);
rt_thread_delay( 45 );
}
}
|
大神,咨询一下你这个卡在 while(DMA_GetFlagStatus(DMA2_Stream2,DMA_FLAG_TCIF2)==RESET);//就是总卡在这里 解决了吗?能说一下方法吗?