<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://forums.obdev.at/app.php/feed/topic/2678" />

	<title>Objective Development Forums</title>
	
	<link href="https://forums.obdev.at/index.php" />
	<updated>2009-04-22T12:15:54+02:00</updated>

	<author><name><![CDATA[Objective Development Forums]]></name></author>
	<id>https://forums.obdev.at/app.php/feed/topic/2678</id>

		<entry>
		<author><name><![CDATA[tkerby]]></name></author>
		<updated>2009-04-22T12:15:54+02:00</updated>

		<published>2009-04-22T12:15:54+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=2678&amp;p=9019#p9019</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=2678&amp;p=9019#p9019"/>
		<title type="html"><![CDATA[Re: One-key USB-HID]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=2678&amp;p=9019#p9019"><![CDATA[
Try this makefile - I had some similar problems<br /><div class="codebox"><p>Code: </p><pre><code># Name: Makefile<br /># Project: 1 key keyboard<br /># Author: <br /># Creation Date: 2008-04-07<br /># Tabsize: 4<br /># Copyright: <br /># License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)<br /><br />DEVICE  = attiny45<br />F_CPU   = 16500000   # in Hz<br />FUSE_L  = 0xc1<br />FUSE_H  = 0xdf<br />AVRDUDE = avrdude -c usbtiny -p $(DEVICE) # edit this line for your programmer<br /><br />CFLAGS  = -Iusbdrv -I. -DDEBUG_LEVEL=0<br />OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o<br /><br />COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)<br /><br />##############################################################################<br /># Fuse values for particular devices<br />##############################################################################<br /># If your device is not listed here, go to<br /># http://palmavr.sourceforge.net/cgi-bin/fc.cgi<br /># and choose options for external crystal clock and no clock divider<br />#<br />################################## ATMega8 ##################################<br /># ATMega8 FUSE_L (Fuse low byte):<br /># 0x9f = 1 0 0 1   1 1 1 1<br />#        ^ ^ \ /   \--+--/<br />#        | |  |       +------- CKSEL 3..0 (external &gt;8M crystal)<br />#        | |  +--------------- SUT 1..0 (crystal osc, BOD enabled)<br />#        | +------------------ BODEN (BrownOut Detector enabled)<br />#        +-------------------- BODLEVEL (2.7V)<br /># ATMega8 FUSE_H (Fuse high byte):<br /># 0xc9 = 1 1 0 0   1 0 0 1 &lt;-- BOOTRST (boot reset vector at 0x0000)<br />#        ^ ^ ^ ^   ^ ^ ^------ BOOTSZ0<br />#        | | | |   | +-------- BOOTSZ1<br />#        | | | |   + --------- EESAVE (don't preserve EEPROM over chip erase)<br />#        | | | +-------------- CKOPT (full output swing)<br />#        | | +---------------- SPIEN (allow serial programming)<br />#        | +------------------ WDTON (WDT not always on)<br />#        +-------------------- RSTDISBL (reset pin is enabled)<br />#<br />############################## ATMega48/88/168 ##############################<br /># ATMega*8 FUSE_L (Fuse low byte):<br /># 0xdf = 1 1 0 1   1 1 1 1<br />#        ^ ^ \ /   \--+--/<br />#        | |  |       +------- CKSEL 3..0 (external &gt;8M crystal)<br />#        | |  +--------------- SUT 1..0 (crystal osc, BOD enabled)<br />#        | +------------------ CKOUT (if 0: Clock output enabled)<br />#        +-------------------- CKDIV8 (if 0: divide by 8)<br /># ATMega*8 FUSE_H (Fuse high byte):<br /># 0xde = 1 1 0 1   1 1 1 0<br />#        ^ ^ ^ ^   ^ \-+-/<br />#        | | | |   |   +------ BODLEVEL 0..2 (110 = 1.8 V)<br />#        | | | |   + --------- EESAVE (preserve EEPROM over chip erase)<br />#        | | | +-------------- WDTON (if 0: watchdog always on)<br />#        | | +---------------- SPIEN (allow serial programming)<br />#        | +------------------ DWEN (debug wire enable)<br />#        +-------------------- RSTDISBL (reset pin is enabled)<br />#<br />############################## ATTiny25/45/85 ###############################<br /># ATMega*5 FUSE_L (Fuse low byte):<br /># 0xef = 1 1 1 0   1 1 1 1<br />#        ^ ^ \+/   \--+--/<br />#        | |  |       +------- CKSEL 3..0 (clock selection -&gt; crystal @ 12 MHz)<br />#        | |  +--------------- SUT 1..0 (BOD enabled, fast rising power)<br />#        | +------------------ CKOUT (clock output on CKOUT pin -&gt; disabled)<br />#        +-------------------- CKDIV8 (divide clock by 8 -&gt; don't divide)<br /># ATMega*5 FUSE_H (Fuse high byte):<br /># 0xdd = 1 1 0 1   1 1 0 1<br />#        ^ ^ ^ ^   ^ \-+-/ <br />#        | | | |   |   +------ BODLEVEL 2..0 (brownout trigger level -&gt; 2.7V)<br />#        | | | |   +---------- EESAVE (preserve EEPROM on Chip Erase -&gt; not preserved)<br />#        | | | +-------------- WDTON (watchdog timer always on -&gt; disable)<br />#        | | +---------------- SPIEN (enable serial programming -&gt; enabled)<br />#        | +------------------ DWEN (debug wire enable)<br />#        +-------------------- RSTDISBL (disable external reset -&gt; enabled)<br />#<br />################################ ATTiny2313 #################################<br /># ATTiny2313 FUSE_L (Fuse low byte):<br /># 0xef = 1 1 1 0   1 1 1 1<br />#        ^ ^ \+/   \--+--/<br />#        | |  |       +------- CKSEL 3..0 (clock selection -&gt; crystal @ 12 MHz)<br />#        | |  +--------------- SUT 1..0 (BOD enabled, fast rising power)<br />#        | +------------------ CKOUT (clock output on CKOUT pin -&gt; disabled)<br />#        +-------------------- CKDIV8 (divide clock by 8 -&gt; don't divide)<br /># ATTiny2313 FUSE_H (Fuse high byte):<br /># 0xdb = 1 1 0 1   1 0 1 1<br />#        ^ ^ ^ ^   \-+-/ ^<br />#        | | | |     |   +---- RSTDISBL (disable external reset -&gt; enabled)<br />#        | | | |     +-------- BODLEVEL 2..0 (brownout trigger level -&gt; 2.7V)<br />#        | | | +-------------- WDTON (watchdog timer always on -&gt; disable)<br />#        | | +---------------- SPIEN (enable serial programming -&gt; enabled)<br />#        | +------------------ EESAVE (preserve EEPROM on Chip Erase -&gt; not preserved)<br />#        +-------------------- DWEN (debug wire enable)<br /><br /><br /># symbolic targets:<br />help:<br />   @echo &quot;This Makefile has no default rule. Use one of the following:&quot;<br />   @echo &quot;make hex ....... to build main.hex&quot;<br />   @echo &quot;make program ... to flash fuses and firmware&quot;<br />   @echo &quot;make fuse ...... to flash the fuses&quot;<br />   @echo &quot;make flash ..... to flash the firmware (use this on metaboard)&quot;<br />   @echo &quot;make clean ..... to delete objects and hex file&quot;<br /><br />hex: main.hex<br /><br />program: flash fuse<br /><br /># rule for programming fuse bits:<br />fuse:<br />   @&#91; &quot;$(FUSE_H)&quot; != &quot;&quot; -a &quot;$(FUSE_L)&quot; != &quot;&quot; &#93; || \<br />      { echo &quot;*** Edit Makefile and choose values for FUSE_L and FUSE_H!&quot;; exit 1; }<br />   $(AVRDUDE) -U hfuse:w:$(FUSE_H):m -U lfuse:w:$(FUSE_L):m<br /><br /># rule for uploading firmware:<br />flash: main.hex<br />   $(AVRDUDE) -U flash:w:main.hex:i<br /><br /># rule for deleting dependent files (those which can be built by Make):<br />clean:<br />   rm -f main.hex main.lst main.obj main.cof main.list main.map main.eep.hex main.elf *.o usbdrv/*.o main.s usbdrv/oddebug.s usbdrv/usbdrv.s<br /><br /># Generic rule for compiling C files:<br />.c.o:<br />   $(COMPILE) -c $&lt; -o $@<br /><br /># Generic rule for assembling Assembler source files:<br />.S.o:<br />   $(COMPILE) -x assembler-with-cpp -c $&lt; -o $@<br /># &quot;-x assembler-with-cpp&quot; should not be necessary since this is the default<br /># file type for the .S (with capital S) extension. However, upper case<br /># characters are not always preserved on Windows. To ensure WinAVR<br /># compatibility define the file type manually.<br /><br /># Generic rule for compiling C to assembler, used for debugging only.<br />.c.s:<br />   $(COMPILE) -S $&lt; -o $@<br /><br /># file targets:<br /><br /># Since we don't want to ship the driver multipe times, we copy it into this project:<br />usbdrv:<br />   cp -r ../../../usbdrv .<br /><br />main.elf: usbdrv $(OBJECTS)   # usbdrv dependency only needed because we copy it<br />   $(COMPILE) -o main.elf $(OBJECTS)<br /><br />main.hex: main.elf<br />   rm -f main.hex main.eep.hex<br />   avr-objcopy -j .text -j .data -O ihex main.elf main.hex<br />   avr-size main.hex<br /><br /># debugging targets:<br /><br />disasm:   main.elf<br />   avr-objdump -d main.elf<br /><br />cpp:<br />   $(COMPILE) -E main.c<br /></code></pre></div><br /><br />I also updated to the latest v-usb and had to modify the main file and descriptors as follows.<br /><br />Main.c<br /><br /><div class="codebox"><p>Code: </p><pre><code>/* Name: main.c<br /> * Project: 1-Key Keyboard<br /> * Author: Flip van den Berg - www.flipwork.nl<br /> * Creation Date: 2008-10-06<br /> * Based on AVR-USB drivers from Objective Developments - http://www.obdev.at/products/avrusb/index.html<br /> */<br /><br />#include &lt;avr/io.h&gt;<br />#include &lt;avr/wdt.h&gt;<br />#include &lt;avr/eeprom.h&gt;<br />#include &lt;avr/interrupt.h&gt;<br />#include &lt;avr/pgmspace.h&gt;<br />#include &lt;util/delay.h&gt;<br />#include &lt;stdlib.h&gt;<br /><br />#include &quot;usbdrv/usbdrv.h&quot;<br />#include &quot;usbdrv/oddebug.h&quot;<br /><br />#define BUTTON_PORT PORTB       /* PORTx - register for button output */<br />#define BUTTON_PIN PINB         /* PINx - register for button input */<br />#define BUTTON_BIT PB3          /* bit for button input/output */<br /><br />/* ------------------------------------------------------------------------- */<br /><br />static uchar    reportBuffer&#91;2&#93;;    /* buffer for HID reports */<br />static uchar    idleRate;           /* in 4 ms units */<br />static uchar    reportCount;      /* current report */<br /><br />static uchar    buttonState;      /*  stores state of button */<br />static uchar   debounceTimeIsOver;   /* for switch debouncing */<br /><br />/* ------------------------------------------------------------------------- */<br /><br />PROGMEM char usbHidReportDescriptor&#91;USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH&#93; = { /* USB report descriptor */<br />    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)<br />    0x09, 0x06,                    // USAGE (Keyboard)<br />    0xa1, 0x01,                    // COLLECTION (Application)<br />    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)<br />    0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)<br />    0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)<br />    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)<br />    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)<br />    0x75, 0x01,                    //   REPORT_SIZE (1)<br />    0x95, 0x08,                    //   REPORT_COUNT (8)<br />    0x81, 0x02,                    //   INPUT (Data,Var,Abs)<br />    0x95, 0x01,                    //   REPORT_COUNT (1)<br />    0x75, 0x08,                    //   REPORT_SIZE (8)<br />    0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)<br />    0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))<br />    0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)<br />    0x81, 0x00,                    //   INPUT (Data,Ary,Abs)<br />    0xc0                           // END_COLLECTION<br />};<br />/* We use a simplifed keyboard report descriptor which does not support the<br /> * boot protocol. We don't allow setting status LEDs and we only allow one<br /> * simultaneous key press (except modifiers). We can therefore use short<br /> * 2 byte input reports.<br /> * The report descriptor has been created with usb.org's &quot;HID Descriptor Tool&quot;<br /> * which can be downloaded from http://www.usb.org/developers/hidpage/.<br /> * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted<br /> * for the second INPUT item.<br /> */<br />static void timerPoll(void)<br />{<br />   static unsigned int timerCnt;<br /><br />    if(TIFR &amp; (1 &lt;&lt; TOV1)){<br />        TIFR = (1 &lt;&lt; TOV1); /* clear overflow */<br />        if(++timerCnt &gt;= 3){       // 3/63 sec delay for switch debouncing<br />         timerCnt = 0;<br />         debounceTimeIsOver = 1; <br />        }<br />    }<br />}<br /><br />static void buildReport(void)<br />{<br />uchar modifier = 0; //if not changed by the if-statement below, then send an empty report<br />uchar key = 0; //if not changed by the if-statement below, then send an empty report<br /><br />    if(reportCount == 0){<br />        if (buttonState == 1){ // if button is not pressed<br />      modifier = 0x08; // modifier = gui button<br />      key = 0x0f; // key = L (lock)<br />      } else {<br />      modifier = 0x00; // modifier = None<br />      key = 0x00; // Empty report<br />       }<br />    }<br /><br /><br />   reportCount++;<br />    reportBuffer&#91;0&#93; = modifier;    /* no modifiers */<br />    reportBuffer&#91;1&#93; = key;<br />}<br /><br />static void checkButtonChange(void) {<br />   <br />   uchar tempButtonValue = bit_is_clear(BUTTON_PIN, BUTTON_BIT); //status of switch is stored in tempButtonValue <br /><br />   if (tempButtonValue != buttonState &amp;&amp; debounceTimeIsOver == 1){ //if status has changed and the debounce-delay is over<br />      buttonState = tempButtonValue;   // change buttonState to new state<br />      debounceTimeIsOver = 0;   // debounce timer starts<br />      reportCount = 0; // start report <br />   }<br />}<br /><br />/* ------------------------------------------------------------------------- */<br /><br />static void timerInit(void)<br />{<br />    TCCR1 = 0x0b;           /* select clock: 16.5M/1k -&gt; overflow rate = 16.5M/256k = 62.94 Hz */<br />}<br /><br />/* -------------------------------------------------------------------------------- */<br />/* ------------------------ interface to USB driver ------------------------ */<br />/* -------------------------------------------------------------------------------- */<br /><br />uchar   usbFunctionSetup(uchar data&#91;8&#93;)<br />{<br />usbRequest_t    *rq = (void *)data;<br /><br />    usbMsgPtr = reportBuffer;<br />    if((rq-&gt;bmRequestType &amp; USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){    /* class request type */<br />        if(rq-&gt;bRequest == USBRQ_HID_GET_REPORT){  /* wValue: ReportType (highbyte), ReportID (lowbyte) */<br />            /* we only have one report type, so don't look at wValue */<br />            buildReport();<br />            return sizeof(reportBuffer);<br />        }else if(rq-&gt;bRequest == USBRQ_HID_GET_IDLE){<br />            usbMsgPtr = &amp;idleRate;<br />            return 1;<br />        }else if(rq-&gt;bRequest == USBRQ_HID_SET_IDLE){<br />            idleRate = rq-&gt;wValue.bytes&#91;1&#93;;<br />        }<br />    }else{<br />        /* no vendor specific requests implemented */<br />    }<br />   return 0;<br />}<br /><br />/* ------------------------------------------------------------------------- */<br />/* ------------------------ Oscillator Calibration ------------------------- */<br />/* ------------------------------------------------------------------------- */<br /><br />/* Calibrate the RC oscillator. Our timing reference is the Start Of Frame<br /> * signal (a single SE0 bit) repeating every millisecond immediately after<br /> * a USB RESET. We first do a binary search for the OSCCAL value and then<br /> * optimize this value with a neighboorhod search.<br /> */<br />void    calibrateOscillator(void)<br />{<br />uchar       step = 128;<br />uchar       trialValue = 0, optimumValue;<br />int         x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);<br /><br />    /* do a binary search: */<br />    do{<br />        OSCCAL = trialValue + step;<br />        x = usbMeasureFrameLength();    /* proportional to current real frequency */<br />        if(x &lt; targetValue)             /* frequency still too low */<br />            trialValue += step;<br />        step &gt;&gt;= 1;<br />    }while(step &gt; 0);<br />    /* We have a precision of +/- 1 for optimum OSCCAL here */<br />    /* now do a neighborhood search for optimum value */<br />    optimumValue = trialValue;<br />    optimumDev = x; /* this is certainly far away from optimum */<br />    for(OSCCAL = trialValue - 1; OSCCAL &lt;= trialValue + 1; OSCCAL++){<br />        x = usbMeasureFrameLength() - targetValue;<br />        if(x &lt; 0)<br />            x = -x;<br />        if(x &lt; optimumDev){<br />            optimumDev = x;<br />            optimumValue = OSCCAL;<br />        }<br />    }<br />    OSCCAL = optimumValue;<br />}<br />/*<br />Note: This calibration algorithm may try OSCCAL values of up to 192 even if<br />the optimum value is far below 192. It may therefore exceed the allowed clock<br />frequency of the CPU in low voltage designs!<br />You may replace this search algorithm with any other algorithm you like if<br />you have additional constraints such as a maximum CPU clock.<br />For version 5.x RC oscillators (those with a split range of 2x128 steps, e.g.<br />ATTiny25, ATTiny45, ATTiny85), it may be useful to search for the optimum in<br />both regions.<br />*/<br /><br /><br />void    hadUsbReset(void)<br />{<br />    calibrateOscillator();<br />    eeprom_write_byte(0, OSCCAL);   /* store the calibrated value in EEPROM */<br />}<br /><br />/* ------------------------------------------------------------------------- */<br />/* --------------------------------- main ---------------------------------- */<br />/* ------------------------------------------------------------------------- */<br /><br />int main(void)<br />{<br />uchar   i;<br />uchar   calibrationValue;<br /><br />   wdt_enable(WDTO_1S);<br />    /* Even if you don't use the watchdog, turn it off here. On newer devices,<br />     * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!<br />     */<br /><br />    calibrationValue = eeprom_read_byte(0); /* calibration value from last time */<br />    if(calibrationValue != 0xff){<br />        OSCCAL = calibrationValue;<br />    }<br />    <br />   //odDebugInit();<br />    usbInit();<br />    usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */<br />    i = 0;<br />    while(--i){             /* fake USB disconnect for &gt; 250 ms */<br />        wdt_reset();<br />        _delay_ms(1);<br />    }<br />    usbDeviceConnect();<br /><br /><br /><br />   /* turn on internal pull-up resistor for the switch */<br />    BUTTON_PORT |= _BV(BUTTON_BIT);<br /><br />    timerInit();<br /><br />    sei();<br /><br />    for(;;){    /* main event loop */<br />        wdt_reset();<br />        usbPoll();<br /><br />      /* A USB keypress cycle is defined as a scancode being present in a report, and<br />      then absent from a later report. To press and release the Caps Lock key, instead of<br />      holding it down, we need to send the report with the Caps Lock scancode and<br />      then an empty report. */<br />      <br />      if(usbInterruptIsReady() &amp;&amp; reportCount &lt; 2){ /* we can send another key */<br />           buildReport();<br />              usbSetInterrupt(reportBuffer, sizeof(reportBuffer));<br />           }<br />        checkButtonChange();<br />      timerPoll();<br />   }<br />      return 0;<br />}<br /></code></pre></div><br /><br /><br />Descriptors<br /><br /><div class="codebox"><p>Code: </p><pre><code>/* Name: usbconfig.h<br /> * Project: AVR USB driver<br /> * Author: Christian Starkjohann<br /> * Creation Date: 2007-06-23<br /> * Tabsize: 4<br /> * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH<br /> * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt)<br /> * This Revision: $Id: usbconfig.h 537 2008-02-28 21:13:01Z cs $<br /> */<br /><br />#ifndef __usbconfig_h_included__<br />#define __usbconfig_h_included__<br /><br />/* ---------------------------- Hardware Config ---------------------------- */<br /><br />#define USB_CFG_IOPORTNAME      B<br />/* This is the port where the USB bus is connected. When you configure it to<br /> * &quot;B&quot;, the registers PORTB, PINB and DDRB will be used.<br /> */<br />#define USB_CFG_DMINUS_BIT      0<br />/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.<br /> * This may be any bit in the port.<br /> */<br />#define USB_CFG_DPLUS_BIT       2<br />/* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.<br /> * This may be any bit in the port. Please note that D+ must also be connected<br /> * to interrupt pin INT0!<br /> */<br />#define USB_CFG_CLOCK_KHZ       (F_CPU/1000)<br />/* Clock rate of the AVR in MHz. Legal values are 12000, 16000 or 16500.<br /> * The 16.5 MHz version of the code requires no crystal, it tolerates +/- 1%<br /> * deviation from the nominal frequency. All other rates require a precision<br /> * of 2000 ppm and thus a crystal!<br /> * Default if not specified: 12 MHz<br /> */<br /> #define USB_CFG_CHECK_CRC       0<br />/* Define this to 1 if you want that the driver checks integrity of incoming<br /> * data packets (CRC checks). CRC checks cost quite a bit of code size and are<br /> * currently only available for 18 MHz crystal clock. You must choose<br /> * USB_CFG_CLOCK_KHZ = 18000 if you enable this option.<br /> */<br /><br />/* ----------------------- Optional Hardware Config ------------------------ */<br /><br />/* #define USB_CFG_PULLUP_IOPORTNAME   D */<br />/* If you connect the 1.5k pullup resistor from D- to a port pin instead of<br /> * V+, you can connect and disconnect the device from firmware by calling<br /> * the macros usbDeviceConnect() and usbDeviceDisconnect() (see usbdrv.h).<br /> * This constant defines the port on which the pullup resistor is connected.<br /> */<br />/* #define USB_CFG_PULLUP_BIT          4 */<br />/* This constant defines the bit number in USB_CFG_PULLUP_IOPORT (defined<br /> * above) where the 1.5k pullup resistor is connected. See description<br /> * above for details.<br /> */<br /><br />/* --------------------------- Functional Range ---------------------------- */<br /><br />#define USB_CFG_HAVE_INTRIN_ENDPOINT    1<br />/* Define this to 1 if you want to compile a version with two endpoints: The<br /> * default control endpoint 0 and an interrupt-in endpoint 1.<br /> */<br />#define USB_CFG_HAVE_INTRIN_ENDPOINT3   0<br />/* Define this to 1 if you want to compile a version with three endpoints: The<br /> * default control endpoint 0, an interrupt-in endpoint 1 and an interrupt-in<br /> * endpoint 3. You must also enable endpoint 1 above.<br /> */<br /> #define USB_CFG_EP3_NUMBER              3<br />/* If the so-called endpoint 3 is used, it can now be configured to any other<br /> * endpoint number (except 0) with this macro. Default if undefined is 3.<br /> */<br />#define USB_INITIAL_DATATOKEN           USBPID_DATA1<br />/* The above macro defines the startup condition for data toggling on the<br /> * interrupt/bulk endpoints 1 and 3. Defaults to USBPID_DATA1.<br /> * Since the token is toggled BEFORE sending any data, the first packet is<br /> * sent with the oposite value of this configuration!<br /> */<br />#define USB_CFG_IMPLEMENT_HALT          0<br />/* Define this to 1 if you also want to implement the ENDPOINT_HALT feature<br /> * for endpoint 1 (interrupt endpoint). Although you may not need this feature,<br /> * it is required by the standard. We have made it a config option because it<br /> * bloats the code considerably.<br /> */<br /> #define USB_CFG_SUPPRESS_INTR_CODE      0<br />/* Define this to 1 if you want to declare interrupt-in endpoints, but don't<br /> * want to send any data over them. If this macro is defined to 1, functions<br /> * usbSetInterrupt() and usbSetInterrupt3() are omitted. This is useful if<br /> * you need the interrupt-in endpoints in order to comply to an interface<br /> * (e.g. HID), but never want to send any data. This option saves a couple<br /> * of bytes in flash memory and the transmit buffers in RAM.<br /> */<br />#define USB_CFG_INTR_POLL_INTERVAL      10<br />/* If you compile a version with endpoint 1 (interrupt-in), this is the poll<br /> * interval. The value is in milliseconds and must not be less than 10 ms for<br /> * low speed devices.<br /> */<br />#define USB_CFG_IS_SELF_POWERED         0<br />/* Define this to 1 if the device has its own power supply. Set it to 0 if the<br /> * device is powered from the USB bus.<br /> */<br />#define USB_CFG_MAX_BUS_POWER           50<br />/* Set this variable to the maximum USB bus power consumption of your device.<br /> * The value is in milliamperes. &#91;It will be divided by two since USB<br /> * communicates power requirements in units of 2 mA.&#93;<br /> */<br />#define USB_CFG_IMPLEMENT_FN_WRITE      0<br />/* Set this to 1 if you want usbFunctionWrite() to be called for control-out<br /> * transfers. Set it to 0 if you don't need it and want to save a couple of<br /> * bytes.<br /> */<br />#define USB_CFG_IMPLEMENT_FN_READ       0<br />/* Set this to 1 if you need to send control replies which are generated<br /> * &quot;on the fly&quot; when usbFunctionRead() is called. If you only want to send<br /> * data from a static buffer, set it to 0 and return the data from<br /> * usbFunctionSetup(). This saves a couple of bytes.<br /> */<br />#define USB_CFG_IMPLEMENT_FN_WRITEOUT   0<br />/* Define this to 1 if you want to use interrupt-out (or bulk out) endpoint 1.<br /> * You must implement the function usbFunctionWriteOut() which receives all<br /> * interrupt/bulk data sent to endpoint 1.<br /> */<br />#define USB_CFG_HAVE_FLOWCONTROL        0<br />/* Define this to 1 if you want flowcontrol over USB data. See the definition<br /> * of the macros usbDisableAllRequests() and usbEnableAllRequests() in<br /> * usbdrv.h.<br /> */<br /> #define USB_CFG_LONG_TRANSFERS          0<br />/* Define this to 1 if you want to send/receive blocks of more than 254 bytes<br /> * in a single control-in or control-out transfer. Note that the capability<br /> * for long transfers increases the driver size.<br /> */<br />/* #define USB_RX_USER_HOOK(data, len)     if(usbRxToken == (uchar)USBPID_SETUP) blinkLED(); */<br />/* This macro is a hook if you want to do unconventional things. If it is<br /> * defined, it's inserted at the beginning of received message processing.<br /> * If you eat the received message and don't want default processing to<br /> * proceed, do a return after doing your things. One possible application<br /> * (besides debugging) is to flash a status LED on each packet.<br /> */<br />#define USB_RESET_HOOK(resetStarts)     if(!resetStarts){hadUsbReset();}<br />/* This macro is a hook if you need to know when an USB RESET occurs. It has<br /> * one parameter which distinguishes between the start of RESET state and its<br /> * end.<br /> */<br />/* #define USB_SET_ADDRESS_HOOK()              hadAddressAssigned(); */<br />/* This macro (if defined) is executed when a USB SET_ADDRESS request was<br /> * received.<br /> */<br />#define USB_COUNT_SOF                   0<br />/* define this macro to 1 if you need the global variable &quot;usbSofCount&quot; which<br /> * counts SOF packets. This feature requires that the hardware interrupt is<br /> * connected to D- instead of D+.<br /> */<br /> /*#ifndef __ASSEMBLER__<br /> * macro myAssemblerMacro<br /> *     in      YL, TCNT0<br /> *     sts     timer0Snapshot, YL<br /> *     endm<br /> * #endif<br /> * #define USB_SOF_HOOK                    myAssemblerMacro<br /> * This macro (if defined) is executed in the assembler module when a<br /> * Start Of Frame condition is detected. It is recommended to define it to<br /> * the name of an assembler macro which is defined here as well so that more<br /> * than one assembler instruction can be used. The macro may use the register<br /> * YL and modify SREG. If it lasts longer than a couple of cycles, USB messages<br /> * immediately after an SOF pulse may be lost and must be retried by the host.<br /> * What can you do with this hook? Since the SOF signal occurs exactly every<br /> * 1 ms (unless the host is in sleep mode), you can use it to tune OSCCAL in<br /> * designs running on the internal RC oscillator.<br /> * Please note that Start Of Frame detection works only if D- is wired to the<br /> * interrupt, not D+. THIS IS DIFFERENT THAN MOST EXAMPLES!<br /> */<br />#define USB_CFG_CHECK_DATA_TOGGLING     0<br />/* define this macro to 1 if you want to filter out duplicate data packets<br /> * sent by the host. Duplicates occur only as a consequence of communication<br /> * errors, when the host does not receive an ACK. Please note that you need to<br /> * implement the filtering yourself in usbFunctionWriteOut() and<br /> * usbFunctionWrite(). Use the global usbCurrentDataToken and a static variable<br /> * for each control- and out-endpoint to check for duplicate packets.<br /> */<br />#define USB_CFG_HAVE_MEASURE_FRAME_LENGTH   1<br />/* define this macro to 1 if you want the function usbMeasureFrameLength()<br /> * compiled in. This function can be used to calibrate the AVR's RC oscillator.<br /> */<br /><br />/* -------------------------- Device Description --------------------------- */<br /><br />#define  USB_CFG_VENDOR_ID       0x42, 0x42<br />/* USB vendor ID for the device, low byte first. If you have registered your<br /> * own Vendor ID, define it here. Otherwise you use obdev's free shared<br /> * VID/PID pair. Be sure to read USBID-License.txt for rules!<br /> * This template uses obdev's shared VID/PID pair for HIDs: 0x16c0/0x5df.<br /> * Use this VID/PID pair ONLY if you understand the implications!<br /> */<br />#define  USB_CFG_DEVICE_ID       0x31, 0xe1<br />/* This is the ID of the product, low byte first. It is interpreted in the<br /> * scope of the vendor ID. If you have registered your own VID with usb.org<br /> * or if you have licensed a PID from somebody else, define it here. Otherwise<br /> * you use obdev's free shared VID/PID pair. Be sure to read the rules in<br /> * USBID-License.txt!<br /> * This template uses obdev's shared VID/PID pair for HIDs: 0x16c0/0x5df.<br /> * Use this VID/PID pair ONLY if you understand the implications!<br /> */<br />#define USB_CFG_DEVICE_VERSION  0x00, 0x01<br />/* Version number of the device: Minor number first, then major number.<br /> */<br />#define USB_CFG_VENDOR_NAME     'T', 'h', 'e', ' ', 'B', 'u', 't', 't', 'o', 'n', '!'<br />#define USB_CFG_VENDOR_NAME_LEN 11<br />/* These two values define the vendor name returned by the USB device. The name<br /> * must be given as a list of characters under single quotes. The characters<br /> * are interpreted as Unicode (UTF-16) entities.<br /> * If you don't want a vendor name string, undefine these macros.<br /> * ALWAYS define a vendor name containing your Internet domain name if you use<br /> * obdev's free shared VID/PID pair. See the file USBID-License.txt for<br /> * details.<br /> */<br />#define USB_CFG_DEVICE_NAME     '1', '-', 'K', 'e', 'y', '-', 'K', 'e', 'y', 'b', 'o', 'a', 'r', 'd'<br />#define USB_CFG_DEVICE_NAME_LEN 14<br />/* Same as above for the device name. If you don't want a device name, undefine<br /> * the macros. See the file USBID-License.txt before you assign a name if you<br /> * use a shared VID/PID.<br /> */<br />/*#define USB_CFG_SERIAL_NUMBER   'N', 'o', 'n', 'e' */<br />/*#define USB_CFG_SERIAL_NUMBER_LEN   0 */<br />/* Same as above for the serial number. If you don't want a serial number,<br /> * undefine the macros.<br /> * It may be useful to provide the serial number through other means than at<br /> * compile time. See the section about descriptor properties below for how<br /> * to fine tune control over USB descriptors such as the string descriptor<br /> * for the serial number.<br /> */<br />#define USB_CFG_DEVICE_CLASS        0<br />#define USB_CFG_DEVICE_SUBCLASS     0<br />/* See USB specification if you want to conform to an existing device class.<br /> */<br />#define USB_CFG_INTERFACE_CLASS     3   /* HID */<br />#define USB_CFG_INTERFACE_SUBCLASS  0   /* no boot interface */<br />#define USB_CFG_INTERFACE_PROTOCOL  0   /* no protocol */<br />/* See USB specification if you want to conform to an existing device class or<br /> * protocol.<br /> */<br />#define USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH    35  /* total length of report descriptor */<br />/* Define this to the length of the HID report descriptor, if you implement<br /> * an HID device. Otherwise don't define it or define it to 0.<br /> * Since this template defines a HID device, it must also specify a HID<br /> * report descriptor length. You must add a PROGMEM character array named<br /> * &quot;usbHidReportDescriptor&quot; to your code which contains the report descriptor.<br /> * Don't forget to keep the array and this define in sync!<br /> */<br /><br />/* #define USB_PUBLIC static */<br />/* Use the define above if you #include usbdrv.c instead of linking against it.<br /> * This technique saves a couple of bytes in flash memory.<br /> */<br /><br />/* ------------------- Fine Control over USB Descriptors ------------------- */<br />/* If you don't want to use the driver's default USB descriptors, you can<br /> * provide our own. These can be provided as (1) fixed length static data in<br /> * flash memory, (2) fixed length static data in RAM or (3) dynamically at<br /> * runtime in the function usbFunctionDescriptor(). See usbdrv.h for more<br /> * information about this function.<br /> * Descriptor handling is configured through the descriptor's properties. If<br /> * no properties are defined or if they are 0, the default descriptor is used.<br /> * Possible properties are:<br /> *   + USB_PROP_IS_DYNAMIC: The data for the descriptor should be fetched<br /> *     at runtime via usbFunctionDescriptor().<br /> *   + USB_PROP_IS_RAM: The data returned by usbFunctionDescriptor() or found<br /> *     in static memory is in RAM, not in flash memory.<br /> *   + USB_PROP_LENGTH(len): If the data is in static memory (RAM or flash),<br /> *     the driver must know the descriptor's length. The descriptor itself is<br /> *     found at the address of a well known identifier (see below).<br /> * List of static descriptor names (must be declared PROGMEM if in flash):<br /> *   char usbDescriptorDevice&#91;&#93;;<br /> *   char usbDescriptorConfiguration&#91;&#93;;<br /> *   char usbDescriptorHidReport&#91;&#93;;<br /> *   char usbDescriptorString0&#91;&#93;;<br /> *   int usbDescriptorStringVendor&#91;&#93;;<br /> *   int usbDescriptorStringDevice&#91;&#93;;<br /> *   int usbDescriptorStringSerialNumber&#91;&#93;;<br /> * Other descriptors can't be provided statically, they must be provided<br /> * dynamically at runtime.<br /> *<br /> * Descriptor properties are or-ed or added together, e.g.:<br /> * #define USB_CFG_DESCR_PROPS_DEVICE   (USB_PROP_IS_RAM | USB_PROP_LENGTH(18))<br /> *<br /> * The following descriptors are defined:<br /> *   USB_CFG_DESCR_PROPS_DEVICE<br /> *   USB_CFG_DESCR_PROPS_CONFIGURATION<br /> *   USB_CFG_DESCR_PROPS_STRINGS<br /> *   USB_CFG_DESCR_PROPS_STRING_0<br /> *   USB_CFG_DESCR_PROPS_STRING_VENDOR<br /> *   USB_CFG_DESCR_PROPS_STRING_PRODUCT<br /> *   USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER<br /> *   USB_CFG_DESCR_PROPS_HID<br /> *   USB_CFG_DESCR_PROPS_HID_REPORT<br /> *   USB_CFG_DESCR_PROPS_UNKNOWN (for all descriptors not handled by the driver)<br /> *<br /> */<br /><br />#define USB_CFG_DESCR_PROPS_DEVICE                  0<br />#define USB_CFG_DESCR_PROPS_CONFIGURATION           0<br />#define USB_CFG_DESCR_PROPS_STRINGS                 0<br />#define USB_CFG_DESCR_PROPS_STRING_0                0<br />#define USB_CFG_DESCR_PROPS_STRING_VENDOR           0<br />#define USB_CFG_DESCR_PROPS_STRING_PRODUCT          0<br />#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER    0<br />#define USB_CFG_DESCR_PROPS_HID                     0<br />#define USB_CFG_DESCR_PROPS_HID_REPORT              0<br />#define USB_CFG_DESCR_PROPS_UNKNOWN                 0<br /><br />/* ----------------------- Optional MCU Description ------------------------ */<br /><br />/* The following configurations have working defaults in usbdrv.h. You<br /> * usually don't need to set them explicitly. Only if you want to run<br /> * the driver on a device which is not yet supported or with a compiler<br /> * which is not fully supported (such as IAR C) or if you use a differnt<br /> * interrupt than INT0, you may have to define some of these.<br /> */<br />/* #define USB_INTR_CFG            MCUCR */<br />/* #define USB_INTR_CFG_SET        ((1 &lt;&lt; ISC00) | (1 &lt;&lt; ISC01)) */<br />/* #define USB_INTR_CFG_CLR        0 */<br />/* #define USB_INTR_ENABLE         GIMSK */<br />/* #define USB_INTR_ENABLE_BIT     INT0 */<br />/* #define USB_INTR_PENDING        GIFR */<br />/* #define USB_INTR_PENDING_BIT    INTF0 */<br />/* #define USB_INTR_VECTOR         SIG_INTERRUPT0 */<br /><br />#endif /* __usbconfig_h_included__ */<br /></code></pre></div><br /><br /><br />This also fixes the problem where the first key was not sent<br /><br />Note my current function is to lock the workstation with GUI-L in windows.  I'm thinking of trying to implement the power button function and also the pcs power led which should both be available over hid although I'm not sure if I will need to also implement the boot protocol to do this<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=2165">tkerby</a> — Wed Apr 22, 2009 12:15 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[ranstam]]></name></author>
		<updated>2009-04-21T02:21:56+02:00</updated>

		<published>2009-04-21T02:21:56+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=2678&amp;p=8995#p8995</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=2678&amp;p=8995#p8995"/>
		<title type="html"><![CDATA[One-key USB-HID]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=2678&amp;p=8995#p8995"><![CDATA[
Hi!<br /><br />Im new at USB, but am trying to get my Atmega16 to work as a Onekey-keyboard. I downloadet the project from the examples page, but i am having problems compiling it. My programing skills isnt directly pushing the envelope, so it might just be a noob error, but could you help me?<br /><br />The code is<br /><br /><div class="codebox"><p>Code: </p><pre><code>/* Name: main.c<br /> * Project: 1-Key Keyboard<br /> * Author: Flip van den Berg - www.flipwork.nl<br /> * Creation Date: 2008-10-06<br /> * Based on AVR-USB drivers from Objective Developments - http://www.obdev.at/products/avrusb/index.html<br /> */<br /><br />#include &lt;avr/io.h&gt;<br />#include &lt;avr/wdt.h&gt;<br />#include &lt;avr/eeprom.h&gt;<br />#include &lt;avr/interrupt.h&gt;<br />#include &lt;avr/pgmspace.h&gt;<br />#include &lt;util/delay.h&gt;<br />#include &lt;stdlib.h&gt;<br /><br />#include &quot;usbdrv.h&quot;<br />#include &quot;oddebug.h&quot;<br /><br />#define BUTTON_PORT PORTB       /* PORTx - register for button output */<br />#define BUTTON_PIN PINB         /* PINx - register for button input */<br />#define BUTTON_BIT PB3          /* bit for button input/output */<br /><br />/* ------------------------------------------------------------------------- */<br /><br />static uchar    reportBuffer&#91;2&#93;;    /* buffer for HID reports */<br />static uchar    idleRate;           /* in 4 ms units */<br />static uchar    reportCount;      /* current report */<br /><br />static uchar    buttonState;      /*  stores state of button */<br />static uchar   debounceTimeIsOver;   /* for switch debouncing */<br /><br />/* ------------------------------------------------------------------------- */<br /><br />PROGMEM char usbHidReportDescriptor&#91;USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH&#93; = { /* USB report descriptor */<br />    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)<br />    0x09, 0x06,                    // USAGE (Keyboard)<br />    0xa1, 0x01,                    // COLLECTION (Application)<br />    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)<br />    0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)<br />    0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)<br />    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)<br />    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)<br />    0x75, 0x01,                    //   REPORT_SIZE (1)<br />    0x95, 0x08,                    //   REPORT_COUNT (8)<br />    0x81, 0x02,                    //   INPUT (Data,Var,Abs)<br />    0x95, 0x01,                    //   REPORT_COUNT (1)<br />    0x75, 0x08,                    //   REPORT_SIZE (8)<br />    0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)<br />    0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))<br />    0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)<br />    0x81, 0x00,                    //   INPUT (Data,Ary,Abs)<br />    0xc0                           // END_COLLECTION<br />};<br />/* We use a simplifed keyboard report descriptor which does not support the<br /> * boot protocol. We don't allow setting status LEDs and we only allow one<br /> * simultaneous key press (except modifiers). We can therefore use short<br /> * 2 byte input reports.<br /> * The report descriptor has been created with usb.org's &quot;HID Descriptor Tool&quot;<br /> * which can be downloaded from http://www.usb.org/developers/hidpage/.<br /> * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted<br /> * for the second INPUT item.<br /> */<br />static void timerPoll(void)<br />{<br />   static unsigned int timerCnt;<br /><br />    if(TIFR &amp; (1 &lt;&lt; TOV1)){<br />        TIFR = (1 &lt;&lt; TOV1); /* clear overflow */<br />        if(++timerCnt &gt;= 3){       // 3/63 sec delay for switch debouncing<br />         timerCnt = 0;<br />         debounceTimeIsOver = 1; <br />        }<br />    }<br />}<br /><br />static void buildReport(void)<br />{<br />uchar key = 0; //if not changed by the if-statement below, then send an empty report<br /><br />    if(reportCount == 0){<br />        if (buttonState == 1){ // if button is not pressed<br />      key = 0x30; // key = &#93;<br />      } else {<br />      key = 0x2F;  // key = &#91;<br />       }<br />    }<br /><br /><br />   reportCount++;<br />    reportBuffer&#91;0&#93; = 0;    /* no modifiers */<br />    reportBuffer&#91;1&#93; = key;<br />}<br /><br />static void checkButtonChange(void) {<br />   <br />   uchar tempButtonValue = bit_is_clear(BUTTON_PIN, BUTTON_BIT); //status of switch is stored in tempButtonValue <br /><br />   if (tempButtonValue != buttonState &amp;&amp; debounceTimeIsOver == 1){ //if status has changed and the debounce-delay is over<br />      buttonState = tempButtonValue;   // change buttonState to new state<br />      debounceTimeIsOver = 0;   // debounce timer starts<br />      reportCount = 0; // start report <br />   }<br />}<br /><br />/* ------------------------------------------------------------------------- */<br /><br />static void timerInit(void)<br />{<br />    TCCR1B = 0x0b;           /* select clock: 16.5M/1k -&gt; overflow rate = 16.5M/256k = 62.94 Hz */<br />}<br /><br />/* -------------------------------------------------------------------------------- */<br />/* ------------------------ interface to USB driver ------------------------ */<br />/* -------------------------------------------------------------------------------- */<br /><br />uchar   usbFunctionSetup(uchar data&#91;8&#93;)<br />{<br />usbRequest_t    *rq = (void *)data;<br /><br />    usbMsgPtr = reportBuffer;<br />    if((rq-&gt;bmRequestType &amp; USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){    /* class request type */<br />        if(rq-&gt;bRequest == USBRQ_HID_GET_REPORT){  /* wValue: ReportType (highbyte), ReportID (lowbyte) */<br />            /* we only have one report type, so don't look at wValue */<br />            buildReport();<br />            return sizeof(reportBuffer);<br />        }else if(rq-&gt;bRequest == USBRQ_HID_GET_IDLE){<br />            usbMsgPtr = &amp;idleRate;<br />            return 1;<br />        }else if(rq-&gt;bRequest == USBRQ_HID_SET_IDLE){<br />            idleRate = rq-&gt;wValue.bytes&#91;1&#93;;<br />        }<br />    }else{<br />        /* no vendor specific requests implemented */<br />    }<br />   return 0;<br />}<br /><br />/* ------------------------------------------------------------------------- */<br />/* ------------------------ Oscillator Calibration ------------------------- */<br />/* ------------------------------------------------------------------------- */<br /><br />/* Calibrate the RC oscillator to 8.25 MHz. The core clock of 16.5 MHz is<br /> * derived from the 66 MHz peripheral clock by dividing. Our timing reference<br /> * is the Start Of Frame signal (a single SE0 bit) available immediately after<br /> * a USB RESET. We first do a binary search for the OSCCAL value and then<br /> * optimize this value with a neighboorhod search.<br /> * This algorithm may also be used to calibrate the RC oscillator directly to<br /> * 12 MHz (no PLL involved, can therefore be used on almost ALL AVRs), but this<br /> * is wide outside the spec for the OSCCAL value and the required precision for<br /> * the 12 MHz clock! Use the RC oscillator calibrated to 12 MHz for<br /> * experimental purposes only!<br /> */<br />static void calibrateOscillator(void)<br />{<br />uchar       step = 128;<br />uchar       trialValue = 0, optimumValue;<br />int         x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);<br /><br />    /* do a binary search: */<br />    do{<br />        OSCCAL = trialValue + step;<br />        x = usbMeasureFrameLength();    /* proportional to current real frequency */<br />        if(x &lt; targetValue)             /* frequency still too low */<br />            trialValue += step;<br />        step &gt;&gt;= 1;<br />    }while(step &gt; 0);<br />    /* We have a precision of +/- 1 for optimum OSCCAL here */<br />    /* now do a neighborhood search for optimum value */<br />    optimumValue = trialValue;<br />    optimumDev = x; /* this is certainly far away from optimum */<br />    for(OSCCAL = trialValue - 1; OSCCAL &lt;= trialValue + 1; OSCCAL++){<br />        x = usbMeasureFrameLength() - targetValue;<br />        if(x &lt; 0)<br />            x = -x;<br />        if(x &lt; optimumDev){<br />            optimumDev = x;<br />            optimumValue = OSCCAL;<br />        }<br />    }<br />    OSCCAL = optimumValue;<br />}<br />/*<br />Note: This calibration algorithm may try OSCCAL values of up to 192 even if<br />the optimum value is far below 192. It may therefore exceed the allowed clock<br />frequency of the CPU in low voltage designs!<br />You may replace this search algorithm with any other algorithm you like if<br />you have additional constraints such as a maximum CPU clock.<br />For version 5.x RC oscillators (those with a split range of 2x128 steps, e.g.<br />ATTiny25, ATTiny45, ATTiny85), it may be useful to search for the optimum in<br />both regions.<br />*/<br /><br />void    usbEventResetReady(void)<br />{<br />    calibrateOscillator();<br />    eeprom_write_byte(0, OSCCAL);   /* store the calibrated value in EEPROM */<br />}<br /><br />/* ------------------------------------------------------------------------- */<br />/* --------------------------------- main ---------------------------------- */<br />/* ------------------------------------------------------------------------- */<br /><br />int main(void)<br />{<br />uchar   i;<br />uchar   calibrationValue;<br /><br />    calibrationValue = eeprom_read_byte(0); /* calibration value from last time */<br />    if(calibrationValue != 0xff){<br />        OSCCAL = calibrationValue;<br />    }<br />    <br />   //odDebugInit();<br />    usbInit();<br />    usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */<br />    i = 0;<br />    while(--i){             /* fake USB disconnect for &gt; 250 ms */<br />        wdt_reset();<br />        _delay_ms(1);<br />    }<br />    usbDeviceConnect();<br /><br />    wdt_enable(WDTO_1S);<br /><br />   /* turn on internal pull-up resistor for the switch */<br />    BUTTON_PORT |= _BV(BUTTON_BIT);<br /><br />    timerInit();<br /><br />    sei();<br /><br />    for(;;){    /* main event loop */<br />        wdt_reset();<br />        usbPoll();<br /><br />      /* A USB keypress cycle is defined as a scancode being present in a report, and<br />      then absent from a later report. To press and release the Caps Lock key, instead of<br />      holding it down, we need to send the report with the Caps Lock scancode and<br />      then an empty report. */<br />      <br />      if(usbInterruptIsReady() &amp;&amp; reportCount &lt; 2){ /* we can send another key */<br />           buildReport();<br />              usbSetInterrupt(reportBuffer, sizeof(reportBuffer));<br />           }<br />        checkButtonChange();<br />      timerPoll();<br />   }<br />      return 0;<br />}<br /></code></pre></div><br /><br /><br />and i get the Errors<br /><br /><div class="codebox"><p>Code: </p><pre><code>Build started 21.4.2009 at 02:16:17<br />avr-gcc.exe  -mmcu=atmega16 -Wall -gdwarf-2 -O0 -MD -MP -MT main.o -MF dep/main.o.d  -c  ../main.c<br />In file included from ../main.c:13:<br />c:/winavr-20090313/lib/gcc/../../avr/include/util/delay.h:85:3: warning: #warning &quot;F_CPU not defined for &lt;util/delay.h&gt;&quot;<br />c:/winavr-20090313/lib/gcc/../../avr/include/util/delay.h:90:3: warning: #warning &quot;Compiler optimizations disabled; functions from &lt;util/delay.h&gt; won't work as designed&quot;<br />avr-gcc.exe -mmcu=atmega16  main.o usbdrv.o oddebug.o     -o onek.elf<br />main.o: In function `calibrateOscillator':<br />C:\Documents and Settings\Alexander\Mina dokument\program\onek\default/../main.c:162: undefined reference to `usbMeasureFrameLength'<br />C:\Documents and Settings\Alexander\Mina dokument\program\onek\default/../main.c:172: undefined reference to `usbMeasureFrameLength'<br />usbdrv.o: In function `usbSetInterrupt':<br />C:\Documents and Settings\Alexander\Mina dokument\program\onek\default/../usbdrv.c:249: undefined reference to `usbCrc16Append'<br />usbdrv.o: In function `usbBuildTxBlock':<br />C:\Documents and Settings\Alexander\Mina dokument\program\onek\default/../usbdrv.c:484: undefined reference to `usbCrc16Append'<br />make: *** &#91;onek.elf&#93; Error 1<br />Build failed with 4 errors and 2 warnings...<br /></code></pre></div><br /><br />I have added oddebug.c and usbdrv.c under sourcefiles<br />and iarcompat.h/oddebug.h/usbconfig-prototype.h/usbdrv.h under headerfiles.<br /><br /><br />What could be the reason that i get the &quot;undefined reference&quot;? error?<br /><br /><br /><br />Help greately appreciated!<br /><br />Regards<br />Alexander<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=2163">ranstam</a> — Tue Apr 21, 2009 2:21 am</p><hr />
]]></content>
	</entry>
	</feed>
