Passing floating point parameters issue

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Passing floating point parameters issue

1,479 Views
mattht42
Contributor I

Hello,

I am currently working on device K70FN1M0M15 with MQX OS.

FPU : fpv4-sp-d16

I have a function which takes an enumerate and a float as parameters and the problem is that sometimes the function’s float parameter takes the value 0 instead of the given value.

Here is an example of the function call, then the source code of the function:

MRTimer_SetValue(mTimerRefBracketAlarm1, 3.0);
void MRTimer_SetValue(tTimersRef TimerRef, float setTimerValue)
{
       uint16_t *pDestValue = NULL;
       uint16_t rawValue = 0;
       test = setTimerValue;

       if(setTimerValue == 0)
             test++;

       if ((TimerRef >= MRTIMER_10MS_FIRSTREF) && (TimerRef <= MRTIMER_10MS_LASTREF))
       {
             rawValue = (uint16_t) (setTimerValue * 100.0);
             pDestValue = &timers10ms[TimerRef - MRTIMER_10MS_FIRSTREF];
       }
       else if (TimerRef >= MRTIMER_100MS_FIRSTREF && TimerRef <= MRTIMER_100MS_LASTREF)
       {
             rawValue = (uint16_t) (setTimerValue * 10.0);
             pDestValue = &timers100ms[TimerRef - MRTIMER_100MS_FIRSTREF];
       }
       else if (TimerRef >= MRTIMER_1000MS_FIRSTREF && TimerRef <= MRTIMER_1000MS_LASTREF)
       {
             rawValue = (uint16_t) (setTimerValue * 1.0);
             pDestValue = &timers1000ms[TimerRef - MRTIMER_1000MS_FIRSTREF];
       }
       else
       {
             return;
       }

       if(rawValue >65530 || rawValue < 3 || pDestValue == NULL)
       {
             tmpdebug_ref = TimerRef;
             tmpdebug_valeur = rawValue;
             test++;
       }
       else
       {
             *pDestValue = rawValue;
       }
       return;

}

As you can see, I give 3.0 as parameter but when the bug occurs « setTimerValue » in the function take the value 0 instead of 3.0.

 

The call of the function uses this assembler code:

577                       MRTimer_SetValue(mTimerRefBracketAlarm2, 3);

0001fd08:   ldr r3, [r7, #4]

0001fd0a:   ldrh r3, [r3, #6]

0001fd0c:   addw r3, r3, #287       ; 0x11f

0001fd10:   uxth r3, r3

0001fd12:   mov r0, r3

0001fd14:   vmov.f32 s0, #8

0001fd18:   bl 0x2bae8 <MRTimer_SetValue>

In my opinion, the value of the parameter is stored in the register s0 (red line).

 

Then when I enter in the function, I have this assembler code:

          MRTimer_SetValue:

0002bae8:   push {r7, lr}

0002baea:   sub sp, #16

0002baec:   add r7, sp, #0

0002baee:   mov r3, r0

0002baf0:   vstr s0, [r7]

0002baf4:   strh r3, [r7, #6]

 

The instruction” 0002baf0:   vstr s0, [r7]” is supposed to store the content of an extension register(S0) to memory (address given by r7).

When the bug occurs, after this instruction the value in the memory is 0.

For your information, it’s not possible to watch S0 in KDS 3.0.

Do you have any clue about this odd behavior ?

0 Kudos
Reply
3 Replies

1,446 Views
bobpaddock
Senior Contributor III

In general Floating Point should be avoided, it is rarely needed in Embedded Code, you are seeing why with this code.

Comparison to zero has no guarantee of being true, see:

https://stackoverflow.com/questions/19837576/comparing-floating-point-number-to-zero#19841844

and the referenced C++ FAQ entry.

Also use 0.0 not 0, however it will not make a lot of difference due to the above.

 

0 Kudos
Reply

1,453 Views
DanielRuvalcaba
NXP TechSupport
NXP TechSupport

Hi,

I highly recommend you to switch to MCUXpresso IDE.

About your problem, is this happening every time you run your code?

Do you have problems with floating point numbers in general or is it just in this case in particular?

Best regards, Daniel.

0 Kudos
Reply

1,422 Views
mattht42
Contributor I

Hello,

Thanks for the answer, the problem occurs randomly when the code is running. I only noticed this problem with this function but I can imagine it can appear in other functions where I use float as parameter.

I'm on a travel work for 2 week, I will give you more information when i come back.

 

 

0 Kudos
Reply