swd_probe_app.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 MEMAP_CSW 0x00
  42. #define MEMAP_TAR 0x04
  43. #define MEMAP_DRW 0x0C
  44. #define AP_IDR 0xFC
  45. #define AP_BASE 0xF8
  46. typedef enum { KeyNone, KeyUp, KeyRight, KeyDown, KeyLeft, KeyOK } KeyCode;
  47. typedef enum {
  48. EventTimerTick,
  49. EventKeyPress,
  50. } EventType;
  51. typedef struct {
  52. EventType type;
  53. InputEvent input;
  54. } AppEvent;
  55. typedef struct {
  56. uint32_t ctrlstat;
  57. bool ctrlstat_ok;
  58. uint32_t dlcr;
  59. bool dlcr_ok;
  60. uint32_t dlpidr;
  61. bool dlpidr_ok;
  62. uint32_t dpidr;
  63. bool dpidr_ok;
  64. uint32_t eventstat;
  65. bool eventstat_ok;
  66. uint32_t select;
  67. bool select_ok;
  68. uint32_t targetid;
  69. bool targetid_ok;
  70. } swd_dpreg_t;
  71. typedef struct {
  72. bool ok;
  73. bool tested;
  74. uint8_t revision;
  75. uint16_t designer;
  76. uint8_t class;
  77. uint8_t variant;
  78. uint8_t type;
  79. uint32_t base;
  80. } swd_apidr_info_t;
  81. typedef struct {
  82. uint8_t revision;
  83. uint8_t partno;
  84. uint8_t version;
  85. uint16_t designer;
  86. } swd_dpidr_info_t;
  87. typedef struct {
  88. uint8_t revision;
  89. uint16_t partno;
  90. uint16_t designer;
  91. } swd_targetid_info_t;
  92. typedef struct sScriptContext ScriptContext;
  93. typedef struct {
  94. FuriMessageQueue* event_queue;
  95. FuriTimer* timer;
  96. NotificationApp* notification;
  97. Storage* storage;
  98. ViewPort* view_port;
  99. Gui* gui;
  100. DialogsApp* dialogs;
  101. ValueMutex state_mutex;
  102. swd_targetid_info_t targetid_info;
  103. swd_dpidr_info_t dpidr_info;
  104. swd_dpreg_t dp_regs;
  105. swd_apidr_info_t apidr_info[256];
  106. ScriptContext* script;
  107. uint8_t current_mask_id;
  108. uint32_t current_mask;
  109. uint8_t io_swc;
  110. uint8_t io_swd;
  111. uint8_t io_num_swc;
  112. uint8_t io_num_swd;
  113. uint32_t detected_timeout;
  114. uint32_t swd_clock_delay;
  115. uint32_t swd_idle_bits;
  116. bool detected;
  117. bool detected_device;
  118. bool detected_notified;
  119. uint32_t mode_page;
  120. uint8_t ap_pos;
  121. uint8_t ap_scanned;
  122. uint32_t hex_addr;
  123. uint8_t hex_select;
  124. uint8_t hex_buffer[32];
  125. uint8_t hex_buffer_valid[8];
  126. uint8_t hex_read_delay;
  127. char state_string[64];
  128. char script_detected[MAX_FILE_LENGTH];
  129. bool script_detected_executed;
  130. } AppFSM;
  131. struct sScriptContext {
  132. AppFSM* app;
  133. File* script_file;
  134. uint64_t position;
  135. uint32_t selected_ap;
  136. uint32_t max_tries;
  137. uint32_t block_size;
  138. bool abort;
  139. bool restart;
  140. bool errors_ignore;
  141. bool status_ignore;
  142. bool goto_active;
  143. char goto_label[64];
  144. };
  145. typedef struct {
  146. const char* prefix;
  147. bool (*func)(ScriptContext* ctx);
  148. } ScriptFunctionInfo;
  149. const NotificationSequence seq_c_minor = {
  150. &message_note_c4,
  151. &message_delay_100,
  152. &message_sound_off,
  153. &message_delay_10,
  154. &message_note_ds4,
  155. &message_delay_100,
  156. &message_sound_off,
  157. &message_delay_10,
  158. &message_note_g4,
  159. &message_delay_100,
  160. &message_sound_off,
  161. &message_delay_10,
  162. &message_vibro_on,
  163. &message_delay_50,
  164. &message_vibro_off,
  165. NULL,
  166. };
  167. const NotificationSequence seq_error = {
  168. &message_vibro_on,
  169. &message_delay_50,
  170. &message_vibro_off,
  171. &message_note_g4,
  172. &message_delay_100,
  173. &message_sound_off,
  174. &message_delay_10,
  175. &message_note_c4,
  176. &message_delay_500,
  177. &message_sound_off,
  178. &message_delay_10,
  179. NULL,
  180. };
  181. const NotificationSequence* seq_sounds[] = {&seq_c_minor, &seq_error};
  182. #endif