furi_hal_usb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <stm32wbxx_ll_pwr.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. bool enabled;
  13. bool connected;
  14. bool mode_lock;
  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. /* Low-level init */
  43. void furi_hal_usb_init(void) {
  44. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  45. LL_PWR_EnableVddUSB();
  46. GPIO_InitStruct.Pin = LL_GPIO_PIN_11 | LL_GPIO_PIN_12;
  47. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  48. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  49. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  50. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  51. GPIO_InitStruct.Alternate = LL_GPIO_AF_10;
  52. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  53. usbd_init(&udev, &usbd_hw, USB_EP0_SIZE, ubuf, sizeof(ubuf));
  54. usbd_enable(&udev, true);
  55. usbd_reg_descr(&udev, usb_descriptor_get);
  56. usbd_reg_event(&udev, usbd_evt_susp, susp_evt);
  57. usbd_reg_event(&udev, usbd_evt_wkup, wkup_evt);
  58. // Reset callback will be enabled after first mode change to avoid getting false reset events
  59. usb.enabled = false;
  60. usb.if_cur = NULL;
  61. NVIC_SetPriority(USB_LP_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  62. NVIC_SetPriority(USB_HP_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 15, 0));
  63. NVIC_EnableIRQ(USB_LP_IRQn);
  64. NVIC_EnableIRQ(USB_HP_IRQn);
  65. usb.thread = furi_thread_alloc_ex("UsbDriver", 1024, furi_hal_usb_thread, NULL);
  66. furi_thread_mark_as_service(usb.thread);
  67. furi_thread_start(usb.thread);
  68. FURI_LOG_I(TAG, "Init OK");
  69. }
  70. bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
  71. if(usb.mode_lock) {
  72. return false;
  73. }
  74. usb.if_next = new_if;
  75. usb.if_ctx = ctx;
  76. if(usb.thread == NULL) {
  77. // Service thread hasn't started yet, so just save interface mode
  78. return true;
  79. }
  80. furi_assert(usb.thread);
  81. furi_thread_flags_set(furi_thread_get_id(usb.thread), EventModeChange);
  82. return true;
  83. }
  84. FuriHalUsbInterface* furi_hal_usb_get_config() {
  85. return usb.if_cur;
  86. }
  87. void furi_hal_usb_lock() {
  88. FURI_LOG_I(TAG, "Mode lock");
  89. usb.mode_lock = true;
  90. }
  91. void furi_hal_usb_unlock() {
  92. FURI_LOG_I(TAG, "Mode unlock");
  93. usb.mode_lock = false;
  94. }
  95. bool furi_hal_usb_is_locked() {
  96. return usb.mode_lock;
  97. }
  98. void furi_hal_usb_disable() {
  99. furi_assert(usb.thread);
  100. furi_thread_flags_set(furi_thread_get_id(usb.thread), EventDisable);
  101. }
  102. void furi_hal_usb_enable() {
  103. furi_assert(usb.thread);
  104. furi_thread_flags_set(furi_thread_get_id(usb.thread), EventEnable);
  105. }
  106. void furi_hal_usb_reinit() {
  107. furi_assert(usb.thread);
  108. furi_thread_flags_set(furi_thread_get_id(usb.thread), EventReinit);
  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. furi_thread_flags_set(furi_thread_get_id(usb.thread), EventRequest);
  120. if(usb.callback != NULL) {
  121. usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.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. usb.callback = cb;
  154. usb.cb_ctx = ctx;
  155. }
  156. static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  157. UNUSED(dev);
  158. UNUSED(event);
  159. UNUSED(ep);
  160. furi_thread_flags_set(furi_thread_get_id(usb.thread), EventReset);
  161. if(usb.callback != NULL) {
  162. usb.callback(FuriHalUsbStateEventReset, usb.cb_ctx);
  163. }
  164. }
  165. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  166. UNUSED(dev);
  167. UNUSED(event);
  168. UNUSED(ep);
  169. if((usb.if_cur != NULL) && (usb.connected == true)) {
  170. usb.connected = false;
  171. usb.if_cur->suspend(&udev);
  172. furi_hal_power_insomnia_exit();
  173. }
  174. if(usb.callback != NULL) {
  175. usb.callback(FuriHalUsbStateEventSuspend, usb.cb_ctx);
  176. }
  177. }
  178. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  179. UNUSED(dev);
  180. UNUSED(event);
  181. UNUSED(ep);
  182. if((usb.if_cur != NULL) && (usb.connected == false)) {
  183. usb.connected = true;
  184. usb.if_cur->wakeup(&udev);
  185. furi_hal_power_insomnia_enter();
  186. }
  187. if(usb.callback != NULL) {
  188. usb.callback(FuriHalUsbStateEventWakeup, usb.cb_ctx);
  189. }
  190. }
  191. static int32_t furi_hal_usb_thread(void* context) {
  192. UNUSED(context);
  193. bool usb_request_pending = false;
  194. uint8_t usb_wait_time = 0;
  195. FuriHalUsbInterface* if_new = NULL;
  196. FuriHalUsbInterface* if_ctx_new = NULL;
  197. if(usb.if_next != NULL) {
  198. furi_thread_flags_set(furi_thread_get_id(usb.thread), EventModeChange);
  199. }
  200. while(true) {
  201. uint32_t flags = furi_thread_flags_wait(USB_SRV_ALL_EVENTS, FuriFlagWaitAny, 500);
  202. if((flags & FuriFlagError) == 0) {
  203. if(flags & EventModeChange) {
  204. if(usb.if_next != usb.if_cur) {
  205. if_new = usb.if_next;
  206. if_ctx_new = usb.if_ctx;
  207. if(usb.enabled) { // Disable current interface
  208. susp_evt(&udev, 0, 0);
  209. usbd_connect(&udev, false);
  210. usb.enabled = false;
  211. furi_delay_ms(USB_RECONNECT_DELAY);
  212. }
  213. flags |= EventModeChangeStart;
  214. }
  215. }
  216. if(flags & EventReinit) {
  217. // Temporary disable callback to avoid getting false reset events
  218. usbd_reg_event(&udev, usbd_evt_reset, NULL);
  219. FURI_LOG_I(TAG, "USB Reinit");
  220. susp_evt(&udev, 0, 0);
  221. usbd_connect(&udev, false);
  222. usb.enabled = false;
  223. usbd_enable(&udev, false);
  224. usbd_enable(&udev, true);
  225. if_new = usb.if_cur;
  226. furi_delay_ms(USB_RECONNECT_DELAY);
  227. flags |= EventModeChangeStart;
  228. }
  229. if(flags & EventModeChangeStart) { // Second stage of mode change process
  230. if(usb.if_cur != NULL) {
  231. usb.if_cur->deinit(&udev);
  232. }
  233. if(if_new != NULL) {
  234. if_new->init(&udev, if_new, if_ctx_new);
  235. usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
  236. FURI_LOG_I(TAG, "USB Mode change done");
  237. usb.enabled = true;
  238. }
  239. usb.if_cur = if_new;
  240. }
  241. if(flags & EventEnable) {
  242. if((!usb.enabled) && (usb.if_cur != NULL)) {
  243. usbd_connect(&udev, true);
  244. usb.enabled = true;
  245. FURI_LOG_I(TAG, "USB Enable");
  246. }
  247. }
  248. if(flags & EventDisable) {
  249. if(usb.enabled) {
  250. susp_evt(&udev, 0, 0);
  251. usbd_connect(&udev, false);
  252. usb.enabled = false;
  253. usb_request_pending = false;
  254. FURI_LOG_I(TAG, "USB Disable");
  255. }
  256. }
  257. if(flags & EventReset) {
  258. if(usb.enabled) {
  259. usb_request_pending = true;
  260. usb_wait_time = 0;
  261. }
  262. }
  263. if(flags & EventRequest) {
  264. usb_request_pending = false;
  265. }
  266. } else if(usb_request_pending) {
  267. usb_wait_time++;
  268. if(usb_wait_time > 4) {
  269. furi_hal_usb_reinit();
  270. usb_request_pending = false;
  271. }
  272. }
  273. }
  274. return 0;
  275. }