想用STM32通过DMA,获得外设SPI的数据。 SPI1->DR寄存器里面有值,但是DMA读到的数一直是0,这是什么情况啊?附上代码,如果哪里有错误请各位帮忙指正。另外,我只用STm32通过SPI获得的数据是正确的。
void DMA_Configuration_SPI1_RX(void) //DMA初始化
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&SPI_Buffer_Rx;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
}
//SPI口初始化
void SPIx_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure SPI1 pins: SCK, MISO 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 I/O for Flash Chip select */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //SPI CS
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Deselect the FLASH: Chip Select high */
GPIO_SetBits(GPIOC,GPIO_Pin_5);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
SPI_Cmd(SPI1, DISABLE); //不使能SPI外设 自己加上
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //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_High; //选择了串行时钟的稳态:时钟悬空高
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //数据捕获于第二个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //定义波特率预分频的值:波特率预分频值为256
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外设
SPIx_ReadWriteByte(0xff);//启动传输
}
//SPIx 读写一个字节
//TxData:要写入的字节
//返回值:读取到的字节
u8 SPIx_ReadWriteByte(u8 TxData)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_5); //CS片选为 低电平有效的逻辑输入
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //检查指定的SPI标志位设置与否:发送缓存空标志位
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, TxData); //通过外设SPIx发送一个数据
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //检查指定的SPI标志位设置与否:接受缓存非空标志
/* Return the byte read from the SPI bus */
GPIO_SetBits(GPIOC,GPIO_Pin_5);
return SPI_I2S_ReceiveData(SPI1); //返回通过SPIx最近接收的数据
}
/********读取AD7705第一通道数据************/
u16 ReadTM7705(u8 n)
{
unsigned long Ret = 0;
TM7705_Init1();
SPIx_ReadWriteByte(0x38);//设置读当前通道数据
while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4)== 1)
{
;
}
MYDMA_Enable(DMA1_Channel4);
Ret = DMA_read();
}
return Ret;
}
void MYDMA_Enable(DMA_Channel_TypeDef*DMAy_Channelx)
{
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA1_Channel4, DISABLE ); //关闭USART1 TX DMA1 所指示的通道
DMA_Cmd(DMA1_Channel4, ENABLE); //使能USART1 TX DMA1 所指示的通道
}
u16 DMA_read(void)
{
u16 data;
data = SPI_Buffer_Rx;
return data;
} |