dap_link.c 18 KB

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