[SOLVED!] Compiling without makefiles.
Moderators: adafruit_support_bill, adafruit

[SOLVED!] Compiling without makefiles.

by enhering on Wed Jun 06, 2012 6:05 pm

Hi.

I'm trying to write a script that compiles a simple code (see below) and uploads it to a BBB ModernDevice Arduino 328P based board. I want to use avr-gcc to compile and an USBtinyISP to upload the code.

The sample program is called test main.cpp and follows below:

Code: Select all | TOGGLE FULL SIZE
#include <avr/io.h>
#include <util/delay.h>
 
int main(void) {

  DDRB = 0xff;
  DDRD = 0xff;
 
  while(1) {
    _delay_ms(1000);
    PORTB = 0xff;
    PORTD = 0xff;
    _delay_ms(1000);
    PORTB = 0x00;
    PORTD = 0x00;
  }

  return 0;
}


To build and link it I have written the following bash script adapted from pieces of makefiles:

Code: Select all | TOGGLE FULL SIZE
#!/bin/sh

MCU=atmega328p
PROGRAMMER_MCU=m328p
AVRDUDE_PROGRAMMERID=usbtiny
AVRDUDE_PORT=""
AVRDUDE_BAUD="-b 57600"

HEXFORMAT=ihex

FLAGS="-fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall -fno-exceptions"
DEFINES="-DF_CPU=16000000UL"
LINKS="-lm"
INCLUDES=""

#compiles the code
avr-gcc -c -mmcu=$MCU -O1 $FLAGS $DEFINES $LINKS $INCLUDES \
                testmain.cpp --output testmain.elf

#transforms it into hex format
avr-objcopy -j .text  -j .data -O $HEXFORMAT testmain.elf testmain.hex

#uploads it to Arduino board
avrdude -c $AVRDUDE_PROGRAMMERID -v  \
             -p $PROGRAMMER_MCU $AVRDUDE_PORT \
             -e $AVRDUDE_BAUD -U flash:w:testmain.hex



The result I get from AVRDUDE is:

Code: Select all | TOGGLE FULL SIZE
avrdude: Version 5.11.1, compiled on Feb 16 2012 at 22:11:20
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/usr/local/CrossPack-AVR-20120217/etc/avrdude.conf"
         User configuration file is "/Users/enhering/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : unknown
         Using Programmer              : usbtiny
         Overriding Baud Rate          : 57600
avrdude: usbdev_open(): Found USBtinyISP, bus:device: 001:002-1781-0c9f-ff-00
         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 : USBtiny
         Description     : USBtiny simple USB programmer, http://www.ladyada.net/make/usbtinyisp/
avrdude: programmer operation not supported

avrdude: Using SCK period of 10 usec
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f
avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as DA
avrdude: safemode: efuse reads as 5
avrdude: erasing chip
avrdude: Using SCK period of 10 usec
avrdude: reading input file "../hex/testmain.hex"
avrdude: input file ../hex/testmain.hex auto detected as Intel Hex
avrdude: writing flash (52 bytes):

Writing | ################################################## | 100% 0.07s



avrdude: 52 bytes of flash written
avrdude: verifying flash memory against ../hex/testmain.hex:
avrdude: load data flash data from input file ../hex/testmain.hex:
avrdude: input file ../hex/testmain.hex auto detected as Intel Hex
avrdude: input file ../hex/testmain.hex contains 52 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.03s



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

avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as DA
avrdude: safemode: efuse reads as 5
avrdude: safemode: Fuses OK

avrdude done.  Thank you.

ENHering:actions enhering$


which shows that the code has been uploaded successfully.

But the LEDs do not blink. Am I missing something?
Last edited by enhering on Wed Jun 06, 2012 7:42 pm, edited 1 time in total.
enhering
 
Posts: 5
Joined: Tue Jun 05, 2012 5:03 pm

SOLVED!

by enhering on Wed Jun 06, 2012 7:41 pm

This code works and you do not need to use mystic makefiles:

Code: Select all | TOGGLE FULL SIZE
#!/bin/sh

MCU=atmega328p
PROGRAMMER_MCU=m328p
AVRDUDE_PROGRAMMERID=usbtiny
AVRDUDE_PORT=""
AVRDUDE_BAUD="-b 57600"

HEXFORMAT=ihex

FLAGS="-fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall -fno-exceptions"
DEFINES="-DF_CPU=16000000UL"
LINKS="-lm"
INCLUDES=""

#compiles the code
avr-gcc -mmcu=$MCU -O1 $FLAGS $DEFINES $LINKS $INCLUDES \
            testmain.cpp -o testmain.out

#transforms it into hex format
avr-objcopy -j .text -O $HEXFORMAT testmain.out testmain.hex

#uploads it to Arduino board
avrdude -c $AVRDUDE_PROGRAMMERID -v  \
             -p $PROGRAMMER_MCU $AVRDUDE_PORT \
             -e $AVRDUDE_BAUD -U flash:w:testmain.hex
enhering
 
Posts: 5
Joined: Tue Jun 05, 2012 5:03 pm

Re: [SOLVED!] Compiling without makefiles.

by westfw on Wed Jun 06, 2012 9:47 pm

avr-gcc -c -mmcu=$MCU -O1 $FLAGS $DEFINES $LINKS $INCLUDES \
testmain.cpp --output testmain.elf

The "-c" switch given to the compiler means "compile only, do not link." So it won't produce a properly relocated and executable binary (although the .o format for intermediate compiled code is the same as the .elf file for final binaries, as far as objcopy is concerned, so you won't get errors in the subsequent steps.)
User avatar
westfw
 
Posts: 1373
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area