try to use _time_delay_until()

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

try to use _time_delay_until()

Jump to solution
2,128 Views
UB39
Contributor I

Hi!

 

I found many examples that use the _time_delay() function but no one that uses the _time_delay_until function.

I have a task that should be executed every 2 ms. I don t want to use the _time_delay() function cause later I ve some more code between my Gpio_toggle and my _time_delay_until instructions which length of time is between 0.5 and 1.5 ms.

So I wrote the following code. If I exchange 

 _time_delay_until(pcTickCounter);

with _time_delay(2);

my GPIO toggles every 2 ms, but with the _time_delay_until instruction, the code waits and waits and waits until I stop execution.

 

Do you know what I made wrong?

 

void fast_com (uint_32initial_data)

{

MQX_TICK_STRUCT_PTR pcTickCounter;

 

 

pcTickCounter=_mem_alloc(sizeof(MQX_TICK_STRUCT_PTR));

 

_time_get_ticks(pcTickCounter);

 

 while(1){

   _time_add_msec_to_ticks(pcTickCounter,2);

    Gpio_toggle(PORTE, GPIO_PIN_ACCESS(28));

   _time_delay_until(pcTickCounter);

  }

}

0 Kudos
Reply
1 Solution
881 Views
MarkP_
Contributor V

Hi,
Probably the time has passed by the delay generated by some higher priority task(s).
I have used the timer function to get 1 ms events.
1)Change tick from 10 ms to 1 ms by defining in user_config.h:
  #define BSP_ALARM_FREQUENCY (1000)

2)Timer event code principle:
void timerCallback(void *arg);

LWEVENT_STRUCT lwEvent;
#define MY_EVENT_01 0x01

void TimerTest(void)
{
  LWTIMER_PERIOD_STRUCT timerPeriodQueue;
  LWTIMER_STRUCT timer;

  _lwevent_create(&lwEvent, LWEVENT_AUTO_CLEAR);

  /* Create one ms timer & queue */    
  _lwtimer_create_periodic_queue(&timerPeriodQueue, 1, 0);
  _lwtimer_add_timer_to_queue(&timerPeriodQueue, &timer, 0, timerCallback, NULL);

  while(1)
  {
    _lwevent_wait_for(&lwEvent, MY_EVENT_01, TRUE, 0);
    /* Handle the jobs */
    ...
  }
}

void timerCallback(void *arg)
{
  _lwevent_set(&lwEvent, MY_EVENT_01);
}
~Mark

View solution in original post

0 Kudos
Reply
4 Replies
882 Views
MarkP_
Contributor V

Hi,
Probably the time has passed by the delay generated by some higher priority task(s).
I have used the timer function to get 1 ms events.
1)Change tick from 10 ms to 1 ms by defining in user_config.h:
  #define BSP_ALARM_FREQUENCY (1000)

2)Timer event code principle:
void timerCallback(void *arg);

LWEVENT_STRUCT lwEvent;
#define MY_EVENT_01 0x01

void TimerTest(void)
{
  LWTIMER_PERIOD_STRUCT timerPeriodQueue;
  LWTIMER_STRUCT timer;

  _lwevent_create(&lwEvent, LWEVENT_AUTO_CLEAR);

  /* Create one ms timer & queue */    
  _lwtimer_create_periodic_queue(&timerPeriodQueue, 1, 0);
  _lwtimer_add_timer_to_queue(&timerPeriodQueue, &timer, 0, timerCallback, NULL);

  while(1)
  {
    _lwevent_wait_for(&lwEvent, MY_EVENT_01, TRUE, 0);
    /* Handle the jobs */
    ...
  }
}

void timerCallback(void *arg)
{
  _lwevent_set(&lwEvent, MY_EVENT_01);
}
~Mark

0 Kudos
Reply
881 Views
UB39
Contributor I

Hi Mark!

 

Maybe I should try to use Timer events. But I thought my application should be possible to implement  with the _time_delay_until function.

 

My tick time is 1 ms. So this is not the reason for my problems.

 

And I have no higher priority tasks at the moment. So this is also not the reason.

 

And as I said: I have no problems if I use _time_delay_ticks instead of _time_delay_until

 

I only find examples for time_delay or time_delay_ticks. The same when I search here in Forum. I wonder why time_delay_ticks seems to be  a little bit exotic cause in my opinion time_delay_until should be more popular than time_delay or _time_delay_ticks.

 

Ulf 

0 Kudos
Reply
881 Views
MarkP_
Contributor V

Hi,

found a bug:

pcTickCounter=_mem_alloc(sizeof(MQX_TICK_STRUCT_PTR));

should be:

pcTickCounter=_mem_alloc(sizeof(MQX_TICK_STRUCT));

 

But still I recommend to use timer function with events.

At one day you will add higher priority task(s) or interrupt load increases,

resulting that the next 2 ms tick may be passed.

The event method works also in these cases: when entering into event wait

and if the event is already set the function returns immediatelly.

~Mark


0 Kudos
Reply
881 Views
RichardR
Contributor IV

For what it's worth, I have tried using the periodic timer    _timer_start_periodic_every(), and am having issues with it.  It continues to generate the periodic call back, which in turn is setting an event, but, it appears that after some randomish time frame, the MQX timer task starts running nonstop, never allowing the task that is supposed to be awakened by the event to run again.

0 Kudos
Reply