furi-hal-usb.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "furi-hal-version.h"
  2. #include "furi-hal-usb_i.h"
  3. #include "furi-hal-usb.h"
  4. #include <furi.h>
  5. #include "usb.h"
  6. #define TAG "FuriHalUsb"
  7. #define USB_RECONNECT_DELAY 500
  8. static UsbInterface* usb_if_cur;
  9. static UsbInterface* usb_if_next;
  10. static const struct usb_string_descriptor dev_lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
  11. static uint32_t ubuf[0x20];
  12. usbd_device udev;
  13. static usbd_respond usb_descriptor_get (usbd_ctlreq *req, void **address, uint16_t *length);
  14. static void susp_evt(usbd_device *dev, uint8_t event, uint8_t ep);
  15. static void wkup_evt(usbd_device *dev, uint8_t event, uint8_t ep);
  16. struct UsbCfg{
  17. osTimerId_t reconnect_tmr;
  18. bool enabled;
  19. bool connected;
  20. } usb_config;
  21. static void furi_hal_usb_tmr_cb(void* context);
  22. /* Low-level init */
  23. void furi_hal_usb_init(void) {
  24. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  25. LL_PWR_EnableVddUSB();
  26. GPIO_InitStruct.Pin = LL_GPIO_PIN_11 | LL_GPIO_PIN_12;
  27. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  28. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  29. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  30. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  31. GPIO_InitStruct.Alternate = LL_GPIO_AF_10;
  32. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  33. usbd_init(&udev, &usbd_hw, USB_EP0_SIZE, ubuf, sizeof(ubuf));
  34. usbd_enable(&udev, true);
  35. usbd_reg_descr(&udev, usb_descriptor_get);
  36. usbd_reg_event(&udev, usbd_evt_susp, susp_evt);
  37. usbd_reg_event(&udev, usbd_evt_wkup, wkup_evt);
  38. usb_config.enabled = false;
  39. usb_config.reconnect_tmr = NULL;
  40. HAL_NVIC_SetPriority(USB_LP_IRQn, 5, 0);
  41. NVIC_EnableIRQ(USB_LP_IRQn);
  42. FURI_LOG_I(TAG, "Init OK");
  43. }
  44. void furi_hal_usb_set_config(UsbInterface* new_if) {
  45. if (new_if != usb_if_cur) {
  46. if (usb_config.enabled) {
  47. usb_if_next = new_if;
  48. if (usb_config.reconnect_tmr == NULL)
  49. usb_config.reconnect_tmr = osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL);
  50. furi_hal_usb_disable();
  51. osTimerStart(usb_config.reconnect_tmr, USB_RECONNECT_DELAY);
  52. }
  53. else {
  54. if (usb_if_cur != NULL)
  55. usb_if_cur->deinit(&udev);
  56. if (new_if != NULL) {
  57. new_if->init(&udev, new_if);
  58. FURI_LOG_I(TAG, "USB mode change");
  59. usb_config.enabled = true;
  60. usb_if_cur = new_if;
  61. }
  62. }
  63. }
  64. }
  65. UsbInterface* furi_hal_usb_get_config() {
  66. return usb_if_cur;
  67. }
  68. void furi_hal_usb_disable() {
  69. if (usb_config.enabled) {
  70. susp_evt(&udev, 0, 0);
  71. usbd_connect(&udev, false);
  72. usb_config.enabled = false;
  73. FURI_LOG_I(TAG, "USB Disable");
  74. }
  75. }
  76. void furi_hal_usb_enable() {
  77. if ((!usb_config.enabled) && (usb_if_cur != NULL)) {
  78. usbd_connect(&udev, true);
  79. usb_config.enabled = true;
  80. FURI_LOG_I(TAG, "USB Enable");
  81. }
  82. }
  83. static void furi_hal_usb_tmr_cb(void* context) {
  84. furi_hal_usb_set_config(usb_if_next);
  85. }
  86. /* Get device / configuration descriptors */
  87. static usbd_respond usb_descriptor_get(usbd_ctlreq *req, void **address, uint16_t *length) {
  88. const uint8_t dtype = req->wValue >> 8;
  89. const uint8_t dnumber = req->wValue & 0xFF;
  90. const void* desc;
  91. uint16_t len = 0;
  92. if (usb_if_cur == NULL)
  93. return usbd_fail;
  94. switch (dtype) {
  95. case USB_DTYPE_DEVICE:
  96. desc = usb_if_cur->dev_descr;
  97. break;
  98. case USB_DTYPE_CONFIGURATION:
  99. desc = usb_if_cur->cfg_descr;
  100. len = ((struct usb_string_descriptor*)(usb_if_cur->cfg_descr))->wString[0];
  101. break;
  102. case USB_DTYPE_STRING:
  103. if (dnumber == UsbDevLang) {
  104. desc = &dev_lang_desc;
  105. } else if (dnumber == UsbDevManuf) {
  106. desc = usb_if_cur->str_manuf_descr;
  107. } else if (dnumber == UsbDevProduct) {
  108. desc = usb_if_cur->str_prod_descr;
  109. } else if (dnumber == UsbDevSerial) {
  110. desc = usb_if_cur->str_serial_descr;
  111. } else
  112. return usbd_fail;
  113. break;
  114. default:
  115. return usbd_fail;
  116. }
  117. if (desc == NULL)
  118. return usbd_fail;
  119. if (len == 0) {
  120. len = ((struct usb_header_descriptor*)desc)->bLength;
  121. }
  122. *address = (void*)desc;
  123. *length = len;
  124. return usbd_ack;
  125. }
  126. static void susp_evt(usbd_device *dev, uint8_t event, uint8_t ep) {
  127. if ((usb_if_cur != NULL) && (usb_config.connected == true)) {
  128. usb_config.connected = false;
  129. usb_if_cur->suspend(&udev);
  130. }
  131. }
  132. static void wkup_evt(usbd_device *dev, uint8_t event, uint8_t ep) {
  133. if ((usb_if_cur != NULL) && (usb_config.connected == false)) {
  134. usb_config.connected = true;
  135. usb_if_cur->wakeup(&udev);
  136. }
  137. }