38
38
39
39
#define NO_PIN 0xff
40
40
#define MAX_PULSE 65535
41
- #define MIN_PULSE 10
41
+ #define MIN_PULSE 0
42
42
43
43
static const uint16_t pulsein_program [] = {
44
44
0x4001 , // 1: in pins, 1
@@ -75,24 +75,11 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
75
75
false, // Not user-interruptible.
76
76
0 , -1 ); // wrap settings
77
77
78
- pio_sm_set_enabled (self -> state_machine .pio ,self -> state_machine .state_machine , false);
79
- pio_sm_clear_fifos (self -> state_machine .pio ,self -> state_machine .state_machine );
80
- self -> last_level = self -> idle_state ;
81
- self -> level_count = 0 ;
82
- self -> buf_index = 0 ;
78
+ common_hal_pulseio_pulsein_pause (self );
83
79
84
- pio_sm_set_in_pins (self -> state_machine .pio ,self -> state_machine .state_machine ,pin -> number );
85
80
common_hal_rp2pio_statemachine_set_interrupt_handler (& (self -> state_machine ),& common_hal_pulseio_pulsein_interrupt ,self ,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS );
86
81
87
- // exec a set pindirs to 0 for input
88
- pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0xe080 );
89
- // exec the appropriate wait for pin
90
- if (self -> idle_state == true) {
91
- pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x2020 );
92
- } else {
93
- pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x20a0 );
94
- }
95
- pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , true);
82
+ common_hal_pulseio_pulsein_resume (self , 0 );
96
83
}
97
84
98
85
bool common_hal_pulseio_pulsein_deinited (pulseio_pulsein_obj_t * self ) {
@@ -113,9 +100,10 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
113
100
void common_hal_pulseio_pulsein_pause (pulseio_pulsein_obj_t * self ) {
114
101
pio_sm_restart (self -> state_machine .pio , self -> state_machine .state_machine );
115
102
pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , false);
103
+ pio_sm_clear_fifos (self -> state_machine .pio ,self -> state_machine .state_machine );
116
104
self -> last_level = self -> idle_state ;
117
105
self -> level_count = 0 ;
118
- self -> buf_index = 0 ;
106
+ self -> paused = true ;
119
107
}
120
108
void common_hal_pulseio_pulsein_interrupt (void * self_in ) {
121
109
pulseio_pulsein_obj_t * self = self_in ;
@@ -134,7 +122,7 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
134
122
} else {
135
123
uint32_t result = self -> level_count ;
136
124
self -> last_level = level ;
137
- self -> level_count = 0 ;
125
+ self -> level_count = 1 ;
138
126
// Pulses that are longer than MAX_PULSE will return MAX_PULSE
139
127
if (result > MAX_PULSE ) {
140
128
result = MAX_PULSE ;
@@ -148,53 +136,36 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
148
136
} else {
149
137
self -> start = (self -> start + 1 ) % self -> maxlen ;
150
138
}
151
- if (self -> buf_index < self -> maxlen ) {
152
- self -> buf_index ++ ;
153
- } else {
154
- self -> start = 0 ;
155
- self -> buf_index = 0 ;
156
- }
157
139
}
158
140
}
159
141
}
160
142
}
161
-
162
- // check for a pulse thats too long (MAX_PULSE us) or maxlen reached, and reset
163
- if ((self -> level_count > MAX_PULSE ) || (self -> buf_index >= self -> maxlen )) {
164
- pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , false);
165
- pio_sm_init (self -> state_machine .pio , self -> state_machine .state_machine , self -> state_machine .offset , & self -> state_machine .sm_config );
166
- pio_sm_restart (self -> state_machine .pio ,self -> state_machine .state_machine );
167
- pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , true);
168
- self -> buf_index = 0 ;
169
- }
170
143
}
171
144
void common_hal_pulseio_pulsein_resume (pulseio_pulsein_obj_t * self ,
172
145
uint16_t trigger_duration ) {
146
+
147
+ common_hal_pulseio_pulsein_pause (self );
173
148
// Send the trigger pulse.
174
149
if (trigger_duration > 0 ) {
175
150
gpio_set_function (self -> pin ,GPIO_FUNC_SIO );
176
151
gpio_set_dir (self -> pin ,true);
177
152
gpio_put (self -> pin , !self -> idle_state );
178
153
common_hal_mcu_delay_us ((uint32_t )trigger_duration );
179
154
gpio_set_function (self -> pin ,GPIO_FUNC_PIO0 );
180
- common_hal_mcu_delay_us (125 );
181
155
}
182
156
183
- // Reconfigure the pin for PIO
184
- gpio_set_function (self -> pin , GPIO_FUNC_PIO0 );
185
157
// exec a wait for the selected pin to change state
186
158
if (self -> idle_state == true) {
187
159
pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x2020 );
188
160
} else {
189
161
pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x20a0 );
190
162
}
191
163
pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , true);
164
+ self -> paused = false;
192
165
}
193
166
194
167
void common_hal_pulseio_pulsein_clear (pulseio_pulsein_obj_t * self ) {
195
- self -> start = 0 ;
196
168
self -> len = 0 ;
197
- self -> buf_index = 0 ;
198
169
}
199
170
200
171
uint16_t common_hal_pulseio_pulsein_popleft (pulseio_pulsein_obj_t * self ) {
@@ -204,12 +175,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
204
175
uint16_t value = self -> buffer [self -> start ];
205
176
self -> start = (self -> start + 1 ) % self -> maxlen ;
206
177
self -> len -- ;
207
- // if we are empty reset buffer pointer and counters
208
- if (self -> len == 0 ) {
209
- self -> start = 0 ;
210
- self -> buf_index = 0 ;
211
- self -> level_count = 0 ;
212
- }
213
178
return value ;
214
179
}
215
180
@@ -222,7 +187,7 @@ uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) {
222
187
}
223
188
224
189
bool common_hal_pulseio_pulsein_get_paused (pulseio_pulsein_obj_t * self ) {
225
- return true ;
190
+ return self -> paused ;
226
191
}
227
192
228
193
uint16_t common_hal_pulseio_pulsein_get_item (pulseio_pulsein_obj_t * self ,
0 commit comments