3737#include "shared-bindings/pulseio/PulseIn.h"
3838#include "common-hal/microcontroller/__init__.h"
3939
40- // XXX map gpio pins to pulsein objects: kinda clumsy.
41- static pulseio_pulsein_obj_t * pulseio_pulsein_objs [GPIO_PIN_COUNT ] = {0 };
42-
4340static void pulsein_set_interrupt (pulseio_pulsein_obj_t * self , bool rising , bool falling ) {
4441 ETS_GPIO_INTR_DISABLE ();
4542 // Set interrupt mode
@@ -55,7 +52,9 @@ static void pulsein_set_interrupt(pulseio_pulsein_obj_t *self, bool rising, bool
5552 ETS_GPIO_INTR_ENABLE ();
5653}
5754
58- void pulseio_pulsein_interrupt_handler (pulseio_pulsein_obj_t * self , uint32_t time_us ) {
55+ void pulseio_pulsein_interrupt_handler (void * data ) {
56+ pulseio_pulsein_obj_t * self = data ;
57+ uint32_t time_us = system_get_time ();
5958 if (self -> first_edge ) {
6059 self -> first_edge = false;
6160 pulsein_set_interrupt (self , true, true);
@@ -72,18 +71,6 @@ void pulseio_pulsein_interrupt_handler(pulseio_pulsein_obj_t *self, uint32_t tim
7271 self -> last_us = time_us ;
7372}
7473
75- // XXX needs a better name, or a better abstraction
76- // XXX called from intr.c:pin_intr_handler_iram ... inelegantly
77- void pulsein_interrupt_handler (uint32_t status ) {
78- uint32_t time_us = system_get_time ();
79- for (int i = 0 ; i < GPIO_PIN_COUNT ; i ++ ) {
80- if (status & 1 <<i ) {
81- pulseio_pulsein_obj_t * self = pulseio_pulsein_objs [i ];
82- if (self ) pulseio_pulsein_interrupt_handler (self , time_us );
83- }
84- }
85- }
86-
8774void common_hal_pulseio_pulsein_construct (pulseio_pulsein_obj_t * self ,
8875 const mcu_pin_obj_t * pin , uint16_t maxlen , bool idle_state ) {
8976 if (pin -> gpio_number == NO_GPIO || pin -> gpio_function == SPECIAL_CASE ) {
@@ -104,19 +91,22 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
10491 self -> len = 0 ;
10592 self -> first_edge = true;
10693 self -> last_us = 0 ;
107- pulseio_pulsein_objs [self -> pin -> gpio_number ] = self ;
94+
95+ microcontroller_pin_register_intr_handler (self -> pin -> gpio_number ,
96+ pulseio_pulsein_interrupt_handler , (void * )self );
10897 pulsein_set_interrupt (self , !idle_state , idle_state );
10998}
11099
111100bool common_hal_pulseio_pulsein_deinited (pulseio_pulsein_obj_t * self ) {
112- return pulseio_pulsein_objs [ self -> pin -> gpio_number ] == NULL ;
101+ return self -> buffer == NULL ;
113102}
114103
115104void common_hal_pulseio_pulsein_deinit (pulseio_pulsein_obj_t * self ) {
116105 pulsein_set_interrupt (self , false, false);
117- pulseio_pulsein_objs [ self -> pin -> gpio_number ] = NULL ;
106+ microcontroller_pin_register_intr_handler ( self -> pin -> gpio_number , NULL , NULL ) ;
118107 PIN_FUNC_SELECT (self -> pin -> peripheral , 0 );
119108 m_free (self -> buffer );
109+ self -> buffer = NULL ;
120110}
121111
122112void common_hal_pulseio_pulsein_pause (pulseio_pulsein_obj_t * self ) {
0 commit comments