1

Let's say I want to sample a pair of GPIO pins of my Raspberry Pi* with a frequency around 10kHz to feed a software-based signal analyzer (written in C for instance). What is the most appropriate method to obtain an accurate timestamp for each sample? Accurate means, the delay between acqiring the sample and reading the clock/time source should be at least constant or negligible at best. The signal analyzer does not necessarily operate in realtime.

I am aware that the sampling quality may also be affected by other circumstances (e.g. preemptive mulititasking), but the question is only about acuqiring an accurate timestamp for each sample.

*) In general: any signal like a pulse wave (high/low) or an audio signal (quantized). The Raspberry Pi is only an example, since it has everything to acquire the signal already built-in.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
code_onkel
  • 2,759
  • 1
  • 16
  • 31
  • You want to sample what RaspberryPi outputs or want to write code for RaspberryPi which processes the inputs? Consider tagging RaspberryPi. – Yunnosch Feb 01 '18 at 13:13
  • 2
    Unless you're running a hard real time operating system sampling things in software is probably a bad idea. In general these kinds of applications usually either do sampling in hardware to a buffer which then a driver reads periodically or the samples have timestamps provided by the hardware. Also, if you're reading from userland (not a driver in the kernel) you might have problems getting a frequency better than 100Hz. – Art Feb 01 '18 at 13:13
  • Question as written has nothing really to do with C, unless you are asking about how to use the C libraries to produce time-stamps. Then you would want to include some code, showing the attempt of what you have tried. And as already commented, you should include `Raspberry Pi` tag. For starters, _[what reading have you done](http://www.hertaville.com/introduction-to-accessing-the-raspberry-pis-gpio-in-c.html)_? – ryyker Feb 01 '18 at 13:25
  • The raspberry is only an example to acquire the source signalr. The method may or may not be platform dependent. I consider this part of the answer. In the same manner, C is a likely candidate for such a piece of software, but maybe assembler would be a better choice. – code_onkel Feb 01 '18 at 13:30
  • 1
    Is this article in the ballpark of what you want to do? _[Introduction to accessing the Raspberry Pi's GPIO in C++ (sysfs)](http://www.hertaville.com/introduction-to-accessing-the-raspberry-pis-gpio-in-c.html)_? From there, you could easily enough add code to use C libraries to log time-stamps. – ryyker Feb 01 '18 at 13:32
  • @Art Thank you. That's the kind of insight I am interested in. – code_onkel Feb 01 '18 at 13:33
  • @ryyker Yes, basically, but my question is about the sampling accurary, not the mere "read the value from a GPIO pin" part. – code_onkel Feb 01 '18 at 13:37
  • 2
    _["Firstly, timing done via software loops involves endless fussing over the exact clock cycle count. This code solves that problem by using..."](https://www.raspberrypi.org/forums/viewtopic.php?t=52393)_. Maybe this will address some of your questions. – ryyker Feb 01 '18 at 13:42
  • How long do you need to acquire for - total duration? – Mark Setchell Feb 01 '18 at 19:10

0 Answers0