Arduino Forum

Using Arduino => Microcontrollers => Topic started by: PieterP on Sep 17, 2016, 02:56 pm

Title: Compiling Optiboot
Post by: PieterP on Sep 17, 2016, 02:56 pm
Hello

I'm trying to compile an Optiboot bootloader for an ATmega328P running @14.7456 MHz.
I've added an new option to the Makefile:
Code: (arduino-1.6.8/hardware/arduino/avr/bootloaders/optiboot/Makefile) [Select]
atmega328_14_7456: TARGET = atmega328
atmega328_14_7456: MCU_TARGET = atmega328p
atmega328_14_7456: CFLAGS += '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200'
atmega328_14_7456: AVR_FREQ = 14745600L
atmega328_14_7456: LDSECTIONS  = -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe
atmega328_14_7456: $(PROGRAM)_atmega328_14_7456.hex
atmega328_14_7456: $(PROGRAM)_atmega328_14_7456.lst

atmega328_14_7456_isp: atmega328
atmega328_14_7456_isp: TARGET = atmega328
atmega328_14_7456_isp: MCU_TARGET = atmega328p
# 512 byte boot, SPIEN
atmega328_14_7456_isp: HFUSE = DE
# Low power xtal (14.7456MHz) 16KCK/14CK+65ms
atmega328_14_7456_isp: LFUSE = FF
# 2.7V brownout
atmega328_14_7456_isp: EFUSE = 05
atmega328_14_7456_isp: isp


I'm having problems compiling it: when using avr-gcc from the Ubuntu 16.04 repositories (sudo apt-get install gcc-avr avr-libc), I get the following error:
Code: [Select]
$ make atmega328_14_7456

avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mshort-calls -mmcu=atmega328p -DF_CPU=14745600L  '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200'   -c -o optiboot.o optiboot.c
avr-gcc: error: unrecognized command line option '-mshort-calls'
<builtin>: recipe for target 'optiboot.o' failed
make: *** [optiboot.o] Error 1


This is probably because the version of avr-gcc I'm using (4.9.2) is not supported. From the Optiboot wiki (https://github.com/Optiboot/optiboot/wiki/CompilingOptiboot):
Quote
Optiboot is designed to be compiled with the same version of avr-gcc that is distributed with the Arduino environment: 4.3.2. This is actually quite an old version of gcc; some effort has been made to allow the compile to procede with new versions of the compiler. However, since Optiboot MUST compile to a total size of less than 512 bytes, it is very sensitive to changes in the way the compiler does optimizations, or the way it processes optimization options on the compile command.
So I decided to use the version that came with the Arduino IDE:
Code: [Select]
$ export ENV=arduino
$ make atmega328_14_7456

../../../tools/avr/bin/avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mshort-calls -mmcu=atmega328p -DF_CPU=14745600L  '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200'   -c -o optiboot.o optiboot.c
make: ../../../tools/avr/bin/avr-gcc: Command not found
<builtin>: recipe for target 'optiboot.o' failed
make: *** [optiboot.o] Error 127


This is because the Makefile contains the wrong paths: on line 52, it has:
Code: [Select]
TOOLROOT = ../../../tools
This is wrong (probably the path for version 1.0.x of the IDE).
After changing it to
Code: [Select]
TOOLROOT = ../../../../tools
it now finds the compiler, but I get the same error as before:
Code: [Select]
make atmega328_14_7456
../../../../tools/avr/bin/avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mshort-calls -mmcu=atmega328p -DF_CPU=14745600L  '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200'   -c -o optiboot.o optiboot.c
avr-gcc: error: unrecognized command line option '-mshort-calls'
<builtin>: recipe for target 'optiboot.o' failed
make: *** [optiboot.o] Error 1


This is because the Arduino IDE now comes with avr-gcc 4.8.1.

But both the Optiboot Wiki page and the Arduino's optiboot folder haven't been updated for over a year ...

How do I compile it? Could I just delete the -mshort flag? That'll probably compile to a hex file that's too large, right?

Thanks in advance
Pieter

Title: Re: Compiling Optiboot
Post by: pert on Sep 17, 2016, 06:59 pm
It looks like you're using optiboot v4.4. Try it with the latest optiboot source. It doesn't use -mshort-calls. It even automatically deals with the different tool paths between Arduino IDE versions: https://github.com/Optiboot/optiboot/blob/master/optiboot/bootloaders/optiboot/Makefile#L80-L84 (https://github.com/Optiboot/optiboot/blob/master/optiboot/bootloaders/optiboot/Makefile#L80-L84)
Title: Re: Compiling Optiboot
Post by: PieterP on Sep 17, 2016, 11:26 pm
It compiles without errors with the new version, thanks a lot!

I was using the version that came with the Arduino IDE 1.6.8. Is there any reason why they don't include a newer version of Optiboot with the IDE, it's a bit pointless to include an old version that isn't even compatible with the included avr-gcc version and expects the wrong directory structure ...
Title: Re: Compiling Optiboot
Post by: pert on Sep 17, 2016, 11:33 pm
Glad to hear!
The Arduino IDE includes the old version because that's the version that they used for the Arduino Uno's optiboot bootloader. They're using the old version for the Arduino bootloaders because that one works fine and I think the developers have the theory of "If it ain't broke...". The only release since v4.4 on the optiboot repository is v6.2, which is "Not a production release".
Title: Re: Compiling Optiboot
Post by: westfw on Sep 18, 2016, 02:05 am
(also, the Arduino IDE distribution no longer includes all of the tools necessary to build (and version of) optiboot.  There's no "make" for instance.  :-( )
Title: Re: Compiling Optiboot
Post by: pert on Sep 18, 2016, 03:38 am
westfw, do you know why they stopped including those tools? make and rm were the only two missing for me on Windows.
Title: Re: Compiling Optiboot
Post by: westfw on Sep 18, 2016, 01:27 pm
Quote
do you know why they stopped including those tools?
The old IDE simply included WINAVR, which in turn included a whole bunch of other tools designed to give the user a full development environment on windows.
WINAVR hasn't been updated since 2010, and isn't likely to be updated.  So to get a newer compiler (and full control  of it), the new IDE includes petty much only the gcc-related tools themselves, built by the Arduino team from source (IIRC.) (although, it's the same set of tools that Atmel includes in their "command line tools for xxx" distributions.)  (Sigh.  WINAVR was a very convenient set of tools...)
Title: Re: Compiling Optiboot
Post by: phiwer on Dec 07, 2017, 05:26 pm
Hi!

I am trying to change the baudrade when I am uploading code to my Uno. I am doing this by changing it in optiboot.c. But when trying to compile I get the following message.


C:\Program Files (x86)\Arduino\hardware\arduino\avr\bootloaders\optiboot>..\..\..\..\tools\avr\bin\make OS=windows ENV=arduino uno
File path not found

I have tried everything posted in this thread and I have been googling for hours but can't find a solution.

Does anyone have the same problem or a solution?

Title: Re: Compiling Optiboot
Post by: PieterP on Dec 07, 2017, 06:09 pm
https://tttapa.github.io/Arduino/ATmega328P/Custom-frequency.html (https://tttapa.github.io/Arduino/ATmega328P/Custom-frequency.html)
Title: Re: Compiling Optiboot
Post by: ron_sutherland on Dec 07, 2017, 10:19 pm
The "make" command is a Linux thing and optiboot is built in a way that the IDE does not handle so it needs some different build tools that are controlled with the Makefile.

I think the only real option is to use Linux which has the native tools, however, there are a few ways to run Linux:

Windows Subsystem for Linux

https://docs.microsoft.com/en-us/windows/wsl/install-win10 (https://docs.microsoft.com/en-us/windows/wsl/install-win10)

Ubuntu on PC hardware

https://www.ubuntu.com/ (https://www.ubuntu.com/)

Raspibian on a Raspberry Pi

https://www.raspberrypi.org/downloads/raspbian/ (https://www.raspberrypi.org/downloads/raspbian/)

Normally I have an old junk computer running one of the Ubuntu flavors that are happy on it (e.g. Xubuntu or Lubuntu).

Anyway whatever sort of Linux you go with then you need to install the software packages.

Code: [Select]
sudo apt-get update
sudo apt-get install make git picocom gcc-avr binutils-avr gdb-avr avr-libc avrdude
sudo usermod -a -G dialout your_user_name
git clone https://github.com/Optiboot/optiboot
cd optiboot
make


Did I forget anything?
Title: Re: Compiling Optiboot
Post by: pert on Dec 07, 2017, 11:12 pm
Old versions of the Arduino IDE (1.0.6 and older I think) came with make. New IDE versions do not, as we discussed above. You can get make for Windows from an old copy of the Arduino IDE, which you can download here:
https://www.arduino.cc/en/Main/OldSoftwareReleases#previous (https://www.arduino.cc/en/Main/OldSoftwareReleases#previous)
or you can get it from winavr:
http://winavr.sourceforge.net/ (http://winavr.sourceforge.net/)

So no, Linux is absolutely not necessary. I compile bootloaders all the time using Windows.
Title: Re: Compiling Optiboot
Post by: DrAzzy on Dec 08, 2017, 01:02 am
I start with a 1.0.6 arduino IDE install (.zip package, not installed), and then replace the compiler and compiler supplied libraries with the ones from the latest version of the IDE (or from Atmel Studio).
Title: Re: Compiling Optiboot
Post by: ron_sutherland on Dec 08, 2017, 04:35 am
WinAVR looks dodgy to me, I know it had its day, but its day has passed.

Those packages in Ubuntu and Raspibin are from Debian, and for me have been reliable and up to date.

https://packages.debian.org/sid/gcc-avr (https://packages.debian.org/sid/gcc-avr)



Title: Re: Compiling Optiboot
Post by: tf68 on Dec 08, 2017, 06:47 am
Lots of GNU tools for Windows here, including make.
https://sourceforge.net/projects/gnuwin32/ (https://sourceforge.net/projects/gnuwin32/)
Title: Re: Compiling Optiboot
Post by: Budvar10 on Dec 08, 2017, 08:38 am
As was mentioned above, I'm also using old v1.0.6 with replaced toolchain (gcc v 4.9.2 - make is missing in this one) and with old make.exe v3.81 (from the toolchain with gcc v4.7.2). It works fine for me.
Eventually, here is a link for how to build yourself and for Win download: http://andybrown.me.uk/2015/03/08/avr-gcc-492/ (http://andybrown.me.uk/2015/03/08/avr-gcc-492/).
Title: Re: Compiling Optiboot
Post by: westfw on Dec 08, 2017, 09:40 am
It's not THAT bad.
Install any relatively recent version of the Arduino IDE.
Install the GnuWin Make for windows (ie from http://gnuwin32.sourceforge.net/packages/make.htm (http://gnuwin32.sourceforge.net/packages/make.htm))  (use the "setup" version to make sure all the right dlls are installed too.)
Open a command prompt window.  Make sure that "make.exe" works (I don't think the installer for make adds the gnuwin directory to the path.)
Execute the really neat and horribly ugly .BAT file described here: https://hackaday.io/project/19935-install-avr-tools (https://hackaday.io/project/19935-install-avr-tools) (download from here https://github.com/WestfW/Arduino-avr-tools-install (https://github.com/WestfW/Arduino-avr-tools-install)
Connect to the ...optiboot/bootloaders/optiboot directory where the source code lives.

Then you should be able to issue "make whatever" command to do the compile...

Oh, admittedly it might be nice if more of this happened semi-automatically, and if the .bat file looked for GnuWin, and it might be a bit ... overwhelming ... if you've never used a "Command Prompt" window before, but it still ought to be better than "learn enough linux to throw away your windows box."

Title: Re: Compiling Optiboot
Post by: westfw on Dec 11, 2017, 06:11 am
Ok, I've made it easier.
If you download the new omake.bat from https://github.com/Optiboot/optiboot/blob/master/optiboot/bootloaders/optiboot/omake.bat (https://github.com/Optiboot/optiboot/blob/master/optiboot/bootloaders/optiboot/omake.bat) (with or without the rest of the most recent optiboot), it is smart enough to go out and find avr-gcc in most of the likely location and version combinations before running the compile.
You still have to have Arduino and make installed somewhere, but you no longer have to fiddle with paths.  So: