AnsweredAssumed Answered

spi receive data via dma

Question asked by aziz.stm32 on Dec 2, 2013
Latest reply on Dec 3, 2013 by matusko.maxim
i traied to recive data from another stm32f4 via SPI that's why i used the dma to receive data from SPI  


GPIO_InitTypeDef  GPIO_InitStructure;
uint16_t value;
void init_SPI(void)
     {
     GPIO_InitTypeDef GPIO_InitStruct;
     SPI_InitTypeDef SPI_InitStruct;
       DMA_InitTypeDef       DMA_InitStructure;
     // enable clock for used IO pins
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
     /* DMA2 Stream0 channel0 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Buffer;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(SPI1->DR); 
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_BufferSize = 6*84;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; 
DMA_Init(DMA1_Stream4, &DMA_InitStructure);
DMA_Cmd(DMA1_Stream4, ENABLE);
 
// ...
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
     /* configure pins used by SPI1
      * PA5 = SCK
      * PA6 = MISO
      * PA7 = MOSI
      */
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOA, &GPIO_InitStruct);
     
     // connect SPI1 pins to SPI alternate function
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
          GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
     // enable clock for used IO pins


     
     /* Configure the chip select pin
        in this case we will use PA4 */
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
     GPIO_Init(GPIOA, &GPIO_InitStruct);
     


     
     // enable peripheral clock
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
     
     /* configure SPI1 in Mode 0 
      * CPOL = 0 --> clock is low when idle
      * CPHA = 0 --> data is sampled at the first edge
      */
     SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
     SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;     // transmit in master mode, NSS pin has to be always high
     SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
     SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle
     SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
     SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
     SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // SPI frequency is APB2 frequency / 4
     SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
     SPI_Init(SPI1, &SPI_InitStruct); 
     
     SPI_Cmd(SPI1, ENABLE); // enable SPI1
}

int main(void)
{
     init_SPI();
  STM32F4_Discovery_LEDInit(LED3);
  STM32F4_Discovery_LEDInit(LED4);
  STM32F4_Discovery_LEDInit(LED5); 
     STM32F4_Discovery_LEDInit(LED6);
     while(1)     {
      value=SPI_I2S_ReceiveData(SPI1);
if (value<400)
STM32F4_Discovery_LEDOn(LED5),
 STM32F4_Discovery_LEDOff(LED6);
else if (value >400)
 STM32F4_Discovery_LEDOn(LED6),
STM32F4_Discovery_LEDOff(LED5);
;}
      
}
     
          

Outcomes