furi_hal_usb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 FuriHalUsbInterface* usb_if_cur;
  9. static FuriHalUsbInterface* 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 FuriHalUsbStateCallback callback;
  14. static void* cb_ctx;
  15. static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length);
  16. static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep);
  17. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep);
  18. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep);
  19. struct UsbCfg {
  20. osTimerId_t reconnect_tmr;
  21. bool enabled;
  22. bool connected;
  23. bool mode_changing;
  24. } usb_config;
  25. static void furi_hal_usb_tmr_cb(void* context);
  26. /* Low-level init */
  27. void furi_hal_usb_init(void) {
  28. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  29. LL_PWR_EnableVddUSB();
  30. GPIO_InitStruct.Pin = LL_GPIO_PIN_11 | LL_GPIO_PIN_12;
  31. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  32. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  33. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  34. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  35. GPIO_InitStruct.Alternate = LL_GPIO_AF_10;
  36. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  37. usbd_init(&udev, &usbd_hw, USB_EP0_SIZE, ubuf, sizeof(ubuf));
  38. usbd_enable(&udev, true);
  39. usbd_reg_descr(&udev, usb_descriptor_get);
  40. usbd_reg_event(&udev, usbd_evt_susp, susp_evt);
  41. usbd_reg_event(&udev, usbd_evt_wkup, wkup_evt);
  42. // Reset callback will be enabled after first mode change to avoid getting false reset events
  43. usb_config.enabled = false;
  44. usb_config.reconnect_tmr = NULL;
  45. HAL_NVIC_SetPriority(USB_LP_IRQn, 5, 0);
  46. NVIC_EnableIRQ(USB_LP_IRQn);
  47. FURI_LOG_I(TAG, "Init OK");
  48. }
  49. void furi_hal_usb_set_config(FuriHalUsbInterface* new_if) {
  50. if((new_if != usb_if_cur) && (usb_config.enabled)) { // Interface mode change - first stage
  51. usb_config.mode_changing = true;
  52. usb_if_next = new_if;
  53. if(usb_config.reconnect_tmr == NULL)
  54. usb_config.reconnect_tmr = osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL);
  55. furi_hal_usb_disable();
  56. usb_config.mode_changing = true;
  57. osTimerStart(usb_config.reconnect_tmr, USB_RECONNECT_DELAY);
  58. } else if(
  59. (usb_config.mode_changing) &&
  60. (usb_if_next != new_if)) { // Last interface mode change wasn't completed
  61. osTimerStop(usb_config.reconnect_tmr);
  62. usb_if_next = new_if;
  63. osTimerStart(usb_config.reconnect_tmr, USB_RECONNECT_DELAY);
  64. } else { // Interface mode change - second stage
  65. if(usb_if_cur != NULL) usb_if_cur->deinit(&udev);
  66. if(new_if != NULL) {
  67. new_if->init(&udev, new_if);
  68. usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
  69. FURI_LOG_I(TAG, "USB Mode change done");
  70. usb_config.enabled = true;
  71. usb_if_cur = new_if;
  72. usb_config.mode_changing = false;
  73. }
  74. }
  75. }
  76. void furi_hal_usb_reinit() {
  77. // Temporary disable callback to avoid getting false reset events
  78. usbd_reg_event(&udev, usbd_evt_reset, NULL);
  79. FURI_LOG_I(TAG, "USB Reinit");
  80. furi_hal_usb_disable();
  81. usbd_enable(&udev, false);
  82. usbd_enable(&udev, true);
  83. if(usb_config.reconnect_tmr == NULL)
  84. usb_config.reconnect_tmr = osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL);
  85. usb_config.mode_changing = true;
  86. usb_if_next = usb_if_cur;
  87. osTimerStart(usb_config.reconnect_tmr, USB_RECONNECT_DELAY);
  88. }
  89. FuriHalUsbInterface* furi_hal_usb_get_config() {
  90. return usb_if_cur;
  91. }
  92. void furi_hal_usb_disable() {
  93. if(usb_config.enabled) {
  94. susp_evt(&udev, 0, 0);
  95. usbd_connect(&udev, false);
  96. usb_config.enabled = false;
  97. FURI_LOG_I(TAG, "USB Disable");
  98. }
  99. }
  100. void furi_hal_usb_enable() {
  101. if((!usb_config.enabled) && (usb_if_cur != NULL)) {
  102. usbd_connect(&udev, true);
  103. usb_config.enabled = true;
  104. FURI_LOG_I(TAG, "USB Enable");
  105. }
  106. }
  107. static void furi_hal_usb_tmr_cb(void* context) {
  108. furi_hal_usb_set_config(usb_if_next);
  109. }
  110. /* Get device / configuration descriptors */
  111. static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) {
  112. const uint8_t dtype = req->wValue >> 8;
  113. const uint8_t dnumber = req->wValue & 0xFF;
  114. const void* desc;
  115. uint16_t len = 0;
  116. if(usb_if_cur == NULL) return usbd_fail;
  117. switch(dtype) {
  118. case USB_DTYPE_DEVICE:
  119. if(callback != NULL) {
  120. callback(FuriHalUsbStateEventDescriptorRequest, cb_ctx);
  121. }
  122. desc = usb_if_cur->dev_descr;
  123. break;
  124. case USB_DTYPE_CONFIGURATION:
  125. desc = usb_if_cur->cfg_descr;
  126. len = ((struct usb_string_descriptor*)(usb_if_cur->cfg_descr))->wString[0];
  127. break;
  128. case USB_DTYPE_STRING:
  129. if(dnumber == UsbDevLang) {
  130. desc = &dev_lang_desc;
  131. } else if((dnumber == UsbDevManuf) && (usb_if_cur->str_manuf_descr != NULL)) {
  132. desc = usb_if_cur->str_manuf_descr;
  133. } else if((dnumber == UsbDevProduct) && (usb_if_cur->str_prod_descr != NULL)) {
  134. desc = usb_if_cur->str_prod_descr;
  135. } else if((dnumber == UsbDevSerial) && (usb_if_cur->str_serial_descr != NULL)) {
  136. desc = usb_if_cur->str_serial_descr;
  137. } else
  138. return usbd_fail;
  139. break;
  140. default:
  141. return usbd_fail;
  142. }
  143. if(desc == NULL) return usbd_fail;
  144. if(len == 0) {
  145. len = ((struct usb_header_descriptor*)desc)->bLength;
  146. }
  147. *address = (void*)desc;
  148. *length = len;
  149. return usbd_ack;
  150. }
  151. void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) {
  152. callback = cb;
  153. cb_ctx = ctx;
  154. }
  155. static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  156. if(callback != NULL) {
  157. callback(FuriHalUsbStateEventReset, cb_ctx);
  158. }
  159. }
  160. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  161. if((usb_if_cur != NULL) && (usb_config.connected == true)) {
  162. usb_config.connected = false;
  163. usb_if_cur->suspend(&udev);
  164. }
  165. if(callback != NULL) {
  166. callback(FuriHalUsbStateEventSuspend, cb_ctx);
  167. }
  168. }
  169. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  170. if((usb_if_cur != NULL) && (usb_config.connected == false)) {
  171. usb_config.connected = true;
  172. usb_if_cur->wakeup(&udev);
  173. }
  174. if(callback != NULL) {
  175. callback(FuriHalUsbStateEventWakeup, cb_ctx);
  176. }
  177. }