]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Output/pjrcUSB/avr/usb_keyboard_serial.c
Initial code for USB cable detection
[kiibohd-controller.git] / Output / pjrcUSB / avr / usb_keyboard_serial.c
index 260733d53b491521f5ccd4571eabdb98c172cd20..1403c9ae3ca06d83d7a5f72f26cbdcff11c83fca 100644 (file)
@@ -104,45 +104,46 @@ inline void usb_keyboard_send()
 
        // Send NKRO keyboard interrupts packet(s)
        case 1:
-               // Check modifiers
-               if ( USBKeys_Changed & USBKeyChangeState_Modifiers )
+               // Check system control keys
+               if ( USBKeys_Changed & USBKeyChangeState_System )
                {
-                       UEDATX = 0x01; // ID
-                       UEDATX = USBKeys_Modifiers;
+                       UEDATX = 0x02; // ID
+                       UEDATX = USBKeys_SysCtrl;
                        UEINTX = 0; // Finished with ID
 
-                       USBKeys_Changed &= ~USBKeyChangeState_Modifiers; // Mark sent
+                       USBKeys_Changed &= ~USBKeyChangeState_System; // Mark sent
                }
-               // Check main key section
-               if ( USBKeys_Changed & USBKeyChangeState_MainKeys )
+
+               // Check consumer control keys
+               if ( USBKeys_Changed & USBKeyChangeState_Consumer )
                {
                        UEDATX = 0x03; // ID
-
-                       // 4-49 (first 6 bytes)
-                       for ( uint8_t byte = 0; byte < 6; byte++ )
-                               UEDATX = USBKeys_Keys[ byte ];
-
+                       UEDATX = (uint8_t)(USBKeys_ConsCtrl & 0x00FF);
+                       UEDATX = (uint8_t)(USBKeys_ConsCtrl >> 8);
                        UEINTX = 0; // Finished with ID
 
-                       USBKeys_Changed &= ~USBKeyChangeState_MainKeys; // Mark sent
+                       USBKeys_Changed &= ~USBKeyChangeState_Consumer; // Mark sent
                }
-               // Check secondary key section
-               if ( USBKeys_Changed & USBKeyChangeState_SecondaryKeys )
+
+               // Standard HID Keyboard
+               if ( USBKeys_Changed )
                {
-                       UEDATX = 0x04; // ID
+                       UEDATX = 0x01; // ID
+
+                       // Modifiers
+                       UEDATX = USBKeys_Modifiers;
 
-                       // 51-164 (Middle 15 bytes)
-                       for ( uint8_t byte = 6; byte < 21; byte++ )
+                       // 4-49 (first 6 bytes)
+                       for ( uint8_t byte = 0; byte < 6; byte++ )
                                UEDATX = USBKeys_Keys[ byte ];
 
-                       UEINTX = 0; // Finished with ID
+                       // 51-155 (Middle 14 bytes)
+                       for ( uint8_t byte = 6; byte < 20; byte++ )
+                               UEDATX = USBKeys_Keys[ byte ];
 
-                       USBKeys_Changed &= ~USBKeyChangeState_SecondaryKeys; // Mark sent
-               }
-               // Check tertiary key section
-               if ( USBKeys_Changed & USBKeyChangeState_TertiaryKeys )
-               {
-                       UEDATX = 0x05; // ID
+                       // 157-164 (Next byte)
+                       for ( uint8_t byte = 20; byte < 21; byte++ )
+                               UEDATX = USBKeys_Keys[ byte ];
 
                        // 176-221 (last 6 bytes)
                        for ( uint8_t byte = 21; byte < 27; byte++ )
@@ -150,26 +151,7 @@ inline void usb_keyboard_send()
 
                        UEINTX = 0; // Finished with ID
 
-                       USBKeys_Changed &= ~USBKeyChangeState_TertiaryKeys; // Mark sent
-               }
-               // Check system control keys
-               if ( USBKeys_Changed & USBKeyChangeState_System )
-               {
-                       UEDATX = 0x06; // ID
-                       UEDATX = USBKeys_SysCtrl;
-                       UEINTX = 0; // Finished with ID
-
-                       USBKeys_Changed &= ~USBKeyChangeState_System; // Mark sent
-               }
-               // Check consumer control keys
-               if ( USBKeys_Changed & USBKeyChangeState_Consumer )
-               {
-                       UEDATX = 0x07; // ID
-                       UEDATX = (uint8_t)(USBKeys_ConsCtrl & 0x00FF);
-                       UEDATX = (uint8_t)(USBKeys_ConsCtrl >> 8);
-                       UEINTX = 0; // Finished with ID
-
-                       USBKeys_Changed &= ~USBKeyChangeState_Consumer; // Mark sent
+                       USBKeys_Changed = USBKeyChangeState_None; // Mark sent
                }
 
                break;
@@ -608,8 +590,13 @@ void wdt_init()
 
 
 // initialize USB
-void usb_init()
+uint8_t usb_init()
 {
+       // Check to see if a usb cable has been plugged in
+       // XXX Not tested (also, not currently needed) -HaaTa
+       //if ( USB0_STAT & (1 << 1)
+       //      return 0;
+
        HW_CONFIG();
        USB_FREEZE();                           // enable USB
        PLL_CONFIG();                           // config PLL
@@ -622,6 +609,8 @@ void usb_init()
 
        // Disable watchdog timer after possible software reset
        //wdt_init(); // XXX Not working...seems to be ok without this, not sure though
+
+       return 1;
 }
 
 // return 0 if the USB is not configured, or the configuration