#   Copyright (c) 2007 - 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("avr board")

#=== setup build environment ==================================================
avrxmpl = avr.Clone()
avrxmpl['BOARD_TYPE'] = board
avrxmpl['CPU'] = avr['boardopts'][board].cpu
avrxmpl['BOOT_LOADER_ADDRESS'] = hex(int(avr['boardopts'][board].blstart/2))
avrxmpl['BOOT_LOADER_OFFSET'] = hex(int(avr['boardopts'][board].blstart))
bdir=Dir(".").path

ccflags = avrxmpl.Split("""-Wall -Wundef
             -g$DBGFMT
             -Os
             -mmcu=${CPU}
             -D${BOARD_TYPE}
             %s
              """ % avr['boardopts'][board].get_ccopts())

if avrxmpl['cmdline_baudrate']:
    ccflags.append("-DHIF_DEFAULT_BAUDRATE=%s" % avrxmpl['cmdline_baudrate'])

avrxmpl['CCFLAGS'] = avrxmpl.subst(" ".join(ccflags))
avrxmpl['LINKFLAGS'] = "-Wl,-Map=${TARGET.dir}/${TARGET.filebase}.map -mmcu=$CPU"
avrxmpl['AR'] = "avr-ar"
avrxmpl['LINK'] = "avr-gcc"
avrxmpl['RANLIB'] = "avr-ranlib"
avrxmpl['CPPPATH'] = ["${dir_install.abspath}/inc","./Inc"]
avrxmpl['LIBPATH'] = "${dir_install.abspath}/lib"

xmpl_general =  [ 'xmpl_keys',
              'xmpl_key_events',
              'xmpl_leds',
              'xmpl_dbg',
              'xmpl_timer',
              'xmpl_hif',
              'xmpl_hif_echo',
              'xmpl_trx_base',
              'xmpl_trx_tx',
              'xmpl_trx_rx',
              'xmpl_trx_echo',
              'xmpl_trx_txaret',
              'xmpl_trx_rxaack',
              'xmpl_linbuf_tx',
              'xmpl_linbuf_rx',
              'xmpl_radio_range',
              'xmpl_radio_stream',
#              'xmpl_1wire_ds1820',
        ]

xapps = []
xapsfiles = []
xmakefiles = []
import pdb

# === build list with example applications ===
myboard = avrxmpl['boardopts'][board]
xmplapps = []
for xmpl in xmpl_general:
    #if board not in glblexclude and xmpl not in myboard.NO_XMPL:
    if xmpl not in myboard.NO_XMPL:
        xmplapps.append(xmpl)

xmplapps += myboard.MORE_XMPLS

# === setup the build rules for the example ===
for xmpl in xmplapps:
    avrxmpl['xapp'] = xmpl
    xelf = avrxmpl.Program('${xapp}_${BOARD_TYPE}.elf',
                           '${xapp}.c',
                           LIBS=["uracoli_${BOARD_TYPE}"])
    xhex = avrxmpl.Command('${xapp}_${BOARD_TYPE}.hex', xelf,
                           "avr-objcopy -O ihex $SOURCE $TARGET")

    # look for "${xxx}" in the XSLT file for getting all variables to be used
    xparm= { 'app' : avrxmpl.subst('${xapp}'),
             'brd' : avrxmpl.subst('${BOARD_TYPE}'),
             'projname' : avrxmpl.subst('${xapp}_${BOARD_TYPE}'),
             'src' : avrxmpl.subst('${xapp}.c'),
             'ccflags' : avrxmpl['CCFLAGS'],
             'part' : avrxmpl['CPU'],
             'f_cpu' : avrxmpl['boardopts'][board].F_CPU
             }
    xapps += [xelf, xhex]

#=== targets ==================================================================
xinst = avrxmpl.Install('${dir_install.abspath}/bin', xapps)
avrxmpl.Alias('x${BOARD_TYPE}', xinst )
