Upgrade doxygen configuration files to the latest version.
[pub/lufa.git] / Demos / Device / LowLevel / Joystick / Joystick.c
index 08c4892..db39961 100644 (file)
@@ -1,24 +1,24 @@
 /*
              LUFA Library
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2010.
-              
+     Copyright (C) Dean Camera, 2021.
+
   dean [at] fourwalledcubicle [dot] com
   dean [at] fourwalledcubicle [dot] com
-      www.fourwalledcubicle.com
+           www.lufa-lib.org
 */
 
 /*
 */
 
 /*
-  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2021  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
 
-  Permission to use, copy, modify, distribute, and sell this 
+  Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
   software and its documentation for any purpose is hereby granted
-  without fee, provided that the above copyright notice appear in 
+  without fee, provided that the above copyright notice appear in
   all copies and that both that the copyright notice and this
   all copies and that both that the copyright notice and this
-  permission notice and warranty disclaimer appear in supporting 
-  documentation, and that the name of the author not be used in 
-  advertising or publicity pertaining to distribution of the 
+  permission notice and warranty disclaimer appear in supporting
+  documentation, and that the name of the author not be used in
+  advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
   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
   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
@@ -42,9 +42,9 @@
 int main(void)
 {
        SetupHardware();
 int main(void)
 {
        SetupHardware();
-       
+
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
-       sei();
+       GlobalInterruptEnable();
 
        for (;;)
        {
 
        for (;;)
        {
@@ -56,12 +56,24 @@ int main(void)
 /** Configures the board hardware and chip peripherals for the demo's functionality. */
 void SetupHardware(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);
        /* 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();
 
        /* Hardware Initialization */
        Joystick_Init();
@@ -90,45 +102,42 @@ void EVENT_USB_Device_Disconnect(void)
 
 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
  *  of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started.
 
 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
  *  of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started.
- */ 
+ */
 void EVENT_USB_Device_ConfigurationChanged(void)
 {
        bool ConfigSuccess = true;
 
        /* Setup HID Report Endpoint */
 void EVENT_USB_Device_ConfigurationChanged(void)
 {
        bool ConfigSuccess = true;
 
        /* Setup HID Report Endpoint */
-       ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
-                                                   JOYSTICK_EPSIZE, ENDPOINT_BANK_SINGLE);
+       ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
 
        /* Indicate endpoint configuration success or failure */
        LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
 }
 
 
        /* Indicate endpoint configuration success or failure */
        LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
 }
 
-/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
- *  control requests that are not handled internally by the USB library (including the HID commands, which are
- *  all issued via the control endpoint), so that they can be handled appropriately for the application.
+/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
+ *  the device from the USB host before passing along unhandled control requests to the library for processing
+ *  internally.
  */
  */
-void EVENT_USB_Device_UnhandledControlRequest(void)
+void EVENT_USB_Device_ControlRequest(void)
 {
        /* Handle HID Class specific requests */
        switch (USB_ControlRequest.bRequest)
        {
 {
        /* Handle HID Class specific requests */
        switch (USB_ControlRequest.bRequest)
        {
-               case REQ_GetReport:
+               case HID_REQ_GetReport:
                        if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
                        {
                                USB_JoystickReport_Data_t JoystickReportData;
                        if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
                        {
                                USB_JoystickReport_Data_t JoystickReportData;
-                               
-                               Endpoint_ClearSETUP();
 
 
-                               /* Create the next HID report to send to the host */                            
+                               /* Create the next HID report to send to the host */
                                GetNextReport(&JoystickReportData);
                                GetNextReport(&JoystickReportData);
-                                       
+
+                               Endpoint_ClearSETUP();
+
                                /* Write the report data to the control endpoint */
                                Endpoint_Write_Control_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));
                                /* Write the report data to the control endpoint */
                                Endpoint_Write_Control_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));
-                               
-                               /* Finalize the stream transfer to send the last packet or clear the host abort */
                                Endpoint_ClearOUT();
                        }
                                Endpoint_ClearOUT();
                        }
-               
+
                        break;
        }
 }
                        break;
        }
 }
@@ -137,7 +146,7 @@ void EVENT_USB_Device_UnhandledControlRequest(void)
  *
  *  \param[out] ReportData  Pointer to a HID report data structure to be filled
  *
  *
  *  \param[out] ReportData  Pointer to a HID report data structure to be filled
  *
- *  \return Boolean true if the new report differs from the last report, false otherwise
+ *  \return Boolean \c true if the new report differs from the last report, \c false otherwise
  */
 bool GetNextReport(USB_JoystickReport_Data_t* const ReportData)
 {
  */
 bool GetNextReport(USB_JoystickReport_Data_t* const ReportData)
 {
@@ -161,11 +170,11 @@ bool GetNextReport(USB_JoystickReport_Data_t* const ReportData)
          ReportData->X =  100;
 
        if (JoyStatus_LCL & JOY_PRESS)
          ReportData->X =  100;
 
        if (JoyStatus_LCL & JOY_PRESS)
-         ReportData->Button  = (1 << 1);
-         
+         ReportData->Button |= (1 << 1);
+
        if (ButtonStatus_LCL & BUTTONS_BUTTON1)
          ReportData->Button |= (1 << 0);
        if (ButtonStatus_LCL & BUTTONS_BUTTON1)
          ReportData->Button |= (1 << 0);
-         
+
        /* Check if the new report is different to the previous report */
        InputChanged = (uint8_t)(PrevJoyStatus ^ JoyStatus_LCL) | (uint8_t)(PrevButtonStatus ^ ButtonStatus_LCL);
 
        /* Check if the new report is different to the previous report */
        InputChanged = (uint8_t)(PrevJoyStatus ^ JoyStatus_LCL) | (uint8_t)(PrevButtonStatus ^ ButtonStatus_LCL);
 
@@ -183,25 +192,26 @@ void HID_Task(void)
        /* Device must be connected and configured for the task to run */
        if (USB_DeviceState != DEVICE_STATE_Configured)
          return;
        /* Device must be connected and configured for the task to run */
        if (USB_DeviceState != DEVICE_STATE_Configured)
          return;
-  
+
        /* Select the Joystick Report Endpoint */
        /* Select the Joystick Report Endpoint */
-       Endpoint_SelectEndpoint(JOYSTICK_EPNUM);
+       Endpoint_SelectEndpoint(JOYSTICK_EPADDR);
 
        /* Check to see if the host is ready for another packet */
        if (Endpoint_IsINReady())
        {
                USB_JoystickReport_Data_t JoystickReportData;
 
        /* Check to see if the host is ready for another packet */
        if (Endpoint_IsINReady())
        {
                USB_JoystickReport_Data_t JoystickReportData;
-               
+
                /* Create the next HID report to send to the host */
                GetNextReport(&JoystickReportData);
                /* Create the next HID report to send to the host */
                GetNextReport(&JoystickReportData);
-       
+
                /* Write Joystick Report Data */
                /* Write Joystick Report Data */
-               Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));
+               Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData), NULL);
 
                /* Finalize the stream transfer to send the last packet */
                Endpoint_ClearIN();
 
                /* Finalize the stream transfer to send the last packet */
                Endpoint_ClearIN();
-               
+
                /* Clear the report data afterwards */
                memset(&JoystickReportData, 0, sizeof(JoystickReportData));
        }
 }
                /* Clear the report data afterwards */
                memset(&JoystickReportData, 0, sizeof(JoystickReportData));
        }
 }
+