Arduino DUE lazarus / 79-Embedded - ARM ---------------------------------------------------------------------- Mathias · 2020-08-06 13:34 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$H-,J-,O-} uses  cortexm3;   procedure Delay;  var    i: uint32;  begin    for i := 0 to 1000000 do begin      asm             Nop end; // Leerbefehl    end;  end; begin  PIOB.PER := $FFFFFFFF;  PIOB.OER := $FFFFFFFF;   PIOB.SODR := 0;  repeat    PIOB.SODR := $FFFFFFFF;    Delay;    PIOB.CODR := $FFFFFFFF;    Delay;  until False;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. ---------------------------------------------------------------------- MiR · 2020-08-06 13:50 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.htm l try 0x00080000, looks like a promising start for experiments.... ---------------------------------------------------------------------- MiR · 2020-08-06 14:25 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 ---------------------------------------------------------------------- Mathias · 2020-08-06 14:51 Quotetry 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=0x00080000Unfortunately, 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: address 0x368 of /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedde d/ARM/Arduino_DUE/Blink_Pin13/Project1.elf section `.text' is not within region `flash'Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm- embedded/arm-none-eabi-ld: /n4800/DATEN/Programmierung/Lazarus/Tutoria ls/Embedded/ARM/Arduino_DUE/Blink_Pin13/Project1.elf section `.data' will not fit in region `flash'Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: address 0x368 of /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedde d/ARM/Arduino_DUE/Blink_Pin13/Project1.elf section `.text' is not within region `flash'Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm- embedded/arm-none-eabi-ld: region `flash' overflowed by -785452 bytesProject1.pas(49,0) Error: Error while linking ---------------------------------------------------------------------- MiR · 2020-08-06 15:47 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$H-,J-,O-} uses  cortexm3;   procedure Delay;  var    i: uint32;  begin    for i := 0 to 1000000 do begin      asm             Nop end; // Leerbefehl    end;  end;   procedure Delay_ms(x: Int32);  var    y: Int32;  begin    y := RTT.VR + x;    while y >= RTT.VR do begin    end;  end; const  led = 1 shl 27; begin  PMC.PCER0 := 1 shl 12;  PIOB.PER := 1 shl 27;  PIOB.OER := 1 shl 27;   repeat    // Pin13 -- High    PIOB.SODR := 1 shl 27;    Delay;     // Pin13 -- Low    PIOB.CODR := 1 shl 27;    Delay;  until False;end. ---------------------------------------------------------------------- Mathias · 2020-08-06 16:22 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. ---------------------------------------------------------------------- MiR · 2020-08-06 19:17 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 ---------------------------------------------------------------------- Mathias · 2020-08-07 07:45 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  SerialHandle: TSerialHandle;begin  SerialHandle := SerOpen('/dev/ttyACM0');  SerSetParams(SerialHandle, 1200, 8, NoneParity, 1, []);   SerSetDTR(SerialHandle, True);  SerSetDTR(SerialHandle, False);  Sleep(500);  SerClose(SerialHandle);  Sleep(500);   This command also looks a bit strange:  --port=cu.usbserial-1440 QuotePart 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 ---------------------------------------------------------------------- MiR · 2020-08-07 08:01 Source attached.... I used bossash to better see what is going on, there I can flash, verify and dump the code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bossa> connect /dev/cu.usbmodem14401Connected to device on /dev/cu.usbmodem14401bossa> eraseErase flashFlash is erasedbossa>  dump 0x80000 0x100            0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f00080000 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������00080010 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������00080020 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������00080030 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������00080040 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������00080050 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������00080060 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������00080070 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������00080080 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������00080090 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������000800a0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������000800b0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������000800c0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������000800d0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������000800e0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������000800f0 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff | ����������������bossa> write Project1.binWrite 521 bytes to flash (3 pages)[==============================] 100% (3/3 pages)Write successfulbossa>  dump 0x80000 0x100            0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f00080000 | 00 00 01 20 89 01 08 00 85 01 08 00 00 00 00 00 | ... ............00080010 | 85 01 08 00 85 01 08 00 85 01 08 00 00 00 00 00 | ................00080020 | 00 00 00 00 00 00 00 00 00 00 00 00 85 01 08 00 | ................00080030 | 85 01 08 00 00 00 00 00 85 01 08 00 85 01 08 00 | ................  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. ---------------------------------------------------------------------- MiR · 2020-08-07 09:18 Quote from: Mathias on August 07, 2020, 09:45:11 amFrom 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. ---------------------------------------------------------------------- MiR · 2020-08-07 11:49 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..... ---------------------------------------------------------------------- Mathias · 2020-08-07 15:13 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? ---------------------------------------------------------------------- MiR · 2020-08-07 16:01 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.... ---------------------------------------------------------------------- Mathias · 2020-08-08 12:12 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/bo ssac/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/A RM/Arduino_DUE/von_MIR$ /home/tux/.arduino15/packages/arduino/tools/bo ssac/1.6.1-arduino/bossac --port=ttyACM0 -U false -e -w -v -b /n4800/D ATEN/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) ---------------------------------------------------------------------- Mathias · 2020-08-08 13:03 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.DUEClick(Sender: TObject);const  bos161 = '/home/tux/.arduino15 /packages/arduino/tools/bossac/1.6.1-arduino/bossac';  bos191 = '/home/tux/Schreibtisch/BOSSA-1.9.1/bin/bossac';  binMIR = '/n4800/DAT EN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/von_MIR/P roject1.bin';  binArduino = '/tmp/arduino_build_303924/Blink.ino.bin';  com = 'ttyACM0';var  SerialHandle: TSerialHandle;begin  if not Assigned(RunCommandForm) then begin    RunCommandForm := TRun_Command_Form.Create(nil);  end;   SerialHandle := SerOpen('/dev/' + com);  SerSetParams(SerialHandle, 1200, 8, NoneParity, 1, []);   SerSetDTR(SerialHandle, True);  SerSetDTR(SerialHandle, False);  Sleep(500);  SerClose(SerialHandle);  Sleep(500); //  RunCommandForm.RunCommand(bos191 + ' --port=' + com + ' -U false -e -w -v -b ' + binArduino + ' -R');  RunCommandForm.RunCommand(bos191 + ' -e -w -v -b ' + binArduino + ' -R'); 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. ---------------------------------------------------------------------- Mathias · 2020-08-09 11:40 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. ---------------------------------------------------------------------- MiR · 2020-08-09 16:18 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. ---------------------------------------------------------------------- Mathias · 2020-08-09 16:29 I did not test the 1.8.  Somehow I overlooked them. ---------------------------------------------------------------------- Mathias · 2020-08-10 14:41 Quote from: MiR on August 09, 2020, 06:18:07 pmVersion 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. ---------------------------------------------------------------------- Mathias · 2020-08-15 14:04 QuoteI 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. ---------------------------------------------------------------------- Mathias · 2022-04-18 15:14 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 ---------------------------------------------------------------------- Mathias · 2022-05-04 15:31 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