BLE application with nRF51822: Makefile

The last element mandatory before being able to compile a nrf51 project is the Makefile. This file allows to tell the compiler what and where are the source and object files, it also allows to tell the linker how to generate the executable regarding the linker script. All the target information are also described in this file. I’m not going to explain all the options used in this Makefile but if you want more information you can visit the following website: http://www.opussoftware.com/tutorial/TutMakefile.htm#Rules

  • Create a Makefile file in led_blinking project folder:
        $ cd led_blinking
        $ touch Makefile
  • Declare the toolchain information and options:
# relative location of the toolchain binary
TOOLCHAIN = ../toolchain/arm_cm0/bin/arm-none-eabi-
# arm-none-eabi-gcc executable
CC   = $(TOOLCHAIN)gcc
# arm-none-eabi-objcopy executable          
CP   = $(TOOLCHAIN)objcopy    
# output format is raw binary and we strip the symbol information from source files                      
BIN  = $(CP) -O binary -S     
  • Declare the source directories:
# Sources
# linker script directory
LDSCRIPT = ../linker_script/nrf51_ld_script.ld   
# startup folder directory     
STARTUP_DIR = ../startup

# main.c directory
SRC  = src/main.c          
# startup.c directory                         
SRC  += $(STARTUP_DIR)/startup.c                      

# include directory
INCLUDES = include/   
# add option "-I" before each included folders                    
INCLUDE_DIR  = $(patsubst %,-I%,$(INCLUDES))   
  • Declare the compiler options
# Project name
PROJECT_NAME = led_blinking

# Options
OBJS  = $(SRC:.c=.o)

CPFLAGS = -mcpu=cortex-m0 -Os -gdwarf-2 -mthumb -fomit-frame-pointer -Wall -Wstrict-prototypes -fverbose-asm -Wa,-ahlms=$(<:.c=.lst) $(DEFS)
LDFLAGS = -mcpu=cortex-m0 -lc -mthumb -nostartfiles -T$(LDSCRIPT) -Wl,-Map=$(PROJECT_NAME).map,--cref,--no-warn-mismatch $(LIBDIR)
 
# Generate dependency information
CPFLAGS += -MD -MP -MF .dep\$(@F).d
  • Declare the project rules
# Rules
all: $(OBJS) $(PROJECT_NAME).elf  $(PROJECT_NAME).bin $(PROJECT_NAME).map
 
 %o: %c
	$(CC) -c $(CPFLAGS) -I . $(INCLUDE_DIR) $< -o $@

%elf: $(OBJS)
	$(CC) $(OBJS) $(LDFLAGS) -o $@
	
%bin: %elf
	$(BIN)  $< $@

clean:
	find ./ -name '*~' | xargs rm -f
	rm -f $(OBJS)
	rm -f $(PROJECT_NAME).elf
	rm -f $(PROJECT_NAME).map
	rm -f $(PROJECT_NAME).hex
	rm -f $(PROJECT_NAME).bin
	rm $(SRC:.c=.lst)
	rm -r  .dep*

At this point we should be able to build our simple project. To do so, in the project folder directory type make and press enter.

  • Build your project:
        $ cd led_blinking
        $ make
  • The output you should see:
-Iinclude/ -I../startup
../toolchain/arm_cm0/bin/arm-none-eabi-gcc -c -mcpu=cortex-m0 -Os -gdwarf-2 -mthumb -fomit-frame-pointer -Wall -Wstrict-prototypes -fverbose-asm -Wa,-ahlms=src/main.lst  -MD -MP -MF .dep\main.o.d -I . -Iinclude/ -I../startup            src/main.c -o src/main.o
../toolchain/arm_cm0/bin/arm-none-eabi-gcc -c -mcpu=cortex-m0 -Os -gdwarf-2 -mthumb -fomit-frame-pointer -Wall -Wstrict-prototypes -fverbose-asm -Wa,-ahlms=../startup/startup.lst  -MD -MP -MF .dep\startup.o.d -I . -Iinclude/ -I../startup            ../startup/startup.c -o ../startup/startup.o
../startup/startup.c:54:2: warning: taking address of expression of type 'void'
  &_end_of_stack,        // The initial stack pointer
  ^
../toolchain/arm_cm0/bin/arm-none-eabi-gcc src/main.o ../startup/startup.o -mcpu=cortex-m0 -lc -mthumb -nostartfiles -T../linker_script/nrf51_ld_script.ld    -Wl,-Map=led_blinking.map,--cref,--no-warn-mismatch  -o led_blinking.elf
../toolchain/arm_cm0/bin/arm-none-eabi-objcopy     -O binary -S       led_blinking.elf led_blinking.bin

As you can see, thanks to the Makefile, 3 new files have been generated. The most important one is the “.bin” file since it will be the one you will use to flash the nrf51. The “.map” file contains the memory map of your project, it could help you to find linker script bugs or to debug your application.

Advertisements

8 thoughts on “BLE application with nRF51822: Makefile

  1. Andre February 7, 2016 / 11:08 am

    Hi,
    First of all, thanks for the super guidance you are giving. But i get stuck at the ” make” of led_blinking.
    It is ending with a error:

    arm-none-eabi-gcc: fatal error: -fuse-linker-plugin, but liblto_plugin.so not found
    compilation terminated.
    make: *** [led_blinking.elf] Error 1

    Can you help me how to solve this?
    Thanks,
    Andre

    Like

    • Jocelyn Masserot February 7, 2016 / 11:56 am

      Hi Andre,

      Sorry about that, it seems your issue could come from binutils. Are you using a Mac? If yes you should try to install binutils through brew:

      If brew is not installed on your computer type in a terminal:

      ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

      then:

      brew outdated && brew update && brew upgrade && brew doctor

      and finally:

      brew install binutils

      Let me know, if it solve your issue, I’ll update my post.

      Thanks,

      Like

      • Jocelyn Masserot February 7, 2016 / 1:20 pm

        Actually this issue happened because the toolchain seems to ne be compatible with El Capitan (I currently use Yosemite). I’ll try to fix the issue asap.

        Like

  2. Andre February 8, 2016 / 3:27 pm

    Yes it is working 🙂 Thanks!

    Like

  3. mpoupm February 1, 2017 / 9:56 am

    Hi,
    thanks for your help.
    I am getting the error below when I execute the make command :
    Makefile:38: *** missing separator. Stop. this line : “$(CC) -c $(CPFLAGS) -I . $(INCLUDE_DIR) $< -o $@".

    Can you help.
    Thanks & Regards.

    Like

  4. mpoupm February 1, 2017 / 10:24 am

    make: *** No rule to make target ‘led_blinking.elf’, needed by ‘all’. Stop.

    Like

    • Jocelyn Masserot February 9, 2017 / 9:32 am

      Hi, I’ll have a look asap. Are you sure to use the same toolchain than the one I’m using?

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s