Understanding the make process

Status
Not open for further replies.

jimmayhugh

Well-known member
OK, it's been a LONG time since I've used makefiles for anything other than the standard

Code:
./configure 
make 
sudo make install

that I occasionally used to compile and install Linux software that doesn't come in a .rpm or .deb package. Anyhoo...

I thought I'd try compiling my program from the command line just for grits and shins.

First step was to copy the Makefile to my program's sketckbook directory and modifying the area specified:
Code:
# The name of your project (used to name the compiled .hex file)
TARGET = TeensyPiPidDebug

# configurable options
OPTIONS = -DF_CPU=48000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH


#************************************************************************
# Location of Teensyduino utilities, Toolchain, and Arduino Libraries.
# To use this makefile without Arduino, copy the resources from these
# locations and edit the pathnames.  The rest of Arduino is not needed.
#************************************************************************

# path location for Teensy Loader, teensy_post_compile and teensy_reboot
TOOLSPATH = /home/teensypi/arduino-1.0.4/hardware/tools   # on Linux
#TOOLSPATH = ../../../tools/avr/bin   # on Mac or Windows

# path location for Arduino libraries (currently not used)
LIBRARYPATH = /home/teensypi/arduino-1.0.4/libraries

# path location for the arm-none-eabi compiler
COMPILERPATH = /home/teensypi/arduino-1.0.4/hardware/tools/arm-none-eabi/bin

Tried running "make" and got the following:
Code:
[teensypi@CentosTeensyPi TeensyPiPidDebug]$ make
make: *** No rule to make target `mk20dx128.ld', needed by `TeensyPiPidDebug.elf'.  Stop.
[teensypi@CentosTeensyPi TeensyPiPidDebug]$

I found the location of mk20dx128.ld, and placed the absolute patch for the file in the Makefile:
Code:
# linker options
LDFLAGS = -Os -Wl,--gc-sections -mcpu=cortex-m4 -mthumb -T/home/teensypi/arduino-1.0.4/hardware/teensy/cores/teensy3/mk20dx128.ld

$(TARGET).elf: $(OBJS) /home/teensypi/arduino-1.0.4/hardware/teensy/cores/teensy3/mk20dx128.ld
	$(CC) $(LDFLAGS) -o $@ $(OBJS)

I also disabled the teensy_reboot:
Code:
%.hex: %.elf
	$(SIZE) $<
	$(OBJCOPY) -O ihex -R .eeprom $< $@
	$(abspath $(TOOLSPATH))/teensy_post_compile -file=$(basename $@) -path=$(shell pwd) -tools=$(abspath $(TOOLSPATH))
#	-$(abspath $(TOOLSPATH))/teensy_reboot

Did a "make clean", followed by "make":
Code:
[teensypi@CentosTeensyPi TeensyPiPidDebug]$ make
/home/teensypi/arduino-1.0.4/hardware/tools/arm-none-eabi/bin/arm-none-eabi-gcc -Os -Wl,--gc-sections -mcpu=cortex-m4 -mthumb -T/home/teensypi/arduino-1.0.4/hardware/teensy/cores/teensy3/mk20dx128.ld -o TeensyPiPidDebug.elf  
/home/teensypi/arduino-1.0.4/hardware/tools/arm-none-eabi/bin/arm-none-eabi-size TeensyPiPidDebug.elf
   text	   data	    bss	    dec	    hex	filename
   1240	      0	     28	   1268	    4f4	TeensyPiPidDebug.elf
/home/teensypi/arduino-1.0.4/hardware/tools/arm-none-eabi/bin/arm-none-eabi-objcopy -O ihex -R .eeprom TeensyPiPidDebug.elf TeensyPiPidDebug.hex
/home/teensypi/arduino-1.0.4/hardware/tools/teensy_post_compile -file=TeensyPiPidDebug -path=/home/teensypi/sketchbook/TeensyPiPidDebug -tools=/home/teensypi/arduino-1.0.4/hardware/tools
Opening Teensy Loader...
Teensy Loader 1.07, begin program
File "TeensyPiPidDebug.hex". 1240 bytes, 1% used
Listening for remote control on port 3149
initialized, showing main window
remote connection opened
remote cmd: "comment: Teensyduino 1.13 - LINUX32"
remote cmd: "dir:/home/teensypi/sketchbook/TeensyPiPidDebug/"
remote cmd: "file:TeensyPiPidDebug.hex"
File "TeensyPiPidDebug.hex". 1240 bytes, 1% used
remote cmd: "status"
status data sent
remote cmd: "auto:on"
/home/teensypi/arduino-1.0.4/hardware/tools/teensy_reboot
remote connection closed
remote connection opened
remote cmd: "status"
status data sent
...
Please press the RESET BUTTON on your Teensy to upload your sketch.  Auto-reboot only works if the Teensy is running a previous sketch.
remote connection closed
[teensypi@CentosTeensyPi TeensyPiPidDebug]$

Examining the directory, I found:
Code:
[teensypi@CentosTeensyPi TeensyPiPidDebug]$ ls -la
total 92
drwxrwxr-x    2 teensypi teensypi   4096  Mar 16 09:10 .
drwxrwxr-x. 63 teensypi teensypi   4096  Mar   2 07:11 ..
-rw-rw-r--       1 teensypi teensypi   2680  Mar 16 09:04 Makefile
-rwxrwxr-x     1 teensypi teensypi  35837 Mar 16 09:10 TeensyPiPidDebug.elf
-rw-rw-r--       1 teensypi teensypi    3507 Mar 16 09:10 TeensyPiPidDebug.hex
-rw-rw-r--       1 teensypi teensypi  58179 Mar 15 09:41 TeensyPiPidDebug.ino

So, shouldn't the .hex file be larger?? What am I missing?

Thank for the help.
 
The Makefile doesn't process .ino files into C or C++ files, so I think that all you are getting is a bare 0x4f4 byte file based solely on the mk20128dx.ld file.

You are missing a main() routine and the rest of the "copy the resources from these # locations and edit the pathnames." I think you could copy the whole contents of the directory containing the mk20dx128.ld file into the directory, and modify the main.cpp file to include your .ino file, and then it may work, but then the existence of main.cpp might mess with a non-Makefile build.


I did the following:
Code:
mkdir JUNK
cd JUNK
cp -a /Applications/Arduino.app/Contents/Resources/Java/hardware/teensy/cores/teensy3/* .                         # copy all the resources
sed  's| \.\./\.\./\.\./| /Applications/Arduino.app/Contents/Resources/Java/hardware/|' Makefile >newMakefile  # edit the paths to point to the tools
make -f newMakefile

####
/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/arm-none-eabi/bin/arm-none-eabi-size main.elf
   text	   data	    bss	    dec	    hex	filename
  22648	   1452	   2292	  26392	   6718	main.elf
If you then blow most everything away, you get the same 4f4 byte hex file:
Code:
rm *.c *.cpp *.h
make clean && make -f newMakefile

#####

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/arm-none-eabi/bin/arm-none-eabi-size main.elf
   text	   data	    bss	    dec	    hex	filename
   1240	      0	     28	   1268	    4f4	main.elf

#  you'll upload a non-blinking program that requires a button-press to overwrite.
 
Teensy 3.0 Makefile and "Hello world!" over USB in plain C

I've been working on porting a c (non-c++) program to Teensy 3.0 and found this to work to get to a fairly plain hello world response:

Code:
mkdir JUNK
cd JUNK
cp -a /Applications/Arduino.app/Contents/Resources/Java/hardware/teensy/cores/teensy3/* . # copy a Makefile environment
sed  's| \.\./\.\./\.\./| /Applications/Arduino.app/Contents/Resources/Java/hardware/|' Makefile >newMakefile  # customize the Makefile to absolute paths
make -f newMakefile  # compile main.cpp and program teensy 3.0.

Then, I deleted main.cpp and replaced it with a main.c like this:

Code:
#include "WProgram.h"

int main(void)
{

        pinMode(13, OUTPUT);
        while (1) {
                digitalWriteFast(13, HIGH);
                delay(500);
                digitalWriteFast(13, LOW);
                delay(500);
                usb_serial_write("Hello World!\n",13);
        }


}

As an Arduino sketch, this works too:

Code:
void setup(void){
}

void loop(void){
 
 usb_serial_write("hello world!\n",13); 
 delay(500);
 
}

Apparently,
  • mk20dx128.c defines the Initial Program Counter as ResetHandler()
  • mk20dx128.c:ResetHandler() sets up some processor stuff and calls _init_Teensyduino_internal_()
  • pins_teensy.c:_init_Teensyduino_internal_() sets up PWM, ... and usb_init()
  • usb_dev.c:usb_init() sets up the usb endpoints as defined in usb_desc.h
  • usb_desc.h defines and chooses a set of endpoints based on -DUSB_* environment variables from the Makefile, or from Arduino/Tools/USBType
  • so ... usb_serial.c:usb_serial_write() is able to write the data

Not all of the functions listed on http://www.pjrc.com/teensy/usb_serial.html are available on Teensy3.0. usb_init() is already called, usb_configured() doesn't exist, none of the "Serial Parameter Functions" exist, and usb_serial_putchar_nowait() isn't there.

One thing that was causing me grief was that my old code called usb_init() which, on the Teensy 3.0, a second makes the usb not seem to work. So don't call usb_init(), just go ahead and use it.
 
Last edited:
Status
Not open for further replies.
Back
Top