Hi Emmanuel,
Example below:
#include <mqx.h>
#include <bsp.h>
#include <fio.h>
#include <lwtimer.h>
#if ! BSPCFG_ENABLE_IO_SUBSYSTEM
#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.
#endif
#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.
#endif
/* This example calls _timer_create_component before it activates the
** timers so that the application can specify the timer task stack size.
** The stack size may need to be larger than the TIMER_DEFAULT_STACK_SIZE
** that is defined in timer.h, since the application's handler function uses
** this stack. In this example, the handlers are LED_on and LED_off,
** which have large stack requirements. You may need to increase the stack size
** for your target hardware.
*/
#define LWTIMER_TASK_PRIORITY 2
#define LWTIMER_STACK_SIZE 2000
#define MAIN_TASK 10
extern void main_task(uint_32);
extern void LED_on(void);
extern void LED_off(void);
volatile _mqx_uint LED_on_counter=0;
volatile _mqx_uint LED_off_counter=0;
const TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
/* Task Index, Function, Stack, Priority, Name, Attributes, Param, Time Slice */
{ MAIN_TASK, main_task, 2000, 8, "Main", MQX_AUTO_START_TASK, 0, 0 },
{ 0 }
};
/*FUNCTION*------------------------------------------------------
*
* Function Name : LED_on
* Returned Value : none
* Comments :
* This timer function prints out "ON"
*END*-----------------------------------------------------------*/
static void LED_on
(
)
{
LED_on_counter++;
}
/*FUNCTION*------------------------------------------------------
*
* Function Name : LED_off
* Returned Value : none
* Comments :
* This timer function prints out "OFF"
*END*-----------------------------------------------------------*/
static void LED_off
(
)
{
LED_off_counter++;
}
/*TASK*----------------------------------------------------------
*
* Task Name : main_task
* Comments :
* This task creates two timers, each of a period of 2 seconds,
* the second timer offset by 1 second from the first.
*END*-----------------------------------------------------------*/
void main_task
(
uint_32 initial_data
)
{
LWTIMER_PERIOD_STRUCT ps;
LWTIMER_STRUCT lst_on, lst_off;
_mqx_uint time=20;
_mqx_uint period=6;
_mqx_uint wait_ticks=0;
_mqx_uint delay_on=3;
_mqx_uint delay_off=3;
printf("\n\nTwo lwtimers are created, each of a period of %d ticks,\nthe second lwtimer offset by %d ticks from the first.\n", period, delay_off);
printf("Task runs for %d seconds,\nthen lwtimers are closed and task finishes.\n\n", time);
/*
** Create the lwtimer component with more stack than the default
** in order to handle printf() requirements:
*/
_lwtimer_create_periodic_queue(&ps, period, wait_ticks);
_lwtimer_add_timer_to_queue(&ps, &lst_on, delay_on, LED_on, 0);
_lwtimer_add_timer_to_queue(&ps, &lst_off, delay_off, LED_off, 0);
_time_delay(time * 1000); // wait 6 seconds
printf("\nThe task is finished!");
printf("\nLED_on_counter = %d LED_off_counter = %d", LED_on_counter, LED_off_counter);
_lwtimer_cancel_period(&ps);
_mqx_exit(0);
}
/* EOF */
Regards,
David