Recent

Author Topic: FPC/Lazarus Arduino Tutorial  (Read 38945 times)

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #30 on: February 02, 2016, 08:46:36 pm »
Hitting a bit of a wall here. Using Arduino's blink sketch and with Verbose mode on the Arduino IDE it shows this line for launching avrdude with the blink in the Mega.

Code: [Select]
D:\_Programs\_ArduinoIDE\hardware\tools\avr/bin/avrdude -CD:\_Programs\_ArduinoIDE\hardware\tools\avr/etc/avrdude.conf -v -patmega2560 -cwiring -PCOM5 -b115200 -D -Uflash:w:C:\Users\admin\AppData\Local\Temp\build5676851336824123120.tmp/Blink.cpp.hex:i

Using that line manually in a Command Prompt, it uploads and gives the same results output as when the IDE did it. BUT, nothing happens. No blink and no clearing of blink uploaded by the IDE. So, it seems there is a command switch that the Arduino is supplying that I am not.

very weird.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

jmpessoa

  • Hero Member
  • *****
  • Posts: 2302
Re: FPC/Lazarus Arduino Tutorial
« Reply #31 on: February 02, 2016, 09:40:10 pm »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #32 on: February 02, 2016, 10:33:14 pm »
https://typeunsafe.wordpress.com/2011/07/22/programming-arduino-with-avrdude/

Thanks, but where he gets to "Magic Words" is where I got to this morning. That command line will program the Uno fine, but not the Nano or Mega.

The other thing is he is addressing the USBPort directly instead of the COM port as he is using a Linux-like system (dmesg is Linux) and with windows the com ports change when they are identified.

Thanks for trying though. I have not yet given up it is sooooo close. That fact that it works with a Uno (and a $5 clone at that) means we are close.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #33 on: February 02, 2016, 11:44:07 pm »
I am beginning to think it is the .hex file. I have managed to use avrdude and the ArduinioIDE "blink.cpp.hex" to upload to the Mega just fine via the Command Line.

D:\Blinky>avrdude -CD:\_Programs\_ArduinoIDE\hardware\tools\avr/etc/avrdude.conf
 -v -patmega2560 -cwiring -P\\.\COM5 -b115200 -D -Uflash:w:D:\Blinky\Blink.cpp.h
ex:i

If I use "Blinky.hex" in place of "Blink.cpp.hex" it doesn't work. No combinations of names work with the Lazarus file.

BUT having said that "Blinky.hex" does upload OK to a Uno and makes it blink. Still the Nano and Mega causing the problems.

Enough for today. I am beginning to feel like Einstein's interpretation of lunacy.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: FPC/Lazarus Arduino Tutorial
« Reply #34 on: February 03, 2016, 12:46:48 am »
I am beginning to think it is the .hex file.

I believe you are right. The process was to show that FPC is able to compile for AVR, and it was specific for Arduino Uno (ATMega328P) as the main post explains:
Quote
In above scripts, notice "avr5" subarch flag. This may be different for other Atmel chips, in FreePascal sources open to "/rtl/embedded/Makefile" to see what chip corresponds to each subarch. For Arduino Uno and ATMega328P, this is "avr5".

For Arduino Mega (atmega2560) you need another compiler using avr6 for SUBARCH flag:
Quote
ifeq ($(SUBARCH),avr6)
CPU_UNITS=atmega2561 atmega2560
CPU_UNITS_DEFINED=1
endif

I just edited my post to reflect this detail.

So, it seems there is a command switch that the Arduino is supplying that I am not.

In this case, I don't think there is any hidden switch. In general, whenever I get a similar feeling I resort to using a fake program -replacing the original one- to show me the command line parameters:
Code: Pascal  [Select][+][-]
  1. uses
  2.     Windows;
  3. ...
  4.  
  5. procedure TMainForm.FormCreate(Sender: TObject);
  6. var
  7.   I: Integer;
  8.  
  9. begin
  10.   LogMemo.Lines.Add(Format('Program: %s', [ParamStr(0)]));
  11.   for I := 1 to ParamCount do
  12.     LogMemo.Lines.Add(Format('Param %d : %s', [I, ParamStr(I)]));
  13.  
  14.   LogMemo.Lines.Add(GetCommandLine);
  15. end;

Edit:
As for Arduino Nano (ATmega168/ATmega328P) it seems to me that it should work with avr5.
« Last Edit: February 03, 2016, 01:19:08 am by engkin »

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #35 on: February 03, 2016, 04:01:27 pm »
I believe you are right. The process was to show that FPC is able to compile for AVR, and it was specific for Arduino Uno (ATMega328P) as the main post explains:
Quote

The avrdude will display all of it's parameters when run with no parameters and I mentioned they are case-sensitive so I'd like to retract that prior thought. :)

What I do, (being an old DOS guy) us

avrdude > out.txt

Which pipes it's output to the text file.

I agree that the Nano has the same Atmel chip, but it has a different serial bootloader. But I saw a youtube video of Julian Lett (I think) programming a Nano with the the Uno bootloader thus turning it into a Uno-like thing. I will try that as I have an ISP prorammer here somewhere. The Nano cost 5-bucks and if it never loads again I will not be crying.

If that fakes avrdude into thinking it is a Nano then I have won as the Uno bootloader is smaller and more efficient than the stock one on the Nano.

Moor to come soon.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #36 on: February 03, 2016, 04:23:23 pm »
I'm not actually sure why you bother with this...

Clearly you haven't read the entire thread.
1: I have been programming in Pascal for almost 40-years.
2: I am retired so I have plenty of time on my hands.
3: I like free stuff and helping to make it work to thwart stupidly high software prices.
4: As Julius Summner Miller used to quote, "There is no crime in not knowing, it is not wanting to know that is the crime."

The C/C++ offered by Arduino software is much more easier to learn than configure FPC/Lazarus properly in order to make workable .hex file from this experimental attempt to support AVRs. 

Obviously you have already managed this with Lazarus otherwise how would you know how long it takes to achieve success?
Please share your experiences and save us all this fun.
Besides, the Lazarus part is now working at least for the Uno which is a mile ahead of where it was when this process started.
Dare I suggest a conflict of interests in getting a FREE working Pascal version of the Arduiono compiler? :o

Experimentation is how things are done in the real world.
If everyone adhered to only what was available you would still be clubbing a buffalo to death for meat.


board (actually ATmega2560 itself) can be blocked refusing further communication with avrdude

And, I will cry myself to sleep each night for a week over that $5 loss.

If you want to use Pascal anyway, you may start with  Mikroelektronika's AVR Pascal compiler.

Hmmm, let me think about that one. Lazarus cost $0.00, your software cost $250.00. :o :o :o 

At least we know it can be done and there is no greater impetus for experimenters than an existing example of an expensive something to replicate.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: FPC/Lazarus Arduino Tutorial
« Reply #37 on: February 03, 2016, 04:52:26 pm »
Hey, it means almost R$ 20,00 + importation fees + some weeks to me... so I would cry a little bit in your position. Just keep doing your stuff dude, you are awesome!

Quote
And, I will cry myself to sleep each night for a week over that $5 loss.
Be mindful and excellent with each other.
https://github.com/cpicanco/

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: FPC/Lazarus Arduino Tutorial
« Reply #38 on: February 04, 2016, 04:50:33 pm »
Great success.  :D :D :D

Using a USBasp programmer and the instructions from Julian's video,
https://www.youtube.com/watch?v=bpXUvRr2ywA

It turned the Nano into a Uno and now the Blinky.hex uploads and runs like a champ. I could not get the programming to work with the USBtiny that Julian used as I got an error "The ID code is invalid, use -F to override." I was using 1.6.7 Arduino IDE (Julian showed 1.6.5) so maybe they have changed something but using the USBasp it just hammered past it and did my bidding.

I am OK with that and will cycle all of my Nano through with the USBasp and make them all Uno. :)

It seems that Blinky.hex needs to be tweaked to suit the boot-loader of the device. By over-writing the Nano boot-loader with the Uno boot-loader, it was happy. A little later on, I will see what I can do with the Mega, but for now I am back in business with Lazarus and uploading to both Nano and Uno.

One thing I will note is that, as the OP mentions within the Blinky.lpr file, the delay is "some kind" as the LED flashes a little faster than the standard "Blink" program. That 400000 probably needs to be 500000. I may play with that, but it is close to what a second looks like compare to the "Blink" sketch. Probably close enough.

Code: [Select]
// This function simulates some kind of delay by looping.
procedure SomeDelay;
var
  I: LongInt;
begin
  for I := 0 to 400000 do
    Dec(DelayVar);
end;
« Last Edit: February 04, 2016, 04:52:44 pm by HatForCat »
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

jmpessoa

  • Hero Member
  • *****
  • Posts: 2302
Re: FPC/Lazarus Arduino Tutorial
« Reply #39 on: February 12, 2016, 07:58:30 pm »
Hello All!

I want to make available in two or three days a new wizard of the "Lamw's" family: a  little brother "Lamwino: Lazarus Arduino Module Wizard" .

key features:

1.Builder for FPC avr/arduino cross compiler
2.New Project option:  "Arduino [avr] Module"

My request. Please, someone could create a new "Arduino" Operating Systems child board?

Thanks to All!

Edited: DONE!

Lamwino: Lazarus Arduino Module Wizard:
   Version 0.1      

   "A wizard to create Arduino loadable module (.hex) using Lazarus/Free Pascal"

         ref.
      https://github.com/jmpessoa/lazarduinomodulewizard

      http://forum.lazarus.freepascal.org/index.php?topic=31513.msg201993
« Last Edit: February 14, 2016, 10:37:24 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

herux

  • Full Member
  • ***
  • Posts: 102
Re: FPC/Lazarus Arduino Tutorial
« Reply #40 on: March 24, 2016, 08:41:49 am »
Hello all, I am going to write a tutorial build fpc for avr embedded in OSX. Completing the tutorial in this thread that only for windows and linux. here is the step i did:
  • Download and install avrdude, here https://www.arduino.cc/en/Main/Donate
  • Make sure you are running up-to-date Trunk versions of both FreePascal and Lazarus.
  • Install avr binutil using homebrew
    Tap the repository, type
Code: Bash  [Select][+][-]
  1. brew tap osx-cross/avr
Install updated version, type:
Code: Bash  [Select][+][-]
  1. brew install avr-libc
avr binutil will be installed in the folder /usr/local/Cellar/avr-binutils

  • Go to folder of your fpc source and type:
Code: Bash  [Select][+][-]
  1. make clean crossall  OS_TARGET=embedded CPU_TARGET=avr SUBARCH=avr5 INSTALL_PREFIX=/usr/local CROSSBINDIR=/usr/local/Cellar/avr-binutils/2.25/bin/ BINUTILSPREFIX=avr- CROSSOPT="-O3 -XX -CX"
  2.  

  • type:
Code: Bash  [Select][+][-]
  1. sudo make crossinstall OS_TARGET=embedded CPU_TARGET=avr SUBARCH=avr5
if successful install, terminal output will appear similar to this
Code: Bash  [Select][+][-]
  1. ....
  2. Skipped package regexpr which has been disabled for target avr-embedded
  3. Skipped package rexx which has been disabled for target avr-embedded
  4. Skipped package rtl-console which has been disabled for target avr-embedded
  5. Skipped package rtl-extra which has been disabled for target avr-embedded
  6. Skipped package rtl-objpas which has been disabled for target avr-embedded
  7. Skipped package rtl-unicode which has been disabled for target avr-embedded
  8. Skipped package sdl which has been disabled for target avr-embedded
  9. Skipped package sqlite which has been disabled for target avr-embedded
  10. Skipped package svgalib which has been disabled for target avr-embedded
  11. Skipped package symbolic which has been disabled for target avr-embedded
  12. Skipped package syslog which has been disabled for target avr-embedded
  13. Skipped package tcl which has been disabled for target avr-embedded
  14. Skipped package univint which has been disabled for target avr-embedded
  15. Skipped package unixutil which has been disabled for target avr-embedded
  16. Skipped package unzip which has been disabled for target avr-embedded
  17. Skipped package users which has been disabled for target avr-embedded
  18. Skipped package utmp which has been disabled for target avr-embedded
  19. Skipped package uuid which has been disabled for target avr-embedded
  20. Skipped package winceunits which has been disabled for target avr-embedded
  21. Skipped package winunits-base which has been disabled for target avr-embedded
  22. Skipped package winunits-jedi which has been disabled for target avr-embedded
  23. Skipped package x11 which has been disabled for target avr-embedded
  24. Skipped package xforms which has been disabled for target avr-embedded
  25. Skipped package zlib which has been disabled for target avr-embedded
  26. Skipped package libenet which has been disabled for target avr-embedded
  27. Skipped package zorba which has been disabled for target avr-embedded
  28. Skipped package googleapi which has been disabled for target avr-embedded
  29. Skipped package fcl-pdf which has been disabled for target avr-embedded
  30. Installing package fpc-all

  • Create symlink for ppcrossavr in /usr/local/bin, type:
Code: Bash  [Select][+][-]
  1. sudo ln -s -f /usr/local/lib/fpc/3.1.1/ppcrossavr /usr/local/bin/ppcavr

  • edit fpc.cfg
Code: Bash  [Select][+][-]
  1. sudo nano /etc/fpc.cfg

and add something like this
Code: Bash  [Select][+][-]
  1. #IFDEF embedded
  2. -OoFastMath
  3. -XX
  4. -CX
  5. -OoNoStackFrame
  6. #IFDEF cpuavr
  7. -XPavr-
  8. #ENDIF
  9. #ENDIF

  • Download Blinky project on this thread, and try to compile, type this in the Blinky folder

Code: Bash  [Select][+][-]
  1. fpc Blinky.lpr  -Tembedded -Pavr -MDelphi -Scghi -CX -O3 -Xs -XX -l -vewnhibq -Filib/avr-embedded -Fu. -FUlib/avr-embedded -Cpavr5 -Wpatmega328p -a

  • Now you can upload hex file using avrdude  :D

Code: Bash  [Select][+][-]
  1. sudo avrdude -v -patmega328p -carduino -P/dev/cu.wchusbserial1420 -b115200 -D -Uflash:w:Blinky.hex:i

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: FPC/Lazarus Arduino Tutorial
« Reply #41 on: March 24, 2016, 09:04:27 am »
@Herux:
Good enough for the wiki. Note linux need some little changes (e.g. apt-get) in the first two steps.
Windows is only slightly different. I use winavr https://sourceforge.net/projects/winavr/files/latest/download that contains a windows version of avrdude.
« Last Edit: March 24, 2016, 09:21:12 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

herux

  • Full Member
  • ***
  • Posts: 102
Re: FPC/Lazarus Arduino Tutorial
« Reply #42 on: March 24, 2016, 10:43:52 am »
please do, i have not account to write down in the wiki

diego bertotti

  • Full Member
  • ***
  • Posts: 101
Re: FPC/Lazarus Arduino Tutorial
« Reply #43 on: January 05, 2019, 03:00:35 pm »
a little contribution

tool config to transfer the .hex file to arduino uno using a previously installed avrdude(arduino ide).

ovbiously, fine tune the paths to your case!

Tools -- Configure external tools -- Add

Title:
Arduino Upload with avrdude

Program File name:
F:\Progra~1\arduino\arduino_1_8_8\hardware\tools\avr\bin\avrdude.exe

Parameters:
-CF:\progra~1\arduino\arduino_1_8_8\hardware\tools\avr\etc\avrdude.conf
-v -patmega328p -carduino -P\\.\COM13 -b115200 -D -Uflash:w:$OutputFile().hex:i

af0815

  • Hero Member
  • *****
  • Posts: 1291
Re: FPC/Lazarus Arduino Tutorial
« Reply #44 on: January 05, 2019, 03:48:25 pm »
Maybe some information is in the freepascal wiki too : http://wiki.freepascal.org/AVR_Embedded_Tutorial/de im German :-) but with google translate it is possible to read.

The info of the tutorial was collected by mathias in the thread https://www.lazarusforum.de/viewtopic.php?f=9&t=11127&hilit=avr (german).

regards
Andreas

 

TinyPortal © 2005-2018