AnsweredAssumed Answered

SPI2 Slave bit shift problem

Question asked by gentili.ivano on Jul 3, 2013
Latest reply on Jul 4, 2013 by John F.
Dear all I hope you can solve m problem. I need to configure the SPI2 on STM32F100V8 on pin PB12-14 in slave mode because I have a master board that communicates to other spi slaves. The SPI has the Clock normally High and the data of the master changes on falling edge of the clock (so I think the slave stm32 should be setted as CPOL High and CPHA = second edge, rising)


Here some little hardware
Master CLK -> Slave STM32 CLK
Master MISO <- Slave STM32 MISO (unconnected from Master for debug purposes)
Master MOSI -> Slave STM32 MOSI
Master CS -> dedicated line to Slave STM32 NSS

In this project i need to use UART3 but is used in asyncronous mode so this excludes some silicon bugs described in the "errata"


The gpio are congigured as AF_PP, the RCC is enabled for SPI2, and the SPI is initialized in this way:


static void SPI2_Init (void){
  SPI_InitTypeDef SPI_InitStructure;
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;   
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;   
  SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;      
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_Init(SPI2, &SPI_InitStructure);
  SPI_Cmd(SPI2, ENABLE);
}



Here the task that is called in a while(1), no watchdog enabled no other tasks enabled for  the moment


void task_spi(vpoid){
  static unsigned char state;
   unsigned char read
  case 0:
      if(READ_CS_SPI2()==0){
        while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)==RESET);
        read = SPI_I2S_ReceiveData(SPI2);
        if(read==0x02){
          state = read;
          read = 0;
        }
      }
  case 2:
       while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
       SPI_I2S_SendData(SPI2, 0xFF); 
       TOGGLE_S4();
       state = 99;
  break;
  case 99:
       while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
       SPI_I2S_SendData(SPI2, 0x00); 
       state = 0;
  break;
  default:
      state = 0;
  break;


  }
}



With this code i have correct receive of the data in the slave stm32 but i have troubles in sending response data. For example if i try to send a reply 0xFF in my oscilloscope I see: 0xFE.
If I try to send 0xAA i only see glitches on the MISO line ( at the moment the miso line is disconnected from the master)

Hope someone can take me away from troubles. Regards, Ivano

Attached two Stamps of the scope for more clarification...

Attachments

Outcomes