diff options
author | Christian Starkjohann <cs+github@obdev.at> | 2008-10-20 16:13:19 +0000 |
---|---|---|
committer | Christian Starkjohann <cs+github@obdev.at> | 2008-10-20 16:13:19 +0000 |
commit | 2b8735a2de7435ba089a91ea9309aa2c20a7600f (patch) | |
tree | a6a507d09e79e62f75a817e6fc4fa7d946de8cfe /examples/drivertest/firmware/main.c | |
parent | 0528a42590ec57fb3c18dd1995ddbf577b3ba003 (diff) | |
download | v-usb-2b8735a2de7435ba089a91ea9309aa2c20a7600f.tar.gz v-usb-2b8735a2de7435ba089a91ea9309aa2c20a7600f.tar.bz2 v-usb-2b8735a2de7435ba089a91ea9309aa2c20a7600f.zip |
- added example project for testing driver reliability and features
Diffstat (limited to 'examples/drivertest/firmware/main.c')
-rw-r--r-- | examples/drivertest/firmware/main.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/examples/drivertest/firmware/main.c b/examples/drivertest/firmware/main.c new file mode 100644 index 0000000..bfebc7e --- /dev/null +++ b/examples/drivertest/firmware/main.c @@ -0,0 +1,114 @@ +/* Name: main.c + * Project: hid-custom-rq example + * Author: Christian Starkjohann + * Creation Date: 2008-04-07 + * Tabsize: 4 + * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) + * This Revision: $Id$ + */ + +/* +This example should run on most AVRs with only little changes. No special +hardware resources except INT0 are used. You may have to change usbconfig.h for +different I/O pins for USB. Please note that USB D+ must be the INT0 pin, or +at least be connected to INT0 as well. +*/ + +#include <avr/io.h> +#include <avr/wdt.h> +#include <avr/interrupt.h> /* for sei() */ +#include <util/delay.h> /* for _delay_ms() */ + +#include <avr/pgmspace.h> /* required by usbdrv.h */ +#include "usbdrv.h" +#include "oddebug.h" /* This is also an example for using debug macros */ +#include "requests.h" /* The custom request numbers we use */ + +#if TUNE_OSCCAL +uchar lastTimer0Value; +#endif + +#if CALIBRATE_OSCCAL +#include "osccal.c" +#endif + + +/* ------------------------------------------------------------------------- */ +/* ----------------------------- USB interface ----------------------------- */ +/* ------------------------------------------------------------------------- */ + +static uchar dataBuffer[64]; +static uchar writeIndex; + +uchar usbFunctionWrite(uchar *data, uchar len) +{ + + if(writeIndex + len <= sizeof(dataBuffer)){ + uchar i; + for(i = 0; i < len; i++){ + dataBuffer[writeIndex++] = *data++; + } + } + return writeIndex >= sizeof(dataBuffer); +} + +usbMsgLen_t usbFunctionSetup(uchar data[8]) +{ +usbRequest_t *rq = (void *)data; + + DBG1(0x50, &rq->bRequest, 1); /* debug output: print our request */ + if(rq->bRequest == CUSTOM_RQ_SET_DATA){ + writeIndex = 0; + return USB_NO_MSG; + }else if(rq->bRequest == CUSTOM_RQ_GET_DATA){ + usbMsgPtr = dataBuffer; /* tell the driver which data to return */ + return sizeof(dataBuffer); /* tell the driver how many bytes to send */ + }else if(rq->bRequest == CUSTOM_RQ_SET_OSCCAL){ + OSCCAL = rq->wValue.bytes[0]; + }else if(rq->bRequest == CUSTOM_RQ_GET_OSCCAL){ + usbMsgPtr = (uchar *)&OSCCAL; + return 1; + } + return 0; /* default for not implemented requests: return no data back to host */ +} + +/* ------------------------------------------------------------------------- */ + +int main(void) +{ +uchar i; + + wdt_enable(WDTO_1S); + /* Even if you don't use the watchdog, turn it off here. On newer devices, + * the status of the watchdog (on/off, period) is PRESERVED OVER RESET! + */ + DBG1(0x00, 0, 0); /* debug output: main starts */ + /* RESET status: all port bits are inputs without pull-up. + * That's the way we need D+ and D-. Therefore we don't need any + * additional hardware initialization. + */ + TCCR2 = 9 | (1 << COM20); + OCR2 = 3; /* should give F_CPU/8 clock */ + + DDRB = (1 << 2) | (1 << 3); + TCCR0 = 3; /* 1/64 prescaler */ + odDebugInit(); + usbInit(); + usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ + i = 0; + while(--i){ /* fake USB disconnect for > 250 ms */ + wdt_reset(); + _delay_ms(1); + } + usbDeviceConnect(); + sei(); + DBG1(0x01, 0, 0); /* debug output: main loop starts */ + for(;;){ /* main event loop */ + wdt_reset(); + usbPoll(); + } + return 0; +} + +/* ------------------------------------------------------------------------- */ |