usbh_hcs.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*!
  2. \file usbh_hcs.c
  3. \brief this file implements functions for opening and closing host channels
  4. \version 2017-06-06, V1.0.0, firmware for GD32F3x0
  5. \version 2019-06-01, V2.0.0, firmware for GD32F3x0
  6. */
  7. /*
  8. Copyright (c) 2019, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #include "usbh_hcs.h"
  31. static uint16_t usbh_freechannel_get (usb_core_handle_struct *pudev);
  32. /*!
  33. \brief open a channel
  34. \param[in] pudev: pointer to usb device
  35. \param[in] channel_num: host channel number which is in (0..7)
  36. \param[in] dev_addr: USB device address allocated to attached device
  37. \param[in] dev_speed: USB device speed (Full speed/Low speed)
  38. \param[in] ep_type: endpoint type (bulk/int/ctl)
  39. \param[in] ep_mps: max packet size
  40. \param[out] none
  41. \retval operation status
  42. */
  43. uint8_t usbh_channel_open (usb_core_handle_struct *pudev,
  44. uint8_t channel_num,
  45. uint8_t dev_addr,
  46. uint8_t dev_speed,
  47. uint8_t ep_type,
  48. uint16_t ep_mps)
  49. {
  50. usb_hostchannel_struct *puhc = &pudev->host.host_channel[channel_num];
  51. uint16_t channel_info = puhc->info;
  52. puhc->endp_id = (uint8_t)channel_info & 0x7FU;
  53. puhc->endp_in = (uint8_t)(channel_info & 0x80U) >> 7;
  54. puhc->endp_type = ep_type;
  55. puhc->endp_mps = ep_mps;
  56. puhc->dev_addr = dev_addr;
  57. puhc->dev_speed = dev_speed;
  58. puhc->data_tg_in = 0U;
  59. puhc->data_tg_out = 0U;
  60. usb_hostchannel_init(pudev, channel_num);
  61. return (uint8_t)HC_OK;
  62. }
  63. /*!
  64. \brief modify a channel
  65. \param[in] pudev: pointer to usb device
  66. \param[in] channel_num: host channel number which is in (0..7)
  67. \param[in] dev_addr: USB Device address allocated to attached device
  68. \param[in] dev_speed: USB device speed (Full speed/Low speed)
  69. \param[in] ep_type: endpoint type (bulk/int/ctl)
  70. \param[in] ep_mps: max packet size
  71. \param[out] none
  72. \retval operation status
  73. */
  74. uint8_t usbh_channel_modify (usb_core_handle_struct *pudev,
  75. uint8_t channel_num,
  76. uint8_t dev_addr,
  77. uint8_t dev_speed,
  78. uint8_t ep_type,
  79. uint16_t ep_mps)
  80. {
  81. usb_hostchannel_struct *puhc = &pudev->host.host_channel[channel_num];
  82. if (0U != dev_addr) {
  83. puhc->dev_addr = dev_addr;
  84. }
  85. if ((puhc->endp_mps != ep_mps) && (0U != ep_mps)) {
  86. puhc->endp_mps = ep_mps;
  87. }
  88. if ((puhc->dev_speed != dev_speed) && (0U != dev_speed)) {
  89. puhc->dev_speed = dev_speed;
  90. }
  91. usb_hostchannel_init(pudev, channel_num);
  92. return (uint8_t)HC_OK;
  93. }
  94. /*!
  95. \brief allocate a new channel for the pipe
  96. \param[in] pudev: pointer to usb device
  97. \param[in] ep_addr: endpoint for which the channel to be allocated
  98. \param[out] none
  99. \retval host channel number
  100. */
  101. uint8_t usbh_channel_alloc (usb_core_handle_struct *pudev, uint8_t ep_addr)
  102. {
  103. uint16_t hc_num = usbh_freechannel_get(pudev);
  104. if ((uint16_t)HC_ERROR != hc_num) {
  105. pudev->host.host_channel[hc_num].info = HC_USED | ep_addr;
  106. }
  107. return (uint8_t)hc_num;
  108. }
  109. /*!
  110. \brief free the usb host channel
  111. \param[in] pudev: pointer to usb device
  112. \param[in] index: channel number to be freed which is in (0..7)
  113. \param[out] none
  114. \retval host operation status
  115. */
  116. uint8_t usbh_channel_free (usb_core_handle_struct *pudev, uint8_t index)
  117. {
  118. if (index < HC_MAX) {
  119. pudev->host.host_channel[index].info &= HC_USED_MASK;
  120. }
  121. return USBH_OK;
  122. }
  123. /*!
  124. \brief free all usb host channel
  125. \param[in] pudev: pointer to usb device
  126. \param[out] none
  127. \retval host operation status
  128. */
  129. uint8_t usbh_allchannel_dealloc (usb_core_handle_struct *pudev)
  130. {
  131. uint8_t index;
  132. for (index = 2U; index < HC_MAX; index ++) {
  133. pudev->host.host_channel[index].info = 0U;
  134. }
  135. return USBH_OK;
  136. }
  137. /*!
  138. \brief get a free channel number for allocation to a device endpoint
  139. \param[in] pudev: pointer to usb device
  140. \param[out] none
  141. \retval free channel number
  142. */
  143. static uint16_t usbh_freechannel_get (usb_core_handle_struct *pudev)
  144. {
  145. uint8_t index = 0U;
  146. for (index = 0U; index < HC_MAX; index++) {
  147. if (0U == (pudev->host.host_channel[index].info & HC_USED)) {
  148. return (uint16_t)index;
  149. }
  150. }
  151. return HC_ERROR;
  152. }