furi_hal_usb.c 9.2 KB

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