AnsweredAssumed Answered

STM32F407 SPI-DMA Continuous Transmit and Receive.

Question asked by ekhare.abhishek on May 27, 2015
Latest reply on Jun 2, 2015 by ekhare.abhishek
Hi,
I am trying to configure the DMA based SPI communication for STM32F407.
STM32F407 is the host controller(master), the slave is CC3100 WiFi module.
I am implementing the DMA for SPI2.
Following is my configuration code:-

static void InitSPI(void)
{
GPIO_InitTypeDef  GPIO_InitStructure;
SPI_InitTypeDef  SPI_InitStructure;

// Enable peripheral clock
RCC_APB1PeriphClockCmd(WIFI_SPI_CLK, ENABLE);

// CLK pin
RCC_AHB1PeriphClockCmd(WIFI_SPI_CLK_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin   = WIFI_SPI_CLK_PIN;
GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = WIFI_GPIO_SPEED;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN; // GPIO_PuPd_UP
GPIO_Init(WIFI_SPI_CLK_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(WIFI_SPI_CLK_GPIO_PORT, WIFI_SPI_CLK_SOURCE, WIFI_SPI_CLK_AF);

// MISO pin
RCC_AHB1PeriphClockCmd(WIFI_SPI_MISO_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin   = WIFI_SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = WIFI_GPIO_SPEED;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN; // GPIO_PuPd_UP
GPIO_Init(WIFI_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(WIFI_SPI_MISO_GPIO_PORT, WIFI_SPI_MISO_SOURCE, WIFI_SPI_MISO_AF);

// MOSI pin
RCC_AHB1PeriphClockCmd(WIFI_SPI_MOSI_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin   = WIFI_SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = WIFI_GPIO_SPEED;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN; // GPIO_PuPd_UP
GPIO_Init(WIFI_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(WIFI_SPI_MOSI_GPIO_PORT, WIFI_SPI_MOSI_SOURCE, WIFI_SPI_MOSI_AF);

// CC3100 SPI configuration
SPI_I2S_DeInit(WIFI_SPI);
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_CPOL = SPI_CPOL_Low;                                 // SPI_CPOL_High
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;                               // SPI_CPHA_2Edge
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;                                  // SPI_NSS_Hard / SPI_NSSInternalSoft_Set
SPI_InitStructure.SPI_BaudRatePrescaler = WIFI_SPI_BAUDRATE_PRESCALER;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(WIFI_SPI, &SPI_InitStructure);

DMA_InitTypeDef  DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);

DMA_DeInit(DMA1_Stream4); //SPI1_TX_DMA_STREAM
DMA_DeInit(DMA1_Stream3); //SPI1_RX_DMA_STREAM

//Enabling DMA for SPI
DMA_InitStructure.DMA_BufferSize = WIFI_DMA_SIZE;
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_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;

DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(SPI2->DR));
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;

/* Configure Tx DMA */
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) gu8a_INIT_DmaWifiTx;
DMA_Init(DMA1_Stream4, &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) gu8a_INIT_DmaWifiRx;
DMA_Init(DMA1_Stream3, &DMA_InitStructure);

DMA_Cmd(DMA1_Stream4, ENABLE); /* Enable the DMA SPI TX Stream */
DMA_Cmd(DMA1_Stream3, ENABLE); /* Enable the DMA SPI RX Stream */

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

// Enable SPI2
SPI_Cmd(WIFI_SPI, ENABLE);
}

Outcomes