Skip to main content
ABlum.2
Associate III
July 6, 2021
Solved

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

  • July 6, 2021
  • 3 replies
  • 5996 views

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

This topic has been closed for replies.
Best answer by ABlum.2

Quick update

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

Regards!

3 replies

CPINA
ST Employee
July 6, 2021

Hello,

Does your SPI is master in transmission ?

Br,

ABlum.2
ABlum.2Author
Associate III
July 6, 2021

Yes it is

CPINA
ST Employee
July 6, 2021

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 ?

TDK
July 6, 2021

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""."
ABlum.2
ABlum.2Author
Associate III
July 6, 2021

Hi @TDK​

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

Regards

Alan

ABlum.2
ABlum.2AuthorBest answer
Associate III
July 9, 2021

Quick update

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

Regards!