swd_probe_app.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <gui/elements.h>
  4. #include <input/input.h>
  5. #include <stdlib.h>
  6. #include <dolphin/dolphin.h>
  7. #include <notification/notification.h>
  8. #include <notification/notification_messages.h>
  9. #include "swd_probe_app.h"
  10. #include "jep106.h"
  11. #define COUNT(x) (sizeof(x) / sizeof((x)[0]))
  12. static const GpioPin* gpios[] = {
  13. &gpio_ext_pc0,
  14. &gpio_ext_pc1,
  15. &gpio_ext_pc3,
  16. &gpio_ext_pb2,
  17. &gpio_ext_pb3,
  18. &gpio_ext_pa4,
  19. &gpio_ext_pa6,
  20. &gpio_ext_pa7};
  21. static const char* gpio_names[] = {"PC0", "PC1", "PC3", "PB2", "PB3", "PA4", "PA6", "PA7"};
  22. /* bit set: clock, else data */
  23. static const uint8_t gpio_direction_mask[6] =
  24. {0b10101010, 0b01010101, 0b11001100, 0b00110011, 0b11110000, 0b00001111};
  25. static const uint8_t gpio_direction_ind[6] = "-\\||/-";
  26. static bool has_multiple_bits(uint8_t x) {
  27. return (x & (x - 1)) != 0;
  28. }
  29. static int get_bit_num(uint8_t x) {
  30. return __builtin_ctz(x);
  31. }
  32. static const char* gpio_name(uint8_t mask) {
  33. if(has_multiple_bits(mask)) {
  34. return "Pxx";
  35. }
  36. int io = get_bit_num(mask);
  37. if(io >= COUNT(gpio_names)) {
  38. return "Pxx";
  39. }
  40. return gpio_names[io];
  41. }
  42. static void swd_configure_pins(AppFSM* const ctx, bool output) {
  43. if(ctx->mode_page != 0 && ctx->io_num_swc < 8 && ctx->io_num_swd < 8) {
  44. furi_hal_gpio_init(
  45. gpios[ctx->io_num_swc], GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  46. if(!output) {
  47. furi_hal_gpio_init(
  48. gpios[ctx->io_num_swd], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  49. } else {
  50. furi_hal_gpio_init(
  51. gpios[ctx->io_num_swd], GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh);
  52. }
  53. return;
  54. }
  55. for(int io = 0; io < 8; io++) {
  56. uint8_t bitmask = 1 << io;
  57. /* if neither candidate for SWC nor SWD then skip */
  58. if(!(ctx->io_swc & bitmask) && !(ctx->io_swd & bitmask)) {
  59. furi_hal_gpio_init(gpios[io], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  60. continue;
  61. }
  62. if(ctx->current_mask & bitmask) {
  63. /* set for clock */
  64. furi_hal_gpio_init(gpios[io], GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  65. } else {
  66. /* set for data */
  67. if(!output) {
  68. furi_hal_gpio_init(gpios[io], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  69. } else {
  70. furi_hal_gpio_init(
  71. gpios[io], GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh);
  72. }
  73. }
  74. }
  75. }
  76. static void swd_set_clock(AppFSM* const ctx, const uint8_t level) {
  77. if(ctx->mode_page != 0 && ctx->io_num_swc < 8) {
  78. furi_hal_gpio_write(gpios[ctx->io_num_swc], level);
  79. return;
  80. }
  81. for(int io = 0; io < 8; io++) {
  82. uint8_t bitmask = 1 << io;
  83. /* if no candidate for SWC then skip */
  84. if(!(ctx->io_swc & bitmask)) {
  85. continue;
  86. }
  87. if(ctx->current_mask & bitmask) {
  88. furi_hal_gpio_write(gpios[io], level);
  89. }
  90. }
  91. }
  92. static void swd_set_data(AppFSM* const ctx, const uint8_t level) {
  93. if(ctx->mode_page != 0 && ctx->io_num_swd < 8) {
  94. furi_hal_gpio_write(gpios[ctx->io_num_swd], level);
  95. return;
  96. }
  97. for(int io = 0; io < 8; io++) {
  98. uint8_t bitmask = 1 << io;
  99. /* if no candidate for SWD then skip */
  100. if(!(ctx->io_swd & bitmask)) {
  101. continue;
  102. }
  103. if(!(ctx->current_mask & bitmask)) {
  104. furi_hal_gpio_write(gpios[io], level);
  105. }
  106. }
  107. }
  108. static uint8_t swd_get_data(AppFSM* const ctx) {
  109. if(ctx->mode_page != 0 && ctx->io_num_swd < 8) {
  110. return furi_hal_gpio_read(gpios[ctx->io_num_swd]);
  111. }
  112. uint8_t bits = 0;
  113. for(int io = 0; io < 8; io++) {
  114. uint8_t bitmask = 1 << io;
  115. /* if no candidate for SWD then skip */
  116. if(!(ctx->io_swd & bitmask)) {
  117. continue;
  118. }
  119. bits |= furi_hal_gpio_read(gpios[io]) ? bitmask : 0;
  120. }
  121. return bits;
  122. }
  123. static void swd_write_bit(AppFSM* const ctx, bool level) {
  124. swd_set_clock(ctx, 0);
  125. swd_set_data(ctx, level);
  126. furi_delay_us(SWD_DELAY_US);
  127. swd_set_clock(ctx, 1);
  128. furi_delay_us(SWD_DELAY_US);
  129. swd_set_clock(ctx, 0);
  130. }
  131. static uint8_t swd_read_bit(AppFSM* const ctx) {
  132. swd_set_clock(ctx, 1);
  133. furi_delay_us(SWD_DELAY_US);
  134. swd_set_clock(ctx, 0);
  135. uint8_t bits = swd_get_data(ctx);
  136. furi_delay_us(SWD_DELAY_US);
  137. swd_set_clock(ctx, 1);
  138. return bits;
  139. }
  140. /* send a byte or less LSB-first */
  141. static void swd_write_byte(AppFSM* const ctx, const uint8_t data, size_t bits) {
  142. for(size_t pos = 0; pos < bits; pos++) {
  143. swd_write_bit(ctx, data & (1 << pos));
  144. }
  145. }
  146. /* send a sequence of bytes LSB-first */
  147. static void swd_write(AppFSM* const ctx, const uint8_t* data, size_t bits) {
  148. size_t byte_pos = 0;
  149. while(bits > 0) {
  150. size_t remain = (bits > 8) ? 8 : bits;
  151. swd_write_byte(ctx, data[byte_pos++], remain);
  152. bits -= remain;
  153. }
  154. }
  155. static uint8_t swd_transfer(AppFSM* const ctx, bool ap, bool write, uint8_t a23, uint32_t* data) {
  156. swd_set_data(ctx, false);
  157. swd_configure_pins(ctx, true);
  158. swd_write_byte(ctx, 0, 8);
  159. uint8_t request[] = {0};
  160. request[0] |= 0x01; /* start bit*/
  161. request[0] |= ap ? 0x02 : 0; /* APnDP */
  162. request[0] |= write ? 0 : 0x04; /* operation */
  163. request[0] |= (a23 & 0x01) ? 0x08 : 0; /* A[2:3] */
  164. request[0] |= (a23 & 0x02) ? 0x10 : 0; /* A[2:3] */
  165. request[0] |= 0x80; /* park bit */
  166. request[0] |= __builtin_parity(request[0]) ? 0x20 : 0; /* parity */
  167. swd_write(ctx, request, sizeof(request) * 8);
  168. /* turnaround cycle */
  169. swd_configure_pins(ctx, false);
  170. uint8_t ack = 0;
  171. /* receive 3 ACK bits */
  172. for(int pos = 0; pos < 3; pos++) {
  173. ack >>= 1;
  174. ack |= swd_read_bit(ctx) ? 0x04 : 0;
  175. }
  176. /* force ABORT/CTRL to always work */
  177. if(!ap && a23 == 0) {
  178. ack = 1;
  179. }
  180. if(ack != 0x01) {
  181. return ack;
  182. }
  183. if(write) {
  184. swd_write_bit(ctx, 0);
  185. swd_configure_pins(ctx, true);
  186. /* send 32 WDATA bits */
  187. for(int pos = 0; pos < 32; pos++) {
  188. swd_write_bit(ctx, *data & (1 << pos));
  189. }
  190. /* send parity bit */
  191. swd_write_bit(ctx, __builtin_parity(*data));
  192. } else {
  193. *data = 0;
  194. /* receive 32 RDATA bits */
  195. for(int pos = 0; pos < 32; pos++) {
  196. *data >>= 1;
  197. *data |= swd_read_bit(ctx) ? 0x80000000 : 0;
  198. }
  199. /* receive parity bit */
  200. bool parity = swd_read_bit(ctx);
  201. if(parity != __builtin_parity(*data)) {
  202. return 8;
  203. }
  204. }
  205. swd_set_data(ctx, false);
  206. swd_configure_pins(ctx, true);
  207. return ack;
  208. }
  209. /* A line reset is achieved by holding the data signal HIGH for at least 50 clock cycles, followed by at least two idle cycles. */
  210. static void swd_line_reset(AppFSM* const ctx) {
  211. for(int bitcount = 0; bitcount < 50; bitcount += 8) {
  212. swd_write_byte(ctx, 0xFF, 8);
  213. }
  214. swd_write_byte(ctx, 0, 8);
  215. }
  216. static void swd_abort(AppFSM* const ctx) {
  217. uint32_t dpidr;
  218. /* first reset the line */
  219. swd_line_reset(ctx);
  220. swd_transfer(ctx, false, false, 0, &dpidr);
  221. uint32_t abort = 0x0E;
  222. swd_transfer(ctx, false, true, 0, &abort);
  223. }
  224. static void swd_abort_simple(AppFSM* const ctx) {
  225. uint32_t abort = 0x0E;
  226. swd_transfer(ctx, false, true, 0, &abort);
  227. uint32_t dpidr;
  228. if(swd_transfer(ctx, false, false, 0, &dpidr) != 1) {
  229. swd_abort(ctx);
  230. }
  231. }
  232. static uint8_t swd_select(AppFSM* const ctx, uint8_t ap_sel, uint8_t ap_bank, uint8_t dp_bank) {
  233. uint32_t bank_reg = (ap_sel << 24) | ((ap_bank & 0x0F) << 4) | (dp_bank & 0x0F);
  234. uint8_t ret = swd_transfer(ctx, false, true, 2, &bank_reg);
  235. if(ret != 1) {
  236. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_select: failed: %d", ret);
  237. }
  238. return ret;
  239. }
  240. static uint8_t
  241. swd_read_dpbank(AppFSM* const ctx, uint8_t dp_off, uint8_t dp_bank, uint32_t* data) {
  242. /* select target bank */
  243. uint8_t ret = swd_select(ctx, 0, 0, dp_bank);
  244. if(ret != 1) {
  245. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_dpbank: swd_select failed");
  246. return ret;
  247. }
  248. /* read data from it */
  249. *data = 0;
  250. ret = swd_transfer(ctx, false, false, dp_off, data);
  251. if(ret != 1) {
  252. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_dpbank: failed: %d", ret);
  253. return ret;
  254. }
  255. /* reset bank to zero again */
  256. ret = swd_select(ctx, 0, 0, 0);
  257. return ret;
  258. }
  259. static uint8_t swd_read_ap(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t* data) {
  260. uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
  261. if(ret != 1) {
  262. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_ap: swd_select failed");
  263. return ret;
  264. }
  265. ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
  266. *data = 0;
  267. ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
  268. if(ret != 1) {
  269. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_ap: failed: %d", ret);
  270. return ret;
  271. }
  272. return ret;
  273. }
  274. static uint8_t swd_read_ap_single(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t* data) {
  275. uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
  276. if(ret != 1) {
  277. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_ap_single: swd_select failed");
  278. return ret;
  279. }
  280. *data = 0;
  281. ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
  282. if(ret != 1) {
  283. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_ap_single: failed: %d", ret);
  284. return ret;
  285. }
  286. return ret;
  287. }
  288. static uint8_t swd_write_ap(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t data) {
  289. uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
  290. if(ret != 1) {
  291. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_write_ap: swd_select failed");
  292. return ret;
  293. }
  294. ret = swd_transfer(ctx, true, true, (ap_off >> 2) & 3, &data);
  295. if(ret != 1) {
  296. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_write_ap: failed: %d", ret);
  297. return ret;
  298. }
  299. return ret;
  300. }
  301. static uint8_t swd_read_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t* data) {
  302. uint8_t ret = 0;
  303. ret |= swd_write_ap(ctx, ap, MEMAP_CSW, 0x03000012);
  304. ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
  305. ret |= swd_read_ap(ctx, ap, MEMAP_DRW, data);
  306. if(ret != 1) {
  307. swd_abort(ctx);
  308. }
  309. return ret;
  310. }
  311. static uint8_t swd_read_memory_cont(AppFSM* const ctx, uint8_t ap, uint32_t* data) {
  312. uint8_t ret = 0;
  313. ret |= swd_read_ap_single(ctx, ap, MEMAP_DRW, data);
  314. if(ret != 1) {
  315. swd_abort(ctx);
  316. }
  317. return ret;
  318. }
  319. static uint32_t swd_detect(AppFSM* const ctx) {
  320. swd_set_data(ctx, false);
  321. swd_configure_pins(ctx, true);
  322. uint8_t data[] = {0xA5};
  323. swd_write(ctx, data, sizeof(data) * 8);
  324. /* turnaround cycle */
  325. swd_configure_pins(ctx, false);
  326. uint8_t ack_bits[3];
  327. uint8_t rdata[32];
  328. /* receive 3 ACK bits */
  329. for(int pos = 0; pos < 3; pos++) {
  330. ack_bits[pos] = swd_read_bit(ctx);
  331. }
  332. /* receive 32 RDATA bits */
  333. for(int pos = 0; pos < 32; pos++) {
  334. rdata[pos] = swd_read_bit(ctx);
  335. }
  336. /* receive parity bit */
  337. uint8_t parity = swd_read_bit(ctx);
  338. for(int io = 0; io < 8; io++) {
  339. uint8_t bitmask = 1 << io;
  340. /* skip if it's a clock */
  341. if(ctx->current_mask & bitmask) {
  342. continue;
  343. }
  344. uint8_t ack = 0;
  345. for(int pos = 0; pos < 3; pos++) {
  346. ack >>= 1;
  347. ack |= (ack_bits[pos] & bitmask) ? 4 : 0;
  348. }
  349. uint32_t dpidr = 0;
  350. for(int pos = 0; pos < 32; pos++) {
  351. dpidr >>= 1;
  352. dpidr |= (rdata[pos] & bitmask) ? 0x80000000 : 0;
  353. }
  354. if(ack == 1 && dpidr != 0 && dpidr != 0xFFFFFFFF) {
  355. bool received_parity = (parity & bitmask);
  356. if(__builtin_parity(dpidr) == received_parity) {
  357. ctx->dp_regs.dpidr = dpidr;
  358. ctx->dp_regs.dpidr_ok = true;
  359. ctx->detected = true;
  360. ctx->io_swd = bitmask;
  361. ctx->io_swc &= ctx->current_mask;
  362. furi_log_print_format(
  363. FuriLogLevelDefault,
  364. TAG,
  365. "swd_detect: data: %08lX, io_swd %02X, io_swc %02X",
  366. dpidr,
  367. ctx->io_swd,
  368. ctx->io_swc);
  369. if(!has_multiple_bits(ctx->io_swc)) {
  370. ctx->io_num_swd = get_bit_num(ctx->io_swd);
  371. ctx->io_num_swc = get_bit_num(ctx->io_swc);
  372. }
  373. }
  374. }
  375. }
  376. swd_set_data(ctx, false);
  377. swd_configure_pins(ctx, true);
  378. return 0;
  379. }
  380. static void swd_scan(AppFSM* const ctx) {
  381. /* To switch SWJ-DP from JTAG to SWD operation:
  382. 1. Send at least 50 SWCLKTCK cycles with SWDIOTMS HIGH. This ensures that the current interface is in its reset state. The JTAG interface only detects the 16-bit JTAG-to-SWD sequence starting from the Test-Logic-Reset state.
  383. 2. Send the 16-bit JTAG-to-SWD select sequence 0x79e7 on SWDIOTMS.
  384. 3. Send at least 50 SWCLKTCK cycles with SWDIOTMS HIGH. This ensures that if SWJ-DP was already in SWD operation before sending the select sequence, the SWD interface enters line reset state.
  385. */
  386. swd_configure_pins(ctx, true);
  387. /* reset JTAG interface */
  388. for(int bitcount = 0; bitcount < 50; bitcount += 8) {
  389. swd_write_byte(ctx, 0xFF, 8);
  390. }
  391. /* Send the 16-bit JTAG-to-SWD select sequence */
  392. swd_write_byte(ctx, 0x9E, 8);
  393. swd_write_byte(ctx, 0xE7, 8);
  394. /* resynchronize SWD */
  395. swd_line_reset(ctx);
  396. swd_detect(ctx);
  397. }
  398. static void render_callback(Canvas* const canvas, void* cb_ctx) {
  399. AppFSM* ctx = acquire_mutex((ValueMutex*)cb_ctx, 25);
  400. if(ctx == NULL) {
  401. return;
  402. }
  403. char buffer[64];
  404. int y = 10;
  405. canvas_draw_frame(canvas, 0, 0, 128, 64);
  406. canvas_set_font(canvas, FontPrimary);
  407. if(ctx->detected_device) {
  408. /* if seen less than a quarter second ago */
  409. switch(ctx->mode_page) {
  410. case 0: {
  411. if((ctx->detected_timeout + TIMER_HZ / 4) >= TIMER_HZ * TIMEOUT) {
  412. snprintf(buffer, sizeof(buffer), "FOUND!");
  413. } else {
  414. /* if it was seen more than a quarter second ago, show countdown */
  415. snprintf(
  416. buffer,
  417. sizeof(buffer),
  418. "FOUND! (%lus)",
  419. (ctx->detected_timeout / TIMER_HZ) + 1);
  420. }
  421. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, buffer);
  422. y += 10;
  423. canvas_set_font(canvas, FontKeyboard);
  424. snprintf(
  425. buffer,
  426. sizeof(buffer),
  427. "SWC/SWD: %s/%s",
  428. gpio_name(ctx->io_swc),
  429. gpio_name(ctx->io_swd));
  430. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  431. y += 10;
  432. snprintf(buffer, sizeof(buffer), "DPIDR 0x%08lX", ctx->dp_regs.dpidr);
  433. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  434. y += 10;
  435. snprintf(
  436. buffer,
  437. sizeof(buffer),
  438. "Part %02X Rev %X DAPv%d",
  439. ctx->dpidr_info.partno,
  440. ctx->dpidr_info.revision,
  441. ctx->dpidr_info.version);
  442. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  443. y += 10;
  444. canvas_set_font(canvas, FontSecondary);
  445. snprintf(buffer, sizeof(buffer), "%s", jep106_manufacturer(ctx->dpidr_info.designer));
  446. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  447. y += 10;
  448. canvas_set_font(canvas, FontSecondary);
  449. elements_button_right(canvas, "DP Regs");
  450. break;
  451. }
  452. case 1: {
  453. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "DP Registers");
  454. y += 10;
  455. canvas_set_font(canvas, FontKeyboard);
  456. if(ctx->dp_regs.dpidr_ok) {
  457. snprintf(buffer, sizeof(buffer), "DPIDR %08lX", ctx->dp_regs.dpidr);
  458. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  459. }
  460. y += 10;
  461. if(ctx->dp_regs.ctrlstat_ok) {
  462. snprintf(buffer, sizeof(buffer), "CTRL %08lX", ctx->dp_regs.ctrlstat);
  463. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  464. }
  465. y += 10;
  466. if(ctx->dp_regs.targetid_ok) {
  467. snprintf(buffer, sizeof(buffer), "TGTID %08lX", ctx->dp_regs.targetid);
  468. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  469. }
  470. y += 10;
  471. if(ctx->dp_regs.eventstat_ok) {
  472. snprintf(buffer, sizeof(buffer), "EVTST %08lX", ctx->dp_regs.eventstat);
  473. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  474. }
  475. y += 10;
  476. canvas_set_font(canvas, FontSecondary);
  477. elements_button_left(canvas, "Scan");
  478. elements_button_right(canvas, "DPID");
  479. break;
  480. }
  481. case 2: {
  482. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "DP ID Register");
  483. y += 10;
  484. canvas_set_font(canvas, FontKeyboard);
  485. if(ctx->dpidr_info.version != 2) {
  486. snprintf(buffer, sizeof(buffer), "TARGETID not supported");
  487. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  488. y += 10;
  489. } else {
  490. if(ctx->dp_regs.targetid_ok) {
  491. snprintf(buffer, sizeof(buffer), "TGTID %08lX", ctx->dp_regs.targetid);
  492. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  493. y += 10;
  494. snprintf(buffer, sizeof(buffer), "Part No. %04X", ctx->targetid_info.partno);
  495. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  496. y += 10;
  497. snprintf(
  498. buffer,
  499. sizeof(buffer),
  500. "%s",
  501. jep106_manufacturer(ctx->targetid_info.designer));
  502. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  503. y += 10;
  504. }
  505. }
  506. canvas_set_font(canvas, FontSecondary);
  507. elements_button_left(canvas, "DP Regs");
  508. elements_button_right(canvas, "APs");
  509. break;
  510. }
  511. case 3: {
  512. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "AP Menu");
  513. y += 10;
  514. canvas_set_font(canvas, FontKeyboard);
  515. char state = ' ';
  516. if(ctx->ap_pos >= ctx->ap_scanned && ctx->ap_pos <= ctx->ap_scanned + 10) {
  517. state = '*';
  518. }
  519. if(!ctx->apidr_info[ctx->ap_pos].ok) {
  520. snprintf(buffer, sizeof(buffer), "[%d]%c<none>", ctx->ap_pos, state);
  521. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  522. y += 10;
  523. if(ctx->ap_pos == 0) {
  524. for(int pos = 0; pos < COUNT(ctx->apidr_info); pos++) {
  525. if(ctx->apidr_info[pos].ok) {
  526. ctx->ap_pos = pos;
  527. }
  528. }
  529. }
  530. } else {
  531. const char* class = "";
  532. switch(ctx->apidr_info[ctx->ap_pos].class) {
  533. case 0:
  534. class = "und";
  535. break;
  536. case 1:
  537. class = "COM";
  538. break;
  539. case 8:
  540. class = "MEM";
  541. break;
  542. default:
  543. class = "unk";
  544. break;
  545. }
  546. const char* types[] = {
  547. "COM-AP",
  548. "AHB3",
  549. "APB2 or APB3",
  550. "Type unknown",
  551. "AXI3 or AXI4",
  552. "AHB5",
  553. "APB4 and APB5",
  554. "AXI5",
  555. "AHB5 enh.",
  556. };
  557. const char* type = "Type unk";
  558. if(ctx->apidr_info[ctx->ap_pos].type < COUNT(types)) {
  559. type = types[ctx->apidr_info[ctx->ap_pos].type];
  560. }
  561. snprintf(buffer, sizeof(buffer), "[%d]%c%s, %s", ctx->ap_pos, state, class, type);
  562. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  563. y += 10;
  564. snprintf(
  565. buffer, sizeof(buffer), "Base 0x%08lX", ctx->apidr_info[ctx->ap_pos].base);
  566. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  567. y += 10;
  568. snprintf(
  569. buffer,
  570. sizeof(buffer),
  571. "Rev %d Var %d",
  572. ctx->apidr_info[ctx->ap_pos].revision,
  573. ctx->apidr_info[ctx->ap_pos].variant);
  574. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  575. y += 10;
  576. snprintf(
  577. buffer,
  578. sizeof(buffer),
  579. "%s",
  580. jep106_manufacturer(ctx->apidr_info[ctx->ap_pos].designer));
  581. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  582. y += 10;
  583. elements_button_center(canvas, "Show");
  584. }
  585. canvas_set_font(canvas, FontSecondary);
  586. elements_button_left(canvas, "DPID");
  587. elements_scrollbar_pos(
  588. canvas, 4, 10, 40, ctx->ap_pos / 32, COUNT(ctx->apidr_info) / 32);
  589. } break;
  590. /* hex dump view */
  591. case 4: {
  592. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Hex dump");
  593. y += 10;
  594. canvas_set_font(canvas, FontKeyboard);
  595. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, "Addr:");
  596. snprintf(buffer, sizeof(buffer), "%08lX", ctx->hex_addr);
  597. canvas_draw_str_aligned(canvas, 38, y, AlignLeft, AlignBottom, buffer);
  598. uint32_t font_width = canvas_glyph_width(canvas, '0');
  599. uint32_t x = 37 + (7 - ctx->hex_select) * font_width;
  600. /* draw selection */
  601. canvas_draw_line(canvas, x, y + 1, x + font_width, y + 1);
  602. y += 10;
  603. uint32_t byte_num = 0;
  604. for(int line = 0; line < 4; line++) {
  605. uint32_t x_pos = 5;
  606. for(int byte_pos = 0; byte_pos < 8; byte_pos++) {
  607. if(ctx->hex_buffer_valid[byte_num / 4]) {
  608. snprintf(buffer, sizeof(buffer), "%02X", ctx->hex_buffer[byte_num]);
  609. } else {
  610. snprintf(buffer, sizeof(buffer), "--");
  611. }
  612. byte_num++;
  613. canvas_draw_str_aligned(canvas, x_pos, y, AlignLeft, AlignBottom, buffer);
  614. x_pos += font_width * 2 + font_width / 2;
  615. }
  616. y += 10;
  617. }
  618. break;
  619. }
  620. }
  621. } else {
  622. snprintf(
  623. buffer, sizeof(buffer), "Searching... %c", gpio_direction_ind[ctx->current_mask_id]);
  624. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, buffer);
  625. y += 10;
  626. }
  627. release_mutex((ValueMutex*)cb_ctx, ctx);
  628. }
  629. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  630. furi_assert(event_queue);
  631. AppEvent event = {.type = EventKeyPress, .input = *input_event};
  632. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  633. }
  634. static void timer_tick_callback(FuriMessageQueue* event_queue) {
  635. furi_assert(event_queue);
  636. AppEvent event = {.type = EventTimerTick};
  637. furi_message_queue_put(event_queue, &event, 0);
  638. }
  639. static void app_init(AppFSM* const ctx, FuriMessageQueue* event_queue) {
  640. ctx->_event_queue = event_queue;
  641. FuriTimer* timer =
  642. furi_timer_alloc(timer_tick_callback, FuriTimerTypePeriodic, ctx->_event_queue);
  643. furi_timer_start(timer, furi_kernel_get_tick_frequency() / TIMER_HZ);
  644. ctx->_timer = timer;
  645. ctx->current_mask_id = 0;
  646. ctx->current_mask = gpio_direction_mask[ctx->current_mask_id];
  647. ctx->io_swd = 0xFF;
  648. ctx->io_swc = 0xFF;
  649. ctx->hex_addr = 0x40002800;
  650. strcpy(ctx->state_string, "none");
  651. }
  652. static void app_deinit(AppFSM* const ctx) {
  653. furi_timer_free(ctx->_timer);
  654. }
  655. static void on_timer_tick(AppFSM* ctx) {
  656. switch(ctx->mode_page) {
  657. case 0: {
  658. /* reset after timeout */
  659. if(ctx->detected_timeout == 0) {
  660. ctx->detected_device = false;
  661. ctx->io_swd = 0xFF;
  662. ctx->io_swc = 0xFF;
  663. ctx->io_num_swd = 0xFF;
  664. ctx->io_num_swc = 0xFF;
  665. ctx->ap_scanned = 0;
  666. memset(&ctx->dp_regs, 0x00, sizeof(ctx->dp_regs));
  667. memset(&ctx->targetid_info, 0x00, sizeof(ctx->targetid_info));
  668. memset(&ctx->apidr_info, 0x00, sizeof(ctx->apidr_info));
  669. } else {
  670. ctx->detected_timeout--;
  671. }
  672. ctx->detected = false;
  673. ctx->current_mask = gpio_direction_mask[ctx->current_mask_id];
  674. /* when SWD was already detected, set it to data pin regardless of the mask */
  675. if(ctx->detected_device) {
  676. ctx->current_mask &= ~ctx->io_swd;
  677. }
  678. /* do the scan */
  679. swd_scan(ctx);
  680. /* now when detected a device, set the timeout */
  681. if(ctx->detected) {
  682. ctx->detected_device = true;
  683. ctx->detected_timeout = TIMER_HZ * TIMEOUT;
  684. /* update DPIDR fields */
  685. ctx->dpidr_info.revision = (ctx->dp_regs.dpidr >> 28) & 0x0F;
  686. ctx->dpidr_info.partno = (ctx->dp_regs.dpidr >> 20) & 0xFF;
  687. ctx->dpidr_info.version = (ctx->dp_regs.dpidr >> 12) & 0x0F;
  688. ctx->dpidr_info.designer = (ctx->dp_regs.dpidr >> 1) & 0x3FF;
  689. if(!has_multiple_bits(ctx->io_swc)) {
  690. /* reset error */
  691. uint8_t ack = swd_transfer(ctx, false, false, 1, &ctx->dp_regs.ctrlstat);
  692. furi_log_print_format(
  693. FuriLogLevelDefault, TAG, "stat %02lX %d", ctx->dp_regs.ctrlstat, ack);
  694. if(ack != 1 || (ctx->dp_regs.ctrlstat & STAT_ERROR_FLAGS)) {
  695. furi_log_print_format(FuriLogLevelDefault, TAG, "send ABORT");
  696. swd_abort(ctx);
  697. }
  698. ctx->dp_regs.ctrlstat_ok = swd_read_dpbank(ctx, 1, 0, &ctx->dp_regs.ctrlstat) == 1;
  699. if(ctx->dpidr_info.version >= 1) {
  700. ctx->dp_regs.dlcr_ok = swd_read_dpbank(ctx, 1, 1, &ctx->dp_regs.dlcr) == 1;
  701. }
  702. if(ctx->dpidr_info.version >= 2) {
  703. ctx->dp_regs.targetid_ok =
  704. swd_read_dpbank(ctx, 1, 2, &ctx->dp_regs.targetid) == 1;
  705. ctx->dp_regs.eventstat_ok =
  706. swd_read_dpbank(ctx, 1, 4, &ctx->dp_regs.eventstat) == 1;
  707. ctx->dp_regs.dlpidr_ok = swd_read_dpbank(ctx, 1, 3, &ctx->dp_regs.dlpidr) == 1;
  708. }
  709. if(ctx->dp_regs.targetid_ok) {
  710. ctx->targetid_info.revision = (ctx->dp_regs.targetid >> 28) & 0x0F;
  711. ctx->targetid_info.partno = (ctx->dp_regs.targetid >> 12) & 0xFFFF;
  712. ctx->targetid_info.designer = (ctx->dp_regs.targetid >> 1) & 0x3FF;
  713. }
  714. }
  715. }
  716. ctx->current_mask_id = (ctx->current_mask_id + 1) % COUNT(gpio_direction_mask);
  717. break;
  718. }
  719. case 1:
  720. case 2:
  721. case 3: {
  722. /* set debug enable request */
  723. if(!(ctx->dp_regs.ctrlstat & (CSYSPWRUPREQ | CDBGPWRUPREQ))) {
  724. /* fetch current CTRL/STAT */
  725. swd_transfer(ctx, false, false, 1, &ctx->dp_regs.ctrlstat);
  726. ctx->dp_regs.ctrlstat |= CDBGPWRUPREQ;
  727. ctx->dp_regs.ctrlstat |= CSYSPWRUPREQ;
  728. furi_log_print_format(FuriLogLevelDefault, TAG, "no (CSYSPWRUPREQ | CDBGPWRUPREQ)");
  729. swd_transfer(ctx, false, true, 1, &ctx->dp_regs.ctrlstat);
  730. break;
  731. }
  732. if(!(ctx->dp_regs.ctrlstat & CDBGPWRUPACK)) {
  733. /* fetch current CTRL/STAT */
  734. swd_transfer(ctx, false, false, 1, &ctx->dp_regs.ctrlstat);
  735. furi_log_print_format(FuriLogLevelDefault, TAG, "no CDBGPWRUPACK");
  736. break;
  737. }
  738. /* only scan a few APs at once to stay responsive */
  739. for(int pos = 0; pos < 5; pos++) {
  740. if(ctx->ap_scanned == 0) {
  741. for(int reset_ap = 0; reset_ap < COUNT(ctx->apidr_info); reset_ap++) {
  742. ctx->apidr_info[reset_ap].tested = false;
  743. }
  744. }
  745. int ap = ctx->ap_scanned++;
  746. if(ctx->apidr_info[ap].tested) {
  747. continue;
  748. }
  749. ctx->apidr_info[ap].tested = true;
  750. uint32_t data = 0;
  751. uint32_t base = 0;
  752. if(swd_read_ap(ctx, ap, AP_IDR, &data) != 1) {
  753. swd_abort(ctx);
  754. continue;
  755. }
  756. if(data == 0) {
  757. continue;
  758. }
  759. furi_log_print_format(FuriLogLevelDefault, TAG, "AP%d detected", ap);
  760. ctx->apidr_info[ap].ok = true;
  761. ctx->apidr_info[ap].revision = (data >> 24) & 0x0F;
  762. ctx->apidr_info[ap].designer = (data >> 17) & 0x3FF;
  763. ctx->apidr_info[ap].class = (data >> 13) & 0x0F;
  764. ctx->apidr_info[ap].variant = (data >> 4) & 0x0F;
  765. ctx->apidr_info[ap].type = (data >> 0) & 0x0F;
  766. if(swd_read_ap(ctx, ap, AP_BASE, &ctx->apidr_info[ap].base) != 1) {
  767. swd_abort(ctx);
  768. }
  769. break;
  770. }
  771. break;
  772. }
  773. case 4: {
  774. if(ctx->hex_read_delay++ < 10) {
  775. break;
  776. }
  777. ctx->hex_read_delay = 0;
  778. memset(ctx->hex_buffer, 0xEE, sizeof(ctx->hex_buffer));
  779. uint32_t addr = ctx->hex_addr;
  780. uint32_t data = 0;
  781. for(int pos = 0; pos < sizeof(ctx->hex_buffer) / 4; pos++) {
  782. ctx->hex_buffer_valid[pos] = swd_read_memory(ctx, ctx->ap_pos, addr, &data) == 1;
  783. if(ctx->hex_buffer_valid[pos]) {
  784. memcpy(&ctx->hex_buffer[pos * 4], &data, 4);
  785. } else {
  786. swd_abort_simple(ctx);
  787. }
  788. addr += 4;
  789. }
  790. }
  791. }
  792. }
  793. int32_t swd_probe_app_main(void* p) {
  794. UNUSED(p);
  795. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(AppEvent));
  796. AppFSM* ctx = malloc(sizeof(AppFSM));
  797. app_init(ctx, event_queue);
  798. ValueMutex state_mutex;
  799. if(!init_mutex(&state_mutex, ctx, sizeof(AppFSM))) {
  800. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  801. free(ctx);
  802. return 255;
  803. }
  804. ViewPort* view_port = view_port_alloc();
  805. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  806. view_port_input_callback_set(view_port, input_callback, event_queue);
  807. Gui* gui = furi_record_open(RECORD_GUI);
  808. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  809. ctx->notification = furi_record_open(RECORD_NOTIFICATION);
  810. notification_message_block(ctx->notification, &sequence_display_backlight_enforce_on);
  811. DOLPHIN_DEED(DolphinDeedPluginGameStart);
  812. AppEvent event;
  813. for(bool processing = true; processing;) {
  814. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  815. AppFSM* ctx = (AppFSM*)acquire_mutex_block(&state_mutex);
  816. if(event_status == FuriStatusOk) {
  817. if(event.type == EventKeyPress) {
  818. if(event.input.type == InputTypePress) {
  819. switch(event.input.key) {
  820. case InputKeyUp:
  821. ctx->last_key = KeyUp;
  822. switch(ctx->mode_page) {
  823. default:
  824. break;
  825. case 3:
  826. if(ctx->ap_pos > 0) {
  827. ctx->ap_pos--;
  828. }
  829. break;
  830. case 4: {
  831. ctx->hex_addr +=
  832. ((ctx->hex_select) ? 1 : 8) * (1 << (4 * ctx->hex_select));
  833. break;
  834. }
  835. }
  836. break;
  837. case InputKeyDown:
  838. ctx->last_key = KeyDown;
  839. switch(ctx->mode_page) {
  840. default:
  841. break;
  842. case 3:
  843. if(ctx->ap_pos + 1 < COUNT(ctx->apidr_info)) {
  844. ctx->ap_pos++;
  845. }
  846. break;
  847. case 4: {
  848. ctx->hex_addr -=
  849. ((ctx->hex_select) ? 1 : 8) * (1 << (4 * ctx->hex_select));
  850. break;
  851. }
  852. }
  853. break;
  854. case InputKeyRight:
  855. ctx->last_key = KeyRight;
  856. if(ctx->mode_page == 4) {
  857. if(ctx->hex_select > 0) {
  858. ctx->hex_select--;
  859. }
  860. } else {
  861. if(ctx->mode_page + 1 < MODE_PAGES) {
  862. ctx->mode_page++;
  863. }
  864. }
  865. break;
  866. case InputKeyLeft:
  867. ctx->last_key = KeyLeft;
  868. if(ctx->mode_page == 4) {
  869. if(ctx->hex_select < 7) {
  870. ctx->hex_select++;
  871. }
  872. } else {
  873. if(ctx->mode_page > 0) {
  874. ctx->mode_page--;
  875. }
  876. }
  877. break;
  878. case InputKeyOk:
  879. ctx->last_key = KeyOK;
  880. if(ctx->mode_page == 3 && ctx->apidr_info[ctx->ap_pos].ok) {
  881. ctx->mode_page = 4;
  882. }
  883. break;
  884. case InputKeyBack:
  885. if(ctx->mode_page == 4) {
  886. ctx->mode_page = 3;
  887. } else if(ctx->mode_page != 0) {
  888. ctx->mode_page = 0;
  889. } else {
  890. processing = false;
  891. }
  892. break;
  893. default:
  894. break;
  895. }
  896. }
  897. } else if(event.type == EventTimerTick) {
  898. FURI_CRITICAL_ENTER();
  899. on_timer_tick(ctx);
  900. FURI_CRITICAL_EXIT();
  901. }
  902. } else {
  903. /* timeout */
  904. }
  905. view_port_update(view_port);
  906. bool beep = false;
  907. if(ctx->detected_device && !ctx->detected_notified) {
  908. ctx->detected_notified = true;
  909. beep = true;
  910. }
  911. if(!ctx->detected_device && ctx->detected_notified) {
  912. ctx->detected_notified = false;
  913. }
  914. release_mutex(&state_mutex, ctx);
  915. if(beep) {
  916. notification_message_block(ctx->notification, &seq_c_minor);
  917. }
  918. }
  919. // Wait for all notifications to be played and return backlight to normal state
  920. app_deinit(ctx);
  921. notification_message_block(ctx->notification, &sequence_display_backlight_enforce_auto);
  922. view_port_enabled_set(view_port, false);
  923. gui_remove_view_port(gui, view_port);
  924. furi_record_close(RECORD_GUI);
  925. furi_record_close(RECORD_NOTIFICATION);
  926. view_port_free(view_port);
  927. furi_message_queue_free(event_queue);
  928. delete_mutex(&state_mutex);
  929. free(ctx);
  930. return 0;
  931. }