AnsweredAssumed Answered

STM32F4 SPI-DMA error

Question asked by canturk.gokhan on Dec 6, 2013
Latest reply on Dec 11, 2013 by canturk.gokhan
Hi everyone I am using STM32F407 and I am trying to learn SPI and DMA. I am trying to send the data from SPI1 and recieve on SPI2 and using DMA in order to direct data to memory adress to an array. But when I display the array I cannot see my data. I couldnt understand ıf my problem is on SPI2 or DMA. I need help. Here is the code:

#include <stm32f4xx.h>    
#include <stm32f4xx_spi.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h> 
#include <stm32f4xx_dma.h>


  int i ;
     uint8_t dizi[100];
     
// this function initializes the SPI1 peripheral
void init_SPI1(void){
     GPIO_InitTypeDef GPIO_InitStruct;
     SPI_InitTypeDef SPI_InitStruct;
     
     // enable clock for used IO pins
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
     
     /* configure pins used by SPI1
      * PA5 = SCK
      * PA7 = MOSI
      */
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | 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);
     
          /* Configure the chip select pin
        in this case we will use PE7 */
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
           
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
     GPIO_Init(GPIOE, &GPIO_InitStruct);
     
     //GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high
     GPIO_ResetBits(GPIOE , GPIO_Pin_7) ; 
     // 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_1Line_Tx; // set to full duplex mode, seperate MOSI and MISO lines
     SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // 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_16; // 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
}




void init_SPI2(void){


  
  DMA_InitTypeDef DMA_InitStructure;
     GPIO_InitTypeDef GPIO_InitStruct;
     SPI_InitTypeDef SPI_InitStruct;
     
     // enable clock for used IO pins
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
     
     /* configure pins used by SPI2
      * PB10 = SCK 
      * PC3 = MOSI
      */
      
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 ;
     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(GPIOC, &GPIO_InitStruct);
     
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12 ;
     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(GPIOB, &GPIO_InitStruct);
     
     // connect SPI2 pins to SPI alternate function
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_SPI2);
     GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_SPI2);
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_SPI2);


     


     
     // enable peripheral clock
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 , ENABLE); 
     
     /* configure SPI1 in Mode 0 
      * CPOL = 0 --> clock is low when idle
      * CPHA = 0 --> data is sampled at the first edge
      */
      /* SPI1'den gönder SPI2'den al  */
     
     SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Rx; // SPI2 sadece veri alacak DMA'yle memorye kaydedecek
     SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;     // recieve in slave mode, NSS pin has to be always low
     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_Reset; // set the NSS management to internal and pull internal NSS low
     SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // SPI frequency is APB2 frequency / 16
     SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is recieved MSB first
     SPI_Init(SPI2, &SPI_InitStruct); 
     
     SPI_Cmd(SPI2, ENABLE); // enable SPI2
     
     //Enable the Direct Memory Access peripheral clocks
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
     
     DMA_InitStructure.DMA_Channel = DMA_Channel_0;         //SPI2 Rx DMA is DMA1/Stream3/Channel0
  DMA_InitStructure.DMA_PeripheralBaseAddr  = (uint32_t)&(SPI2->DR);  //Set the SPI2 Rx
  DMA_InitStructure.DMA_Memory0BaseAddr  = (uint32_t)&(dizi);    //Set the memory location to the adress of the array
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;      //Sending data from peripheral's Rx register to the memory
  DMA_InitStructure.DMA_BufferSize  = 100;    //Define the number of bytes to send
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;        //Don't increment the peripheral 'memory'
  DMA_InitStructure.DMA_MemoryInc  = DMA_MemoryInc_Enable;                //Increment the memory location
  DMA_InitStructure.DMA_PeripheralDataSize  = DMA_PeripheralDataSize_Byte;    //Byte size memory transfers
  DMA_InitStructure.DMA_MemoryDataSize  = DMA_MemoryDataSize_Byte;         //Byte size memory transfers
  DMA_InitStructure.DMA_Mode  = DMA_Mode_Normal;                           //Normal mode (not circular)
  DMA_InitStructure.DMA_Priority  = DMA_Priority_High;               //Priority is high to avoid saturating the FIFO since we are in direct mode
  DMA_InitStructure.DMA_FIFOMode  = DMA_FIFOMode_Disable;           //Operate in 'direct mode' without FIFO
  //DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
     
     DMA_Init(DMA1_Stream3, &DMA_InitStructure);
     
     SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);    //Enable the DMA Transmit Request
  DMA_Cmd(DMA1_Stream3, ENABLE);                   //Enable the DMA stream assigned to SPI2
     
}






/* This funtion is used to transmit and receive data 
 * with SPI1
 *                data --> data to be transmitted
 *                returns received value
 */
void SPI1_send(uint8_t data){
     
  
  SPI1->DR = data; // write data to be transmitted to the SPI data register
     while( SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET ); // wait until SPI1 transmit complete
}




int main(void){
     
     while(1){


     init_SPI1();
     init_SPI2();
     
     for( i = 0; i<100 ; i++  ){
          
          //GPIOE->BSRRH |= GPIO_Pin_7; // set PE7 (CS) low
          SPI1_send(0x1F);  // transmit data
          //GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 (CS) high
     }
}
     
     SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);    //Enable the DMA Transmit Request
  DMA_Cmd(DMA1_Stream3, ENABLE);                   //Enable the DMA stream assigned to SPI2
     
}

Outcomes