usbh_hcs.c 6.0 KB

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