furi_hal_usb.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <stm32wbxx_ll_pwr.h>
  7. #include <furi.h>
  8. #include "usb.h"
  9. #define TAG "FuriHalUsb"
  10. #define USB_RECONNECT_DELAY 500
  11. typedef struct {
  12. FuriThread* thread;
  13. bool enabled;
  14. bool connected;
  15. bool mode_lock;
  16. FuriHalUsbInterface* if_cur;
  17. FuriHalUsbInterface* if_next;
  18. void* if_ctx;
  19. FuriHalUsbStateCallback callback;
  20. void* cb_ctx;
  21. } UsbSrv;
  22. typedef enum {
  23. EventModeChange = (1 << 0),
  24. EventEnable = (1 << 1),
  25. EventDisable = (1 << 2),
  26. EventReinit = (1 << 3),
  27. EventReset = (1 << 4),
  28. EventRequest = (1 << 5),
  29. EventModeChangeStart = (1 << 6),
  30. } UsbEvent;
  31. #define USB_SRV_ALL_EVENTS \
  32. (EventModeChange | EventEnable | EventDisable | EventReinit | EventReset | EventRequest | \
  33. EventModeChangeStart)
  34. static UsbSrv usb;
  35. static const struct usb_string_descriptor dev_lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
  36. static uint32_t ubuf[0x20];
  37. usbd_device udev;
  38. static int32_t furi_hal_usb_thread(void* context);
  39. static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length);
  40. static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep);
  41. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep);
  42. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep);
  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. NVIC_SetPriority(USB_LP_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 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. bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
  72. if(usb.mode_lock) {
  73. return false;
  74. }
  75. usb.if_next = new_if;
  76. usb.if_ctx = ctx;
  77. if(usb.thread == NULL) {
  78. // Service thread hasn't started yet, so just save interface mode
  79. return true;
  80. }
  81. furi_assert(usb.thread);
  82. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
  83. return true;
  84. }
  85. FuriHalUsbInterface* furi_hal_usb_get_config() {
  86. return usb.if_cur;
  87. }
  88. void furi_hal_usb_lock() {
  89. FURI_LOG_I(TAG, "Mode lock");
  90. usb.mode_lock = true;
  91. }
  92. void furi_hal_usb_unlock() {
  93. FURI_LOG_I(TAG, "Mode unlock");
  94. usb.mode_lock = false;
  95. }
  96. bool furi_hal_usb_is_locked() {
  97. return usb.mode_lock;
  98. }
  99. void furi_hal_usb_disable() {
  100. furi_assert(usb.thread);
  101. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventDisable);
  102. }
  103. void furi_hal_usb_enable() {
  104. furi_assert(usb.thread);
  105. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventEnable);
  106. }
  107. void furi_hal_usb_reinit() {
  108. furi_assert(usb.thread);
  109. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReinit);
  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. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventRequest);
  121. if(usb.callback != NULL) {
  122. usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.cb_ctx);
  123. }
  124. desc = usb.if_cur->dev_descr;
  125. break;
  126. case USB_DTYPE_CONFIGURATION:
  127. desc = usb.if_cur->cfg_descr;
  128. len = ((struct usb_string_descriptor*)(usb.if_cur->cfg_descr))->wString[0];
  129. break;
  130. case USB_DTYPE_STRING:
  131. if(dnumber == UsbDevLang) {
  132. desc = &dev_lang_desc;
  133. } else if((dnumber == UsbDevManuf) && (usb.if_cur->str_manuf_descr != NULL)) {
  134. desc = usb.if_cur->str_manuf_descr;
  135. } else if((dnumber == UsbDevProduct) && (usb.if_cur->str_prod_descr != NULL)) {
  136. desc = usb.if_cur->str_prod_descr;
  137. } else if((dnumber == UsbDevSerial) && (usb.if_cur->str_serial_descr != NULL)) {
  138. desc = usb.if_cur->str_serial_descr;
  139. } else
  140. return usbd_fail;
  141. break;
  142. default:
  143. return usbd_fail;
  144. }
  145. if(desc == NULL) return usbd_fail;
  146. if(len == 0) {
  147. len = ((struct usb_header_descriptor*)desc)->bLength;
  148. }
  149. *address = (void*)desc;
  150. *length = len;
  151. return usbd_ack;
  152. }
  153. void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) {
  154. usb.callback = cb;
  155. usb.cb_ctx = ctx;
  156. }
  157. static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  158. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReset);
  159. if(usb.callback != NULL) {
  160. usb.callback(FuriHalUsbStateEventReset, usb.cb_ctx);
  161. }
  162. }
  163. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  164. if((usb.if_cur != NULL) && (usb.connected == true)) {
  165. usb.connected = false;
  166. usb.if_cur->suspend(&udev);
  167. furi_hal_power_insomnia_exit();
  168. }
  169. if(usb.callback != NULL) {
  170. usb.callback(FuriHalUsbStateEventSuspend, usb.cb_ctx);
  171. }
  172. }
  173. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  174. if((usb.if_cur != NULL) && (usb.connected == false)) {
  175. usb.connected = true;
  176. usb.if_cur->wakeup(&udev);
  177. furi_hal_power_insomnia_enter();
  178. }
  179. if(usb.callback != NULL) {
  180. usb.callback(FuriHalUsbStateEventWakeup, usb.cb_ctx);
  181. }
  182. }
  183. static int32_t furi_hal_usb_thread(void* context) {
  184. bool usb_request_pending = false;
  185. uint8_t usb_wait_time = 0;
  186. FuriHalUsbInterface* if_new = NULL;
  187. FuriHalUsbInterface* if_ctx_new = NULL;
  188. if(usb.if_next != NULL) {
  189. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
  190. }
  191. while(true) {
  192. uint32_t flags = osThreadFlagsWait(USB_SRV_ALL_EVENTS, osFlagsWaitAny, 500);
  193. if((flags & osFlagsError) == 0) {
  194. if(flags & EventModeChange) {
  195. if(usb.if_next != usb.if_cur) {
  196. if_new = usb.if_next;
  197. if_ctx_new = usb.if_ctx;
  198. if(usb.enabled) { // Disable current interface
  199. susp_evt(&udev, 0, 0);
  200. usbd_connect(&udev, false);
  201. usb.enabled = false;
  202. osDelay(USB_RECONNECT_DELAY);
  203. }
  204. flags |= EventModeChangeStart;
  205. }
  206. }
  207. if(flags & EventReinit) {
  208. // Temporary disable callback to avoid getting false reset events
  209. usbd_reg_event(&udev, usbd_evt_reset, NULL);
  210. FURI_LOG_I(TAG, "USB Reinit");
  211. susp_evt(&udev, 0, 0);
  212. usbd_connect(&udev, false);
  213. usb.enabled = false;
  214. usbd_enable(&udev, false);
  215. usbd_enable(&udev, true);
  216. if_new = usb.if_cur;
  217. osDelay(USB_RECONNECT_DELAY);
  218. flags |= EventModeChangeStart;
  219. }
  220. if(flags & EventModeChangeStart) { // Second stage of mode change process
  221. if(usb.if_cur != NULL) {
  222. usb.if_cur->deinit(&udev);
  223. }
  224. if(if_new != NULL) {
  225. if_new->init(&udev, if_new, if_ctx_new);
  226. usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
  227. FURI_LOG_I(TAG, "USB Mode change done");
  228. usb.enabled = true;
  229. }
  230. usb.if_cur = if_new;
  231. }
  232. if(flags & EventEnable) {
  233. if((!usb.enabled) && (usb.if_cur != NULL)) {
  234. usbd_connect(&udev, true);
  235. usb.enabled = true;
  236. FURI_LOG_I(TAG, "USB Enable");
  237. }
  238. }
  239. if(flags & EventDisable) {
  240. if(usb.enabled) {
  241. susp_evt(&udev, 0, 0);
  242. usbd_connect(&udev, false);
  243. usb.enabled = false;
  244. usb_request_pending = false;
  245. FURI_LOG_I(TAG, "USB Disable");
  246. }
  247. }
  248. if(flags & EventReset) {
  249. if(usb.enabled) {
  250. usb_request_pending = true;
  251. usb_wait_time = 0;
  252. }
  253. }
  254. if(flags & EventRequest) {
  255. usb_request_pending = false;
  256. }
  257. } else if(usb_request_pending) {
  258. usb_wait_time++;
  259. if(usb_wait_time > 4) {
  260. furi_hal_usb_reinit();
  261. usb_request_pending = false;
  262. }
  263. }
  264. }
  265. return 0;
  266. }