mifare_nested.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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_set_event_callback_context(mifare_nested->view_dispatcher, mifare_nested);
  190. view_dispatcher_set_custom_event_callback(
  191. mifare_nested->view_dispatcher, mifare_nested_custom_event_callback);
  192. view_dispatcher_set_navigation_event_callback(
  193. mifare_nested->view_dispatcher, mifare_nested_back_event_callback);
  194. view_dispatcher_set_tick_event_callback(
  195. mifare_nested->view_dispatcher, mifare_nested_tick_event_callback, 100);
  196. // Nfc device
  197. mifare_nested->nfc_dev = nfc_device_alloc();
  198. // Open GUI record
  199. mifare_nested->gui = furi_record_open(RECORD_GUI);
  200. view_dispatcher_attach_to_gui(
  201. mifare_nested->view_dispatcher, mifare_nested->gui, ViewDispatcherTypeFullscreen);
  202. // Open Notification record
  203. mifare_nested->notifications = furi_record_open(RECORD_NOTIFICATION);
  204. // Submenu
  205. mifare_nested->submenu = submenu_alloc();
  206. view_dispatcher_add_view(
  207. mifare_nested->view_dispatcher,
  208. MifareNestedViewMenu,
  209. submenu_get_view(mifare_nested->submenu));
  210. // Popup
  211. mifare_nested->popup = popup_alloc();
  212. view_dispatcher_add_view(
  213. mifare_nested->view_dispatcher,
  214. MifareNestedViewPopup,
  215. popup_get_view(mifare_nested->popup));
  216. // Loading
  217. mifare_nested->loading = loading_alloc();
  218. view_dispatcher_add_view(
  219. mifare_nested->view_dispatcher,
  220. MifareNestedViewLoading,
  221. loading_get_view(mifare_nested->loading));
  222. // Text Input
  223. mifare_nested->text_input = text_input_alloc();
  224. view_dispatcher_add_view(
  225. mifare_nested->view_dispatcher,
  226. MifareNestedViewTextInput,
  227. text_input_get_view(mifare_nested->text_input));
  228. // Custom Widget
  229. mifare_nested->widget = widget_alloc();
  230. view_dispatcher_add_view(
  231. mifare_nested->view_dispatcher,
  232. MifareNestedViewWidget,
  233. widget_get_view(mifare_nested->widget));
  234. // Variable Item List
  235. mifare_nested->variable_item_list = variable_item_list_alloc();
  236. view_dispatcher_add_view(
  237. mifare_nested->view_dispatcher,
  238. MifareNestedViewVariableList,
  239. variable_item_list_get_view(mifare_nested->variable_item_list));
  240. // Nested attack state
  241. NestedState* plugin_state = collection_alloc();
  242. view_set_context(plugin_state->view, mifare_nested);
  243. mifare_nested->nested_state = plugin_state;
  244. view_dispatcher_add_view(
  245. mifare_nested->view_dispatcher, MifareNestedViewCollecting, plugin_state->view);
  246. // Check keys attack state
  247. CheckKeysState* keys_state = check_keys_alloc();
  248. view_set_context(keys_state->view, mifare_nested);
  249. mifare_nested->keys_state = keys_state;
  250. view_dispatcher_add_view(
  251. mifare_nested->view_dispatcher, MifareNestedViewCheckKeys, keys_state->view);
  252. KeyInfo_t* key_info = malloc(sizeof(KeyInfo_t));
  253. mifare_nested->keys = key_info;
  254. MifareNestedSettings* settings = malloc(sizeof(MifareNestedSettings));
  255. settings->only_hardnested = false;
  256. mifare_nested->settings = settings;
  257. view_set_draw_callback(plugin_state->view, nested_draw_callback);
  258. view_set_input_callback(plugin_state->view, nested_input_callback);
  259. view_set_draw_callback(keys_state->view, check_keys_draw_callback);
  260. view_set_input_callback(keys_state->view, nested_input_callback);
  261. mifare_nested->collecting_type = MifareNestedWorkerStateReady;
  262. mifare_nested->run = NestedRunIdle;
  263. return mifare_nested;
  264. }
  265. void mifare_nested_free(MifareNested* mifare_nested) {
  266. furi_assert(mifare_nested);
  267. // Nfc device
  268. nfc_device_free(mifare_nested->nfc_dev);
  269. // Submenu
  270. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewMenu);
  271. submenu_free(mifare_nested->submenu);
  272. // Popup
  273. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewPopup);
  274. popup_free(mifare_nested->popup);
  275. // Loading
  276. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewLoading);
  277. loading_free(mifare_nested->loading);
  278. // TextInput
  279. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewTextInput);
  280. text_input_free(mifare_nested->text_input);
  281. // Custom Widget
  282. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewWidget);
  283. widget_free(mifare_nested->widget);
  284. // Variable Item List
  285. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewVariableList);
  286. variable_item_list_free(mifare_nested->variable_item_list);
  287. // Nested
  288. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewCollecting);
  289. // Check keys
  290. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewCheckKeys);
  291. // Nonces states
  292. free(mifare_nested->nested_state);
  293. // Keys
  294. free(mifare_nested->keys);
  295. // Settings
  296. free(mifare_nested->settings);
  297. // Worker
  298. mifare_nested_worker_stop(mifare_nested->worker);
  299. mifare_nested_worker_free(mifare_nested->worker);
  300. // View Dispatcher
  301. view_dispatcher_free(mifare_nested->view_dispatcher);
  302. // Scene Manager
  303. scene_manager_free(mifare_nested->scene_manager);
  304. // GUI
  305. furi_record_close(RECORD_GUI);
  306. mifare_nested->gui = NULL;
  307. // Notifications
  308. furi_record_close(RECORD_NOTIFICATION);
  309. mifare_nested->notifications = NULL;
  310. free(mifare_nested);
  311. }
  312. void mifare_nested_blink_start(MifareNested* mifare_nested) {
  313. notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_start_blue);
  314. }
  315. void mifare_nested_blink_calibration_start(MifareNested* mifare_nested) {
  316. notification_message(
  317. mifare_nested->notifications, &mifare_nested_sequence_blink_start_magenta);
  318. }
  319. void mifare_nested_blink_nonce_collection_start(MifareNested* mifare_nested) {
  320. notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_start_yellow);
  321. }
  322. void mifare_nested_blink_stop(MifareNested* mifare_nested) {
  323. notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_stop);
  324. }
  325. void remove_new_nfc_and_init_old() {
  326. // Deinitialize new NFC and init legacy
  327. if(furi_hal_nfc_is_hal_ready() != FuriHalNfcErrorNone) {
  328. FURI_LOG_E("nested", "NFC chip failed to start\r\n");
  329. return;
  330. }
  331. furi_hal_nfc_acquire();
  332. furi_hal_nfc_reset_mode();
  333. furi_hal_nfc_low_power_mode_start();
  334. furi_hal_nfc_release();
  335. furry_hal_nfc_init();
  336. }
  337. void remove_old_nfc_and_init_new_back() {
  338. // Deinitialize NFC
  339. furry_hal_nfc_deinit();
  340. //
  341. furi_hal_nfc_acquire();
  342. furi_hal_nfc_reset_mode();
  343. furi_hal_nfc_low_power_mode_start();
  344. FuriHalSpiBusHandle* handle = &furi_hal_spi_bus_handle_nfc;
  345. // Set default state
  346. st25r3916_direct_cmd(handle, ST25R3916_CMD_SET_DEFAULT);
  347. // Increase IO driver strength of MISO and IRQ
  348. st25r3916_write_reg(handle, ST25R3916_REG_IO_CONF2, ST25R3916_REG_IO_CONF2_io_drv_lvl);
  349. // Check chip ID
  350. uint8_t chip_id = 0;
  351. st25r3916_read_reg(handle, ST25R3916_REG_IC_IDENTITY, &chip_id);
  352. if((chip_id & ST25R3916_REG_IC_IDENTITY_ic_type_mask) !=
  353. ST25R3916_REG_IC_IDENTITY_ic_type_st25r3916) {
  354. furi_hal_nfc_low_power_mode_start();
  355. furi_hal_nfc_release();
  356. return;
  357. }
  358. // Clear interrupts
  359. st25r3916_get_irq(handle);
  360. // Mask all interrupts
  361. st25r3916_mask_irq(handle, ST25R3916_IRQ_MASK_ALL);
  362. // Enable interrupts
  363. //furi_hal_nfc_init_gpio_isr();
  364. // Disable internal overheat protection
  365. st25r3916_change_test_reg_bits(handle, 0x04, 0x10, 0x10);
  366. // Measure voltage
  367. // Set measure power supply voltage source
  368. st25r3916_change_reg_bits(
  369. handle,
  370. ST25R3916_REG_REGULATOR_CONTROL,
  371. ST25R3916_REG_REGULATOR_CONTROL_mpsv_mask,
  372. ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd);
  373. // Enable timer and interrupt register
  374. st25r3916_mask_irq(handle, ~ST25R3916_IRQ_MASK_DCT);
  375. st25r3916_direct_cmd(handle, ST25R3916_CMD_MEASURE_VDD);
  376. //furi_hal_nfc_event_wait_for_specific_irq(handle, ST25R3916_IRQ_MASK_DCT, 100);
  377. st25r3916_mask_irq(handle, ST25R3916_IRQ_MASK_ALL);
  378. uint8_t ad_res = 0;
  379. st25r3916_read_reg(handle, ST25R3916_REG_AD_RESULT, &ad_res);
  380. uint16_t mV = ((uint16_t)ad_res) * 23U;
  381. mV += (((((uint16_t)ad_res) * 4U) + 5U) / 10U);
  382. if(mV < 3600) {
  383. st25r3916_change_reg_bits(
  384. handle,
  385. ST25R3916_REG_IO_CONF2,
  386. ST25R3916_REG_IO_CONF2_sup3V,
  387. ST25R3916_REG_IO_CONF2_sup3V_3V);
  388. } else {
  389. st25r3916_change_reg_bits(
  390. handle,
  391. ST25R3916_REG_IO_CONF2,
  392. ST25R3916_REG_IO_CONF2_sup3V,
  393. ST25R3916_REG_IO_CONF2_sup3V_5V);
  394. }
  395. // Disable MCU CLK
  396. st25r3916_change_reg_bits(
  397. handle,
  398. ST25R3916_REG_IO_CONF1,
  399. ST25R3916_REG_IO_CONF1_out_cl_mask | ST25R3916_REG_IO_CONF1_lf_clk_off,
  400. 0x07);
  401. // Disable MISO pull-down
  402. st25r3916_change_reg_bits(
  403. handle,
  404. ST25R3916_REG_IO_CONF2,
  405. ST25R3916_REG_IO_CONF2_miso_pd1 | ST25R3916_REG_IO_CONF2_miso_pd2,
  406. 0x00);
  407. // Set tx driver resistance to 1 Om
  408. st25r3916_change_reg_bits(
  409. handle, ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_d_res_mask, 0x00);
  410. // Use minimum non-overlap
  411. st25r3916_change_reg_bits(
  412. handle,
  413. ST25R3916_REG_RES_AM_MOD,
  414. ST25R3916_REG_RES_AM_MOD_fa3_f,
  415. ST25R3916_REG_RES_AM_MOD_fa3_f);
  416. // Set activation threashold
  417. st25r3916_change_reg_bits(
  418. handle,
  419. ST25R3916_REG_FIELD_THRESHOLD_ACTV,
  420. ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_mask,
  421. ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_105mV);
  422. st25r3916_change_reg_bits(
  423. handle,
  424. ST25R3916_REG_FIELD_THRESHOLD_ACTV,
  425. ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_mask,
  426. ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_105mV);
  427. // Set deactivation threashold
  428. st25r3916_change_reg_bits(
  429. handle,
  430. ST25R3916_REG_FIELD_THRESHOLD_DEACTV,
  431. ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_mask,
  432. ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_75mV);
  433. st25r3916_change_reg_bits(
  434. handle,
  435. ST25R3916_REG_FIELD_THRESHOLD_DEACTV,
  436. ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_mask,
  437. ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_75mV);
  438. // Enable external load modulation
  439. st25r3916_change_reg_bits(
  440. handle, ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_lm_ext, ST25R3916_REG_AUX_MOD_lm_ext);
  441. // Enable internal load modulation
  442. st25r3916_change_reg_bits(
  443. handle, ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_lm_dri, ST25R3916_REG_AUX_MOD_lm_dri);
  444. // Adjust FDT
  445. st25r3916_change_reg_bits(
  446. handle,
  447. ST25R3916_REG_PASSIVE_TARGET,
  448. ST25R3916_REG_PASSIVE_TARGET_fdel_mask,
  449. (5U << ST25R3916_REG_PASSIVE_TARGET_fdel_shift));
  450. // Reduce RFO resistance in Modulated state
  451. st25r3916_change_reg_bits(
  452. handle,
  453. ST25R3916_REG_PT_MOD,
  454. ST25R3916_REG_PT_MOD_ptm_res_mask | ST25R3916_REG_PT_MOD_pt_res_mask,
  455. 0x0f);
  456. // Enable RX start on first 4 bits
  457. st25r3916_change_reg_bits(
  458. handle,
  459. ST25R3916_REG_EMD_SUP_CONF,
  460. ST25R3916_REG_EMD_SUP_CONF_rx_start_emv,
  461. ST25R3916_REG_EMD_SUP_CONF_rx_start_emv_on);
  462. // Set antena tunning
  463. st25r3916_change_reg_bits(handle, ST25R3916_REG_ANT_TUNE_A, 0xff, 0x82);
  464. st25r3916_change_reg_bits(handle, ST25R3916_REG_ANT_TUNE_B, 0xff, 0x82);
  465. st25r3916_change_reg_bits(
  466. handle,
  467. ST25R3916_REG_OP_CONTROL,
  468. ST25R3916_REG_OP_CONTROL_en_fd_mask,
  469. ST25R3916_REG_OP_CONTROL_en_fd_auto_efd);
  470. // Perform calibration
  471. if(st25r3916_check_reg(
  472. handle, ST25R3916_REG_REGULATOR_CONTROL, ST25R3916_REG_REGULATOR_CONTROL_reg_s, 0x00)) {
  473. //FURI_LOG_I(TAG, "Adjusting regulators");
  474. // Reset logic
  475. st25r3916_set_reg_bits(
  476. handle, ST25R3916_REG_REGULATOR_CONTROL, ST25R3916_REG_REGULATOR_CONTROL_reg_s);
  477. st25r3916_clear_reg_bits(
  478. handle, ST25R3916_REG_REGULATOR_CONTROL, ST25R3916_REG_REGULATOR_CONTROL_reg_s);
  479. st25r3916_direct_cmd(handle, ST25R3916_CMD_ADJUST_REGULATORS);
  480. furi_delay_ms(6);
  481. }
  482. furi_hal_nfc_low_power_mode_start();
  483. furi_hal_nfc_release();
  484. }
  485. int32_t mifare_nested_app(void* p) {
  486. UNUSED(p);
  487. // Do required stuff
  488. remove_new_nfc_and_init_old();
  489. MifareNested* mifare_nested = mifare_nested_alloc();
  490. scene_manager_next_scene(mifare_nested->scene_manager, MifareNestedSceneStart);
  491. view_dispatcher_run(mifare_nested->view_dispatcher);
  492. mifare_nested_free(mifare_nested);
  493. // Lets hope nothing goes wrong
  494. remove_old_nfc_and_init_new_back();
  495. return 0;
  496. }