#
# Copyright 2010 by Adam Mayer	 <phooky@gmail.com>
#

#
# HOW TO USE THIS BUILD SCRIPT
#
# To upload the firmware via AVRISP, specify the "upload" target.  
# $ scons upload
#

import os
import re
from os.path import dirname
# Parameters
f_cpu='8000000L'
mcu='atmega328p'

target="WheyStation"
srcs = Glob('*.cc')

flags=[
	'-DF_CPU='+str(f_cpu),
	'-mmcu='+mcu,
	'-g',
	'-Os',
	'-w',
	'-fno-exceptions',
	'-ffunction-sections',
	'-fdata-sections']

env=Environment(CC="avr-g++",
	CXX="avr-g++",
	CCFLAGS=flags)
objs = env.Object(srcs)

hex_name = target + '.hex'
elf_name = target + '.elf'

env.Append(BUILDERS={'Elf':Builder(action="avr-gcc -mmcu="+mcu+" -Os -Wl,--gc-sections -o $TARGET $SOURCES")})
env.Append(BUILDERS={'Hex':Builder(action="avr-objcopy -O ihex -R .eeprom $SOURCES $TARGET")})
env.Elf(elf_name, objs) 
env.Hex(hex_name, elf_name)

avrdude_base = "avrdude -V -F -p m328p -c usbtiny"

avrdude_cmd = avrdude_base + " -U flash:w:$SOURCES"
avrdude_fuses = avrdude_base + " -U lfuse:w:0xe2:m -U hfuse:w:0xd9:m"

# env.Alias instead of just Alias because of
# http://scons.tigris.org/issues/show_bug.cgi?id=2443
upload_alias = env.Alias('upload', hex_name, avrdude_cmd)
fuses_alias = env.Alias('upload-fuses', hex_name, avrdude_fuses)
AlwaysBuild(upload_alias)
AlwaysBuild(fuses_alias)
