Skip to content

Commit 4f30bea

Browse files
committed
mimxrt: Clean up the Encoder/Counter class.
- Remove the match=xx keyword from init() and thematch() method. - Add a value=xxx keyowrd option to the irq() method for setting the match value. Using value=xxxx implies setting IRQ_MATCH. - Make all irq() argmens keyword_only, because value= implies trigger=, and this a strict ordering and number cannot be assumed. - Remove the 'reverse' keyword argument.
1 parent f96c96c commit 4f30bea

File tree

1 file changed

+24
-41
lines changed

1 file changed

+24
-41
lines changed

ports/mimxrt/machine_qecnt.c

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ STATIC uint32_t calc_filter(uint32_t filter_ns, uint16_t *count, uint16_t *perio
263263
STATIC void mp_machine_qencd_init_helper_common(machine_qencd_obj_t *self,
264264
mp_arg_val_t args[], enc_config_t *enc_config) {
265265

266-
enum { ARG_match_pin, ARG_filter_ns, ARG_cpc, ARG_match, ARG_signed, ARG_index };
266+
enum { ARG_match_pin, ARG_filter_ns, ARG_cpc, ARG_signed, ARG_index };
267267

268268
// Check for a Match pin for the compare match signal
269269
if (args[ARG_match_pin].u_obj != MP_ROM_INT(-1)) {
@@ -295,10 +295,6 @@ STATIC void mp_machine_qencd_init_helper_common(machine_qencd_obj_t *self,
295295
}
296296
}
297297

298-
if (args[ARG_match].u_obj != mp_const_none) {
299-
enc_config->positionCompareValue = mp_obj_int_get_truncated(args[ARG_match].u_obj);
300-
}
301-
302298
if (args[ARG_signed].u_int >= 0) {
303299
self->is_signed = !!args[ARG_signed].u_int;
304300
}
@@ -323,18 +319,16 @@ STATIC void mp_machine_qencd_init_helper_common(machine_qencd_obj_t *self,
323319

324320
STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
325321
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
326-
enum { ARG_phase_a, ARG_phase_b, ARG_home, ARG_reverse,
327-
ARG_match_pin, ARG_filter_ns, ARG_cpc, ARG_match, ARG_signed, ARG_index};
322+
enum { ARG_phase_a, ARG_phase_b, ARG_home,
323+
ARG_match_pin, ARG_filter_ns, ARG_cpc, ARG_signed, ARG_index};
328324

329325
static const mp_arg_t allowed_args[] = {
330326
{ MP_QSTR_phase_a, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
331327
{ MP_QSTR_phase_b, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
332328
{ MP_QSTR_home, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
333-
{ MP_QSTR_reverse, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
334-
{ MP_QSTR_match, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
329+
{ MP_QSTR_match_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
335330
{ MP_QSTR_filter_ns, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
336331
{ MP_QSTR_cpc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
337-
{ MP_QSTR_match, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
338332
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
339333
{ MP_QSTR_index, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
340334
};
@@ -365,10 +359,6 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
365359
}
366360
}
367361

368-
if (args[ARG_reverse].u_int >= 0) {
369-
self->enc_config.enableReverseDirection = !!args[ARG_reverse].u_int;
370-
}
371-
372362
// Set the common options
373363
mp_machine_qencd_init_helper_common(self, args + ARG_match_pin, &self->enc_config);
374364

@@ -494,42 +484,38 @@ STATIC mp_obj_t machine_qencd_cycles(size_t n_args, const mp_obj_t *args) {
494484
}
495485
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_qencd_cycles_obj, 1, 2, machine_qencd_cycles);
496486

497-
// qencd.match([value])
498-
STATIC mp_obj_t machine_qencd_match(size_t n_args, const mp_obj_t *args) {
499-
machine_qencd_obj_t *self = MP_OBJ_TO_PTR(args[0]);
500-
if (n_args == 1) {
501-
return mp_obj_new_int_from_uint(self->enc_config.positionCompareValue);
502-
} else {
503-
// Set the match_pos value
504-
uint32_t match = mp_obj_int_get_truncated(args[1]);
505-
self->enc_config.positionCompareValue = match;
506-
self->instance->LCOMP = (uint16_t)(match) & 0xffff; /* Lower 16 pos bits. */
507-
self->instance->UCOMP = (uint16_t)(match >> 16U) & 0xffff; /* Upper 16 pos bits. */
508-
return mp_const_none;
509-
}
510-
}
511-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_qencd_match_obj, 1, 2, machine_qencd_match);
512-
513487
// encoder.irq(trigger=ENCODER.IRQ_MATCH, handler=None, hard=False)
514488
STATIC mp_obj_t machine_qencd_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
515-
enum { ARG_trigger, ARG_handler, ARG_hard };
489+
enum { ARG_trigger, ARG_value, ARG_handler, ARG_hard };
516490
static const mp_arg_t allowed_args[] = {
517-
{ MP_QSTR_trigger, MP_ARG_INT, {.u_int = 0} },
518-
{ MP_QSTR_handler, MP_ARG_OBJ, {.u_obj = mp_const_none} },
519-
{ MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} },
491+
{ MP_QSTR_trigger, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
492+
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
493+
{ MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
494+
{ MP_QSTR_hard, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
520495
};
521496
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
522497
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
523-
524498
machine_qencd_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
525-
uint16_t trigger = args[ARG_trigger].u_int &
526-
(ENCODER_TRIGGER_MATCH | ENCODER_TRIGGER_ROLL_UNDER | ENCODER_TRIGGER_ROLL_OVER);
499+
527500
if (self->irq == NULL) {
528501
self->irq = m_new_obj(mp_irq_obj_t);
529502
self->irq->base.type = &mp_irq_type;
530503
self->irq->parent = MP_OBJ_FROM_PTR(self);
531504
self->irq->methods = NULL;
505+
self->irq->ishard = false;
506+
}
507+
508+
uint16_t trigger = args[ARG_trigger].u_int &
509+
(ENCODER_TRIGGER_MATCH | ENCODER_TRIGGER_ROLL_UNDER | ENCODER_TRIGGER_ROLL_OVER);
510+
511+
if (args[ARG_value].u_obj != mp_const_none) {
512+
uint32_t value = mp_obj_int_get_truncated(args[ARG_value].u_obj);
513+
self->enc_config.positionCompareValue = value;
514+
self->instance->LCOMP = (uint16_t)(value) & 0xffff; /* Lower 16 pos bits. */
515+
self->instance->UCOMP = (uint16_t)(value >> 16U) & 0xffff; /* Upper 16 pos bits. */
516+
trigger |= ENCODER_TRIGGER_MATCH;
532517
}
518+
533519
self->irq->handler = args[ARG_handler].u_obj;
534520
self->irq->ishard = args[ARG_hard].u_bool;
535521
self->requested_irq = trigger;
@@ -556,7 +542,6 @@ MP_DEFINE_CONST_FUN_OBJ_KW(machine_qencd_init_obj, 1, machine_qencd_init);
556542

557543
STATIC const mp_rom_map_elem_t machine_qencd_locals_dict_table[] = {
558544
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_qencd_deinit_obj) },
559-
{ MP_ROM_QSTR(MP_QSTR_match), MP_ROM_PTR(&machine_qencd_match_obj) },
560545
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_qencd_init_obj) },
561546
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_qencd_irq_obj) },
562547
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_qencd_value_obj) },
@@ -581,14 +566,13 @@ const mp_obj_type_t machine_qencd_type = {
581566

582567
STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
583568
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
584-
enum { ARG_src, ARG_direction, ARG_match_pin, ARG_filter_ns, ARG_cpc, ARG_match, ARG_signed, ARG_index };
569+
enum { ARG_src, ARG_direction, ARG_match_pin, ARG_filter_ns, ARG_cpc, ARG_signed, ARG_index };
585570
static const mp_arg_t allowed_args[] = {
586571
{ MP_QSTR_src, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
587572
{ MP_QSTR_direction, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
588573
{ MP_QSTR_match_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
589574
{ MP_QSTR_filter_ns, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
590575
{ MP_QSTR_cpc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
591-
{ MP_QSTR_match, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
592576
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
593577
{ MP_QSTR_index, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
594578
};
@@ -680,7 +664,6 @@ MP_DEFINE_CONST_FUN_OBJ_KW(machine_counter_init_obj, 1, machine_counter_init);
680664

681665
STATIC const mp_rom_map_elem_t machine_counter_locals_dict_table[] = {
682666
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_qencd_deinit_obj) },
683-
{ MP_ROM_QSTR(MP_QSTR_match), MP_ROM_PTR(&machine_qencd_match_obj) },
684667
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_qencd_value_obj) },
685668
{ MP_ROM_QSTR(MP_QSTR_cycles), MP_ROM_PTR(&machine_qencd_cycles_obj) },
686669
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_counter_init_obj) },

0 commit comments

Comments
 (0)