Lazarus

Programming => Embedded => Operating Systems => Embedded - ARM => Topic started by: Mathias on August 06, 2020, 03:34:12 pm

Title: Arduino DUE
Post by: Mathias on August 06, 2020, 03:34:12 pm
I'm trying to build a flasher with the Arduino DUE, but unfortunately I don't see any reaction on the LED13.
Does anyone have a tip on what I'm doing wrong?

I tried to convert the following code from GITHUB to Lazarus.
https://github.com/marangisto/blink-sam3x8e

My cross compiler compiles everything smoothly.


Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$H-,J-,O-}
  4.  
  5. uses
  6.   cortexm3;
  7.  
  8.   procedure Delay;
  9.   var
  10.     i: uint32;
  11.   begin
  12.     for i := 0 to 1000000 do begin
  13.       asm
  14.                Nop end; // Leerbefehl
  15.     end;
  16.   end;
  17.  
  18. begin
  19.   PIOB.PER := $FFFFFFFF;
  20.   PIOB.OER := $FFFFFFFF;
  21.  
  22.   PIOB.SODR := 0;
  23.   repeat
  24.     PIOB.SODR := $FFFFFFFF;
  25.     Delay;
  26.     PIOB.CODR := $FFFFFFFF;
  27.     Delay;
  28.   until False;
  29. end.

Maybe I'm doing something wrong with loading too?
Code: [Select]
$ /home/tux/.arduino15/packages/arduino/tools/bossac/1.6.1-arduino/bossac --port=ttyACM0 --info -e -w -v -b Project1.bin -R
Atmel SMART device 0x285e0a60 found
Device       : ATSAM3X8
Chip ID      : 285e0a60
Version      : v1.1 Dec 15 2010 19:25:04
Address      : 524288
Pages        : 2048
Page Size    : 256 bytes
Total Size   : 512KB
Planes       : 2
Lock Regions : 32
Locked       : none
Security     : false
Boot Flash   : false
Erase flash
done in 0.033 seconds

Write 980 bytes to flash (4 pages)
Gleitkomma-Ausnahme (Speicherabzug geschrieben)

In the appendix the complete source.
Title: Re: Arduino DUE
Post by: MiR on August 06, 2020, 03:50:39 pm
I have very limited knowledge of the sam3x8e, so I cannot tell if your code is correct but one thing is that you flash using a bootloader and the bootloader will likely conflict with your image as both expect to run at the start of flash.
Have a look at this Forum post:

https://forum.lazarus.freepascal.org/index.php/topic,49243.0.html

on how to tweak the start address that fpc assumes.

In this example another bootloader for an STM32 was used so you will likely have to exchange the address used there with some offset to the start of flash on sam3x8e

Found this link:

http://www.inspirel.com/articles/Ada_On_Cortex_Linking_And_Booting.html

try 0x00080000, looks like a promising start for experiments....
Title: Re: Arduino DUE
Post by: MiR on August 06, 2020, 04:25:55 pm
Found this link to enable a half-decent, real debugger:

https://github.com/lanserge/at16u2_cmsis_dap

Also found an Arduino Due board in my 'Grabbelkiste', trying to connect it via JLink Debugger, will keep you posted.

Michael
Title: Re: Arduino DUE
Post by: Mathias on August 06, 2020, 04:51:52 pm
Quote
try 0x00080000, looks like a promising start for experiments....


I have now entered the following in Lazarus under "Settings for Project / Compiler Settings / Custom Settings".
Code: [Select]
-WpATSAM3X8E
-k-Ttext=0x00080000
Unfortunately, nothing has changed.

I don't see any change in the output of bossac either.



I tried it with "-k-Ttext = 0x00000000", then an interesting error message comes from the compiler.
Code: Pascal  [Select][+][-]
  1. ...
  2. Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: address 0x368 of /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/Blink_Pin13/Project1.elf section `.text' is not within region `flash'
  3. Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/Blink_Pin13/Project1.elf section `.data' will not fit in region `flash'
  4. Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: address 0x368 of /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/Blink_Pin13/Project1.elf section `.text' is not within region `flash'
  5. Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: region `flash' overflowed by -785452 bytes
  6. Project1.pas(49,0) Error: Error while linking



Title: Re: Arduino DUE
Post by: MiR on August 06, 2020, 05:47:15 pm
This code works for me, in the Debugger I saw that enabling all port bits crashed the debugger.
Explicitly enabling only the led port did the job.
Enabling the clock for the pio is not needed (I guess, this is what it says in the reference Manual), I did it anyway as not everything works when pio clock is disabled.

The setting of the text segment is correct as is, you do not need to change that, for me the default worked out of the box.

I jaw in your project file that you also own an stlink. If you are serious about playing with this cpu i'd convert it to a black magic probe so that you can do real debugging.

In the debugger i saw the issue immediately.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$H-,J-,O-}
  4.  
  5. uses
  6.   cortexm3;
  7.  
  8.   procedure Delay;
  9.   var
  10.     i: uint32;
  11.   begin
  12.     for i := 0 to 1000000 do begin
  13.       asm
  14.                Nop end; // Leerbefehl
  15.     end;
  16.   end;
  17.  
  18.   procedure Delay_ms(x: Int32);
  19.   var
  20.     y: Int32;
  21.   begin
  22.     y := RTT.VR + x;
  23.     while y >= RTT.VR do begin
  24.     end;
  25.   end;
  26.  
  27. const
  28.   led = 1 shl 27;
  29.  
  30. begin
  31.   PMC.PCER0 := 1 shl 12;
  32.   PIOB.PER := 1 shl 27;
  33.   PIOB.OER := 1 shl 27;
  34.  
  35.   repeat
  36.     // Pin13 -- High
  37.     PIOB.SODR := 1 shl 27;
  38.     Delay;
  39.  
  40.     // Pin13 -- Low
  41.     PIOB.CODR := 1 shl 27;
  42.     Delay;
  43.   until False;
  44. end.
  45.  
Title: Re: Arduino DUE
Post by: Mathias on August 06, 2020, 06:22:07 pm
I have adopted your code 1: 1, this also does not work.

I also uploaded your hex once, there is the following output:

Code: [Select]
$ /home/tux/.arduino15/packages/arduino/tools/bossac/1.6.1-arduino/bossac --port=ttyACM0 --info -e -w -v -b test.hex -R
Atmel SMART device 0x285e0a60 found
Device       : ATSAM3X8
Chip ID      : 285e0a60
Version      : v1.1 Dec 15 2010 19:25:04
Address      : 524288
Pages        : 2048
Page Size    : 256 bytes
Total Size   : 512KB
Planes       : 2
Lock Regions : 32
Locked       : none
Security     : false
Boot Flash   : false
Erase flash
done in 0.033 seconds

Write 2893 bytes to flash (12 pages)
[==============================] 100% (12/12 pages)
done in 0.573 seconds

Verify 2893 bytes of flash
[==============================] 100% (12/12 pages)
Verify successful
done in 0.545 seconds
Set boot flash true
CPU reset.
Nothing happens there, except that I have to reset the Com-Port, who I upload from the Arduino IDE.

The ST-Link line is still a mess because I modified an example from an ST32.
Title: Re: Arduino DUE
Post by: MiR on August 06, 2020, 09:17:46 pm
I played arround a while with bossac and then gave up, the problem seems to be that I am too stupid to use that tool and sam-ba is even worse.

With my trusty JLink things are flashed in a second and all works as expected, with bossac I am unable to flash the device.

Part of the problem may be that bossac (I think) only supports bin files, when I write a hey file the flash memory looks like it is filled with the ascii chars of the hex file.

There are also several different bossac versions, a special version that comes with Arduino and latest/greatest which is V1.9.1

commandline I use was:

bossac -i --port=cu.usbserial-1440 -U false -e -w -v -b Project1.bin -R

but I tried a lot of other versions.

Sometimes I thought I had it running, then I redid and it stopped working.

Perhaps you have more luck, attaching my working bin file
Title: Re: Arduino DUE
Post by: Mathias on August 07, 2020, 09:45:11 am
I've tried your Project.bin in your attachment. With multiple uploads, it works in between. bassac I used the 1.9.1.

Can't you upload the sources of your project with?
I never got it done with my project.

From the looks of it, a DUE is a pretty annoying part.

With the following code, I can reset the COM port:

Code: Pascal  [Select][+][-]
  1. var
  2.   SerialHandle: TSerialHandle;
  3. begin
  4.   SerialHandle := SerOpen('/dev/ttyACM0');
  5.   SerSetParams(SerialHandle, 1200, 8, NoneParity, 1, []);
  6.  
  7.   SerSetDTR(SerialHandle, True);
  8.   SerSetDTR(SerialHandle, False);
  9.   Sleep(500);
  10.   SerClose(SerialHandle);
  11.   Sleep(500);  


This command also looks a bit strange:  --port=cu.usbserial-1440


Quote
Part of the problem may be that bossac (I think) only supports bin files, when I write a hey file the flash memory looks like it is filled with the ascii chars of the hex file.
Funnily enough, he even uploads a Project.pas  %)
Title: Re: Arduino DUE
Post by: MiR on August 07, 2020, 10:01:18 am
Source attached....

I used bossash to better see what is going on, there I can flash, verify and dump the code:

Code: Pascal  [Select][+][-]
  1. bossa> connect /dev/cu.usbmodem14401
  2. Connected to device on /dev/cu.usbmodem14401
  3. bossa> erase
  4. Erase flash
  5. Flash is erased
  6. bossa>  dump 0x80000 0x100
  7.             0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  8. 00080000 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  9. 00080010 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  10. 00080020 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  11. 00080030 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  12. 00080040 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  13. 00080050 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  14. 00080060 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  15. 00080070 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  16. 00080080 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  17. 00080090 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  18. 000800a0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  19. 000800b0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  20. 000800c0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  21. 000800d0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  22. 000800e0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  23. 000800f0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������
  24. bossa> write Project1.bin
  25. Write 521 bytes to flash (3 pages)
  26. [==============================] 100% (3/3 pages)
  27. Write successful
  28. bossa>  dump 0x80000 0x100
  29.             0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  30. 00080000 | 00 00 01 20 89 01 08 00 85 01 08 00 00 00 00 00 | ... ............
  31. 00080010 | 85 01 08 00 85 01 08 00 85 01 08 00 00 00 00 00 | ................
  32. 00080020 | 00 00 00 00 00 00 00 00 00 00 00 00 85 01 08 00 | ................
  33. 00080030 | 85 01 08 00 00 00 00 00 85 01 08 00 85 01 08 00 | ................
  34.  

I found a clone with CH340 chip instead of the original, there I sometimes see that after flashing the memory is dumped as all zeros, looks like the chip is not properly connected at that point but there is no error message at all.

I can flash the code via bossash, then connect with my Jlink and the code works, so I guess flashing is ok and there is some magic ingredience missing in the code. I see in the debugger that WDT is disabled, perhaps it is worth disabling it in code, too, but I doubt it.

I fear that you will have to look through ther initialization in the C Header files to find that magic missing part.
Title: Re: Arduino DUE
Post by: MiR on August 07, 2020, 11:18:34 am
From the looks of it, a DUE is a pretty annoying part.

It is by far not my favorite path (I do not like the way that atmel Reference Manuals are written) but the problems we see right now have nothing to do with the chip itself.

By the way,

/dev/cu.usbmodem14401

is the serial device of the Arduino Due on my mac.
Title: Re: Arduino DUE
Post by: MiR on August 07, 2020, 01:49:02 pm
I think I got it right now using bossash:

I have USB connected to the programming port, then I press erase button and connect via bossash 1.9.1: (remember to replace connect with the matching command for linux-device)

Code: [Select]
./bossash
Press Ctrl-D or enter "exit" to end session.
Enter "help" to display a command list.
bossa>  connect /dev/cu.usbmodem14401
Connected to device on /dev/cu.usbmodem14401
bossa>  dump 0x80000 0x8
            0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00080000 | ff ff ff ff ff ff ff ff                         | ��������
bossa>  write Project1.bin
Write 541 bytes to flash (3 pages)
[==============================] 100% (3/3 pages)
Write successful
bossa>  dump 0x80000 0x10
            0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00080000 | 00 00 01 20 91 01 08 00 8d 01 08 00 00 00 00 00 | ... ............
bossa> bootf true
Boot to flash flag set to true
bossa> options
bossa>  reset

I guess starting from here it should be possible (for you) to find a way to consistently do the same things with bossac.

Or buy a JLink Edu Mini for €20 and enjoy real flashing and debugging....

attached my code, fex experiments inside, I guess the real winner was using the 'options' command to actually save the boot from flash mode.....
Title: Re: Arduino DUE
Post by: Mathias on August 07, 2020, 05:13:57 pm
When I just switched on my PC earlier, I was able to upload the project of your last post right away. Only the LED flashed every 30 seconds.
After later attempts it worked again.

With an ST32 you enter 0x8000000, doesn't the DUO also need something similar?
Code: [Select]
st-flash write stm32Blink.bin 0x8000000When I look at the following file in the FPC sources, line 1024, the DUO also has a flasbase value.
/home/tux/fpcupdeluxe_stm32/fpcsrc/compiler/arm/cpuinfo.pas

Something else, why doesn't the Arduino IDE bother whom dor bossac is called?
Title: Re: Arduino DUE
Post by: MiR on August 07, 2020, 06:01:26 pm
The Flash offset of the Arduino Due is $00080000 this is what is also set in cpuinfo.pas:

      (controllertypestr:'ATSAM3X8E';     controllerunitstr:'SAM3X8E'; cputype:cpu_armv7m; fputype:fpu_soft; flashbase:$00080000; flashsize:$00040000; srambase:$20000000; sramsize:$00010000),
      (controllertypestr:'ARDUINO_DUE';   controllerunitstr:'SAM3X8E'; cputype:cpu_armv7m; fputype:fpu_soft; flashbase:$00080000; flashsize:$00040000; srambase:$20000000; sramsize:$00010000),

so there is no reason to set an offset for compilation.

What I saw is that in the cases where the app would not start the debugger stopped inside of the boot-rom, this is why I investigated if the boot option is properly set.

I guess what you can also do is to flash with bossac, then start bossash, set the flash boot, do option and reset. Chances are good that after this the app runs. for me it worked every time I flashed with the bossash shell steps I described.

Why it does not work for Arduino Due directly with bossac.... I have no clue....
Title: Re: Arduino DUE
Post by: Mathias on August 08, 2020, 02:12:28 pm
Somehow I have the feeling that Arduino is building the bin file very differently than FPC does.
See the following editions:

Arduino:
Code: [Select]
$ /home/tux/.arduino15/packages/arduino/tools/bossac/1.6.1-arduino/bossac --port=ttyACM0 -U false -e -w -v -b /tmp/arduino_build_309523/Blink.ino.bin -R
Atmel SMART device 0x285e0a60 found
Erase flash
done in 0.037 seconds

Write 11864 bytes to flash (47 pages)
[==============================] 100% (47/47 pages)
done in 2.294 seconds

Verify 11864 bytes of flash
[==============================] 100% (47/47 pages)
Verify successful
done in 2.122 seconds
Set boot flash true
CPU reset.
Project.bin:
Code: [Select]
/n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/von_MIR$ /home/tux/.arduino15/packages/arduino/tools/bossac/1.6.1-arduino/bossac --port=ttyACM0 -U false -e -w -v -b /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/von_MIR/Project1.bin -R
Atmel SMART device 0x285e0a60 found
Erase flash
done in 0.037 seconds

Write 552 bytes to flash (3 pages)
Gleitkomma-Ausnahme (Speicherabzug geschrieben)
Title: Re: Arduino DUE
Post by: Mathias on August 08, 2020, 03:03:15 pm
Which is also strange, if I try to upload the "am" from Arduino with bassac 1.9.1 it doesn't work either.
Bassac 1.6.1 works without any problems. Assuming I reset the com port beforehand.

To make it easier, I've written a utility program:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DUEClick(Sender: TObject);
  2. const
  3.   bos161 = '/home/tux/.arduino15/packages/arduino/tools/bossac/1.6.1-arduino/bossac';
  4.   bos191 = '/home/tux/Schreibtisch/BOSSA-1.9.1/bin/bossac';
  5.   binMIR = '/n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/von_MIR/Project1.bin';
  6.   binArduino = '/tmp/arduino_build_303924/Blink.ino.bin';
  7.   com = 'ttyACM0';
  8. var
  9.   SerialHandle: TSerialHandle;
  10. begin
  11.   if not Assigned(RunCommandForm) then begin
  12.     RunCommandForm := TRun_Command_Form.Create(nil);
  13.   end;
  14.  
  15.   SerialHandle := SerOpen('/dev/' + com);
  16.   SerSetParams(SerialHandle, 1200, 8, NoneParity, 1, []);
  17.  
  18.   SerSetDTR(SerialHandle, True);
  19.   SerSetDTR(SerialHandle, False);
  20.   Sleep(500);
  21.   SerClose(SerialHandle);
  22.   Sleep(500);
  23.  
  24. //  RunCommandForm.RunCommand(bos191 + ' --port=' + com + ' -U false -e -w -v -b ' + binArduino + ' -R');
  25.   RunCommandForm.RunCommand(bos191 + ' -e -w -v -b ' + binArduino + ' -R');
  26.  
  27. end;  

What is also strange, if I try to upload the "am" from Arduino with bossac 1.9.1, it doesn't work either.
With bossac 1.6.1 there is no problem. Assuming I reset the com port beforehand.

To make it easier, I've written a utility program:

By the way, whoever I try with sleober (Arduino plugin for Eclipse) doesn't want the DUE either, although bossac 1.6.1 is used there.
Title: Re: Arduino DUE
Post by: Mathias on August 09, 2020, 01:40:04 pm
Thanks to this post I have come a step further: https://github.com/Sloeber/arduino-eclipse-plugin/issues/892

I have now taken version 1.7.0 of bossac, the upload worked right away, even after several attempts.

But unfortunately it still has a very big shortcoming. As soon as I disconnect the DUE from the power supply and reconnect it, the program no longer starts and the LED no longer flashes.
Only when I upload again will it work again.  ::)

And one more thing, uploading is only possible at the programming port.

But at least you can now test at least a few programs on the DUO. ;)

I use the following command to upload:
Code: [Select]
xxx/BOSSA-1.7.0/bin/bossac -e -w -v -b Project1.bin -R
Thanks to this post I have come a step further:

I have now taken version 1.7.0 of bossac, the upload worked right away, even after several attempts.

But unfortunately it still has a very big shortcoming. As soon as I disconnect the DUE from the power supply and reconnect it, the program no longer starts and the LED no longer flashes.
Only when I upload again will it work again.

And one more thing, uploading is only possible at the programming port.

But at least you can now test at least a few programs on the DUO.

I use the following command to upload:

Another thing to note is that the Com port must be reset via DTS, as is the case in the last code.
Whatever else, if you reset the port after uploading, the program will also be stopped, as with the current removal.
Title: Re: Arduino DUE
Post by: MiR on August 09, 2020, 06:18:07 pm
Version 1.7 has the disadvantage that there is no precompiled x64 version for Mac available.

So if you you look for doing something that benefits everybody then 1.8 is the minimum version.
Title: Re: Arduino DUE
Post by: Mathias on August 09, 2020, 06:29:42 pm

I did not test the 1.8.  Somehow I overlooked them.  :-X
Title: Re: Arduino DUE
Post by: Mathias on August 10, 2020, 04:41:44 pm
Version 1.7 has the disadvantage that there is no precompiled x64 version for Mac available.

So if you you look for doing something that benefits everybody then 1.8 is the minimum version.
I have tested the 1.8, only an uninclusive error message.

Code: [Select]
$ /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/bossac/BOSSA-1.8.0/bin/bossac --port=ttyACM0 -i

SAM-BA operation failed
With the 1.7, the information from the CPU comes here.

I also observed something. I wrote yesterday. That with the current removal of the DUE, the program no longer starts.
But when I switched on the PC earlier, the LED was blinking.
Let's see if it will be the same next time.
Title: Re: Arduino DUE
Post by: Mathias on August 15, 2020, 04:04:09 pm
Quote
I also observed something. I wrote yesterday. That with the current removal of the DUE, the program no longer starts.
But when I switched on the PC earlier, the LED was blinking.
Let's see if it will be the same next time.

This is understandable. The PC must first be disconnected from the power supply. Just a soft off / on via the power button is of no use.
Title: Re: Arduino DUE
Post by: Mathias on April 18, 2022, 05:14:44 pm
Code: [Select]
[quote]What is also strange, if I try to upload the "am" from Arduino with bossac 1.9.1, it doesn't work either.[/quote]

With the 1.9.1 it also works halfway. You simply have to press the reset button after each upload.
Unfortunately, you also have to do this if you disconnect the DUE from the power supply and reconnect it. Which of course is stupid, you want the program to start immediately if there is a power failure.

By the way, my Linux Mint comes with bossac 1.9.1 by default, you just have to install it as follows:

[codel]sudo apt-get install bossac
Title: Re: Arduino DUE
Post by: Mathias on May 04, 2022, 05:31:31 pm
Now I've almost managed it, now the program starts immediately after uploading, it also starts immediately as soon as the DUE gets power.
As a test program, I used a simple blinker.

But now the DUE has a very strange shortcoming. The program runs for about 10 seconds, then it pauses, also for about 10 seconds, then it starts again, etc.

I'm using Bossac 1.9.1 now

Does anyone have any advice?

The DUE now has the shortcoming with the following parameters:

Code: [Select]
/bin/bossac -e -b -s -R -v -w Project1.bin   
TinyPortal © 2005-2018