Recent

Author Topic: Pascal bootloader for AVR  (Read 1600 times)

ccrause

  • Hero Member
  • *****
  • Posts: 862
Pascal bootloader for AVR
« on: April 29, 2023, 09:58:32 am »
I have started a serial bootloader project for AVR controllers: This project supports most of the STK500V1 protocol, mostly as required to function with the avrdude upload tool.

While this bootloader is (not yet) particularly small or fast, it is a demonstration of what can be done in FPC, and possibly identify areas which can be improved in the compiler.

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Pascal bootloader for AVR
« Reply #1 on: April 29, 2023, 10:24:25 am »
Hi,

  Very nice !

  I wanted to research freepascal avr bootloader topic for a long time ...

  ( there goes my weekend playing and testing with your code .... ;) )

regards,

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Pascal bootloader for AVR
« Reply #2 on: April 29, 2023, 01:06:45 pm »
That reminds me: https://osdn.net/projects/sfnet_avrub/releases/ is an AVR downloader that I came across from a little handheld oscilloscope, written using Delphi. Might be useful to somebody.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ccrause

  • Hero Member
  • *****
  • Posts: 862
Re: Pascal bootloader for AVR
« Reply #3 on: April 29, 2023, 06:02:57 pm »
That reminds me: https://osdn.net/projects/sfnet_avrub/releases/ is an AVR downloader that I came across from a little handheld oscilloscope, written using Delphi. Might be useful to somebody.

MarkMLl
An interesting idea, it seems as if it has a GUI to configure bootloader settings, generate a header file and then compile a new bootloader using avr-gcc. Unfortunately I do not recognize the download protocol and it isn't described anywhere, so not that easy to re-use.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Pascal bootloader for AVR
« Reply #4 on: April 29, 2023, 06:19:01 pm »
An interesting idea, it seems as if it has a GUI to configure bootloader settings, generate a header file and then compile a new bootloader using avr-gcc. Unfortunately I do not recognize the download protocol and it isn't described anywhere, so not that easy to re-use.

I didn't go too deeply into it since my main interest was talking to the 'scope, but taking into account that it's apparently got code signing etc. it might even be an AVR chip protocol rather than a separate bootloader.

Slightly later: "The bootloader included with this application note..." http://ww1.microchip.com/downloads/en/AppNotes/00002462A.pdf hence apparently something like http://start.atmel.com/#example/Atmel:AVR231_AES_Bootloader:0.0.1::Application:AVR231_AES_Bootloader: although I've not yet worked out how to use that. The important thing is that it is an "official" protocol, even if it's been overshadowed by- in particular- Arduino conventions.

Even later: https://community.element14.com/cfs-file/__key/communityserver-wikis-components-files/00-00-00-01-46/Atmel_2D00_megaAVR_2D00_ATmega48A_2D00_Design-Elements_2D00_Application-Library_2D00_Atmel.Application_5F00_Library_5F00_23.zip

MarkMLl
« Last Edit: May 01, 2023, 02:26:55 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Pascal bootloader for AVR
« Reply #5 on: May 05, 2023, 12:12:20 pm »
Hi,

  is it possible to call the external linker with a custom link script ?

  I would like to use the AVR link script changes from [AVR] Add command line options to selectively remove portions of the RTL startup code. with the fpc 3.2.3 fixes compiler and not not main .
 
regards,

ccrause

  • Hero Member
  • *****
  • Posts: 862
Re: Pascal bootloader for AVR
« Reply #6 on: May 05, 2023, 01:39:45 pm »
Hi,

  is it possible to call the external linker with a custom link script ?

  I would like to use the AVR link script changes from [AVR] Add command line options to selectively remove portions of the RTL startup code. with the fpc 3.2.3 fixes compiler and not not main .
 
regards,

One option is to compile with command line option -sh.  This will generate the default linker script (it will create something like a linknnnn.res) and a shell script (ppas.sh or ppas.bat (I think)) to call the assembler and linker and pass the name of the link script to ld.  Modify the linknnn.res file as needed, then execute the ppas script.

PS: This can also work in principle for fpc 3.2.2, but there all the startup code sits in the _FPC_start procedure, so one needs a discard statement in the linker script more or less like this (untested):
Code: [Select]
  /DISCARD/ :
  {
    *(*_fpc_start)
  }

Edit: Fixed /DISCARD/ above.
« Last Edit: May 05, 2023, 07:23:41 pm by ccrause »

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Pascal bootloader for AVR
« Reply #7 on: May 05, 2023, 05:26:19 pm »
  is it possible to call the external linker with a custom link script ?
< snip>

One option is to compile with command line option -sh.  This will generate the default linker script (it will create something like a linknnnn.res) and a shell script (ppas.sh or ppas.bat (I think)) to call the assembler and linker and pass the name of the link script to ld.  Modify the linknnn.res file as needed, then execute the ppas script.

Thx !

PS: This can also work in principle for fpc 3.2.2, but there all the startup code sits in the _FPC_start procedure, so one needs a discard statement in the linker script more or less like this (untested):
Code: [Select]
  /*DISCARD*/ :
  {
    *(.text.*_fpc_start)
  }

Well I assume that is "/DISCARD/" without the * . I inserted that statement in the line as your PR but doesn't have any effect .

I tried with  the following and it worked !

Code: [Select]
     *(.init.*_fpc_start)


Thx !

Regards,



d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Pascal bootloader for AVR
« Reply #8 on: May 05, 2023, 05:50:36 pm »
Hi,

  of course, now that the startup code is eliminated, I'm trying to find a way to tell the avr-embedded-ld that the entry is the symbol main. I tried to change the ENTRY(_START) to ENTRY(main) in the script and/or add to the linker command line the --entry=main option, with no success :( . I always get the same wrong code at the bootloader start address :

Code: ASM  [Select][+][-]
  1. G:\Programming\dimitris\Projects\avrpasboot>G:\Programming\dimitris\tools\laz-2.2.2_fpc-3.2.2\fpc\3.2.3\bin\i386-win32\avr-embedded-objdump.exe -d pasboot.elf | more
  2.  
  3. pasboot.elf:     file format elf32-avr
  4.  
  5.  
  6. Disassembly of section .text:
  7.  
  8. 00007000 <PsPASBOOT_ss_UART_TRANSMIT_BUFFERsPBYTEsBYTE>:
  9.     7000:       4f 92           push    r4
  10.     7002:       3f 92           push    r3
  11.     7004:       2f 92           push    r2
  12.     7006:       1c 01           movw    r2, r24
  13.     7008:       46 2e           mov     r4, r22
  14.     700a:       08 c0           rjmp    .+16            ; 0x701c <PsPASBOOT_ss_UART_TRANSMIT_BUFFERsPBYTEsBYTE+0x1c>
  15.     700c:       f1 01           movw    r30, r2
  16.     700e:       80 81           ld      r24, Z
  17.     7010:       0e 94 45 3c     call    0x788a  ; 0x788a <UART_ss_UART_TRANSMITsBYTE>
  18.     7014:       21 e0           ldi     r18, 0x01       ; 1

Pitty that the fpc 3.2.3 fixes doesn't have the avr sections PR merged !

I attached both the batch file and linker script, so as I'm not familiar with this link topic, does anyone have any hint ?

regards,
« Last Edit: May 05, 2023, 05:54:59 pm by d.ioannidis »

ccrause

  • Hero Member
  • *****
  • Posts: 862
Re: Pascal bootloader for AVR
« Reply #9 on: May 05, 2023, 07:23:03 pm »
Code: [Select]
  /*DISCARD*/ :
  {
    *(.text.*_fpc_start)
  }

Well I assume that is "/DISCARD/" without the * . I inserted that statement in the line as your PR but doesn't have any effect .

I tried with  the following and it worked !

Code: [Select]
     *(.init.*_fpc_start)

Yes, well spotted! I fixed it in my post.
« Last Edit: May 05, 2023, 07:39:38 pm by ccrause »

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Pascal bootloader for AVR
« Reply #10 on: May 05, 2023, 07:23:19 pm »
Hi,

  FYI if I move all the procedures from the lpr file, leaving only the "begin end." ( aka as main ), to another unit, then it works !

Code: ASM  [Select][+][-]
  1. G:\Programming\dimitris\Projects\avrpasboot>G:\Programming\dimitris\tools\laz-2.2.2_fpc-3.2.2\fpc\3.2.3\bin\i386-win32\avr-embedded-objdump.exe -d pasboot.elf | more
  2.  
  3. pasboot.elf:     file format elf32-avr
  4.  
  5.  
  6. Disassembly of section .text:
  7.  
  8. 00007000 <main>:
  9.     7000:       0e 94 7d 3b     call    0x76fa  ; 0x76fa <FPC_INIT_FUNC_TABLE>
  10.     7004:       04 b6           in      r0, 0x34        ; 52
  11.     7006:       00 92 65 02     sts     0x0265, r0      ; 0x800265 <U_sPsPASBOOT_ss_STARTUPSTATUS>
  12.     700a:       14 be           out     0x34, r1        ; 52
  13.  

Yeah ...  :D

regards,
« Last Edit: May 05, 2023, 07:25:22 pm by d.ioannidis »

ccrause

  • Hero Member
  • *****
  • Posts: 862
Re: Pascal bootloader for AVR
« Reply #11 on: May 05, 2023, 07:36:34 pm »
  of course, now that the startup code is eliminated, I'm trying to find a way to tell the avr-embedded-ld that the entry is the symbol main. I tried to change the ENTRY(_START) to ENTRY(main) in the script and/or add to the linker command line the --entry=main option, with no success :( . I always get the same wrong code at the bootloader start address :
One way of fixing this is to place a light version of the startup code that jump to PASCALMAIN in the init section:
Code: Pascal  [Select][+][-]
  1. procedure PASCALMAIN; external name 'PASCALMAIN';
  2.  
  3. procedure custom_start; assembler; nostackframe; noreturn;
  4. asm
  5.   .init
  6.   jmp PASCALMAIN
  7.   .text
  8. end;
I don't know whether the linker can automatically insert code at the reset vector to jump to the entry point.

Note that with fpc 3.3.1 it can be done in a slightly simpler way.

ccrause

  • Hero Member
  • *****
  • Posts: 862
Re: Pascal bootloader for AVR
« Reply #12 on: May 05, 2023, 07:53:17 pm »
  FYI if I move all the procedures from the lpr file, leaving only the "begin end." ( aka as main ), to another unit, then it works !
This is indeed one way of doing it.  The less obvious downside to this is if one wants to store progmem data, it will be inserted before .text. Fixing this would require even more changes to the custom linker script.

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Pascal bootloader for AVR
« Reply #13 on: May 05, 2023, 07:53:46 pm »
Hi,

  of course, now that the startup code is eliminated, I'm trying to find a way to tell the avr-embedded-ld that the entry is the symbol main. I tried to change the ENTRY(_START) to ENTRY(main) in the script and/or add to the linker command line the --entry=main option, with no success :( . I always get the same wrong code at the bootloader start address :
One way of fixing this is to place a light version of the startup code that jump to PASCALMAIN in the init section:
Code: Pascal  [Select][+][-]
  1. procedure PASCALMAIN; external name 'PASCALMAIN';
  2.  
  3. procedure custom_start; assembler; nostackframe; noreturn;
  4. asm
  5.   .init
  6.   jmp PASCALMAIN
  7.   .text
  8. end;
I don't know whether the linker can automatically insert code at the reset vector to jump to the entry point.

Note that with fpc 3.3.1 it can be done in a slightly simpler way.

Thank you !

I added your code just before the begin :

Code: Pascal  [Select][+][-]
  1. procedure PASCALMAIN; external name 'PASCALMAIN';
  2.  
  3. procedure custom_start; assembler; nostackframe; noreturn;
  4. asm
  5.   .init
  6.   jmp PASCALMAIN
  7.   .text
  8. end;
  9.  
  10. begin
  11.   startupStatus := xMCUSR;
  12.   xMCUSR := 0;
  13.   // Disable watchdog
  14.  


and everything is in the right place  :D .



Code: ASM  [Select][+][-]
  1. G:\Programming\dimitris\Projects\avrpasboot>G:\Programming\dimitris\tools\laz-2.2.2_fpc-3.2.2\fpc\3.2.3\bin\i386-win32\avr-embedded-objdump.exe -d pasboot.elf | more
  2.  
  3. pasboot.elf:     file format elf32-avr
  4.  
  5.  
  6. Disassembly of section .text:
  7.  
  8. 00007000 <PsPASBOOT_ss_UART_TRANSMIT_BUFFERsPBYTEsBYTE-0x4>:
  9.     7000:       0c 94 57 38     jmp     0x70ae  ; 0x70ae <main>
  10.  
  11. 00007004 <PsPASBOOT_ss_UART_TRANSMIT_BUFFERsPBYTEsBYTE>:
  12.     7004:       4f 92           push    r4
  13.     7006:       3f 92           push    r3
  14.     7008:       2f 92           push    r2
  15.     700a:       1c 01           movw    r2, r24
  16.     .........
  17.     .........
  18.     .........
  19.     .........
  20.     70a6:       0e 94 47 3c     call    0x788e  ; 0x788e <UART_ss_UART_TRANSMITsBYTE>
  21.     70aa:       2f 90           pop     r2
  22.     70ac:       08 95           ret
  23.  
  24. 000070ae <main>:
  25.     70ae:       0e 94 d4 3b     call    0x77a8  ; 0x77a8 <FPC_INIT_FUNC_TABLE>
  26.     70b2:       04 b6           in      r0, 0x34        ; 52
  27.     70b4:       00 92 65 02     sts     0x0265, r0      ; 0x800265 <U_sPsPASBOOT_ss_STARTUPSTATUS>
  28.     70b8:       14 be           out     0x34, r1        ; 52
  29.     70ba:       f8 94           cli  
  30.  

Linker MAP :

Code: ASM  [Select][+][-]
  1. .rela.plt
  2.  *(.rela.plt)
  3.  
  4. /DISCARD/
  5.  *(.init.*_fpc_start)
  6.  
  7. .text           0x00007000      0x8c4
  8.  *(.init .init.*)
  9.  .init.n_pspasboot_ss_custom_start
  10.                 0x00007000        0x4 G:\Programming\dimitris\Projects\avrpasboot\lib\avr-embedded\pasboot.o
  11.  *(.progmem.gcc*)
  12.  *(.progmem*)
  13.                 0x00007004                . = ALIGN (0x2)

Again thank you @ccrause !

regards,
« Last Edit: May 05, 2023, 07:56:34 pm by d.ioannidis »

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Pascal bootloader for AVR
« Reply #14 on: May 05, 2023, 08:02:32 pm »
Hi,

  FYI if I move all the procedures from the lpr file, leaving only the "begin end." ( aka as main ), to another unit, then it works !
This is indeed one way of doing it.  The less obvious downside to this is if one wants to store progmem data, it will be inserted before .text. Fixing this would require even more changes to the custom linker script.

Your solution is more elegant because we can conditional define it using fpc_fullversion  . All is perfect !

( I'll make a PR soon :) )

regards,

 

TinyPortal © 2005-2018