Skip to main content
ABlum.2
Associate III
Jul 6, 2021 1:16 PM UTC
Solved

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

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.
ABlum.2
ABlum.2Best answer
Associate III
Jul 9, 2021 12:30 PM UTC

Quick update

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

Regards!

12 replies

CPINA
ST Employee
Jul 6, 2021 1:29 PM UTC

Hello,

Does your SPI is master in transmission ?

Br,

ABlum.2
ABlum.2Author
Associate III
Jul 6, 2021 1:30 PM UTC

Yes it is

CPINA
ST Employee
Jul 6, 2021 1:36 PM UTC

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
Jul 6, 2021 1:35 PM UTC

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
Jul 6, 2021 1:49 PM UTC

Hi @TDK​

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

Regards

Alan

ABlum.2
ABlum.2AuthorBest answer
Associate III
Jul 9, 2021 12:30 PM UTC

Quick update

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

Regards!