furi_hal_usb.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. 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. static void furi_hal_usb_tmr_cb(void* context);
  44. /* Low-level init */
  45. void furi_hal_usb_init(void) {
  46. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  47. LL_PWR_EnableVddUSB();
  48. GPIO_InitStruct.Pin = LL_GPIO_PIN_11 | LL_GPIO_PIN_12;
  49. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  50. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  51. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  52. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  53. GPIO_InitStruct.Alternate = LL_GPIO_AF_10;
  54. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  55. usbd_init(&udev, &usbd_hw, USB_EP0_SIZE, ubuf, sizeof(ubuf));
  56. usbd_enable(&udev, true);
  57. usbd_reg_descr(&udev, usb_descriptor_get);
  58. usbd_reg_event(&udev, usbd_evt_susp, susp_evt);
  59. usbd_reg_event(&udev, usbd_evt_wkup, wkup_evt);
  60. // Reset callback will be enabled after first mode change to avoid getting false reset events
  61. usb.enabled = false;
  62. usb.if_cur = NULL;
  63. HAL_NVIC_SetPriority(USB_LP_IRQn, 5, 0);
  64. NVIC_EnableIRQ(USB_LP_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. static void furi_hal_usb_tmr_cb(void* context) {
  113. furi_assert(usb.thread);
  114. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChangeStart);
  115. }
  116. /* Get device / configuration descriptors */
  117. static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) {
  118. const uint8_t dtype = req->wValue >> 8;
  119. const uint8_t dnumber = req->wValue & 0xFF;
  120. const void* desc;
  121. uint16_t len = 0;
  122. if(usb.if_cur == NULL) return usbd_fail;
  123. switch(dtype) {
  124. case USB_DTYPE_DEVICE:
  125. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventRequest);
  126. if(usb.callback != NULL) {
  127. usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.cb_ctx);
  128. }
  129. desc = usb.if_cur->dev_descr;
  130. break;
  131. case USB_DTYPE_CONFIGURATION:
  132. desc = usb.if_cur->cfg_descr;
  133. len = ((struct usb_string_descriptor*)(usb.if_cur->cfg_descr))->wString[0];
  134. break;
  135. case USB_DTYPE_STRING:
  136. if(dnumber == UsbDevLang) {
  137. desc = &dev_lang_desc;
  138. } else if((dnumber == UsbDevManuf) && (usb.if_cur->str_manuf_descr != NULL)) {
  139. desc = usb.if_cur->str_manuf_descr;
  140. } else if((dnumber == UsbDevProduct) && (usb.if_cur->str_prod_descr != NULL)) {
  141. desc = usb.if_cur->str_prod_descr;
  142. } else if((dnumber == UsbDevSerial) && (usb.if_cur->str_serial_descr != NULL)) {
  143. desc = usb.if_cur->str_serial_descr;
  144. } else
  145. return usbd_fail;
  146. break;
  147. default:
  148. return usbd_fail;
  149. }
  150. if(desc == NULL) return usbd_fail;
  151. if(len == 0) {
  152. len = ((struct usb_header_descriptor*)desc)->bLength;
  153. }
  154. *address = (void*)desc;
  155. *length = len;
  156. return usbd_ack;
  157. }
  158. void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) {
  159. usb.callback = cb;
  160. usb.cb_ctx = ctx;
  161. }
  162. static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
  163. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReset);
  164. if(usb.callback != NULL) {
  165. usb.callback(FuriHalUsbStateEventReset, usb.cb_ctx);
  166. }
  167. }
  168. static void susp_evt(usbd_device* dev, uint8_t event, uint8_t 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. if((usb.if_cur != NULL) && (usb.connected == false)) {
  180. usb.connected = true;
  181. usb.if_cur->wakeup(&udev);
  182. furi_hal_power_insomnia_enter();
  183. }
  184. if(usb.callback != NULL) {
  185. usb.callback(FuriHalUsbStateEventWakeup, usb.cb_ctx);
  186. }
  187. }
  188. static int32_t furi_hal_usb_thread(void* context) {
  189. usb.tmr = osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL);
  190. bool usb_request_pending = false;
  191. uint8_t usb_wait_time = 0;
  192. if(usb.if_next != NULL) {
  193. osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
  194. }
  195. while(true) {
  196. uint32_t flags = osThreadFlagsWait(USB_SRV_ALL_EVENTS, osFlagsWaitAny, 500);
  197. if((flags & osFlagsError) == 0) {
  198. if(flags & EventModeChange) {
  199. if(usb.if_next != usb.if_cur) {
  200. if(usb.enabled) { // Disable current interface
  201. susp_evt(&udev, 0, 0);
  202. usbd_connect(&udev, false);
  203. usb.enabled = false;
  204. osTimerStart(usb.tmr, USB_RECONNECT_DELAY);
  205. } else {
  206. flags |= EventModeChangeStart;
  207. }
  208. }
  209. }
  210. if(flags & EventReinit) {
  211. // Temporary disable callback to avoid getting false reset events
  212. usbd_reg_event(&udev, usbd_evt_reset, NULL);
  213. FURI_LOG_I(TAG, "USB Reinit");
  214. susp_evt(&udev, 0, 0);
  215. usbd_connect(&udev, false);
  216. usb.enabled = false;
  217. usbd_enable(&udev, false);
  218. usbd_enable(&udev, true);
  219. usb.if_next = usb.if_cur;
  220. osTimerStart(usb.tmr, USB_RECONNECT_DELAY);
  221. }
  222. if(flags & EventModeChangeStart) { // Second stage of mode change process
  223. if(usb.if_cur != NULL) {
  224. usb.if_cur->deinit(&udev);
  225. }
  226. if(usb.if_next != NULL) {
  227. usb.if_next->init(&udev, usb.if_next, usb.if_ctx);
  228. usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
  229. FURI_LOG_I(TAG, "USB Mode change done");
  230. usb.enabled = true;
  231. usb.if_cur = usb.if_next;
  232. }
  233. }
  234. if(flags & EventEnable) {
  235. if((!usb.enabled) && (usb.if_cur != NULL)) {
  236. usbd_connect(&udev, true);
  237. usb.enabled = true;
  238. FURI_LOG_I(TAG, "USB Enable");
  239. }
  240. }
  241. if(flags & EventDisable) {
  242. if(usb.enabled) {
  243. susp_evt(&udev, 0, 0);
  244. usbd_connect(&udev, false);
  245. usb.enabled = false;
  246. usb_request_pending = false;
  247. FURI_LOG_I(TAG, "USB Disable");
  248. }
  249. }
  250. if(flags & EventReset) {
  251. usb_request_pending = true;
  252. usb_wait_time = 0;
  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. }