37
37
#include "shared-bindings/pulseio/PulseIn.h"
38
38
#include "common-hal/microcontroller/__init__.h"
39
39
40
- // XXX map gpio pins to pulsein objects: kinda clumsy.
41
- static pulseio_pulsein_obj_t * pulseio_pulsein_objs [GPIO_PIN_COUNT ] = {0 };
42
-
43
40
static void pulsein_set_interrupt (pulseio_pulsein_obj_t * self , bool rising , bool falling ) {
44
41
ETS_GPIO_INTR_DISABLE ();
45
42
// Set interrupt mode
@@ -55,7 +52,9 @@ static void pulsein_set_interrupt(pulseio_pulsein_obj_t *self, bool rising, bool
55
52
ETS_GPIO_INTR_ENABLE ();
56
53
}
57
54
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 ();
59
58
if (self -> first_edge ) {
60
59
self -> first_edge = false;
61
60
pulsein_set_interrupt (self , true, true);
@@ -72,18 +71,6 @@ void pulseio_pulsein_interrupt_handler(pulseio_pulsein_obj_t *self, uint32_t tim
72
71
self -> last_us = time_us ;
73
72
}
74
73
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
-
87
74
void common_hal_pulseio_pulsein_construct (pulseio_pulsein_obj_t * self ,
88
75
const mcu_pin_obj_t * pin , uint16_t maxlen , bool idle_state ) {
89
76
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,
104
91
self -> len = 0 ;
105
92
self -> first_edge = true;
106
93
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 );
108
97
pulsein_set_interrupt (self , !idle_state , idle_state );
109
98
}
110
99
111
100
bool 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 ;
113
102
}
114
103
115
104
void common_hal_pulseio_pulsein_deinit (pulseio_pulsein_obj_t * self ) {
116
105
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 ) ;
118
107
PIN_FUNC_SELECT (self -> pin -> peripheral , 0 );
119
108
m_free (self -> buffer );
109
+ self -> buffer = NULL ;
120
110
}
121
111
122
112
void common_hal_pulseio_pulsein_pause (pulseio_pulsein_obj_t * self ) {
0 commit comments