View: 13397|Reply: 2

The best way to get microseconds precision time?

[Copy link]

1

threads

3

posts

65

credits

Registered member

Rank: 2

credits
65
Published in 2016-10-20 09:09:38 | Show all floors |Read mode
Hey folks,

It's my first time here. Cheers o/

Well, I have one Orange Pi Plus 2 and I am trying to make it read from one HC-SR04 utrassonic sensor. And it sends the measured distance by turning its echo port high by one instant of time. This time is proportional to the measured distance.
To work this out, I have a interruption callback using WereCatf's WiringOP fork, that works good but I can't get correct timing because the function I am using don't give me a good and accurate values.
I've used clock_gettime() with CLOCK_REALTIME but it have some milliseconds increments every second (even if I run OPi for performance) and I don't want more than 58 microsecond increments, because this is the time for each centimeter to be measured by the sensor (more info here https://docs.google.com/document ... 2qR4vP8saG73rE/edit).

So do you guys know any way to get one time with microseconds accurace? I saw in the Allwinner H3 documentation something about it's hardware Timers but that is completely unclear to me on how to setup and make it work properly.


Thank you and sorry about my english.

4

threads

8

posts

70

credits

Registered member

Rank: 2

credits
70
Published in 2017-9-16 13:24:13 | Show all floors
Edited by DarkSorrow at 2017-9-16 13:29

As you mentioned in your last paragraph you will have to take access of hardware timer.
Though I have never done this on Orange Pi or any Allwinner processor, I had done once for completely different SBC.
It is very similar to bare-metal programming like that of micro-controllers.
You will have to use mmap system call to map/get physical address of peripheral's (timer's) register address. Once you get the proper physical address of the necessary registers it is just matter of setting proper values. You will also have to write interrupt handler and register interrupts if you want interrupt driven approach or alternatively you can also pool for flag in while loop (very inefficient).
Refer page 156 of Allwinner H3 documentation to get Timer's register map.

27

threads

58

posts

2239

credits

Gold member

Rank: 6Rank: 6

credits
2239
Published in 2019-11-1 04:02:51 | Show all floors
There is the so called High-Speed-Timer at address 0x01C60000 (https://github.com/vanvught/rpid ... h3/include/h3.h#L60).
Maybe you can do a open("/dev/mem", O_RDWR | O_SYNC)  in Linux for direct access. Otherwise you are stuck with the precision given by the Linux API's.
You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list