AnsweredAssumed Answered

STM32F4 SPI-DMA data is shifted

Question asked by canturk.gokhan on Dec 11, 2013
Latest reply on May 5, 2014 by waclawek.jan
Hello, I am using STM32F407. I am sending the data from SPI1 using DMA and getting data from SPI2 manually. However, I get data 1 or 2 bits shifted, 1 bit for some configurations of CPOL and CPHA, 2 bits shifted for some other configurations of CPOL and CPHA. I couldnt understand the problem. Here is the code:

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


  int i , j ;
     uint8_t dizi[100];
     uint8_t array[100] ;
     uint8_t data;


     
     uint8_t SPI2_recieve(void) ;
     
void init_SPI1(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);
     
     /* 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_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);
     
     GPIO_ResetBits(GPIOE , GPIO_Pin_7) ;   


     
     // enable peripheral clock
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
     
     /* configure SPI1 in Mode 0 
      * CPOL = 1 --> clock is high when idle
      * CPHA = 0 --> data is sampled at the first edge
      */
     
     SPI_I2S_DeInit(SPI1) ;
     SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex ; // set to 1line unidirectional mode(MISO is not assigned)
     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_High;        // clock is high 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 / 16
     SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
     SPI_Init(SPI1, &SPI_InitStruct); 
     
     SPI_Cmd(SPI1, ENABLE); // enable SPI1     


     // enable DMA clock and configure it
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
     
     DMA_InitStructure.DMA_Channel = DMA_Channel_3;         //SPI1 Tx DMA is DMA2/Stream3/Channe3
  DMA_InitStructure.DMA_PeripheralBaseAddr  = (uint32_t)&(SPI1->DR);  //Set the SPI1 Tx
  DMA_InitStructure.DMA_Memory0BaseAddr  = (uint32_t)&(array);    //Set the memory location to the adress of the array
  DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ;      //Sending data from memory to the peripheral's Tx register
  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_Circular;                     //Circular mode
  DMA_InitStructure.DMA_Priority  = DMA_Priority_High;               //Priority is high 
  DMA_InitStructure.DMA_FIFOMode  = DMA_FIFOMode_Enable;           //Operate in FIFO mode
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
     DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_INC4;
     DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_INC4;
     DMA_Init(DMA2_Stream3, &DMA_InitStructure);
     


     
     SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);    //Enable the DMA Transmit Request
      DMA_Cmd(DMA2_Stream3, ENABLE);                   //Enable the DMA stream assigned to SPI1


}




void init_SPI2(void){


  
     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
      * PB12 = NSS (driven low by chip select of SPI1 if in hardware mode)
      */
     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_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 SPI2 in Mode 0 
      * CPOL = 1 --> clock is high when idle
      * CPHA = 0 --> data is sampled at the first edge
      */
      /* SPI1'den gönder SPI2'den al  */
     SPI_I2S_DeInit(SPI2);
     SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_RxOnly; // SPI2 only recieves data
     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_High;        // clock is high when idle
     SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
     SPI_InitStruct.SPI_NSS = SPI_NSS_Hard; // set the NSS management to hardware mode
     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
      
}






int main(void){
     
     /* Fill the array to be sent*/
     for( j = 1; j<100 ; j++ )
     {
          array[j] = 0x71 ; 
     }
     
     /* Call the initialization functions*/
     init_SPI2();
     for( j=0 ; j<1000; j++ );
     init_SPI1();
  for( j=0 ; j<1000; j++ );




/* Main loop; SPI2 continiously gets data and fills it in another array*/
     while(1){


          
     for( i = 0; i<100 ; i++  ){


     dizi[i] = SPI2_recieve();  // recieve data
          
     
}
}
}
/* Recieve function */


uint8_t SPI2_recieve(void){
     
     while( !(SPI2->SR & SPI_I2S_FLAG_RXNE)); // wait until receive complete // wait until SPI1 transmit complete
  data = SPI2->DR ; // write data to be transmitted to the SPI data register
     return data;
}


Thank you.


Outcomes