furi_hal_usb.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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();
  66. furi_thread_set_name(usb.thread, "UsbDriver");
  67. furi_thread_set_stack_size(usb.thread, 1024);
  68. furi_thread_set_callback(usb.thread, furi_hal_usb_thread);
  69. furi_thread_start(usb.thread);
  70. FURI_LOG_I(TAG, "Init OK");
  71. }
  72. bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
  73. if(usb.mode_lock) {
  74. return false;
  75. }
  76. usb.if_next = new_if;
  77. usb.if_ctx = ctx;
  78. if(usb.thread == NULL) {
  79. // Service thread hasn't started yet, so just save interface mode
  80. return true;
  81. }
  82. furi_assert(usb.thread);
  83. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
  84. return true;
  85. }
  86. FuriHalUsbInterface* furi_hal_usb_get_config() {
  87. return usb.if_cur;
  88. }
  89. void furi_hal_usb_lock() {
  90. FURI_LOG_I(TAG, "Mode lock");
  91. usb.mode_lock = true;
  92. }
  93. void furi_hal_usb_unlock() {
  94. FURI_LOG_I(TAG, "Mode unlock");
  95. usb.mode_lock = false;
  96. }
  97. bool furi_hal_usb_is_locked() {
  98. return usb.mode_lock;
  99. }
  100. void furi_hal_usb_disable() {
  101. furi_assert(usb.thread);
  102. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventDisable);
  103. }
  104. void furi_hal_usb_enable() {
  105. furi_assert(usb.thread);
  106. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventEnable);
  107. }
  108. void furi_hal_usb_reinit() {
  109. furi_assert(usb.thread);
  110. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReinit);
  111. }
  112. /* Get device / configuration descriptors */
  113. static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) {
  114. const uint8_t dtype = req->wValue >> 8;
  115. const uint8_t dnumber = req->wValue & 0xFF;
  116. const void* desc;
  117. uint16_t len = 0;
  118. if(usb.if_cur == NULL) return usbd_fail;
  119. switch(dtype) {
  120. case USB_DTYPE_DEVICE:
  121. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventRequest);
  122. if(usb.callback != NULL) {
  123. usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.cb_ctx);
  124. }
  125. desc = usb.if_cur->dev_descr;
  126. break;
  127. case USB_DTYPE_CONFIGURATION:
  128. desc = usb.if_cur->cfg_descr;
  129. len = ((struct usb_string_descriptor*)(usb.if_cur->cfg_descr))->wString[0];
  130. break;
  131. case USB_DTYPE_STRING:
  132. if(dnumber == UsbDevLang) {
  133. desc = &dev_lang_desc;
  134. } else if((dnumber == UsbDevManuf) && (usb.if_cur->str_manuf_descr != NULL)) {
  135. desc = usb.if_cur->str_manuf_descr;
  136. } else if((dnumber == UsbDevProduct) && (usb.if_cur->str_prod_descr != NULL)) {
  137. desc = usb.if_cur->str_prod_descr;
  138. } else if((dnumber == UsbDevSerial) && (usb.if_cur->str_serial_descr != NULL)) {
  139. desc = usb.if_cur->str_serial_descr;
  140. } else
  141. return usbd_fail;
  142. break;
  143. default:
  144. return usbd_fail;
  145. }
  146. if(desc == NULL) return usbd_fail;
  147. if(len == 0) {
  148. len = ((struct usb_header_descriptor*)desc)->bLength;
  149. }
  150. *address = (void*)desc;
  151. *length = len;
  152. return usbd_ack;
  153. }
  154. void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) {
  155. usb.callback = cb;
  156. usb.cb_ctx = ctx;
  157. }
  158. static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  159. UNUSED(dev);
  160. UNUSED(event);
  161. UNUSED(ep);
  162. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReset);
  163. if(usb.callback != NULL) {
  164. usb.callback(FuriHalUsbStateEventReset, usb.cb_ctx);
  165. }
  166. }
  167. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  168. UNUSED(dev);
  169. UNUSED(event);
  170. UNUSED(ep);
  171. if((usb.if_cur != NULL) && (usb.connected == true)) {
  172. usb.connected = false;
  173. usb.if_cur->suspend(&udev);
  174. furi_hal_power_insomnia_exit();
  175. }
  176. if(usb.callback != NULL) {
  177. usb.callback(FuriHalUsbStateEventSuspend, usb.cb_ctx);
  178. }
  179. }
  180. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  181. UNUSED(dev);
  182. UNUSED(event);
  183. UNUSED(ep);
  184. if((usb.if_cur != NULL) && (usb.connected == false)) {
  185. usb.connected = true;
  186. usb.if_cur->wakeup(&udev);
  187. furi_hal_power_insomnia_enter();
  188. }
  189. if(usb.callback != NULL) {
  190. usb.callback(FuriHalUsbStateEventWakeup, usb.cb_ctx);
  191. }
  192. }
  193. static int32_t furi_hal_usb_thread(void* context) {
  194. UNUSED(context);
  195. bool usb_request_pending = false;
  196. uint8_t usb_wait_time = 0;
  197. FuriHalUsbInterface* if_new = NULL;
  198. FuriHalUsbInterface* if_ctx_new = NULL;
  199. if(usb.if_next != NULL) {
  200. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
  201. }
  202. while(true) {
  203. uint32_t flags = osThreadFlagsWait(USB_SRV_ALL_EVENTS, osFlagsWaitAny, 500);
  204. if((flags & osFlagsError) == 0) {
  205. if(flags & EventModeChange) {
  206. if(usb.if_next != usb.if_cur) {
  207. if_new = usb.if_next;
  208. if_ctx_new = usb.if_ctx;
  209. if(usb.enabled) { // Disable current interface
  210. susp_evt(&udev, 0, 0);
  211. usbd_connect(&udev, false);
  212. usb.enabled = false;
  213. osDelay(USB_RECONNECT_DELAY);
  214. }
  215. flags |= EventModeChangeStart;
  216. }
  217. }
  218. if(flags & EventReinit) {
  219. // Temporary disable callback to avoid getting false reset events
  220. usbd_reg_event(&udev, usbd_evt_reset, NULL);
  221. FURI_LOG_I(TAG, "USB Reinit");
  222. susp_evt(&udev, 0, 0);
  223. usbd_connect(&udev, false);
  224. usb.enabled = false;
  225. usbd_enable(&udev, false);
  226. usbd_enable(&udev, true);
  227. if_new = usb.if_cur;
  228. osDelay(USB_RECONNECT_DELAY);
  229. flags |= EventModeChangeStart;
  230. }
  231. if(flags & EventModeChangeStart) { // Second stage of mode change process
  232. if(usb.if_cur != NULL) {
  233. usb.if_cur->deinit(&udev);
  234. }
  235. if(if_new != NULL) {
  236. if_new->init(&udev, if_new, if_ctx_new);
  237. usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
  238. FURI_LOG_I(TAG, "USB Mode change done");
  239. usb.enabled = true;
  240. }
  241. usb.if_cur = if_new;
  242. }
  243. if(flags & EventEnable) {
  244. if((!usb.enabled) && (usb.if_cur != NULL)) {
  245. usbd_connect(&udev, true);
  246. usb.enabled = true;
  247. FURI_LOG_I(TAG, "USB Enable");
  248. }
  249. }
  250. if(flags & EventDisable) {
  251. if(usb.enabled) {
  252. susp_evt(&udev, 0, 0);
  253. usbd_connect(&udev, false);
  254. usb.enabled = false;
  255. usb_request_pending = false;
  256. FURI_LOG_I(TAG, "USB Disable");
  257. }
  258. }
  259. if(flags & EventReset) {
  260. if(usb.enabled) {
  261. usb_request_pending = true;
  262. usb_wait_time = 0;
  263. }
  264. }
  265. if(flags & EventRequest) {
  266. usb_request_pending = false;
  267. }
  268. } else if(usb_request_pending) {
  269. usb_wait_time++;
  270. if(usb_wait_time > 4) {
  271. furi_hal_usb_reinit();
  272. usb_request_pending = false;
  273. }
  274. }
  275. }
  276. return 0;
  277. }