cancel
Showing results for 
Search instead for 
Did you mean: 

How to use the for loop in a simple sequence with STM32H7 in a SPI transmission?

ABlum.2
Associate III

Hello everyone,

I am trying to replace a simple code in C inside a STM32H7 that goes like this:

function(0, &array[0]);
 
function(1, &array[1]);
 
function(2, &array[2]);
 
function(3, &array[3]);
 
function(4, &array[4]);

with

enum_variable_t index ;
 
for(index = FIRST_ENUM ; index < LAST_ENUM; index ++)
 
{
 
      function((uint16_t)index , &array[index]);
 
}

However, some data is corrupted due to this simple change, I thought it might be an optimization problem but I can't solve it by including the (-O) flag. Any advice would be of great help.

Regards

Alan

1 ACCEPTED SOLUTION

Accepted Solutions
ABlum.2
Associate III

Quick update

I have changed all global variables to volatile and now everything works as intended.

Regards!

View solution in original post

12 REPLIES 12
CPINA
Associate III

Hello,

Does your SPI is master in transmission ?

Br,

Yes it is

TDK
Guru

What data is corrupted? There's nothing wrong with the code you posted, assuming FIRST_ENUM=0 and LAST_ENUM=5.

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

And when you say data is corrupted, it means that all the next data are wrong or are they any recovery ? Is data shifted by some bits ?

Hi @CPINA​ ,

When I read the values in array[0] without the for loop, I get these values (which are correct)

0693W00000BcxGcQAJ.png 

But when I replace the code with the for loop (nothing else changes) I get this data.

0693W00000BcxHBQAZ.png

The blue line represent array[0]

ABlum.2
Associate III

Hi @TDK​

I explained in the topic avode what I mean with corrupted data.

Regards

Alan

Not sure to understand, for both blue line is your array[0) ? I was thinking it was the yellow one. what is supposed to be the data linked to array[0].

If the data line is supposed to be the blue one, it seems it is even not a data, looks like noise. Did you check that the SPI is well configured in debug just before writing the data into the data register ? Please confirm to me, you are master transmitter in such case ? If yes can you see the SPI going out after writing the Data register of the SPI ?

Hello @CPINA​ 

After restarting my computer today morning it seems that the problem solves itself and now works without the error, some kind of magic I guess.

Regards

Alan