Skip to main content
Mnemocron
Associate III
August 14, 2021
Question

float values of array are not assigned during a for loop

  • August 14, 2021
  • 6 replies
  • 4402 views

I want to convert a bunch of uint16_t values from an ADC into floating point voltages. For this I use a for loop to loop through the uint16_t array and write the values to a float array. But the float array remains 0 as if no assignment is ever made. Outside of the for loop the conversion works. And when I step through the program with the debugger, I see reasonable float values but they do not end up being written to the array. Why?

the temp float value is 1.847

0693W00000D2U7SQAV.png 

the temporary index is 0

0693W00000D2U7XQAV.png 

but the array at index 0 is not 1.847

0693W00000D2U7cQAF.png 

    This topic has been closed for replies.

    6 replies

    T J
    Senior III
    August 14, 2021

    we'd have to look at the code, as a guess.

    and need to know if the processor has cache

    and need to know if a DMA is somehow involved in the process.

    but its likely you are not reading the right memory or not writing to the right memory

    need to see the code

    Mnemocron
    MnemocronAuthor
    Associate III
    August 14, 2021

    STM32f373re does not have a cache afaik.

    I just posted the main file.

    the uint16_t array is indeed being filled by the DMA.

    I am confused because the conversion works outside of a for loop.

    Tesla DeLorean
    Guru
    August 14, 2021

    For DMA the memory array needs to be volatile so as not to be folded by optimization, the DMA also needs to have completed prior to working being done on the data it has fetched.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    T J
    Senior III
    August 14, 2021

    check the DMA flag or DMA counter for completion,

    then enter the for loop

    Mnemocron
    MnemocronAuthor
    Associate III
    August 15, 2021

    https://stackoverflow.com/questions/68783394/float-values-of-array-are-not-assigned-during-a-for-loop-stm32-arm-none-eabi

    I was able to resolve it, without knowing what cause this. Either way, my code is now much cleaner and easier to read

    waclawek.jan
    Super User
    August 15, 2021

    > I was able to resolve it, without knowing what cause this.

    Then you did not resolve it.

    If a global variable (adc_voltages[]) has nonzero value before you wrote into it the first time, it means either incorrect software wrote into it (incorrect pointer or out-of-bounds index to array), or incorrectly set DMA wrote into it.

    You should be able to catch the culprit using data breakpoints (a.k.a. watchpoints). If there's no break and the data are changed, then it's DMA.

    JW