0

I have a program where each step, two GPIO pins have their duty cycle updated (I'm using pigpio). I'm running the program for millions of steps (think 50 to 100 million) and running the PWM outputs through a low pass filter and then to an oscilloscope. It updates kinda slowly. How might I speed this up so I don't have to wait 15+ minutes to watch all of the motion on the oscilloscope? (Note: the rest of the program is pretty fast; I timed it at one point at it was ~30 seconds on its own, but I have to see the output.)

1
  • Are you using Python? Either way let's see the program you are using. Also what are you trying to do? There may be another solution.
    – joan
    Jun 15, 2018 at 7:18

1 Answer 1

2

Have you looked at the source for pigpio? Or programmed a microcontroller that required you to access pins at the hardware level? I'll explain why I ask this in a few lines.

If you're running this application in Python, then the communication between python and the pigpio is run through a socket, which is another layer that slows things down.

Pigpio is an abstraction layer to allow access to the raw hardware of the Raspberry Pi's processor. It is well written and made to be fast, but it's a large library because it has to be. At the hardware level, the gpio are not organized by pin, but rather by registers. Typically these are 8 bytes wide, so each register controls 8 gpio pins. So to set a single pin high or low, the following must take place.

Setup: This happens once unless you change something about a pin's setup. Let's say this is just to set the pin to input or output, with a pullup resister. This is just a rough description based on my experience 15 years ago. Basically all the ease of use that pigpio gives you comes at a price.

  1. Determine which register the pin p is in. I'm going to say its in resister A.
  2. Determine which bit of register A is needed for pin p. I'll call this x.
  3. Create a constant C so that you can set the pin you wish without effecting the other pins. This constant is C = 0x01 << x.
  4. First, make the pin into an input/output by and'ing or or xor'ing the i/o register A with C.
  5. Second, make the pin have a pullup resister by doing a very similar thing with the pullup register A.

Done every pin access Now we're working directly with resister A.

Steps 1-3 are the same. 4. And/or C with register A.

And there are some sanity checks that are used. Some registers aren't fully used. Pigpio must make sure that the pin is within range (in other words, there is a pin with that number).

If you're willing to do this all yourself, you can speed up the access quite a bit.

But please remember that I'm writing this from memory. I used to do this all the time on PIC microcontrollers, but that was 15 years ago.

2
  • What resources could I look at to learn to do this by myself? I'm going to be doing a lot with this sort of thing, and the less time I spend waiting for things to run through, the better. Also, would it be faster to (for example) use only certain pigpio commands/import only part of the pigpio module/etc? Or is there a faster library than pigpio? Thanks for answering! Jun 15, 2018 at 0:08
  • I would look at the source to pigpio. You could program in C or Rust, they are both faster language than python if you're concerned about running time. If you're concerned about your programming time, then python better, usually.
    – NomadMaker
    Jun 15, 2018 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.