cancel
Showing results for 
Search instead for 
Did you mean: 

missing one byte out of 26 while receiving data with spi

SA.17
Associate III

I am using STM32L072RB

I am receiving 26 bytes at one go from an adc using spi interface

but i found that i miss one byte out of 26 bytes(from any position) every 6-7 times of reading adc.

I saw that data byte is present using oscillosope ,but mcu misses it somehow.

I am using below code to receive each byte of data .i call it 26 times and save result in an array.

i am transmitting 0's only(c=0)

__STATIC_INLINE uint8_t spi2_read_write(unsigned char c)
{
uint8_t x; 
while(!(SPI2->SR&(1<<1))){;} 
GPIOB->BRR = GPIO_PIN_12 ;  // CS LOW
SPI2->DR=c; 
while(!(SPI2->SR&(1<<1))){;} 
while(!(SPI2->SR&(1<<0))){;}
x=SPI2->DR; 
while(SPI2->SR&(1<<7)){;} 
GPIOB->BSRR = GPIO_PIN_12 ;  //CS high
	
return x;
}

one more issue with above code:

when i start reading ,first byte(of 26 bytes) is always the one which i received last during previous read.And i always miss the last byte during my current read.

How to resolve this ?

to ignore this issue What i am doing is reading 27 instead of 26 bytes and hence ignoring the first byte always (which is 0). but its not good solution i guess

May be this issue is causing missing byte issue ?

4 REPLIES 4

Do you observe SPI registers in debugger? Don't.

JW

found that when i print SPI2->SR (status register) on line 12 of above code

SPI2->SR = 3 always

but the point it misses a data byte

it becomes SPI2->SR=2 and continuous to be 2 till next cycle of 26 bytes ,

even if i miss only one byte ,not only that time ,but also till the 26th byte SR remains 2 (even though i am getting correct data)

found by reading data received and matching with SR status ,

as one byte is missed ,rest data shifts upwards in my array

TDK
Guru

You could read DR prior to transmitting to clear the RXNE bit. If you have spurious clock edges, this may fix it. I don't see any problems with the code you posted.

If you feel a post has answered your question, please click "Accept as Solution".

> SPI2->SR = 3 always

So, after a TX/RX cycle, you *normally* see *both* TXE and RXNE set? That doesn't sound right.

Reduce the program to absolute minimum exhibiting the problem, but still complete compilable, and post.

JW