[文章]怎样去移植基于Hi3861 micropython的Timer定时器呢

阅读量0
0
2
1、移植的准备
Timer主要用于定时触发事件使用。移植相对容易。需要#include "hi_timer.h"中的相关函数。
2、machine_timer_obj_t定义
typedef struct _machine_timer_obj_t {
    mp_obj_base_t base;
    mp_obj_t timeout_cb;
    uint32_t mode;
    uint32_t period;
    hi_u32 g_timer_handle;  
    struct _machine_timer_obj_t *next;
} machine_timer_obj_t;
定义hi_u32 g_timer_handle用于存储定时器handle
3、创建Timer定时器
STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
    machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
    self->base.type = &machine_timer_type;
    hi_u32 ret;
    ret = hi_timer_create(&self->g_timer_handle);
    if (ret != HI_ERR_SUCCESS) {
        mp_raise_ValueError("timer create fail");
    }
    // Add the timer to the linked-list of timers
    self->next = MP_STATE_PORT(machine_timer_obj_head);
    MP_STATE_PORT(machine_timer_obj_head) = self;
    return MP_OBJ_FROM_PTR(self);
}
4、开启定时器
STATIC mp_obj_t machine_timer_start(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
    machine_timer_obj_t *self = (machine_timer_obj_t *)args[0];
    int mode = 0;
    enum {
        ARG_mode,
        ARG_period,
        ARG_callback,
    };
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_mode,         MP_ARG_INT, {.u_int = 0} },
        { MP_QSTR_period,       MP_ARG_INT, {.u_int = 0xffffffff} },
        { MP_QSTR_callback,     MP_ARG_OBJ, {.u_obj = mp_const_none} },
    };
    mp_arg_val_t dargs[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, dargs);
    if (4 == n_args) {
        self->mode = dargs[ARG_mode].u_int;
        self->period = dargs[ARG_period].u_int;
        self->timeout_cb = dargs[ARG_callback].u_obj;
    } else {
        mp_raise_ValueError("invalid format");
    }
    error_check(self->period > 0, "Set timeout value error");
    int res = hi_timer_start(self->g_timer_handle,self->mode,self->period,timer_event_handler,(void*)self);
    if (res != HI_ERR_SUCCESS) {
        mp_raise_ValueError("timer start fail");
    }
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_start_obj, 1, machine_timer_start);
5、关闭定时器
STATIC mp_obj_t machine_timer_stop(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
    machine_timer_obj_t *self = (machine_timer_obj_t *)args[0];
    int ret = hi_timer_stop(self->g_timer_handle);
    if (ret != HI_ERR_SUCCESS) {
        mp_raise_ValueError("timer stop fail");
    }
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_stop_obj, 1, machine_timer_stop);
6、定时器的回调
STATIC void timer_event_handler(void *arg) {
    machine_timer_obj_t *self = arg;
    //printf("machine_pin_isr_handler = %drn",self->pin);
    mp_sched_schedule(self->timeout_cb, MP_OBJ_FROM_PTR(self));
}
回调的使用和Pin的类似。
7、python的使用
from machine import Timer
import time
def callback_periodic(obj): # defined  preiodic mode timeout callback
    print("Timer callback periodic test")
def callback_oneshot(obj): # defined  ont shot mode timeout callback
    print("Timer callback oneshot test")
print("start")
timer = Timer() # Create Timer object, driver support timer1/timer2/timer3
timer.start(timer.PERIODIC, 1000, callback_periodic) # Initialize the Timer device object
# Set Timer mode to preiodic mode, set timeout to 1 seconds and set callback fucntion
time.sleep(5)# Execute 5 times timeout callback in the delay time
timer.start(timer.ONE_SHOT, 1000, callback_oneshot) # Reset initialize the Timer device object
# Set Timer mode to one shot mode, set timeout to 1 seconds and set callback fucntion
time.sleep(5) # Execute 1 times timeout callback in the delay time
timer.deinit()  # Stop and close Timer device object

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友