furi_hal_usb.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include "furi_hal_version.h"
  2. #include "furi_hal_usb_i.h"
  3. #include "furi_hal_usb.h"
  4. #include "furi_hal_vcp.h"
  5. #include <furi_hal_power.h>
  6. #include <furi.h>
  7. #include "usb.h"
  8. #define TAG "FuriHalUsb"
  9. #define USB_RECONNECT_DELAY 500
  10. typedef struct {
  11. FuriThread* thread;
  12. osTimerId_t tmr;
  13. bool enabled;
  14. bool connected;
  15. FuriHalUsbInterface* if_cur;
  16. FuriHalUsbInterface* if_next;
  17. void* if_ctx;
  18. FuriHalUsbStateCallback callback;
  19. void* cb_ctx;
  20. } UsbSrv;
  21. typedef enum {
  22. EventModeChange = (1 << 0),
  23. EventEnable = (1 << 1),
  24. EventDisable = (1 << 2),
  25. EventReinit = (1 << 3),
  26. EventReset = (1 << 4),
  27. EventRequest = (1 << 5),
  28. EventModeChangeStart = (1 << 6),
  29. } UsbEvent;
  30. #define USB_SRV_ALL_EVENTS \
  31. (EventModeChange | EventEnable | EventDisable | EventReinit | EventReset | EventRequest | \
  32. EventModeChangeStart)
  33. static UsbSrv usb;
  34. static const struct usb_string_descriptor dev_lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
  35. static uint32_t ubuf[0x20];
  36. usbd_device udev;
  37. static int32_t furi_hal_usb_thread(void* context);
  38. static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length);
  39. static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep);
  40. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep);
  41. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep);
  42. static void furi_hal_usb_tmr_cb(void* context);
  43. /* Low-level init */
  44. void furi_hal_usb_init(void) {
  45. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  46. LL_PWR_EnableVddUSB();
  47. GPIO_InitStruct.Pin = LL_GPIO_PIN_11 | LL_GPIO_PIN_12;
  48. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  49. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  50. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  51. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  52. GPIO_InitStruct.Alternate = LL_GPIO_AF_10;
  53. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  54. usbd_init(&udev, &usbd_hw, USB_EP0_SIZE, ubuf, sizeof(ubuf));
  55. usbd_enable(&udev, true);
  56. usbd_reg_descr(&udev, usb_descriptor_get);
  57. usbd_reg_event(&udev, usbd_evt_susp, susp_evt);
  58. usbd_reg_event(&udev, usbd_evt_wkup, wkup_evt);
  59. // Reset callback will be enabled after first mode change to avoid getting false reset events
  60. usb.enabled = false;
  61. usb.if_cur = NULL;
  62. HAL_NVIC_SetPriority(USB_LP_IRQn, 5, 0);
  63. NVIC_EnableIRQ(USB_LP_IRQn);
  64. usb.thread = furi_thread_alloc();
  65. furi_thread_set_name(usb.thread, "UsbDriver");
  66. furi_thread_set_stack_size(usb.thread, 1024);
  67. furi_thread_set_callback(usb.thread, furi_hal_usb_thread);
  68. furi_thread_start(usb.thread);
  69. FURI_LOG_I(TAG, "Init OK");
  70. }
  71. void furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
  72. usb.if_next = new_if;
  73. usb.if_ctx = ctx;
  74. if(usb.thread == NULL) {
  75. // Service thread hasn't started yet, so just save interface mode
  76. return;
  77. }
  78. furi_assert(usb.thread);
  79. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
  80. }
  81. FuriHalUsbInterface* furi_hal_usb_get_config() {
  82. return usb.if_cur;
  83. }
  84. void furi_hal_usb_disable() {
  85. furi_assert(usb.thread);
  86. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventDisable);
  87. }
  88. void furi_hal_usb_enable() {
  89. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventEnable);
  90. }
  91. void furi_hal_usb_reinit() {
  92. furi_assert(usb.thread);
  93. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReinit);
  94. }
  95. static void furi_hal_usb_tmr_cb(void* context) {
  96. furi_assert(usb.thread);
  97. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChangeStart);
  98. }
  99. /* Get device / configuration descriptors */
  100. static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) {
  101. const uint8_t dtype = req->wValue >> 8;
  102. const uint8_t dnumber = req->wValue & 0xFF;
  103. const void* desc;
  104. uint16_t len = 0;
  105. if(usb.if_cur == NULL) return usbd_fail;
  106. switch(dtype) {
  107. case USB_DTYPE_DEVICE:
  108. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventRequest);
  109. if(usb.callback != NULL) {
  110. usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.cb_ctx);
  111. }
  112. desc = usb.if_cur->dev_descr;
  113. break;
  114. case USB_DTYPE_CONFIGURATION:
  115. desc = usb.if_cur->cfg_descr;
  116. len = ((struct usb_string_descriptor*)(usb.if_cur->cfg_descr))->wString[0];
  117. break;
  118. case USB_DTYPE_STRING:
  119. if(dnumber == UsbDevLang) {
  120. desc = &dev_lang_desc;
  121. } else if((dnumber == UsbDevManuf) && (usb.if_cur->str_manuf_descr != NULL)) {
  122. desc = usb.if_cur->str_manuf_descr;
  123. } else if((dnumber == UsbDevProduct) && (usb.if_cur->str_prod_descr != NULL)) {
  124. desc = usb.if_cur->str_prod_descr;
  125. } else if((dnumber == UsbDevSerial) && (usb.if_cur->str_serial_descr != NULL)) {
  126. desc = usb.if_cur->str_serial_descr;
  127. } else
  128. return usbd_fail;
  129. break;
  130. default:
  131. return usbd_fail;
  132. }
  133. if(desc == NULL) return usbd_fail;
  134. if(len == 0) {
  135. len = ((struct usb_header_descriptor*)desc)->bLength;
  136. }
  137. *address = (void*)desc;
  138. *length = len;
  139. return usbd_ack;
  140. }
  141. void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) {
  142. usb.callback = cb;
  143. usb.cb_ctx = ctx;
  144. }
  145. static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  146. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReset);
  147. if(usb.callback != NULL) {
  148. usb.callback(FuriHalUsbStateEventReset, usb.cb_ctx);
  149. }
  150. }
  151. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  152. if((usb.if_cur != NULL) && (usb.connected == true)) {
  153. usb.connected = false;
  154. usb.if_cur->suspend(&udev);
  155. furi_hal_power_insomnia_exit();
  156. }
  157. if(usb.callback != NULL) {
  158. usb.callback(FuriHalUsbStateEventSuspend, usb.cb_ctx);
  159. }
  160. }
  161. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  162. if((usb.if_cur != NULL) && (usb.connected == false)) {
  163. usb.connected = true;
  164. usb.if_cur->wakeup(&udev);
  165. furi_hal_power_insomnia_enter();
  166. }
  167. if(usb.callback != NULL) {
  168. usb.callback(FuriHalUsbStateEventWakeup, usb.cb_ctx);
  169. }
  170. }
  171. static int32_t furi_hal_usb_thread(void* context) {
  172. usb.tmr = osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL);
  173. bool usb_request_pending = false;
  174. uint8_t usb_wait_time = 0;
  175. if(usb.if_next != NULL) {
  176. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
  177. }
  178. while(true) {
  179. uint32_t flags = osThreadFlagsWait(USB_SRV_ALL_EVENTS, osFlagsWaitAny, 500);
  180. if((flags & osFlagsError) == 0) {
  181. if(flags & EventModeChange) {
  182. if(usb.if_next != usb.if_cur) {
  183. if(usb.enabled) { // Disable current interface
  184. susp_evt(&udev, 0, 0);
  185. usbd_connect(&udev, false);
  186. usb.enabled = false;
  187. osTimerStart(usb.tmr, USB_RECONNECT_DELAY);
  188. } else {
  189. flags |= EventModeChangeStart;
  190. }
  191. }
  192. }
  193. if(flags & EventReinit) {
  194. // Temporary disable callback to avoid getting false reset events
  195. usbd_reg_event(&udev, usbd_evt_reset, NULL);
  196. FURI_LOG_I(TAG, "USB Reinit");
  197. susp_evt(&udev, 0, 0);
  198. usbd_connect(&udev, false);
  199. usb.enabled = false;
  200. usbd_enable(&udev, false);
  201. usbd_enable(&udev, true);
  202. usb.if_next = usb.if_cur;
  203. osTimerStart(usb.tmr, USB_RECONNECT_DELAY);
  204. }
  205. if(flags & EventModeChangeStart) { // Second stage of mode change process
  206. if(usb.if_cur != NULL) {
  207. usb.if_cur->deinit(&udev);
  208. }
  209. if(usb.if_next != NULL) {
  210. usb.if_next->init(&udev, usb.if_next, usb.if_ctx);
  211. usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
  212. FURI_LOG_I(TAG, "USB Mode change done");
  213. usb.enabled = true;
  214. usb.if_cur = usb.if_next;
  215. }
  216. }
  217. if(flags & EventEnable) {
  218. if((!usb.enabled) && (usb.if_cur != NULL)) {
  219. usbd_connect(&udev, true);
  220. usb.enabled = true;
  221. FURI_LOG_I(TAG, "USB Enable");
  222. }
  223. }
  224. if(flags & EventDisable) {
  225. if(usb.enabled) {
  226. susp_evt(&udev, 0, 0);
  227. usbd_connect(&udev, false);
  228. usb.enabled = false;
  229. usb_request_pending = false;
  230. FURI_LOG_I(TAG, "USB Disable");
  231. }
  232. }
  233. if(flags & EventReset) {
  234. usb_request_pending = true;
  235. usb_wait_time = 0;
  236. }
  237. if(flags & EventRequest) {
  238. usb_request_pending = false;
  239. }
  240. } else if(usb_request_pending) {
  241. usb_wait_time++;
  242. if(usb_wait_time > 4) {
  243. furi_hal_usb_reinit();
  244. usb_request_pending = false;
  245. }
  246. }
  247. }
  248. return 0;
  249. }