@@ -85,211 +85,3 @@ uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
8585uint16_t common_hal_pulseio_pulsein_get_item (pulseio_pulsein_obj_t * self , int16_t index ) {
8686 return 0xadaf ;
8787}
88-
89-
90- #if 0
91-
92- static pulseio_pulsein_obj_t * active_pulseins [EIC_NUMBER_OF_INTERRUPTS ];
93- static uint64_t last_ms [EIC_NUMBER_OF_INTERRUPTS ];
94- static uint16_t last_us [EIC_NUMBER_OF_INTERRUPTS ];
95-
96- void pulsein_reset (void ) {
97- for (int i = 0 ; i < EIC_NUMBER_OF_INTERRUPTS ; i ++ ) {
98- if (active_pulseins [i ] != NULL ) {
99- extint_chan_disable_callback (i , EXTINT_CALLBACK_TYPE_DETECT );
100- }
101- active_pulseins [i ] = NULL ;
102- last_ms [i ] = 0 ;
103- last_us [i ] = 0 ;
104- }
105- }
106-
107- static void pulsein_set_config (pulseio_pulsein_obj_t * self , bool first_edge ) {
108- struct extint_chan_conf config ;
109- extint_chan_get_config_defaults (& config );
110- config .gpio_pin = self -> pin ;
111- config .gpio_pin_pull = EXTINT_PULL_NONE ;
112- config .filter_input_signal = true;
113-
114- if (!first_edge ) {
115- config .detection_criteria = EXTINT_DETECT_BOTH ;
116- } else if (self -> idle_state ) {
117- config .detection_criteria = EXTINT_DETECT_FALLING ;
118- } else {
119- config .detection_criteria = EXTINT_DETECT_RISING ;
120- }
121- extint_chan_disable_callback (self -> channel , EXTINT_CALLBACK_TYPE_DETECT );
122- extint_chan_set_config (self -> channel , & config );
123- // Clear any interrupts that may have triggered without notifying the CPU.
124- EIC -> INTFLAG .reg |= (1UL << self -> channel );
125- extint_chan_enable_callback (self -> channel , EXTINT_CALLBACK_TYPE_DETECT );
126- }
127-
128- static void pulsein_callback (void ) {
129- // Grab the current time first.
130- uint16_t current_us = tc_get_count_value (& ms_timer );
131- // Add the overflow flag to account for tick interrupts that are blocked by
132- // this interrupt.
133- uint64_t current_ms = ticks_ms + TC5 -> COUNT16 .INTFLAG .bit .OVF ;
134- pulseio_pulsein_obj_t * self = active_pulseins [extint_get_current_channel ()];
135- current_us = current_us * 1000 / self -> ticks_per_ms ;
136- if (self -> first_edge ) {
137- self -> first_edge = false;
138- pulsein_set_config (self , false);
139- } else {
140- uint32_t ms_diff = current_ms - last_ms [self -> channel ];
141- uint16_t us_diff = current_us - last_us [self -> channel ];
142- uint32_t total_diff = us_diff ;
143- if (last_us [self -> channel ] > current_us ) {
144- total_diff = 1000 + current_us - last_us [self -> channel ];
145- if (ms_diff > 1 ) {
146- total_diff += (ms_diff - 1 ) * 1000 ;
147- }
148- } else {
149- total_diff += ms_diff * 1000 ;
150- }
151- uint16_t duration = 0xffff ;
152- if (total_diff < duration ) {
153- duration = total_diff ;
154- }
155-
156- uint16_t i = (self -> start + self -> len ) % self -> maxlen ;
157- self -> buffer [i ] = duration ;
158- if (self -> len < self -> maxlen ) {
159- self -> len ++ ;
160- } else {
161- self -> start ++ ;
162- }
163- }
164- last_ms [self -> channel ] = current_ms ;
165- last_us [self -> channel ] = current_us ;
166- }
167-
168- void common_hal_pulseio_pulsein_construct (pulseio_pulsein_obj_t * self ,
169- const mcu_pin_obj_t * pin , uint16_t maxlen , bool idle_state ) {
170- if (!pin -> has_extint ) {
171- mp_raise_RuntimeError ("No hardware support on pin" );
172- }
173- // TODO(tannewt): Switch to checking actual extint peripheral state when other
174- // classes use extints.
175- if (active_pulseins [pin -> extint_channel ] != NULL ) {
176- mp_raise_RuntimeError ("EXTINT channel already in use" );
177- }
178-
179- self -> buffer = (uint16_t * ) gc_alloc (maxlen * sizeof (uint16_t ), false);
180- if (self -> buffer == NULL ) {
181- mp_raise_msg_varg (& mp_type_MemoryError , "Failed to allocate RX buffer of %d bytes" , maxlen * sizeof (uint16_t ));
182- }
183- self -> channel = pin -> extint_channel ;
184- self -> pin = pin -> pin ;
185- self -> maxlen = maxlen ;
186- self -> idle_state = idle_state ;
187- self -> start = 0 ;
188- self -> len = 0 ;
189- self -> first_edge = true;
190- self -> ticks_per_ms = (system_cpu_clock_get_hz () / 1000 - 1 );
191-
192- active_pulseins [pin -> extint_channel ] = self ;
193-
194- pulsein_set_config (self , true);
195- extint_register_callback (
196- pulsein_callback ,
197- self -> channel ,
198- EXTINT_CALLBACK_TYPE_DETECT );
199- extint_chan_enable_callback (self -> channel , EXTINT_CALLBACK_TYPE_DETECT );
200- }
201-
202- bool common_hal_pulseio_pulsein_deinited (pulseio_pulsein_obj_t * self ) {
203- return self -> pin == NO_PIN ;
204- }
205-
206- void common_hal_pulseio_pulsein_deinit (pulseio_pulsein_obj_t * self ) {
207- if (common_hal_pulseio_pulsein_deinited (self )) {
208- return ;
209- }
210- extint_chan_disable_callback (self -> channel , EXTINT_CALLBACK_TYPE_DETECT );
211- active_pulseins [self -> channel ] = NULL ;
212- reset_pin (self -> pin );
213- self -> pin = NO_PIN ;
214- }
215-
216- void common_hal_pulseio_pulsein_pause (pulseio_pulsein_obj_t * self ) {
217- extint_chan_disable_callback (self -> channel , EXTINT_CALLBACK_TYPE_DETECT );
218- }
219-
220- void common_hal_pulseio_pulsein_resume (pulseio_pulsein_obj_t * self ,
221- uint16_t trigger_duration ) {
222- // Send the trigger pulse.
223- if (trigger_duration > 0 ) {
224- struct port_config pin_conf ;
225- port_get_config_defaults (& pin_conf );
226-
227- pin_conf .direction = PORT_PIN_DIR_OUTPUT ;
228- pin_conf .input_pull = PORT_PIN_PULL_NONE ;
229- port_pin_set_config (self -> pin , & pin_conf );
230-
231- // TODO(tannewt): delay_us isn't exactly correct so we adjust the value
232- // here before calling it. Find out why its not exact and fix it instead
233- // of hacking around it here.
234- uint32_t adjusted_duration = trigger_duration ;
235- adjusted_duration *= 4 ;
236- adjusted_duration /= 5 ;
237-
238- common_hal_mcu_disable_interrupts ();
239- port_pin_set_output_level (self -> pin , !self -> idle_state );
240- common_hal_mcu_delay_us (adjusted_duration );
241- port_pin_set_output_level (self -> pin , self -> idle_state );
242- common_hal_mcu_enable_interrupts ();
243- }
244-
245- // Reconfigure the pin and make sure its set to detect the first edge.
246- last_ms [self -> channel ] = 0 ;
247- last_us [self -> channel ] = 0 ;
248- self -> first_edge = true;
249- pulsein_set_config (self , true);
250- }
251-
252- void common_hal_pulseio_pulsein_clear (pulseio_pulsein_obj_t * self ) {
253- common_hal_mcu_disable_interrupts ();
254- self -> start = 0 ;
255- self -> len = 0 ;
256- common_hal_mcu_enable_interrupts ();
257- }
258-
259- uint16_t common_hal_pulseio_pulsein_popleft (pulseio_pulsein_obj_t * self ) {
260- if (self -> len == 0 ) {
261- mp_raise_IndexError ("pop from an empty PulseIn" );
262- }
263- common_hal_mcu_disable_interrupts ();
264- uint16_t value = self -> buffer [self -> start ];
265- self -> start = (self -> start + 1 ) % self -> maxlen ;
266- self -> len -- ;
267- common_hal_mcu_enable_interrupts ();
268-
269- return value ;
270- }
271-
272- uint16_t common_hal_pulseio_pulsein_get_maxlen (pulseio_pulsein_obj_t * self ) {
273- return self -> maxlen ;
274- }
275-
276- uint16_t common_hal_pulseio_pulsein_get_len (pulseio_pulsein_obj_t * self ) {
277- return self -> len ;
278- }
279-
280- uint16_t common_hal_pulseio_pulsein_get_item (pulseio_pulsein_obj_t * self ,
281- int16_t index ) {
282- common_hal_mcu_disable_interrupts ();
283- if (index < 0 ) {
284- index += self -> len ;
285- }
286- if (index < 0 || index >= self -> len ) {
287- common_hal_mcu_enable_interrupts ();
288- mp_raise_IndexError ("index out of range" );
289- }
290- uint16_t value = self -> buffer [(self -> start + index ) % self -> maxlen ];
291- common_hal_mcu_enable_interrupts ();
292- return value ;
293- }
294-
295- #endif
0 commit comments