2

I need to read all 7 analog pins in the BBB every 5 milliseconds. I'm doing so with the following C code:

void main(){
    char value_str[7];
    long int value_int = 0;

    FILE* f0 = fopen("/sys/bus/iio/devices/iio:device0/in_voltage0_raw", "r");

    while(1){
            fread(&value_str, 6, 6, f0);
            value_int = strtol(value_str,NULL,0);
            printf("0 %li\n", value_int);
            fflush(stdout);

            usleep(5000);
            rewind(f0);
    }

Hoever, the cpu usage goes up really high (20%). Is there any way to read the analog inputs differently so that it doesn't use as much CPU? Someone suggested "DMA" but I'm completely lost on that regard...

Any help will be appreciated.

Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
Rafael Vega
  • 4,575
  • 4
  • 32
  • 50

2 Answers2

5

This thread in the BBB forum was very useful and I ended up using libpruio. It uses the PRU to read the Beagle's built in io pins and analog to digital converters. The code I ended up using:

#include <stdio.h>
#include <unistd.h>
#include "pruio_c_wrapper.h"
#include "pruio_pins.h"

int main(int argc, const char *argv[]) { 
   PruIo *io = pruio_new(0, 0x98, 0, 1);
   if (io->Errr) {
      printf("Initialisation failed (%s)\n", io->Errr);
      return 1;
   }

   if(pruio_config(io, 0, 0x1FE, 0, 4, 0)){
      printf("Config failed (%s)\n", io->Errr); 
      return 1;
   }

   while(1){
      printf"\r%12o  %12o  %12o  %12o  %4X %4X %4X %4X %4X %4X %4X %4X\n"
         , io->Gpio[0].Stat, io->Gpio[1].Stat, io->Gpio[2].Stat, io->Gpio[3].Stat
         , io->Value[1], io->Value[2], io->Value[3], io->Value[4], io->Value[5]
         , io->Value[6], io->Value[7], io->Value[8]);
      usleep(1000);
    }

   pruio_destroy(io);

   return 0;
}
Rafael Vega
  • 4,575
  • 4
  • 32
  • 50
1

I suggest you use the PRU. It's very fast! This should get you started-> http://www.element14.com/community/community/knode/single-board_computers/next-gen_beaglebone/blog/2013/08/04/bbb--high-speed-data-acquisition-and-web-based-ui

Zaxter
  • 2,939
  • 3
  • 31
  • 48
  • If I understand correctly, they describe how to read samples from an external ADC in that article. I would like to use the bult-in BBB ADCs. – Rafael Vega Jun 12 '14 at 15:38