swd_probe_app.h 5.0 KB

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