Recent

Author Topic: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library  (Read 63796 times)

mmbk

  • Newbie
  • Posts: 2
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #105 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
« Last Edit: October 17, 2017, 06:04:58 pm by mmbk »

Sam_Y

  • Newbie
  • Posts: 1
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #106 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

mmbk

  • Newbie
  • Posts: 2
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #107 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 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!

seliman

  • New member
  • *
  • Posts: 10
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #108 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

ATILIUS

  • New member
  • *
  • Posts: 9
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #109 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?

sfischer

  • New member
  • *
  • Posts: 37
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #110 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.

ATILIUS

  • New member
  • *
  • Posts: 9
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #111 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.

kaimex

  • New member
  • *
  • Posts: 7
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #112 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

kaimex

  • New member
  • *
  • Posts: 7
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #113 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

kaimex

  • New member
  • *
  • Posts: 7
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #114 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

kaimex

  • New member
  • *
  • Posts: 7
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #115 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

kaimex

  • New member
  • *
  • Posts: 7
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #116 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).

kaimex

  • New member
  • *
  • Posts: 7
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #117 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/ .

kaimex

  • New member
  • *
  • Posts: 7
Re: Lazarus / RaspberyPi rpi_hal Hardware Abstraction Library
« Reply #118 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.