File tree Expand file tree Collapse file tree
ports/raspberrypi/common-hal/pulseio Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
5656 self -> idle_state = idle_state ;
5757 self -> start = 0 ;
5858 self -> len = 0 ;
59+ self -> len_target = 0 ;
5960
6061 common_hal_rp2pio_statemachine_construct (& self -> state_machine ,
6162 pulsein_program , MP_ARRAY_SIZE (pulsein_program ),
@@ -133,6 +134,8 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
133134 self -> buffer [buf_index ] = (uint16_t )result ;
134135 if (self -> len < self -> maxlen ) {
135136 self -> len ++ ;
137+ self -> len_target ++ ; // The interrupt will only cause a problem in either len or len_target, not both.
138+ // So we can just check for a match, and choose the higher.
136139 } else {
137140 self -> start = (self -> start + 1 ) % self -> maxlen ;
138141 }
@@ -174,7 +177,13 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
174177 }
175178 uint16_t value = self -> buffer [self -> start ];
176179 self -> start = (self -> start + 1 ) % self -> maxlen ;
180+ self -> len_target -- ;
177181 self -> len -- ;
182+ if (self -> len != self -> len_target ) {
183+ uint16_t len_accurate = self -> len > self -> len_target ? self -> len : self -> len_target ;
184+ self -> len_target = len_accurate ;
185+ self -> len = len_accurate ;
186+ }
178187 return value ;
179188}
180189
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ typedef struct {
4343 volatile bool last_level ;
4444 volatile uint32_t level_count ;
4545 volatile uint16_t len ;
46+ volatile uint16_t len_target ;
4647 volatile uint16_t start ;
4748 rp2pio_statemachine_obj_t state_machine ;
4849} pulseio_pulsein_obj_t ;
You can’t perform that action at this time.
0 commit comments