cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Slave is giving garbage values.

shafqathussnain
Associate II
Posted on December 10, 2014 at 19:15

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)

{

}

  

}

3 REPLIES 3
Posted on December 10, 2014 at 19:26

Can you post examples of a stream transmitted by master, and corresponding data as the slave receives them?

JW
shafqathussnain
Associate II
Posted on December 11, 2014 at 06:34

Actual Data Sent By Master: (MSB First) Observed on Digital Oscilloscope

0000 0111 -> 1110 0000 (0xE0 Expected to be received)

0101 0000 -> 0000 1010 (0x0A)

0000 1000 -> 0001 0000 (0x10)

1010 0101 -> 1010 0101 (0xA5)

But Slave is providing following Data: (I don't know which Byte correspond to that sent by Master)

0100 1011 (0x4B)

0000 1111 (0xF)

1010 0000 (0xA0)

0001 0001 (0X11)

Posted on December 11, 2014 at 08:58

> I don't know which Byte correspond to that sent by Master

This makes your data worthless... Try to synchronize them somehow.

Also, make sure you have consistent ground - always connect your oscilloscopes' ground to the receiver's ground.

JW