Fix up the ADC driver for the U4 parts to remove a typo on one of the register names.
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / DevChapter9.c
index 4527c1b..b7f811b 100644 (file)
@@ -1,13 +1,13 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
 \r
   Permission to use, copy, modify, distribute, and sell this \r
   software and its documentation for any purpose is hereby granted\r
 \r
   Permission to use, copy, modify, distribute, and sell this \r
   software and its documentation for any purpose is hereby granted\r
   this software.\r
 */\r
 \r
   this software.\r
 */\r
 \r
+#define  __INCLUDE_FROM_USB_DRIVER\r
 #include "../HighLevel/USBMode.h"\r
 \r
 #if defined(USB_CAN_BE_DEVICE)\r
 \r
 #include "../HighLevel/USBMode.h"\r
 \r
 #if defined(USB_CAN_BE_DEVICE)\r
 \r
-#define  INCLUDE_FROM_DEVCHAPTER9_C\r
+#define  __INCLUDE_FROM_DEVCHAPTER9_C\r
 #include "DevChapter9.h"\r
 \r
 uint8_t USB_ConfigurationNumber;\r
 #include "DevChapter9.h"\r
 \r
 uint8_t USB_ConfigurationNumber;\r
@@ -135,8 +136,7 @@ static void USB_Device_SetAddress(void)
                  return;\r
        }\r
 \r
                  return;\r
        }\r
 \r
-       if (DeviceAddress)\r
-         USB_DeviceState = DEVICE_STATE_Addressed;\r
+       USB_DeviceState = (DeviceAddress) ? DEVICE_STATE_Addressed : DEVICE_STATE_Default;\r
 \r
        UDADDR = ((1 << ADDEN) | DeviceAddress);\r
 \r
 \r
        UDADDR = ((1 << ADDEN) | DeviceAddress);\r
 \r
@@ -145,12 +145,21 @@ static void USB_Device_SetAddress(void)
 \r
 static void USB_Device_SetConfiguration(void)\r
 {\r
 \r
 static void USB_Device_SetConfiguration(void)\r
 {\r
+       if (USB_DeviceState != DEVICE_STATE_Addressed)\r
+         return;\r
+       \r
 #if defined(FIXED_NUM_CONFIGURATIONS)\r
        if ((uint8_t)USB_ControlRequest.wValue > FIXED_NUM_CONFIGURATIONS)\r
          return;\r
 #else\r
 #if defined(FIXED_NUM_CONFIGURATIONS)\r
        if ((uint8_t)USB_ControlRequest.wValue > FIXED_NUM_CONFIGURATIONS)\r
          return;\r
 #else\r
-       #if !defined(USE_FLASH_DESCRIPTORS) && !defined(USE_EEPROM_DESCRIPTORS) && !defined(USE_RAM_DESCRIPTORS)\r
-       uint8_t MemoryAddressSpace;\r
+       #if defined(USE_FLASH_DESCRIPTORS)\r
+               #define MemoryAddressSpace  MEMSPACE_FLASH\r
+       #elif defined(USE_EEPROM_DESCRIPTORS)\r
+               #define MemoryAddressSpace  MEMSPACE_EEPROM\r
+       #elif defined(USE_SRAM_DESCRIPTORS)\r
+               #define MemoryAddressSpace  MEMSPACE_SRAM\r
+       #else\r
+               uint8_t MemoryAddressSpace;\r
        #endif\r
        \r
        USB_Descriptor_Device_t* DevDescriptorPtr;\r
        #endif\r
        \r
        USB_Descriptor_Device_t* DevDescriptorPtr;\r
@@ -164,16 +173,6 @@ static void USB_Device_SetConfiguration(void)
                return;\r
        }\r
        \r
                return;\r
        }\r
        \r
-       #if defined(USE_RAM_DESCRIPTORS)\r
-       if ((uint8_t)USB_ControlRequest.wValue > DevDescriptorPtr->NumberOfConfigurations)\r
-         return;\r
-       #elif defined (USE_EEPROM_DESCRIPTORS)\r
-       if ((uint8_t)USB_ControlRequest.wValue > eeprom_read_byte(&DevDescriptorPtr->NumberOfConfigurations))\r
-         return;\r
-       #elif defined (USE_FLASH_DESCRIPTORS)\r
-       if ((uint8_t)USB_ControlRequest.wValue > pgm_read_byte(&DevDescriptorPtr->NumberOfConfigurations))\r
-         return;\r
-       #else\r
        if (MemoryAddressSpace == MEMSPACE_FLASH)\r
        {\r
                if (((uint8_t)USB_ControlRequest.wValue > pgm_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))\r
        if (MemoryAddressSpace == MEMSPACE_FLASH)\r
        {\r
                if (((uint8_t)USB_ControlRequest.wValue > pgm_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))\r
@@ -189,7 +188,6 @@ static void USB_Device_SetConfiguration(void)
                if ((uint8_t)USB_ControlRequest.wValue > DevDescriptorPtr->NumberOfConfigurations)\r
                  return;\r
        }\r
                if ((uint8_t)USB_ControlRequest.wValue > DevDescriptorPtr->NumberOfConfigurations)\r
                  return;\r
        }\r
-       #endif\r
 #endif\r
        \r
        Endpoint_ClearSETUP();\r
 #endif\r
        \r
        Endpoint_ClearSETUP();\r
@@ -233,17 +231,20 @@ static void USB_Device_GetInternalSerialDescriptor(void)
        \r
        uint8_t SigReadAddress = 0x0E;\r
 \r
        \r
        uint8_t SigReadAddress = 0x0E;\r
 \r
-       for (uint8_t SerialCharNum = 0; SerialCharNum < 20; SerialCharNum++)\r
+       ATOMIC_BLOCK(ATOMIC_RESTORESTATE)\r
        {\r
        {\r
-               uint8_t SerialByte = boot_signature_byte_get(SigReadAddress);\r
-               \r
-               if (SerialCharNum & 0x01)\r
+               for (uint8_t SerialCharNum = 0; SerialCharNum < 20; SerialCharNum++)\r
                {\r
                {\r
-                       SerialByte >>= 4;\r
-                       SigReadAddress++;\r
+                       uint8_t SerialByte = boot_signature_byte_get(SigReadAddress);\r
+                       \r
+                       if (SerialCharNum & 0x01)\r
+                       {\r
+                               SerialByte >>= 4;\r
+                               SigReadAddress++;\r
+                       }\r
+                       \r
+                       SignatureDescriptor.UnicodeString[SerialCharNum] = USB_Device_NibbleToASCII(SerialByte);\r
                }\r
                }\r
-               \r
-               SignatureDescriptor.UnicodeString[SerialCharNum] = USB_Device_NibbleToASCII(SerialByte);\r
        }\r
        \r
        Endpoint_ClearSETUP();\r
        }\r
        \r
        Endpoint_ClearSETUP();\r
@@ -322,7 +323,7 @@ static void USB_Device_GetStatus(void)
 #endif\r
 #if !defined(CONTROL_ONLY_DEVICE)\r
                case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT):\r
 #endif\r
 #if !defined(CONTROL_ONLY_DEVICE)\r
                case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT):\r
-                       Endpoint_SelectEndpoint(USB_ControlRequest.wIndex & 0xFF);\r
+                       Endpoint_SelectEndpoint((uint8_t)USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);\r
 \r
                        CurrentStatus = Endpoint_IsStalled();\r
 \r
 \r
                        CurrentStatus = Endpoint_IsStalled();\r
 \r
@@ -343,7 +344,7 @@ static void USB_Device_GetStatus(void)
 }\r
 \r
 static void USB_Device_ClearSetFeature(void)\r
 }\r
 \r
 static void USB_Device_ClearSetFeature(void)\r
-{      \r
+{\r
        switch (USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT)\r
        {\r
 #if !defined(NO_DEVICE_REMOTE_WAKEUP)                  \r
        switch (USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT)\r
        {\r
 #if !defined(NO_DEVICE_REMOTE_WAKEUP)                  \r
@@ -366,18 +367,18 @@ static void USB_Device_ClearSetFeature(void)
 \r
                                Endpoint_SelectEndpoint(EndpointIndex);\r
 \r
 \r
                                Endpoint_SelectEndpoint(EndpointIndex);\r
 \r
-                               if (Endpoint_IsEnabled())\r
-                               {                               \r
-                                       if (USB_ControlRequest.bRequest == REQ_SetFeature)\r
-                                       {\r
-                                               Endpoint_StallTransaction();\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               Endpoint_ClearStall();\r
-                                               Endpoint_ResetFIFO(EndpointIndex);\r
-                                               Endpoint_ResetDataToggle();\r
-                                       }                                       \r
+                               if (!(Endpoint_IsEnabled()))\r
+                                 return;\r
+\r
+                               if (USB_ControlRequest.bRequest == REQ_SetFeature)\r
+                               {\r
+                                       Endpoint_StallTransaction();\r
+                               }\r
+                               else\r
+                               {\r
+                                       Endpoint_ClearStall();\r
+                                       Endpoint_ResetFIFO(EndpointIndex);\r
+                                       Endpoint_ResetDataToggle();\r
                                }\r
                        }\r
                        \r
                                }\r
                        }\r
                        \r