swd_probe_app.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #ifndef __ARHA_FLIPPERAPP_DEMO
  2. #define __ARHA_FLIPPERAPP_DEMO
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <furi.h>
  9. #include <furi_hal.h>
  10. #include <furi_hal_speaker.h>
  11. #include <gui/gui.h>
  12. #include <gui/elements.h>
  13. #include <dialogs/dialogs.h>
  14. #include <input/input.h>
  15. #include <storage/storage.h>
  16. #include <dolphin/dolphin.h>
  17. #include <notification/notification.h>
  18. #include <notification/notification_messages.h>
  19. #include <lib/subghz/receiver.h>
  20. #include <lib/subghz/transmitter.h>
  21. #include <lib/subghz/subghz_file_encoder_worker.h>
  22. #include <notification/notification.h>
  23. #include "usb_uart.h"
  24. #define TAG "SWD"
  25. /* short debug message */
  26. #define DBGS(format) furi_log_print_format(FuriLogLevelDebug, TAG, "%s: " format, __FUNCTION__)
  27. /* formatted debug message */
  28. #define DBG(format, ...) \
  29. furi_log_print_format(FuriLogLevelDebug, TAG, "%s: " format, __FUNCTION__, __VA_ARGS__)
  30. /* log message*/
  31. #define LOG(...) furi_log_print_format(FuriLogLevelDefault, TAG, __VA_ARGS__)
  32. #define COUNT(x) ((size_t)(sizeof(x) / sizeof((x)[0])))
  33. #define ARRAY_SIZE(x) COUNT(x)
  34. #define SWD_DELAY_US 0
  35. #define TIMER_HZ 25
  36. #define TIMEOUT 3
  37. #define QUEUE_SIZE 8
  38. #define IDLE_BITS 8
  39. #define CLOCK_DELAY 0
  40. #define MAX_FILE_LENGTH 128
  41. #define SCRIPT_MAX_LINES 1000
  42. typedef enum {
  43. ModePageScan = 0,
  44. ModePageFound = 1,
  45. ModePageDPRegs = 2,
  46. ModePageDPID = 3,
  47. ModePageAPID = 4,
  48. ModePageCount = 5,
  49. ModePageHexDump = 0x100,
  50. ModePageScript = 0x101,
  51. ModePageCoresight = 0x102,
  52. } ModePages;
  53. #define CDBGPWRUPREQ (1 << 28)
  54. #define CDBGPWRUPACK (1 << 29)
  55. #define CSYSPWRUPREQ (1 << 30)
  56. #define CSYSPWRUPACK (1 << 31)
  57. #define WDATAERR (1 << 7)
  58. #define STICKYERR (1 << 5)
  59. #define STAT_ERROR_FLAGS (WDATAERR | STICKYERR)
  60. #define REG_IDCODE 0x00
  61. #define REG_CTRLSTAT 0x01
  62. #define REG_CTRLSTAT_BANK 0x00
  63. #define REG_DLCR 0x01
  64. #define REG_DLCR_BANK 0x01
  65. #define REG_TARGETID 0x01
  66. #define REG_TARGETID_BANK 0x02
  67. #define REG_DLPIDR 0x01
  68. #define REG_DLPIDR_BANK 0x03
  69. #define REG_EVENTSTAT 0x01
  70. #define REG_EVENTSTAT_BANK 0x04
  71. #define REG_SELECT 0x02
  72. #define MEMAP_CSW 0x00
  73. #define MEMAP_TAR 0x04
  74. #define MEMAP_DRW 0x0C
  75. #define AP_IDR 0xFC
  76. #define AP_BASE 0xF8
  77. typedef enum { KeyNone, KeyUp, KeyRight, KeyDown, KeyLeft, KeyOK } KeyCode;
  78. typedef enum {
  79. EventTimerTick,
  80. EventKeyPress,
  81. } EventType;
  82. typedef struct {
  83. EventType type;
  84. InputEvent input;
  85. } AppEvent;
  86. typedef struct {
  87. uint32_t ctrlstat;
  88. bool ctrlstat_ok;
  89. uint32_t dlcr;
  90. bool dlcr_ok;
  91. uint32_t dlpidr;
  92. bool dlpidr_ok;
  93. uint32_t dpidr;
  94. bool dpidr_ok;
  95. uint32_t eventstat;
  96. bool eventstat_ok;
  97. uint32_t select;
  98. bool select_ok;
  99. uint32_t targetid;
  100. bool targetid_ok;
  101. } swd_dpreg_t;
  102. typedef struct {
  103. bool ok;
  104. bool tested;
  105. uint8_t revision;
  106. uint16_t designer;
  107. uint8_t class;
  108. uint8_t variant;
  109. uint8_t type;
  110. uint32_t base;
  111. } swd_apidr_info_t;
  112. typedef struct {
  113. uint8_t revision;
  114. uint8_t partno;
  115. uint8_t version;
  116. uint16_t designer;
  117. } swd_dpidr_info_t;
  118. typedef struct {
  119. uint8_t revision;
  120. uint16_t partno;
  121. uint16_t designer;
  122. } swd_targetid_info_t;
  123. typedef struct sScriptContext ScriptContext;
  124. typedef struct {
  125. Storage* storage;
  126. Gui* gui;
  127. DialogsApp* dialogs;
  128. NotificationApp* notification;
  129. FuriTimer* timer;
  130. UsbUart* uart;
  131. ViewPort* view_port;
  132. FuriMessageQueue* event_queue;
  133. FuriMutex* swd_mutex;
  134. FuriMutex* gui_mutex;
  135. swd_targetid_info_t targetid_info;
  136. swd_dpidr_info_t dpidr_info;
  137. swd_dpreg_t dp_regs;
  138. swd_apidr_info_t apidr_info[256];
  139. ScriptContext* script;
  140. ScriptContext* commandline;
  141. uint8_t timeout_overdue;
  142. uint32_t loop_count;
  143. uint8_t current_mask_id;
  144. uint32_t current_mask;
  145. uint8_t io_swc;
  146. uint8_t io_swd;
  147. uint8_t io_num_swc;
  148. uint8_t io_num_swd;
  149. int32_t detected_timeout;
  150. uint32_t swd_clock_delay;
  151. uint32_t swd_idle_bits;
  152. bool detected;
  153. bool detected_device;
  154. bool detected_notified;
  155. uint32_t mode_page;
  156. uint8_t ap_pos;
  157. uint8_t ap_scanned;
  158. uint32_t coresight_pos[16];
  159. uint32_t coresight_count[16];
  160. uint8_t coresight_level;
  161. uint32_t coresight_bases[16];
  162. uint32_t hex_addr;
  163. uint8_t hex_select;
  164. uint8_t hex_buffer[32];
  165. uint8_t hex_buffer_valid[8];
  166. char state_string[64];
  167. char script_detected[MAX_FILE_LENGTH];
  168. bool script_detected_executed;
  169. } AppFSM;
  170. struct sScriptContext {
  171. AppFSM* app;
  172. ScriptContext* parent;
  173. char filename[MAX_FILE_LENGTH];
  174. /* when used with string input */
  175. char line_data[128];
  176. uint64_t line_pos;
  177. /* when used with file input */
  178. File* script_file;
  179. uint64_t position;
  180. uint32_t selected_ap;
  181. uint32_t max_tries;
  182. uint32_t block_size;
  183. bool abort;
  184. bool restart;
  185. bool errors_ignore;
  186. bool status_ignore;
  187. bool goto_active;
  188. char goto_label[64];
  189. };
  190. typedef struct {
  191. const char* prefix;
  192. bool (*func)(ScriptContext* ctx);
  193. } ScriptFunctionInfo;
  194. uint8_t swd_read_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t* data);
  195. #endif