furi_hal_usb.c 6.6 KB

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