#   Copyright (c) 2007 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
pp = lambda x : pprint.pprint(x)

Import("avr board")

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

avrlocal["BOARD_TYPE"] = board
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

avrlocal['CPPPATH'] = "inc"
ccflags = avrlocal.Split("""-Wall -Wundef
             -Os -g$DBGFMT -fno-inline
             -mmcu=$CPU
             -D$BOARD_TYPE
             %s
          """ % avr['boardopts'][board].get_ccopts())

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

avrlocal['radiolib'] = avrlocal.subst("radio_${BOARD_TYPE}")
avrlocal['iolib'] = avrlocal.subst("io_${BOARD_TYPE}")
avrlocal['uracolilib'] = avrlocal.subst("uracoli_${BOARD_TYPE}")

avrlocal['CPPPATH'] = ["Inc","Ioutil","${dir_install.abspath}/inc"]
avrlocal['CCFLAGS'] = avrlocal.subst(" ".join(ccflags))
avrlocal['LINKFLAGS'] = "-Wl,-Map=${TARGET.dir}/${TARGET.filebase}.map -mmcu=$CPU -L%s" % bdir
avrlocal['AR'] = "avr-ar"
avrlocal['RANLIB'] = "avr-ranlib"

# === targets =========================================================
iolib = avrlocal.StaticLibrary(
            target = "${iolib}",
            source = [
                      "Ioutil/timer.c",
                      "Ioutil/timer_tstamp.c",
                      "Ioutil/hif_uart.c",
                      "Ioutil/hif_ft245.c",
                      "Ioutil/hif_at90usb.c",
                      "Ioutil/hif_print.c",
                      "Ioutil/hif_dump.c",
                      "Ioutil/lin_buffer.c",
                      "Ioutil/stream_io.c",
                      #"Ioutil/mcu_osccal.c",
                      #"Ioutil/one_wire.c",
                   ])

radiolib = avrlocal.StaticLibrary(
            target = "${radiolib}",
            source = ["P2P/p2p.c",
                      "Rf230/radio_rfa.c",
                      "Rf230/trx_rfa.c",
                      "Rf230/radio_rf230.c",
                      "Rf230/trx_rf230.c",
                      "Rf230/trx_rf230_bitrd.c",
                      "Rf230/trx_rf230_bitwr.c",
                      "Rf230/trx_rf230_frame.c",
                      "Rf230/trx_rf230_sram.c",
                      "Rf230/trx_rf230_irq.c",
                      "Rf230/trx_rf230_crc.c",
                      "Rf230/trx_rf230_misc.c",
                      "Rf230/trx_rf230_param.c",
                      "Rf230/trx_datarate.c",
                      "Rf230/trx_datarate_str.c",
                      "Rf230/usr_radio_error.c",
                      "Rf230/usr_radio_irq.c",
                      "Rf230/usr_radio_receive_frame.c",
                      "Rf230/usr_radio_tx_done.c"])

uracolilib = avrlocal.StaticLibrary(
            target = "${uracolilib}",
            source = Glob("*/*.c"))

# === install =========================================================
libs = [radiolib, iolib, uracolilib]
libinst = avrlocal.Install('${dir_install.abspath}/lib', libs)

# === aliases =========================================================
avrlocal.Alias('l${BOARD_TYPE}', libinst)
