furi_hal_usb.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_EnableIRQ(USB_LP_IRQn);
  63. usb.thread = furi_thread_alloc();
  64. furi_thread_set_name(usb.thread, "UsbDriver");
  65. furi_thread_set_stack_size(usb.thread, 1024);
  66. furi_thread_set_callback(usb.thread, furi_hal_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. osThreadFlagsSet(furi_thread_get_thread_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. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventDisable);
  101. }
  102. void furi_hal_usb_enable() {
  103. furi_assert(usb.thread);
  104. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventEnable);
  105. }
  106. void furi_hal_usb_reinit() {
  107. furi_assert(usb.thread);
  108. osThreadFlagsSet(furi_thread_get_thread_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. osThreadFlagsSet(furi_thread_get_thread_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. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReset);
  158. if(usb.callback != NULL) {
  159. usb.callback(FuriHalUsbStateEventReset, usb.cb_ctx);
  160. }
  161. }
  162. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  163. if((usb.if_cur != NULL) && (usb.connected == true)) {
  164. usb.connected = false;
  165. usb.if_cur->suspend(&udev);
  166. furi_hal_power_insomnia_exit();
  167. }
  168. if(usb.callback != NULL) {
  169. usb.callback(FuriHalUsbStateEventSuspend, usb.cb_ctx);
  170. }
  171. }
  172. static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  173. if((usb.if_cur != NULL) && (usb.connected == false)) {
  174. usb.connected = true;
  175. usb.if_cur->wakeup(&udev);
  176. furi_hal_power_insomnia_enter();
  177. }
  178. if(usb.callback != NULL) {
  179. usb.callback(FuriHalUsbStateEventWakeup, usb.cb_ctx);
  180. }
  181. }
  182. static int32_t furi_hal_usb_thread(void* context) {
  183. bool usb_request_pending = false;
  184. uint8_t usb_wait_time = 0;
  185. FuriHalUsbInterface* if_new = NULL;
  186. FuriHalUsbInterface* if_ctx_new = NULL;
  187. if(usb.if_next != NULL) {
  188. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
  189. }
  190. while(true) {
  191. uint32_t flags = osThreadFlagsWait(USB_SRV_ALL_EVENTS, osFlagsWaitAny, 500);
  192. if((flags & osFlagsError) == 0) {
  193. if(flags & EventModeChange) {
  194. if(usb.if_next != usb.if_cur) {
  195. if_new = usb.if_next;
  196. if_ctx_new = usb.if_ctx;
  197. if(usb.enabled) { // Disable current interface
  198. susp_evt(&udev, 0, 0);
  199. usbd_connect(&udev, false);
  200. usb.enabled = false;
  201. osDelay(USB_RECONNECT_DELAY);
  202. }
  203. flags |= EventModeChangeStart;
  204. }
  205. }
  206. if(flags & EventReinit) {
  207. // Temporary disable callback to avoid getting false reset events
  208. usbd_reg_event(&udev, usbd_evt_reset, NULL);
  209. FURI_LOG_I(TAG, "USB Reinit");
  210. susp_evt(&udev, 0, 0);
  211. usbd_connect(&udev, false);
  212. usb.enabled = false;
  213. usbd_enable(&udev, false);
  214. usbd_enable(&udev, true);
  215. if_new = usb.if_cur;
  216. osDelay(USB_RECONNECT_DELAY);
  217. flags |= EventModeChangeStart;
  218. }
  219. if(flags & EventModeChangeStart) { // Second stage of mode change process
  220. if(usb.if_cur != NULL) {
  221. usb.if_cur->deinit(&udev);
  222. }
  223. if(if_new != NULL) {
  224. if_new->init(&udev, if_new, if_ctx_new);
  225. usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
  226. FURI_LOG_I(TAG, "USB Mode change done");
  227. usb.enabled = true;
  228. }
  229. usb.if_cur = if_new;
  230. }
  231. if(flags & EventEnable) {
  232. if((!usb.enabled) && (usb.if_cur != NULL)) {
  233. usbd_connect(&udev, true);
  234. usb.enabled = true;
  235. FURI_LOG_I(TAG, "USB Enable");
  236. }
  237. }
  238. if(flags & EventDisable) {
  239. if(usb.enabled) {
  240. susp_evt(&udev, 0, 0);
  241. usbd_connect(&udev, false);
  242. usb.enabled = false;
  243. usb_request_pending = false;
  244. FURI_LOG_I(TAG, "USB Disable");
  245. }
  246. }
  247. if(flags & EventReset) {
  248. if(usb.enabled) {
  249. usb_request_pending = true;
  250. usb_wait_time = 0;
  251. }
  252. }
  253. if(flags & EventRequest) {
  254. usb_request_pending = false;
  255. }
  256. } else if(usb_request_pending) {
  257. usb_wait_time++;
  258. if(usb_wait_time > 4) {
  259. furi_hal_usb_reinit();
  260. usb_request_pending = false;
  261. }
  262. }
  263. }
  264. return 0;
  265. }