Merge pull request #132 from erikarvstedt/update-pyusb
[pub/USBasp.git] / Demos / Device / LowLevel / Mouse / Mouse.c
index 4df986c..5aa4176 100644 (file)
@@ -1,13 +1,13 @@
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2011.
+     Copyright (C) Dean Camera, 2018.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2018  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
@@ -18,7 +18,7 @@
   advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
-  The author disclaim all warranties with regard to this
+  The author disclaims all warranties with regard to this
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
 
 #include "Mouse.h"
 
-/** Indicates what report mode the host has requested, true for normal HID reporting mode, false for special boot
+/** Indicates what report mode the host has requested, true for normal HID reporting mode, \c false for special boot
  *  protocol reporting mode.
  */
-bool UsingReportProtocol = true;
+static bool UsingReportProtocol = true;
 
 /** Current Idle period. This is set by the host via a Set Idle HID class request to silence the device's reports
  *  for either the entire idle duration, or until the report status changes (e.g. the user moves the mouse).
  */
-uint16_t IdleCount = 0;
+static uint16_t IdleCount = 0;
 
 /** Current Idle period remaining. When the IdleCount value is set, this tracks the remaining number of idle
  *  milliseconds. This is separate to the IdleCount timer and is incremented and compared as the host may request
  *  the current idle period via a Get Idle HID class request, thus its value must be preserved.
  */
-uint16_t IdleMSRemaining = 0;
+static uint16_t IdleMSRemaining = 0;
 
 
 /** Main program entry point. This routine configures the hardware required by the application, then
@@ -61,7 +61,7 @@ int main(void)
        SetupHardware();
 
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
-       sei();
+       GlobalInterruptEnable();
 
        for (;;)
        {
@@ -73,12 +73,24 @@ int main(void)
 /** Configures the board hardware and chip peripherals for the demo's functionality. */
 void SetupHardware(void)
 {
+#if (ARCH == ARCH_AVR8)
        /* Disable watchdog if enabled by bootloader/fuses */
        MCUSR &= ~(1 << WDRF);
        wdt_disable();
 
        /* Disable clock division */
        clock_prescale_set(clock_div_1);
+#elif (ARCH == ARCH_XMEGA)
+       /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
+       XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
+       XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
+
+       /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
+       XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
+       XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
+
+       PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
+#endif
 
        /* Hardware Initialization */
        Joystick_Init();
@@ -116,8 +128,7 @@ void EVENT_USB_Device_ConfigurationChanged(void)
        bool ConfigSuccess = true;
 
        /* Setup HID Report Endpoint */
-       ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
-                                                   MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
+       ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_EPADDR, EP_TYPE_INTERRUPT, MOUSE_EPSIZE, 1);
 
        /* Turn on Start-of-Frame events for tracking HID report period expiry */
        USB_Device_EnableSOFEvents();
@@ -160,7 +171,7 @@ void EVENT_USB_Device_ControlRequest(void)
                                Endpoint_ClearSETUP();
 
                                /* Write the current protocol flag to the host */
-                               Endpoint_Write_Byte(UsingReportProtocol);
+                               Endpoint_Write_8(UsingReportProtocol);
 
                                Endpoint_ClearIN();
                                Endpoint_ClearStatusStage();
@@ -195,7 +206,7 @@ void EVENT_USB_Device_ControlRequest(void)
                                Endpoint_ClearSETUP();
 
                                /* Write the current idle duration to the host, must be divided by 4 before sent to host */
-                               Endpoint_Write_Byte(IdleCount >> 2);
+                               Endpoint_Write_8(IdleCount >> 2);
 
                                Endpoint_ClearIN();
                                Endpoint_ClearStatusStage();
@@ -233,10 +244,10 @@ void CreateMouseReport(USB_MouseReport_Data_t* const ReportData)
        if (JoyStatus_LCL & JOY_LEFT)
          ReportData->X = -1;
        else if (JoyStatus_LCL & JOY_RIGHT)
-         ReportData->X = 1;
+         ReportData->X =  1;
 
        if (JoyStatus_LCL & JOY_PRESS)
-         ReportData->Button  = (1 << 0);
+         ReportData->Button |= (1 << 0);
 
        if (ButtonStatus_LCL & BUTTONS_BUTTON1)
          ReportData->Button |= (1 << 1);
@@ -271,7 +282,7 @@ void SendNextReport(void)
        }
 
        /* Select the Mouse Report Endpoint */
-       Endpoint_SelectEndpoint(MOUSE_EPNUM);
+       Endpoint_SelectEndpoint(MOUSE_EPADDR);
 
        /* Check if Mouse Endpoint Ready for Read/Write and if we should send a new report */
        if (Endpoint_IsReadWriteAllowed() && SendReport)
@@ -280,7 +291,7 @@ void SendNextReport(void)
                PrevMouseReportData = MouseReportData;
 
                /* Write Mouse Report Data */
-               Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));
+               Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData), NULL);
 
                /* Finalize the stream transfer to send the last packet */
                Endpoint_ClearIN();