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_UC3.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_UC3)
  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_CAN_BE_BOTH)
  55. if (Mode == USB_MODE_UID)
  56. {
  57. AVR32_USBB.USBCON.uide = true;
  58. USB_INT_Enable(USB_INT_IDTI);
  59. USB_CurrentMode = USB_GetUSBModeFromUID();
  60. }
  61. else
  62. {
  63. AVR32_USBB.USBCON.uide = false;
  64. USB_CurrentMode = Mode;
  65. }
  66. #else
  67. AVR32_USBB.USBCON.uide = false;
  68. #endif
  69. USB_IsInitialized = true;
  70. USB_ResetInterface();
  71. }
  72. void USB_Disable(void)
  73. {
  74. USB_INT_DisableAllInterrupts();
  75. USB_INT_ClearAllInterrupts();
  76. USB_Detach();
  77. USB_Controller_Disable();
  78. USB_OTGPAD_Off();
  79. #if defined(USB_CAN_BE_BOTH)
  80. USB_CurrentMode = USB_MODE_None;
  81. #endif
  82. AVR32_PM.GCCTRL[3].cen = false;
  83. USB_IsInitialized = false;
  84. }
  85. void USB_ResetInterface(void)
  86. {
  87. #if defined(USB_CAN_BE_BOTH)
  88. bool UIDModeSelectEnabled = AVR32_USBB.USBCON.uide;
  89. #endif
  90. AVR32_PM.GCCTRL[AVR32_PM_GCLK_USBB].pllsel = !(USB_Options & USB_OPT_GCLK_SRC_OSC);
  91. AVR32_PM.GCCTRL[AVR32_PM_GCLK_USBB].oscsel = !(USB_Options & USB_OPT_GCLK_CHANNEL_0);
  92. AVR32_PM.GCCTRL[AVR32_PM_GCLK_USBB].diven = (F_USB != USB_CLOCK_REQUIRED_FREQ);
  93. AVR32_PM.GCCTRL[AVR32_PM_GCLK_USBB].div = (F_USB == USB_CLOCK_REQUIRED_FREQ) ? 0 : (uint32_t)((F_USB / USB_CLOCK_REQUIRED_FREQ / 2) - 1);
  94. AVR32_PM.GCCTRL[AVR32_PM_GCLK_USBB].cen = true;
  95. USB_INT_DisableAllInterrupts();
  96. USB_INT_ClearAllInterrupts();
  97. USB_Controller_Reset();
  98. #if defined(USB_CAN_BE_BOTH)
  99. if (UIDModeSelectEnabled)
  100. USB_INT_Enable(USB_INT_IDTI);
  101. #endif
  102. USB_CLK_Unfreeze();
  103. if (USB_CurrentMode == USB_MODE_Device)
  104. {
  105. #if defined(USB_CAN_BE_DEVICE)
  106. AVR32_USBB.USBCON.uimod = true;
  107. USB_Init_Device();
  108. #endif
  109. }
  110. else if (USB_CurrentMode == USB_MODE_Host)
  111. {
  112. #if defined(INVERTED_VBUS_ENABLE_LINE)
  113. AVR32_USBB.USBCON.vbuspo = true;
  114. #endif
  115. #if defined(USB_CAN_BE_HOST)
  116. AVR32_USBB.USBCON.uimod = false;
  117. USB_Init_Host();
  118. #endif
  119. }
  120. USB_OTGPAD_On();
  121. }
  122. #if defined(USB_CAN_BE_DEVICE)
  123. static void USB_Init_Device(void)
  124. {
  125. USB_DeviceState = DEVICE_STATE_Unattached;
  126. USB_Device_ConfigurationNumber = 0;
  127. #if !defined(NO_DEVICE_REMOTE_WAKEUP)
  128. USB_Device_RemoteWakeupEnabled = false;
  129. #endif
  130. #if !defined(NO_DEVICE_SELF_POWER)
  131. USB_Device_CurrentlySelfPowered = false;
  132. #endif
  133. #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
  134. USB_Descriptor_Device_t* DeviceDescriptorPtr;
  135. if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)
  136. USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
  137. #endif
  138. if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
  139. {
  140. USB_Device_SetLowSpeed();
  141. }
  142. else
  143. {
  144. #if defined(USB_DEVICE_OPT_HIGHSPEED)
  145. if (USB_Options & USB_DEVICE_OPT_HIGHSPEED)
  146. USB_Device_SetHighSpeed();
  147. else
  148. USB_Device_SetFullSpeed();
  149. #else
  150. USB_Device_SetFullSpeed();
  151. #endif
  152. }
  153. USB_INT_Enable(USB_INT_VBUSTI);
  154. Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP, EP_TYPE_CONTROL,
  155. USB_Device_ControlEndpointSize, 1);
  156. USB_INT_Clear(USB_INT_SUSPI);
  157. USB_INT_Enable(USB_INT_SUSPI);
  158. USB_INT_Enable(USB_INT_EORSTI);
  159. USB_Attach();
  160. }
  161. #endif
  162. #if defined(USB_CAN_BE_HOST)
  163. static void USB_Init_Host(void)
  164. {
  165. USB_HostState = HOST_STATE_Unattached;
  166. USB_Host_ConfigurationNumber = 0;
  167. USB_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
  168. USB_Host_HostMode_On();
  169. USB_Host_VBUS_Auto_On();
  170. USB_INT_Enable(USB_INT_DCONNI);
  171. USB_INT_Enable(USB_INT_BCERRI);
  172. USB_Attach();
  173. }
  174. #endif
  175. #endif