SPI为主机,发送数据,使用DMA来接收数据。可是DMA一直接收到的都是零,是不是SPI+DMA只能对八位数据操作?附上程序
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = SPI_NsPin;
GPIO_Init(SPI_NsPort,&GPIO_InitStructure);
GPIO_SetBits(SPI_NsPort,SPI_NsPin);
// SCK MOSI
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = SPI_MosiPin;
GPIO_Init(SPI_MosiPort,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI_SckPin;
GPIO_Init(SPI_SckPort,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = SPI_MisoPin;
GPIO_Init(SPI_MisoPort,&GPIO_InitStructure);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x4000380C;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&AD_Buffer_1;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_PeripheralInc_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_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
SPI_InitStruct.SPI_CRCPolynomial = 0;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPI_Number,&SPI_InitStruct);
SPI_I2S_ClearFlag(SPI_Number,SPI_I2S_FLAG_TXE);
SPI_I2S_ClearFlag(SPI_Number,SPI_I2S_FLAG_RXNE);
SPI_I2S_DMACmd(SPI_Number, SPI_I2S_DMAReq_Rx, ENABLE); 、
SPI_Cmd(SPI_Number,ENABLE);
上面是初始化
DMA_Cmd(DMA1_Channel1, ENABLE);
MA730_SetCS(ENABLE); 片选脚
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, 0x0000);
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, 0x0000);
730_SetCS(DISABLE); 片选脚
while(DMA_GetFlagStatus(DMA1_FLAG_TC1));
DMA_Cmd(DMA1_Channel1, DISABLE);
|