_time_diff_milliseconds returns 0

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

_time_diff_milliseconds returns 0

Jump to solution
751 Views
nitinharish
Contributor V

My BSP is set to 5msec with 200Hz alarm frequency.

I am trying to use this tiny piece of code to see if 1msec has passed or not (Is there any other way to attain this without re-compiling BSP with 1000Hz)

_time_get_ticks(&ticker_old);

while(1)

{

     _time_get_ticks(&ticker_new);

      int32_t timediff_msec = _time_diff_milliseconds(&ticker_new,&ticker_old, &overflow);

     if(timediff_msec >=1)

     {

          break;

     }

}

BUT "_time_diff_milliseconds" always returns 0...What am I doing wrong here ?

Tags (2)
1 Solution
477 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi Nitin,

I am sorry this is not possible to achieve with the function you try to use. In this case you need a hardware timer, you may find the following document interesting.

Using the MQX timers

Now, the reason why you are not able to use _time_diff_milliseconds is the same why it is not possible to have less that 5 ms delays. Please find the explanation below.

The systick is the smallest unit of time in MQX, it is used by MQX as the system clock and is the base of any delay or timer function. 

Systick is defined by default as follows:

#define BSP_ALARM_FREQUENCY (200).

This macro defines the number of ticks per second, default is 200 ticks per second, this means each tick is 5 milliseconds, therefore, the smallest delay you can have in your MQX application is 5 milliseconds. All values that are not multiple of 5 millisecond will be truncated.

In order to change the tick period you must calculate the value of #define BSP_ALARM_FREQUENCY ( ) to met your application's requirements. You can redeclare this macro in user_config.h. After editing this macro you must recompile BSP and PSP.

Delays are calculated in the following way:

# of ticks = # of miliseconds requested x ( # of ticks per second / 1000 miliseconds)

For example:

if BSP_ALARM_FREQUENCY is set as default (100) and you request a 55ms delay you will have: 55ms x (100 ticks / 1000ms) = 5.5 ticks This value will be truncated to 5 ticks.

Please note that modifying the systick may impact the kernel performance because the dispatcher will be called each time the systick expires.


Best regards,
Carlos

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

1 Reply
478 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi Nitin,

I am sorry this is not possible to achieve with the function you try to use. In this case you need a hardware timer, you may find the following document interesting.

Using the MQX timers

Now, the reason why you are not able to use _time_diff_milliseconds is the same why it is not possible to have less that 5 ms delays. Please find the explanation below.

The systick is the smallest unit of time in MQX, it is used by MQX as the system clock and is the base of any delay or timer function. 

Systick is defined by default as follows:

#define BSP_ALARM_FREQUENCY (200).

This macro defines the number of ticks per second, default is 200 ticks per second, this means each tick is 5 milliseconds, therefore, the smallest delay you can have in your MQX application is 5 milliseconds. All values that are not multiple of 5 millisecond will be truncated.

In order to change the tick period you must calculate the value of #define BSP_ALARM_FREQUENCY ( ) to met your application's requirements. You can redeclare this macro in user_config.h. After editing this macro you must recompile BSP and PSP.

Delays are calculated in the following way:

# of ticks = # of miliseconds requested x ( # of ticks per second / 1000 miliseconds)

For example:

if BSP_ALARM_FREQUENCY is set as default (100) and you request a 55ms delay you will have: 55ms x (100 ticks / 1000ms) = 5.5 ticks This value will be truncated to 5 ticks.

Please note that modifying the systick may impact the kernel performance because the dispatcher will be called each time the systick expires.


Best regards,
Carlos

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------