@@ -335,4 +335,65 @@ uint32_t ap3_gpio_enable_interrupts(uint32_t ui32Pin, uint32_t eIntDir)
335335
336336 return AM_HAL_STATUS_SUCCESS;
337337
338- } // am_hal_gpio_pinconfig()
338+ } // am_hal_gpio_pinconfig()
339+
340+ /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
341+ or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
342+ to 3 minutes in length, but must be called at least a few dozen microseconds
343+ before the start of the pulse.
344+
345+ Original Arduino function could operate in noInterrupt() context. This
346+ function cannot.
347+ */
348+ unsigned long pulseIn (uint8_t pinNumber, uint8_t state, unsigned long timeout)
349+ {
350+ return (pulseInLong (pinNumber, state, timeout));
351+ }
352+
353+ /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
354+ or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
355+ to 3 minutes in length, but must be called at least a few dozen microseconds
356+ before the start of the pulse.
357+
358+ ATTENTION: This function relies on micros() so cannot be used in noInterrupt() context
359+ */
360+ unsigned long pulseInLong (uint8_t pinNumber, uint8_t state, unsigned long timeout)
361+ {
362+ uint8_t padNumber = ap3_gpio_pin2pad (pinNumber);
363+
364+ if (timeout > 3 * 60 * 1000000L )
365+ timeout = 3 * 60 * 1000000L ; // Limit timeout to 3 minutes
366+
367+ // Enable fast GPIO for this pad
368+ am_hal_gpio_fastgpio_disable (padNumber);
369+ am_hal_gpio_fastgpio_clr (padNumber);
370+ am_hal_gpio_fast_pinconfig ((uint64_t )0x1 << padNumber, g_AM_HAL_GPIO_OUTPUT_WITH_READ, 0 );
371+
372+ uint32_t startMicros = micros ();
373+
374+ while (am_hal_gpio_fastgpio_read (padNumber) == state) // Wait for previous pulse to end
375+ {
376+ if (micros () - startMicros > timeout)
377+ return (0 ); // Pulse did not end
378+ }
379+
380+ while (am_hal_gpio_fastgpio_read (padNumber) != state) // Wait for pin to change state
381+ {
382+ if (micros () - startMicros > timeout)
383+ return (0 ); // Pulse did not start
384+ }
385+
386+ startMicros = micros (); // Restart time
387+
388+ while (am_hal_gpio_fastgpio_read (padNumber) == state) // Wait for pin to exit sought state
389+ {
390+ if (micros () - startMicros > timeout)
391+ return (0 ); // Pulse did not end
392+ }
393+
394+ uint32_t stopMicros = micros ();
395+
396+ am_hal_gpio_fastgpio_disable (padNumber);
397+
398+ return (stopMicros - startMicros);
399+ }
0 commit comments