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_AVR8.c 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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_AVR8)
  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. void USB_Init(
  38. #if defined(USB_CAN_BE_BOTH)
  39. const uint8_t Mode
  40. #endif
  41. #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))
  42. ,
  43. #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))
  44. void
  45. #endif
  46. #if !defined(USE_STATIC_OPTIONS)
  47. const uint8_t Options
  48. #endif
  49. )
  50. {
  51. #if !defined(USE_STATIC_OPTIONS)
  52. USB_Options = Options;
  53. #endif
  54. #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
  55. /* Workaround for AVR8 bootloaders that fail to turn off the OTG pad before running
  56. * the loaded application. This causes VBUS detection to fail unless we first force
  57. * it off to reset it. */
  58. USB_OTGPAD_Off();
  59. #endif
  60. if (!(USB_Options & USB_OPT_REG_DISABLED))
  61. USB_REG_On();
  62. else
  63. USB_REG_Off();
  64. if (!(USB_Options & USB_OPT_MANUAL_PLL))
  65. {
  66. #if defined(USB_SERIES_4_AVR)
  67. PLLFRQ = (1 << PDIV2);
  68. #endif
  69. }
  70. #if defined(USB_CAN_BE_BOTH)
  71. if (Mode == USB_MODE_UID)
  72. {
  73. UHWCON |= (1 << UIDE);
  74. USB_INT_Enable(USB_INT_IDTI);
  75. USB_CurrentMode = USB_GetUSBModeFromUID();
  76. }
  77. else
  78. {
  79. UHWCON &= ~(1 << UIDE);
  80. USB_CurrentMode = Mode;
  81. }
  82. #endif
  83. USB_IsInitialized = true;
  84. USB_ResetInterface();
  85. }
  86. void USB_Disable(void)
  87. {
  88. USB_INT_DisableAllInterrupts();
  89. USB_INT_ClearAllInterrupts();
  90. USB_Detach();
  91. USB_Controller_Disable();
  92. if (!(USB_Options & USB_OPT_MANUAL_PLL))
  93. USB_PLL_Off();
  94. if (!(USB_Options & USB_OPT_REG_KEEP_ENABLED))
  95. USB_REG_Off();
  96. #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
  97. USB_OTGPAD_Off();
  98. #endif
  99. #if defined(USB_CAN_BE_BOTH)
  100. USB_CurrentMode = USB_MODE_None;
  101. #endif
  102. USB_IsInitialized = false;
  103. }
  104. void USB_ResetInterface(void)
  105. {
  106. #if defined(USB_CAN_BE_BOTH)
  107. bool UIDModeSelectEnabled = ((UHWCON & (1 << UIDE)) != 0);
  108. #endif
  109. USB_INT_DisableAllInterrupts();
  110. USB_INT_ClearAllInterrupts();
  111. USB_Controller_Reset();
  112. #if defined(USB_CAN_BE_BOTH)
  113. if (UIDModeSelectEnabled)
  114. USB_INT_Enable(USB_INT_IDTI);
  115. #endif
  116. USB_CLK_Unfreeze();
  117. if (USB_CurrentMode == USB_MODE_Device)
  118. {
  119. #if defined(USB_CAN_BE_DEVICE)
  120. #if (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
  121. UHWCON |= (1 << UIMOD);
  122. #endif
  123. if (!(USB_Options & USB_OPT_MANUAL_PLL))
  124. {
  125. #if defined(USB_SERIES_2_AVR)
  126. USB_PLL_On();
  127. while (!(USB_PLL_IsReady()));
  128. #else
  129. USB_PLL_Off();
  130. #endif
  131. }
  132. USB_Init_Device();
  133. #endif
  134. }
  135. else if (USB_CurrentMode == USB_MODE_Host)
  136. {
  137. #if defined(USB_CAN_BE_HOST)
  138. UHWCON &= ~(1 << UIMOD);
  139. if (!(USB_Options & USB_OPT_MANUAL_PLL))
  140. {
  141. #if defined(USB_CAN_BE_HOST)
  142. USB_PLL_On();
  143. while (!(USB_PLL_IsReady()));
  144. #endif
  145. }
  146. USB_Init_Host();
  147. #endif
  148. }
  149. #if (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
  150. USB_OTGPAD_On();
  151. #endif
  152. }
  153. #if defined(USB_CAN_BE_DEVICE)
  154. static void USB_Init_Device(void)
  155. {
  156. USB_DeviceState = DEVICE_STATE_Unattached;
  157. USB_Device_ConfigurationNumber = 0;
  158. #if !defined(NO_DEVICE_REMOTE_WAKEUP)
  159. USB_Device_RemoteWakeupEnabled = false;
  160. #endif
  161. #if !defined(NO_DEVICE_SELF_POWER)
  162. USB_Device_CurrentlySelfPowered = false;
  163. #endif
  164. #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
  165. USB_Descriptor_Device_t* DeviceDescriptorPtr;
  166. #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
  167. !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
  168. uint8_t DescriptorAddressSpace;
  169. if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr, &DescriptorAddressSpace) != NO_DESCRIPTOR)
  170. {
  171. if (DescriptorAddressSpace == MEMSPACE_FLASH)
  172. USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
  173. else if (DescriptorAddressSpace == MEMSPACE_EEPROM)
  174. USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
  175. else
  176. USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
  177. }
  178. #else
  179. if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)
  180. {
  181. #if defined(USE_RAM_DESCRIPTORS)
  182. USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
  183. #elif defined(USE_EEPROM_DESCRIPTORS)
  184. USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
  185. #else
  186. USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
  187. #endif
  188. }
  189. #endif
  190. #endif
  191. #if (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
  192. if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
  193. USB_Device_SetLowSpeed();
  194. else
  195. USB_Device_SetFullSpeed();
  196. USB_INT_Enable(USB_INT_VBUSTI);
  197. #endif
  198. Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP, EP_TYPE_CONTROL,
  199. USB_Device_ControlEndpointSize, 1);
  200. USB_INT_Clear(USB_INT_SUSPI);
  201. USB_INT_Enable(USB_INT_SUSPI);
  202. USB_INT_Enable(USB_INT_EORSTI);
  203. USB_Attach();
  204. }
  205. #endif
  206. #if defined(USB_CAN_BE_HOST)
  207. static void USB_Init_Host(void)
  208. {
  209. USB_HostState = HOST_STATE_Unattached;
  210. USB_Host_ConfigurationNumber = 0;
  211. USB_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
  212. USB_Host_HostMode_On();
  213. USB_Host_VBUS_Auto_Off();
  214. USB_Host_VBUS_Manual_Enable();
  215. USB_Host_VBUS_Manual_On();
  216. USB_INT_Enable(USB_INT_SRPI);
  217. USB_INT_Enable(USB_INT_BCERRI);
  218. USB_Attach();
  219. }
  220. #endif
  221. #endif