rfid_reader.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "rfid_reader.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <stm32wbxx_ll_cortex.h>
  5. /**
  6. * @brief private violation assistant for RfidReader
  7. */
  8. struct RfidReaderAccessor {
  9. static void decode(RfidReader& rfid_reader, bool polarity) {
  10. rfid_reader.decode(polarity);
  11. }
  12. };
  13. void RfidReader::decode(bool polarity) {
  14. uint32_t current_dwt_value = DWT->CYCCNT;
  15. uint32_t period = current_dwt_value - last_dwt_value;
  16. last_dwt_value = current_dwt_value;
  17. #ifdef RFID_GPIO_DEBUG
  18. decoder_gpio_out.process_front(polarity, period);
  19. #endif
  20. switch(type) {
  21. case Type::Normal:
  22. decoder_em.process_front(polarity, period);
  23. decoder_hid26.process_front(polarity, period);
  24. decoder_ioprox.process_front(polarity, period);
  25. break;
  26. case Type::Indala:
  27. decoder_em.process_front(polarity, period);
  28. decoder_hid26.process_front(polarity, period);
  29. decoder_ioprox.process_front(polarity, period);
  30. decoder_indala.process_front(polarity, period);
  31. break;
  32. }
  33. detect_ticks++;
  34. }
  35. bool RfidReader::switch_timer_elapsed() {
  36. const uint32_t seconds_to_switch = furi_kernel_get_tick_frequency() * 2.0f;
  37. return (furi_get_tick() - switch_os_tick_last) > seconds_to_switch;
  38. }
  39. void RfidReader::switch_timer_reset() {
  40. switch_os_tick_last = furi_get_tick();
  41. }
  42. void RfidReader::switch_mode() {
  43. switch(type) {
  44. case Type::Normal:
  45. type = Type::Indala;
  46. furi_hal_rfid_change_read_config(62500.0f, 0.25f);
  47. break;
  48. case Type::Indala:
  49. type = Type::Normal;
  50. furi_hal_rfid_change_read_config(125000.0f, 0.5f);
  51. break;
  52. }
  53. switch_timer_reset();
  54. }
  55. static void comparator_trigger_callback(bool level, void* comp_ctx) {
  56. RfidReader* _this = static_cast<RfidReader*>(comp_ctx);
  57. RfidReaderAccessor::decode(*_this, !level);
  58. }
  59. RfidReader::RfidReader() {
  60. }
  61. void RfidReader::start() {
  62. type = Type::Normal;
  63. furi_hal_rfid_pins_read();
  64. furi_hal_rfid_tim_read(125000, 0.5);
  65. furi_hal_rfid_tim_read_start();
  66. start_comparator();
  67. switch_timer_reset();
  68. last_read_count = 0;
  69. }
  70. void RfidReader::start_forced(RfidReader::Type _type) {
  71. start();
  72. if(_type == Type::Indala) {
  73. switch_mode();
  74. }
  75. }
  76. void RfidReader::stop() {
  77. furi_hal_rfid_pins_reset();
  78. furi_hal_rfid_tim_read_stop();
  79. furi_hal_rfid_tim_reset();
  80. stop_comparator();
  81. }
  82. bool RfidReader::read(LfrfidKeyType* _type, uint8_t* data, uint8_t data_size, bool switch_enable) {
  83. bool result = false;
  84. bool something_read = false;
  85. // reading
  86. if(decoder_em.read(data, data_size)) {
  87. *_type = LfrfidKeyType::KeyEM4100;
  88. something_read = true;
  89. }
  90. if(decoder_hid26.read(data, data_size)) {
  91. *_type = LfrfidKeyType::KeyH10301;
  92. something_read = true;
  93. }
  94. if(decoder_ioprox.read(data, data_size)) {
  95. *_type = LfrfidKeyType::KeyIoProxXSF;
  96. something_read = true;
  97. }
  98. if(decoder_indala.read(data, data_size)) {
  99. *_type = LfrfidKeyType::KeyI40134;
  100. something_read = true;
  101. }
  102. // validation
  103. if(something_read) {
  104. switch_timer_reset();
  105. if(last_read_type == *_type && memcmp(last_read_data, data, data_size) == 0) {
  106. last_read_count = last_read_count + 1;
  107. if(last_read_count > 2) {
  108. result = true;
  109. }
  110. } else {
  111. last_read_type = *_type;
  112. memcpy(last_read_data, data, data_size);
  113. last_read_count = 0;
  114. }
  115. }
  116. // mode switching
  117. if(switch_enable && switch_timer_elapsed()) {
  118. switch_mode();
  119. last_read_count = 0;
  120. }
  121. return result;
  122. }
  123. bool RfidReader::detect() {
  124. bool detected = false;
  125. if(detect_ticks > 10) {
  126. detected = true;
  127. }
  128. detect_ticks = 0;
  129. return detected;
  130. }
  131. bool RfidReader::any_read() {
  132. return last_read_count > 0;
  133. }
  134. void RfidReader::start_comparator(void) {
  135. furi_hal_rfid_comp_set_callback(comparator_trigger_callback, this);
  136. last_dwt_value = DWT->CYCCNT;
  137. furi_hal_rfid_comp_start();
  138. }
  139. void RfidReader::stop_comparator(void) {
  140. furi_hal_rfid_comp_stop();
  141. furi_hal_rfid_comp_set_callback(NULL, NULL);
  142. }