[RVB2601 Creative Application Development] User Experience 03 -- System Timer
[Copy link]
This creative project requires regular command issuance and polling of sensor nodes, so this article records the process of using aos_timer .
1. Introduction to system timer
The timer of AOS is based on the system tick as the most basic time scheduling unit, that is, the minimum time period. The YoC platform provides this basic software timer function. In the CDK project, the relevant functions are located in aos pack , and the APIs listed in README.md are as follows. For further description of these APIs , please refer to the link: https://yoc.docs.t-head.cn/yocbook/Chapter3-AliOS/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E6%8E%A5%E5%8F%A3/%E5%AE%9A%E6%97%B6%E5%99%A8.html .
Figure 3-1 Timer interface description
Analyzing the API , we know that the timer function needs to define the " aos_timer_t " structure variable, and pass its pointer to the function. The structure-related type definition is located in " ../PACK/aos/v7.4.3/include/aos/kernel.h ". In fact, this is a type redefinition, which is essentially a void pointer type.
typedef struct {
void *hdl;
} aos_hdl_t;
typedef aos_hdl_t aos_task_t;
typedef aos_hdl_t aos_mutex_t;
typedef aos_hdl_t aos_sem_t;
typedef aos_hdl_t aos_queue_t;
typedef aos_hdl_t aos_timer_t;
typedef aos_hdl_t aos_work_t;
typedef aos_hdl_t aos_event_t;
When tracing the code, it was found that the timer API was defined in the system kernel. In the " Hello World " case, AOS is based on the rhino kernel, so the relevant code for the timer implementation is located in " ../PACK/aos/v7.4.3/src/adapter/rhino/aos_rhino.c ".
Figure 3-2 Timer code location
In fact, there are two functions for creating timers, " aos_timer_new() " and " aos_timer_new_ext() ". The latter passes an extra parameter - " auto_run ", which means whether to start the timer automatically.
Figure 3-3 Screenshot of creating a timer function document
2. Timer use
My creative project requires timed data collection. The period is the period in the project parameters configured in the previous article . The original unit is minutes. In order to quickly see the debugging effect, I will use seconds as the unit.
The timer overflow callback is pollingTimerCb() , which has two parameters. Through log output debugging, it is found that the second parameter " arg2 " is the callback parameter set when the timer is created. The first parameter is found to be the address of the " hdl " member in the timer structure variable after analysis - hdl points to the real timer data structure.
void pollingTimerCb(void *arg1, void *arg2) {
char *s1 = (char *)arg1;
char *s2 = (char *)arg2;
LOGI(TAG, "polling timer callback! arg1:%s, arg2:%s\n", s1, s2);
if(arg1 == polling_timer.hdl)
LOGI(TAG, "arg1 is timer's address\n");
}
void aita_InitTimer(void) {
int ret = -1;
LOGI(TAG, "initialize timer for polling job!\n");
ret = aos_timer_new_ext(&polling_timer, pollingTimerCb, "args", period*1000, 1, 0);
if(ret != 0) {
LOGE(TAG, "polling_timer create failed\n");
}
LOGI(TAG, "polling timer start\n");
aos_timer_start(&polling_timer);
}
void board_yoc_init()
{
board_init();
console_init(CONSOLE_UART_IDX, 115200, 128);
ulog_init();
aos_set_log_level(AOS_LL_DEBUG);
LOGI(TAG, "Build:%s,%s\n",__DATE__, __TIME__);
/* load partition & init kv function */
aita_InitKV();
/* set cycle timer for polling job */
aita_InitTimer();
board_cli_init();
}
Figure 3-4 Timer function test
|