Author Topic: Higher resolution Timers in uTasker simulator  (Read 9042 times)

Offline mhoneywill

  • Full Member
  • ***
  • Posts: 173
    • View Profile
Higher resolution Timers in uTasker simulator
« on: June 07, 2009, 01:49:54 PM »
Hi Mark,

I'm starting to use the utasker simulator to produce standalone simulators that are simulating other parts of my hardware. (Like Modbus slaves etc). This is allowing me to test other parts of my system. One limitiation I'm running into is that lask of support for hardware Timers in the windows simulator, my understanding is that hardware timers are mapped to the basic system tick which I have reduced to 10ms, in config.h

#define TICK_RESOLUTION      10

It seems that the basic windows Tick rate can be easily modified from its default 10ms to 1ms see http://technet.microsoft.com/en-gb/sysinternals/bb897569.aspx I was wondering how easy it would be to modify uTasker so that in its simulator it could support a higher resolution timers, thus approximating the real hardware more closely.

Cheers

Martin

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Higher resolution Timers in uTasker simulator
« Reply #1 on: June 07, 2009, 03:02:30 PM »
Hi Martin

According to the article the default max.- resolution is 10ms. This corresponds with my experiences since trying values lower than 10ms doesn't make things faster and actually result sin things getting slower (possibly ticks are being left out?).

The routine NtSetTimerResolution() seemingly allows the system resolution to be increased to up to 1ms. Assuming that this works it may be interesting to add this call when the simulator is initialised (possibly conditional on the TICK setting being lower than 10ms). If it then allows the higher resolution all time critical simulation parts (like the timers) will result in a ten fold increase of accuracy, which would be quite interesting.

Can you give it a try?

Regards

Mark

P.S. A reduction of the TICK value aids in the simulation accuracy of HW timers. However it may be unnecessary in the target since the HW timers are not related to the TICK. Having a fast TICK may thus result in extra overhead with no actual benefits.
Therefore it can be useful to make this conditional on the simulator
#ifdef _WINDOWS
    #define TICK_RESOLUTION      1
#else
    #define TICK_RESOLUTION      50
#endif


P.P.S. The lower the TICK value, the lower is the maximum TICK based time delay that can be programmed. However usually the TICK counter is an unsigned long and so still allows up to about 49 days with 1ms resolution.
typedef unsigned long     UTASK_TICK; // in types.h

Offline mhoneywill

  • Full Member
  • ***
  • Posts: 173
    • View Profile
Re: Higher resolution Timers in uTasker simulator
« Reply #2 on: June 08, 2009, 01:55:50 PM »
Hi Mark,

I'm not familiar with windows calling conventions so am not quite sure how to declare a call to NtSetTimerResolution in WinMain, from reading around it seems that something like

    NtSetTimerResolution(10000, TRUE, &ulCurTimerRate);

Would set the tick rate to 1ms, (its specified in 100ns steps).

Can you give me some points on how to declare the API function above.

Cheers

Martin

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Higher resolution Timers in uTasker simulator
« Reply #3 on: June 08, 2009, 03:18:31 PM »
Hi Martin

It seems as though these functions are in a library called ntdll.lib but I couldn't find this on my PC.
It will also be necessary to have a header file for the prototype. Strangely the ntdll.h files that I found on the Internet don't actually have the call in them (?)

I think that it is necessary to add the library to the library list in the VS project (although some sources say that it should be loaded and not linked (?)).

Therefore I can't offer a great deal of help because Windows programming is not my strength at all. Perhaps someone else has an idea?

Regards

Mark