#   Copyright (c) 2010 Axel Wachtler
#   All rights reserved.
#
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the authors nor the names of its contributors
#     may be used to endorse or promote products derived from this software
#     without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#   POSSIBILITY OF SUCH DAMAGE.

# $Id$

#=== modules ==================================================================
import pprint, glob, os
Import("avr board")

#=== setup build environment ==================================================
avrlocal = avr.Clone()

avrlocal["BOARD_TYPE"] = "psk231"
#avrlocal['CPU'] = avr['boardopts'][board].cpu
avrlocal['BOOT_LOADER_ADDRESS'] = hex(int(avr['boardopts'][board].blstart/2))
avrlocal['BOOT_LOADER_OFFSET'] = hex(int(avr['boardopts'][board].blstart))

bdir=Dir(".").path

ccflags = avrlocal.Split("-Wall -Wundef -g$DBGFMT -Os -fshort-enums -fpack-struct -std=c99" )

avrlocal['radiolib'] = avrlocal.subst("radio_${BOARD_TYPE}")
avrlocal['iolib'] = avrlocal.subst("io_${BOARD_TYPE}")
avrlocal['AR'] = "avr-ar"
avrlocal['LINK'] = "avr-gcc"
avrlocal['RANLIB'] = "avr-ranlib"
avrlocal['CPPPATH'] = ["${dir_install.abspath}/inc","./Inc"]
avrlocal['LIBPATH'] = "${dir_install.abspath}/lib"


apps = []

#=== build rose host applications =============================================
for board in ["psk230", "psk231", "stb231", "radiofaro"]:
    if not avr['boardopts'].has_key(board):
        continue
    current_ccflags = ccflags + \
                      [ "-DF_CPU=%s" % avr['boardopts'][board].f_cpu,
                        "-D%s" % board ,
                        "-mmcu=%s" % avr['boardopts'][board].cpu]
    current_linkflags="-mmcu=%s -Wl,--section-start,.bootloader=0x%x" %\
                       (avr['boardopts'][board].cpu, int(avr['boardopts'][board].blstart))

    o = avrlocal.Object("host_%s.o" % board , "host.c",
                        CCFLAGS = current_ccflags)
    elf = avrlocal.Program('rosehost_%s.elf' % board,
                               [o],
                               LIBS=["radio_%s" % board, "io_%s" % board],
                               LINKFLAGS=current_linkflags)
    hex = avrlocal.Command('rosehost_%s.hex' % board, elf,
                           'avr-objcopy -O ihex $SOURCE $TARGET')

    apps += [elf, hex]


#=== build rose sensor applications ===========================================
if avr['boardopts'].has_key("rose231"):
    board = "rose231"
    current_ccflags = ccflags + \
                  [ "-DF_CPU=%s" % avr['boardopts'][board].f_cpu,
                    "-D%s" % board ,
                    "-mmcu=%s" % avr['boardopts'][board].cpu, "-O2"]

    current_linkflags="-mmcu=%s -Wl,--section-start,.bootloader=0x%x" %\
                   (avr['boardopts'][board].cpu, int(avr['boardopts'][board].blstart))

    elf = avrlocal.Program('rose.elf',
                       ["i2c.c", "rose.c", "sensors.c", "selftest.c", "sensorproto.c"],
                       CCFLAGS = current_ccflags,
                       LIBS=["radio_%s" % board, "io_%s" % board],
                       LINKFLAGS=current_linkflags)
    hex = avrlocal.Command('rose.hex', elf,
                       'avr-objcopy -O ihex $SOURCE $TARGET')
    apps += [elf, hex]

#=== install binaries =========================================================
inst_apps = avrlocal.Install("#install/bin",apps)

#=== main target of this SConscript ===========================================
avrlocal.Alias("roseapps", inst_apps)
