Streaming capture of ADC on BeagleBone (Black)
Project description
Streaming ADC capture on BeagleBone (Black) with PRU
Provides PRU firmware that captures up to 8 ADC channels, and userspace driver to receive this as a stream of buffers containing voltage readings from ADC.
Python is the most convenient way of using it. Lower-level API can be also accessed via dynamic library, if needed.
Features:
- configurable capture speed. Highest speed is around 15KHz.
- configurable set of AIN pins to capture. From just one AIN channel and up to 8 AIN channels
- reports dropped readings (when userspace client is not fast enough to process incoming buffers data is dropped to avoid buffer overflow)
- Uses just 15-20% CPU, leaving plenty of cycles to actually deal with the data
Requirements
- Hardware: BeagleBone (Black)
- OS: Debian GNU/Linux 10 (buster), see https://rcn-ee.com/rootfs/bb.org/testing/2019-12-10/buster-iot/
Building and trying
git clone https://github.com/pgmmpk/bbb_pru_adc.git
cd bbb_pru_adc/
make clean
make
python3 -m bbb_pru_adc.main
Stream structure
Each incoming buffer contains three pieces of information:
num_dropped
- the number of dropped readings before this buffer was filled (i.e. between readings from previous buffer and this buffer there was a gap). Under normal conditions this value is zero. It can not grow beyond0xffff
. Thus, if you are unlucky enough to receive0xffff
, it basically means that delay was at least that big (and probably bigger).values
- array of readings, packed in the channel-first order. It is anarray.array
object with elements offloat
type. Length isnum_readings * num_channels
.timestamps
- array of relative timestamps, corresponding to the readings. It is anarray.array
object with elements ofunsigned int
type. Length isnum_readings
. Value is the number of PRU clock ticks since the last reading. These values allow one to know exact timing between two readings. Time distance between readings is fairly stable, small deviations are due to varying codepaths to process outgoing and incoming messages.
How many readings do we have per buffer? This depends on the number of channels we capture. Exact answer is:
num_readings = (512 - 16 - 4) // (4 + 2 * num_readings)
This formula is mandated by IO buffer size limit (defined as 512 at kernel compile time).
For a given capture session number of readings per buffer stays the same.
Capture API
from bbb_pru_adc.capture import capture
with capture(speed=0, channels=[3, 5, 7], auto_install=False) as cap:
for num_dropped, timestamps, values in cap:
# do something with this information
This example starts capturing ADC inputs 3, 5, and 7 (channels=[3, 5, 7]
)
at full speed (speed=0
). It will not attempt to
install PRU firmware (auto_install=False
).
If driver detects that system firmware is missing or obsolete, and error will be thrown.
Capture has to be used as a context manager. The context is a generator spitting out the pieces of our buffer.
Capture parameters:
speed
- ADC capture speed as a clock divider value. Fastest is speed=0
, capturing at about 15KHz. In many applications 15KHz is just too much data (hard to process), and speed
can be set to other
values. For example, setting speed=9
will capture 10 times slower (at about 1.5KHz).
channels
- which AIN pins (aka channels) to capture.
auto_install
- if we detect that firmware is not installed, or is different, attempt to re-install by copying firmware file from python package resources to /lib/firmware
. This action requires root priveleges. Once installed, you can use the driver as a non-root user.
Links
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for bbb_pru_adc-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bbacb6d29d6d3d492c8794a70506560e487025006889db093fae4bbc5a4b9bd |
|
MD5 | b803055fc1d0fce62ac1ab0ca5cd8ab8 |
|
BLAKE2b-256 | c1869d71b4d28064c3448e36bdb59143f5f523943a61cda65853f16cc4cf5323 |