Skip to content

Commit 84dcd9e

Browse files
committed
mimxrt: Some refactoring of the Encoder/Counter classes.
- rename the revolution() and overflow() method to cycles() - rename the option cpr to cpc (counts per cycle) and enable the option for the Counter class as well. - Move common init code to a common helper function. - Simplify the object print function. Allowing a modulus counting for a modulus smaller than 2**32. As a result of the changemthe print function is simplfied. Further optimization is possible for common code of the init_helper functions.
1 parent 2f60079 commit 84dcd9e

File tree

1 file changed

+63
-77
lines changed

1 file changed

+63
-77
lines changed

ports/mimxrt/machine_qecnt.c

Lines changed: 63 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,9 @@ __attribute__((section(".ram_functions"))) void ENC4_IRQHandler(void) {
158158

159159
STATIC void mp_machine_qencd_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
160160
machine_qencd_obj_t *self = MP_OBJ_TO_PTR(self_in);
161-
if (self->mode == MODE_ENCODER) {
162-
mp_printf(print, "<Encoder cpr=%lu compare=%lu filter=%luns>\n",
163-
self->cpr, self->compare_pos, self->filter);
164-
} else {
165-
mp_printf(print, "<Counter compare=%lu filter=%luns>\n",
166-
self->compare_pos, self->filter);
167-
}
161+
mp_printf(print, "%s cpr=%lu compare=%lu filter=%luns>\n",
162+
self->mode == MODE_ENCODER ? "<Encoder" : "<Counter",
163+
self->cpr, self->compare_pos, self->filter);
168164
}
169165

170166
// Utililty functions
@@ -293,18 +289,58 @@ STATIC uint32_t calc_filter(uint32_t filter_ns, uint16_t *count, uint16_t *perio
293289

294290
// Micropython API functions
295291
//
292+
STATIC void mp_machine_qencd_init_helper_common(machine_qencd_obj_t *self,
293+
mp_arg_val_t args[], enc_config_t *enc_config) {
294+
295+
enum { ARG_match, ARG_filter, ARG_cpc, ARG_compare, ARG_signed };
296+
297+
if (args[ARG_signed].u_int != 0) {
298+
self->is_signed = true;
299+
}
300+
301+
if (args[ARG_cpc].u_obj != mp_const_none) {
302+
uint32_t cpr = mp_get_ll_int(args[ARG_cpc].u_obj);
303+
self->cpr = cpr;
304+
if (cpr == 0) {
305+
enc_config->enableModuloCountMode = false;
306+
enc_config->positionModulusValue = 0;
307+
enc_config->positionInitialValue = 0;
308+
} else {
309+
enc_config->enableModuloCountMode = true;
310+
enc_config->positionModulusValue = cpr - 1;
311+
}
312+
}
313+
314+
// Check for a Match pin for the compare match signal
315+
mp_obj_t match = args[ARG_match].u_obj;
316+
if (match != MP_ROM_INT(-1)) {
317+
self->match_pin = connect_pin_to_encoder(match, xbar_ouput_table[self->id].enc_match, XBAR_OUT);
318+
}
319+
320+
if (args[ARG_compare].u_obj != mp_const_none) {
321+
uint32_t compare = mp_get_ll_int(args[ARG_compare].u_obj);
322+
self->compare_pos = compare;
323+
enc_config->positionCompareValue = compare;
324+
}
325+
326+
if (args[ARG_filter].u_int >= 0) {
327+
self->filter = calc_filter(args[ARG_filter].u_int,
328+
&(enc_config->filterCount), &(enc_config->filterSamplePeriod));
329+
}
330+
}
331+
296332
STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
297333
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
298-
enum { ARG_index, ARG_home, ARG_match, ARG_filter, ARG_reverse, ARG_cpr, ARG_compare, ARG_signed};
334+
enum { ARG_match, ARG_filter, ARG_cpc, ARG_compare, ARG_signed, ARG_index, ARG_home, ARG_reverse };
299335
static const mp_arg_t allowed_args[] = {
300-
{ MP_QSTR_index, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
301-
{ MP_QSTR_home, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
302336
{ MP_QSTR_match, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
303337
{ MP_QSTR_filter, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
304-
{ MP_QSTR_reverse, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
305-
{ MP_QSTR_cpr, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
338+
{ MP_QSTR_cpc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
306339
{ MP_QSTR_compare, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
307340
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
341+
{ MP_QSTR_index, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
342+
{ MP_QSTR_home, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
343+
{ MP_QSTR_reverse, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
308344
};
309345
enc_config_t enc_config;
310346

@@ -314,31 +350,17 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
314350

315351
ENC_GetDefaultConfig(&enc_config);
316352

353+
// Set the first common options
354+
mp_machine_qencd_init_helper_common(self, args, &enc_config);
355+
317356
// Allow the trigger pulse to clear the registers
318357
enc_config.enableTRIGGERClearPositionCounter = true;
319358

320-
// Process the keyword arguments
359+
// Process the remaining keyword arguments
321360
if (args[ARG_reverse].u_int != 0) {
322361
enc_config.enableReverseDirection = true;
323362
}
324363

325-
if (args[ARG_signed].u_int != 0) {
326-
self->is_signed = true;
327-
}
328-
329-
if (args[ARG_cpr].u_obj != mp_const_none) {
330-
uint32_t cpr = mp_get_ll_int(args[ARG_cpr].u_obj);
331-
self->cpr = cpr;
332-
if (cpr == 0) {
333-
enc_config.enableModuloCountMode = false;
334-
enc_config.positionModulusValue = 0;
335-
enc_config.positionInitialValue = 0;
336-
} else {
337-
enc_config.enableModuloCountMode = true;
338-
enc_config.positionModulusValue = cpr - 1;
339-
}
340-
}
341-
342364
// Count revolutions on RollOverModulus or index pulse
343365
enc_config.revolutionCountCondition = kENC_RevolutionCountOnRollOverModulus;
344366
mp_obj_t index = args[ARG_index].u_obj;
@@ -356,23 +378,6 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
356378
enc_config.HOMETriggerMode = kENC_HOMETriggerOnRisingEdge;
357379
}
358380

359-
// Check for a Match pin for the compare match signal
360-
mp_obj_t match = args[ARG_match].u_obj;
361-
if (match != MP_ROM_INT(-1)) {
362-
self->match_pin = connect_pin_to_encoder(match, xbar_ouput_table[self->id].enc_match, XBAR_OUT);
363-
}
364-
365-
if (args[ARG_compare].u_obj != mp_const_none) {
366-
uint32_t compare = mp_get_ll_int(args[ARG_compare].u_obj);
367-
self->compare_pos = compare;
368-
enc_config.positionCompareValue = compare;
369-
}
370-
371-
if (args[ARG_filter].u_int >= 0) {
372-
self->filter = calc_filter(args[ARG_filter].u_int,
373-
&enc_config.filterCount, &enc_config.filterSamplePeriod);
374-
}
375-
376381
// Initialize the ENC module.
377382
ENC_Init(self->instance, &enc_config);
378383
clear_encoder_registers(self);
@@ -480,8 +485,8 @@ STATIC mp_obj_t machine_qencd_value(size_t n_args, const mp_obj_t *args) {
480485
}
481486
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_qencd_value_obj, 1, 2, machine_qencd_value);
482487

483-
// qencd.revolution([value])
484-
STATIC mp_obj_t machine_qencd_revolution(size_t n_args, const mp_obj_t *args) {
488+
// qencd.cycles([value])
489+
STATIC mp_obj_t machine_qencd_cycles(size_t n_args, const mp_obj_t *args) {
485490
machine_qencd_obj_t *self = MP_OBJ_TO_PTR(args[0]);
486491
if (n_args == 1) {
487492
// Get the revolution counter as unsigned int.
@@ -492,7 +497,7 @@ STATIC mp_obj_t machine_qencd_revolution(size_t n_args, const mp_obj_t *args) {
492497
return mp_const_none;
493498
}
494499
}
495-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_qencd_revolution_obj, 1, 2, machine_qencd_revolution);
500+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_qencd_cycles_obj, 1, 2, machine_qencd_cycles);
496501

497502
// qencd.compare([value])
498503
STATIC mp_obj_t machine_qencd_compare(size_t n_args, const mp_obj_t *args) {
@@ -561,7 +566,7 @@ STATIC const mp_rom_map_elem_t machine_qencd_locals_dict_table[] = {
561566
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_qencd_init_obj) },
562567
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_qencd_irq_obj) },
563568
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_qencd_value_obj) },
564-
{ MP_ROM_QSTR(MP_QSTR_revolution), MP_ROM_PTR(&machine_qencd_revolution_obj) },
569+
{ MP_ROM_QSTR(MP_QSTR_cycles), MP_ROM_PTR(&machine_qencd_cycles_obj) },
565570
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&machine_qencd_status_obj) },
566571

567572
{ MP_ROM_QSTR(MP_QSTR_COMPARE), MP_ROM_INT(ENCODER_TRIGGER_COMPARE) },
@@ -583,13 +588,14 @@ const mp_obj_type_t machine_qencd_type = {
583588

584589
STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
585590
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
586-
enum { ARG_direction, ARG_match, ARG_filter, ARG_compare, ARG_signed};
591+
enum {ARG_match, ARG_filter, ARG_cpc, ARG_compare, ARG_signed, ARG_direction };
587592
static const mp_arg_t allowed_args[] = {
588-
{ MP_QSTR_direction, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
589593
{ MP_QSTR_match, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
590594
{ MP_QSTR_filter, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
595+
{ MP_QSTR_cpc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
591596
{ MP_QSTR_compare, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
592597
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = false} },
598+
{ MP_QSTR_direction, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
593599
};
594600
enc_config_t enc_config;
595601

@@ -604,7 +610,9 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
604610
enc_config.revolutionCountCondition = kENC_RevolutionCountOnRollOverModulus;
605611
enc_config.enableTRIGGERClearPositionCounter = true;
606612

607-
// Process the keyword arguments
613+
// Set the first common options
614+
mp_machine_qencd_init_helper_common(self, args, &enc_config);
615+
608616
mp_obj_t direction = args[ARG_direction].u_obj;
609617
if (direction != MP_ROM_INT(-1)) {
610618
if (direction == MP_ROM_INT(COUNTER_UP)) {
@@ -616,28 +624,6 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
616624
}
617625
}
618626

619-
// Check for a Match pin for the compare match signal
620-
mp_obj_t match = args[ARG_match].u_obj;
621-
if (match != MP_ROM_INT(-1)) {
622-
self->match_pin = connect_pin_to_encoder(match, xbar_ouput_table[self->id].enc_match, XBAR_OUT);
623-
}
624-
625-
if (args[ARG_compare].u_obj != mp_const_none) {
626-
uint64_t compare = mp_get_ll_int(args[ARG_compare].u_obj);
627-
self->compare_pos = compare & 0xffffffff;
628-
self->compare_rev = (uint16_t)(compare >> 32U) & 0xffff;
629-
enc_config.positionCompareValue = self->compare_pos;
630-
}
631-
632-
if (args[ARG_filter].u_int >= 0) {
633-
self->filter = calc_filter(args[ARG_filter].u_int,
634-
&enc_config.filterCount, &enc_config.filterSamplePeriod);
635-
}
636-
637-
if (args[ARG_signed].u_int != 0) {
638-
self->is_signed = true;
639-
}
640-
641627
// Initialize the ENC module.
642628
ENC_Init(self->instance, &enc_config);
643629
clear_encoder_registers(self);
@@ -695,7 +681,7 @@ STATIC const mp_rom_map_elem_t machine_counter_locals_dict_table[] = {
695681
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_qencd_deinit_obj) },
696682
{ MP_ROM_QSTR(MP_QSTR_compare), MP_ROM_PTR(&machine_qencd_compare_obj) },
697683
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_qencd_value_obj) },
698-
{ MP_ROM_QSTR(MP_QSTR_overflow), MP_ROM_PTR(&machine_qencd_revolution_obj) },
684+
{ MP_ROM_QSTR(MP_QSTR_cycles), MP_ROM_PTR(&machine_qencd_cycles_obj) },
699685
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_counter_init_obj) },
700686
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_qencd_irq_obj) },
701687
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&machine_qencd_status_obj) },

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