usb_phy.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "soc/soc_caps.h"
  10. #include "hal/usb_phy_types.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * @brief Initialization for usb_phy_otg_io_conf_t: Self-powered device
  16. */
  17. #define USB_PHY_SELF_POWERED_DEVICE(vbus_monitor_io) \
  18. { \
  19. .iddig_io_num = -1, \
  20. .avalid_io_num = -1, \
  21. .vbusvalid_io_num = -1, \
  22. .idpullup_io_num = -1, \
  23. .dppulldown_io_num = -1, \
  24. .dmpulldown_io_num = -1, \
  25. .drvvbus_io_num = -1, \
  26. .bvalid_io_num = vbus_monitor_io, \
  27. .sessend_io_num = -1, \
  28. .chrgvbus_io_num = -1, \
  29. .dischrgvbus_io_num = -1, \
  30. };
  31. /**
  32. * @brief USB PHY status
  33. */
  34. typedef enum {
  35. USB_PHY_STATUS_FREE, /**< PHY is not being used */
  36. USB_PHY_STATUS_IN_USE, /**< PHY is in use */
  37. } usb_phy_status_t;
  38. /**
  39. * @brief USB PHY available actions
  40. */
  41. typedef enum {
  42. USB_PHY_ACTION_HOST_ALLOW_CONN, /**< Enable physical connection when operating as an OTG Host */
  43. USB_PHY_ACTION_HOST_FORCE_DISCONN, /**< Disable physical connection when operating as an OTG Host */
  44. USB_PHY_ACTION_MAX,
  45. } usb_phy_action_t;
  46. /**
  47. * @brief USB external PHY IO pins configuration structure
  48. */
  49. typedef struct {
  50. int vp_io_num; /**< GPIO pin to USB_EXTPHY_VP_IDX */
  51. int vm_io_num; /**< GPIO pin to USB_EXTPHY_VM_IDX */
  52. int rcv_io_num; /**< GPIO pin to USB_EXTPHY_RCV_IDX */
  53. int oen_io_num; /**< GPIO pin to USB_EXTPHY_OEN_IDX */
  54. int vpo_io_num; /**< GPIO pin to USB_EXTPHY_VPO_IDX */
  55. int vmo_io_num; /**< GPIO pin to USB_EXTPHY_VMO_IDX */
  56. } usb_phy_ext_io_conf_t;
  57. /**
  58. * @brief USB OTG IO pins configuration structure
  59. */
  60. typedef struct {
  61. int iddig_io_num; /**< GPIO pin to USB_OTG_IDDIG_IN_IDX */
  62. int avalid_io_num; /**< GPIO pin to USB_OTG_AVALID_IN_IDX */
  63. int vbusvalid_io_num; /**< GPIO pin to USB_OTG_VBUSVALID_IN_IDX */
  64. int idpullup_io_num; /**< GPIO pin to USB_OTG_IDPULLUP_IDX */
  65. int dppulldown_io_num; /**< GPIO pin to USB_OTG_DPPULLDOWN_IDX */
  66. int dmpulldown_io_num; /**< GPIO pin to USB_OTG_DMPULLDOWN_IDX */
  67. int drvvbus_io_num; /**< GPIO pin to USB_OTG_DRVVBUS_IDX */
  68. int bvalid_io_num; /**< GPIO pin to USB_SRP_BVALID_IN_IDX */
  69. int sessend_io_num; /**< GPIO pin to USB_SRP_SESSEND_IN_IDX */
  70. int chrgvbus_io_num; /**< GPIO pin to USB_SRP_CHRGVBUS_IDX */
  71. int dischrgvbus_io_num; /**< GPIO pin to USB_SRP_DISCHRGVBUS_IDX */
  72. } usb_phy_otg_io_conf_t;
  73. /**
  74. * @brief USB PHY configure struct
  75. *
  76. * At minimum the PHY controller and PHY target must be initialized.
  77. */
  78. typedef struct {
  79. usb_phy_controller_t controller; /**< USB PHY controller */
  80. usb_phy_target_t target; /**< USB PHY target INT/EXT */
  81. usb_otg_mode_t otg_mode; /**< USB OTG mode */
  82. usb_phy_speed_t otg_speed; /**< USB OTG speed */
  83. const usb_phy_ext_io_conf_t *ext_io_conf; /**< USB external PHY IO pins configuration */
  84. const usb_phy_otg_io_conf_t *otg_io_conf; /**< USB OTG IO pins configuration */
  85. } usb_phy_config_t;
  86. typedef struct phy_context_t *usb_phy_handle_t; /**< USB PHY context handle */
  87. /**
  88. * @brief Initialize a new USB PHY
  89. * Configure at least PHY source.
  90. *
  91. * This function will enable the OTG Controller
  92. *
  93. * @param[in] config USB PHY configurtion struct
  94. * @param[out] handle_ret USB PHY context handle
  95. *
  96. * @return
  97. * - ESP_OK Success
  98. * - ESP_FAIL USB PHY init error.
  99. * - ESP_ERR_INVALID_STATE USB PHY not installed.
  100. * - ESP_ERR_NO_MEM USB_OTG installation failed due to no mem.
  101. */
  102. esp_err_t usb_new_phy(const usb_phy_config_t *config, usb_phy_handle_t *handle_ret);
  103. /**
  104. * @brief Configure OTG mode for a USB PHY
  105. *
  106. * @param handle Pointer of USB PHY context handle
  107. * @param mode USB OTG mode
  108. *
  109. * @return
  110. * - ESP_OK Success
  111. * - ESP_ERR_INVALID_ARG Parameter error.
  112. * - ESP_FAIL OTG set mode fail.
  113. */
  114. esp_err_t usb_phy_otg_set_mode(usb_phy_handle_t handle, usb_otg_mode_t mode);
  115. /**
  116. * @brief Configure USB speed for a USB PHY that is operating as an OTG Device
  117. *
  118. * @param handle Pointer of USB PHY context handle
  119. * @param mode USB speed
  120. *
  121. * @return
  122. * - ESP_OK Success
  123. * - ESP_ERR_INVALID_ARG Parameter error.
  124. * - ESP_FAIL OTG set speed fail.
  125. */
  126. esp_err_t usb_phy_otg_dev_set_speed(usb_phy_handle_t handle, usb_phy_speed_t speed);
  127. /**
  128. * @brief Take a action for a USB PHY
  129. *
  130. * @param handle Pointer of USB PHY context handle
  131. * @param action USB PHY action
  132. *
  133. * @return
  134. * - ESP_OK Success
  135. * - ESP_ERR_INVALID_ARG Parameter error.
  136. * - ESP_FAIL Action cannot be performed.
  137. */
  138. esp_err_t usb_phy_action(usb_phy_handle_t handle, usb_phy_action_t action);
  139. /**
  140. * @brief Delete a USB PHY
  141. *
  142. * @param handle Pointer of USB PHY context handle
  143. *
  144. * @return
  145. * - ESP_OK Success
  146. * - ESP_ERR_INVALID_ARG Parameter error.
  147. */
  148. esp_err_t usb_del_phy(usb_phy_handle_t handle);
  149. /**
  150. * @brief Get status of a USB PHY
  151. *
  152. * @param[in] target The specific PHY target to check
  153. * @param[out] status Status of the PHY
  154. *
  155. * @return
  156. * - ESP_OK Success
  157. * - ESP_ERR_INVALID_ARG Parameter error.
  158. * - ESP_ERR_INVALID_STATE USB PHY not installed.
  159. */
  160. esp_err_t usb_phy_get_phy_status(usb_phy_target_t target, usb_phy_status_t *status);
  161. #ifdef __cplusplus
  162. }
  163. #endif