rfid-reader.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "rfid-reader.h"
  2. #include <furi.h>
  3. #include <api-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. switch(type) {
  18. case Type::Normal:
  19. decoder_em.process_front(polarity, current_dwt_value - last_dwt_value);
  20. decoder_hid26.process_front(polarity, current_dwt_value - last_dwt_value);
  21. //decoder_indala.process_front(polarity, current_dwt_value - last_dwt_value);
  22. //decoder_analyzer.process_front(polarity, current_dwt_value - last_dwt_value);
  23. last_dwt_value = current_dwt_value;
  24. break;
  25. case Type::Indala:
  26. break;
  27. }
  28. }
  29. static void comparator_trigger_callback(void* hcomp, void* comp_ctx) {
  30. COMP_HandleTypeDef* _hcomp = static_cast<COMP_HandleTypeDef*>(hcomp);
  31. RfidReader* _this = static_cast<RfidReader*>(comp_ctx);
  32. if(hcomp == &hcomp1) {
  33. RfidReaderAccessor::decode(
  34. *_this, (HAL_COMP_GetOutputLevel(_hcomp) == COMP_OUTPUT_LEVEL_HIGH));
  35. }
  36. }
  37. RfidReader::RfidReader() {
  38. }
  39. void RfidReader::start(Type _type) {
  40. type = _type;
  41. start_gpio();
  42. switch(type) {
  43. case Type::Normal:
  44. start_timer();
  45. break;
  46. case Type::Indala:
  47. start_timer_indala();
  48. break;
  49. }
  50. start_comparator();
  51. }
  52. void RfidReader::stop() {
  53. stop_gpio();
  54. stop_timer();
  55. stop_comparator();
  56. }
  57. bool RfidReader::read(LfrfidKeyType* type, uint8_t* data, uint8_t data_size) {
  58. bool result = false;
  59. if(decoder_em.read(data, data_size)) {
  60. *type = LfrfidKeyType::KeyEM4100;
  61. result = true;
  62. }
  63. if(decoder_hid26.read(data, data_size)) {
  64. *type = LfrfidKeyType::KeyH10301;
  65. result = true;
  66. }
  67. //decoder_indala.read(NULL, 0);
  68. //decoder_analyzer.read(NULL, 0);
  69. return result;
  70. }
  71. void RfidReader::start_comparator(void) {
  72. api_interrupt_add(comparator_trigger_callback, InterruptTypeComparatorTrigger, this);
  73. last_dwt_value = DWT->CYCCNT;
  74. hcomp1.Init.InputMinus = COMP_INPUT_MINUS_1_2VREFINT;
  75. hcomp1.Init.InputPlus = COMP_INPUT_PLUS_IO1;
  76. hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
  77. hcomp1.Init.Hysteresis = COMP_HYSTERESIS_LOW;
  78. hcomp1.Init.BlankingSrce = COMP_BLANKINGSRC_NONE;
  79. hcomp1.Init.Mode = COMP_POWERMODE_MEDIUMSPEED;
  80. hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
  81. hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING_FALLING;
  82. if(HAL_COMP_Init(&hcomp1) != HAL_OK) {
  83. Error_Handler();
  84. }
  85. HAL_COMP_Start(&hcomp1);
  86. }
  87. void RfidReader::start_timer(void) {
  88. api_hal_rfid_tim_read(125000, 0.5);
  89. api_hal_rfid_tim_read_start();
  90. }
  91. void RfidReader::start_timer_indala(void) {
  92. api_hal_rfid_tim_read(62500, 0.25);
  93. api_hal_rfid_tim_read_start();
  94. }
  95. void RfidReader::start_gpio(void) {
  96. api_hal_rfid_pins_read();
  97. }
  98. void RfidReader::stop_comparator(void) {
  99. HAL_COMP_Stop(&hcomp1);
  100. api_interrupt_remove(comparator_trigger_callback, InterruptTypeComparatorTrigger);
  101. }
  102. void RfidReader::stop_timer(void) {
  103. api_hal_rfid_tim_read_stop();
  104. api_hal_rfid_tim_reset();
  105. }
  106. void RfidReader::stop_gpio(void) {
  107. api_hal_rfid_pins_reset();
  108. }