#! /bin/sh
#
# avr-example -- a shell script to bootstrap a new avr project
# 2008-09-01, jw -- initial draught
# 2010-02-08, jw -- Makefile extended with more comments
# 2010-03-23, jw -- no longer part of avr-libc
# 2011-12-27, jw -- example comment, how to add to DIST_EXCLUDE
# 2014-03-29, jw -- added PORTD and simplified the compile call.

# from=/opt/cross/share/doc/avr-libc*/examples/
from=/usr/share/doc/packages/avr-example*/
name=example
mkdir -p $name

cd $name || exit 0
test -f avr_common.mk || cp $from/avr_common.mk .
test -f avr_isp.pl    || cp $from/avr_isp.pl .
chmod a+x avr_isp.pl
test -f blink.c || cat << EOF > blink.c
/*
 * blink.c -- simple LED blinker
 *
 * Copyright (C) 2008, jw@suse.de, distribute under GPL, use with mercy.
 *
 */
// #include "config.h"
#include "cpu_mhz.h"

#include <util/delay.h>			// needs F_CPU from cpu_mhz.h
#include <avr/io.h>

#ifndef LED_PORT
# ifdef PORTD
#  define LED_PORT PORTD
#  define LED_DDR  DDRD
# else
#  ifdef PORTB
#   define LED_PORT PORTB
#   define LED_DDR  DDRB
#  else
#   ifdef PORTA
#    define LED_PORT PORTA
#    define LED_DDR  DDRA
#   else
#    error "This CPU has no PORTD, or PORTB, or PORTA, try somethig different"
#   endif
#  endif
# endif
#endif

#ifndef LED_BITS
# define LED_BITS	0xff		// try all ...
#endif

int main()
{
  LED_DDR |= LED_BITS;			// all pins outout
  for (;;)
    {
      _delay_ms(500.0); LED_PORT &= ~(LED_BITS);        // pull low ...
      _delay_ms(500.0); LED_PORT |=   LED_BITS;         // pull high ...
    }
}
EOF


test -f Makefile || cat << EOF > Makefile
# Makefile for project \$(NAME)
# Distribute under GPLv2, use with care.
#
# 2008-09-25, jw@suse.de
# 2012-11-24, jw@suse.de

NAME          = example
CFILES        = blink.c # \$(NAME).c eeprom.c i2c_slave_cb.c

CPU           = tiny2313
#CPU           = mega48
#CPU           = mega169
#CPU           = mega644

PROG_HW       = usbtiny		# www.ladyada.com
#PROG_HW       = usbasp		# www.fischl.de
#PROG_HW       = stk200		# www.atmel.com
#PROG_HW       = butterfly	# www.atmel.com
#PROG_HW       = mkbutterfly	# www.mikrokopter.de
#PROG_PORT     = /dev/ttyUSB0
#PROG_PORT     = /dev/ttyACM0
#PROG_PORT     = /dev/rfcomm1
#AVRDUDE_OPT   = -b 57600 -P \$(PROG_PORT)

PROG_SW       = avrdude
#PROG_SW       = sudo avrdude	# recommended, if you get permission denied errors.
#PROG_SW       = uisp		# deprecated


## try this, if \$(ISP) magic fails
#UPLOAD_CMD    = \$(PROG_SW) -p AT\$(CPU) -c \$(PROG_HW) \$(AVRDUDE_OPT) \$(addprefix -P,\$(PROG_PORT)) -U \$(NAME).hex -v

#CFLAGS        += -Os -g -Wall -DLED_PORT=PORTB -DLED_DDR=DDRB -DLED_BITS=0xaf
CFLAGS        += -Os -g -Wall
#LDFLAGS       += -Wl,-u,vfprintf -lprintf_min

TOP_DIR       = .

include \$(TOP_DIR)/avr_common.mk
#DIST_EXCLUDE += --exclude photos


distclean:: 
	rm -f download* ee_data.* 

## header file dependencies
#############################
include depend.mk
EOF

:>> depend.mk
set -x
export PATH=/opt/cross/bin:$PATH
ISP_OPT=-n make 1mhz
make depend
make
echo try 'make help', for a list of make variables and targets.
