usbh_hcs.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. \file usbh_hcs.c
  3. \brief this file implements functions for opening and closing host channels
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #include "usbh_hcs.h"
  10. static uint16_t usbh_freechannel_get (usb_core_handle_struct *pudev);
  11. /*!
  12. \brief open a channel
  13. \param[in] pudev: pointer to usb device
  14. \param[in] channel_num: host channel number which is in (0..7)
  15. \param[in] dev_addr: USB device address allocated to attached device
  16. \param[in] dev_speed: USB device speed (Full speed/Low speed)
  17. \param[in] ep_type: endpoint type (bulk/int/ctl)
  18. \param[in] ep_mps: max packet size
  19. \param[out] none
  20. \retval operation status
  21. */
  22. uint8_t usbh_channel_open (usb_core_handle_struct *pudev,
  23. uint8_t channel_num,
  24. uint8_t dev_addr,
  25. uint8_t dev_speed,
  26. uint8_t ep_type,
  27. uint16_t ep_mps)
  28. {
  29. usb_hostchannel_struct *puhc = &pudev->host.host_channel[channel_num];
  30. uint16_t channel_info = puhc->info;
  31. puhc->endp_id = (uint8_t)channel_info & 0x7FU;
  32. puhc->endp_in = (uint8_t)(channel_info & 0x80U) >> 7;
  33. puhc->endp_type = ep_type;
  34. puhc->endp_mps = ep_mps;
  35. puhc->dev_addr = dev_addr;
  36. puhc->dev_speed = dev_speed;
  37. puhc->data_tg_in = 0U;
  38. puhc->data_tg_out = 0U;
  39. if (HPRT_PRTSPD_HIGH_SPEED == dev_speed) {
  40. puhc->do_ping = 1U;
  41. }
  42. usb_hostchannel_init(pudev, channel_num);
  43. return (uint8_t)HC_OK;
  44. }
  45. /*!
  46. \brief modify a channel
  47. \param[in] pudev: pointer to usb device
  48. \param[in] channel_num: host channel number which is in (0..7)
  49. \param[in] dev_addr: USB Device address allocated to attached device
  50. \param[in] dev_speed: USB device speed (Full speed/Low speed)
  51. \param[in] ep_type: endpoint type (bulk/int/ctl)
  52. \param[in] ep_mps: max packet size
  53. \param[out] none
  54. \retval operation status
  55. */
  56. uint8_t usbh_channel_modify (usb_core_handle_struct *pudev,
  57. uint8_t channel_num,
  58. uint8_t dev_addr,
  59. uint8_t dev_speed,
  60. uint8_t ep_type,
  61. uint16_t ep_mps)
  62. {
  63. usb_hostchannel_struct *puhc = &pudev->host.host_channel[channel_num];
  64. if (0U != dev_addr) {
  65. puhc->dev_addr = dev_addr;
  66. }
  67. if ((puhc->endp_mps != ep_mps) && (0U != ep_mps)) {
  68. puhc->endp_mps = ep_mps;
  69. }
  70. if ((puhc->dev_speed != dev_speed) && (0U != dev_speed)) {
  71. puhc->dev_speed = dev_speed;
  72. }
  73. usb_hostchannel_init(pudev, channel_num);
  74. return (uint8_t)HC_OK;
  75. }
  76. /*!
  77. \brief allocate a new channel for the pipe
  78. \param[in] pudev: pointer to usb device
  79. \param[in] ep_addr: endpoint for which the channel to be allocated
  80. \param[out] none
  81. \retval host channel number
  82. */
  83. uint8_t usbh_channel_alloc (usb_core_handle_struct *pudev, uint8_t ep_addr)
  84. {
  85. uint16_t hc_num = usbh_freechannel_get(pudev);
  86. if ((uint16_t)HC_ERROR != hc_num) {
  87. pudev->host.host_channel[hc_num].info = HC_USED | ep_addr;
  88. }
  89. return (uint8_t)hc_num;
  90. }
  91. /*!
  92. \brief free the usb host channel
  93. \param[in] pudev: pointer to usb device
  94. \param[in] index: channel number to be freed which is in (0..7)
  95. \param[out] none
  96. \retval host operation status
  97. */
  98. uint8_t usbh_channel_free (usb_core_handle_struct *pudev, uint8_t index)
  99. {
  100. if (index < HC_MAX) {
  101. pudev->host.host_channel[index].info &= HC_USED_MASK;
  102. }
  103. return USBH_OK;
  104. }
  105. /*!
  106. \brief free all usb host channel
  107. \param[in] pudev: pointer to usb device
  108. \param[out] none
  109. \retval host operation status
  110. */
  111. uint8_t usbh_allchannel_dealloc (usb_core_handle_struct *pudev)
  112. {
  113. uint8_t index;
  114. for (index = 2U; index < HC_MAX; index ++) {
  115. pudev->host.host_channel[index].info = 0U;
  116. }
  117. return USBH_OK;
  118. }
  119. /*!
  120. \brief get a free channel number for allocation to a device endpoint
  121. \param[in] pudev: pointer to usb device
  122. \param[out] none
  123. \retval free channel number
  124. */
  125. static uint16_t usbh_freechannel_get (usb_core_handle_struct *pudev)
  126. {
  127. uint8_t index = 0U;
  128. for (index = 0U; index < HC_MAX; index++) {
  129. if (0U == (pudev->host.host_channel[index].info & HC_USED)) {
  130. return (uint16_t)index;
  131. }
  132. }
  133. return HC_ERROR;
  134. }