nrf5/timer: Adding initializaton of id field for Timer_HandleTypeDef's. Adding simple print function. Adding make_new function. Enabling the functions in machine_timer_type.

This commit is contained in:
Glenn Ruben Bakke 2016-12-26 16:13:09 +01:00
parent 53fdcf91d7
commit 9c828c7630

View File

@ -40,9 +40,9 @@ typedef struct _machine_timer_obj_t {
Timer_HandleTypeDef *timer;
} machine_timer_obj_t;
Timer_HandleTypeDef TimerHandle0 = {.instance = NULL};
Timer_HandleTypeDef TimerHandle1 = {.instance = NULL};
Timer_HandleTypeDef TimerHandle2 = {.instance = NULL};
Timer_HandleTypeDef TimerHandle0 = {.instance = NULL, .id = 0};
Timer_HandleTypeDef TimerHandle1 = {.instance = NULL, .id = 1};
Timer_HandleTypeDef TimerHandle2 = {.instance = NULL, .id = 2};
STATIC const machine_timer_obj_t machine_timer_obj[] = {
{{&machine_timer_type}, &TimerHandle0},
@ -60,7 +60,6 @@ void timer_init0(void) {
TimerHandle0.instance = TIMER2;
}
#if 0
STATIC int timer_find(mp_obj_t id) {
// given an integer id
int timer_id = mp_obj_get_int(id);
@ -71,14 +70,50 @@ STATIC int timer_find(mp_obj_t id) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Timer(%d) does not exist", timer_id));
}
#endif
STATIC void timer_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
machine_timer_obj_t *self = o;
mp_printf(print, "Timer(%u)", self->timer->id);
}
/******************************************************************************/
/* MicroPython bindings for machine API */
// for make_new
enum {
ARG_NEW_id,
};
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 *all_args) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)} },
};
// parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (args[ARG_NEW_id].u_obj == MP_OBJ_NEW_SMALL_INT(-1)) {
// index -1 does not exist
return mp_const_none;
// TODO: raise exception
}
// get static peripheral object
int timer_id = timer_find(args[ARG_NEW_id].u_obj);
const machine_timer_obj_t *self = &machine_timer_obj[timer_id];
hal_timer_init(self->timer->instance, &self->timer->init);
return MP_OBJ_FROM_PTR(self);
}
const mp_obj_type_t machine_timer_type = {
{ &mp_type_type },
.name = MP_QSTR_Timer,
#if 0
.print = machine_timer_print,
.print = timer_print,
.make_new = machine_timer_make_new,
#if 0
.locals_dict = (mp_obj_t)&machine_timer_locals_dict
#endif
};