dap_link.c 18 KB

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