swd_probe_app.h 4.1 KB

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