Recent

Author Topic: Raspberry Pi 2 ARM embedded target  (Read 6844 times)

rca

  • Full Member
  • ***
  • Posts: 118
Raspberry Pi 2 ARM embedded target
« on: May 04, 2020, 11:16:54 pm »
I found this information, where the author "Robert Roland" has managed that add Raspberry Pi 2 ARM embedded target.

https://bugs.freepascal.org/view.php?id=35236
https://github.com/graemeg/freepascal/commit/f06ffad7f6044d167ea206afd26d8c1f5dd979b1

How can it be used?
Some example?

Could it be extended to a Raspberry Pi 3?

avra

  • Hero Member
  • *****
  • Posts: 2580
    • Additional info
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

rca

  • Full Member
  • ***
  • Posts: 118
Re: Raspberry Pi 2 ARM embedded target
« Reply #2 on: May 05, 2020, 04:02:24 am »
Are you aware of Ultibo?
https://wiki.lazarus.freepascal.org/Ultibo_core
https://wiki.lazarus.freepascal.org/Ultibo_Quick_Start

If I know him, Ultibo is very good.

I was struck that now formally fpc, has included the Raspi 2 like ARM embedded target.

And I would really like to try and understand how to use it.
Although at the moment I only have Rpi3+ and Rpi4.

Thaddy

  • Hero Member
  • *****
  • Posts: 18676
  • Jungle wars. And failing health it seems.
Re: Raspberry Pi 2 ARM embedded target
« Reply #3 on: May 05, 2020, 08:37:29 am »
Build a cross compiler for arm-embedded and also install the toolchain. Use it for Armv6.
Here is a good article on which files are needed to bootstrap:
https://www.studica.com/blog/program-embedded-systems-raspberry-pi
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

rca

  • Full Member
  • ***
  • Posts: 118
Re: Raspberry Pi 2 ARM embedded target
« Reply #4 on: May 09, 2020, 06:05:36 pm »
Here is a good article on which files are needed to bootstrap:
https://www.studica.com/blog/program-embedded-systems-raspberry-pi

Thanks for excellent article.

I have read it these days and now it is more understandable how it works.
And I'm doing some tests.

With lazarus-freepascal, when compiling a arm-embedded program, files are automatically generated:

name.bin
name.elf
name.hex

To generate the kernel7.img file, I have done:
Code: Pascal  [Select][+][-]
  1. arm-none-eabi-objcopy name.elf -O binary kernel7.img

and I have verified that the new file 'kernel7.img', is the same file 'name.bin'

Is there a way to automatically change name.bin to name.img when compiling?
Or does it automatically generate name.img as well?

rca

  • Full Member
  • ***
  • Posts: 118
Re: Raspberry Pi 2 ARM embedded target
« Reply #5 on: May 10, 2020, 12:38:06 am »
After doing some research, I've already figured out how to automatically create the 'img' extension.

Inside the file:
compiler/systems/t_embed.pas

Found 'Post process':
Code: Pascal  [Select][+][-]
  1.       if success then
  2.         success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  3.           ChangeFileExt(current_module.exefilename,'.elf')+' '+
  4.           ChangeFileExt(current_module.exefilename,'.bin'),true,false);

You can add:
Code: Pascal  [Select][+][-]
  1.       if success then
  2.         success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  3.           ChangeFileExt(current_module.exefilename,'.elf')+' '+
  4.           ChangeFileExt(current_module.exefilename,'.img'),true,false);

avra

  • Hero Member
  • *****
  • Posts: 2580
    • Additional info
Re: Raspberry Pi 2 ARM embedded target
« Reply #6 on: May 10, 2020, 04:19:00 pm »
If you use ifdefs to properly isolate a case where IMG should be used instead of BIN, then it would be nice if you could create a patch for compiler/systems/t_embed.pas and report it to the bugtracker so others could benefit from your findings.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

rca

  • Full Member
  • ***
  • Posts: 118
Re: Raspberry Pi 2 ARM embedded target
« Reply #7 on: May 11, 2020, 02:48:48 am »
Following the suggestions of @avra

For the Raspberry we only need the file with the extension '.img',
the other files with the extension '.bin', '.hex' and '.elf' are not necessary.

I have modified compiler/systems/t_embed.pas

I have added a variable:
  
Code: Pascal  [Select][+][-]
  1. OnlyExtImg: boolean = False;

And changing this Original part
Code: Pascal  [Select][+][-]
  1.   if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded,system_xtensa_embedded]) then
  2.     begin
  3.       success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  4.         FixedExeFileName+' '+
  5.         maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.hex'))),true,false);
  6.       if success then
  7.         success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  8.           FixedExeFileName+' '+
  9.           maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin'))),true,false);
  10.     end;

For this other:
Resolving that it only affects when we are compiling for the raspberry:
Code: Pascal  [Select][+][-]
  1.   if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded,system_xtensa_embedded]) then
  2.     begin
  3.       {$ifdef ARM}
  4.       if (current_settings.controllertype = ct_raspi2) then OnlyExtImg := True;    
  5.       {$endif ARM}
  6.       if OnlyExtImg then
  7.       begin
  8.         success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  9.           FixedExeFileName+' '+
  10.           maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.img'))),true,false);
  11.           DeleteFile(FixedExeFileName);
  12.        end             
  13.        else
  14.        begin   
  15.          success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  16.            FixedExeFileName+' '+
  17.            maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.hex'))),true,false);
  18.          if success then
  19.            success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  20.              FixedExeFileName+' '+
  21.              maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin'))),true,false);
  22.           end;
  23.     end;


Sorry, but I don't know how to create a patch and I don't know how to report it to the bugtracker.
Perhaps some other user can collaborate to do so.

avra

  • Hero Member
  • *****
  • Posts: 2580
    • Additional info
Re: Raspberry Pi 2 ARM embedded target
« Reply #8 on: May 11, 2020, 10:57:14 am »
For the Raspberry we only need the file with the extension '.img',
the other files with the extension '.bin', '.hex' and '.elf' are not necessary.

I have modified compiler/systems/t_embed.pas

I have added a variable:
 
Code: Pascal  [Select][+][-]
  1. OnlyExtImg: boolean = False;
...
No, do not change more then really needed. I meant something like
Code: Pascal  [Select][+][-]
  1.       if success then
  2.         success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  3.           ChangeFileExt(current_module.exefilename,'.elf')+' '+
  4.          {$ifdef ARM}
  5.           ChangeFileExt(current_module.exefilename,'.img'),true,false);
  6.          {$else}
  7.           ChangeFileExt(current_module.exefilename,'.bin'),true,false);
  8.          {$endif ARM}
  9.  
but that would change extension for all ARM processors and not just for Pi. If that is what it should be then fine. If there are ARMs that need different file extension then maybe it would be easier for you if you simply create a batch command to rename file extension after compilation. You can set it to be called here: Project / Project Options / Compiler Options / Compiler Commands / Execute After / Command.

Sorry, but I don't know how to create a patch and I don't know how to report it to the bugtracker.
Those are very useful skills that you should learn sooner or later. I am not pushing - it's really up to you and your priorities...

Perhaps some other user can collaborate to do so.
If I was certain it was a good change I would do that. Unfortunately I am not familiar enough with that particular topic to be confident that such change is good and take the responsibility for the action.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

ccrause

  • Hero Member
  • *****
  • Posts: 1086
Re: Raspberry Pi 2 ARM embedded target
« Reply #9 on: May 11, 2020, 03:01:46 pm »
... but that would change extension for all ARM processors and not just for Pi. If that is what it should be then fine. If there are ARMs that need different file extension then maybe it would be easier for you if you simply create a batch command to rename file extension after compilation.
I suspect the .img extension is a convention used by the RPi bootloader and not a general requirement for ARM systems.  Other people use FPC on embedded ARM, so perhaps they can chip in with real information here.

Another alternative you have is to specify the name (and extension) to your kernel image in config.txt on the boot card.

rca

  • Full Member
  • ***
  • Posts: 118
Re: Raspberry Pi 2 ARM embedded target
« Reply #10 on: May 11, 2020, 08:54:15 pm »
Currently, the changes that Robert Roland made in freepascal
are compatible with the file that Raspberry recognizes by default
'kernel7.img' without changing or adding the config.txt file.

The compiler is expected to automatically generate the file 'kernel7.img'
like other bare metal compilers including "Ultibo".

For Raspberry, the other files generated with the extension '.bin', '.hex' and '.elf' are not necessary.

That is why I made this simple change, which only affects when compiling for the Raspberry:
Code: Pascal  [Select][+][-]
  1.       {$ifdef ARM}
  2.       if success then
  3.       begin    
  4.           if (current_settings.controllertype = ct_raspi2) then
  5.           begin
  6.             success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+ FixedExeFileName + ' kernel7.img',true,false);
  7.             DeleteFile(FixedExeFileName);
  8.             DeleteFile(current_module.exefilename + '.hex');
  9.             DeleteFile(current_module.exefilename + '.bin');
  10.           end;
  11.       end;                                 
  12.       {$endif ARM}


Currently, the original is:
Code: Pascal  [Select][+][-]
  1.   if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded,system_xtensa_embedded]) then
  2.     begin
  3.       success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  4.         FixedExeFileName+' '+
  5.         maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.hex'))),true,false);
  6.       if success then
  7.         success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  8.           FixedExeFileName+' '+
  9.           maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin'))),true,false);
  10.     end;


Now it remains:
Code: Pascal  [Select][+][-]
  1.   if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded,system_xtensa_embedded]) then
  2.     begin
  3.       success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  4.         FixedExeFileName+' '+
  5.         maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.hex'))),true,false);
  6.       if success then
  7.         success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  8.           FixedExeFileName+' '+
  9.           maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin'))),true,false);
  10.       {$ifdef ARM}
  11.       if success then
  12.       begin      
  13.           if (current_settings.controllertype = ct_raspi2) then
  14.           begin
  15.             success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+ FixedExeFileName + ' kernel7.img',true,false);
  16.             DeleteFile(FixedExeFileName);
  17.             DeleteFile(current_module.exefilename + '.hex');
  18.             DeleteFile(current_module.exefilename + '.bin');
  19.           end;
  20.       end;                                 
  21.       {$endif ARM}
  22.     end;


And I've verified that that change is only reflected when I compile for Raspberry (-WpRASPI2).
For everyone else, it remains the same.

rca

  • Full Member
  • ***
  • Posts: 118
Re: Raspberry Pi 2 ARM embedded target
« Reply #11 on: May 11, 2020, 10:01:09 pm »
Also, I already generated the respective patch and reported it:

https://bugs.freepascal.org/view.php?id=37052

Thaddy

  • Hero Member
  • *****
  • Posts: 18676
  • Jungle wars. And failing health it seems.
Re: Raspberry Pi 2 ARM embedded target
« Reply #12 on: May 12, 2020, 08:55:58 am »
This is not complete, since the Rasberry Pi 2 v 1.2 also supports AARCH64 which may be of interest for embedded.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

ccrause

  • Hero Member
  • *****
  • Posts: 1086
Re: Raspberry Pi 2 ARM embedded target
« Reply #13 on: May 12, 2020, 09:28:19 am »
The compiler is expected to automatically generate the file 'kernel7.img'
like other bare metal compilers including "Ultibo".
A good point, the Ultibo version of FPC does not modify t_embed.pas the way you propose.  I'm not familiar with the design of Ultibo, but it may be worth looking at that project to see how they prepare binaries and rename for RPi.

rca

  • Full Member
  • ***
  • Posts: 118
Re: Raspberry Pi 2 ARM embedded target
« Reply #14 on: May 12, 2020, 02:50:26 pm »
This is not complete, since the Rasberry Pi 2 v 1.2 also supports AARCH64 which may be of interest for embedded.
Since Rasberry Pi 2 v1.2, it allows to run in AArch32 or AArch64 mode.

For those raspberry models, in AArch32 mode, they support 'kernel7.img' and in AArch64 mode, they support 'Kernel8.img'.

In other words, a Raspberry Pi 3 or Pi 4 works correctly in AArch32 mode.

Multiple bare metal compilers have the option to choose to compile for AArch32 or AArch64 mode.
In the case of Ultibo, currently only AArch32 mode works. And it is still very complete and functional.

Also, from what I understand, freepascal does not support aarch64-embedded.

A good point, the Ultibo version of FPC does not modify t_embed.pas the way you propose.
That's because Ultibo developed an independent RTL:
https://github.com/ultibohub/Core/tree/master/source/rtl/ultibo


 

TinyPortal © 2005-2018