USBH0_LPC18xx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* --------------------------------------------------------------------------
  2. * Copyright (c) 2013-2016 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * $Date: 02. March 2016
  19. * $Revision: V2.4
  20. *
  21. * Driver: Driver_USBH0_HCI
  22. * Configured: via RTE_Device.h configuration file
  23. * Project: USB Host 0 HCI Controller (EHCI) Driver for NXP LPC18xx
  24. * --------------------------------------------------------------------------
  25. * Use the following configuration settings in the middleware component
  26. * to connect to this driver.
  27. *
  28. * Configuration Setting Value
  29. * --------------------- -----
  30. * Connect to hardware via Driver_USBH# = 0
  31. * USB Host controller interface = EHCI
  32. * -------------------------------------------------------------------------- */
  33. /* History:
  34. * Version 2.4
  35. * Corrected PowerControl function for conditional Power full (driver must be initialized)
  36. * Version 2.3
  37. * PowerControl for Power OFF and Uninitialize functions made unconditional
  38. * Version 2.2
  39. * Updated in accordance with USB Device Driver
  40. * Version 2.1
  41. * Moved register initialization and uninitialization to PowerControl
  42. * function and removed from Initialize/Uninitialize functions
  43. * Pin configuration moved to USB_LPC18xx_USB0.c
  44. * Version 2.0
  45. * Initial release for USB Host EHCI Driver API v2.0
  46. * Version 1.0
  47. * Initial release
  48. */
  49. #include "Driver_USBH.h"
  50. #include "LPC18xx.h"
  51. #include "USB_LPC18xx.h"
  52. #include "RTE_Device.h"
  53. #include "RTE_Components.h"
  54. #if (RTE_USB_USB0 == 0)
  55. #error "USB0 is not enabled in the RTE_Device.h!"
  56. #endif
  57. extern uint8_t USB0_role;
  58. extern uint8_t USB0_state;
  59. extern void USB0_PinsConfigure (void);
  60. extern void USB0_PinsUnconfigure (void);
  61. // USBH EHCI Driver ************************************************************
  62. #define ARM_USBH_EHCI_DRIVER_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2,4)
  63. // Driver Version
  64. static const ARM_DRIVER_VERSION usbh_ehci_driver_version = { ARM_USBH_API_VERSION, ARM_USBH_EHCI_DRIVER_VERSION };
  65. // Driver Capabilities
  66. static const ARM_USBH_HCI_CAPABILITIES usbh_ehci_driver_capabilities = {
  67. 0x0001U // Root HUB available Ports Mask
  68. };
  69. static ARM_USBH_HCI_Interrupt_t EHCI_IRQ;
  70. // USBH EHCI Driver functions
  71. /**
  72. \fn ARM_DRIVER_VERSION USBH_HCI_GetVersion (void)
  73. \brief Get USB Host HCI (OHCI/EHCI) driver version.
  74. \return \ref ARM_DRIVER_VERSION
  75. */
  76. static ARM_DRIVER_VERSION USBH_HCI_GetVersion (void) { return usbh_ehci_driver_version; }
  77. /**
  78. \fn ARM_USBH_HCI_CAPABILITIES USBH_HCI_GetCapabilities (void)
  79. \brief Get driver capabilities.
  80. \return \ref ARM_USBH_HCI_CAPABILITIES
  81. */
  82. static ARM_USBH_HCI_CAPABILITIES USBH_HCI_GetCapabilities (void) { return usbh_ehci_driver_capabilities; }
  83. /**
  84. \fn int32_t USBH_HCI_Initialize (ARM_USBH_HCI_Interrupt_t *cb_interrupt)
  85. \brief Initialize USB Host HCI (OHCI/EHCI) Interface.
  86. \param[in] cb_interrupt Pointer to Interrupt Handler Routine
  87. \return \ref execution_status
  88. */
  89. static int32_t USBH_HCI_Initialize (ARM_USBH_HCI_Interrupt_t cb_interrupt) {
  90. if ((USB0_state & USBH_DRIVER_INITIALIZED) != 0U) { return ARM_DRIVER_OK; }
  91. EHCI_IRQ = cb_interrupt;
  92. USB0_role = ARM_USB_ROLE_HOST;
  93. USB0_PinsConfigure ();
  94. USB0_state = USBH_DRIVER_INITIALIZED;
  95. return ARM_DRIVER_OK;
  96. }
  97. /**
  98. \fn int32_t USBH_HCI_Uninitialize (void)
  99. \brief De-initialize USB Host HCI (OHCI/EHCI) Interface.
  100. \return \ref execution_status
  101. */
  102. static int32_t USBH_HCI_Uninitialize (void) {
  103. USB0_PinsUnconfigure ();
  104. USB0_role = ARM_USB_ROLE_NONE;
  105. USB0_state &= ~USBH_DRIVER_INITIALIZED;
  106. return ARM_DRIVER_OK;
  107. }
  108. /**
  109. \fn int32_t USBH_HCI_PowerControl (ARM_POWER_STATE state)
  110. \brief Control USB Host HCI (OHCI/EHCI) Interface Power.
  111. \param[in] state Power state
  112. \return \ref execution_status
  113. */
  114. static int32_t USBH_HCI_PowerControl (ARM_POWER_STATE state) {
  115. switch (state) {
  116. case ARM_POWER_OFF:
  117. NVIC_DisableIRQ (USB0_IRQn); // Disable interrupt
  118. NVIC_ClearPendingIRQ (USB0_IRQn); // Clear pending interrupt
  119. USB0_state &= ~USBH_DRIVER_POWERED; // Clear powered flag
  120. if ((LPC_CGU->BASE_USB0_CLK & 1U) == 0U) {
  121. LPC_CREG->CREG0 |= (1U << 5); // Disable USB0 PHY
  122. LPC_CCU1->CLK_USB0_CFG &= ~1U; // Disable USB0 Base Clock
  123. while (LPC_CCU1->CLK_USB0_STAT & 1U);
  124. LPC_CCU1->CLK_M3_USB0_CFG &= ~1U; // Disable USB0 Register Interface Clock
  125. while (LPC_CCU1->CLK_M3_USB0_STAT & 1U);
  126. LPC_CGU->BASE_USB0_CLK = 1U; // Disable Base Clock
  127. }
  128. break;
  129. case ARM_POWER_FULL:
  130. if ((USB0_state & USBH_DRIVER_INITIALIZED) == 0U) { return ARM_DRIVER_ERROR; }
  131. if ((USB0_state & USBH_DRIVER_POWERED) != 0U) { return ARM_DRIVER_OK; }
  132. LPC_CGU->BASE_USB0_CLK = (0x01U << 11) | // Auto-block Enable
  133. (0x07U << 24) ; // Clock source: PLL0USB
  134. LPC_CCU1->CLK_M3_USB0_CFG |= 1U; // Enable USB0 Register Interface Clock
  135. while (!(LPC_CCU1->CLK_M3_USB0_STAT & 1U));
  136. LPC_CCU1->CLK_USB0_CFG |= 1U; // Enable USB0 Base Clock
  137. while (!(LPC_CCU1->CLK_USB0_STAT & 1U));
  138. LPC_CREG->CREG0 &= ~(1U << 5); // Enable USB0 PHY
  139. USB0_state |= USBH_DRIVER_POWERED; // Set powered flag
  140. NVIC_EnableIRQ (USB0_IRQn); // Enable interrupt
  141. break;
  142. default:
  143. return ARM_DRIVER_ERROR_UNSUPPORTED;
  144. }
  145. return ARM_DRIVER_OK;
  146. }
  147. /**
  148. \fn int32_t USBH_HCI_PortVbusOnOff (uint8_t port, bool vbus)
  149. \brief USB Host HCI (OHCI/EHCI) Root HUB Port VBUS on/off.
  150. \param[in] port Root HUB Port Number
  151. \param[in] vbus
  152. - \b false VBUS off
  153. - \b true VBUS on
  154. \return \ref execution_status
  155. */
  156. static int32_t USBH_HCI_PortVbusOnOff (uint8_t port, bool power) {
  157. // No GPIO pins used for VBUS control it is controlled by EHCI Controller
  158. if (((1U << port) & usbh_ehci_driver_capabilities.port_mask) == 0U) { return ARM_DRIVER_ERROR; }
  159. return ARM_DRIVER_OK;
  160. }
  161. /**
  162. \fn void USBH0_IRQ (void)
  163. \brief USB0 Host Interrupt Routine (IRQ).
  164. */
  165. void USBH0_IRQ (void) {
  166. EHCI_IRQ();
  167. }
  168. ARM_DRIVER_USBH_HCI Driver_USBH0_HCI = {
  169. USBH_HCI_GetVersion,
  170. USBH_HCI_GetCapabilities,
  171. USBH_HCI_Initialize,
  172. USBH_HCI_Uninitialize,
  173. USBH_HCI_PowerControl,
  174. USBH_HCI_PortVbusOnOff
  175. };