libpruio (BB D/A - I/O fast and easy)

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
mcorrea
Posts: 1
Joined: Feb 06, 2017 11:33

Re: libpruio (BB D/A - I/O fast and easy)

Post by mcorrea »

Mr TJF, thanks for the explanation and for the libary. It is super functional to serius projects. You can see below my code that reads two ADC channels and after the reading it prints the values.

I'd like to efetuate some electrical measurements with the values. To do that, I'd like to write all the algorithms using Pyton or C in the Cloud9. I have to access the variable v1[] and v2[] in the code bellow which uses the libpruio and runs at the PRU.

How could I do this? I mean, how could I acces these values and use then in aprogram witten in C/C++ or Pyton in the Cloud9?

Code: Select all

#include "../c_wrapper/pruio.h" // include header	// Include libaries
#include "time.h"	
	
int main(int argc, char **argv)	
{	
 float n[256];	//Declaration of variables
float v1[128];	
float v2[128];	
 int i = 0;	
	
  pruIo *io = pruio_new(PRUIO_DEF_ACTIVE, 0x98, 0, 0);	//Create a new driver structure
  pruio_adc_setStep(io, 9, 1, 0, 0, 0); 	//Step 9, AIN-0
  pruio_adc_setStep(io, 10, 2, 0, 0, 0);	//Step 10, AIN-1
	
  if (pruio_config(io, 128, 3<<9 , 156250, 4)){ 	// upload settings: 128 samples, 6250 Hz, 16 bits
                              printf("config failed (%s)\n", io->Errr);}	
  else {	
	
  if (pruio_rb_start(io)) printf("rb_start failed (%s)\n", io->Errr); // start measurement	//Starts the rb_mode
	
  else{	
        sleep(1);	//One second breathing
        do{	
                if(io->DRam[0] == (i)){	//Logic to fill the buffer: If Dram[0] changes, store the Adc value read.
                        n[i] = io->Adc->Value[i];	
                        i=i+1;	
                }	
        }while(i < 256);	//256 times, because the ring buffer has 128 x 2 positions
	
        for(i = 0; i<=127; i++ ){	//Logic to divide values of each ADC in two differents vectors.
         v1[i] = n[2*i];	
         v2[i] = n[2*i +1];	
        }	
	
        printf("adc1*************\n");	//Print the value of the AIN0
        for(i=0; i<=127; i++){	
         printf("%f \n", v1[i]*1.8/65536);	
         }	
	
        printf("\n adc2*************\n");	//Print the value of the AIN1
        for(i=0; i<=127; i++){	
         printf("%f \n ", v2[i]*1.8/65536);	
        }	
	
}	
	
}	
  pruio_destroy(io);        /* destroy driver structure */	
        return 0;	
}	
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: libpruio (BB D/A - I/O fast and easy)

Post by TJF »

Hi mcorrea, sorry i don't understand your question.
mcorrea wrote:... To do that, I'd like to write all the algorithms using Pyton or C in the Cloud9. I have to access the variable v1[] and v2[] in the code bellow which uses the libpruio and runs at the PRU.
This code does not run at the PRU. It runs at the ARM CPU.
mcorrea wrote:How could I do this? I mean, how could I acces these values and use then in aprogram witten in C/C++ or Pyton in the Cloud9?
You're already running C code. If you want to use Python instead, you can either create a Python binding by transforming the C header with SWIG. Or check out the CType based Python bindings, which you can find here.

Regards
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: libpruio (BB D/A - I/O fast and easy)

Post by TJF »

@mcorrea

One more hint: I wonder why you use RB mode. RB mode is designed for use cases with continuous measurements, when the amount of data is bigger than the ERam size (which is 256k by default). Another use case is when evaluation of the data must happen during the measurement.

In your case you sample 256 values (= 512 bytes) and then quit the program. When you use MM mode instead, the code gets much easier (untested):

Code: Select all

#include "../c_wrapper/pruio.h" // include header   // Include libaries
#include "time.h"

int main(int argc, char **argv)
{
  int i = 0;

  pruIo *io = pruio_new(PRUIO_DEF_ACTIVE, 0x98, 0, 0);   //Create a new driver structure
  pruio_adc_setStep(io, 9, 1, 0, 0, 0);    //Step 9, AIN-0
  pruio_adc_setStep(io, 10, 2, 0, 0, 0);   //Step 10, AIN-1

  if (pruio_config(io, 128, 3<<9 , 156250, 4)){    // upload settings: 128 samples, 6400 Hz, 16 bits
                              printf("config failed (%s)\n", io->Errr);}
  else {
    if (pruio_mm_start(io, 0, 0, 0, 0)) printf("mm_start failed (%s)\n", io->Errr); // start measurement   //Starts in mm_mode

    else{
      printf("adc1*************\n");   //Print the value of the AIN0
      for(i = 0; i < io->Adc->Samples; i += io->Adc->ChAz)
        printf("%f \n", (io->Adc->Value[i] * 1.8 / 65520));

      printf("\n adc2*************\n");   //Print the value of the AIN1
      for(i = 1; i < io->Adc->Samples; i += io->Adc->ChAz)
        printf("%f \n", (io->Adc->Value[i] * 1.8 / 65520));
    }
  }
  pruio_destroy(io);        /* destroy driver structure */
  return 0;
}
Regards
chenhx
Posts: 1
Joined: Mar 19, 2018 16:57

Re: libpruio (BB D/A - I/O fast and easy)

Post by chenhx »

Hi,

I meet an error in building cmakefbc.

---------
Make Error at doxy/CMakeLists.txt:6 (INCLUDE):
include could not find load file:

UseFb-Doc


CMake Error: Could not find cmake module file: CMakeDetermineFbcCompiler.cmake
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_Fbc_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_Fbc_COMPILER
CMake Error: Could not find cmake module file: /home/debian/cmakefbc/CMakeFiles/3.5.2/CMakeFbcCompiler.cmake
CMake Error at cmakefbc_deps/CMakeLists.txt:4 (PROJECT):
No CMAKE_Fbc_COMPILER could be found.

Tell CMake where to find the compiler by setting the CMake cache entry
CMAKE_Fbc_COMPILER to the full path to the compiler, or to the compiler
name if it is in the PATH.


CMake Error: Could not find cmake module file: CMakeFbcInformation.cmake
CMake Error: CMAKE_Fbc_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "/home/debian/cmakefbc/CMakeFiles/CMakeOutput.log".
--------

uname -a
Linux beaglebone 4.4.113-ti-r148 #1 SMP Wed Mar 7 19:19:19 UTC 2018 armv7l GNU/Linux

I don't know what is the problem. I tried both cmake 2.8.3 and cmake 3.5.2, but they don't work.....
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: libpruio (BB D/A - I/O fast and easy)

Post by TJF »

Hi chenhx, welcome to the forum!

First, sorry for the late answer. I got the notification on your post just today.

You didn't mention it, but I guess you're using the libpruio-0.4 version from https://github.com/DTJF/libpruio.
chenhx wrote: Make Error at doxy/CMakeLists.txt:6 (INCLUDE):
include could not find load file:

UseFb-Doc
There's a problem in the cmakefbc installation on your system. CMake tries to load the script UseFb-Doc.cmake, but couldn't find it in the CMAKE_MODULE_PATH.
chenhx wrote: ...
CMake Error: Could not find cmake module file: /home/debian/cmakefbc/CMakeFiles/3.5.2/CMakeFbcCompiler.cmake
CMake Error at cmakefbc_deps/CMakeLists.txt:4 (PROJECT):
Either install cmakefbc for the 3.5.2 version.

Or use the cmake files shipped with the libpruio package by executing the preparing command

Code: Select all

cmake . -DCMAKE_MODULE_PATH=./cmake/Modules
for in-source builds, or for out-of-source in a subfolder in the projects directory

Code: Select all

mkdir build
cd build
cmake .. -DCMAKE_MODULE_PATH=../cmake/Modules
Good luck!
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

libpruio version 0.6 is out now

Post by TJF »

Image

Major highlights:
  • Debian packages, easy installation
  • Python bindings and examples available
  • Pinmuxing faster and unlimited
It's tested on kernel 3.8.13 and several 4.x versions (-bone and -ti).

1) Packages

Arend Lammertink created Debian packages and hosts them at his server. The following resources are available:

Code: Select all

   Size | Package                      | Description

1291932 | libpruio_0.6.0.tar.xz        | source code
  45686 | libpruio_0.6.0_armhf.deb     | shared lib binary and LKM
  80620 | libpruio-bin_0.6.0_armhf.deb | executable examples
  18552 | libpruio-dev_0.6.0_armhf.deb | examples/bindings C
  27626 | libpruio-bas_0.6.0_armhf.deb | examples/bindings FreeBASIC
  17902 | python-pruio_0.6.0_armhf.deb | examples/bindings Python
4083394 | libpruio-doc_0.6.0_all.deb   | documentation
(Size may vary, just a rough marker for the download volume.)

In order to download them from the PPA, add it to your sources list

Code: Select all

sudo echo "deb http://beagle.tuks.nl/debian jessie" > /etc/apt/sources.list.d/tuks.nl.list
sudo echo "deb-src http://beagle.tuks.nl/debian jessie" >> /etc/apt/sources.list.d/tuks.nl.list
(In case of 3.8 kernel replace 'jessie' by 'wheezy'.)

Then grep the security keyring by

Code: Select all

wget -qO - http://beagle.tuks.nl/debian/public.key | sudo apt-key add -
and install as usual

Code: Select all

sudo apt-get install libpruio-dev # development in C programming language
sudo apt-get install libpruio-bas # development in FreeBASIC
sudo apt-get install python-pruio # development in Python
sudo apt-get install libpruio-doc # common documentation
sudo apt-get install libpruio-bin # test pre-compiled examples (!read docs first!)
All these packages depend on the binary package 'libpruio'. The -lkm package installs a new system group on your box, named 'pruio'. Note: removing the package does not remove this group (a Debian policy). Find details in the online docs.

2) Python

Python bindings are now shipped in the 'python-pruio' package. It also contains example source code (the reduced C set).

Packing for several Python versions is a mess, and out of my league. My solution: the code installs to folder '/usr/share/doc/python-pruio/examples'. Copy that directory to your working directory, and there execute the source code with your prefered Python version.

It's my first Python experience, so please be patient and report in case of trouble.

3) Pinmuxing

This version has advanced pinmuxing capabilities. It doesn't need a punmux-helper device tree overlay any more. The remaining overlay action is enabling the PRUSS and loading the `uio_pruss` driver, ie by the following configuration in file '/boot/uEnx.txt':

Code: Select all

enable_uboot_overlays=1
uboot_overlay_pru=/lib/firmware/AM335X-PRU-UIO-00A0.dtbo
#enable_uboot_cape_universal=1
Instead libpruio uses its own loadable kernel module. This fixes the kernel 4.x PWMSS bug and can set all modes for all header pins, which are not claimed by the kernel. You don't need cape_universal* overlays any more. This speeds up boot time and reduces kernel memory footprint. (The wheezy -lkm package is broken, you've to install from source - sorry.)

The package manager installs a new system user group named 'pruio' at your box. Make yourself a member of that group and you can do pinmuxing from user space (without administrator privileges). Therefor execute 'sudo adduser <YourUserID> pruio'
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

libpruio version 0.6.2 is out now

Post by TJF »

Image

Major highlights:
Find
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

libpruio version 0.6.4 is out

Post by TJF »

Image

Major highlights:
  • PocketBeagle support (experimental)
  • PRU examples also for Python
  • Improved Documentation
Find
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: libpruio (BB D/A - I/O fast and easy)

Post by Dinosaur »

Hi All

Investigating the suitability of the BBB in some of my projects.
Certainly the spec indicates that it will be suitable.
The availability of this library has taken away the complexity of GPIO which previously was my biggest obstacle.
My compliments to TJF for the enormous amount of work that must have been involved in this.

As I haven't played with one yet, the reading of this thread has left me with some confusion. (easy for a Dinosaur)
So from a beginners point of view:

Can I install the library on my development computer (Linux MInt 18.3) , compile the code and the load the program into the BBB for execution.?
If So, what supporting software has to be loaded onto the BBB ?
If Not and it needs the compiler to be on the BBB, is there a step by step installation instruction for doing so.

Regards
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: libpruio (BB D/A - I/O fast and easy)

Post by TJF »

Hi Dinosaur!

As far as I know your work the BBB should be a good choise for your applications. Handling GPIO on LINUX isn't a big obstacle, but configuring the system (pinmuxing). You're correct, for libpruio I spend a lot of time to bypass all that mystic device tree stuff, in order to make system configuration as easy and as much fun as on the Arduino.
Dinosaur wrote: Can I install the library on my development computer (Linux MInt 18.3) , compile the code and the load the program into the BBB for execution.?
Some users reported that they installed a cross-compile environment on a PC, compile the binary there against libpruio and copy it on the BBB for testing. (Testing needs the PRU and the CPU subsystems hardware, so it cannot be done on a PC.)
Dinosaur wrote: If So, what supporting software has to be loaded onto the BBB ?
Sorry, I've no experience in this. I did all "the enormous amount of work that must have been involved in this" natively on the BBB. So I cannot help you regarding the installation of a cross compiler.

But you should know that I never missed a cross-compiler. All my source code is separated in modules. The build process is organized by the management system CMake. For a new build only up-dated code (modules) get compiled. So the build process isn't much slower than building on a PC. (It's just the documentation I build on a PC by Doxygen.)

And often I've several programs for an appilcation. One of them is the main controller starting very early in the boot process. Later the GUI and perhaps the network connection get startet. All programs exchange data by shared memory. That way I can ie. exchange the GUI part while the main controller is still running. This concept may be beneficially for your tasks as well.
Dinosaur wrote:If Not and it needs the compiler to be on the BBB, is there a step by step installation instruction for doing so.
Find a step-by-step guide to install an environment for building libpruio on the BBB in the docs in chapter Preparation. A system that can compile libpruio is also capable to compile your FB source code.

Do not hesitate to ask here for further help in case of trouble.

Regards
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: libpruio (BB D/A - I/O fast and easy)

Post by Dinosaur »

Hi All

TJF, many thanks for the detailed reply.
Before I commit myself and start buying a full test kit, a couple of further questions.

I bought the 2019 edition of "Exploring Beaglebone" book by Derek Molloy and am reading it to familiarize myself.
Equally you have a lot of information on the German FB site which I have to study.

1: I gather that the standard FB compiler can be installed and does not need to be compiled on the BBB.
Your use of the words "Cross Compiler" gave me doubt.

2: On my "Fitlet" installations (they have an internal 32Gb mSata) i develop by using a usb/mSata adapter and boot of the usb.
It is reasonably fast.(under 1 Minute)
On the BBB I can boot using a uSD , which I have also done on the Fitlet but it is very slow.
Did you find that the embedded 4Gb MMC was large enough to install all the extra tools and a GUI application ?
Once my GUI is loaded I don't access the disk unless settings are modified, so a uSD boot may suffice.

3. Do you use the Linux that comes pre-installed or use your own flavour.?
What were your reasons ?

Regards
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: libpruio (BB D/A - I/O fast and easy)

Post by TJF »

Dinosaur wrote:I bought the 2019 edition of "Exploring Beaglebone" book by Derek Molloy and am reading it to familiarize myself.
Equally you have a lot of information on the German FB site which I have to study.
The guides from D. Molloy are very good. I used them a lot while learning. Unfortunately they're related to the outdated kernel 3.8, AFAIK. A lot of things changed in kernel 4.x. The book wont help much. And you won't need that stuff when you use libpruio, since you'll have single source. All configuration gets done in your code. This concept isn't described in the book.
Dinosaur wrote:1: I gather that the standard FB compiler can be installed and does not need to be compiled on the BBB.
Your use of the words "Cross Compiler" gave me doubt.
Installing FB on BBB is straight forward (commands as on any Debian based LINUX, like Mint):

Code: Select all

sudo apt update; sudo apt install freebasic
A cross compiler is a compiler that runs on one system while generating a binary for an other architecture. Ie. it runs on i386 and compiles for armhf. From my point of view you wont need this for the controller stuff. It may be helpful for a big GUI application. But the GUI part you can develop on the PC, and compile the final code only once on the BBB.
Dinosaur wrote:2: On my "Fitlet" installations (they have an internal 32Gb mSata) i develop by using a usb/mSata adapter and boot of the usb.
It is reasonably fast.(under 1 Minute)
On the BBB I can boot using a uSD , which I have also done on the Fitlet but it is very slow.
Did you find that the embedded 4Gb MMC was large enough to install all the extra tools and a GUI application ?
Once my GUI is loaded I don't access the disk unless settings are modified, so a uSD boot may suffice.
The EMC is big enough for an IOT image (console without graphical desktop). In contrast when installing a graphical desktop there isn't much space left. Enough to compile one application, but not enough to generate big log files.

I don't use EMC. Instead I boot from uSD, so that I can exchange all system software by just exchanging the card. The boot time depends on the used software. Ie. a ntp time synchronization needs the network and then the response from the server. The graphical desktop also needs some boot time. In any case booting should be shorter than 60 s. A console image needs about 15 s, the desktop comes up in about 30 s.
Dinosaur wrote:3. Do you use the Linux that comes pre-installed or use your own flavour.?
What were your reasons ?
I use standard images. The current stable kernel is 4.14. In most cases I don't install a graphical desktop. Instead I use an http server (lighttpd) that provides an http GUI to be used in any browser. The FB stuff gets connected by fcgi. This makes it easy to remote control the system from anywhere. And I can omit some hardware (display, keyboard and mouse/trackball).

Regards
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: libpruio (BB D/A - I/O fast and easy)

Post by Dinosaur »

Hi All

TJF, thanks for your reply.
I have bought a unit and will play with it and see.

Regards
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: libpruio (BB D/A - I/O fast and easy)

Post by Dinosaur »

Hi TJF

Usually when I do something new, I write down every step I take in the installation.
So, I want to make sure I only use legit steps in the setup of BBB and the library.

I "read" skimmed over every post in this thread, and hopefully none of those difficulties prop up for me.

The first step I have taken was to confirm the BBB worked, and then wrote the Ubuntu precise image to a uSD.
At the moment I am waiting for a uHDMI cable before I can proceed.(I can't see the screen when I boot the uSD)
Then Install the full desktop and the Poseidon editor, after which compiling a test program will prove things.
Once that works I will image the uSD and save a copy.

Interestingly I can't see/browse the on board 4Gb MMC with only the usb cable powering it ?


1: Installing the library:
sudo apt-get install libpruio-bas libpruio-lkm libpruio-doc
Is this the only step required if I don't want to compile from Github?
Does this include the LKM option ?
EDIT: Rephrase that: Does this enable the LKM

2: You stated in early posts that you use Ubuntu.
I will install Ubuntu full desktop on a 32Gb uSD and use Poseidon as the editor for FB.
Do you see any problems in this configuration ?

3: Are there any other Files/libraries to install ?

Regards
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: libpruio (BB D/A - I/O fast and easy)

Post by TJF »

Hi Dinosaur!
Dinosaur wrote:The first step I have taken was to confirm the BBB worked, and then wrote the Ubuntu precise image to a uSD.
At the moment I am waiting for a uHDMI cable before I can proceed.(I can't see the screen when I boot the uSD)
Then Install the full desktop and the Poseidon editor, after which compiling a test program will prove things.
Once that works I will image the uSD and save a copy.
I don't know Poseidon. I still use Geany, loading and editing the source from the BBB over the network connection. Once written back, I compile on the BBB by invoking the FB compiler on the command line over ssh connection (controlled by CMake build management system).
Dinosaur wrote:Interestingly I can't see/browse the on board 4Gb MMC with only the usb cable powering it ?
I'm not sure what you're talking about (4Gb = 4 Giga bit = 512 Mega byte = 512 MB).

The boot drive is /dev/memblk0. When booting from uSD the MMC (4GB on rev. C, 2 GB on old rev. B) is /dev/memblk1. You'll have to mount it before you can access it.

Usually I destroy the MMC boot sector, so that I'm sure the system is booting from uSD (without pressing any button).
Dinosaur wrote:1: Installing the library:
sudo apt-get install libpruio-bas libpruio-lkm libpruio-doc
Is this the only step required if I don't want to compile from Github?
Does this include the LKM option ?
EDIT: Rephrase that: Does this enable the LKM
This installes/enables the LKM. But the command doesn't work out of the box. Currently the packages are only at the PPA from Arend Lammertink, which you have to add to your sources lists first:

Code: Select all

sudo su
echo "deb http://beagle.tuks.nl/debian jessie/" > /etc/apt/sources.list.d/tuks.list
echo "deb-src http://beagle.tuks.nl/debian jessie/" >> /etc/apt/sources.list.d/tuks.list
wget -qO - http://beagle.tuks.nl/debian/pubring.gpg | sudo apt-key add -
apt-get update
apt-get install libpruio-bas libpruio-lkm libpruio-doc
adduser <YourUserID> pruio
rm /boot/dtbs/`uname -r`/am335x-boneblack-uboot-univ.dtb
reboot
Afterwards you should add your user name (<YourUserID>) to the new user group 'pruio' (to allow pinmuxing without root privilegues), remove the cape-universal trigger file (to avoid interferences with the standard pinmuxing system), and reboot (to get ride of the cape-universal stuff). (Note: this is for kernel 4.14! This stuff changed in each 4.x kernel version. On other kernel versions it may need other operations to get ride of the cape-universal stuff.)

I install and use the libpruio-doc package on my PC, where I edit the source.
Dinosaur wrote:2: You stated in early posts that you use Ubuntu.
I will install Ubuntu full desktop on a 32Gb uSD and use Poseidon as the editor for FB.
Do you see any problems in this configuration ?
I've no experience with this configuration, but it sounds reasonable. From my point of view the downside is that you'll need keyboard, mouse and screen for the BBB.

I use Xubuntu on the PC. On the BBB I usually install console images (no graphical desktop). Never tried Poseidon. (Is there a reason why I should?) The BBBs are connected by LAN/WLAN to the PC and get controlled over ssh (using one PC screen/keyboard for all BBBs).
Dinosaur wrote:3: Are there any other Files/libraries to install ?
No, not for the controller part. But you may need ie. GTK for your GUI stuff.
Post Reply