Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
stockerc
PostPosted: Jun 15, 2010 - 05:14 AM
Newbie


Joined: Mar 25, 2008
Posts: 7


In the interest of making AVR programing a little easier on Ubuntu I decided to write a simple little script. It does not handle all the possible options but does do what I need and can be easily modified.

Code:

#!/bin/bash

#I coppied the script to the /usr/bin directory
#to make it easier to use sudo avr.sh /usr/bin/avr.sh
#I used the commands listed on instructables.com
#article "Getting started with ubuntu and the AVR dragon"
#the artical did leave out one important option
#sudo apt-get install avrlibc
#helpful if you like to use the include files like avr/io.h

if [ $1 == c ] && [ $# = 3 ] #compile option
#argument 0 is the command
#argument 1 is the option
#argument 2 is the project to be compiled
#argument 3 is the proccessor to comple for
#example avr.sh c blinking atmega8
then
   avr-gcc -g -Os -mmcu=$3 -c $2.c
   avr-gcc -g -mmcu=$3 -o $2.elf $2.o
   avr-objcopy -j .text -j .data -O ihex $2.elf $2.hex
elif [ $1 == "clean" ]
#Clean option to used to remove files created from compiling
#argument 0 is the command
#argument 1 is the option
then
   rm *.hex *.elf *.o

elif [ $1 == "p" ] && [ $# > 4 ]
#program option to be used with avrdue
#argument 0 is the command
#argument 1 is the option
#argument 2 is the project to be programed
#argument 3 is the programer to be used
#argument 4 is the proccessor to programed
then
   avrdude -c $3 -e -p $4   -U flash:w:$2.hex $5
else
   echo "Incorrect format"
   echo "There are three options, compile project, clean, and program"
   echo "Options:"
   echo "avr.sh c project atmega"
   echo "avr.sh clean"
   echo "avr.sh p project usbasp atmega8 -F"
fi


Hope someone finds this useful.
 
 View user's profile Send private message  
Reply with quote Back to top
ArnoldB
PostPosted: Jun 15, 2010 - 06:58 AM
Raving lunatic


Joined: Nov 29, 2007
Posts: 3219


man make
man mfile
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 15, 2010 - 09:36 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62899
Location: (using avr-gcc in) Finchingfield, Essex, England

Agree with Arnold. About 40 years work has gone into "make" first in Unix then the GNU make in Linux to make possibly the best scripting language for building code that could possibly ever exist. Then you throw this aside with some half baked bash script. Wouldn't your time have been better spent reading "man make" as Arnold suggests for an hour or two? In fact the GNU make manual:

http://www.gnu.org/software/make/manual/

is actually a very readable document (kind of unusual for open source documentation in fact!). I'd suggest readers of this thread expend their time doing that and also exploring the Mfile utility that takes the pain out of creating/editing Makefiles:

http://www.sax.de/~joerg/mfile/

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
ArnoldB
PostPosted: Jun 15, 2010 - 06:59 PM
Raving lunatic


Joined: Nov 29, 2007
Posts: 3219


All hail Stuart Feldman.
 
 View user's profile Send private message  
Reply with quote Back to top
Artcfox
PostPosted: Jun 16, 2010 - 04:57 AM
Rookie


Joined: Jan 21, 2010
Posts: 23
Location: Reston, VA

stockerc, Don't let anyone discourage you from sharing what you've learned. You had a problem, and you solved it using the methods and tools that you were comfortable with. That is the essence of hacking and tinkering!

I recently made the switch from OS X back to Linux, and I really missed the way that CrossPack for OS X was configured. It came with a handy script for creating new projects called `avr-project` which creates a new directory based on a skeleton project and Makefile. Say you want to create a project called my_test, you would run `avr-project my_test` and then you would have a new directory called my_test which contains a Makefile and a main.c that you can customize for that particular project.

I modified the avr-project script slightly so it integrates nicely with the directory structure on a Linux system:
Code:
#!/bin/sh
if [ $# != 1 ]; then
echo "usage: $0 <ProjectName>" 1>&2
exit 1
fi
if [ "$1" = "--help" -o "$1" = "-h" ]; then
{
echo "This command creates an empty project with template files";
echo
echo "usage: $0 <ProjectName>"
} 1>&2
exit 0;
fi

name=`basename "$1"`
dir=`dirname "$1"`
cd "$dir"
if [ -x "$name" ]; then
echo "An object named $name already exists." 1>&2
echo "Please delete this object and try again." 1>&2
exit 1
fi
template=~/.avr-project/templates/TemplateProject
if [ ! -d "$template" ]; then
template="/usr/local/avr-project/templates/TemplateProject"
fi
echo "Using template: $template"
cp -r "$template" "$name" || exit 1
cd "$name" || exit 1
#mv TemplateProject.xcodeproj "$name.xcodeproj"


Save that script in a file called `avr-project` somewhere in your path, perhaps in ~/bin/ or /usr/local/bin/, and make it executable by typing `chmod 755 avr-project`

Now all you need to do is create a skeleton project directory in one of the two places that the script expects it.

Either:
Code:
mkdir -p ~/.avr-project/templates/TemplateProject/

or (as root):
Code:
mkdir -p /usr/local/avr-project/templates/TemplateProject/

and populate it with a default Makefile (this is mine):
Code:
# Name: Makefile
# Author: <insert your name here>
# Copyright: <insert your copyright message here>
# License: <insert your license reference here>

# This is a prototype Makefile. Modify it according to your needs.
# You should at least check the settings for
# DEVICE ....... The AVR device you compile for
# CLOCK ........ Target AVR clock rate in Hertz
# OBJECTS ...... The object files created from your source files. This list is
#                usually the same as the list of source files with suffix ".o".
# PROGRAMMER ... Options to avrdude which define the hardware you use for
#                uploading to the AVR and the interface where this hardware
#                is connected.
# FUSES ........ Parameters for avrdude to flash the fuses appropriately.

DEVICE     = atmega328p
CLOCK      = 18432000
#CLOCK      = 16000000
#CLOCK      = 8000000
#CLOCK      = 1000000
PROGRAMMER = -c avrispmkII -P usb
OBJECTS    = main.o

# Default setting for ATmega328P in Arduino Duemilanove
#FUSES      = -U hfuse:w:0xda:m -U lfuse:w:0xff:m

# Default setting for ATmega328P
#FUSES      = -U hfuse:w:0xd9:m -U lfuse:w:0x62:m

# Remove clock divider for ATmega328P
#FUSES      = -U hfuse:w:0xd9:m -U lfuse:w:0xe2:m

# Remove clock divider, set external crystal for ATmega328P
FUSES      = -U hfuse:w:0xd9:m -U lfuse:w:0xe6:m

# Remove clock divider, set external crystal, enable clock output
#FUSES      = -U hfuse:w:0xd9:m -U lfuse:w:0xa6:m

# Tune the lines below only if you know what you are doing:
AVRDUDE    = avrdude $(PROGRAMMER) -p $(DEVICE)
COMPILE    = avr-gcc -std=gnu99 -g -Wall -Winline -O3 -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
#COMPILE    = avr-gcc -std=gnu99 -g -Wall -Winline -O3 -funroll-loops -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
#COMPILE    = avr-gcc -std=gnu99 -g -Wall -Winline -mint8 -O3 -funroll-loops -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)

LINK_FLAGS = -lc -lm

# symbolic targets:
all:   main.hex

.c.o:
   $(COMPILE) -c $< -o $@

.S.o:
   $(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.

.c.s:
   $(COMPILE) -S $< -o $@

flash:   all
   $(AVRDUDE) -U flash:w:main.hex:i

pflash:   all
   $(AVRDUDE) -n -U flash:w:main.hex:i

fuse:
   $(AVRDUDE) $(FUSES)

# Xcode uses the Makefile targets "", "clean" and "install"
install: flash fuse

# if you use a bootloader, change the command below appropriately:
load: all
   bootloadHID main.hex

clean:
   rm -f main.hex main.elf $(OBJECTS)

# file targets:
main.elf: $(OBJECTS)
   $(COMPILE) -o main.elf $(OBJECTS) $(LINK_FLAGS)

main.hex: main.elf
   rm -f main.hex
   avr-objcopy -j .text -j .data -O ihex main.elf main.hex
# If you have an EEPROM section, you must also create a hex file for the
# EEPROM and add it to the "flash" target.

# Targets for code debugging and analysis:
disasm:   main.elf
   avr-objdump -S -d main.elf

cpp:
   $(COMPILE) -E main.c

%.lst: %.c
   { echo '.psize 0' ; $(COMPILE) -S -g -o - $< ; } | avr-as -alhd -mmcu=$(DEVICE) -o /dev/null - > $@

Edit: If you just copied and pasted the above code segment into a file called Makefile it will not work. As Markus pointed out below, this forum converts tab characters into multiple spaces, which will cause the Makefile to fail. As a remedy, I placed the avr-project, Makefile, and main.c in a zip file, which is now attached to the end of this post.

Next add a default main.c file to the TemplateProject directory (this is mine):
Code:
#include <stdint.h>
#include <avr/io.h>

int main() {

  for (;;) {
  }

  return 0;
}


Don't forget to set the DEVICE, CLOCK, PROGRAMMER, and FUSES in the Makefile to what you normally use, so that you don't have to reconfigure those values for every new project.

Common use cases are:
`make` (to compile and create a .hex file)
`make clean` (to delete all output files)
`make flash` (to flash the .hex file onto the AVR)
`make pflash` (like flash, but doesn't actually write to the AVR)
`make fuse` (to program the fuses)
`make disasm` (to see the compiled machine and asm code)
`make main.lst` (to create a file called main.lst, which contains the compiled machine and asm code in a different format)


Last edited by Artcfox on Jun 16, 2010 - 11:46 PM; edited 2 times in total
 
 View user's profile Send private message  
Reply with quote Back to top
stockerc
PostPosted: Jun 16, 2010 - 04:17 PM
Newbie


Joined: Mar 25, 2008
Posts: 7


Thanks Artcfox for the useful information.

I just started moving away from AVR Studio and the majority my other programing experience has been in Visual Studio so I have never had to deal with make files. My theory is you got to start somewhere and then improve on that.
 
 View user's profile Send private message  
Reply with quote Back to top
Artcfox
PostPosted: Jun 16, 2010 - 04:48 PM
Rookie


Joined: Jan 21, 2010
Posts: 23
Location: Reston, VA

stockerc wrote:
Thanks Artcfox for the useful information.

I just started moving away from AVR Studio and the majority my other programing experience has been in Visual Studio so I have never had to deal with make files. My theory is you got to start somewhere and then improve on that.


No problem! That is very true.

Something I just noticed in the Makefile I provided, you may want to change the COMPILE line to:
Code:
COMPILE    = avr-gcc -std=gnu99 -g -Wall -Winline -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)

to optimize for size (-Os) rather than speed (-O3).
 
 View user's profile Send private message  
Reply with quote Back to top
markus_b
PostPosted: Jun 16, 2010 - 05:58 PM
Posting Freak


Joined: Mar 20, 2001
Posts: 1517
Location: Switzerland

I've moved from Windows with AVR-Studio to Linux with Eclipse (and the AVR plugin). Besides the missing simulator Eclipse is a good replacement of Studio. As Studio it has a built-in make mechanism and allows 1-click programming (via avrdude) too.

One remark about the above makefile: The forum does not convey the tab's necessary for its correct function, as it is above it will not work for a make newbie.

_________________
Markus
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
Artcfox
PostPosted: Jun 16, 2010 - 11:48 PM
Rookie


Joined: Jan 21, 2010
Posts: 23
Location: Reston, VA

Markus, thanks for pointing out the issue with this forum mangling the tab characters. I edited my post to include a little note, and created an attachment so the tab characters are preserved.
 
 View user's profile Send private message  
Reply with quote Back to top
ArnoldB
PostPosted: Jun 17, 2010 - 06:35 AM
Raving lunatic


Joined: Nov 29, 2007
Posts: 3219


Artcfox wrote:
stockerc, Don't let anyone discourage you from sharing what you've learned.
Sharing misleading information is frowned upon.

There are decades of established, proven practice how to script a build system on *nixoid operating systems and others, with and without tool support (imake anyone, or automake? Or makefile.pl). And yet still some people try to push the shell script, batch job thingy as the best invention since sliced bread.

Further, there is established, proven practice how to rig up a makefile for an AVR program (mfile).

Those who want to inform themselves can do so by reading about the two mentioned tools. Those who don't are bound to stick with their shell scripts.
 
 View user's profile Send private message  
Reply with quote Back to top
Artcfox
PostPosted: Jun 17, 2010 - 08:55 AM
Rookie


Joined: Jan 21, 2010
Posts: 23
Location: Reston, VA

ArnoldB wrote:
Artcfox wrote:
stockerc, Don't let anyone discourage you from sharing what you've learned.
Sharing misleading information is frowned upon.

There are decades of established, proven practice how to script a build system on *nixoid operating systems and others, with and without tool support (imake anyone, or automake? Or makefile.pl). And yet still some people try to push the shell script, batch job thingy as the best invention since sliced bread.

Further, there is established, proven practice how to rig up a makefile for an AVR program (mfile).

Those who want to inform themselves can do so by reading about the two mentioned tools. Those who don't are bound to stick with their shell scripts.

I'm not condoning sharing misleading information. I just felt that the initial responses were a bit harsh and didn't want that to discourage him from sharing with us things he learns in the future. By posting what he discovered (the shell script), he learned the established way of doing things (Makefiles). Had he not shared it, he might not have learned about Makefiles. That is all I meant by that statement.

This learning process is not that different from me posting the Makefile and then having Markus point out that the forum mangled the tabs, which caused me to do things a better way (as an attachment).
 
 View user's profile Send private message  
Reply with quote Back to top
stockerc
PostPosted: Jun 19, 2010 - 02:22 AM
Newbie


Joined: Mar 25, 2008
Posts: 7


What about this is misleading information Arnold? I said down and worked with a makefile and guess what it produces the same results as what I posted.
 
 View user's profile Send private message  
Reply with quote Back to top
Artcfox
PostPosted: Jun 19, 2010 - 02:46 AM
Rookie


Joined: Jan 21, 2010
Posts: 23
Location: Reston, VA

stockerc wrote:
What about this is misleading information Arnold? I said down and worked with a makefile and guess what it produces the same results as what I posted.


stockerc, your script should not be touted as a way of doing things, because it is not the same as the Makefile.

Take for instance when your project has the source split up into multiple files. In the Makefile, you would just add to the OBJECTS variable:
Code:
OBJECTS = main.o tlc5940.o

but your script won't properly link in multiple object files.

My point was by posting your script, people responded and you discovered how to use Makefiles, which is the proper way.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits