@@ -78,28 +78,42 @@ static void IRAM_ATTR pcnt_intr_handler(void *arg) {
78
78
// self->event_status = PCNT.status_unit[id].val;
79
79
pcnt_get_event_status (id , & self -> event_status );
80
80
if (self -> event_status & PCNT_EVT_H_LIM ) {
81
+ // when counting up
82
+ // debug_printf("H");
81
83
self -> counter += INT16_ROLL ;
82
84
self -> counter_accum += INT16_ROLL ;
85
+ if (self -> handler_roll_over != MP_OBJ_NULL ) {
86
+ mp_sched_schedule (self -> handler_roll_over , MP_OBJ_FROM_PTR (self ));
87
+ mp_hal_wake_main_task_from_isr ();
88
+ }
83
89
} else if (self -> event_status & PCNT_EVT_L_LIM ) {
90
+ // when counting down
91
+ // debug_printf("L");
84
92
self -> counter -= INT16_ROLL ;
85
93
self -> counter_accum -= INT16_ROLL ;
94
+ if (self -> handler_roll_under != MP_OBJ_NULL ) {
95
+ mp_sched_schedule (self -> handler_roll_under , MP_OBJ_FROM_PTR (self ));
96
+ mp_hal_wake_main_task_from_isr ();
97
+ }
86
98
}
87
-
88
99
if (self -> event_status & PCNT_EVT_THRES_1 ) {
89
- // counting up
100
+ // when counting up & treshold value > 0
101
+ // debug_printf("1");
90
102
if (self -> counter_accum == self -> counter_match ) {
91
103
mp_sched_schedule (self -> handler_match , MP_OBJ_FROM_PTR (self ));
92
104
mp_hal_wake_main_task_from_isr ();
93
105
}
94
- }
95
- if ( self -> event_status & PCNT_EVT_THRES_0 ) {
96
- // counting down
106
+ } else if ( self -> event_status & PCNT_EVT_THRES_0 ) {
107
+ // when counting down & treshold value < 0
108
+ // debug_printf("0");
97
109
if (self -> counter_accum == self -> counter_match + INT16_ROLL ) {
98
110
mp_sched_schedule (self -> handler_match , MP_OBJ_FROM_PTR (self ));
99
111
mp_hal_wake_main_task_from_isr ();
100
112
}
101
113
}
102
114
if (self -> event_status & PCNT_EVT_ZERO ) {
115
+ // when counting up/down
116
+ // debug_printf("Z");
103
117
if (self -> counter == 0 ) {
104
118
mp_sched_schedule (self -> handler_zero , MP_OBJ_FROM_PTR (self ));
105
119
mp_hal_wake_main_task_from_isr ();
@@ -161,6 +175,12 @@ static void set_filter_value(pcnt_unit_t unit, int16_t value) {
161
175
}
162
176
163
177
static void pcnt_disable_events (mp_pcnt_obj_t * self ) {
178
+ if (self -> handler_roll_over != MP_OBJ_NULL ) {
179
+ self -> handler_roll_over = MP_OBJ_NULL ;
180
+ }
181
+ if (self -> handler_roll_under != MP_OBJ_NULL ) {
182
+ self -> handler_roll_under = MP_OBJ_NULL ;
183
+ }
164
184
if (self -> handler_match != MP_OBJ_NULL ) {
165
185
check_esp_err (pcnt_event_disable (self -> unit , PCNT_EVT_THRES_1 ));
166
186
check_esp_err (pcnt_event_disable (self -> unit , PCNT_EVT_THRES_0 ));
@@ -183,6 +203,8 @@ static void reset(mp_pcnt_obj_t *self) {
183
203
self -> counter_match = 0 ;
184
204
self -> handler_match = MP_OBJ_NULL ;
185
205
self -> handler_zero = MP_OBJ_NULL ;
206
+ self -> handler_roll_over = MP_OBJ_NULL ;
207
+ self -> handler_roll_under = MP_OBJ_NULL ;
186
208
self -> event_status = 0 ;
187
209
188
210
self -> filter = 0 ;
@@ -192,7 +214,6 @@ static void reset(mp_pcnt_obj_t *self) {
192
214
static void pcnt_deinit (mp_pcnt_obj_t * self ) {
193
215
if (self != NULL ) {
194
216
check_esp_err (pcnt_counter_pause (self -> unit ));
195
-
196
217
check_esp_err (pcnt_intr_disable (self -> unit ));
197
218
198
219
check_esp_err (pcnt_event_disable (self -> unit , PCNT_EVT_L_LIM ));
@@ -202,6 +223,7 @@ static void pcnt_deinit(mp_pcnt_obj_t *self) {
202
223
check_esp_err (pcnt_set_pin (self -> unit , PCNT_CHANNEL_0 , PCNT_PIN_NOT_USED , PCNT_PIN_NOT_USED ));
203
224
check_esp_err (pcnt_set_pin (self -> unit , PCNT_CHANNEL_1 , PCNT_PIN_NOT_USED , PCNT_PIN_NOT_USED ));
204
225
226
+ check_esp_err (pcnt_counter_clear (self -> unit ));
205
227
reset (self );
206
228
pcnts [self -> unit ] = NULL ;
207
229
}
@@ -317,7 +339,7 @@ static mp_obj_t machine_PCNT_irq(size_t n_pos_args, const mp_obj_t *pos_args, mp
317
339
mp_obj_t handler = args [ARG_handler ].u_obj ;
318
340
mp_uint_t trigger = args [ARG_trigger ].u_int ;
319
341
320
- if (trigger & ~(PCNT_EVT_THRES_1 | PCNT_EVT_ZERO )) {
342
+ if (trigger & ~(PCNT_EVT_THRES_1 | PCNT_EVT_ZERO | PCNT_EVT_H_LIM | PCNT_EVT_L_LIM )) {
321
343
mp_raise_ValueError (MP_ERROR_TEXT ("trigger" ));
322
344
}
323
345
@@ -333,6 +355,12 @@ static mp_obj_t machine_PCNT_irq(size_t n_pos_args, const mp_obj_t *pos_args, mp
333
355
check_esp_err (pcnt_event_disable (self -> unit , PCNT_EVT_ZERO ));
334
356
self -> handler_zero = MP_OBJ_NULL ;
335
357
}
358
+ if (trigger & PCNT_EVT_H_LIM ) {
359
+ self -> handler_roll_over = MP_OBJ_NULL ;
360
+ }
361
+ if (trigger & PCNT_EVT_L_LIM ) {
362
+ self -> handler_roll_under = MP_OBJ_NULL ;
363
+ }
336
364
} else {
337
365
if (trigger & PCNT_EVT_THRES_1 ) {
338
366
if (args [ARG_value ].u_obj != MP_OBJ_NULL ) {
@@ -362,6 +390,12 @@ static mp_obj_t machine_PCNT_irq(size_t n_pos_args, const mp_obj_t *pos_args, mp
362
390
check_esp_err (pcnt_event_enable (self -> unit , PCNT_EVT_THRES_1 ));
363
391
check_esp_err (pcnt_event_enable (self -> unit , PCNT_EVT_THRES_0 ));
364
392
}
393
+ if (trigger & PCNT_EVT_H_LIM ) {
394
+ self -> handler_roll_over = handler ;
395
+ }
396
+ if (trigger & PCNT_EVT_L_LIM ) {
397
+ self -> handler_roll_under = handler ;
398
+ }
365
399
if (trigger & PCNT_EVT_ZERO ) {
366
400
/*
367
401
int16_t count;
@@ -571,19 +605,30 @@ static mp_obj_t machine_Counter_init(size_t n_args, const mp_obj_t *args, mp_map
571
605
MP_DEFINE_CONST_FUN_OBJ_KW (machine_Counter_init_obj , 1 , machine_Counter_init );
572
606
573
607
static void common_print_kw (const mp_print_t * print , mp_pcnt_obj_t * self ) {
608
+ if (self -> handler_roll_over != MP_OBJ_NULL ) {
609
+ mp_printf (print , ", roll_over=%ld" , INT16_ROLL );
610
+ }
611
+ if (self -> handler_roll_under != MP_OBJ_NULL ) {
612
+ mp_printf (print , ", roll_under=%ld" , - INT16_ROLL );
613
+ }
574
614
if (self -> handler_match != MP_OBJ_NULL ) {
575
- mp_printf (print , ", match1 =%ld" , self -> match );
615
+ mp_printf (print , ", match =%ld" , self -> match );
576
616
}
577
617
if (self -> handler_zero != MP_OBJ_NULL ) {
578
- mp_printf (print , ", match =0" );
618
+ mp_printf (print , ", match0 =0" );
579
619
}
580
620
mp_printf (print , ", filter_ns=%d)" , filter_to_ns (self -> filter ));
581
621
}
582
622
583
623
static void machine_Counter_print (const mp_print_t * print , mp_obj_t self_obj , mp_print_kind_t kind ) {
584
624
mp_pcnt_obj_t * self = MP_OBJ_TO_PTR (self_obj );
585
625
586
- mp_printf (print , "Counter(%u, src=Pin(%u)" , self -> unit , self -> aPinNumber );
626
+ mp_printf (print , "Counter(%u" , self -> unit );
627
+ if (self -> aPinNumber == PCNT_PIN_NOT_USED ) {
628
+ mp_printf (print , ")" );
629
+ return ;
630
+ }
631
+ mp_printf (print , "), src=Pin(%u)" , self -> aPinNumber );
587
632
if (self -> x124 < 0 ) {
588
633
mp_printf (print , ", _src=Pin(%u)" , self -> bPinNumber );
589
634
} else {
@@ -611,7 +656,9 @@ static void machine_Counter_print(const mp_print_t *print, mp_obj_t self_obj, mp
611
656
612
657
#define COMMON_CONSTANTS \
613
658
{ MP_ROM_QSTR(MP_QSTR_IRQ_ZERO), MP_ROM_INT(PCNT_EVT_ZERO) }, \
614
- { MP_ROM_QSTR(MP_QSTR_IRQ_MATCH1), MP_ROM_INT(PCNT_EVT_THRES_1) }
659
+ { MP_ROM_QSTR(MP_QSTR_IRQ_ROLL_OVER), MP_ROM_INT(PCNT_EVT_H_LIM) }, \
660
+ { MP_ROM_QSTR(MP_QSTR_IRQ_ROLL_UNDER), MP_ROM_INT(PCNT_EVT_L_LIM) }, \
661
+ { MP_ROM_QSTR(MP_QSTR_IRQ_MATCH), MP_ROM_INT(PCNT_EVT_THRES_1) }
615
662
616
663
static const mp_rom_map_elem_t machine_Counter_locals_dict_table [] = {
617
664
{ MP_ROM_QSTR (MP_QSTR_init ), MP_ROM_PTR (& machine_Counter_init_obj ) },
@@ -755,8 +802,12 @@ MP_DEFINE_CONST_FUN_OBJ_KW(machine_Encoder_init_obj, 1, machine_Encoder_init);
755
802
756
803
static void machine_Encoder_print (const mp_print_t * print , mp_obj_t self_obj , mp_print_kind_t kind ) {
757
804
mp_pcnt_obj_t * self = MP_OBJ_TO_PTR (self_obj );
758
-
759
- mp_printf (print , "Encoder(%u, phase_a=Pin(%u), phase_b=Pin(%u), x124=%d" , self -> unit , self -> aPinNumber , self -> bPinNumber , self -> x124 );
805
+ mp_printf (print , "Encoder(%u" , self -> unit );
806
+ if (self -> aPinNumber == PCNT_PIN_NOT_USED ) {
807
+ mp_printf (print , ")" );
808
+ return ;
809
+ }
810
+ mp_printf (print , ", phase_a=Pin(%u), phase_b=Pin(%u), x124=%d" , self -> aPinNumber , self -> bPinNumber , self -> x124 );
760
811
common_print_kw (print , self );
761
812
}
762
813
0 commit comments