Skip to content

Commit 23d151c

Browse files
committed
mimxrt: Move the Encoder/Counter pin handling back to init_helper().
To comply with the common documenation.
1 parent 1802ef7 commit 23d151c

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

ports/mimxrt/machine_qecnt.c

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ typedef struct _machine_qencd_obj_t {
4242
mp_obj_base_t base;
4343
ENC_Type *instance;
4444
int8_t id;
45+
uint8_t input_a;
46+
uint8_t input_b;
4547
uint8_t mode;
4648
bool is_signed;
4749
uint8_t match_pin;
@@ -343,9 +345,12 @@ STATIC void mp_machine_qencd_init_helper_common(machine_qencd_obj_t *self,
343345

344346
STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
345347
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
346-
enum { ARG_home, ARG_reverse, ARG_match, ARG_filter_ns, ARG_cpc, ARG_compare, ARG_signed, ARG_index};
348+
enum { ARG_phase_a, ARG_phase_b, ARG_home, ARG_reverse,
349+
ARG_match, ARG_filter_ns, ARG_cpc, ARG_compare, ARG_signed, ARG_index};
347350

348351
static const mp_arg_t allowed_args[] = {
352+
{ MP_QSTR_phase_a, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
353+
{ MP_QSTR_phase_b, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
349354
{ MP_QSTR_home, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
350355
{ MP_QSTR_reverse, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
351356
{ MP_QSTR_match, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
@@ -360,6 +365,18 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
360365
MP_ARRAY_SIZE(allowed_args), allowed_args, args);
361366

362367
// Process the Encoder specific keyword arguments
368+
// Get referred Pin object(s) and connect them to the encoder
369+
if (args[ARG_phase_a].u_obj != mp_const_none) {
370+
self->input_a = connect_pin_to_encoder(args[ARG_phase_a].u_obj, xbar_signal_table[self->id].enc_input_a, XBAR_IN);
371+
}
372+
if (args[ARG_phase_b].u_obj != mp_const_none) {
373+
self->input_b = connect_pin_to_encoder(args[ARG_phase_b].u_obj, xbar_signal_table[self->id].enc_input_b, XBAR_IN);
374+
}
375+
// Check for valid input pins
376+
if (self->input_a == 0 || self->input_b == 0 || self->input_a == self->input_b) {
377+
mp_raise_ValueError(MP_ERROR_TEXT("invalid or missing input pins"));
378+
}
379+
363380
// Check for a Home pin, resetting the counters
364381
if (args[ARG_home].u_obj != MP_ROM_INT(-1)) {
365382
if (args[ARG_home].u_obj != mp_const_none) {
@@ -383,7 +400,7 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
383400
// Qencoder(id, input_a, input_b, [args])
384401
STATIC mp_obj_t mp_machine_qencd_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
385402
// Check number of arguments
386-
mp_arg_check_num(n_args, n_kw, 3, MP_OBJ_FUN_ARGS_MAX, true);
403+
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
387404

388405
CLOCK_EnableClock(kCLOCK_Iomuxc); // just in case it was not set yet
389406
XBARA_Init(XBARA1);
@@ -397,21 +414,15 @@ STATIC mp_obj_t mp_machine_qencd_make_new(const mp_obj_type_t *type, size_t n_ar
397414
qenc_deinit_single(qencd_table[id]);
398415
}
399416

400-
// Get referred Pin object(s) and connect them to the encoder
401-
uint8_t input_a = connect_pin_to_encoder(args[1], xbar_signal_table[id].enc_input_a, XBAR_IN);
402-
uint8_t input_b = connect_pin_to_encoder(args[2], xbar_signal_table[id].enc_input_b, XBAR_IN);
403-
// Check for valid input pins
404-
if (input_a == input_b) {
405-
mp_raise_ValueError(MP_ERROR_TEXT("input pins must be diffrent"));
406-
}
407-
408417
// Connect the trigger input to low level
409418
XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicLow, xbar_signal_table[id].enc_trigger);
410419

411420
// Create and populate the Qencoder object.
412421
machine_qencd_obj_t *self = m_new_obj(machine_qencd_obj_t);
413422
qencd_table[id] = self;
414423
self->id = id;
424+
self->input_a = 0;
425+
self->input_b = 0;
415426
self->base.type = &machine_qencd_type;
416427
self->instance = enc_instances[id + 1];
417428
self->cpc = 0;
@@ -429,7 +440,7 @@ STATIC mp_obj_t mp_machine_qencd_make_new(const mp_obj_type_t *type, size_t n_ar
429440
// Process the remaining parameters
430441
mp_map_t kw_args;
431442
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
432-
mp_machine_qencd_init_helper(self, n_args - 3, args + 3, &kw_args);
443+
mp_machine_qencd_init_helper(self, n_args - 1, args + 1, &kw_args);
433444

434445
return MP_OBJ_FROM_PTR(self);
435446
}
@@ -592,8 +603,9 @@ const mp_obj_type_t machine_qencd_type = {
592603

593604
STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
594605
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
595-
enum { ARG_direction, ARG_match, ARG_filter_ns, ARG_cpc, ARG_compare, ARG_signed, ARG_index };
606+
enum { ARG_src, ARG_direction, ARG_match, ARG_filter_ns, ARG_cpc, ARG_compare, ARG_signed, ARG_index };
596607
static const mp_arg_t allowed_args[] = {
608+
{ MP_QSTR_src, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
597609
{ MP_QSTR_direction, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
598610
{ MP_QSTR_match, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
599611
{ MP_QSTR_filter_ns, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
@@ -607,6 +619,13 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
607619
mp_arg_parse_all(n_args, pos_args, kw_args,
608620
MP_ARRAY_SIZE(allowed_args), allowed_args, args);
609621

622+
if (args[ARG_src].u_obj != mp_const_none) {
623+
self->input_a = connect_pin_to_encoder(args[ARG_src].u_obj, xbar_signal_table[self->id].enc_input_a, XBAR_IN);
624+
}
625+
if (self->input_a == 0) {
626+
mp_raise_ValueError(MP_ERROR_TEXT("missing input pin"));
627+
}
628+
610629
mp_obj_t direction = args[ARG_direction].u_obj;
611630
if (direction != MP_ROM_INT(-1)) {
612631
if (direction == MP_ROM_INT(COUNTER_UP)) {
@@ -625,7 +644,7 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
625644
// Counter(id, input, [args])
626645
STATIC mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
627646
// Check number of arguments
628-
mp_arg_check_num(n_args, n_kw, 2, MP_OBJ_FUN_ARGS_MAX, true);
647+
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
629648

630649
CLOCK_EnableClock(kCLOCK_Iomuxc); // just in case it was not set yet
631650
XBARA_Init(XBARA1);
@@ -639,9 +658,6 @@ STATIC mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_
639658
qenc_deinit_single(qencd_table[id]);
640659
}
641660

642-
// Get input Pin object and connect it to the encoder
643-
connect_pin_to_encoder(args[1], xbar_signal_table[id].enc_input_a, XBAR_IN);
644-
645661
// Connect input_b and the trigger input to a fixed level.
646662
XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicLow, xbar_signal_table[id].enc_input_b);
647663
XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicLow, xbar_signal_table[id].enc_trigger);
@@ -650,6 +666,8 @@ STATIC mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_
650666
machine_qencd_obj_t *self = m_new_obj(machine_qencd_obj_t);
651667
qencd_table[id] = self;
652668
self->id = id;
669+
self->input_a = 0;
670+
self->input_b = 0;
653671
self->base.type = &machine_counter_type;
654672
self->instance = enc_instances[id + 1];
655673
self->cpc = 0;
@@ -670,7 +688,7 @@ STATIC mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_
670688
// Process the remaining parameters
671689
mp_map_t kw_args;
672690
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
673-
mp_machine_counter_init_helper(self, n_args - 2, args + 2, &kw_args);
691+
mp_machine_counter_init_helper(self, n_args - 1, args + 1, &kw_args);
674692

675693
return MP_OBJ_FROM_PTR(self);
676694
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy