13

I have to time the time difference between high -> low and low -> high signal edge on GPIO pins. I have written simple program that does this. After running it for some time I was quite happy with the result (0,01 s variations). But from time to time there was like 0,5 s error. I was thinking that this may be due to some other system process running at that time. So my question is:

Can I reserve one processor core just for my program and let other 3 cores for the system?

I'm using Raspbian Jessie Lite, so I think that 3 cores will be enough to run it.

NonStandardModel
  • 286
  • 2
  • 12

4 Answers4

14

Dedicating a core is probably overkill.

I suggest you try my pigpio library. By default it will time GPIO level changes to within 10µs.

As a quick test I suggest you look at this Python example, which will print any GPIO level transition and the time in microseconds since the last transition on that GPIO.

pigpio is not installed by default in Jessie Lite. Either install the latest from the linked site or install the older version in the repositories.

sudo apt-get install pigpio python-pigpio python3-pigpio

pigpio - Library for Raspberry Pi GPIO control
python-pigpio - Python module which talks to the pigpio daemon (Python 2)
python3-pigpio - Python module which talks to the pigpio daemon (Python 3)
joan
  • 71,852
  • 5
  • 76
  • 108
5

You can lock your program to one core using schedutils as described in this Cyberciti article:

sudo apt-get install schedutils
sudo taskset -c 3 -p 13545  # Lock PID 13545 to core 3

Other processes can still be scheduled on the same core, though. So the second thing to do is to make sure your command runs with the highest priority using the nice command (this will tell the Linux kernel that other processes should be pre-empted if necessary). Start your program in this way:

nice -n -20 your-program

There are some other possible reasons for your timing issues. These are not as easy to do anything about:

  • If you are programming in Python there is a garbage collector that sometimes pauses your program to free up unused memory.
  • Interrupts makes the CPU handle something else than you want. For example, network packets or other input/output.
  • If your program is sleeping a lot there might be other processes that fill up the CPU caches (L1/L2 cache). This forces you to wait for RAM access.
    • Even worse if your RAM is full so that your process gets swapped out to disk because SD cards are sloooow.

There are ways to make your process realtime, which means that it will run with certain timing guarantees. The problem with this is that everything else might be slower, and it is a complex topic. If you want to go down this rabbit hole I suggest you start reading up on real time processes in Linux.

3

Since you are having timing requirements Raspberry Pi is no longer the appropriate platform for this. It is not a real time platform and timing can be thrown off by a lot of different sources of interference.

Instead you should use a microcontroller to measure this time, preferably using interrupts, and pass the information to the Pi later.

Maxthon Chan
  • 1,051
  • 8
  • 14
2

As per you requirement I don't think you need to use a single core processor. What you need is to make sure your program runs all the time. To achieve that, you can set the priority of your program very high, so that it does not get disturbed by any other process.

As far as I know the OS (General Purpose OS), that we use are not designed to be used in real time systems, so if you want to run your process in real time so that no other process disturbs it, you need to go for a Real Time OS (RTOS). Maybe they will come up with core selection. :)

Bex
  • 2,929
  • 3
  • 26
  • 34