main.c 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Name: main.c
  2. * Project: custom-class, a basic USB example
  3. * Author: Christian Starkjohann
  4. * Creation Date: 2008-04-09
  5. * Tabsize: 4
  6. * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
  7. * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
  8. * This Revision: $Id: main.c 692 2008-11-07 15:07:40Z cs $
  9. */
  10. /*
  11. This example should run on most AVRs with only little changes. No special
  12. hardware resources except INT0 are used. You may have to change usbconfig.h for
  13. different I/O pins for USB. Please note that USB D+ must be the INT0 pin, or
  14. at least be connected to INT0 as well.
  15. We assume that an LED is connected to port B bit 0. If you connect it to a
  16. different port or bit, change the macros below:
  17. */
  18. #define LED_PORT_DDR DDRB
  19. #define LED_PORT_OUTPUT PORTB
  20. #define LED_BIT 0
  21. #include <avr/io.h>
  22. #include <avr/wdt.h>
  23. #include <avr/interrupt.h> /* for sei() */
  24. #include <util/delay.h> /* for _delay_ms() */
  25. #include <avr/pgmspace.h> /* required by usbdrv.h */
  26. #include "usbdrv.h"
  27. #include "oddebug.h" /* This is also an example for using debug macros */
  28. #include "requests.h" /* The custom request numbers we use */
  29. /* ------------------------------------------------------------------------- */
  30. /* ----------------------------- USB interface ----------------------------- */
  31. /* ------------------------------------------------------------------------- */
  32. usbMsgLen_t usbFunctionSetup(uchar data[8])
  33. {
  34. usbRequest_t *rq = (void *)data;
  35. static uchar dataBuffer[4]; /* buffer must stay valid when usbFunctionSetup returns */
  36. if(rq->bRequest == CUSTOM_RQ_ECHO){ /* echo -- used for reliability tests */
  37. dataBuffer[0] = rq->wValue.bytes[0];
  38. dataBuffer[1] = rq->wValue.bytes[1];
  39. dataBuffer[2] = rq->wIndex.bytes[0];
  40. dataBuffer[3] = rq->wIndex.bytes[1];
  41. usbMsgPtr = dataBuffer; /* tell the driver which data to return */
  42. return 4;
  43. }else if(rq->bRequest == CUSTOM_RQ_SET_STATUS){
  44. if(rq->wValue.bytes[0] & 1){ /* set LED */
  45. LED_PORT_OUTPUT |= _BV(LED_BIT);
  46. }else{ /* clear LED */
  47. LED_PORT_OUTPUT &= ~_BV(LED_BIT);
  48. }
  49. }else if(rq->bRequest == CUSTOM_RQ_GET_STATUS){
  50. dataBuffer[0] = ((LED_PORT_OUTPUT & _BV(LED_BIT)) != 0);
  51. usbMsgPtr = dataBuffer; /* tell the driver which data to return */
  52. return 1; /* tell the driver to send 1 byte */
  53. }
  54. return 0; /* default for not implemented requests: return no data back to host */
  55. }
  56. /* ------------------------------------------------------------------------- */
  57. int main(void)
  58. {
  59. uchar i;
  60. wdt_enable(WDTO_1S);
  61. /* Even if you don't use the watchdog, turn it off here. On newer devices,
  62. * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
  63. */
  64. DBG1(0x00, 0, 0); /* debug output: main starts */
  65. /* RESET status: all port bits are inputs without pull-up.
  66. * That's the way we need D+ and D-. Therefore we don't need any
  67. * additional hardware initialization.
  68. */
  69. odDebugInit();
  70. usbInit();
  71. usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
  72. i = 0;
  73. while(--i){ /* fake USB disconnect for > 250 ms */
  74. wdt_reset();
  75. _delay_ms(1);
  76. }
  77. usbDeviceConnect();
  78. LED_PORT_DDR |= _BV(LED_BIT); /* make the LED bit an output */
  79. sei();
  80. DBG1(0x01, 0, 0); /* debug output: main loop starts */
  81. for(;;){ /* main event loop */
  82. DBG1(0x02, 0, 0); /* debug output: main loop iterates */
  83. wdt_reset();
  84. usbPoll();
  85. }
  86. return 0;
  87. }
  88. /* ------------------------------------------------------------------------- */