Lazarus

Programming => General => Topic started by: sfischer on May 25, 2013, 05:48:17 pm

Title: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on May 25, 2013, 05:48:17 pm
Hi,

below you can find the source code of unit rpi_hal on my web shop. You can use the code without the piggy back board, so there is no need to buy the board, but I whould apreciate it.

The rpi_hal unit delivers functions and procedures to access rpi's HW: SPI, I2C and GPIO. Hope you can use it in your rpi projects. Have a lot of fun and pls. discuss code enhancements and report bugs here in this forum.

http://shop.basis.biz/shop/Raspberry-PI/piggy-back-board/

Thanks,
Stefan
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: jwdietrich on May 26, 2013, 09:39:43 am
That is intereseting, but where is the link to your source code?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: pridi on May 26, 2013, 09:49:01 pm
Hi!! I'm very interested in your unit....may I have the link???

Thank you!!
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on May 27, 2013, 09:51:36 am
The SW can be found here:
http://shop.basis.biz/shop/Raspberry-PI/piggy-back-board/
under the point SW: SW e.g. rpi_hal:
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on July 26, 2013, 06:08:35 pm
Hi,

new version of rpi_hal is available. Now it supports the PiFace Board.

Stefan
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: angel on July 30, 2013, 06:15:52 pm
This is very interesting.
My only problem is: I'm an electronics technician, and I have limited programming knowledge.
Is there any tutorial on how to use the HAL? I can follow part of the code...
What I would like is how to do a simple data := read(address, register) and write(address, register, data) just byte by byte.
And if I need to open/close the I2C bus...
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on July 31, 2013, 01:44:46 pm
I have added an i2test routine in rpi_hal to show you the usage of the i2c routines.
pls. download newest rpi_hal (Version 1.3). the i2c_test reads and writes to an rtc 3232m clock
chip. If you have a different chip hooked up to the i2c bus, then the code give you just an idea how to use them:

i2c_string_write($68,$05,#$08+#$12,NO_TEST); // write 08 in reg 0x05 and 12 in reg 0x06
s:=i2c_string_read($68,$05,2,NO_TEST); showstr(s); // read 2 bytes. startreg is 0x05


program i2ctest;
uses rpi_hal;  { minimum version V1.3 required }
begin
  writeln('program start');
  i2c_test;
  writeln('program end');
end.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: hallenberger on July 31, 2013, 02:27:23 pm
Hello!

I can't compile the unit, because of a variable name named "result".

How do you compile it?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on July 31, 2013, 02:30:05 pm
just install lazarus on rpi:

root@rpi> apt-get install lazarus

copy rpi_hal.pas and i2ctest.pas in a directory and compile it.

root@rpi>  fpc i2ctest

 
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on July 31, 2013, 02:34:01 pm
in which line do you get the error with 'result'

can you paste the compiler messages pls.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: hallenberger on July 31, 2013, 02:46:31 pm
Hi!

For example here:

function  BitSlice32(data:longword; bitstartpos,slicelgt:byte) : longword; 
{.c returns Bit slice area (Bit Nr. from  1 to 32)
i.e. data(bin) = 00101100; startpos=6; slicelgt=4; result:= 00001011 }
var result,msk : longword; i:longint;
begin
  result := 0; msk:=0;
  for i:= bitstartpos downto (bitstartpos-slicelgt+1) do msk := msk or Bit_Set_OR_Mask32(i);
  result:= data and msk;
  result:=result shr (bitstartpos-slicelgt);
  BitSlice32 := result;
end;

Tho compiler message is:

rpi_hal.pas(565,5) Error: Duplicate identifier "RESULT"


And here:

function  rpi_mmap_reg_write(adr,value: longword) : longword;
var result:longword;
begin

rpi_hal.pas(629,5) Error: Duplicate identifier "RESULT"
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on July 31, 2013, 03:10:28 pm
pls. add {$MODE FPC} in the first line of rpi_hal.pas

or compile it as follows:

fpc -MFPC rpi_hal
 
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: hallenberger on July 31, 2013, 05:05:49 pm
Thank you!

Now it compiles.

But another question:
If I make a new project (only one form) an add rpi_hal.pas in the uses clause, compile and run the project I get an exception (SIGSEGV) when I close the program.
Without rpi_hal in the uses the program works.

What am I doin wrong?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on August 01, 2013, 01:08:57 pm
Hi,

i'm not using Lazarus itself, only fpc on commanline.
just compile your program, which is using rpi_hal with uses clause, with fpc.

What OS are you using ?
I've tested rpi_hal V1.3 only on wheezy:
Linux rpi 3.6.11+ #474 PREEMPT Thu Jun 13 17:14:42 BST 2013 armv6l GNU/Linux
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: bartdereu on August 10, 2013, 09:22:13 pm
Hi,

i tried to use this library in Lazarus, but i can't get it compiled without errors (211)
Anyone has a piece of code that works with this in Lazarus,

btw, GREAT work !
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: bartdereu on August 10, 2013, 10:05:19 pm
Thank you!

Now it compiles.

But another question:
If I make a new project (only one form) an add rpi_hal.pas in the uses clause, compile and run the project I get an exception (SIGSEGV) when I close the program.
Without rpi_hal in the uses the program works.

What am I doin wrong?

I have the same problem, but a runtime error 211 . The test program for the command line test (./testrpi ) works fine, so the hardware and other things should be installed fine.

But i really need to get this PiFace running under Lazarus, and so far i'm unable to do so. Anyone has had more luck then me ?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on August 11, 2013, 06:36:51 pm
Hi Bart,

I never tried Lazarus. next week I'm very busy. Maybe we can do a remote session on Monday 19.Aug or Tuesday.
Stefan
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: bartdereu on August 12, 2013, 04:16:43 pm
Ok, no problem .  i got a little futher as you can read in this post :

http://forum.lazarus.freepascal.org/index.php/topic,17404.msg127797.html#msg127797

only thing left is to be able to read the inputs wit the fpsystem function. i can't get it to work !  >:D
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: justtt on October 24, 2013, 09:02:43 am
Hi all ... :)

my english is not the best, but i hope you will understand me ... ;-)

firstly thank you sFischer for your work ...
i got a running Raspi with Lazarus/Freepascal and your rpi_hal to read the Ports of the Piface in a thread to update (100ms) my Gui.
It works really fine, i needed a while to understand all the things about and around the Raspi (never worked with Linux before and never bind Units from other programmers in Lazarus) ... but it's allmost done ...

Now it's a point where i need more Inputs to my Pi ... i read about Pi Rack and the ability to add PiFaces up to 4 to ONE PI ...
My Problem is, how can i get to communication to an other PiFace, means to the SPI address 1 (default is 0; Jp1(0);Jp2(0))!?
any ideas for me? is it possible with your unit rpi_hal?

Thanks a lot for all help.

Greetz Tom / justtt
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on October 24, 2013, 11:57:22 am
Hi,

pls. download updated version of rpi_hal (V1.9) from following link. I have added the device address (devadr) to all pfio_x functions. devadr is the MCP23S17SP address, determined by the A2-A0 pin settings (from 0-3).
So you can address multiple PiFace boards. But I don't have tested the code. Pls. test the code with different settings of A2-A0 and report your testresults.

Stefan

The SW can be found here:
http://shop.basis.biz/shop/Raspberry-PI/piggy-back-board/
under the point SW: SW e.g. rpi_hal:
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: justtt on October 24, 2013, 12:57:17 pm
WoW brilliant sfischer ;-)

I've tested 0-3, all done fine ... So everybody who wants a Pi with 32 Inputs and 32 Outs can use it :-)

I'm very happy about it. Thank you very much.

I have no PiRack till now, but i will improvise for now to test simultan using of two PiFaces.
I will edit this here later.

Thank you
- Tom | justtt
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on October 24, 2013, 07:04:01 pm
Hi,

just uploaded a new Version of rpi_hal (V1.10).
I have added some BitBang-functionality.
So you can Switch-On/OFF PowerSwitches (e.g. ELRO, Intertechno).
Prerequisite is, that you have a connected 433.92MHz Transmitter on a GPIO-Pin to the RPI. Also the Morse-Alphabet is implemented. That enables you, to Morse with a e.g. LED which is connected to a GPIO-Pin. Any other Device, that is OOK (OnOffKeyed) can be used.

BB_SetPin(rpi_status_led_GPIO); // set the GPIO-Pin to Rpi Status LED
Morse_tx('This is a Morse Test'); 

Stefan

The SW can be found here:
http://shop.basis.biz/shop/Raspberry-PI/piggy-back-board/
under the point SW: SW e.g. rpi_hal:
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: justtt on October 28, 2013, 08:51:26 am
So I've tested the abbility to usw more then one PiFace in my source code ...

It works really nice. The PiFaces dont need to be initialized with pfio_init.
[EDIT 15.11.2013 Only dont need to be initialized when using address 0 ... all other need to be initialized]

@bartdereu ... the best way is to "synchronize" the Gui by using a external Thread and bind it by synchronize ... dont forget a delaytime by using it ;-) ... It's like to "speak" with a serial port ...

@stefan ... thanks a lot ... really really thank you much :-)
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on June 11, 2014, 01:27:25 pm
Hi,
the software has been moved to:

http://shop.basis.biz/shop/Raspberry-PI/piggy-back-board/index.html
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Tibo on December 02, 2014, 01:48:43 pm
Hi, and thanks for your great work on RPI HAL!

I'm new to the RPI world.  I'm interested in i2c communication.

Your last posted URL leads to rpi hal version 1.1:
http://shop.basis.biz/shop/Raspberry-PI/piggy-back-board/index.html

Since I'm interested in i2c communication, I'm looking for version 1.3 (or higher if it exists ;-)
Could you please share a link to version 1.3

Thanks! 
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on December 06, 2014, 05:51:32 pm
Hi,

just uploaded new version 3.1 of rpi_hal.
You can find it here: http://shop.basis.biz/shop/Raspberry-PI/piggy-back-board/index.html
scroll down until you see rpi_sw.tgz and download the archive.

New function:
Added a bidirectional device like /dev/mydevice
you can read and write to it.
No kernel modifications necessary. Just pure user space.

Stefan
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: richard_rtech on February 12, 2015, 12:58:47 pm
Have not had time to look into it but with Laz 1.2.4 on RPI2 even adding RPI_HAL in the uses clause of an app causes a SIGABRT. If I get ten mins I'll try with the older board and see if the same happens. There is no code using RPI_HAL at this point and I havd DCPCrypt, Sockets (my rebuilt ver) and SynaSer installed. :( Looks like I'm writing my own I2C code...
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: szlbz on March 12, 2015, 08:59:26 am
Hi,

just uploaded new version 3.1 of rpi_hal.
You can find it here: http://shop.basis.biz/shop/Raspberry-PI/piggy-back-board/index.html
scroll down until you see rpi_sw.tgz and download the archive.

New function:
Added a bidirectional device like /dev/mydevice
you can read and write to it.
No kernel modifications necessary. Just pure user space.

Stefan

Hello, I can not download the Rpi HAL file, you can EMAIL to me? My EMAIL is szlbz@sina.com, thank you.

Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: ghamm on March 17, 2015, 04:59:02 am
Were you able to get this to work? Im anxious to get this to work on the PI 2.. Just getting started..

Have not had time to look into it but with Laz 1.2.4 on RPI2 even adding RPI_HAL in the uses clause of an app causes a SIGABRT. If I get ten mins I'll try with the older board and see if the same happens. There is no code using RPI_HAL at this point and I havd DCPCrypt, Sockets (my rebuilt ver) and SynaSer installed. :( Looks like I'm writing my own I2C code...
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: epielettronica on April 03, 2015, 07:39:09 pm
to run with pi2 you must:
change line
  BCM2708_PBASE_pag    = $20000; { Peripheral Base in pages }
in
  BCM2708_PBASE_pag    = $3F000; { Peripheral Base in pages }
and change in function  rpi_mmap_get_info (modus:longint)  : longword;
the lines:
     7   : if ((rpi_mmap_get_info(5)=1) and
               (Upper(rpi_hw)='BCM2708')) then valu:=1;                     (* runs on known rpi HW *)
in:
     7   : valu:=1;                                                         (* runs on known rpi HW *)
rebuild and good job!
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Yoyo07 on April 21, 2015, 07:18:16 pm
Hi,
I use pi2 and I changed the lines like previous and the compile is ok but :
RunError(211)...
What I forgot to change ?
Thanks
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: pascalbythree on August 02, 2015, 08:19:26 pm
Hello coders.

Is het possible to run:

unit rpi_hal; { V3.1 }

on the lastest version of of Raspbian Wheezy
   2015-05-05-raspbian-wheezy.zip   07-May-2015 10:54    1.0G
RPI_HAL.PAS does compile with FPC on the command line.
But testrpi.pas does not compile or give any system info by the code below here.

booting: Linux raspberrypi 3.18.11+

  program testrpi;
  uses rpi_hal;
  const piggyback=1; piface=2; board_installed=0; //board_installed=piggyback;
  begin
    writeln('Show CPU-Info, RPI-HW-Info and Registers:');
    rpi_show_all_info;


or do i need a version below

//Simple Test program, which is using rpi_hal. compile it on rpi with '#fpc testrpi.pas' and run it '#./testrpi'
//Tested on Raspbian Wheezy. Distro was occidentalis.
//#uname -a
//#Linux raspberrypi 3.6.11+ #399 PREEMPT Sun Mar 24 19:22:58 GMT 2013 armv6l

Named:

[DIR]   2013-02-09-wheezy-raspbian/   09-Feb-2013 10:35    

or it boots with

   raspbian-2015-05-07/   07-May-2015

The lastest version, that i am now booting.

Greets Wouter van Wegen

woutervanwegen@mijnplinq.nl



Or is there a difference between fpc command line and compiling it with Lazarus on X-Windows?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on August 03, 2015, 10:50:23 am
Hi,
I use pi2 and I changed the lines like previous and the compile is ok but :
RunError(211)...
What I forgot to change ?
Thanks

That's an abstract error: your code ends up in a method that has no implementation yet.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: katonacs73 on August 04, 2015, 09:50:42 am
Hi,
I use pi2 and I changed the lines like previous and the compile is ok but :
RunError(211)...
What I forgot to change ?
Thanks

I had the same problem, but I found a solution:
modify the Lazarus IDE compiler options:

Project -> Project Options ... -> Compiler Options -> Other

then add:
-dUseCThreads     
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on August 31, 2015, 11:09:06 am
source of rpi_hal has been moved to github:

https://github.com/rudiratlos/rpi_hal

I will release a new version soon.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: tigerA15 on November 28, 2015, 01:26:51 pm
I try your source , it's NOT worked.

root@raspberrypi:/home/pi# uname -a
Linux raspberrypi 4.1.7-v7+ #817 SMP PREEMPT Sat Sep 19 15:32:00 BST 2015 armv7l GNU/Linux

Hardware   : BCM2709


 Raspberry Pi 2 B
source:
Code: Pascal  [Select][+][-]
  1. program testrpi;
  2.  
  3. {$mode objfpc}{$H+}
  4. uses
  5.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  6.   cthreads,
  7.   {$ENDIF}{$ENDIF} sysutils, rpi_hal;
  8.  
  9. const
  10.   piggyback = 1;
  11.   piface = 2;
  12.   board_installed = piggyback;//board_installed=piggyback;
  13. var isr:isr_t;
  14. begin
  15.   writeln('Show CPU-Info, RPI-HW-Info and Registers:');
  16.   rpi_show_all_info;
  17.   isr.gpio:=16;
  18.   isr.rising_edge:=true;
  19.   gpio_set_int (isr, 16,@my_isr,true);
  20.   gpio_int_enable(isr);
  21.   gpio_set_pin(16,true);
  22.   delay_msec(200);
  23.   gpio_set_pin(16,false);
  24.  
  25.   gpio_int_disable(isr);
  26.   gpio_int_release(isr);
  27. end.      

===========

but I use shell script & command line , that's worked.

/tmp/gpio_int_setup.sh 16 out rising
echo "1">/sys/class/gpio/gpio16/value
echo "O">/sys/class/gpio/gpio16/value
/tmp/gpio_int_setup.sh 16 stop

what's the matter ?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on November 28, 2015, 04:23:07 pm
NEW Version V3.4 of RPI_HAL is available:

Additional support for RPI2B and Pi Zero

If you use Lazarus, then modify the Lazarus IDE compiler options:
Project -> Project Options ... -> Compiler Options -> Other and add: -dUseCThreads   

Download location:
https://github.com/rudiratlos/rpi-hal

functions:
- I2C
- SPI
- GPIO (input, output, SW-PWM, HW-PWM)
- Bitbang functions for Powerswitches (ELRO, Intertechno, Sartano, Nexa)
- Morse functions
- Rotational Encoders implemented with Threads (e.g. Keyes KY-040 Rotary Encoder)
- Servo functions
- PID Algorithmus
- functions to access PiFace Board
- Bidirectional serial device access in User space /dev/yourdevice
- USB Reset and Access
...
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on November 28, 2015, 04:28:33 pm
I try your source , it's NOT worked.

root@raspberrypi:/home/pi# uname -a
Linux raspberrypi 4.1.7-v7+ #817 SMP PREEMPT Sat Sep 19 15:32:00 BST 2015 armv7l GNU/Linux

Hardware   : BCM2709


 Raspberry Pi 2 B
source:
Code: Pascal  [Select][+][-]
  1. program testrpi;
  2.  
  3. {$mode objfpc}{$H+}
  4. uses
  5.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  6.   cthreads,
  7.   {$ENDIF}{$ENDIF} sysutils, rpi_hal;
  8.  
  9. const
  10.   piggyback = 1;
  11.   piface = 2;
  12.   board_installed = piggyback;//board_installed=piggyback;
  13. var isr:isr_t;
  14. begin
  15.   writeln('Show CPU-Info, RPI-HW-Info and Registers:');
  16.   rpi_show_all_info;
  17.   isr.gpio:=16;
  18.   isr.rising_edge:=true;
  19.   gpio_set_int (isr, 16,@my_isr,true);
  20.   gpio_int_enable(isr);
  21.   gpio_set_pin(16,true);
  22.   delay_msec(200);
  23.   gpio_set_pin(16,false);
  24.  
  25.   gpio_int_disable(isr);
  26.   gpio_int_release(isr);
  27. end.      

===========

but I use shell script & command line , that's worked.

/tmp/gpio_int_setup.sh 16 out rising
echo "1">/sys/class/gpio/gpio16/value
echo "O">/sys/class/gpio/gpio16/value
/tmp/gpio_int_setup.sh 16 stop

what's the matter ?

pls. use new rpi_hal V3.4, it supports the RPI2B Board
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: tigerA15 on November 29, 2015, 01:02:30 pm
I try your source , it's NOT worked.

root@raspberrypi:/home/pi# uname -a
Linux raspberrypi 4.1.7-v7+ #817 SMP PREEMPT Sat Sep 19 15:32:00 BST 2015 armv7l GNU/Linux

Hardware   : BCM2709


 Raspberry Pi 2 B
source:
Code: Pascal  [Select][+][-]
  1. program testrpi;
  2.  
  3. {$mode objfpc}{$H+}
  4. uses
  5.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  6.   cthreads,
  7.   {$ENDIF}{$ENDIF} sysutils, rpi_hal;
  8.  
  9. const
  10.   piggyback = 1;
  11.   piface = 2;
  12.   board_installed = piggyback;//board_installed=piggyback;
  13. var isr:isr_t;
  14. begin
  15.   writeln('Show CPU-Info, RPI-HW-Info and Registers:');
  16.   rpi_show_all_info;
  17.   isr.gpio:=16;
  18.   isr.rising_edge:=true;
  19.   gpio_set_int (isr, 16,@my_isr,true);
  20.   gpio_int_enable(isr);
  21.   gpio_set_pin(16,true);
  22.   delay_msec(200);
  23.   gpio_set_pin(16,false);
  24.  
  25.   gpio_int_disable(isr);
  26.   gpio_int_release(isr);
  27. end.      

===========

but I use shell script & command line , that's worked.

/tmp/gpio_int_setup.sh 16 out rising
echo "1">/sys/class/gpio/gpio16/value
echo "O">/sys/class/gpio/gpio16/value
/tmp/gpio_int_setup.sh 16 stop

what's the matter ?

pls. use new rpi_hal V3.4, it supports the RPI2B Board

ItÅ› worked! thatÅ› greate! Thank you very much!
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: tigerA15 on December 04, 2015, 08:31:58 am
How to achieve 'soft pwm' function by rpi_hal library ? Can you show us the sample code ? :)
Thanks.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on December 14, 2015, 05:05:10 pm
How to achieve 'soft pwm' function by rpi_hal library ? Can you show us the sample code ? :)
Thanks.

pls. see procedure GPIO_PWM_Test in rpi_hal. It shows the usages of HW and SWM PWM
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: alex1991 on February 02, 2016, 07:10:16 am
Hello!

Is it possible connect the ADC AD7866 to the  Raspberry Pi B+?
What procedure or function should be used to obtain information from the ADC?
What are the input parameters?

ADC AD7866
SCLK 20MHz, 1MSPS.
The data stream consists of one leading zero followed by three STATUS bits, followed by the 12 bits of conversion data.

P.S. SPI, of course.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on February 02, 2016, 10:17:57 am
Looks like, SPI BUS. But you can only connect one Output to SPI (DoutA or DoutB).

e.g.
rpi     to ADC

MISO to DOUTA
SCLK to SCLK
/SS   to /CS
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on February 02, 2016, 10:30:20 am
Looks like, that you can connect the ADC A0 line to an GPIO, for selecting DOUTA or DOUTB.

Connect DOUTA and DOUTB to rpi's MISO line
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: alex1991 on February 02, 2016, 10:47:16 am
Yes, of course. Thanks for the answer!

Can you show an example of using this function (SPI BUS)?

Activate SPI channel, obtaining first measurement, next measurement, ...... , last measurement, Close SPI Channel.
What type of data (byte/integer/string/other)?

How to change / specify the frequency SCLK?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: alex1991 on February 12, 2016, 12:31:42 pm
The data stream consists of one leading zero followed by three STATUS bits, followed by the 12 bits of conversion data.

https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md
SUPPORTED BITS PER WORD
8 - Normal
9 - This is supported using LoSSI mode.

Can I obtain 12 bits data from ADC, if 8-9 bits per word supported?
And output data type - word(2 byte-(0..65535))?


Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on February 12, 2016, 01:47:18 pm
The data stream consists of one leading zero followed by three STATUS bits, followed by the 12 bits of conversion data.

https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md
SUPPORTED BITS PER WORD
8 - Normal
9 - This is supported using LoSSI mode.

Can I obtain 12 bits data from ADC, if 8-9 bits per word supported?
And output data type - word(2 byte-(0..65535))?

Mask.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: alex1991 on February 12, 2016, 02:49:08 pm
The data stream consists of one leading zero followed by three STATUS bits, followed by the 12 bits of conversion data.

https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md
SUPPORTED BITS PER WORD
8 - Normal
9 - This is supported using LoSSI mode.

Can I obtain 12 bits data from ADC, if 8-9 bits per word supported?
And output data type - word(2 byte-(0..65535))?

Mask.

Can you show an example of using this function (SPI BUS)?

Activate SPI channel, obtaining first measurement, next measurement, ...... , last measurement, Close SPI Channel.
What type of data (byte/integer/string/other)?

How to change / specify the frequency SCLK?

Can you write example code for newbie?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Koenraad on March 09, 2016, 02:29:49 pm
Hi,
I'm trying to use rpi_hal in a lazarus-project.
I'm experiencing strange things.
It's on a Pi2, with lazarus 1.6rc1. Enclosed the used project.
It's very basic, rpi_hal is not really used, but included in the "uses". It runs OK, but when I terminate it (close button), I get a number of errors (when run from the command-line). When you comment out the rpi_hal from the uses, the application runs fine.
What's happening ?

A second strange thing : add a togglebutton, or a checkbox and the application crashes when it starts.
Any help ?
Is there a lazarus-demo project ?
The fpc-demo works fine.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Koenraad on March 10, 2016, 11:42:00 am
Is anyone really using rpi_hal ? With lazarus ?
I was trying to debug the problems I have and enabled debug-mode of the compiler. Then the program crashes right from the start with range-check errors. Looking into this I found some strange bugs : a number of arrays used out of bounds.
I'm really puzzled !
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: DonAlfredo on March 10, 2016, 01:09:59 pm
I would definitely suggest to use the Aspire library !

http://forum.lazarus.freepascal.org/index.php/topic,29852.0.html

https://sourceforge.net/projects/asphyre/

It is very well written, and in use by me with great success !
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: mig-31 on March 10, 2016, 02:54:29 pm
I use low-level native Freepascal code to program ARM Linux development boards.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Koenraad on March 10, 2016, 03:31:32 pm
@DonAlfredo
I hesitated because of the size of the library. Now I tried : problems !
I compiled Blinky, and I have to be root to run it. I hate running applications as root.
Then I enabled the define for RPi2. There I got errors : some assembler instructions are unknown by fpc 3.0. so disabled those instructions (enabled the rpi1 ones by modifying the "ifdef"). Still only works as root.
I also modified /dev/mem in /dev/gpiomem, but still I can't run as normal user but now the application hangs. I need to "kill" it. Running as root behaves the same.

@mig-31
What is "low-level native Freepascal" ?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: DonAlfredo on March 10, 2016, 04:27:04 pm
Well, you need an FPC that is compiled for ARMV7A to get this library running for RPi2, which has an ARMV7 cpu !
Only then, the dmb assembler instruction is accepted.

(you can add: {$ifdef CPUARM_HAS_DMB})

I do not think that your program has to run as root.
Perhaps you have to allow access to your hardware for non-root ?

When accessing USB (on RPi), I always have to add an udev-rule to get access for non-root !!

Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on August 04, 2016, 06:34:23 pm
Hi,
just uploaded new version of rpi-hal V4.2 to github: https://github.com/rudiratlos/rpi-hal
I have corrected several bugs and enhanced the library with new features, like reading config files...
I've testet rpi-hal in a pure free pascal environment (fpc 2.6.4).
Would be nice to get information from users, which want to use rpi-hal in lazarus environment.
 
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on August 04, 2016, 07:25:38 pm
Will try it out this weekend. report back.
Good work!

And yes people are using it! I just didn't use it yet on RPi3, but now i have a setup that chains all the old ones into one "Powerful" RPi network computing system with some Arduino's on all of them (and some proprietary sh*t that I will share if it does what I mean.)
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: nicolap on August 11, 2016, 04:08:28 pm
@sfischer
I thought a lot before posting this. Please note that my english is very bad so I'm sinthetic, not rude at all.
I think that you have done a big job, really.
But your code is a collection of "bad practices".
I will try to explain some, because I would try to help you to develop better.

All code snippets come from version 4.2.

Code: Pascal  [Select][+][-]
  1. function  I2C_word_write(baseadr,basereg:word; data:word; flip:boolean; errhdl:integer):integer;
  2. begin I2C_word_write:=I2C_word_write(RPI_I2C_busgen,baseadr,basereg,data,flip,errhdl); end;
This code is difficult to read: after punctuation (comma, semicolon, etc) put a space!
And let the begin and end alone in a line.
The "Overload" modifier is not required but I think that is a very good habit to use it http://www.freepascal.org/docs-html/ref/refsu80.html

Code: Pascal  [Select][+][-]
  1. procedure SPI_show_buffer(busnum,devnum:byte);
  2. const errlvl=LOG_WARNING; maxshowbuf=35;
  3. var i,eidx:longint; sh:string;
  4. begin
  5.  
Ouch! It's ugly, compare to this:
Code: Pascal  [Select][+][-]
  1. procedure SPI_show_buffer(busnum, devnum:byte);
  2. const
  3.   errlvl = LOG_WARNING;
  4.   maxshowbuf = 35;
  5.  
  6. var
  7.   i, eidx : longint;
  8.   sh : string;
  9.  
  10. begin
  11.  
The readability is much improved, for other people but also for you when will read the code after some time.
Learn to use "Const parameters" http://www.freepascal.org/docs-html/ref/refsu68.html

Don't mix "library code" with "test code": the GPIO_int_test or I2C_test functions MUST be in a separate file.

Learn to use objects: it's a different world :-) (after the first headache)

Code: Pascal  [Select][+][-]
  1.   with spi_buf[busnum,devnum] do
If the parameters are outside the range (that you know but the compiler no) you will have a runtime error at best. Or, if you shut off the range check errors, a "strange" behaviour.
Use Enumerations!

Last, for now: the unit initialization code decides which peripherals to initialize and which is the default log level. But these decisions are duty of the user/programmer not yours. Better if you have an RPI_hal_Initialize(...) function that do these things and return an error code (it's not your decision when to halt the program and with which error code).

Remember an important thing (that a C programmer ignores  >:D ): a programming language is a COMMON language between YOU and your computer so it must be (easily) readable from BOTH.
     NicolaP
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: nicolap on August 11, 2016, 04:10:09 pm
Small modify required in procedure Get_CPU_INFO_Init
You must add a case for value $222082
This is returned by RPi 3B produced in China http://www.raspberrypi-spy.co.uk/2012/09/checking-your-raspberry-pi-board-version/
Thanks
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on September 09, 2016, 02:59:26 pm
Hi nicolap,
thank you very much for your feedback. I appricate it, because I want to improve rpi_hal. I encourage all of the users to supply constructive feedback for further improvement/enhancements. I will modify the code in the next release, to avoid runtime errors (with spi_buf[busnum,devnum]..) if busnum/devnum are out of range.
I understand your feedback which addresses the readability. In general, you're right that the code could be more prettified. But I've choosen that style for several codesections, where the functionality of the code is obvious (like Num2Str, Str2Num...). E.g. your 1st example (i2c_word_write), this function is an overloaded function and differs only in the parameter line. It's my old way, to organize the code. I seperate code which is 'easy/obvious' to understand with compaction from 'complexer' code, which is more styled in your sense. I'm considering your suggestion, to seperate test-/example-code from the library, to bring down line of code. I already put the code for the pfio card in a seperate unit. But I can not say when/if it will happen. Feedback from other users and their experiance whould be very valuable for me. I really don't know how many people are using rpi_hal and I don't know if it's worth to spend more time, I dont get so much feedback. I'm using rpi_hal in a lot of my free pascal projects with rpi hw and I'm very happy with the stability and huge functionality until know.

best regards,
SF

 
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: ThomasE on November 20, 2016, 10:07:58 am
First of all: the rpi_hal-unit seems to be an excellent tool !
I am running lazarus (1.2.4) with fpc (2.6.4) on a Raspberry PI B+.

I installed rpi_hal without problem.
Compiling my test program without problem.

Running the program and nothing happens !

My test program starts with the code:

unit unit1;
{$mode objfpc}{$H+}

interface

uses
rpi_hal, Unix...........StdControls;


At the moment I am not using any functions or procedures from the rpi_hal-unit, just to be able to see if the unit is working ok or not.
If I take away rpi_hal under Uses, the program starts.

I really can not figure out where the problem is !

Anyone have a clue ?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on November 20, 2016, 10:37:40 am
Yup. The program must run as root or you must configure the program rights such that is allowed access to the low-level hardware (add group with rights, add program name as user to that group)
The latter is always the better solution, but takes more effort.
I always get "grumpy mode on  >:D " when I read that a program *must* run as root, by the way, and reluctant to advice it. (Yes I have grumpy mode on from my own answer, can't help it)
That is almost never the case, must run as root...
It is simply a matter of proper unix system management.

For an average Pi user, however, and to test your software, feel free to run as root ;)
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: ThomasE on November 20, 2016, 11:38:21 am
Brilliant !

I hope I have understood what you propose me to do.
Will try it later to day and come back with the result.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on November 20, 2016, 01:14:41 pm
Let us know if your program indeed runs as root, so
Code: Pascal  [Select][+][-]
  1. sudo ./yourprogram

I know it probably does, because I use that unit myself.

I will try to add a wiki subject on the matter on how to avoid root later. If it is not already there somewhere.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: krolikbest on November 27, 2016, 03:33:29 pm
I'm using Lazarus 1.2.4, Raspbian Jessie, RPi3
Although rpi_hal works excellent, still I'm having one problem-closing program get SIGSEGV. Without rpi_hal everything is ok. Tried to catch it in try except, but no lucky.

Martin
Title: New rpi_hal uploaded to github
Post by: sfischer on December 22, 2016, 02:09:22 pm
Hello rpi_hal users,

just uploaded a new version (V4.3) of rpi_hal to https://github.com/rudiratlos/rpi-hal

I've added some new functions to V4.3 like:

SW Maintenance-/Service-functions:
- Upload Logfiles to FTP-Server
- Download new SW from FTP-Server
- Install new SW on rpi

best wishes,
Stefan
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: hansotten on December 22, 2016, 04:06:45 pm
Thanks!

A welcome Xmas present!
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: hansotten on December 22, 2016, 04:20:07 pm
I'm using Lazarus 1.2.4, Raspbian Jessie, RPi3
Although rpi_hal works excellent, still I'm having one problem-closing program get SIGSEGV. Without rpi_hal everything is ok. Tried to catch it in try except, but no lucky.

Martin
That is quite an old version of Lazarus. Time to upgrade!
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: rainer on January 10, 2017, 09:13:40 am
I am not sure if I am asking here at the right place, so I try. Excuse my long message, I want to give you as many details as possible.

I want to use the cheaper "NanoPI M1" instead of an Raspberry Pi for controlling hardware, robotics, ..., where a Microcontroller (even a powerful PIC18F67J60) is insufficient. This NanoPI M1 uses an "Allwinner H3" chip, a very powerful 4-core 32-Bit model.
As the only working Linux I found "DietPi" (I would love to use openSUSE, but there is no working one). I was able to install DietPi and also to install FreePascal 2.6.4 and started testing:

"Hello World"  is working, so the compiler is basically fine - but this is not want I finally want to do.  ::)

So I started to play with GPIO, but here I have extreme troubles.
Currently a simple LED is connected between Pins 6 (GND) and 12 (GPIO18), of course with a resistor in series, and as first test I simply wanted to control it from FreePascal. But until now I found absolutely no way to get it ON. Not from bash, and also not from FPC.

I downloaded the entire rpi_hal and the testing program from here (https://github.com/rudiratlos/rpi-hal), compiled it, and gave it a try. The test program crashes with an "Access Violation" after showing CPU details.

This is what I get (with already activated debug info):
...
CLK max  :  500.00 MHz
PWMHW min:    1.00 Hz
PWMHW max:   18.75 kHz

GPIOBase    : 20200000
Adr(1F-00)  : FEDC BA98 7654 3210 FEDC BA98 7654 3210
An unhandled exception occurred at $00054088 :
EAccessViolation : Access violation
  $00054088  BCM_GETREG,  line 2968 of rpi_hal.pp
  $00056E3C  SHOW_REG,  line 3416 of rpi_hal.pp
  $00057258  SHOW_REGS,  line 3435 of rpi_hal.pp
  $000573AC  SHOW_REGS,  line 3440 of rpi_hal.pp
  $000573FC  GPIO_SHOW_REGS,  line 3442 of rpi_hal.pp
  $0006C22C  RPI_SHOW_ALL_INFO,  line 6289 of rpi_hal.pp
  $00010734  main,  line 18 of testrpi.pp

2017-01-10 08:55:31 ERR pfio_exit: Exitcode: 217
2017-01-10 08:55:31 ERR RPI_hal_exit: Exitcode: 217


It seems that there is an incompatibility to the "Allwinner H3 CPU inside the NanoPI M1".  :'(

Is there a way to get this working?

I am no specialist in coding at that deep and hardware-dependent level, so I kindly please for help.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: krolikbest on January 11, 2017, 11:14:22 am
Hi,

could you explain how would you like to use it in robotics, eg. why do you think RPi3 is insufficient?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on January 11, 2017, 12:32:22 pm
GPIOBase    : 20200000 seems wrong to me for that NanoPi.
There's nothing wrong with the CPU otherwise it would not even compile.
Look up the hardware addresses in the documentation for that "NanoPI M1"
If it is sensably designed, you just change the base address.
Also note you may have to test as root... As explained above...

<note to self: I have to order a couple of these cheap ones? (note from wife:but you already have a Pi zero and an expensive subscription) >
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on March 04, 2017, 05:32:36 pm
rpi-hal is currently not working with kernels above #970 (4.9).

Re/install latest supported kernel: #970 SMP Mon Feb 20 19:18:29 GMT 2017 (4.4.50-v7+)

root#> rpi-update 52241088c1da59a359110d39c1875cda56496764   
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on March 07, 2017, 05:15:49 pm
Hi,
just uploaded new rpi-hal V4.4 on github: https://github.com/rudiratlos/rpi-hal
This version supports kernel version 4.9
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on March 07, 2017, 05:51:07 pm
Ah, new toys! like it.
I have some issues, which I will post in a private message. Not that it does not work, but where it can be improved.

Nice job!
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on March 08, 2017, 03:24:57 pm
@Thaddy, feel free to contact me. I left my private email-addr in your personal box.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: jdp on March 08, 2017, 07:42:48 pm
Hi. I tried this and get this error.

Code: Pascal  [Select][+][-]
  1. Xlib: extension "RANDR" missing on display":1.0"
  2. 2017-03-08 14:12:00 ERR I2C start [0x01]: /dev/i2c-1
  3. 2017-03-08 14:12:00 ERR SPI_Dev_Init[0x00/00]: /dev/spidev0.0
  4. 2017-03-08 14:12:00 ERR SPI_Dev_Init[0x00/00]: /dev/spidev0.1
  5. *** Error in './project1': realloc(): invalid pointer: 0x75ca801c ***
  6.  
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on March 09, 2017, 01:24:23 pm
@jdp, pls. try new version V4.5
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: jdp on March 09, 2017, 02:55:13 pm
Hi. I still get the same error
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on March 09, 2017, 03:25:50 pm
if you want to use I2C and SPI, you have to run with user root.

if you run with user pi:
sudo passwd
<enter pwd for root>
su
<login with pwd>
try to run your program
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: jdp on March 09, 2017, 05:36:23 pm
Hi. I just want GPIO and serial # for now.
I ran it as root and get the same. Do you have to enable it in rapy config even if you dont want those protocols
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on March 09, 2017, 05:46:49 pm
pls. download rpi_hal from github again (just uploaded corrected version a few seconds ago) and use the following init sequences in your main program:

if RPI_HW_Start([InitHaltOnError,InitGPIO]) then
begin
  <your code>
end;
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: jdp on March 10, 2017, 07:44:40 am

I only have one line of code in my test app.

Code: Pascal  [Select][+][-]
  1.  edit1.text = rpi_snr;

I still get the invalid pointer error.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on March 11, 2017, 01:34:40 pm
pls. compile it with -B -gl (e.g. fpc -B -gl), run it and post the outpt.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: nyarfa on March 12, 2017, 07:09:57 pm
Hi all,

I'd like to use this with some DS18B20. I'm an absolute beginner so thanks if you help me.


Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: jdp on March 14, 2017, 10:47:09 pm
Where do I set these options ?

pls. compile it with -B -gl (e.g. fpc -B -gl), run it and post the outpt.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sohrab on March 15, 2017, 08:44:12 pm
Hi,
Used V4.5 on my Pi3B
Testrpi worked with fpc.  8-)
Result -->
2017-03-16 00:51:58 ERR I2C_start[0x01]: /dev/i2c-1
2017-03-16 00:51:58 ERR SPI_Dev_Init[0x00/00]: /dev/spidev0.0
2017-03-16 00:51:58 ERR SPI_Dev_Init[0x00/01]: /dev/spidev0.1
Show CPU-Info, RPI-HW-Info and Registers:
rpi Snr  : 0000000019a2908f
rpi HW   : BCM2709
rpi proc : 0
rpi rev  : rev4;1024MB;3B;BCM2709;a02082;40~~~

But with lazarus compiles but throws an error on run -
'Project project1 raised exception class 'External: SIGABRT''
In file '../nptl/sysdeps/unix/sysv/linux/raise.c' at line 56

(its run with sudo)
Thanks


Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: mark on March 18, 2017, 12:20:32 pm
thanks for the lib. It seems to have all i need. :-)
except for 1 thing : documentation or samples.
In the readme i see samples mentioned but i can not find them?
As a beginner i like simple and small samples. I want to use SPI but there are a lot of SPI functions/procedures. What to use, and in which order?

for example SPI_Write
I can understand the busnum and devnum.
But the basereg, i have no clue about that. IMO, when using SPI you simply send data. In the PI that is restricted to 8 bit data.
It depends on the Slave device how many bytes i need to send. So i can not figure out this base address which seems to be combined with the data and send as a word.
Why would that be needed?
Or is the base address something that is not sent out with the SPI data?
Do i get a byte or word sent?

And what about the slave select line, must i do this manual or is it automatic?
Lot of questions for a simple protocol.

As i noticed the burst write, i will likely use that. but here too, what to do to use it.
I imagine:
SPI_StartBurst
SPI_BurstWriteBuffer
SPI_EndBurst

mark

Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on March 18, 2017, 01:37:13 pm
Hi,
Used V4.5 on my Pi3B
Testrpi worked with fpc.  8-)
Result -->
2017-03-16 00:51:58 ERR I2C_start[0x01]: /dev/i2c-1
2017-03-16 00:51:58 ERR SPI_Dev_Init[0x00/00]: /dev/spidev0.0
2017-03-16 00:51:58 ERR SPI_Dev_Init[0x00/01]: /dev/spidev0.1
Show CPU-Info, RPI-HW-Info and Registers:
rpi Snr  : 0000000019a2908f
rpi HW   : BCM2709
rpi proc : 0
rpi rev  : rev4;1024MB;3B;BCM2709;a02082;40~~~

But with lazarus compiles but throws an error on run -
'Project project1 raised exception class 'External: SIGABRT''
In file '../nptl/sysdeps/unix/sysv/linux/raise.c' at line 56

(its run with sudo)
Thanks
Did you enable I2C in Raspi-config?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: mark on March 18, 2017, 04:15:02 pm
still not figured out how to use SPI but i tried some things.
i debugged and tried and modified the spitransfer so it could send more than 64 bytes.
But as always there is a new problem : i can not increase the limit of 4096 bytes.
that is more a PI problem. all the things i found on the net do not work. Anyone got this going or ideas?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on March 18, 2017, 05:23:01 pm
Buffer. There are C examples on the RP forum.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: mark on March 18, 2017, 08:42:46 pm
after struggling for many hours i give up. maybe when this lib/unit is documented i use it again. for now i tried the PXL lib and it worked within minutes.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Handoko on March 19, 2017, 03:21:31 am
Sorry if it is off topic.

I know Raspberry Pi isn't expensive, I want to join the fun. But before I buy one, I wonder what you guys do with your Raspberry Pi?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Cyrax on March 19, 2017, 04:46:53 am
How about making your own smartphone? https://hackaday.io/project/19035-zerophone-a-raspberry-pi-smartphone

Idea is tempting and I have put some thought on it.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on March 19, 2017, 05:39:39 am
- I have used a RPi3 - a a bet - as my main computer for almost 8 months.... And it did not disappoint.
- Build server consists of 4 RPi3's for a total of 16 threads. Builds kernels and FPC-Lazarus
- My daughter (almost 9) uses one for all kinds of stuff and MineCraft.
- My RPi -1 and 2's are used for robotics and home automation and what not.
- I also tried an OrangePi. (Really cheap but NOT recommended: bad user experience, functions as door-opener)
- There must be about 9 in total in the house + one broken. And one camera.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: DonAlfredo on March 19, 2017, 10:44:05 am
50+ in use for rechargeable battery testing: running 24/7 for 2 years !
Arch Linux + FPC/Laz app, cross-compiled from Windows.
Not a single failure with read-only Linux file system.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sky_khan on March 19, 2017, 11:37:38 am
I know Raspberry Pi isn't expensive, I want to join the fun. But before I buy one, I wonder what you guys do with your Raspberry Pi?

I have a RPi2 attached to 2TB external disk. Its my low cost, low power 7/24 personal network server.  File server + scm server + dlna server.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: ertank on March 19, 2017, 01:13:24 pm
The most known advantage of Raspberry Pi is having access to sensors, camera and alike hardware. On the other hand, you can use expensive industrial sensors, too.

I am trying to earn money with Raspberry Pi 3. Working on a software project for a device using Raspberry Pi with sensors. Device is targeted for a health sector which will count wasted material after surgeries and save some information into a database.

Just be informed that sensors Raspberry Pi uses are need to be digital for the best practice. They also need to be at certain voltage levels or else you may burn GPIO port of your Raspberry Pi.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on March 19, 2017, 01:22:42 pm
Name the voltage level: 3.3V.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on March 19, 2017, 02:28:01 pm
@mark,

pls. use SPI_Transfer. The SPI BufferSize is limited to 64Byte (pls. see SPI_BUF_SIZE_c). If you want to transfer more bytes, you have to split the IO into several 64Byte chunks. Your question about BaseReg. This is legacy and it comes from the idea, to address the register within a spi device. e.g write 2Bytes (e.g. 0x1234) to controlregister (0x01). in general it's a 3Byte transfer. SPI_Transfer is more basic and just transfers all at once. e.g SPI_Transfer(<bus>,<adr>,<cmdstring>); e.g SPI_Transfer($00,$00,#$01+#$12+#$34); or SPI_Transfer($00,$00,char($01)+char($12)+char($34)); This will transfer the data 0x011234 to an SPI-Device with ChipEnable0 (HW-Pin CE0) which is connected to SPI-Bus 0. The maximal length of <cmdstring> is SPI_BUF_SIZE_c=64.
If you want to see examples, pls. look for procedures/functions '_test' e.g. SPI_Loop_Test within rpi_hal.

hope this helps,
Stefan
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: enko on July 20, 2017, 06:57:38 pm
Hello, I'm try to understand how to use Raspberry GPIO.
I've installed last release of Lazarus on a Rasperry Pi3 Model B
Create a project with Form + 2 TButton + TMemo
Project -> Project Options ... -> Compiler Options -> Other add: -dUseCThreads 
uses sequence : cthreads, rpi_hal, pfio, ecc
First Button execute this code without errors
  Memo1.Lines.Add('SERIAL NUMBER : ' + rpi_snr);
  Memo1.Lines.Add('HW            : ' + RPI_hw); // BCM2708
  Memo1.Lines.Add('PROCESSOR     : ' + RPI_proc); // ARMv6-compatible processor rev 7 (v6l)
  Memo1.Lines.Add('MIPS          : ' + RPI_mips); // 697.95
  Memo1.Lines.Add('REVISION      : ' + RPI_rev); // rev1;256MB;1000002
  Memo1.Lines.Add('FREQUENCY     : ' + RPI_freq); // 700000;700000;900000;Hz   

Second Button execute this code quitting from the app
 if RPI_HW_Start([InitHaltOnError,InitGPIO]) then
  begin   
  // NOTHING 
  end
  else
  Memo1.Lines.Add('ERROR, something went wrong with RPI_HW_Start');

Can anyone help me ?
I need set some compiling directive ?
I net configure hardware ?
I'm newbie on linux and raspberry

Can anyone provide to me a working example ?
Thanks for all.
Enrico
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: avra on July 21, 2017, 11:32:23 am
Can anyone help me ?
Is it the same if run as root?

You can find more info at http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Thaddy on July 21, 2017, 01:25:59 pm
Can anyone help me ?
Is it the same if run as root?

You can find more info at http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware
No that's not the same as root.. Why is everybody spreading that rumor over and over again.
Linux allows to create a user for a specific piece of hardware or software that requires elevation or that can be accessed and run by or through a normal user.

You don't need root. You need an update on access rights. I hate that. People may start to believe it....

Anyway @ OP Give me some compilable code. I am on an RPi for half a day's time or more.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: enko on July 21, 2017, 03:25:08 pm
Hi, I do not have many skills in the linux world and certain things considered simple are not for me
To solve my problem if I have my application named TEST in  /home/pi/Desktop/TEST path i need input in terminal
sudo chown root /home/pi/Desktop/TEST/TEST
sudo chmod +s /home/pi/Desktop/TEST/TEST

is right, or iI misses something ?
E.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: avra on July 22, 2017, 08:14:05 pm
No that's not the same as root.. Why is everybody spreading that rumor over and over again.
Linux allows to create a user for a specific piece of hardware or software that requires elevation or that can be accessed and run by or through a normal user.
You don't need root. You need an update on access rights.
A little more attention could be appreciated when reading. My question "Is it the same if run as root?" was just to do basic troubleshooting and to determine if executable would run without problems if started as root. If this simple troubleshooting succeeded then, and only then we would discuss what is the best way to do it. And wiki has a lot about proper way of handling this situation.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: polikarp87 on October 05, 2017, 08:49:27 am
Hello all,
first - I'm sorry for my english ;)
I just started to use RPI 3 about two weeks ago. I add rpi_hal to my program in Lazarus and after trying to compile I have still a SIGABRT error (raise.c). Without rpi_hal in uses everything is OK. Somebody could help me with that?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: kris on October 13, 2017, 09:33:40 am
Stefan, I found your unit when I was trying to use pseudo terminals.
I wanted to run 'sudo whoami' using TProcess but sudo checks if it is ran in an interactive terminal or not, so if I just use TProcess normally, the sudo will never print any prompt. I spent lots of time trying to use forkpty like so in my code:

Code: Pascal  [Select][+][-]
  1. //(...)
  2.   function forkpty(__amaster:Plongint; __name:Pchar; __termp:Ptermios; __winp:Pwinsize):longint; cdecl; external 'libutil' name 'forkpty';
  3. //(..)
  4.  
  5.   Procedure TForm1.Button1Click(Sender: TObject);
  6.    var
  7.      stdout,stderr : string;
  8.      WS       : TWinSize;
  9.      ChildPid : Longint;
  10.      term     : termios;
  11.      Fpty     : Longint;
  12.      tName    : string;
  13.    begin
  14.      WS.ws_row:= 25;
  15.      WS.ws_col:= 80;
  16.      WS.ws_xpixel:= 0;
  17.      WS.ws_ypixel:= 0;
  18.      tName := 'xterm'#0;
  19.      //ChildPid := forkpty(@Fpty, @tName[1], @term, @WS);
  20.      ChildPid := forkpty(@Fpty, nil, nil, nil);
  21.      if ChildPid < 0 then
  22.        begin
  23.          //Result:= -1;
  24.          Exit;
  25.        end;
  26.      if ChildPid = 0 then
  27.        begin
  28.          //Child
  29.          //libUbuntu.ParseCommand('script', ['-qfc', 'sudo whoami'], @OnStdout, @OnStderr);
  30.          StdoutMemo.Lines.Add('Child!');
  31.          exit;
  32.        end;
  33.      StdoutMemo.Lines.Add('Parent!');  
  34.   //(...)
  35.   end;
  36.  

but I really get all sorts of weird behaviors. Sometimes I see "child!' and sometimes 'parent!' printed into the memo, and either way I cannot get the RunCommand (I use my own modified version that also tests for certain prompts) to work in an interactive way.

I see that you use pseudo terminals via /dev/ptmx approach. Could you please tell me, if you think I could use your approach to achieve my goal? I want to be able to use TProcess to call sudo in such a way that it would print its prompts to the stdout as if it was ran in a terminal.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: mmbk on October 17, 2017, 06:02:36 pm
Hi,

I'm testing rpi_hal.pas on Raspberry Pi 3 and FPC 3.0.0 but I'm getting this erro:

2017-10-17 13:45:46 ERR RPI_mmap_init, can not open /dev/mem on target CPU arm, result: -1
2017-10-17 13:45:46 ERR RPI_hal: supported min-/maximum kernel #797 - #970 ( uname -a )
2017-10-17 13:45:46 ERR RPI_hal: can not initialize MemoryMap. RPI_hal will Halt(1)
2017-10-17 13:45:46 ERR RPI_hal_exit: Exitcode:   1

If I use SUDO it working fine, but I don't want to run with root user.

Permissions of the files:
0 crw-r----- 1 root kmem 1, 1 Oct 17 11:31 /dev/mem
0 crw-rw---- 1 root gpio 243, 0 Oct 17 11:31 /dev/gpiomem

Pi groups:
pi : pi adm kmem dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio

This is my example:

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. uses rpi_hal;
  4.  
  5. const
  6.  pinoLed = 23;
  7. begin
  8.   RPI_HW_Start([InitHaltOnError,InitGPIO]);
  9.   gpio_set_output(pinoLed);
  10.   GPIO_set_pin(pinoLed, true);
  11.   WriteLn('continue..');
  12.   ReadLn;
  13.   GPIO_set_pin(pinoLed, false);
  14. end.                    
  15.  
ps.:Sorry about my English.

Any idea?

tks
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: Sam_Y on October 20, 2017, 02:05:14 pm
Hi mmbk

Have you tried setcap?
in the terminal window, type

$ sudo setcap cap_sys_rawio,cap_dac_override=ep test
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: mmbk on October 20, 2017, 03:05:22 pm
Hi Sam_Y,

I follow this tip http://lists.freepascal.org/pipermail/fpc-other/2015-October/001042.html (http://lists.freepascal.org/pipermail/fpc-other/2015-October/001042.html) and work fine!

Code: [Select]
interface

// The following option controls whether the code is optimized for Raspberry
PI 2.
// Added 2015-10-10 by Bo Berglund following web advice to get it working on
the Pi2 hardware
{$DEFINE RPi2}
...
const
  REG_GPIO = {$IFDEF RPi2} $3F000 {$ELSE} $20000 {$ENDIF};//bcm2835/bcm2836
gpio register
...
  // Mapping the RPi GPIO pin functions to GPIO I/O, should be here rather
than in user code
  RPI_P3  =  2; //GPIO2
  RPI_P5  =  3; //GPIO3
  RPI_P7  =  4; //GPIO4
  RPI_P8  = 14; //GPIO14
  RPI_P10 = 15; //GPIO15
  RPI_P11 = 17; //GPIO17
  RPI_P12 = 18; //GPIO18
  RPI_P13 = 27; //GPIO27
  RPI_P15 = 22; //GPIO22
  RPI_P16 = 23; //GPIO23
  RPI_P18 = 24; //GPIO24
  RPI_P19 = 10; //GPIO10
  RPI_P21 =  9; //GPIO9
  RPI_P22 = 25; //GPIO25
  RPI_P23 = 11; //GPIO11
  RPI_P24 =  8; //GPIO8
  RPI_P26 =  7; //GPIO7
...
function TIoDriver.MapIo: boolean;
begin
 Result := True;
 {$IFDEF RPi2}
   fd := fpopen('/dev/gpiomem', O_RdWr or O_Sync); // Open the master
/dev/memory device
 {$ELSE}
   fd := fpopen('/dev/mem', O_RdWr or O_Sync); // Open the master
/dev/memory device
 {$ENDIF}
  if fd < 0 then
  begin
    Result := False; // unsuccessful memory mapping
  end;
 //
end;

Thank you!
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: seliman on November 11, 2017, 11:45:01 pm
Hi , mi name is Ramiro , I from Argentina , to many years ago , I m program in delphi , windows , now I Program in Lazarus , and  use cross compiled for RPI 3 , mi problem is , what I don t Know , where to put the library rpi_hal ?  can you help me please , I m Sorry mi english

Thanks in advance
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: ATILIUS on January 06, 2019, 04:18:13 pm
Hope this thread is still active...

Trying to program an ADS1115 on the I2C bus of the Raspberry, I couldn't find any working I2C library until now.
My Raspberryx Pi is model 3B, fpc is V3.3.1.

Currently I am trying rpi_hal V4.5.
Here are my problems:

1. sudo rights
After compiling testrpi with fpc, testrpi only runs with sudo rights or as root. Running testrpi as normal user pi gives "ERR RPI_mmap_init, can not open /dev/mem on target CPU arm, result: -1", even though I have used "sudo adduser pi gpio, sudo adduser pi i2c, sudo adduser pi spi".

2. rpi_hal.pas and Lazarus
As soon as I include rpi_hal.pas in the uses clause of my project, I get an exception with message "/build/glibc-x07mZ9/glibc-2.24/sysdeps/unix/sysv/linux/raise.c" was not found.".

Is there any solution, just to run the I2C part without any errors?
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: sfischer on January 06, 2019, 07:41:13 pm
/dev/mem is required for GPIO access. If you do not need GPIOs, then use a different RPI_HW_Start option.
e.g. RPI_HW_Start([InitHaltOnError,InitI2C,InitSPI]);
and not RPI_HW_Start([InitHaltOnError,InitGPIO,InitI2C,InitSPI]);
just drop the paramter 'InitGPIO'
or run your prog with sudo or as root.

program yourprog;
uses rpi_hal;
begin
  if RPI_HW_Start([InitHaltOnError,InitI2C,InitSPI]) then
  begin
    ... your code ...
  end;
end.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: ATILIUS on January 07, 2019, 02:08:51 pm
The problem is, that as soon as I include rpi_hal in the uses clause, the program throws an exception at start and I not even get to the  RPI_HW_Start([InitHaltOnError,InitI2C,InitSPI]).

Therefore, I will use FpOpen, FpWrite and FpRead commands, which are working ok.

Thanks for your quick reply anyways.
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: kaimex on March 06, 2019, 08:49:48 pm
Hi Stefan,
I have just compiled testrpi, rpi_hal and pfio on a Raspberry Pi 3B+.
During compilation there were 2 warnings, complaining that crtbegin.o and crtend.o could not be found.
The first three lines of output from testrpi report errors:
2019-03-06 20:20:58 ERR Get_CPU_INFO_Init: (0x00A020D3)  unknown rev:a020d3: RPI not supported
2019-03-06 20:20:58 ERR SPI_Dev_Init[0x00/00]: /dev/spidev0.0
2019-03-06 20:20:58 ERR SPI_Dev_Init[0x00/01]: /dev/spidev0.1
Are these serious errors or ignorable ?
Regards Kai
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: kaimex on March 09, 2019, 04:25:30 pm
In the meantime I have found that the "SPI_Dev_Init" related  errors came from not having enabled SPI in /boot/config.txt. After uncommenting the line #dtparam=spi=on those error messages disappear.
The first error message can be avoided by adding a line to the procedure Get_CPU_INFO_Init:
-------
     $202082  : cpu_rev:=RPI_SetInfo(cpu_rev,'3B',4,1,2,47,40,1024);    // Pi3B (a02082 Sony, UK)
     $2020d3  : cpu_rev:=RPI_SetInfo(cpu_rev,'3B+',4,1,2,47,40,1024); // Pi3B+ (a020d3 Sony, UK) <---new line
          else LOG_Writeln(LOG_ERROR,'Get_CPU_INFO_Init: (0x'+Hex(lw,8)+') unknown rev:'+cpu_rev+': RPI not supported');
----------------
It is not yet clear to me, whether more has to be changed.
Regards Kai
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: kaimex on March 12, 2019, 09:17:00 pm
I have just noticed, that the author has re-invented  some FrePascal function like e.g. hexstr() or upper() , lower(), furthermore number-to-string conversions and string-to-number conversions, that are already available within FPC. The newly defined hexstr() uses a different number of parameters.
I wonder why this had to be done.
I also wonder, why (if it was really necessary or at least convenient to use differently defined functions) why he did not implement these functions with different names.
In order to be able to use FPC's built-in functions, these re-definitions have to be renamed.

Kaimex
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: kaimex on March 14, 2019, 05:48:03 pm
Hi Stefan
many thanks for the new version V5.0

I have a question regarding I2C-operation:
Is it possible to speed-up the I2C-functions ?
I have read about 100 kb/s, 400 kb/s and 1.x Mb/s speeds.
Does your code use the lowest per default ?
Is it possible to change that ?

Regards Kai
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: kaimex on March 15, 2019, 02:03:30 pm
In the meantime I have found that the I2C speed can be (set) requested in /boot/config.txt by adding ",i2c_arm_baudrate=200000" or "...400000" to the line with dtparam=i2c_arm=on" as sudo and reboot.
The actual I2C speed, however, depends on the dynamic CPU clocking (or related other clock).
On my Raspi 3B+ (default seems to be baudrate=100000) I observed an I2C clock cycle time of 16 us (~60 kBaud) at low work load for the CPU (sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq reports 600000 = 600 MHz). When the work load is higher, cpuinfo_cur_freq reports 1400 MHz and the I2C clock cycle time was 10 us, corresponding to 100 kBaud. After configuring the I2C baudrate to 200000, I found cycle times of 8 & 5 us, correspondig to 125 kBaud & 200 kBaud, finally for the requested baudrate 400000 the observed cycle times were 4 & 2.5 us (250 kBaud & 400 kBaud).
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: kaimex on March 16, 2019, 01:29:29 pm
The explanation for the 1.6 ratio of I2C clock cycle times under idle vs. load conditions seems to be the following: The I2C clock is derived from the core_freq (GPU) via SCL=core_freq/CDIV . See BCM2835-ARM-Peripherals.pdf, which uses the term "core clock". CDIV is a constant even number. On the Raspberry 3B+ the defaults for core_freq are 250 MHz under idle conditions and 400 MHz under load conditions. 400/250=1.6 . See e.g. https://haydenjames.io/raspberry-pi-3-overclock/ .
Title: Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
Post by: kaimex on March 16, 2019, 08:12:00 pm
The Raspi has an over-clocking parameter called "core_freq_min". When this is set to the value of core_freq (in /boot/cofig.txt) then the I/O-speeds (UART, SPI, I2C) should be constant. It is not clear whether this may be done for the default setting of 400 MHz (Raspi 3B+) without infringing CPU guarantee.
TinyPortal © 2005-2018