Building and Installing grbl from source

Building grbl from source on Linux

There are three basic steps

  • Install the necessary OS level packages
  • Get the grbl source
    • If necessary patch the grbl source, e.g. to work with LaserAxe controller
  • Build grbl

It should go without saying you don't need to patch the grbl source if you're running a system which works with the default config.

I use Linux Mint, an Ubunt based distribution. Ubuntu is, in turn, based on Debian. The steps on a Red Hat based distro are the same but some of the details, such as the command to install system packages and the names of those packages are different.

The build example presented below was done on an ubuntu based virtual machine under vagrant.

Install the OS packages

You'll need the build tools, e.g. make, gcc, ld as well as the avr cross compier and libaries.
As root or with sudo: apt-get install build-essential gcc-avr avr-libc

vagrant@ubuntu-bionic$ sudo apt-get install build-essential gcc-avr avr-libc
 Reading package lists...
 Building dependency tree...
 Reading state information...
The following additional packages will be installed:
  binutils binutils-avr binutils-common binutils-x86-64-linux-gnu cpp cpp-7 dpkg-dev fakeroot g++ g++-7 gcc gcc-7 gcc-7-base gcc-8-base libalgorithm-diff-perl
  ...
Suggested packages:
  binutils-doc cpp-doc gcc-7-locales debian-keyring g++-multilib g++-7-multilib gcc-7-doc libstdc++6-7-dbg gcc-multilib autoconf automake libtool flex bison
  ...
The following NEW packages will be installed:
  avr-libc binutils binutils-avr binutils-common binutils-x86-64-linux-gnu build-essential cpp cpp-7 dpkg-dev fakeroot g++ g++-7 gcc gcc-7 gcc-7-base gcc-avr
  ...
The following packages will be upgraded:
  gcc-8-base libgcc1 libstdc++6
3 upgraded, 43 newly installed, 0 to remove and 115 not upgraded.
Need to get 59.0 MB of archives.
After this operation, 288 MB of additional disk space will be used.
Do you want to continue? [Y/n]
 Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 gcc-8-base amd64 8.3.0-6ubuntu1~18.04 [18.6 kB]
 Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libstdc++6 amd64 8.3.0-6ubuntu1~18.04 [400 kB]
 ...
 Get:46 http://archive.ubuntu.com/ubuntu bionic/universe amd64 avr-libc all 1:2.0.0+Atmel3.6.0-1 [4872 kB]
 Fetched 59.0 MB in 56s (1048 kB/s)
 Extracting templates from packages:
 Preparing to unpack .../libstdc++6_8.3.0-6ubuntu1~18.04_amd64.deb ...
 Unpacking libstdc++6:amd64 (8.3.0-6ubuntu1~18.04) over (8.2.0-1ubuntu2~18.04) ...
 Setting up libstdc++6:amd64 (8.3.0-6ubuntu1~18.04) ...
 ...
 Setting up build-essential (12.4ubuntu1) ...
 Processing triggers for libc-bin (2.27-3ubuntu1) ...

Clone grbl from the official repo

You can download via the web interface our use git to grab the source code from the grbl code repo

vagrant@ubuntu-bionic$ git clone https://github.com/gnea/grbl.git
Cloning into 'grbl'...
remote: Enumerating objects: 4569, done.
Receiving objects:   0% (1/4569)
 ...
Receiving objects: 100% (4569/4569), 2.17 MiB | 602.00 KiB/s, done.
Resolving deltas:   0% (0/3470)
 ...
Resolving deltas: 100% (3470/3470), done.

Change into the directory at the top of the code tree

Commands in following steps will be referenced from the top of the code tree.

vagrant@ubuntu-bionic$ cd grbl/

Replace the default config.h with the one modified for the LaserAxe

Per above, if you're running hardware that works with default build, you don't have to do this. For my hardware, I need to patch the default config to work with the LaserAxe board. Note, not all systems include curl in the default install. If you system doesn't have it, you can install it with apt-get install curl as root or using sudo. (Again, that's for debian based distros, Red Hat based distros use rpm)

vagrant@ubuntu-bionic$ mv grbl/config.h grbl/config.h.ORIG
vagrant@ubuntu-bionic$ curl -L https://gitlab.com/snippets/1836412/raw > grbl/config.h
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  100 52178    0 52178    0     0  16869      0 --:--:--  0:00:03 --:--:-- 16869

Build the software

At this point, you should be able to simply build the firmware with the make command.

vagrant@ubuntu-bionic$ make
avr-gcc -Wall -Os -DF_CPU=16000000 -mmcu=atmega328p -I. -ffunction-sections -flto -MMD -MP -c grbl/main.c -o build/main.o
avr-gcc -Wall -Os -DF_CPU=16000000 -mmcu=atmega328p -I. -ffunction-sections -flto -MMD -MP -c grbl/motion_control.c -o build/motion_control.o
  ...
avr-gcc -Wall -Os -DF_CPU=16000000 -mmcu=atmega328p -I. -ffunction-sections -flto -o build/main.elf build/main.o build/motion_control.o build/gcode.o build/spindle_control.o build/coolant_control.o build/serial.o build/protocol.o build/stepper.o build/eeprom.o build/settings.o build/planner.o build/nuts_bolts.o build/limits.o build/jog.o build/print.o build/probe.o build/report.o build/system.o -lm -Wl,--gc-sections
rm -f grbl.hex
avr-objcopy -j .text -j .data -O ihex build/main.elf grbl.hex
avr-size --format=berkeley build/main.elf
   text    data     bss     dec     hex filename
  29654       0    1633   31287    7a37 build/main.elf

The build will generate both Intel hex and ELF formatted files

vagrant@ubuntu-bionic:~/grbl$ ls grbl.hex build/main.elf
build/main.elf  grbl.hex

Installing the grbl firmware with avrdude

You can get avrdude from a variety of locations. As the arduino software uses it under the covers it may be simplest to just install the arduino IDE. This also means you should be able to use it on any platform which runs the arduino IDE.

Installing the arduino IDE under Linux

Once you've downladed the archive file, you can simply exapand it to a convenient location, e.g.

kryton:/home/ws/FirmwareDemo> xzcat arduino-1.8.9-linux64.tar.xz |tar -xv
arduino-1.8.9/
arduino-1.8.9/arduino-linux-setup.sh
arduino-1.8.9/lib/
arduino-1.8.9/lib/version.txt
  ...
arduino-1.8.9/tools/WiFi101/tool/firmwares/NINA/1.2.1/NINA_W102-Uno_WiFi_Rev2.bin
arduino-1.8.9/tools/howto.txt

Running avrdude

There are a plenty of docs and tuturials for avrdude available on the net. A rather good on is avaialable ladyada - avrdude totorial Basically, you run the command with a few parameters telling it how to use the I/O port connected to the device and the type and location of the file to upload. We'll start by setting some environment variables to save ourselves some typing later on.

making life easier by setting the path and config file

kryton:/home/ws/FirmwareDemo> export PATH=/home/ws/FirmwareDemo/arduino-1.8.9/hardware/tools/avr/bin:$PATH
kryton:/home/ws/FirmwareDemo> export AVRCONF=/home/ws/FirmwareDemo/arduino-1.8.9/hardware/tools/avr/etc/avrdude.conf
kryton:/home/ws/FirmwareDemo> which avrdude
/home/ws/FirmwareDemo/arduino-1.8.9/hardware/tools/avr/bin/avrdude

It doesn't mater which firmware file you use, Intel Hex or ELF. You just need to specify the filename and type. The following example shows both versions but you only have to use one. Also, note the paramaters on your system may be different, e.g. your com port may not be called '/dev/ttyUSB0'. Consult the docs or one of the tutorials for more info.

Uploading the ELF format file

kryton:/home/ws/FirmwareDemo> avrdude -C $AVRCONF -c arduino -p m328p -P /dev/ttyUSB0 -b 57600 -Uflash:w:./grbl/build/main.elf:e -v

avrdude: Version 6.3-20171130
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/ws/FirmwareDemo/arduino-1.8.9/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/ws/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 2
         Firmware Version: 1.16
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions


Reading |                                                    | 0% 0.00s
Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "./grbl/build/main.elf"
avrdude: writing flash (29654 bytes):


Writing |                                                    | 0% 0.00s
Writing | #                                                  | 1% 0.11s
  ...
Writing | ################################################## | 99% 8.60s
Writing | ################################################## | 100% 8.67s

avrdude: 29654 bytes of flash written
avrdude: verifying flash memory against ./grbl/build/main.elf:
avrdude: load data flash data from input file ./grbl/build/main.elf:
avrdude: input file ./grbl/build/main.elf contains 29654 bytes
avrdude: reading on-chip flash data:


Reading |                                                    | 0% 0.00s
Reading | #                                                  | 1% 0.08s
Reading | #                                                  | 2% 0.14s
  ...
Reading | ################################################## | 99% 6.46s
Reading | ################################################## | 100% 6.51s

avrdude: verifying ...
avrdude: 29654 bytes of flash verified

avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: safemode: Fuses OK (E:00, H:00, L:00)

avrdude done.  Thank you.

Uploading the Intel Hex format file

Same as above, but this time with the hex format file.

kryton:/home/ws/FirmwareDemo> avrdude -C $AVRCONF -c arduino -p m328p -P /dev/ttyUSB0 -b 57600 -Uflash:w:./grbl/grbl.hex:i -v

avrdude: Version 6.3-20171130
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/ws/FirmwareDemo/arduino-1.8.9/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/ws/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 2
         Firmware Version: 1.16
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions


Reading |                                                    | 0% 0.00s
Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "./grbl/grbl.hex"
avrdude: writing flash (29654 bytes):


Writing |                                                    | 0% 0.00s
Writing | #                                                  | 1% 0.11s
Writing | #                                                  | 2% 0.19s
  ...
Writing | ################################################## | 99% 8.62s
Writing | ################################################## | 100% 8.69s

avrdude: 29654 bytes of flash written
avrdude: verifying flash memory against ./grbl/grbl.hex:
avrdude: load data flash data from input file ./grbl/grbl.hex:
avrdude: input file ./grbl/grbl.hex contains 29654 bytes
avrdude: reading on-chip flash data:


Reading |                                                    | 0% 0.00s
Reading | #                                                  | 1% 0.08s
  ...
Reading | ################################################## | 99% 6.48s
Reading | ################################################## | 100% 6.53s

avrdude: verifying ...
avrdude: 29654 bytes of flash verified

avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: safemode: Fuses OK (E:00, H:00, L:00)

avrdude done.  Thank you.

kryton:/home/ws/FirmwareDemo>