gpt4 book ai didi

spi - STM32f4 SPI DMA接收

转载 作者:行者123 更新时间:2023-12-04 18:04:07 29 4
gpt4 key购买 nike

我在 STM32F4 Discovery 上安装了 STM32F4407VGT6 Controller 。我尝试使用 SPI + DMA 从 AD7683 ADC 读取数据,但 DMA 接收缓冲区始终为空(全为零)。在轮询模式下,一切正常,但我必须读取一个 16 位样本值作为 3x 8 位 SPI 值并使用位移位。这也许就是问题所在。我的采样频率是 48 kHz,并且在每个周期内必须读取三个 spi 值以获得一个 ADC 样本。

AD7683时序图在datasheet中,第 5 页。

引脚上的 SPI 通信正常。这是分析仪的屏幕:pic

有谁知道如何解决这个问题,或者问题出在哪里?

提前致谢。

这是我的代码:

#define DMAbufferSizeRx 3
__IO uint8_t DMAbufferRx[DMAbufferSizeRx];

#define DMAbufferSizeTx 1
__IO uint8_t DMAbufferTx[DMAbufferSizeTx] ;

void DMAconfig(void)
{

NVIC_InitTypeDef NVIC_InitStructure;

/* Enable the DMA Stream IRQ Channel */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);





DMA_InitTypeDef DMA_InitStructure;

DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;

DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(SPI3->DR));
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;




// Configure Tx DMA
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) &DMAbufferTx[0];
DMA_InitStructure.DMA_BufferSize = DMAbufferSizeTx;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_Cmd(DMA1_Stream5, DISABLE);
while (DMA1_Stream5->CR & DMA_SxCR_EN);
DMA_Init(DMA1_Stream5, &DMA_InitStructure);


/* Configure Rx DMA */
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) &DMAbufferRx[0];
DMA_InitStructure.DMA_BufferSize = DMAbufferSizeRx;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_Cmd(DMA1_Stream0, DISABLE);
while (DMA1_Stream0->CR & DMA_SxCR_EN);
DMA_Init(DMA1_Stream0, &DMA_InitStructure);



DMA_ITConfig(DMA1_Stream0, DMA_IT_TC , ENABLE); //| DMA_IT_HT

/* Enable the DMA channel */

DMA_ClearFlag(DMA1_Stream0, DMA_FLAG_FEIF0|DMA_FLAG_DMEIF0|DMA_FLAG_TEIF0|DMA_FLAG_HTIF0|DMA_FLAG_TCIF0);
DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_FEIF5|DMA_FLAG_DMEIF5|DMA_FLAG_TEIF5|DMA_FLAG_HTIF5|DMA_FLAG_TCIF5);


DMA_Cmd(DMA1_Stream0, ENABLE); // Enable the DMA SPI TX Stream
DMA_Cmd(DMA1_Stream5, ENABLE); // Enable the DMA SPI RX Stream


// Enable the SPI Rx/Tx DMA request
SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Rx, ENABLE);
SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE);

SPI_Cmd(SPI3, ENABLE);

}






void DMA1_Stream0_IRQHandler(void)
{
/* Test on DMA Stream Transfer Complete interrupt */
if(DMA_GetITStatus(DMA1_Stream0, DMA_IT_TCIF0))
{
/* Clear DMA Stream Transfer Complete interrupt pending bit */
DMA_ClearITPendingBit(DMA1_Stream0, DMA_IT_TCIF0);

// here is buffer still empty....
}
}






void SPIconfig()
{

SPI_InitTypeDef SPI_InitStructure;

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_DataSize = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;

SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_CRCPolynomial = 0;

SPI_Init(SPI3, &SPI_InitStructure);
SPI_CalculateCRC(SPI3, DISABLE);
SPI_Cmd(SPI3,ENABLE);
}




void RCCenable(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_DMA1, ENABLE);
}






void GPIOconfig(void)
{

GPIO_InitTypeDef GPIO_InitDef;

GPIO_InitDef.GPIO_Pin = REG_ON_OFF | ADC_DRIVER_ON_OFF | GPIO_Pin_6;
GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOE, &GPIO_InitDef);



GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = SDATA | MOSI | SCLK;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI3);



GPIO_InitStructure.GPIO_Pin = CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_SetBits(GPIOD,CS);
}




void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

GPIO_ToggleBits(GPIOD,CS);

}
}



void TIM2_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);


TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

TIM_TimeBaseStructure.TIM_Period = 875;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

TIM_Cmd(TIM2, ENABLE);
}

最佳答案

看起来您将 DMA_ModeDMA_MemoryDataSizeDMA_MemoryInc 字段初始化了两次。这是无用的 - 只保存最后一条评论。

此外,您的 DMA_PeripheralDataSizeDMA_MemoryDataSize 不匹配。

你没有附加主函数,所以我看不到初始化函数调用顺序。 DMAconfig() 必须早于 SPIconfig() 调用。

SPIconfig 处有下一个错误:

1) 我想你猜到了,你必须纠正什么。

SPI_InitStructure.SPI_DataSize = SPI_FirstBit_MSB;

2)改变

SPI_InitStructure.SPI_NSS     = SPI_NSS_Soft |
SPI_NSSInternalSoft_Set;

SPI_init.SPI_NSS = SPI_NSS_Soft;

最后像这样初始化:

SPI_Init(SPI3, &SPI_init);
SPI_SSOutputCmd(SPI3, ENABLE); //!!!!
SPI_Cmd(SPI3, ENABLE);
SPI_NSSInternalSoftwareConfig(SPI3, SPI_NSSInternalSoft_Set); //!!!!
SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE);

如果您还没有解决问题,希望它能对您有所帮助。

关于spi - STM32f4 SPI DMA接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30077785/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com