usbh_hcs.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*!
  2. \file usbh_hcs.c
  3. \brief this file implements functions for opening and closing host channels
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-02-10, V1.0.0, firmware for GD32F30x
  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. usb_hostchannel_init(pudev, channel_num);
  40. return (uint8_t)HC_OK;
  41. }
  42. /*!
  43. \brief modify a channel
  44. \param[in] pudev: pointer to usb device
  45. \param[in] channel_num: host channel number which is in (0..7)
  46. \param[in] dev_addr: USB Device address allocated to attached device
  47. \param[in] dev_speed: USB device speed (Full speed/Low speed)
  48. \param[in] ep_type: endpoint type (bulk/int/ctl)
  49. \param[in] ep_mps: max packet size
  50. \param[out] none
  51. \retval operation status
  52. */
  53. uint8_t usbh_channel_modify (usb_core_handle_struct *pudev,
  54. uint8_t channel_num,
  55. uint8_t dev_addr,
  56. uint8_t dev_speed,
  57. uint8_t ep_type,
  58. uint16_t ep_mps)
  59. {
  60. usb_hostchannel_struct *puhc = &pudev->host.host_channel[channel_num];
  61. if (0U != dev_addr) {
  62. puhc->dev_addr = dev_addr;
  63. }
  64. if ((puhc->endp_mps != ep_mps) && (0U != ep_mps)) {
  65. puhc->endp_mps = ep_mps;
  66. }
  67. if ((puhc->dev_speed != dev_speed) && (0U != dev_speed)) {
  68. puhc->dev_speed = dev_speed;
  69. }
  70. usb_hostchannel_init(pudev, channel_num);
  71. return (uint8_t)HC_OK;
  72. }
  73. /*!
  74. \brief allocate a new channel for the pipe
  75. \param[in] pudev: pointer to usb device
  76. \param[in] ep_addr: endpoint for which the channel to be allocated
  77. \param[out] none
  78. \retval host channel number
  79. */
  80. uint8_t usbh_channel_alloc (usb_core_handle_struct *pudev, uint8_t ep_addr)
  81. {
  82. uint16_t hc_num = usbh_freechannel_get(pudev);
  83. if ((uint16_t)HC_ERROR != hc_num) {
  84. pudev->host.host_channel[hc_num].info = HC_USED | ep_addr;
  85. }
  86. return (uint8_t)hc_num;
  87. }
  88. /*!
  89. \brief free the usb host channel
  90. \param[in] pudev: pointer to usb device
  91. \param[in] index: channel number to be freed which is in (0..7)
  92. \param[out] none
  93. \retval host operation status
  94. */
  95. uint8_t usbh_channel_free (usb_core_handle_struct *pudev, uint8_t index)
  96. {
  97. if (index < HC_MAX) {
  98. pudev->host.host_channel[index].info &= HC_USED_MASK;
  99. }
  100. return USBH_OK;
  101. }
  102. /*!
  103. \brief free all usb host channel
  104. \param[in] pudev: pointer to usb device
  105. \param[out] none
  106. \retval host operation status
  107. */
  108. uint8_t usbh_allchannel_dealloc (usb_core_handle_struct *pudev)
  109. {
  110. uint8_t index;
  111. for (index = 2U; index < HC_MAX; index ++) {
  112. pudev->host.host_channel[index].info = 0U;
  113. }
  114. return USBH_OK;
  115. }
  116. /*!
  117. \brief get a free channel number for allocation to a device endpoint
  118. \param[in] pudev: pointer to usb device
  119. \param[out] none
  120. \retval free channel number
  121. */
  122. static uint16_t usbh_freechannel_get (usb_core_handle_struct *pudev)
  123. {
  124. uint8_t index = 0U;
  125. for (index = 0U; index < HC_MAX; index++) {
  126. if (0U == (pudev->host.host_channel[index].info & HC_USED)) {
  127. return (uint16_t)index;
  128. }
  129. }
  130. return HC_ERROR;
  131. }