Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

USBController_XMEGA.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #include "../../../../Common/Common.h"
  27. #if (ARCH == ARCH_XMEGA)
  28. #define __INCLUDE_FROM_USB_DRIVER
  29. #define __INCLUDE_FROM_USB_CONTROLLER_C
  30. #include "../USBController.h"
  31. #if defined(USB_CAN_BE_BOTH)
  32. volatile uint8_t USB_CurrentMode = USB_MODE_None;
  33. #endif
  34. #if !defined(USE_STATIC_OPTIONS)
  35. volatile uint8_t USB_Options;
  36. #endif
  37. /* Ugly workaround to ensure an aligned table, since __BIGGEST_ALIGNMENT__ == 1 for the 8-bit AVR-GCC toolchain */
  38. uint8_t USB_EndpointTable[sizeof(USB_EndpointTable_t) + 1];
  39. void USB_Init(
  40. #if defined(USB_CAN_BE_BOTH)
  41. const uint8_t Mode
  42. #endif
  43. #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))
  44. ,
  45. #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))
  46. void
  47. #endif
  48. #if !defined(USE_STATIC_OPTIONS)
  49. const uint8_t Options
  50. #endif
  51. )
  52. {
  53. #if !defined(USE_STATIC_OPTIONS)
  54. USB_Options = Options;
  55. #endif
  56. uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
  57. GlobalInterruptDisable();
  58. NVM.CMD = NVM_CMD_READ_CALIB_ROW_gc;
  59. USB.CAL0 = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBCAL0));
  60. USB.CAL1 = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBCAL1));
  61. NVM.CMD = NVM_CMD_NO_OPERATION_gc;
  62. /* Ugly workaround to ensure an aligned table, since __BIGGEST_ALIGNMENT__ == 1 for the 8-bit AVR-GCC toolchain */
  63. USB.EPPTR = ((intptr_t)&USB_EndpointTable[1] & ~(1 << 0));
  64. USB.CTRLA = (USB_STFRNUM_bm | ((ENDPOINT_TOTAL_ENDPOINTS - 1) << USB_MAXEP_gp));
  65. if ((USB_Options & USB_OPT_BUSEVENT_PRIHIGH) == USB_OPT_BUSEVENT_PRIHIGH)
  66. USB.INTCTRLA = (3 << USB_INTLVL_gp);
  67. else if ((USB_Options & USB_OPT_BUSEVENT_PRIMED) == USB_OPT_BUSEVENT_PRIMED)
  68. USB.INTCTRLA = (2 << USB_INTLVL_gp);
  69. else
  70. USB.INTCTRLA = (1 << USB_INTLVL_gp);
  71. SetGlobalInterruptMask(CurrentGlobalInt);
  72. #if defined(USB_CAN_BE_BOTH)
  73. USB_CurrentMode = Mode;
  74. #endif
  75. USB_IsInitialized = true;
  76. USB_ResetInterface();
  77. }
  78. void USB_Disable(void)
  79. {
  80. USB_INT_DisableAllInterrupts();
  81. USB_INT_ClearAllInterrupts();
  82. USB_Detach();
  83. USB_Controller_Disable();
  84. USB_IsInitialized = false;
  85. }
  86. void USB_ResetInterface(void)
  87. {
  88. uint8_t PrescalerNeeded;
  89. #if defined(USB_DEVICE_OPT_FULLSPEED)
  90. if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
  91. PrescalerNeeded = F_USB / 6000000;
  92. else
  93. PrescalerNeeded = F_USB / 48000000;
  94. #else
  95. PrescalerNeeded = F_USB / 6000000;
  96. #endif
  97. uint8_t DividerIndex = 0;
  98. while (PrescalerNeeded > 0)
  99. {
  100. DividerIndex++;
  101. PrescalerNeeded >>= 1;
  102. }
  103. CLK.USBCTRL = (DividerIndex - 1) << CLK_USBPSDIV_gp;
  104. if (USB_Options & USB_OPT_PLLCLKSRC)
  105. CLK.USBCTRL |= (CLK_USBSRC_PLL_gc | CLK_USBSEN_bm);
  106. else
  107. CLK.USBCTRL |= (CLK_USBSRC_RC32M_gc | CLK_USBSEN_bm);
  108. USB_Device_SetDeviceAddress(0);
  109. USB_INT_DisableAllInterrupts();
  110. USB_INT_ClearAllInterrupts();
  111. USB_Controller_Reset();
  112. USB_Init_Device();
  113. }
  114. #if defined(USB_CAN_BE_DEVICE)
  115. static void USB_Init_Device(void)
  116. {
  117. USB_DeviceState = DEVICE_STATE_Unattached;
  118. USB_Device_ConfigurationNumber = 0;
  119. #if !defined(NO_DEVICE_REMOTE_WAKEUP)
  120. USB_Device_RemoteWakeupEnabled = false;
  121. #endif
  122. #if !defined(NO_DEVICE_SELF_POWER)
  123. USB_Device_CurrentlySelfPowered = false;
  124. #endif
  125. #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
  126. USB_Descriptor_Device_t* DeviceDescriptorPtr;
  127. #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
  128. !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
  129. uint8_t DescriptorAddressSpace;
  130. if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr, &DescriptorAddressSpace) != NO_DESCRIPTOR)
  131. {
  132. if (DescriptorAddressSpace == MEMSPACE_FLASH)
  133. USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
  134. else if (DescriptorAddressSpace == MEMSPACE_EEPROM)
  135. USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
  136. else
  137. USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
  138. }
  139. #else
  140. if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)
  141. {
  142. #if defined(USE_RAM_DESCRIPTORS)
  143. USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
  144. #elif defined(USE_EEPROM_DESCRIPTORS)
  145. USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
  146. #else
  147. USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
  148. #endif
  149. }
  150. #endif
  151. #endif
  152. if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
  153. USB_Device_SetLowSpeed();
  154. else
  155. USB_Device_SetFullSpeed();
  156. Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP, EP_TYPE_CONTROL,
  157. USB_Device_ControlEndpointSize, 1);
  158. USB_INT_Enable(USB_INT_BUSEVENTI);
  159. USB_Attach();
  160. }
  161. #endif
  162. #endif