第一次使用407,感觉和M3的库相差很大,调试过程中发现程序连时钟都没输出来,不知道哪里配置错了, 请问下DMA初始化可以下面这样写嘛?uint32_t SpiRegInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* Enable the SPI periph */
// RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 ,ENABLE);
/* Enable SCK, MOSI and MISO GPIO clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* SPI CK pin configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI MISO pin configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI MISI pin configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI configuration -------------------------------------------------------*/
SPI_I2S_DeInit(SPI2);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//2
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
/* Configure GPIO PIN for Lis Chip select */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//should change another pin for cs, don't need GPIO_PinAFConfig when gpio
/* Deselect : Chip Select high */
//GPIO_SetBits(GPIOB, GPIO_Pin_12);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
//configure DMA RX
DMA_DeInit(DMA1_Stream3);
DMA_InitStructure.DMA_Channel=DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x4000380C;//(uint32_t)(SPI2+0x0C);
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SPI2_Buffer_Rx;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = SPI2_BufferSize;
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_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_Init(DMA1_Stream3, &DMA_InitStructure);
//configure DMA Tx
DMA_DeInit(DMA1_Stream4);
DMA_InitStructure.DMA_Channel=DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x4000380C;//(uint32_t)(SPI2+0x0C);
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SPI2_Buffer_Tx;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = SPI2_BufferSize;
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_Medium;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_Init(DMA1_Stream4, &DMA_InitStructure);
/* Enable SPI2 Rx DMA Request */
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
/* Enable SPI2 Tx DMA Request */
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
/* Enable DMA1 Stream3 */
DMA_Cmd(DMA1_Stream3,ENABLE);
/* Enable DMA1 Stream4 */
DMA_Cmd(DMA1_Stream4,ENABLE);
}
void SPI2_ReceiveSendByte( uint16_t num )
{
DMA_SetCurrDataCounter(DMA1_Stream4, num);
DMA_SetCurrDataCounter(DMA1_Stream3, num);
DMA_ClearFlag(DMA1_Stream4,DMA_FLAG_HTIF4|DMA_FLAG_TCIF4|DMA_FLAG_HTIF4|DMA_FLAG_TEIF4);
DMA_ClearFlag(DMA1_Stream3,DMA_FLAG_HTIF3|DMA_FLAG_TCIF3|DMA_FLAG_HTIF3|DMA_FLAG_TEIF3);
SPI2->DR ;
while(((SPI2->SR)&(1<<1))==0);// TXNE statue
DMA_Cmd(DMA1_Stream4, ENABLE);
DMA_Cmd(DMA1_Stream3, ENABLE);
while(!DMA_GetFlagStatus(DMA1_Stream3,DMA_FLAG_TCIF3));
DMA_Cmd(DMA1_Stream4, DISABLE);
DMA_Cmd(DMA1_Stream3, DISABLE);
}
void SPI2_Write(uint8_t* data,uint8_t bytesNumber)
{
uint8_t byte = 0;
for(byte=0;byte<bytesNumber;byte++)
{
SPI2_Buffer_Tx[byte]=data[byte];
}
// GPIO_ResetBits(GPIOB,GPIO_Pin_12);
SPI2_ReceiveSendByte(bytesNumber);
//GPIO_SetBits(GPIOB, GPIO_Pin_12);
// return bytesNumber;
}
void SPI2_Read(uint8_t * data,uint8_t bytesNumber)
{
uint8_t byte = 0;
for(byte = 0; byte < bytesNumber; byte++)
{
SPI2_Buffer_Rx[byte]=0;
}
// GPIO_ResetBits(GPIOB,GPIO_Pin_12);
SPI2_ReceiveSendByte(bytesNumber);
// GPIO_SetBits(GPIOB, GPIO_Pin_12);
for(byte = 0; byte < bytesNumber; byte++)
{
data[byte]=SPI2_Buffer_Rx[byte];
}
// return bytesNumber;
}
上面就是SPI2 DMA的代码,我现在直接调用上面的代码不知道为何连时钟都没有输出,希望做过的能指点一二,多谢了。
此帖出自
小平头技术问答
一周热门 更多>