I'm working on Android Things on an rpi3 dev kit, with the IMHO excellent Picon Zero hat. I'm trying to trigger and read the reply from a HC-SR04, which requires a ~10 μS trigger pulse, then quickly swap the GPIO to be an input, and listen for the length of the reply pulse.
But things in Java are sloooooww compared to Arduino. (I know, real-time system vs RPi3, apples and oranges). I tried a variety of "wait for a small enough amount of time to have it be just 10 μS" and everything was way too big.
With a target of 10,000 nS:
- Vanilla Android Spin-wait reading
System.nanoTime()= 125% (best!)
- Kotlin's coroutine "delay" = 2000%
And the only thing that could consistently get a time-slice that small was two sequential
System.nanoTime() calls - which had ~ 14 μS between them. And that was before stuffing back in the calls to change the GPIO pin from write to read.
Is this expected? It seems slow. 900 MHz (each core) is 9000 cycles in that 10 μS window I'm shooting for. Having literally the one instruction of "get nano time" eat the entire 9000 cycles seems... odd.