furi_hal_usb.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 =
  50. osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL);
  51. furi_hal_usb_disable();
  52. osTimerStart(usb_config.reconnect_tmr, USB_RECONNECT_DELAY);
  53. } else {
  54. if(usb_if_cur != NULL) usb_if_cur->deinit(&udev);
  55. if(new_if != NULL) {
  56. new_if->init(&udev, new_if);
  57. FURI_LOG_I(TAG, "USB mode change");
  58. usb_config.enabled = true;
  59. usb_if_cur = new_if;
  60. }
  61. }
  62. }
  63. }
  64. UsbInterface* furi_hal_usb_get_config() {
  65. return usb_if_cur;
  66. }
  67. void furi_hal_usb_disable() {
  68. if(usb_config.enabled) {
  69. susp_evt(&udev, 0, 0);
  70. usbd_connect(&udev, false);
  71. usb_config.enabled = false;
  72. FURI_LOG_I(TAG, "USB Disable");
  73. }
  74. }
  75. void furi_hal_usb_enable() {
  76. if((!usb_config.enabled) && (usb_if_cur != NULL)) {
  77. usbd_connect(&udev, true);
  78. usb_config.enabled = true;
  79. FURI_LOG_I(TAG, "USB Enable");
  80. }
  81. }
  82. static void furi_hal_usb_tmr_cb(void* context) {
  83. furi_hal_usb_set_config(usb_if_next);
  84. }
  85. /* Get device / configuration descriptors */
  86. static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) {
  87. const uint8_t dtype = req->wValue >> 8;
  88. const uint8_t dnumber = req->wValue & 0xFF;
  89. const void* desc;
  90. uint16_t len = 0;
  91. if(usb_if_cur == NULL) return usbd_fail;
  92. switch(dtype) {
  93. case USB_DTYPE_DEVICE:
  94. desc = usb_if_cur->dev_descr;
  95. break;
  96. case USB_DTYPE_CONFIGURATION:
  97. desc = usb_if_cur->cfg_descr;
  98. len = ((struct usb_string_descriptor*)(usb_if_cur->cfg_descr))->wString[0];
  99. break;
  100. case USB_DTYPE_STRING:
  101. if(dnumber == UsbDevLang) {
  102. desc = &dev_lang_desc;
  103. } else if(dnumber == UsbDevManuf) {
  104. desc = usb_if_cur->str_manuf_descr;
  105. } else if(dnumber == UsbDevProduct) {
  106. desc = usb_if_cur->str_prod_descr;
  107. } else if(dnumber == UsbDevSerial) {
  108. desc = usb_if_cur->str_serial_descr;
  109. } else
  110. return usbd_fail;
  111. break;
  112. default:
  113. return usbd_fail;
  114. }
  115. if(desc == NULL) return usbd_fail;
  116. if(len == 0) {
  117. len = ((struct usb_header_descriptor*)desc)->bLength;
  118. }
  119. *address = (void*)desc;
  120. *length = len;
  121. return usbd_ack;
  122. }
  123. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  124. if((usb_if_cur != NULL) && (usb_config.connected == true)) {
  125. usb_config.connected = false;
  126. usb_if_cur->suspend(&udev);
  127. }
  128. }
  129. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  130. if((usb_if_cur != NULL) && (usb_config.connected == false)) {
  131. usb_config.connected = true;
  132. usb_if_cur->wakeup(&udev);
  133. }
  134. }