mifare_nested.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. #include "mifare_nested_i.h"
  2. #include <gui/elements.h>
  3. #include <furi_hal_nfc.h>
  4. #include <lib/drivers/st25r3916.h>
  5. bool mifare_nested_custom_event_callback(void* context, uint32_t event) {
  6. furi_assert(context);
  7. MifareNested* mifare_nested = context;
  8. return scene_manager_handle_custom_event(mifare_nested->scene_manager, event);
  9. }
  10. bool mifare_nested_back_event_callback(void* context) {
  11. furi_assert(context);
  12. MifareNested* mifare_nested = context;
  13. return scene_manager_handle_back_event(mifare_nested->scene_manager);
  14. }
  15. void mifare_nested_tick_event_callback(void* context) {
  16. furi_assert(context);
  17. MifareNested* mifare_nested = context;
  18. scene_manager_handle_tick_event(mifare_nested->scene_manager);
  19. }
  20. void mifare_nested_show_loading_popup(void* context, bool show) {
  21. MifareNested* mifare_nested = context;
  22. if(show) {
  23. // Raise timer priority so that animations can play
  24. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  25. view_dispatcher_switch_to_view(mifare_nested->view_dispatcher, MifareNestedViewLoading);
  26. } else {
  27. // Restore default timer priority
  28. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  29. }
  30. }
  31. NestedState* collection_alloc() {
  32. NestedState* nested = malloc(sizeof(NestedState));
  33. nested->view = view_alloc();
  34. view_allocate_model(nested->view, ViewModelTypeLocking, sizeof(NestedAttackViewModel));
  35. with_view_model(
  36. nested->view,
  37. NestedAttackViewModel * model,
  38. {
  39. model->header = furi_string_alloc();
  40. furi_string_set(model->header, "Collecting nonces");
  41. model->keys_count = 0;
  42. model->hardnested_states = 0;
  43. model->lost_tag = false;
  44. model->calibrating = false;
  45. model->need_prediction = false;
  46. model->hardnested = false;
  47. },
  48. false);
  49. return nested;
  50. }
  51. CheckKeysState* check_keys_alloc() {
  52. CheckKeysState* state = malloc(sizeof(CheckKeysState));
  53. state->view = view_alloc();
  54. view_allocate_model(state->view, ViewModelTypeLocking, sizeof(CheckKeysViewModel));
  55. with_view_model(
  56. state->view,
  57. CheckKeysViewModel * model,
  58. {
  59. model->header = furi_string_alloc();
  60. furi_string_set(model->header, "Checking keys");
  61. model->lost_tag = false;
  62. },
  63. false);
  64. return state;
  65. }
  66. static void nested_draw_callback(Canvas* canvas, void* model) {
  67. NestedAttackViewModel* m = model;
  68. if(m->lost_tag) {
  69. canvas_set_font(canvas, FontPrimary);
  70. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
  71. canvas_set_font(canvas, FontSecondary);
  72. elements_multiline_text_aligned(
  73. canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
  74. } else if(m->calibrating) {
  75. canvas_set_font(canvas, FontPrimary);
  76. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Calibrating...");
  77. canvas_set_font(canvas, FontSecondary);
  78. if(!m->need_prediction) {
  79. elements_multiline_text_aligned(
  80. canvas, 64, 23, AlignCenter, AlignTop, "Don't touch or move\nFlipper/Tag!");
  81. } else {
  82. elements_multiline_text_aligned(
  83. canvas, 64, 18, AlignCenter, AlignTop, "Don't touch or move tag!");
  84. canvas_set_font(canvas, FontPrimary);
  85. elements_multiline_text_aligned(
  86. canvas, 64, 30, AlignCenter, AlignTop, "Calibration will take\nmore time");
  87. }
  88. } else if(m->hardnested) {
  89. char draw_str[32] = {};
  90. canvas_set_font(canvas, FontPrimary);
  91. canvas_draw_str_aligned(
  92. canvas, 64, 2, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
  93. canvas_set_font(canvas, FontSecondary);
  94. float progress =
  95. m->keys_count == 0 ? 0 : (float)(m->nonces_collected) / (float)(m->keys_count);
  96. if(progress > 1.0) {
  97. progress = 1.0;
  98. }
  99. elements_progress_bar(canvas, 5, 15, 120, progress);
  100. canvas_set_font(canvas, FontSecondary);
  101. snprintf(
  102. draw_str,
  103. sizeof(draw_str),
  104. "Nonces collected: %lu/%lu",
  105. m->nonces_collected,
  106. m->keys_count);
  107. canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
  108. snprintf(draw_str, sizeof(draw_str), "States found: %lu/256", m->hardnested_states);
  109. canvas_draw_str_aligned(canvas, 1, 40, AlignLeft, AlignTop, draw_str);
  110. } else {
  111. char draw_str[32] = {};
  112. canvas_set_font(canvas, FontPrimary);
  113. canvas_draw_str_aligned(
  114. canvas, 64, 2, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
  115. canvas_set_font(canvas, FontSecondary);
  116. float progress =
  117. m->keys_count == 0 ? 0 : (float)(m->nonces_collected) / (float)(m->keys_count);
  118. if(progress > 1.0) {
  119. progress = 1.0;
  120. }
  121. elements_progress_bar(canvas, 5, 15, 120, progress);
  122. canvas_set_font(canvas, FontSecondary);
  123. snprintf(
  124. draw_str,
  125. sizeof(draw_str),
  126. "Nonces collected: %lu/%lu",
  127. m->nonces_collected,
  128. m->keys_count);
  129. canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
  130. }
  131. elements_button_center(canvas, "Stop");
  132. }
  133. static void check_keys_draw_callback(Canvas* canvas, void* model) {
  134. CheckKeysViewModel* m = model;
  135. if(m->lost_tag) {
  136. canvas_set_font(canvas, FontPrimary);
  137. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
  138. canvas_set_font(canvas, FontSecondary);
  139. elements_multiline_text_aligned(
  140. canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
  141. } else if(m->processing_keys) {
  142. canvas_set_font(canvas, FontPrimary);
  143. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Processing keys...");
  144. canvas_set_font(canvas, FontSecondary);
  145. elements_multiline_text_aligned(
  146. canvas, 64, 23, AlignCenter, AlignTop, "Checking which keys you\nalready have...");
  147. } else {
  148. char draw_str[32] = {};
  149. char draw_sub_str[32] = {};
  150. canvas_set_font(canvas, FontPrimary);
  151. canvas_draw_str_aligned(
  152. canvas, 64, 2, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
  153. canvas_set_font(canvas, FontSecondary);
  154. float progress = m->keys_count == 0 ? 0 :
  155. (float)(m->keys_checked) / (float)(m->keys_count);
  156. if(progress > 1.0) {
  157. progress = 1.0;
  158. }
  159. elements_progress_bar(canvas, 5, 15, 120, progress);
  160. canvas_set_font(canvas, FontSecondary);
  161. snprintf(
  162. draw_str, sizeof(draw_str), "Keys checked: %lu/%lu", m->keys_checked, m->keys_count);
  163. canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
  164. snprintf(
  165. draw_sub_str,
  166. sizeof(draw_sub_str),
  167. "Keys found: %lu/%lu",
  168. m->keys_found,
  169. m->keys_total);
  170. canvas_draw_str_aligned(canvas, 1, 40, AlignLeft, AlignTop, draw_sub_str);
  171. }
  172. elements_button_center(canvas, "Stop");
  173. }
  174. static bool nested_input_callback(InputEvent* event, void* context) {
  175. MifareNested* mifare_nested = context;
  176. bool consumed = false;
  177. if(event->type == InputTypeShort && (event->key == InputKeyBack || event->key == InputKeyOk)) {
  178. scene_manager_search_and_switch_to_previous_scene(mifare_nested->scene_manager, 0);
  179. consumed = true;
  180. }
  181. return consumed;
  182. }
  183. MifareNested* mifare_nested_alloc() {
  184. MifareNested* mifare_nested = malloc(sizeof(MifareNested));
  185. mifare_nested->worker = mifare_nested_worker_alloc();
  186. mifare_nested->view_dispatcher = view_dispatcher_alloc();
  187. mifare_nested->scene_manager =
  188. scene_manager_alloc(&mifare_nested_scene_handlers, mifare_nested);
  189. view_dispatcher_enable_queue(mifare_nested->view_dispatcher);
  190. view_dispatcher_set_event_callback_context(mifare_nested->view_dispatcher, mifare_nested);
  191. view_dispatcher_set_custom_event_callback(
  192. mifare_nested->view_dispatcher, mifare_nested_custom_event_callback);
  193. view_dispatcher_set_navigation_event_callback(
  194. mifare_nested->view_dispatcher, mifare_nested_back_event_callback);
  195. view_dispatcher_set_tick_event_callback(
  196. mifare_nested->view_dispatcher, mifare_nested_tick_event_callback, 100);
  197. // Nfc device
  198. mifare_nested->nfc_dev = nfc_device_alloc();
  199. // Open GUI record
  200. mifare_nested->gui = furi_record_open(RECORD_GUI);
  201. view_dispatcher_attach_to_gui(
  202. mifare_nested->view_dispatcher, mifare_nested->gui, ViewDispatcherTypeFullscreen);
  203. // Open Notification record
  204. mifare_nested->notifications = furi_record_open(RECORD_NOTIFICATION);
  205. // Submenu
  206. mifare_nested->submenu = submenu_alloc();
  207. view_dispatcher_add_view(
  208. mifare_nested->view_dispatcher,
  209. MifareNestedViewMenu,
  210. submenu_get_view(mifare_nested->submenu));
  211. // Popup
  212. mifare_nested->popup = popup_alloc();
  213. view_dispatcher_add_view(
  214. mifare_nested->view_dispatcher,
  215. MifareNestedViewPopup,
  216. popup_get_view(mifare_nested->popup));
  217. // Loading
  218. mifare_nested->loading = loading_alloc();
  219. view_dispatcher_add_view(
  220. mifare_nested->view_dispatcher,
  221. MifareNestedViewLoading,
  222. loading_get_view(mifare_nested->loading));
  223. // Text Input
  224. mifare_nested->text_input = text_input_alloc();
  225. view_dispatcher_add_view(
  226. mifare_nested->view_dispatcher,
  227. MifareNestedViewTextInput,
  228. text_input_get_view(mifare_nested->text_input));
  229. // Custom Widget
  230. mifare_nested->widget = widget_alloc();
  231. view_dispatcher_add_view(
  232. mifare_nested->view_dispatcher,
  233. MifareNestedViewWidget,
  234. widget_get_view(mifare_nested->widget));
  235. // Variable Item List
  236. mifare_nested->variable_item_list = variable_item_list_alloc();
  237. view_dispatcher_add_view(
  238. mifare_nested->view_dispatcher,
  239. MifareNestedViewVariableList,
  240. variable_item_list_get_view(mifare_nested->variable_item_list));
  241. // Nested attack state
  242. NestedState* plugin_state = collection_alloc();
  243. view_set_context(plugin_state->view, mifare_nested);
  244. mifare_nested->nested_state = plugin_state;
  245. view_dispatcher_add_view(
  246. mifare_nested->view_dispatcher, MifareNestedViewCollecting, plugin_state->view);
  247. // Check keys attack state
  248. CheckKeysState* keys_state = check_keys_alloc();
  249. view_set_context(keys_state->view, mifare_nested);
  250. mifare_nested->keys_state = keys_state;
  251. view_dispatcher_add_view(
  252. mifare_nested->view_dispatcher, MifareNestedViewCheckKeys, keys_state->view);
  253. KeyInfo_t* key_info = malloc(sizeof(KeyInfo_t));
  254. mifare_nested->keys = key_info;
  255. MifareNestedSettings* settings = malloc(sizeof(MifareNestedSettings));
  256. settings->only_hardnested = false;
  257. mifare_nested->settings = settings;
  258. view_set_draw_callback(plugin_state->view, nested_draw_callback);
  259. view_set_input_callback(plugin_state->view, nested_input_callback);
  260. view_set_draw_callback(keys_state->view, check_keys_draw_callback);
  261. view_set_input_callback(keys_state->view, nested_input_callback);
  262. mifare_nested->collecting_type = MifareNestedWorkerStateReady;
  263. mifare_nested->run = NestedRunIdle;
  264. return mifare_nested;
  265. }
  266. void mifare_nested_free(MifareNested* mifare_nested) {
  267. furi_assert(mifare_nested);
  268. // Nfc device
  269. nfc_device_free(mifare_nested->nfc_dev);
  270. // Submenu
  271. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewMenu);
  272. submenu_free(mifare_nested->submenu);
  273. // Popup
  274. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewPopup);
  275. popup_free(mifare_nested->popup);
  276. // Loading
  277. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewLoading);
  278. loading_free(mifare_nested->loading);
  279. // TextInput
  280. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewTextInput);
  281. text_input_free(mifare_nested->text_input);
  282. // Custom Widget
  283. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewWidget);
  284. widget_free(mifare_nested->widget);
  285. // Variable Item List
  286. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewVariableList);
  287. variable_item_list_free(mifare_nested->variable_item_list);
  288. // Nested
  289. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewCollecting);
  290. // Check keys
  291. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewCheckKeys);
  292. // Nonces states
  293. free(mifare_nested->nested_state);
  294. // Keys
  295. free(mifare_nested->keys);
  296. // Settings
  297. free(mifare_nested->settings);
  298. // Worker
  299. mifare_nested_worker_stop(mifare_nested->worker);
  300. mifare_nested_worker_free(mifare_nested->worker);
  301. // View Dispatcher
  302. view_dispatcher_free(mifare_nested->view_dispatcher);
  303. // Scene Manager
  304. scene_manager_free(mifare_nested->scene_manager);
  305. // GUI
  306. furi_record_close(RECORD_GUI);
  307. mifare_nested->gui = NULL;
  308. // Notifications
  309. furi_record_close(RECORD_NOTIFICATION);
  310. mifare_nested->notifications = NULL;
  311. free(mifare_nested);
  312. }
  313. void mifare_nested_blink_start(MifareNested* mifare_nested) {
  314. notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_start_blue);
  315. }
  316. void mifare_nested_blink_calibration_start(MifareNested* mifare_nested) {
  317. notification_message(
  318. mifare_nested->notifications, &mifare_nested_sequence_blink_start_magenta);
  319. }
  320. void mifare_nested_blink_nonce_collection_start(MifareNested* mifare_nested) {
  321. notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_start_yellow);
  322. }
  323. void mifare_nested_blink_stop(MifareNested* mifare_nested) {
  324. notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_stop);
  325. }
  326. void remove_new_nfc_and_init_old() {
  327. // Deinitialize new NFC and init legacy
  328. if(furi_hal_nfc_is_hal_ready() != FuriHalNfcErrorNone) {
  329. FURI_LOG_E("nested", "NFC chip failed to start\r\n");
  330. return;
  331. }
  332. furi_hal_nfc_acquire();
  333. furi_hal_nfc_reset_mode();
  334. furi_hal_nfc_low_power_mode_start();
  335. furi_hal_nfc_release();
  336. furry_hal_nfc_init();
  337. }
  338. void remove_old_nfc_and_init_new_back() {
  339. // Deinitialize NFC
  340. furry_hal_nfc_deinit();
  341. //
  342. furi_hal_nfc_acquire();
  343. furi_hal_nfc_reset_mode();
  344. furi_hal_nfc_low_power_mode_start();
  345. FuriHalSpiBusHandle* handle = &furi_hal_spi_bus_handle_nfc;
  346. // Set default state
  347. st25r3916_direct_cmd(handle, ST25R3916_CMD_SET_DEFAULT);
  348. // Increase IO driver strength of MISO and IRQ
  349. st25r3916_write_reg(handle, ST25R3916_REG_IO_CONF2, ST25R3916_REG_IO_CONF2_io_drv_lvl);
  350. // Check chip ID
  351. uint8_t chip_id = 0;
  352. st25r3916_read_reg(handle, ST25R3916_REG_IC_IDENTITY, &chip_id);
  353. if((chip_id & ST25R3916_REG_IC_IDENTITY_ic_type_mask) !=
  354. ST25R3916_REG_IC_IDENTITY_ic_type_st25r3916) {
  355. furi_hal_nfc_low_power_mode_start();
  356. furi_hal_nfc_release();
  357. return;
  358. }
  359. // Clear interrupts
  360. st25r3916_get_irq(handle);
  361. // Mask all interrupts
  362. st25r3916_mask_irq(handle, ST25R3916_IRQ_MASK_ALL);
  363. // Enable interrupts
  364. //furi_hal_nfc_init_gpio_isr();
  365. // Disable internal overheat protection
  366. st25r3916_change_test_reg_bits(handle, 0x04, 0x10, 0x10);
  367. // Measure voltage
  368. // Set measure power supply voltage source
  369. st25r3916_change_reg_bits(
  370. handle,
  371. ST25R3916_REG_REGULATOR_CONTROL,
  372. ST25R3916_REG_REGULATOR_CONTROL_mpsv_mask,
  373. ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd);
  374. // Enable timer and interrupt register
  375. st25r3916_mask_irq(handle, ~ST25R3916_IRQ_MASK_DCT);
  376. st25r3916_direct_cmd(handle, ST25R3916_CMD_MEASURE_VDD);
  377. //furi_hal_nfc_event_wait_for_specific_irq(handle, ST25R3916_IRQ_MASK_DCT, 100);
  378. st25r3916_mask_irq(handle, ST25R3916_IRQ_MASK_ALL);
  379. uint8_t ad_res = 0;
  380. st25r3916_read_reg(handle, ST25R3916_REG_AD_RESULT, &ad_res);
  381. uint16_t mV = ((uint16_t)ad_res) * 23U;
  382. mV += (((((uint16_t)ad_res) * 4U) + 5U) / 10U);
  383. if(mV < 3600) {
  384. st25r3916_change_reg_bits(
  385. handle,
  386. ST25R3916_REG_IO_CONF2,
  387. ST25R3916_REG_IO_CONF2_sup3V,
  388. ST25R3916_REG_IO_CONF2_sup3V_3V);
  389. } else {
  390. st25r3916_change_reg_bits(
  391. handle,
  392. ST25R3916_REG_IO_CONF2,
  393. ST25R3916_REG_IO_CONF2_sup3V,
  394. ST25R3916_REG_IO_CONF2_sup3V_5V);
  395. }
  396. // Disable MCU CLK
  397. st25r3916_change_reg_bits(
  398. handle,
  399. ST25R3916_REG_IO_CONF1,
  400. ST25R3916_REG_IO_CONF1_out_cl_mask | ST25R3916_REG_IO_CONF1_lf_clk_off,
  401. 0x07);
  402. // Disable MISO pull-down
  403. st25r3916_change_reg_bits(
  404. handle,
  405. ST25R3916_REG_IO_CONF2,
  406. ST25R3916_REG_IO_CONF2_miso_pd1 | ST25R3916_REG_IO_CONF2_miso_pd2,
  407. 0x00);
  408. // Set tx driver resistance to 1 Om
  409. st25r3916_change_reg_bits(
  410. handle, ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_d_res_mask, 0x00);
  411. // Use minimum non-overlap
  412. st25r3916_change_reg_bits(
  413. handle,
  414. ST25R3916_REG_RES_AM_MOD,
  415. ST25R3916_REG_RES_AM_MOD_fa3_f,
  416. ST25R3916_REG_RES_AM_MOD_fa3_f);
  417. // Set activation threashold
  418. st25r3916_change_reg_bits(
  419. handle,
  420. ST25R3916_REG_FIELD_THRESHOLD_ACTV,
  421. ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_mask,
  422. ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_105mV);
  423. st25r3916_change_reg_bits(
  424. handle,
  425. ST25R3916_REG_FIELD_THRESHOLD_ACTV,
  426. ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_mask,
  427. ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_105mV);
  428. // Set deactivation threashold
  429. st25r3916_change_reg_bits(
  430. handle,
  431. ST25R3916_REG_FIELD_THRESHOLD_DEACTV,
  432. ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_mask,
  433. ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_75mV);
  434. st25r3916_change_reg_bits(
  435. handle,
  436. ST25R3916_REG_FIELD_THRESHOLD_DEACTV,
  437. ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_mask,
  438. ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_75mV);
  439. // Enable external load modulation
  440. st25r3916_change_reg_bits(
  441. handle, ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_lm_ext, ST25R3916_REG_AUX_MOD_lm_ext);
  442. // Enable internal load modulation
  443. st25r3916_change_reg_bits(
  444. handle, ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_lm_dri, ST25R3916_REG_AUX_MOD_lm_dri);
  445. // Adjust FDT
  446. st25r3916_change_reg_bits(
  447. handle,
  448. ST25R3916_REG_PASSIVE_TARGET,
  449. ST25R3916_REG_PASSIVE_TARGET_fdel_mask,
  450. (5U << ST25R3916_REG_PASSIVE_TARGET_fdel_shift));
  451. // Reduce RFO resistance in Modulated state
  452. st25r3916_change_reg_bits(
  453. handle,
  454. ST25R3916_REG_PT_MOD,
  455. ST25R3916_REG_PT_MOD_ptm_res_mask | ST25R3916_REG_PT_MOD_pt_res_mask,
  456. 0x0f);
  457. // Enable RX start on first 4 bits
  458. st25r3916_change_reg_bits(
  459. handle,
  460. ST25R3916_REG_EMD_SUP_CONF,
  461. ST25R3916_REG_EMD_SUP_CONF_rx_start_emv,
  462. ST25R3916_REG_EMD_SUP_CONF_rx_start_emv_on);
  463. // Set antena tunning
  464. st25r3916_change_reg_bits(handle, ST25R3916_REG_ANT_TUNE_A, 0xff, 0x82);
  465. st25r3916_change_reg_bits(handle, ST25R3916_REG_ANT_TUNE_B, 0xff, 0x82);
  466. st25r3916_change_reg_bits(
  467. handle,
  468. ST25R3916_REG_OP_CONTROL,
  469. ST25R3916_REG_OP_CONTROL_en_fd_mask,
  470. ST25R3916_REG_OP_CONTROL_en_fd_auto_efd);
  471. // Perform calibration
  472. if(st25r3916_check_reg(
  473. handle, ST25R3916_REG_REGULATOR_CONTROL, ST25R3916_REG_REGULATOR_CONTROL_reg_s, 0x00)) {
  474. //FURI_LOG_I(TAG, "Adjusting regulators");
  475. // Reset logic
  476. st25r3916_set_reg_bits(
  477. handle, ST25R3916_REG_REGULATOR_CONTROL, ST25R3916_REG_REGULATOR_CONTROL_reg_s);
  478. st25r3916_clear_reg_bits(
  479. handle, ST25R3916_REG_REGULATOR_CONTROL, ST25R3916_REG_REGULATOR_CONTROL_reg_s);
  480. st25r3916_direct_cmd(handle, ST25R3916_CMD_ADJUST_REGULATORS);
  481. furi_delay_ms(6);
  482. }
  483. furi_hal_nfc_low_power_mode_start();
  484. furi_hal_nfc_release();
  485. }
  486. int32_t mifare_nested_app(void* p) {
  487. UNUSED(p);
  488. // Do required stuff
  489. remove_new_nfc_and_init_old();
  490. MifareNested* mifare_nested = mifare_nested_alloc();
  491. scene_manager_next_scene(mifare_nested->scene_manager, MifareNestedSceneStart);
  492. view_dispatcher_run(mifare_nested->view_dispatcher);
  493. mifare_nested_free(mifare_nested);
  494. // Lets hope nothing goes wrong
  495. remove_old_nfc_and_init_new_back();
  496. return 0;
  497. }