AnsweredAssumed Answered

SPI Slave is giving garbage values.

Question asked by hussnain.shafqat on Dec 10, 2014
Latest reply on Dec 11, 2014 by waclawek.jan
I have configured my STM32F407 Discovery Board as slave with a STM8S00. The data sent by Master on MOSI is totally different from that which that my slave is receiving I have observed that data on the Digital Oscilloscope. Setting in the master is as follows: CPHA=1 CPOL=1 Master sends at Negative edge and samples at positive edge. SPI Clock is 4 MHz. It is also sending back different data that sent by a STM32F407 master.
Here is the code of main file:
#include "stm32f4_discovery.h"


int i=0, IT_St=0;
int Rx_Buf[1000];
void SPI_TR(void);;
void n_Tick(int tk);


void n_Tick(int tk)
{
     while(tk--)
     {
          
     }
}




void SPI_TR(void)
{
     GPIO_InitTypeDef GPIO_SPI;
     SPI_InitTypeDef My_SPI;
     NVIC_InitTypeDef nvic;
     EXTI_InitTypeDef itr;
     
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
     
     //PA4=NSS  PA5=SCK  PA6=MISO  PA7=MOSI
     GPIO_SPI.GPIO_Pin=GPIO_Pin_4 | GPIO_Pin_5 |  GPIO_Pin_6 | GPIO_Pin_7;
     GPIO_SPI.GPIO_Mode=GPIO_Mode_AF;
     GPIO_SPI.GPIO_PuPd=GPIO_PuPd_NOPULL;
     GPIO_SPI.GPIO_OType=GPIO_OType_PP ;
     GPIO_SPI.GPIO_Speed=GPIO_Speed_100MHz;
     GPIO_Init(GPIOA, &GPIO_SPI);
     
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
     
  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
     nvic.NVIC_IRQChannel=SPI1_IRQn;
     nvic.NVIC_IRQChannelCmd = ENABLE;
     nvic.NVIC_IRQChannelPreemptionPriority= 0;
  NVIC_Init(&nvic);
     
     My_SPI.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
     My_SPI.SPI_Mode = SPI_Mode_Slave;
     My_SPI.SPI_DataSize = SPI_DataSize_8b;
     My_SPI.SPI_CPOL = SPI_CPOL_High;
     My_SPI.SPI_CPHA = SPI_CPHA_2Edge;
     My_SPI.SPI_NSS = SPI_NSS_Hard; // Configure it make high to low transition
     //My_SPI.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_16;
     My_SPI.SPI_FirstBit = SPI_FirstBit_MSB;
     
     SPI_Init(SPI1, &My_SPI);
     SPI_Cmd(SPI1, ENABLE);
}
void SPI1_IRQHandler()
{
     Rx_Buf[i]=SPI_I2S_ReceiveData(SPI1);
  //i=SPI_I2S_ReceiveData(SPI1);
     SPI_I2S_ClearFlag(SPI1, SPI_I2S_FLAG_RXNE);
     SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_RXNE);
     SPI_I2S_SendData(SPI1, FIFO_STATUS_RX_FULL);
     i++;
     if(i>=1000)
          i=0;
     
}


     
int main(void)
{
     SPI_TR();
     while(1)
     {
          
     }
  
}

Outcomes