8000 mimxrt: Add signed/unsigned option for Encoder and Counter. · micropython/micropython@f2a6554 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2a6554

Browse files
committed
mimxrt: Add signed/unsigned option for Encoder and Counter.
Using the keyword option signed=True/False it can be controlled, whther the return value of Encoder.value() and Counter.value() is signed or unsigned. The return calue of Encoder.revolution() and Countger.overflow() stay signed. The value submitted to compare() can be signed or unsigned and will be treated accordingly. This option does not change the returned bit pattern, it changes its interpretation.
1 parent 7e919ae commit f2a6554

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

ports/mimxrt/machine_qecnt.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef struct _machine_qencd_obj_t {
4343
ENC_Type *instance;
4444
int8_t id;
4545
uint8_t mode;
46-
bool home_trigger_mode;
46+
bool is_signed;
4747
uint8_t match_pin;
4848
uint32_t cpr;
4949
uint32_t compare_pos;
@@ -295,7 +295,7 @@ STATIC uint32_t calc_filter(uint32_t filter_ns, uint16_t *count, uint16_t *perio
295295
//
296296
STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
297297
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};
298+
enum { ARG_index, ARG_home, ARG_match, ARG_filter, ARG_reverse, ARG_cpr, ARG_compare, ARG_signed};
299299
static const mp_arg_t allowed_args[] = {
300300
{ MP_QSTR_index, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
301301
{ MP_QSTR_home, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
@@ -304,6 +304,7 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
304304
{ MP_QSTR_reverse, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
305305
{ MP_QSTR_cpr, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
306306
{ MP_QSTR_compare, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
307+
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
307308
};
308309
enc_config_t enc_config;
309310

@@ -321,6 +322,10 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
321322
enc_config.enableReverseDirection = true;
322323
}
323324

325+
if (args[ARG_signed].u_int != 0) {
326+
self->is_signed = true;
327+
}
328+
324329
if (args[ARG_cpr].u_obj != mp_const_none) {
325330
uint32_t cpr = mp_get_ll_int(args[ARG_cpr].u_obj);
326331
self->cpr = cpr;
@@ -350,7 +355,6 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
350355
connect_pin_to_encoder(home, xbar_ouput_table[self->id].enc_home, XBAR_IN);
351356
enc_config.HOMETriggerMode = kENC_HOMETriggerOnRisingEdge;
352357
}
353-
self->home_trigger_mode = enc_config.HOMETriggerMode;
354358

355359
// Check for a Match pin for the compare match signal
356360
mp_obj_t match = args[ARG_match].u_obj;
@@ -405,6 +409,7 @@ STATIC mp_obj_t mp_machine_qencd_make_new(const mp_obj_type_t *type, size_t n_ar
405409
self->status = 0;
406410
self->irq = NULL;
407411
self->match_pin = 0;
412+
self->is_signed = false;
408413
self->mode = MODE_ENCODER;
409414

410415
// Process the remaining parameters
@@ -456,7 +461,11 @@ STATIC mp_obj_t machine_qencd_value(size_t n_args, const mp_obj_t *args) {
456461
machine_qencd_obj_t *self = MP_OBJ_TO_PTR(args[0]);
457462
if (n_args == 1) {
458463
// Get the counter or position as unsigned 32 bit value.
459-
return mp_obj_new_int_from_uint(ENC_GetPositionValue(self->instance));
464+
if (self->is_signed) {
465+
return mp_obj_new_int((int32_t)ENC_GetPositionValue(self->instance));
466+
} else {
467+
return mp_obj_new_int_from_uint(ENC_GetPositionValue(self->instance));
468+
}
460469
} else {
461470
// Set the encoder position value and clear the rev counter.
462471
uint64_t value = mp_get_ll_int(args[1]);
@@ -574,12 +583,13 @@ const mp_obj_type_t machine_qencd_type = {
574583

575584
STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
576585
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
577-
enum { ARG_direction, ARG_match, ARG_filter, ARG_compare};
586+
enum { ARG_direction, ARG_match, ARG_filter, ARG_compare, ARG_signed};
578587
static const mp_arg_t allowed_args[] = {
579588
{ MP_QSTR_direction, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
580589
{ MP_QSTR_match, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
581590
{ MP_QSTR_filter, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
582591
{ MP_QSTR_compare, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
592+
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = false} },
583593
};
584594
enc_config_t enc_config;
585595

@@ -624,6 +634,10 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
624634
&enc_config.filterCount, &enc_config.filterSamplePeriod);
625635
}
626636

637+
if (args[ARG_signed].u_int != 0) {
638+
self->is_signed = true;
639+
}
640+
627641
// Initialize the ENC module.
628642
ENC_Init(self->instance, &enc_config);
629643
clear_encoder_registers(self);
@@ -659,6 +673,7 @@ STATIC mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_
659673
self->status = 0;
660674
self->irq = NULL;
661675
self->match_pin = 0;
676+
self->is_signed = false;
662677
self->mode = MODE_COUNTER;
663678

664679
// Process the remaining parameters

ports/mimxrt/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ uint32_t trng_random_u32(void);
4747
#define MICROPY_OPT_LOAD_ATTR_FAST_PATH (1)
4848
#define MICROPY_OPT_MAP_LOOKUP_CACHE (1)
4949

50+
// Compiler configuration
51+
5052
// Python internal features
5153
#define MICROPY_READER_VFS (1)
5254
#define MICROPY_ENABLE_GC (1)

0 commit comments

Comments
 (0)
0