Page 1 of 1
[DONE] Problem with MS2ST macro
Posted: Mon Jun 03, 2013 5:38 pm
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
Re: Problem with MS2ST macro
Posted: Mon Jun 03, 2013 5:58 pm
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
Re: [TODO] Problem with MS2ST macro
Posted: Mon Jun 03, 2013 6:18 pm
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)
Re: [TODO] Problem with MS2ST macro
Posted: Tue Jun 04, 2013 8:37 am
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
Re: [TODO] Problem with MS2ST macro
Posted: Tue Jun 04, 2013 8:59 am
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;
}
Re: [TODO] Problem with MS2ST macro
Posted: Tue Jun 04, 2013 9:14 am
by Giovanni
Thank you, committed.
Giovanni