liteos software timer (X)

1 Overview

1.1 Basic Concepts

Software timer, is based on the system clock Tick simulated by software interrupts and a timer, when after Tick clock count value to a user defined triggers callback function. Tick ​​clock and timing accuracy of the system cycle.

Hardware timer by hardware limitations, insufficient to meet the actual needs of users on the number, so in order to meet customer needs, provide more timers, Huawei LiteOS operating system provides a software timer function.

Software Timer expands the number of timers, timing allows the creation of more business.

Software timer function support:

  • Static Cutting: Close the software through the macro timer function.
  • Create a software timer.
  • Software timer is started.
  • Software timer is stopped.
  • Software Delete timer.
  • Tick ​​acquire the remaining number of software timers

1.2 operating mechanism

Software timer is a system resource, the module initialization has been allocated a piece of contiguous memory, the maximum number supported by the system timer is configured by los_config.h in LOSCFG_BASE_CORE_SWTMR_LIMIT macro.

Software uses a timer queue and a task resource system, the software timer queue trigger follow rules
then, FIFO. Short timer has expired is always longer than the timing near the head of the queue time to meet the priority criteria are triggered.

Tick ​​software timer to the basic time unit, when a user creates and starts a software timer, HuaweiLiteOS determines Tick expiration time of the timer timing interval according to the current system time and Tick set by the user, and the timer control timing structure engaged global chained list.

When the interrupt arrives Tick, Tick interrupt timer in the global list handler scanning software timer to see if the timer expires, if the timeout timer will be recorded. After Tick interrupt handler, software timer task (highest priority) to be awakened, the timeout callback function recorded before calling in this task timer.

Timer status

  • OS_SWTMR_STATUS_UNUSED (not used)

System initialization module when the timer in the system is initialized to all the resources of the state timer.

  • OS_SWTMR_STATUS_CREATED (creation does not start / stop)

After calling LOS_SwtmrCreate unused state in the interface or the interface call LOS_SwtmrStop after startup, the given
time will become the state.

  • OS_SWTMR_STATUS_TICKING (count)

Call after the timer created LOS_SwtmrStart interfaces, a timer will turn into the state, it represents the state of the timer is running.

Timer Mode
Huawei LiteOS software timer provides two types of timer mechanism:

  • The first is the one-shot timer, this type of timer is triggered only once after starting the timer event, then the timer automatically deleted.
  • The second category is the cycle timer is triggered, this type of timer periodically triggers the timer event until the user manually stop the timer, otherwise it will go on forever execution

2. Development Guidance

2.1 usage scenarios

  • Create a one-shot timer, perform user-defined callback function after a timeout.
  • Create a periodic timer is triggered, the implementation of user-defined callback function after a timeout.

2.2 features

Huawei LiteOS system software timer module provides users with several features below, see the following specific API software timer external interface manual.

Functional Classification Interface name description
Create, delete timers LOS_SwtmrCreate Create a timer
- LOS_SwtmrDelete Delete timer
Start and stop the timer LOS_SwtmrStart Start timer
- LOS_SwtmrStop Stop the timer
Tick ​​to obtain the remaining number of software timers LOS_SwtmrTimeGet Tick ​​to obtain the remaining number of software timers

3. Development Process

Software Timer typical development process:

  1. Configuration software timer.
  • And confirm configuration item LOSCFG_BASE_CORE_SWTMR LOSCFG_BASE_IPC_QUEUE
    YES in an open state;
  • Configuring LOSCFG_BASE_CORE_SWTMR_LIMIT maximum number of supported software timer;
  • Configuration software timer OS_SWTMR_HANDLE_QUEUE_SIZE maximum queue length;
  1. Create a timer LOS_SwtmrCreate.
  • Create a long time the designated time, the specified timeout handler specified software timer trigger mode;
  • Function returns the operating results, success or failure;
  1. Start timer LOS_SwtmrStart.
  2. Access to software timer remaining Tick LOS_SwtmrTimeGet.
  3. Stop the timer LOS_SwtmrStop.
  4. Delete timer LOS_SwtmrDelete.

4. Software timer error code

There is the possibility of failure of the operation of the software timers, including create, delete, pause, restart timer, etc., are required to return an error code corresponding to quickly locate the cause of the error.

No. definition The actual value description Reference solutions
1 LOS_ERRNO_SWTMR_PTR_NULL 0x02000300 Software timer callback function is empty Custom software timer callback function
2 LOS_ERRNO_SWTMR_INTERVAL_NOT_SUITD 0x02000301 Software timer interval of 0 Redefine the interval
3 LOS_ERRNO_SWTMR_MODE_INVALI D 0x02000302 Incorrect software timer mode Software timer confirmation mode, in the range [0,2]
4 LOS_ERRNO_SWTMR_RET_PTR_NULL 0x02000303 Software timer ID is NULL pointer into parameters Variable definition ID, pointer passed
5 LOS_ERRNO_SWTMR_MAXSIZE 0x02000304 Software timer exceeds the maximum number Redefine the maximum number of timer, or wait for the release of a software timer resources
6 LOS_ERRNO_SWTMR_ID_INVALID 0x02000305 Incorrect software timer ID to the Senate To ensure that the legal parameters
7 LOS_ERRNO_SWTMR_NOT_CREATED 0x02000306 Software Timer not created Create a software timer
8 LOS_ERRNO_SWTMR_NO_MEMORY 0x02000307 Insufficient memory to create a software timer list Apply a large enough memory for software timers use
9 LOS_ERRNO_SWTMR_MAXSIZE_INVALID 0x02000308 Incorrect maximum number of software timers Redefine the value
10 LOS_ERRNO_SWTMR_HWI_ACTIVE 0x02000309 Using a timer interrupt Modify the source code to ensure that no interruption in the use
11 LOS_ERRNO_SWTMR_HANDLER_POOL_NO_MEM 0x0200030a membox out of memory Expanded memory
12 LOS_ERRNO_SWTMR_QUEUE_CREATE_FAILED 0x0200030b Software timer queue creation failed Check the memory used to create the queue adequacy
13 LOS_ERRNO_SWTMR_TASK_CREATE_FAILED 0x0200030c Software Timer task creation failed Check the memory task to create a software timer adequacy and recreate
14 LOS_ERRNO_SWTMR_NOT_STARTED 0x0200030d Software timer does not start Start the software timer
15 LOS_ERRNO_SWTMR_STATUS_INVALID 0x0200030e Incorrect software timer status Check that the software timer status
16 LOS_ERRNO_SWTMR_SORTLIST_NULL null No The error code is not in use
17 LOS_ERRNO_SWTMR_TICK_PTR_NULL 0x02000310 The argument pointer to get the number of tick timer expires software is NULL Create a valid variable

No Error: the error code is a 32-bit memory cells, 31 to 24 indicate an error level, 23 to 16 indicate an error code flags, 15 to 8 bits represent the error code belongs module 7 ~ 0 indicates an error code number, as follows

#define LOS_ERRNO_OS_NORMAL(MID,ERRNO) \
(LOS_ERRTYPE_NORMAL | LOS_ERRNO_OS_ID | ((UINT32)(MID) << 8) | (ERRNO))
LOS_ERRTYPE_NORMAL :Define the error level as critical
LOS_ERRNO_OS_ID :OS error code flag.
MID:OS_MOUDLE_ID
ERRNO:error ID number

E.g:

#define LOS_ERRNO_SWTMR_PTR_NULL \
LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x00)

5. Programming Examples

5.1 Example Description

In the following examples, demonstrate the following functions:

  1. Create a software timer, start, delete, pause, restart operation.
  2. A single software timer, software timer cycle use.

    5.2 Programming Example

    Prerequisites:
  • In los_config.h, the LOSCFG_BASE_CORE_SWTMR open configuration items.
  • LOSCFG_BASE_CORE_SWTMR_LIMIT configured to support the maximum number of software timers.
  • OS_SWTMR_HANDLE_QUEUE_SIZE configured software timer queue maximum length.

Code is implemented as follows:

void Timer1_Callback(uint32_t arg); // callback fuction
void Timer2_Callback(uint32_t arg);
UINT32 g_timercount1 = 0;
UINT32 g_timercount2 = 0;
void Timer1_Callback(uint32_t arg)//回调函数1
{
    unsigned long tick_last1;
    g_timercount1++;
    tick_last1=(UINT32)LOS_TickCountGet();//获取当前Tick数
    dprintf("g_timercount1=%d\n",g_timercount1);
    dprintf("tick_last1=%d\n",tick_last1);
}
void Timer2_Callback(uint32_t arg)//回调函数2
{
    unsigned long tick_last2;
    tick_last2=(UINT32)LOS_TickCountGet();
    g_timercount2 ++;
    dprintf("g_timercount2=%d\n",g_timercount2);
    dprintf("tick_last2=%d\n",tick_last2);
}
void Timer_example (void) {
    UINT16 id1;
    UINT16 id2;// timer id
    UINT32 uwTick;
    /*创建单次软件定时器,Tick数为1000,启动到1000Tick数时执行回调函数1 */
    LOS_SwtmrCreate (1000, LOS_SWTMR_MODE_ONCE,Timer1_Callback,&id1,1);
    /*创建周期性软件定时器,每100Tick数执行回调函数2 */
    LOS_SwtmrCreate(100,LOS_SWTMR_MODE_PERIOD,Timer2_Callback,&id2,1);
    dprintf("create Timer1 success\n");
    LOS_SwtmrStart (id1); //启动单次软件定时器
    dprintf("start Timer1 sucess\n");
    LOS_TaskDelay(200);//延时200Tick数
    LOS_SwtmrTimeGet(id1,&uwTick);//获得单次软件定时器剩余Tick数
    dprintf("uwTick =%d\n",uwTick);
    LOS_SwtmrStop(id1);//停止软件定时器
    dprintf("stop Timer1 sucess\n");
    LOS_SwtmrStart(id1);
    LOS_TaskDelay(1000);
    LOS_SwtmrDelete(id1);//删除软件定时器
    dprintf("delete Timer1 sucess\n");
    LOS_SwtmrStart(id2);//启动周期性软件定时器
    dprintf("start Timer2\n");
    LOS_TaskDelay(1000);
    LOS_SwtmrStop(id2);
    LOS_SwtmrDelete(id2);
}

5.3 verify the results

The results were as follows:

Guess you like

Origin www.cnblogs.com/linhaostudy/p/10963066.html