usb_serial.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2023-2025 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. #include "ZuluSCSI_platform.h"
  22. #include "usb_serial.h"
  23. extern "C"
  24. {
  25. #include <gd32_cdc_acm_core.h>
  26. #include <drv_usb_hw.h>
  27. #include <usbd_core.h>
  28. #include <drv_usbd_int.h>
  29. usb_core_driver cdc_acm;
  30. }
  31. void usb_serial_init(void)
  32. {
  33. // set USB full speed prescaler and turn on USB clock
  34. rcu_usbfs_trng_clock_config(RCU_CKUSB_CKPLL_DIV2_5);
  35. rcu_periph_clock_enable(RCU_USBFS);
  36. usbd_init(&cdc_acm, USB_CORE_ENUM_FS, &gd32_cdc_desc, &gd32_cdc_class);
  37. // USB full speed Interrupt config
  38. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  39. nvic_irq_enable((uint8_t)USBFS_IRQn, 2U, 0U);
  40. }
  41. bool usb_serial_ready(void)
  42. {
  43. // check that (our) serial is the currently active class
  44. if ((USBD_CONFIGURED == cdc_acm.dev.cur_status) && (cdc_acm.dev.desc == &gd32_cdc_desc))
  45. {
  46. gd32_usb_cdc_handler *cdc = (gd32_usb_cdc_handler *)cdc_acm.dev.class_data[CDC_COM_INTERFACE];
  47. if (cdc->packet_receive)
  48. {
  49. // Discard any received data.
  50. // Otherwise it queues up on the host side and can cause the port to hang.
  51. cdc->packet_receive = 0;
  52. gd32_cdc_acm_data_receive(&cdc_acm);
  53. }
  54. if (1U == gd32_cdc_acm_check_ready(&cdc_acm))
  55. {
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. void usb_serial_send(uint8_t *data, uint32_t length)
  62. {
  63. gd32_cdc_acm_data_send(&cdc_acm, data, length);
  64. }
  65. void USBFS_IRQHandler(void)
  66. {
  67. usbd_isr(&cdc_acm);
  68. }
  69. void usb_udelay (const uint32_t usec)
  70. {
  71. delay_us(usec);
  72. }
  73. void usb_mdelay (const uint32_t msec)
  74. {
  75. delay(msec);
  76. }