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
- uint16_t pulsein_program [] = {
43
+ static const uint16_t pulsein_program [] = {
44
44
0x4001 , // 1: in pins, 1
45
45
};
46
46
@@ -57,48 +57,29 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
57
57
self -> start = 0 ;
58
58
self -> len = 0 ;
59
59
60
- bool ok = rp2pio_statemachine_construct (& self -> state_machine ,
61
- pulsein_program , sizeof (pulsein_program ) / sizeof (pulsein_program [0 ]),
62
- 1000000 ,
63
- NULL , 0 ,
64
- NULL , 0 ,
65
- pin , 1 ,
66
- 0 ,0 ,
67
- NULL , 0 ,
68
- NULL , 0 ,
69
- 1 , 0 ,
70
- NULL , // jump pin
71
- 1 << self -> pin , false, true,
72
- false, 8 , false, // TX, unused
73
- false,
74
- true, 32 , true, // RX auto-push every 32 bits
75
- false, // claim pins
76
- false, // Not user-interruptible.
77
- false, // No sideset enable
78
- 0 , -1 ); // wrap settings
79
-
80
- if (!ok ) {
81
- mp_raise_RuntimeError (translate ("All state machines in use" ));
82
- }
83
-
84
- pio_sm_set_enabled (self -> state_machine .pio ,self -> state_machine .state_machine , false);
85
- pio_sm_clear_fifos (self -> state_machine .pio ,self -> state_machine .state_machine );
86
- self -> last_level = self -> idle_state ;
87
- self -> level_count = 0 ;
88
- self -> buf_index = 0 ;
60
+ common_hal_rp2pio_statemachine_construct (& self -> state_machine ,
61
+ pulsein_program , MP_ARRAY_SIZE (pulsein_program ),
62
+ 1000000 , // frequency
63
+ NULL , 0 , // init, init_len
64
+ NULL , 0 , 0 , 0 , // first out pin, # out pins, initial_out_pin_state
65
+ pin , 1 , 0 , 0 , // first in pin, # in pins
66
+ NULL , 0 , 0 , 0 , // first set pin
67
+ NULL , 0 , 0 , 0 , // first sideset pin
68
+ false, // No sideset enable
69
+ NULL , PULL_NONE , // jump pin, jmp_pull
70
+ 0 , // wait gpio pins
71
+ true, // exclusive pin usage
72
+ false, 8 , false, // TX, setting we don't use
73
+ false, // wait for TX stall
74
+ true, 32 , true, // RX auto pull every 32 bits. shift left to output msb first
75
+ false, // Not user-interruptible.
76
+ 0 , -1 ); // wrap settings
77
+
78
+ common_hal_pulseio_pulsein_pause (self );
89
79
90
- pio_sm_set_in_pins (self -> state_machine .pio ,self -> state_machine .state_machine ,pin -> number );
91
80
common_hal_rp2pio_statemachine_set_interrupt_handler (& (self -> state_machine ),& common_hal_pulseio_pulsein_interrupt ,self ,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS );
92
81
93
- // exec a set pindirs to 0 for input
94
- pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0xe080 );
95
- // exec the appropriate wait for pin
96
- if (self -> idle_state == true) {
97
- pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x2020 );
98
- } else {
99
- pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x20a0 );
100
- }
101
- pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , true);
82
+ common_hal_pulseio_pulsein_resume (self , 0 );
102
83
}
103
84
104
85
bool common_hal_pulseio_pulsein_deinited (pulseio_pulsein_obj_t * self ) {
@@ -119,9 +100,10 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
119
100
void common_hal_pulseio_pulsein_pause (pulseio_pulsein_obj_t * self ) {
120
101
pio_sm_restart (self -> state_machine .pio , self -> state_machine .state_machine );
121
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 );
122
104
self -> last_level = self -> idle_state ;
123
105
self -> level_count = 0 ;
124
- self -> buf_index = 0 ;
106
+ self -> paused = true ;
125
107
}
126
108
void common_hal_pulseio_pulsein_interrupt (void * self_in ) {
127
109
pulseio_pulsein_obj_t * self = self_in ;
@@ -140,7 +122,7 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
140
122
} else {
141
123
uint32_t result = self -> level_count ;
142
124
self -> last_level = level ;
143
- self -> level_count = 0 ;
125
+ self -> level_count = 1 ;
144
126
// Pulses that are longer than MAX_PULSE will return MAX_PULSE
145
127
if (result > MAX_PULSE ) {
146
128
result = MAX_PULSE ;
@@ -154,53 +136,36 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
154
136
} else {
155
137
self -> start = (self -> start + 1 ) % self -> maxlen ;
156
138
}
157
- if (self -> buf_index < self -> maxlen ) {
158
- self -> buf_index ++ ;
159
- } else {
160
- self -> start = 0 ;
161
- self -> buf_index = 0 ;
162
- }
163
139
}
164
140
}
165
141
}
166
142
}
167
-
168
- // check for a pulse thats too long (MAX_PULSE us) or maxlen reached, and reset
169
- if ((self -> level_count > MAX_PULSE ) || (self -> buf_index >= self -> maxlen )) {
170
- pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , false);
171
- pio_sm_init (self -> state_machine .pio , self -> state_machine .state_machine , self -> state_machine .offset , & self -> state_machine .sm_config );
172
- pio_sm_restart (self -> state_machine .pio ,self -> state_machine .state_machine );
173
- pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , true);
174
- self -> buf_index = 0 ;
175
- }
176
143
}
177
144
void common_hal_pulseio_pulsein_resume (pulseio_pulsein_obj_t * self ,
178
145
uint16_t trigger_duration ) {
146
+
147
+ common_hal_pulseio_pulsein_pause (self );
179
148
// Send the trigger pulse.
180
149
if (trigger_duration > 0 ) {
181
150
gpio_set_function (self -> pin ,GPIO_FUNC_SIO );
182
151
gpio_set_dir (self -> pin ,true);
183
152
gpio_put (self -> pin , !self -> idle_state );
184
153
common_hal_mcu_delay_us ((uint32_t )trigger_duration );
185
154
gpio_set_function (self -> pin ,GPIO_FUNC_PIO0 );
186
- common_hal_mcu_delay_us (125 );
187
155
}
188
156
189
- // Reconfigure the pin for PIO
190
- gpio_set_function (self -> pin , GPIO_FUNC_PIO0 );
191
157
// exec a wait for the selected pin to change state
192
158
if (self -> idle_state == true) {
193
159
pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x2020 );
194
160
} else {
195
161
pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x20a0 );
196
162
}
197
163
pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , true);
164
+ self -> paused = false;
198
165
}
199
166
200
167
void common_hal_pulseio_pulsein_clear (pulseio_pulsein_obj_t * self ) {
201
- self -> start = 0 ;
202
168
self -> len = 0 ;
203
- self -> buf_index = 0 ;
204
169
}
205
170
206
171
uint16_t common_hal_pulseio_pulsein_popleft (pulseio_pulsein_obj_t * self ) {
@@ -210,12 +175,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
210
175
uint16_t value = self -> buffer [self -> start ];
211
176
self -> start = (self -> start + 1 ) % self -> maxlen ;
212
177
self -> len -- ;
213
- // if we are empty reset buffer pointer and counters
214
- if (self -> len == 0 ) {
215
- self -> start = 0 ;
216
- self -> buf_index = 0 ;
217
- self -> level_count = 0 ;
218
- }
219
178
return value ;
220
179
}
221
180
@@ -228,7 +187,7 @@ uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) {
228
187
}
229
188
230
189
bool common_hal_pulseio_pulsein_get_paused (pulseio_pulsein_obj_t * self ) {
231
- return true ;
190
+ return self -> paused ;
232
191
}
233
192
234
193
uint16_t common_hal_pulseio_pulsein_get_item (pulseio_pulsein_obj_t * self ,
0 commit comments