wiegand.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "wiegand.h"
  2. #include <furi.h>
  3. #include <api-hal.h>
  4. volatile unsigned long WIEGAND::_cardTempHigh = 0;
  5. volatile unsigned long WIEGAND::_cardTemp = 0;
  6. volatile unsigned long WIEGAND::_lastWiegand = 0;
  7. unsigned long WIEGAND::_code = 0;
  8. unsigned long WIEGAND::_codeHigh = 0;
  9. volatile int WIEGAND::_bitCount = 0;
  10. int WIEGAND::_wiegandType = 0;
  11. constexpr uint32_t clocks_in_ms = 64 * 1000;
  12. WIEGAND::WIEGAND() {
  13. }
  14. unsigned long WIEGAND::getCode() {
  15. return _code;
  16. }
  17. unsigned long WIEGAND::getCodeHigh() {
  18. return _codeHigh;
  19. }
  20. int WIEGAND::getWiegandType() {
  21. return _wiegandType;
  22. }
  23. bool WIEGAND::available() {
  24. bool ret;
  25. __disable_irq();
  26. ret = DoWiegandConversion();
  27. __enable_irq();
  28. return ret;
  29. }
  30. void input_isr(void* _pin, void* _ctx) {
  31. // interrupt manager get us pin constant, so...
  32. uint32_t pin = (uint32_t)_pin;
  33. WIEGAND* _this = static_cast<WIEGAND*>(_ctx);
  34. if(pin == gpio_ext_pa6.pin) {
  35. _this->ReadD0();
  36. }
  37. if(pin == gpio_ext_pa7.pin) {
  38. _this->ReadD1();
  39. }
  40. }
  41. void WIEGAND::begin() {
  42. _lastWiegand = 0;
  43. _cardTempHigh = 0;
  44. _cardTemp = 0;
  45. _code = 0;
  46. _wiegandType = 0;
  47. _bitCount = 0;
  48. const GpioPin* pinD0 = &gpio_ext_pa6;
  49. const GpioPin* pinD1 = &gpio_ext_pa7;
  50. hal_gpio_init(pinD0, GpioModeInterruptFall, GpioPullNo, GpioSpeedLow); // Set D0 pin as input
  51. hal_gpio_init(pinD1, GpioModeInterruptFall, GpioPullNo, GpioSpeedLow); // Set D1 pin as input
  52. api_interrupt_add(
  53. input_isr, InterruptTypeExternalInterrupt, this); // Hardware interrupt - high to low pulse
  54. }
  55. void WIEGAND::ReadD0() {
  56. _bitCount++; // Increament bit count for Interrupt connected to D0
  57. if(_bitCount > 31) // If bit count more than 31, process high bits
  58. {
  59. _cardTempHigh |= ((0x80000000 & _cardTemp) >> 31); // shift value to high bits
  60. _cardTempHigh <<= 1;
  61. _cardTemp <<= 1;
  62. } else {
  63. _cardTemp <<= 1; // D0 represent binary 0, so just left shift card data
  64. }
  65. _lastWiegand = DWT->CYCCNT; // Keep track of last wiegand bit received
  66. }
  67. void WIEGAND::ReadD1() {
  68. _bitCount++; // Increment bit count for Interrupt connected to D1
  69. if(_bitCount > 31) // If bit count more than 31, process high bits
  70. {
  71. _cardTempHigh |= ((0x80000000 & _cardTemp) >> 31); // shift value to high bits
  72. _cardTempHigh <<= 1;
  73. _cardTemp |= 1;
  74. _cardTemp <<= 1;
  75. } else {
  76. _cardTemp |= 1; // D1 represent binary 1, so OR card data with 1 then
  77. _cardTemp <<= 1; // left shift card data
  78. }
  79. _lastWiegand = DWT->CYCCNT; // Keep track of last wiegand bit received
  80. }
  81. unsigned long WIEGAND::GetCardId(
  82. volatile unsigned long* codehigh,
  83. volatile unsigned long* codelow,
  84. char bitlength) {
  85. if(bitlength == 26) // EM tag
  86. return (*codelow & 0x1FFFFFE) >> 1;
  87. if(bitlength == 24) return (*codelow & 0x7FFFFE) >> 1;
  88. if(bitlength == 34) // Mifare
  89. {
  90. *codehigh = *codehigh & 0x03; // only need the 2 LSB of the codehigh
  91. *codehigh <<= 30; // shift 2 LSB to MSB
  92. *codelow >>= 1;
  93. return *codehigh | *codelow;
  94. }
  95. if(bitlength == 32) {
  96. return (*codelow & 0x7FFFFFFE) >> 1;
  97. }
  98. return *codelow; // EM tag or Mifare without parity bits
  99. }
  100. char translateEnterEscapeKeyPress(char originalKeyPress) {
  101. switch(originalKeyPress) {
  102. case 0x0b: // 11 or * key
  103. return 0x0d; // 13 or ASCII ENTER
  104. case 0x0a: // 10 or # key
  105. return 0x1b; // 27 or ASCII ESCAPE
  106. default:
  107. return originalKeyPress;
  108. }
  109. }
  110. bool WIEGAND::DoWiegandConversion() {
  111. unsigned long cardID;
  112. unsigned long sysTick = DWT->CYCCNT;
  113. if((sysTick - _lastWiegand) >
  114. (25 * clocks_in_ms)) // if no more signal coming through after 25ms
  115. {
  116. if((_bitCount == 24) || (_bitCount == 26) || (_bitCount == 32) || (_bitCount == 34) ||
  117. (_bitCount == 37) || (_bitCount == 40) || (_bitCount == 8) ||
  118. (_bitCount ==
  119. 4)) // bitCount for keypress=4 or 8, Wiegand 26=24 or 26, Wiegand 34=32 or 34
  120. {
  121. _codeHigh = 0;
  122. // shift right 1 bit to get back the real value - interrupt done 1 left shift in advance
  123. _cardTemp >>= 1;
  124. // bit count more than 32 bits, shift high bits right to make adjustment
  125. if(_bitCount > 32) _cardTempHigh >>= 1;
  126. if(_bitCount == 8) // keypress wiegand with integrity
  127. {
  128. // 8-bit Wiegand keyboard data, high nibble is the "NOT" of low nibble
  129. // eg if key 1 pressed, data=E1 in binary 11100001 , high nibble=1110 , low nibble = 0001
  130. char highNibble = (_cardTemp & 0xf0) >> 4;
  131. char lowNibble = (_cardTemp & 0x0f);
  132. _wiegandType = _bitCount;
  133. _bitCount = 0;
  134. _cardTemp = 0;
  135. _cardTempHigh = 0;
  136. if(lowNibble ==
  137. (~highNibble & 0x0f)) // check if low nibble matches the "NOT" of high nibble.
  138. {
  139. _code = (int)translateEnterEscapeKeyPress(lowNibble);
  140. return true;
  141. } else {
  142. _lastWiegand = sysTick;
  143. _bitCount = 0;
  144. _cardTemp = 0;
  145. _cardTempHigh = 0;
  146. return false;
  147. }
  148. // TODO: Handle validation failure case!
  149. } else if(4 == _bitCount) {
  150. // 4-bit Wiegand codes have no data integrity check so we just
  151. // read the LOW nibble.
  152. _code = (int)translateEnterEscapeKeyPress(_cardTemp & 0x0000000F);
  153. _wiegandType = _bitCount;
  154. _bitCount = 0;
  155. _cardTemp = 0;
  156. _cardTempHigh = 0;
  157. return true;
  158. } else if(40 == _bitCount) {
  159. _cardTempHigh >>= 1;
  160. _code = _cardTemp;
  161. _codeHigh = _cardTempHigh;
  162. _wiegandType = _bitCount;
  163. _bitCount = 0;
  164. _cardTemp = 0;
  165. _cardTempHigh = 0;
  166. return true;
  167. } else {
  168. // wiegand 26 or wiegand 34
  169. cardID = GetCardId(&_cardTempHigh, &_cardTemp, _bitCount);
  170. _wiegandType = _bitCount;
  171. _bitCount = 0;
  172. _cardTemp = 0;
  173. _cardTempHigh = 0;
  174. _code = cardID;
  175. return true;
  176. }
  177. } else {
  178. // well time over 25 ms and bitCount !=8 , !=26, !=34 , must be noise or nothing then.
  179. _lastWiegand = sysTick;
  180. _bitCount = 0;
  181. _cardTemp = 0;
  182. _cardTempHigh = 0;
  183. return false;
  184. }
  185. } else
  186. return false;
  187. }