@@ -158,13 +158,9 @@ __attribute__((section(".ram_functions"))) void ENC4_IRQHandler(void) {
158
158
159
159
STATIC void mp_machine_qencd_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
160
160
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 );
168
164
}
169
165
170
166
// Utililty functions
@@ -293,18 +289,58 @@ STATIC uint32_t calc_filter(uint32_t filter_ns, uint16_t *count, uint16_t *perio
293
289
294
290
// Micropython API functions
295
291
//
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
+
296
332
STATIC void mp_machine_qencd_init_helper (machine_qencd_obj_t * self ,
297
333
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 };
299
335
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 )} },
302
336
{ MP_QSTR_match , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
303
337
{ 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 } },
306
339
{ MP_QSTR_compare , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
307
340
{ 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 } },
308
344
};
309
345
enc_config_t enc_config ;
310
346
@@ -314,31 +350,17 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
314
350
315
351
ENC_GetDefaultConfig (& enc_config );
316
352
353
+ // Set the first common options
354
+ mp_machine_qencd_init_helper_common (self , args , & enc_config );
355
+
317
356
// Allow the trigger pulse to clear the registers
318
357
enc_config .enableTRIGGERClearPositionCounter = true;
319
358
320
- // Process the keyword arguments
359
+ // Process the remaining keyword arguments
321
360
if (args [ARG_reverse ].u_int != 0 ) {
322
361
enc_config .enableReverseDirection = true;
323
362
}
324
363
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
-
342
364
// Count revolutions on RollOverModulus or index pulse
343
365
enc_config .revolutionCountCondition = kENC_RevolutionCountOnRollOverModulus ;
344
366
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,
356
378
enc_config .HOMETriggerMode = kENC_HOMETriggerOnRisingEdge ;
357
379
}
358
380
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
-
376
381
// Initialize the ENC module.
377
382
ENC_Init (self -> instance , & enc_config );
378
383
clear_encoder_registers (self );
@@ -480,8 +485,8 @@ STATIC mp_obj_t machine_qencd_value(size_t n_args, const mp_obj_t *args) {
480
485
}
481
486
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_qencd_value_obj , 1 , 2 , machine_qencd_value );
482
487
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 ) {
485
490
machine_qencd_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
486
491
if (n_args == 1 ) {
487
492
// 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) {
492
497
return mp_const_none ;
493
498
}
494
499
}
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 );
496
501
497
502
// qencd.compare([value])
498
503
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[] = {
561
566
{ MP_ROM_QSTR (MP_QSTR_init ), MP_ROM_PTR (& machine_qencd_init_obj ) },
562
567
{ MP_ROM_QSTR (MP_QSTR_irq ), MP_ROM_PTR (& machine_qencd_irq_obj ) },
563
568
{ 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 ) },
565
570
{ MP_ROM_QSTR (MP_QSTR_status ), MP_ROM_PTR (& machine_qencd_status_obj ) },
566
571
567
572
{ MP_ROM_QSTR (MP_QSTR_COMPARE ), MP_ROM_INT (ENCODER_TRIGGER_COMPARE ) },
@@ -583,13 +588,14 @@ const mp_obj_type_t machine_qencd_type = {
583
588
584
589
STATIC void mp_machine_counter_init_helper (machine_qencd_obj_t * self ,
585
590
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 };
587
592
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 )} },
589
593
{ MP_QSTR_match , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
590
594
{ 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 } },
591
596
{ MP_QSTR_compare , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
592
597
{ 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 )} },
593
599
};
594
600
enc_config_t enc_config ;
595
601
@@ -604,7 +610,9 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
604
610
enc_config .revolutionCountCondition = kENC_RevolutionCountOnRollOverModulus ;
605
611
enc_config .enableTRIGGERClearPositionCounter = true;
606
612
607
- // Process the keyword arguments
613
+ // Set the first common options
614
+ mp_machine_qencd_init_helper_common (self , args , & enc_config );
615
+
608
616
mp_obj_t direction = args [ARG_direction ].u_obj ;
609
617
if (direction != MP_ROM_INT (-1 )) {
610
618
if (direction == MP_ROM_INT (COUNTER_UP )) {
@@ -616,28 +624,6 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
616
624
}
617
625
}
618
626
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
-
641
627
// Initialize the ENC module.
642
628
ENC_Init (self -> instance , & enc_config );
643
629
clear_encoder_registers (self );
@@ -695,7 +681,7 @@ STATIC const mp_rom_map_elem_t machine_counter_locals_dict_table[] = {
695
681
{ MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& machine_qencd_deinit_obj ) },
696
682
{ MP_ROM_QSTR (MP_QSTR_compare ), MP_ROM_PTR (& machine_qencd_compare_obj ) },
697
683
{ 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 ) },
699
685
{ MP_ROM_QSTR (MP_QSTR_init ), MP_ROM_PTR (& machine_counter_init_obj ) },
700
686
{ MP_ROM_QSTR (MP_QSTR_irq ), MP_ROM_PTR (& machine_qencd_irq_obj ) },
701
687
{ MP_ROM_QSTR (MP_QSTR_status ), MP_ROM_PTR (& machine_qencd_status_obj ) },
0 commit comments