rfid-reader.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "rfid-reader.h"
  2. #include <furi.h>
  3. #include <furi-hal.h>
  4. #include <stm32wbxx_ll_cortex.h>
  5. #include <tim.h>
  6. extern COMP_HandleTypeDef hcomp1;
  7. /**
  8. * @brief private violation assistant for RfidReader
  9. */
  10. struct RfidReaderAccessor {
  11. static void decode(RfidReader& rfid_reader, bool polarity) {
  12. rfid_reader.decode(polarity);
  13. }
  14. };
  15. void RfidReader::decode(bool polarity) {
  16. uint32_t current_dwt_value = DWT->CYCCNT;
  17. uint32_t period = current_dwt_value - last_dwt_value;
  18. last_dwt_value = current_dwt_value;
  19. #ifdef RFID_GPIO_DEBUG
  20. decoder_gpio_out.process_front(polarity, period);
  21. #endif
  22. switch(type) {
  23. case Type::Normal:
  24. decoder_em.process_front(polarity, period);
  25. decoder_hid26.process_front(polarity, period);
  26. break;
  27. case Type::Indala:
  28. decoder_em.process_front(polarity, period);
  29. decoder_hid26.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 = osKernelGetTickFreq() * 2.0f;
  37. return (osKernelGetTickCount() - switch_os_tick_last) > seconds_to_switch;
  38. }
  39. void RfidReader::switch_timer_reset() {
  40. switch_os_tick_last = osKernelGetTickCount();
  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(void* hcomp, void* comp_ctx) {
  56. COMP_HandleTypeDef* _hcomp = static_cast<COMP_HandleTypeDef*>(hcomp);
  57. RfidReader* _this = static_cast<RfidReader*>(comp_ctx);
  58. if(hcomp == &hcomp1) {
  59. RfidReaderAccessor::decode(
  60. *_this, (HAL_COMP_GetOutputLevel(_hcomp) == COMP_OUTPUT_LEVEL_HIGH));
  61. }
  62. }
  63. RfidReader::RfidReader() {
  64. }
  65. void RfidReader::start() {
  66. type = Type::Normal;
  67. furi_hal_rfid_pins_read();
  68. furi_hal_rfid_tim_read(125000, 0.5);
  69. furi_hal_rfid_tim_read_start();
  70. start_comparator();
  71. switch_timer_reset();
  72. last_readed_count = 0;
  73. }
  74. void RfidReader::start_forced(RfidReader::Type _type) {
  75. start();
  76. if(_type == Type::Indala) {
  77. switch_mode();
  78. }
  79. }
  80. void RfidReader::stop() {
  81. furi_hal_rfid_pins_reset();
  82. furi_hal_rfid_tim_read_stop();
  83. furi_hal_rfid_tim_reset();
  84. stop_comparator();
  85. }
  86. bool RfidReader::read(LfrfidKeyType* _type, uint8_t* data, uint8_t data_size) {
  87. bool result = false;
  88. bool something_readed = false;
  89. // reading
  90. if(decoder_em.read(data, data_size)) {
  91. *_type = LfrfidKeyType::KeyEM4100;
  92. something_readed = true;
  93. }
  94. if(decoder_hid26.read(data, data_size)) {
  95. *_type = LfrfidKeyType::KeyH10301;
  96. something_readed = true;
  97. }
  98. if(decoder_indala.read(data, data_size)) {
  99. *_type = LfrfidKeyType::KeyI40134;
  100. something_readed = true;
  101. }
  102. // validation
  103. if(something_readed) {
  104. switch_timer_reset();
  105. if(last_readed_type == *_type && memcmp(last_readed_data, data, data_size) == 0) {
  106. last_readed_count = last_readed_count + 1;
  107. if(last_readed_count > 2) {
  108. result = true;
  109. }
  110. } else {
  111. last_readed_type = *_type;
  112. memcpy(last_readed_data, data, data_size);
  113. last_readed_count = 0;
  114. }
  115. }
  116. // mode switching
  117. if(switch_timer_elapsed()) {
  118. switch_mode();
  119. last_readed_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_readed_count > 0;
  133. }
  134. void RfidReader::start_comparator(void) {
  135. api_interrupt_add(comparator_trigger_callback, InterruptTypeComparatorTrigger, this);
  136. last_dwt_value = DWT->CYCCNT;
  137. hcomp1.Init.InputMinus = COMP_INPUT_MINUS_1_2VREFINT;
  138. hcomp1.Init.InputPlus = COMP_INPUT_PLUS_IO1;
  139. hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
  140. hcomp1.Init.Hysteresis = COMP_HYSTERESIS_HIGH;
  141. hcomp1.Init.BlankingSrce = COMP_BLANKINGSRC_NONE;
  142. hcomp1.Init.Mode = COMP_POWERMODE_MEDIUMSPEED;
  143. hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
  144. hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING_FALLING;
  145. if(HAL_COMP_Init(&hcomp1) != HAL_OK) {
  146. Error_Handler();
  147. }
  148. HAL_COMP_Start(&hcomp1);
  149. }
  150. void RfidReader::stop_comparator(void) {
  151. HAL_COMP_Stop(&hcomp1);
  152. api_interrupt_remove(comparator_trigger_callback, InterruptTypeComparatorTrigger);
  153. }