cassemblyarminterruptinterfacing

Using multiple hcsr04 sensors on Beaglebone Black


I am trying to use hcsr04 sensors on the Beaglebone black (adapted from this code - https://github.com/luigif/hcsr04)

I got it working for 4 different sets of sensors individually, and were now unsure of how to combine them into one program.

Is there a way to give the trigger and receive the echos simultaneously, such that interrupts can be generated as different events to the C program.

Running them one after the other is the last option we have in mind.


Solution

  • Russ is correct - since there's 2x PRU cores in the BeagleBone's AM335x processor, there's no way to run 4 instances of that PRU program simultaneously. I suppose you could load one compiled for one set of pins, take a measurement, stop it, then load a different binary compiled for a sensor on different pins, but that would be a pretty inefficient (and ugly, IMHO) way to do it.

    If you know any assembly it should be pretty straight-forward to update that code to drive all 4 sensors (PRU assembly instructions). Alternatively you could start from scratch in C and use the clpru PRU C compiler as Russ suggested, though AFAIK that's still in somewhat of a beta state and there's not much info out there on it. Either way, I'd recommend reading from the 4 sensors in parallel or one after the other, loading the measurements into the PRU memory at different offsets, then sending a single signal to the ARM.

    In that code you linked, the line:

    SBCO roundtrip, c24, 0, 4
    

    Takes 4 bytes from register roundtrip (which is register r4, per the #define roundtrip r4 at the top of the file), and loads it into the PRU data RAM (constant c24 is set to the beginning of data RAM in lines 39-41) at offset 0. So if you had 4 different measurements in 4 registers, you could offset the data in RAM, e.g.:

    SBCO roundtrip1, c24, 0, 4
    SBCO roundtrip2, c24, 4, 4
    SBCO roundtrip3, c24, 8, 4
    SBCO roundtrip4, c24, 12, 4
    

    Then read those 4 consecutive 32-bit integers in your C program.