I am getting a signal at 1 GPIO which i need to read at an interval of exactly 10^ (-6) seconds. I am using wiringpi.h in C and the issue is that digitalRead() also takes some time thus i am not able to ensure exact timing. Please Suggest a solution
3 Answers
To "ensure exact timing" you will need to use a microprocessor. Perhaps an Arduino would be suitable. You should be able to find one which will "ensure exact timing" to any accuracy you need.
A multi-tasking multi-user operating system such as Linux is not suitable for this task.
-
1The real problem is not the hardware that is being used - and I think you meant to suggest to use a microcontroller instead of a microprocessor - but as you do point out, the operating system being used. For this task, you need to use a real time operating system, or even no operating system at all. Commented Apr 22, 2019 at 18:05
-
@GlenYates The problem is not just Linux. There are things going on behind the scenes on the SoC which means you can't guarantee "ensure exact timing" for many meanings of "ensure exact timing".– joanCommented Apr 22, 2019 at 20:15
My 2 cents:
Similar to the Arduino (8 bit AVR based uC) recommendation I would look at the far more capable STM32 series from ST microelectronics.
- There are at least two good Arduino API ports here and here,
- Great support forum
- Inexpensive: ~$2 for the cheapest breakout boards - "Bluepill" and a reusable ST Link V2 clone programmer
- Plethora of periperials like SPI, I2C, USART, USB, DMA and sophisticated Timers with a 32 bit ARM Cortex M0/M3/M4 CPU
With a bit of ramp up you could use a combination of DMA, Timers and CPU logic to acquire from GPIO pins, condition, compress and stream out the data via USB, possibly even to a Raspberry Pi.
Using PlatformIO with VSCode/Atom makes development much easier
-
How nice to read your recommendation on STM32. I fully agree with you. Now I have updated my answer, in order not to mislead newbies. It is now the age of system integration, low level things are getting less and less useful year after year, and can no longer earn you a living.– tlfong01Commented Apr 23, 2019 at 2:00
I think a simple sequential logic circuit can easily do the job in a couple of steps.
Get a 32 MHz clock.
Every 1 uS clock one bit data input to a D type flip flop and feed its output to a serial to 8 bit parallel register.
Every 8 uS clock the parallel register contents into a flash memory.
For an Electronics Enggr major undergrad, I think it is much easier to do the above 3 steps using sequential logic than debugging a messy mpu/mcu macro assembly program.
Using amtel/arm/stm mpu/mcu/ etc, you still need to read one bit every uS, and do the serial to parallel job before storing the 8 bit word to some flash memory. In other words, just wasting time and money.
The advantage of using logic circuit is that Rpi can directly oversee and control everything, without losing one bit of data. Once Rpi starts the bit D flip flop + flash memory reader, it can leave it standalone reading bit non stop into the flash memory, which Rpi can come back any time to read the flash, like it is a 1Gbit long FIFO or circular buffer.
My electronics skills are very rusty. Perhaps we can pass the question to the Electronics and Arduino Stack Exchange and see what they think.
By the way, don't say that 1uS is fast, because those EE guys talking nS/pS everyday would LOL!
Update 2019apr23hkt0858
After reading the answer from @Shreyas Murali, I think I need to point out that I don't encourage any electronic enggr beginners to learnt basic logic gates circuit design, for the same reason that newbies should not learn assembly language hoping to earn a living, because over these years hardware has become so fast that even "slow" C in mcu becomes fast enough for tasks in real life. There is no need to use assembly language which might run fast but the much longer development time is not worth it.
IN short, life is too short to learn things that could not earn you a living.
Rpi Raspbian is not realtime OS and as such is very weak in real time IO.
My first Arduino is Decimilla and last Arduino Due (ARM Cortex-M3).
Now the ugly Arduino vendors are suing each other, and I don't want to hear the name XYZduino any more.
To do realtime tasks, I would start a fresh with something like STM32 Blue Pill, which I all the things I dream but Rpi will not give, even Rpi4, 5, 6 (USART x 3, I2C x 2, SPI x 2, Can x 1, ...).
What I want is humble, only more UART/SPI/I2C channels. I actually had them when I played with small guys like PyBoard, ...