title: makefile search device, avrdude
body: <p>I want to program my AVR ATmega644. Therefore I have two programmers. For compiling and programming I using a Makefile, so I only need to type in <code>make</code> or <code>make prog</code> in a command window and the whole project get's compiled and programmed to the device. This works so far. One programmer is the AVRISPmkII and the other is the Diamex PROG-S. The AVRISPmkII is listened in <code>/dev/</code> as an USB device (<code>/dev/ttyUSB0</code>) and the Diamex PROG-S as a serial device (<code>/dev/ttyACM0</code>). But both are connected via USB. When programming with help of <code>avrdude</code> using the AVRISPmkII I have to select the option <code>-P usb</code> otherwise the option is `-P /dev/ttyACM0' (compare to my Makefile code below). So I need always to change my Makefile depending on the programmer I use.</p>
  
  <p>Now my question: How can I select the programmer (or avrdude command) automatically depending on the programmer I plugged in? I thought I have to read vendor and product ID of the USB devices to get the device type and then select the command. But what do I have to write into my makefile to do so? </p>
  
  <p>Makefile</p>
  
  <pre><code># Makefile for ATmega644
  
  SRCDIR = src
  TMPDIR = tmp
  BINDIR = bin
  INCDIR = include
  
  mcu = atmega644
  SIZE = avr-size
  CC = avr-gcc
  AS = avr-as
  CCFLAGS = -mmcu=$(mcu) -Wall -Os $(addprefix -I, $(INCDIR))
  ASMFLAGS = -mmcu=$(mcu) -I $(INCDIR) -Wall
  
  # programmer
  #programmer = stk500v2
  programmer = avrispmkII
  #programmer = avrisp2
  partno = m644p
  bitclock = 1
  port = /dev/ttyACM0
  #port = usb
  
  TMP := $(TMPDIR)
  OBJECTS = $(patsubst $(SRCDIR)/%.c, $(TMP)/%.o, $(wildcard $(SRCDIR)/*.c))
  OBJECTS += $(patsubst $(SRCDIR)/%.S, $(TMP)/%.o, $(wildcard $(SRCDIR)/*.S))
  TARGET = clock.elf
  HEXFILE =$(patsubst %.elf,%.hex,$(TARGET))
  
  all: $(TARGET) $(HEXFILE) 
      $(SIZE) $(TARGET)
  
  $(TARGET): $(OBJECTS)
      $(CC) $(CCFLAGS) $(OBJECTS) -o $(TARGET)
  
  $(TMP)/%.o:$(SRCDIR)/%.c $(TMP)
      $(CC) $(CCFLAGS) -o $@ -c $&lt;
  
  $(TMP)/%.o:$(SRCDIR)/%.S $(TMP)
      $(CC) $(ASMFLAGS) -o $@ -c $&lt;
  
  $(TMP):
      mkdir -p $(TMP)
  
  $(HEXFILE): $(TARGET)
      avr-objcopy -O ihex -R .eeprom $&lt; $@
  
  .PHONY: clean
  clean:
      rm -rf $(TMP)/*
      rm -rf $(TARGET) $(HEXFILE)
  
  .PHONY: prog    
  prog:
      avrdude -p $(partno) -B $(bitclock) -P $(port) -c $(programmer) -U flash:w:$(HEXFILE)
  
  .PHONY: fuse
  fuse:
      avrdude -p $(partno) -P $(port) -c $(programmer) -U lfuse:w:0xe2:m -U hfuse:w:0xd9:m
  
  .PHONY: read_fuse
  read_fuse:
      avrdude -p $(partno) -P $(port) -c $(programmer) -U lfuse:r:-:i -U hfuse:r:-:i
  
  .PHONY: debug
  debug:
      avr-objdump -dS $(TARGET)
  
  .PHONY: reset
  reset:
      avrdude -p $(partno) -P $(port) -c $(programmer)
  </code></pre>