Guest User

avrtest

a guest
Mar 7th, 2016
165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Codes changed/added:
  3. - (change) usbFunctionSetup(uchar data[8]) > add build report func call
  4. - (add) usbFunctionDescriptor
  5. -
  6.  
  7.  
  8. */
  9.  
  10. #include <avr/io.h>
  11. #include <avr/wdt.h>
  12. #include <avr/interrupt.h>  /* for sei() */
  13. #include <util/delay.h>     /* for _delay_ms() */
  14.  
  15. #include <avr/pgmspace.h>   /* required by usbdrv.h */
  16. #include "usbdrv.h"
  17. //#include "oddebug.h"        /* This is also an example for using debug macros */
  18. #include "a2d.h"
  19.  
  20. /* ------------------------------------------------------------------------- */
  21. /* ----------------------------- USB interface ----------------------------- */
  22. /* ------------------------------------------------------------------------- */
  23.  
  24. PROGMEM const char usbHidReportDescriptor[45] = { /* USB report descriptor, size must match usbconfig.h */
  25.     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
  26.     0x09, 0x05,                    // USAGE (Game Pad)
  27.     0xa1, 0x01,                    // COLLECTION (Application)
  28.     0xa1, 0x00,                    //   COLLECTION (Physical)
  29.     0x05, 0x09,                    //     USAGE_PAGE (Button)
  30.     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
  31.     0x29, 0x08,                    //     USAGE_MAXIMUM (Button 8)
  32.     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
  33.     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
  34.     0x95, 0x08,                    //     REPORT_COUNT (8)
  35.     0x75, 0x01,                    //     REPORT_SIZE (1)
  36.     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
  37.     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
  38.     0x09, 0x33,                    //     USAGE (Rx)
  39.     0x09, 0x34,                    //     USAGE (Ry)
  40.     0x09, 0x35,                    //     USAGE (Rz)
  41.     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
  42.     0x26, 0xff, 0x00,              //     LOGICAL_MAXIMUM (255)
  43.     0x75, 0x08,                    //     REPORT_SIZE (8)
  44.     0x95, 0x03,                    //     REPORT_COUNT (3)
  45.     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
  46.     0xc0,                          //     END_COLLECTION
  47.     0xc0                           // END_COLLECTION
  48. };
  49.  
  50. typedef struct{
  51.     uchar   buttonMask; //8 button but only 7 actually used
  52.     uchar    rx; //3 analog in
  53.     uchar    ry;
  54.     uchar    rz;
  55. }report_t;
  56.  
  57. static report_t reportBuffer;
  58. static uchar    idleRate;   /* repeat rate for keyboards, never used for mice */
  59.  
  60.  
  61. usbMsgLen_t usbFunctionSetup(uchar data[8])
  62. {
  63. usbRequest_t    *rq = (void *)data;
  64.  
  65.     /* The following requests are never used. But since they are required by
  66.      * the specification, we implement them in this example.
  67.      */
  68.     if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){    /* class request type */
  69.         //DBG1(0x50, &rq->bRequest, 1);   /* debug output: print our request */
  70.         if(rq->bRequest == USBRQ_HID_GET_REPORT){  /* wValue: ReportType (highbyte), ReportID (lowbyte) */
  71.             /* we only have one report type, so don't look at wValue */
  72.             usbMsgPtr = (void *)&reportBuffer;
  73.             return sizeof(reportBuffer);
  74.         }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  75.             usbMsgPtr = &idleRate;
  76.             return 1;
  77.         }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  78.             idleRate = rq->wValue.bytes[1];
  79.         }
  80.     }else{
  81.         /* no vendor specific requests implemented */
  82.     }
  83.     return 0;   /* default for not implemented requests: return no data back to host */
  84. }
  85.  
  86. /* ------------------------------------------------------------------------- */
  87.  
  88. int __attribute__((noreturn)) main(void)
  89. {
  90. uchar   i;
  91.  
  92.     wdt_enable(WDTO_1S);
  93.     /* Even if you don't use the watchdog, turn it off here. On newer devices,
  94.      * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
  95.      */
  96.     /* RESET status: all port bits are inputs without pull-up.
  97.      * That's the way we need D+ and D-. Therefore we don't need any
  98.      * additional hardware initialization.
  99.      */
  100.  
  101.     /*
  102.     used I/O pin:
  103.     as digital in: PB0-5, PD0
  104.     as analog in : PC0-2
  105.     PD1 led output
  106.     */
  107.     PORTB |= 0b00111111; //enable internal pull up
  108.     PORTD |= 0b00000001; //enable internal pull up
  109.     DDRD |= 0b00000010; //set pd1 as out
  110.  
  111.     a2dInit(); 
  112.  
  113.     //odDebugInit();
  114.     //DBG1(0x00, 0, 0);       /* debug output: main starts */
  115.     usbInit();
  116.     usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
  117.     i = 0;
  118.     while(--i){             /* fake USB disconnect for > 250 ms */
  119.         wdt_reset();
  120.         _delay_ms(1);
  121.     }
  122.     usbDeviceConnect();
  123.     sei();
  124.     //DBG1(0x01, 0, 0);       /* debug output: main loop starts */
  125.     for(;;){                /* main event loop */
  126.         //DBG1(0x02, 0, 0);   /* debug output: main loop iterates */
  127.         wdt_reset();
  128.         usbPoll();
  129.         if(usbInterruptIsReady()){
  130.             /* called after every poll of the interrupt endpoint */
  131.            
  132.             //prepare report buffer here
  133.             reportBuffer.buttonMask = ~((PINB & 0b00111111) | ((PIND & 1) << 6));
  134.             reportBuffer.rx = i;
  135.             i++;
  136.             //reportBuffer.rx = a2dRead(0);
  137.             //reportBuffer.ry = a2dRead(1);
  138.             //reportBuffer.rz = a2dRead(2);
  139.  
  140.             //DBG1(0x03, 0, 0);   /* debug output: interrupt report prepared */
  141.             usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
  142.         }
  143.     }
  144. }
  145.  
  146. /* ------------------------------------------------------------------------- */
RAW Paste Data