dap_link.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #include <dap.h>
  2. #include <furi.h>
  3. #include <furi_hal_version.h>
  4. #include <furi_hal_gpio.h>
  5. #include <furi_hal_uart.h>
  6. #include <furi_hal_console.h>
  7. #include <furi_hal_resources.h>
  8. #include <furi_hal_power.h>
  9. #include <stm32wbxx_ll_usart.h>
  10. #include <stm32wbxx_ll_lpuart.h>
  11. #include "dap_link.h"
  12. #include "dap_config.h"
  13. #include "gui/dap_gui.h"
  14. #include "usb/dap_v2_usb.h"
  15. /***************************************************************************/
  16. /****************************** DAP COMMON *********************************/
  17. /***************************************************************************/
  18. struct DapApp {
  19. FuriThread* dap_thread;
  20. FuriThread* cdc_thread;
  21. FuriThread* gui_thread;
  22. DapState state;
  23. DapConfig config;
  24. };
  25. void dap_app_get_state(DapApp* app, DapState* state) {
  26. *state = app->state;
  27. }
  28. #define DAP_PROCESS_THREAD_TICK 500
  29. typedef enum {
  30. DapThreadEventStop = (1 << 0),
  31. } DapThreadEvent;
  32. void dap_thread_send_stop(FuriThread* thread) {
  33. furi_thread_flags_set(furi_thread_get_id(thread), DapThreadEventStop);
  34. }
  35. GpioPin flipper_dap_swclk_pin;
  36. GpioPin flipper_dap_swdio_pin;
  37. GpioPin flipper_dap_reset_pin;
  38. GpioPin flipper_dap_tdo_pin;
  39. GpioPin flipper_dap_tdi_pin;
  40. /***************************************************************************/
  41. /****************************** DAP PROCESS ********************************/
  42. /***************************************************************************/
  43. typedef struct {
  44. uint8_t data[DAP_CONFIG_PACKET_SIZE];
  45. uint8_t size;
  46. } DapPacket;
  47. typedef enum {
  48. DAPThreadEventStop = DapThreadEventStop,
  49. DAPThreadEventRxV1 = (1 << 1),
  50. DAPThreadEventRxV2 = (1 << 2),
  51. DAPThreadEventUSBConnect = (1 << 3),
  52. DAPThreadEventUSBDisconnect = (1 << 4),
  53. DAPThreadEventApplyConfig = (1 << 5),
  54. DAPThreadEventAll = DAPThreadEventStop | DAPThreadEventRxV1 | DAPThreadEventRxV2 |
  55. DAPThreadEventUSBConnect | DAPThreadEventUSBDisconnect |
  56. DAPThreadEventApplyConfig,
  57. } DAPThreadEvent;
  58. #define USB_SERIAL_NUMBER_LEN 16
  59. char usb_serial_number[USB_SERIAL_NUMBER_LEN] = {0};
  60. const char* dap_app_get_serial(DapApp* app) {
  61. UNUSED(app);
  62. return usb_serial_number;
  63. }
  64. static void dap_app_rx1_callback(void* context) {
  65. furi_assert(context);
  66. FuriThreadId thread_id = (FuriThreadId)context;
  67. furi_thread_flags_set(thread_id, DAPThreadEventRxV1);
  68. }
  69. static void dap_app_rx2_callback(void* context) {
  70. furi_assert(context);
  71. FuriThreadId thread_id = (FuriThreadId)context;
  72. furi_thread_flags_set(thread_id, DAPThreadEventRxV2);
  73. }
  74. static void dap_app_usb_state_callback(bool state, void* context) {
  75. furi_assert(context);
  76. FuriThreadId thread_id = (FuriThreadId)context;
  77. if(state) {
  78. furi_thread_flags_set(thread_id, DAPThreadEventUSBConnect);
  79. } else {
  80. furi_thread_flags_set(thread_id, DAPThreadEventUSBDisconnect);
  81. }
  82. }
  83. static void dap_app_process_v1() {
  84. DapPacket tx_packet;
  85. DapPacket rx_packet;
  86. memset(&tx_packet, 0, sizeof(DapPacket));
  87. rx_packet.size = dap_v1_usb_rx(rx_packet.data, DAP_CONFIG_PACKET_SIZE);
  88. dap_process_request(rx_packet.data, rx_packet.size, tx_packet.data, DAP_CONFIG_PACKET_SIZE);
  89. dap_v1_usb_tx(tx_packet.data, DAP_CONFIG_PACKET_SIZE);
  90. }
  91. static void dap_app_process_v2() {
  92. DapPacket tx_packet;
  93. DapPacket rx_packet;
  94. memset(&tx_packet, 0, sizeof(DapPacket));
  95. rx_packet.size = dap_v2_usb_rx(rx_packet.data, DAP_CONFIG_PACKET_SIZE);
  96. size_t len = dap_process_request(
  97. rx_packet.data, rx_packet.size, tx_packet.data, DAP_CONFIG_PACKET_SIZE);
  98. dap_v2_usb_tx(tx_packet.data, len);
  99. }
  100. void dap_app_vendor_cmd(uint8_t cmd) {
  101. // openocd -c "cmsis-dap cmd 81"
  102. if(cmd == 0x01) {
  103. furi_hal_power_reset();
  104. }
  105. }
  106. void dap_app_target_reset() {
  107. FURI_LOG_I("DAP", "Target reset");
  108. }
  109. static void dap_init_gpio(DapSwdPins swd_pins) {
  110. switch(swd_pins) {
  111. case DapSwdPinsPA7PA6:
  112. flipper_dap_swclk_pin = gpio_ext_pa7;
  113. flipper_dap_swdio_pin = gpio_ext_pa6;
  114. break;
  115. case DapSwdPinsPA14PA13:
  116. flipper_dap_swclk_pin = (GpioPin){.port = GPIOA, .pin = LL_GPIO_PIN_14};
  117. flipper_dap_swdio_pin = (GpioPin){.port = GPIOA, .pin = LL_GPIO_PIN_13};
  118. break;
  119. }
  120. flipper_dap_reset_pin = gpio_ext_pa4;
  121. flipper_dap_tdo_pin = gpio_ext_pb3;
  122. flipper_dap_tdi_pin = gpio_ext_pb2;
  123. }
  124. static void dap_deinit_gpio(DapSwdPins swd_pins) {
  125. // setup gpio pins to default state
  126. furi_hal_gpio_init(&flipper_dap_reset_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  127. furi_hal_gpio_init(&flipper_dap_tdo_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  128. furi_hal_gpio_init(&flipper_dap_tdi_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  129. if(DapSwdPinsPA14PA13 == swd_pins) {
  130. // PA14 and PA13 are used by SWD
  131. furi_hal_gpio_init_ex(
  132. &flipper_dap_swclk_pin,
  133. GpioModeAltFunctionPushPull,
  134. GpioPullDown,
  135. GpioSpeedLow,
  136. GpioAltFn0JTCK_SWCLK);
  137. furi_hal_gpio_init_ex(
  138. &flipper_dap_swdio_pin,
  139. GpioModeAltFunctionPushPull,
  140. GpioPullUp,
  141. GpioSpeedVeryHigh,
  142. GpioAltFn0JTMS_SWDIO);
  143. } else {
  144. furi_hal_gpio_init(&flipper_dap_swclk_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  145. furi_hal_gpio_init(&flipper_dap_swdio_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  146. }
  147. }
  148. static int32_t dap_process(void* p) {
  149. DapApp* app = p;
  150. DapState* dap_state = &(app->state);
  151. // allocate resources
  152. FuriHalUsbInterface* usb_config_prev;
  153. app->config.swd_pins = DapSwdPinsPA7PA6;
  154. DapSwdPins swd_pins_prev = app->config.swd_pins;
  155. // init pins
  156. dap_init_gpio(swd_pins_prev);
  157. // init dap
  158. dap_init();
  159. // get name
  160. const char* name = furi_hal_version_get_name_ptr();
  161. if(!name) {
  162. name = "Flipper";
  163. }
  164. snprintf(usb_serial_number, USB_SERIAL_NUMBER_LEN, "DAP_%s", name);
  165. // init usb
  166. usb_config_prev = furi_hal_usb_get_config();
  167. dap_common_usb_alloc_name(usb_serial_number);
  168. dap_common_usb_set_context(furi_thread_get_id(furi_thread_get_current()));
  169. dap_v1_usb_set_rx_callback(dap_app_rx1_callback);
  170. dap_v2_usb_set_rx_callback(dap_app_rx2_callback);
  171. dap_common_usb_set_state_callback(dap_app_usb_state_callback);
  172. furi_hal_usb_set_config(&dap_v2_usb_hid, NULL);
  173. // work
  174. uint32_t events;
  175. while(1) {
  176. events = furi_thread_flags_wait(DAPThreadEventAll, FuriFlagWaitAny, FuriWaitForever);
  177. if(!(events & FuriFlagError)) {
  178. if(events & DAPThreadEventRxV1) {
  179. dap_app_process_v1();
  180. dap_state->dap_counter++;
  181. dap_state->dap_version = DapVersionV1;
  182. }
  183. if(events & DAPThreadEventRxV2) {
  184. dap_app_process_v2();
  185. dap_state->dap_counter++;
  186. dap_state->dap_version = DapVersionV2;
  187. }
  188. if(events & DAPThreadEventUSBConnect) {
  189. dap_state->usb_connected = true;
  190. }
  191. if(events & DAPThreadEventUSBDisconnect) {
  192. dap_state->usb_connected = false;
  193. dap_state->dap_version = DapVersionUnknown;
  194. }
  195. if(events & DAPThreadEventApplyConfig) {
  196. if(swd_pins_prev != app->config.swd_pins) {
  197. dap_deinit_gpio(swd_pins_prev);
  198. swd_pins_prev = app->config.swd_pins;
  199. dap_init_gpio(swd_pins_prev);
  200. }
  201. }
  202. if(events & DAPThreadEventStop) {
  203. break;
  204. }
  205. }
  206. }
  207. // deinit usb
  208. furi_hal_usb_set_config(usb_config_prev, NULL);
  209. dap_common_wait_for_deinit();
  210. dap_common_usb_free_name();
  211. dap_deinit_gpio(swd_pins_prev);
  212. return 0;
  213. }
  214. /***************************************************************************/
  215. /****************************** CDC PROCESS ********************************/
  216. /***************************************************************************/
  217. typedef enum {
  218. CDCThreadEventStop = DapThreadEventStop,
  219. CDCThreadEventUARTRx = (1 << 1),
  220. CDCThreadEventCDCRx = (1 << 2),
  221. CDCThreadEventCDCConfig = (1 << 3),
  222. CDCThreadEventApplyConfig = (1 << 4),
  223. CDCThreadEventAll = CDCThreadEventStop | CDCThreadEventUARTRx | CDCThreadEventCDCRx |
  224. CDCThreadEventCDCConfig | CDCThreadEventApplyConfig,
  225. } CDCThreadEvent;
  226. typedef struct {
  227. FuriStreamBuffer* rx_stream;
  228. FuriThreadId thread_id;
  229. FuriHalUartId uart_id;
  230. struct usb_cdc_line_coding line_coding;
  231. } CDCProcess;
  232. static void cdc_uart_irq_cb(UartIrqEvent ev, uint8_t data, void* ctx) {
  233. CDCProcess* app = ctx;
  234. if(ev == UartIrqEventRXNE) {
  235. furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
  236. furi_thread_flags_set(app->thread_id, CDCThreadEventUARTRx);
  237. }
  238. }
  239. static void cdc_usb_rx_callback(void* context) {
  240. CDCProcess* app = context;
  241. furi_thread_flags_set(app->thread_id, CDCThreadEventCDCRx);
  242. }
  243. static void cdc_usb_control_line_callback(uint8_t state, void* context) {
  244. UNUSED(context);
  245. UNUSED(state);
  246. }
  247. static void cdc_usb_config_callback(struct usb_cdc_line_coding* config, void* context) {
  248. CDCProcess* app = context;
  249. app->line_coding = *config;
  250. furi_thread_flags_set(app->thread_id, CDCThreadEventCDCConfig);
  251. }
  252. static FuriHalUartId cdc_init_uart(
  253. DapUartType type,
  254. DapUartTXRX swap,
  255. uint32_t baudrate,
  256. void (*cb)(UartIrqEvent ev, uint8_t data, void* ctx),
  257. void* ctx) {
  258. FuriHalUartId uart_id = FuriHalUartIdUSART1;
  259. if(baudrate == 0) baudrate = 115200;
  260. switch(type) {
  261. case DapUartTypeUSART1:
  262. uart_id = FuriHalUartIdUSART1;
  263. furi_hal_console_disable();
  264. furi_hal_uart_deinit(uart_id);
  265. if(swap == DapUartTXRXSwap) {
  266. LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_SWAPPED);
  267. } else {
  268. LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_STANDARD);
  269. }
  270. furi_hal_uart_init(uart_id, baudrate);
  271. furi_hal_uart_set_irq_cb(uart_id, cb, ctx);
  272. break;
  273. case DapUartTypeLPUART1:
  274. uart_id = FuriHalUartIdLPUART1;
  275. furi_hal_uart_deinit(uart_id);
  276. if(swap == DapUartTXRXSwap) {
  277. LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_SWAPPED);
  278. } else {
  279. LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_STANDARD);
  280. }
  281. furi_hal_uart_init(uart_id, baudrate);
  282. furi_hal_uart_set_irq_cb(uart_id, cb, ctx);
  283. break;
  284. }
  285. return uart_id;
  286. }
  287. static void cdc_deinit_uart(DapUartType type) {
  288. switch(type) {
  289. case DapUartTypeUSART1:
  290. furi_hal_uart_deinit(FuriHalUartIdUSART1);
  291. LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_STANDARD);
  292. furi_hal_console_init();
  293. break;
  294. case DapUartTypeLPUART1:
  295. furi_hal_uart_deinit(FuriHalUartIdLPUART1);
  296. LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_STANDARD);
  297. break;
  298. }
  299. }
  300. static int32_t cdc_process(void* p) {
  301. DapApp* dap_app = p;
  302. DapState* dap_state = &(dap_app->state);
  303. dap_app->config.uart_pins = DapUartTypeLPUART1;
  304. dap_app->config.uart_swap = DapUartTXRXNormal;
  305. DapUartType uart_pins_prev = dap_app->config.uart_pins;
  306. DapUartTXRX uart_swap_prev = dap_app->config.uart_swap;
  307. CDCProcess* app = malloc(sizeof(CDCProcess));
  308. app->thread_id = furi_thread_get_id(furi_thread_get_current());
  309. app->rx_stream = furi_stream_buffer_alloc(512, 1);
  310. const uint8_t rx_buffer_size = 64;
  311. uint8_t* rx_buffer = malloc(rx_buffer_size);
  312. app->uart_id = cdc_init_uart(
  313. uart_pins_prev, uart_swap_prev, dap_state->cdc_baudrate, cdc_uart_irq_cb, app);
  314. dap_cdc_usb_set_context(app);
  315. dap_cdc_usb_set_rx_callback(cdc_usb_rx_callback);
  316. dap_cdc_usb_set_control_line_callback(cdc_usb_control_line_callback);
  317. dap_cdc_usb_set_config_callback(cdc_usb_config_callback);
  318. uint32_t events;
  319. while(1) {
  320. events = furi_thread_flags_wait(CDCThreadEventAll, FuriFlagWaitAny, FuriWaitForever);
  321. if(!(events & FuriFlagError)) {
  322. if(events & CDCThreadEventCDCConfig) {
  323. if(dap_state->cdc_baudrate != app->line_coding.dwDTERate) {
  324. dap_state->cdc_baudrate = app->line_coding.dwDTERate;
  325. if(dap_state->cdc_baudrate > 0) {
  326. furi_hal_uart_set_br(app->uart_id, dap_state->cdc_baudrate);
  327. }
  328. }
  329. }
  330. if(events & CDCThreadEventUARTRx) {
  331. size_t len =
  332. furi_stream_buffer_receive(app->rx_stream, rx_buffer, rx_buffer_size, 0);
  333. if(len > 0) {
  334. dap_cdc_usb_tx(rx_buffer, len);
  335. }
  336. dap_state->cdc_rx_counter += len;
  337. }
  338. if(events & CDCThreadEventCDCRx) {
  339. size_t len = dap_cdc_usb_rx(rx_buffer, rx_buffer_size);
  340. if(len > 0) {
  341. furi_hal_uart_tx(app->uart_id, rx_buffer, len);
  342. }
  343. dap_state->cdc_tx_counter += len;
  344. }
  345. if(events & CDCThreadEventApplyConfig) {
  346. if(uart_pins_prev != dap_app->config.uart_pins ||
  347. uart_swap_prev != dap_app->config.uart_swap) {
  348. cdc_deinit_uart(uart_pins_prev);
  349. uart_pins_prev = dap_app->config.uart_pins;
  350. uart_swap_prev = dap_app->config.uart_swap;
  351. app->uart_id = cdc_init_uart(
  352. uart_pins_prev,
  353. uart_swap_prev,
  354. dap_state->cdc_baudrate,
  355. cdc_uart_irq_cb,
  356. app);
  357. }
  358. }
  359. if(events & CDCThreadEventStop) {
  360. break;
  361. }
  362. }
  363. }
  364. cdc_deinit_uart(uart_pins_prev);
  365. free(rx_buffer);
  366. furi_stream_buffer_free(app->rx_stream);
  367. free(app);
  368. return 0;
  369. }
  370. /***************************************************************************/
  371. /******************************* MAIN APP **********************************/
  372. /***************************************************************************/
  373. static FuriThread* furi_thread_alloc_ex(
  374. const char* name,
  375. uint32_t stack_size,
  376. FuriThreadCallback callback,
  377. void* context) {
  378. FuriThread* thread = furi_thread_alloc();
  379. furi_thread_set_name(thread, name);
  380. furi_thread_set_stack_size(thread, stack_size);
  381. furi_thread_set_callback(thread, callback);
  382. furi_thread_set_context(thread, context);
  383. return thread;
  384. }
  385. static DapApp* dap_app_alloc() {
  386. DapApp* dap_app = malloc(sizeof(DapApp));
  387. dap_app->dap_thread = furi_thread_alloc_ex("DAP Process", 1024, dap_process, dap_app);
  388. dap_app->cdc_thread = furi_thread_alloc_ex("DAP CDC", 1024, cdc_process, dap_app);
  389. dap_app->gui_thread = furi_thread_alloc_ex("DAP GUI", 1024, dap_gui_thread, dap_app);
  390. return dap_app;
  391. }
  392. static void dap_app_free(DapApp* dap_app) {
  393. furi_assert(dap_app);
  394. furi_thread_free(dap_app->dap_thread);
  395. furi_thread_free(dap_app->cdc_thread);
  396. furi_thread_free(dap_app->gui_thread);
  397. free(dap_app);
  398. }
  399. static DapApp* app_handle = NULL;
  400. void dap_app_disconnect() {
  401. app_handle->state.dap_mode = DapModeDisconnected;
  402. }
  403. void dap_app_connect_swd() {
  404. app_handle->state.dap_mode = DapModeSWD;
  405. }
  406. void dap_app_connect_jtag() {
  407. app_handle->state.dap_mode = DapModeJTAG;
  408. }
  409. void dap_app_set_config(DapApp* app, DapConfig* config) {
  410. app->config = *config;
  411. furi_thread_flags_set(furi_thread_get_id(app->dap_thread), DAPThreadEventApplyConfig);
  412. furi_thread_flags_set(furi_thread_get_id(app->cdc_thread), CDCThreadEventApplyConfig);
  413. }
  414. DapConfig* dap_app_get_config(DapApp* app) {
  415. return &app->config;
  416. }
  417. int32_t dap_link_app(void* p) {
  418. UNUSED(p);
  419. // alloc app
  420. DapApp* app = dap_app_alloc();
  421. app_handle = app;
  422. furi_thread_start(app->dap_thread);
  423. furi_thread_start(app->cdc_thread);
  424. furi_thread_start(app->gui_thread);
  425. // wait until gui thread is finished
  426. furi_thread_join(app->gui_thread);
  427. // send stop event to threads
  428. dap_thread_send_stop(app->dap_thread);
  429. dap_thread_send_stop(app->cdc_thread);
  430. // wait for threads to stop
  431. furi_thread_join(app->dap_thread);
  432. furi_thread_join(app->cdc_thread);
  433. // free app
  434. dap_app_free(app);
  435. return 0;
  436. }