[DONE] Problem with MS2ST macro

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
MathiasP
Posts: 4
Joined: Mon Jun 03, 2013 4:24 pm

[DONE] Problem with MS2ST macro

Post by MathiasP »

Hello,

I think I found a bug in the MS2ST macro, when the value of CH_FREQUENCY is changed.

Here a example calculation for CH_FREQUENCY = 10000 and msec = 1:
(((((msec) - 1L) * CH_FREQUENCY) / 1000L) + 1L))
=> (((((1) - 1L) * 10000) / 1000L) + 1L))
=> (((0 * 10000) / 1000L) + 1L))
=> 1 tick
but shoud be 10

I'm not sure how to change the macro, the first idea would be to remove the - 1L, but I don't know if this would cause any side effects.

Can anyone confirm this? Or did I get something wrong?

Best regards
Mathias
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Problem with MS2ST macro

Post by Giovanni »

Hi,

It is a bug, it fails for msec==1, thanks for finding, I will look into this before the 2.6.0 release. As workaround you could use US2ST, that one should work.

Giovanni
theShed
Posts: 50
Joined: Tue Feb 26, 2013 3:43 pm

Re: [TODO] Problem with MS2ST macro

Post by theShed »

I guess that macro was written asuming that systick >= 1mS.
Which is a reasonble assumption...

Many of the lower powered devices would stuggle to service a 100uS systick interval.
@4mHz that's an interrupt every 400 instructions (assuming you can execute an instruction every clock cycle - which you can't!)

Doesn't leave a lot of time to do anything else....
--
mike

Edit: having said that, it should still be fixed.

Edit: Quick and dirty fix:-)

#define MS2ST(msec) US2ST(msec*1000)
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: [TODO] Problem with MS2ST macro

Post by Giovanni »

I am committing the following changes:

Code: Select all

#define MS2ST(msec)                                                         \
  ((systime_t)((((msec) * CH_FREQUENCY - 1L) / 1000L) + 1L))
#define US2ST(usec)                                                         \
  ((systime_t)((((usec) * CH_FREQUENCY - 1L) / 1000000L) + 1L))


This should address that corner case.

Thoughts?

Giovanni
MathiasP
Posts: 4
Joined: Mon Jun 03, 2013 4:24 pm

Re: [TODO] Problem with MS2ST macro

Post by MathiasP »

Hello Giovanni,

I did some tests for the CH_FREQUENCY values 100, 1000 and 10000.

The results look fine ;)

Here my test code (tested with MS Visual Studio):

Code: Select all

#define CH_FREQUENCY   10000

#define MS2ST(msec) ((((((msec)) * CH_FREQUENCY- 1L) / 1000L) +1L))
#define US2ST(usec) ((((((usec)) * CH_FREQUENCY- 1L) / 1000000L) +1L))


void printMs(int ms)
{
   printf("MS2ST( %d )\t=\t%d\n", ms, MS2ST(ms));
}

void printUs(int us)
{
   printf("US2ST( %d )\t=\t%d\n", us, US2ST(us));
}

int main(int argc, char* argv[])
{
   printf("Testing MS2ST for CH_FREQUENCY = %d\n", CH_FREQUENCY);
   printMs(1);
   printMs(5);
   printMs(10);
   printMs(20);
   printMs(50);
   printMs(100);
   printMs(200);

   printf("\nTesting US2ST for CH_FREQUENCY = %d\n", CH_FREQUENCY);
   printUs(100);
   printUs(500);
   printUs(1000);
   printUs(10000);
   
   return 0;
}
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: [TODO] Problem with MS2ST macro

Post by Giovanni »

Thank you, committed.

Giovanni
Post Reply