Getting up and running on SparkFun ProMicro without Arduino IDE and using C to program the ProMicro.

So this was an effort in reverse engineering. There were a lot of things that I had to strip apart in order to get this to work.  As I suggested before, in the Arduino IDE under File>Preferences there is a place to edit the preferences.txt file (you might have to get there some other way), and in there set the build.verbose and upload.verbose to True.  This allows you see the output of how the files are being compiled and built and then being uploaded onto the SparkFun ProMicro. Here is a great high level overview on how avr-gcc chain works.

The first thing I did was create 2 very different sets of code that would allow me to debug if anything failed at a certain step. The 2 sets of code I created were:

The first one was just a simple code that blinks 2 leds and this was done entirely in the arduino IDE and used all the functions that arduino provides:

/* Pro Micro Test Code
 by: Nathan Seidle
 modified by: Jim Lindblom
 SparkFun Electronics
 date: September 16, 2013
 license: Public Domain - please use this code however you'd like.
 It's provided as a learning tool.

This code is provided to show how to control the SparkFun
 ProMicro's TX and RX LEDs within a sketch. It also serves
 to explain the difference between Serial.print() and
 Serial1.print().
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>

//int RXLED = 17; // The RX LED has a defined Arduino pin
// The TX LED was not so lucky, we'll need to use pre-defined
// macros (TXLED1, TXLED0) to control that.
// (We could use the same macros for the RX LED too -- RXLED1,
// and RXLED0.)

void setup()
{
 //pinMode(RXLED, OUTPUT); // Set RX LED as an output
 //setting up PBO
 (*(volatile unsigned char *)(0x24)) = 0x1; //setting the 0th bit in DDRB to 1 so that it can set up for output
 (*(volatile unsigned char *)(0x25)) = 0x0; //setting the 0th bit in PortB to 0 so that it can set output low

//setting up PD5
 (*(volatile unsigned char *)(0x2A)) = 0x20; //setting the 5th bit in DDRB to 1 so that it can set up for output
 (*(volatile unsigned char *)(0x2B)) = 0x0; //setting the 5th bit in PortB to so that it can set output low

// TX LED is set as an output behind the scenes

Serial.begin(9600); //This pipes to the serial monitor
 Serial1.begin(9600); //This is the UART, pipes to sensors attached to board
}

void loop()
{
 //Serial.println("Hello world"); // Print "Hello World" to the Serial Monitor
 Serial1.println("Hello!"); // Print "Hello!" over hardware UART


 //digitalWrite(RXLED, LOW); // set the LED on
 (*(volatile unsigned char *)(0x25)) = 0x0;
 //Serial.println((*(volatile unsigned char *)(0x23)));
 
 (*(volatile unsigned char *)(0x2B)) = 0x20; //setting the 5th bit in PortB to so that it can set output high
 //TXLED0; //TX LED is not tied to a normally controlled pin

delay(1000); // wait for a second
//
 //digitalWrite(RXLED, HIGH); // set the LED off
 (*(volatile unsigned char *)(0x2B)) = 0x0; //setting the 5th bit in PortB to so that it can set output low
 
 //TXLED1;
 (*(volatile unsigned char *)(0x25)) = 0x1;
 //Serial.println((*(volatile unsigned char *)(0x23)));


 delay(1000); // wait for a second


}

 

The second one was a program that sent Pin B5 high and low as fast as it could.  This could addressed the DDRB and PortB directly with the registers and removed as much dependency on arduino as possible. The code was:

blah

#include <avr/io.h>
#include <util/delay.h>

#define BLINK_DELAY_MS 100

int main (void)
{
 /* set pin 5 of PORTB for output*/
 (*(volatile unsigned int*)(0x24)) |= (1<<(5));

while(1) {
 /* set pin 5 high to turn led on */
 (*(volatile unsigned int*)(0x25)) |= (1<<(5));
 
 /* set pin 5 low to turn led off */
 (*(volatile unsigned int*)(0x25)) &= (0<<(5));
 }
 }

I had to use avr/io and util/delay.h because those are atmel avr functions not ardunio functions. As you can see this is written in code.  I tested both the codes in Arduino and they were working. This confirmed that code base I had was working completely.

The next step was to take the output of the arduino and start breaking it apart in order to get a better understanding of what is happening.  Here is the arduino output:

 

home/ashwini/Downloads/arduino-1.8.3/arduino-builder -dump-prefs -logger=machine -hardware /home/ashwini/Downloads/arduino-1.8.3/hardware -hardware /home/ashwini/.arduino15/packages -tools /home/ashwini/Downloads/arduino-1.8.3/tools-builder -tools /home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr -tools /home/ashwini/.arduino15/packages -built-in-libraries /home/ashwini/Downloads/arduino-1.8.3/libraries -libraries /home/ashwini/Arduino/libraries -fqbn=SparkFun:avr:promicro:cpu=8MHzatmega32U4 -ide-version=10803 -build-path /tmp/arduino_build_569596 -warnings=all -build-cache /tmp/arduino_cache_732047 -prefs=build.warn_data_percentage=75 -verbose /home/ashwini/Arduino/blinky_c_no_serial_for_avrgcc/blinky_c_no_serial_for_avrgcc.ino

/home/ashwini/Downloads/arduino-1.8.3/arduino-builder -compile -logger=machine -hardware /home/ashwini/Downloads/arduino-1.8.3/hardware -hardware /home/ashwini/.arduino15/packages -tools /home/ashwini/Downloads/arduino-1.8.3/tools-builder -tools /home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr -tools /home/ashwini/.arduino15/packages -built-in-libraries /home/ashwini/Downloads/arduino-1.8.3/libraries -libraries /home/ashwini/Arduino/libraries -fqbn=SparkFun:avr:promicro:cpu=8MHzatmega32U4 -ide-version=10803 -build-path /tmp/arduino_build_569596 -warnings=all -build-cache /tmp/arduino_cache_732047 -prefs=build.warn_data_percentage=75 -verbose /home/ashwini/Arduino/blinky_c_no_serial_for_avrgcc/blinky_c_no_serial_for_avrgcc.ino

Using board 'promicro' from platform in folder: /home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6

Using core 'arduino' from platform in folder: /home/ashwini/Downloads/arduino-1.8.3/hardware/arduino/avr

Detecting libraries used...

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp" -o "/dev/null"

Generating function prototypes...

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp" -o "/tmp/arduino_build_569596/preproc/ctags_target_for_gcc_minus_e.cpp"

"/home/ashwini/Downloads/arduino-1.8.3/tools-builder/ctags/5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "/tmp/arduino_build_569596/preproc/ctags_target_for_gcc_minus_e.cpp"

Compiling sketch...

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp" -o "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp.o"

Compiling libraries...

Compiling core...

Using precompiled core

Linking everything together...

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-gcc" -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega32u4 -o "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.elf" "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp.o" "/tmp/arduino_build_569596/../arduino_cache_732047/core/core_SparkFun_avr_promicro_cpu_8MHzatmega32U4_1e6cbd299ffad185359e932e3440541e.a" "-L/tmp/arduino_build_569596" -lm

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.elf" "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.eep"

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-objcopy" -O ihex -R .eeprom "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.elf" "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.hex"

Sketch uses 230 bytes (0%) of program storage space. Maximum is 28672 bytes.
Global variables use 0 bytes (0%) of dynamic memory, leaving 2560 bytes for local variables. Maximum is 2560 bytes.

/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avrdude -C/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/avrdude.conf -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.hex:i

These were the set of commands used and yes it seems very daunting, but there is a method to madness that you can parse away, hopefully this will also help people get a better understanding of how to reverse engineer things (even though I am pretty bad at it). The thing to note is that these commands can be directly ran on the Linux command line. The first step for me was to get rid of the everything that depends on ino files and look for files that are c/c++ related (files with endings of c and cpp). The first thing to note is that the first 2 lines convert the into a tmp directory and build a cpp file and those 2 lines are:

home/ashwini/Downloads/arduino-1.8.3/arduino-builder -dump-prefs -logger=machine -hardware /home/ashwini/Downloads/arduino-1.8.3/hardware -hardware /home/ashwini/.arduino15/packages -tools /home/ashwini/Downloads/arduino-1.8.3/tools-builder -tools /home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr -tools /home/ashwini/.arduino15/packages -built-in-libraries /home/ashwini/Downloads/arduino-1.8.3/libraries -libraries /home/ashwini/Arduino/libraries -fqbn=SparkFun:avr:promicro:cpu=8MHzatmega32U4 -ide-version=10803 -build-path /tmp/arduino_build_569596 -warnings=all -build-cache /tmp/arduino_cache_732047 -prefs=build.warn_data_percentage=75 -verbose /home/ashwini/Arduino/blinky_c_no_serial_for_avrgcc/blinky_c_no_serial_for_avrgcc.ino

/home/ashwini/Downloads/arduino-1.8.3/arduino-builder -compile -logger=machine -hardware /home/ashwini/Downloads/arduino-1.8.3/hardware -hardware /home/ashwini/.arduino15/packages -tools /home/ashwini/Downloads/arduino-1.8.3/tools-builder -tools /home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr -tools /home/ashwini/.arduino15/packages -built-in-libraries /home/ashwini/Downloads/arduino-1.8.3/libraries -libraries /home/ashwini/Arduino/libraries -fqbn=SparkFun:avr:promicro:cpu=8MHzatmega32U4 -ide-version=10803 -build-path /tmp/arduino_build_569596 -warnings=all -build-cache /tmp/arduino_cache_732047 -prefs=build.warn_data_percentage=75 -verbose /home/ashwini/Arduino/blinky_c_no_serial_for_avrgcc/blinky_c_no_serial_for_avrgcc.ino

I at this point realized that the c/cpp files are stored in

/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp

because it is the same name as ino file also. I then removed the first 2 lines and commented out lines for generating function prototypes just to see what it would do and it seemed to have no impact on the build process. Note: Right before running the avrdude command to upload I had to always reset the MCU in order for the upload to be successful. In order to make sure I was doing everything right, I would first use the led blinking script to make sure things are working and then just compile the script that doesn’t have any dependency on Arduino and then try to upload everything one line at a time on the cmdline. So the new script now became:

 

#removed all the dependency of the arduino core libraries and removed all appearance of ino files, even though ino files have not been removed

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp" -o "/dev/null"

#Generating function prototypes...

#"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp" -o "/tmp/arduino_build_569596/preproc/ctags_target_for_gcc_minus_e.cpp"

#"/home/ashwini/Downloads/arduino-1.8.3/tools-builder/ctags/5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "/tmp/arduino_build_569596/preproc/ctags_target_for_gcc_minus_e.cpp"

#Compiling sketch...

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp" -o "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp.o"

#Compiling libraries...

#Compiling core...

#Using precompiled core

#Linking everything together...

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-gcc" -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega32u4 -o "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.elf" "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp.o" "/tmp/arduino_build_569596/../arduino_cache_732047/core/core_SparkFun_avr_promicro_cpu_8MHzatmega32U4_1e6cbd299ffad185359e932e3440541e.a" "-L/tmp/arduino_build_569596" -lm

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.elf" "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.eep"

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-objcopy" -O ihex -R .eeprom "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.elf" "/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.hex"

#Sketch uses 230 bytes (0%) of program storage space. Maximum is 28672 bytes.
#Global variables use 0 bytes (0%) of dynamic memory, leaving 2560 bytes for local variables. Maximum is 2560 bytes.

/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avrdude -C/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/avrdude.conf -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:/tmp/arduino_build_569596/blinky_c_no_serial_for_avrgcc.ino.hex:i

#this still turns out to be succesfull

As you can see the ino depedency is gone. The ‘#’ in these scripts represent a comment. I played around with the ctags but that seemed to very little impact on my code so I removed all ctags information with the following command:

rm /tmp/arduino_build_569596/preproc/ctags_target_for_gcc_minus_e.cpp

and it didn’t seem to have a large impact. The next step was to completely remove all notion of using tmp files and copy the c file that I created which I wrote as an ino file and move it to my git repo so I could play with the file. The file I created was called led.c (yea I know I am using confusing nomenclature, but at after a certain point at night everything starts to blur together). The next code got rid of all dependency on the tmp directory and it looked like this

#replacing all tmp directory calls to be in the git repo
"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.cpp" -o "/dev/null"

#Generating function prototypes...

#"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/tmp/arduino_build_569596/sketch/blinky_c_no_serial_for_avrgcc.ino.cpp" -o "/tmp/arduino_build_569596/preproc/ctags_target_for_gcc_minus_e.cpp"

#"/home/ashwini/Downloads/arduino-1.8.3/tools-builder/ctags/5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "/tmp/arduino_build_569596/preproc/ctags_target_for_gcc_minus_e.cpp"

rm /tmp/arduino_build_569596/preproc/ctags_target_for_gcc_minus_e.cpp

#Compiling sketch...

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.cpp" -o "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.cpp.o"




#Compiling libraries...

#Compiling core...

#Using precompiled core

#Linking everything together...

rm /tmp/arduino_build_569596/../arduino_cache_732047/core/core_SparkFun_avr_promicro_cpu_8MHzatmega32U4_1e6cbd299ffad185359e932e3440541e.a

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-gcc" -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega32u4 -o "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.elf" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.cpp.o" "-L/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide" -lm

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.elf" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.eep"

"/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avr-objcopy" -O ihex -R .eeprom "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.elf" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.hex"

#Sketch uses 230 bytes (0%) of program storage space. Maximum is 28672 bytes.
#Global variables use 0 bytes (0%) of dynamic memory, leaving 2560 bytes for local variables. Maximum is 2560 bytes.

read -p "Reset the sparkfun micro and then press any key to flash the arduino"

/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avrdude -C/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/avrdude.conf -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.hex:i

#this works also I was able to replace all the extra calls to the tmp directory and do it all in c

This was the biggest clue I was going down the right path, because I used a file called led.c:

#include <avr/io.h>
#include <util/delay.h>

#define BLINK_DELAY_MS 100

int main(void);
int main (void)
{
 /* set pin 5 of PORTB for output*/
 (*(volatile unsigned int*)(0x24)) |= (1<<(5));

while(1) {
 /* set pin 5 high to turn led on */
 (*(volatile unsigned int*)(0x25)) |= (1<<(5));

/* set pin 5 low to turn led off */
 (*(volatile unsigned int*)(0x25)) &= (0<<(5));
 }
}

and since this worked I knew everything was on the right path and remember I always verify by going back to a known state which is using the scrip that blinks to LEDs using the arduino and then I can always come back to toggling the PB5  when I upload my code so that really helped. The next step was to remove all commands being executed from the arduino directory and use the base avr-gcc available on my linux computer and the script was:

#replacing all tmp directory calls to be in the git repo
avr-gcc -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -flto -w -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.c" -o "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/log.txt"

#Compiling sketch...

avr-gcc -c -g -Os -Wall -Wextra -fno-exceptions -ffunction-sections -fdata-sections -MMD -flto -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10803 -DARDUINO_AVR_PROMICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x1b4f -DUSB_PID=0x9204 '-DUSB_MANUFACTURER="Unknown"' '-DUSB_PRODUCT="SparkFun Pro Micro"' "-I/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/variants/promicro" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.c" -o "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.o"




#Compiling libraries...

#Compiling core...

#Using precompiled core

#Linking everything together...

avr-gcc -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega32u4 -o "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.elf" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.o" "-L/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide" -lm

avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.elf" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.eep"

avr-objcopy -O ihex -R .eeprom "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.elf" "/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.hex"

#Sketch uses 230 bytes (0%) of program storage space. Maximum is 28672 bytes.
#Global variables use 0 bytes (0%) of dynamic memory, leaving 2560 bytes for local variables. Maximum is 2560 bytes.

read -p "Reset the sparkfun micro and then press any key to flash the arduino"

avrdude -C/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/avrdude.conf -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:/home/ashwini/git-repos/arduino/led_blink_without_arduino_ide/led.hex:i

#this works also I was able to replace all the extra calls to the tmp directory and do it all in c, removed all c++ specific flags. Now removed all the arduino specific gcc calls and now using system avr-gcc and avr-objcopy and avrdude

The read part I have before avr dude allows me to reset the pro micro before I flash it using avr dude. I completely removed all arduino related dependency.  I still do need to point to the ProMicro configuration file in order to use the header files that are available and to properly upload the executable file.  This allowed me to completely run without the arduino IDE. The next step is to figure out how all the functions work. I looked in ELF (Executable and Linkable Format) files which are extremely useful in these conversations and i found this link and this link, which were extremely useful in explaining how an elf file is created and made. This link was also useful. I was still in the process of decoupling all the flags but

-ffunction-sections -fdata-sections

are used so that every function I create get goes in a seperate section and each piece of data gets a seperate section. The -D flag are macros, which can also be set in a #define as shown in this link. Here is another link that explain those compiler optimizations.

UPDATE:

As I was playing around I have noticed that the manual version that I created to compile and upload the to sparkfun causes an issue.  The reason for this because the arduino has a main.ccp file which runs code called USBDevice.attach() which is relevant for arduino leonard (or the sparkfun pro micro for that matter). This is important because it allows the usb device to stay attached after the hex file is uploaded to mcu. If this is not in your file I saw /dev/ttyACM0 get attached for 5-15 secs and then you see the device disappear. There is a usb attach function that keeps it attached but for now I think that will be too confusing for me so I have decided to switch to Arduino uno to make my life a little easier. I will let you know when I come back to sparkfun pro micro.

As always here is the link to the github.

Here is my list of useful links:

https://arduino.stackexchange.com/questions/9064/trying-to-track-down-macro-or-define-values-possibly-in-arduino-preprocessor

https://balau82.wordpress.com/2011/03/29/programming-arduino-uno-in-pure-c/

http://www.nongnu.org/avrdude/user-manual/avrdude_10.htmlhttps://www.arduino.cc/en/Hacking/Bootloader

https://github.com/arduino/Arduino/wiki/Build-Process

https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification

https://github.com/arduino/Arduino/wiki/Arduino-Hardware-Cores-migration-guide-from-1.0-to-1.6

https://forum.arduino.cc/index.php?topic=158701.0

https://www.mankier.com/1/arduino-ctagshttps://stackoverflow.com/questions/20177113/how-to-disable-gcc-warning-cc1-warning-command-line-option-std-c11-is-va

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options

https://arduino.stackexchange.com/questions/9064/trying-to-track-down-macro-or-define-values-possibly-in-arduino-preprocessor

https://groups.google.com/forum/#!topic/fa.freebsd.current/RUZnbnYykQo

ELF information:

http://www.linuxjournal.com/article/1059?page=0,1

http://www.cirosantilli.com/elf-hello-world/

http://www.linuxjournal.com/article/1060

https://linux-audit.com/elf-binaries-on-linux-understanding-and-analysis/https://groups.google.com/forum/#!topic/fa.freebsd.current/RUZnbnYykQo

 

 

 

 

Advertisements

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