swd_probe_app.h 4.7 KB

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