lf-rfid.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include <furi.h>
  2. #include <api-hal.h>
  3. #include <gui/gui.h>
  4. #include <stream_buffer.h>
  5. typedef enum { EventTypeTick, EventTypeKey, EventTypeRx } EventType;
  6. typedef struct {
  7. uint8_t dummy;
  8. } RxEvent;
  9. typedef struct {
  10. union {
  11. InputEvent input;
  12. RxEvent rx;
  13. } value;
  14. EventType type;
  15. } AppEvent;
  16. typedef struct {
  17. uint32_t freq_khz;
  18. bool on;
  19. uint8_t customer_id;
  20. uint32_t em_data;
  21. bool dirty;
  22. bool dirty_freq;
  23. } State;
  24. static void render_callback(Canvas* canvas, void* ctx) {
  25. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  26. canvas_clear(canvas);
  27. canvas_set_color(canvas, ColorBlack);
  28. canvas_set_font(canvas, FontPrimary);
  29. canvas_draw_str(canvas, 2, 12, "LF RFID");
  30. canvas_draw_str(canvas, 2, 24, state->on ? "Reading" : "Emulating");
  31. char buf[14];
  32. sprintf(buf, "%d kHz", (int)state->freq_khz);
  33. canvas_draw_str(canvas, 2, 36, buf);
  34. sprintf(buf, "%02d:%010ld", state->customer_id, state->em_data);
  35. canvas_draw_str(canvas, 2, 45, buf);
  36. release_mutex((ValueMutex*)ctx, state);
  37. }
  38. static void input_callback(InputEvent* input_event, void* ctx) {
  39. osMessageQueueId_t event_queue = ctx;
  40. AppEvent event;
  41. event.type = EventTypeKey;
  42. event.value.input = *input_event;
  43. osMessageQueuePut(event_queue, &event, 0, 0);
  44. }
  45. extern TIM_HandleTypeDef TIM_C;
  46. void em4100_emulation(uint8_t* data, GpioPin* pin);
  47. void prepare_data(uint32_t ID, uint32_t VENDOR, uint8_t* data);
  48. GpioPin debug_0 = {.pin = GPIO_PIN_2, .port = GPIOB};
  49. GpioPin debug_1 = {.pin = GPIO_PIN_3, .port = GPIOC};
  50. extern COMP_HandleTypeDef hcomp1;
  51. typedef struct {
  52. osMessageQueueId_t event_queue;
  53. uint32_t prev_dwt;
  54. int8_t symbol;
  55. bool center;
  56. size_t symbol_cnt;
  57. StreamBufferHandle_t stream_buffer;
  58. uint8_t* int_buffer;
  59. } ComparatorCtx;
  60. void init_comp_ctx(ComparatorCtx* ctx) {
  61. ctx->prev_dwt = 0;
  62. ctx->symbol = -1; // init state
  63. ctx->center = false;
  64. ctx->symbol_cnt = 0;
  65. xStreamBufferReset(ctx->stream_buffer);
  66. for(size_t i = 0; i < 64; i++) {
  67. ctx->int_buffer[i] = 0;
  68. }
  69. }
  70. void comparator_trigger_callback(void* hcomp, void* comp_ctx) {
  71. ComparatorCtx* ctx = (ComparatorCtx*)comp_ctx;
  72. uint32_t dt = (DWT->CYCCNT - ctx->prev_dwt) / (SystemCoreClock / 1000000.0f);
  73. ctx->prev_dwt = DWT->CYCCNT;
  74. if(dt < 150) return; // supress noise
  75. // wait message will be consumed
  76. if(xStreamBufferBytesAvailable(ctx->stream_buffer) == 64) return;
  77. gpio_write(&debug_0, true);
  78. // TOOD F4 and F5 differ
  79. bool rx_value = get_rfid_in_level();
  80. if(dt > 384) {
  81. // change symbol 0->1 or 1->0
  82. ctx->symbol = rx_value;
  83. ctx->center = true;
  84. } else {
  85. // same symbol as prev or center
  86. ctx->center = !ctx->center;
  87. }
  88. /*
  89. gpio_write(&debug_1, true);
  90. delay_us(center ? 10 : 30);
  91. gpio_write(&debug_1, false);
  92. */
  93. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  94. if(ctx->center && ctx->symbol != -1) {
  95. /*
  96. gpio_write(&debug_0, true);
  97. delay_us(symbol ? 10 : 30);
  98. gpio_write(&debug_0, false);
  99. */
  100. ctx->int_buffer[ctx->symbol_cnt] = ctx->symbol;
  101. ctx->symbol_cnt++;
  102. }
  103. // check preamble
  104. if(ctx->symbol_cnt <= 9 && ctx->symbol == 0) {
  105. ctx->symbol_cnt = 0;
  106. ctx->symbol = -1;
  107. }
  108. // check stop bit
  109. if(ctx->symbol_cnt == 64 && ctx->symbol == 1) {
  110. ctx->symbol_cnt = 0;
  111. ctx->symbol = -1;
  112. }
  113. // TODO
  114. // write only 9..64 symbols directly to streambuffer
  115. if(ctx->symbol_cnt == 64) {
  116. if(xStreamBufferSendFromISR(
  117. ctx->stream_buffer, ctx->int_buffer, 64, &xHigherPriorityTaskWoken) == 64) {
  118. AppEvent event;
  119. event.type = EventTypeRx;
  120. osMessageQueuePut(ctx->event_queue, &event, 0, 0);
  121. }
  122. ctx->symbol_cnt = 0;
  123. }
  124. gpio_write(&debug_0, false);
  125. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  126. }
  127. const uint8_t ROW_SIZE = 4;
  128. const uint8_t LINE_SIZE = 10;
  129. static bool even_check(uint8_t* buf) {
  130. uint8_t col_parity_sum[ROW_SIZE];
  131. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  132. col_parity_sum[col] = 0;
  133. }
  134. // line parity
  135. for(uint8_t line = 0; line < LINE_SIZE; line++) {
  136. printf("%d: ", line);
  137. uint8_t parity_sum = 0;
  138. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  139. parity_sum += buf[line * (ROW_SIZE + 1) + col];
  140. col_parity_sum[col] += buf[line * (ROW_SIZE + 1) + col];
  141. printf("%d ", buf[line * (ROW_SIZE + 1) + col]);
  142. }
  143. if((1 & parity_sum) != buf[line * (ROW_SIZE + 1) + ROW_SIZE]) {
  144. printf(
  145. "line parity fail at %d (%d : %d)\n",
  146. line,
  147. parity_sum,
  148. buf[line * (ROW_SIZE + 1) + ROW_SIZE]);
  149. return false;
  150. }
  151. printf("\r\n");
  152. }
  153. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  154. if((1 & col_parity_sum[col]) != buf[LINE_SIZE * (ROW_SIZE + 1) + col]) {
  155. printf(
  156. "col parity fail at %d (%d : %d)\n",
  157. col,
  158. col_parity_sum[col],
  159. buf[LINE_SIZE * (ROW_SIZE + 1) + col]);
  160. return false;
  161. }
  162. }
  163. return true;
  164. }
  165. static void extract_data(uint8_t* buf, uint8_t* customer, uint32_t* em_data) {
  166. uint32_t data = 0;
  167. uint8_t offset = 0;
  168. printf("customer: ");
  169. for(uint8_t line = 0; line < 2; line++) {
  170. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  171. uint32_t bit = buf[line * (ROW_SIZE + 1) + col];
  172. data |= bit << (7 - offset);
  173. printf("%ld ", bit);
  174. offset++;
  175. }
  176. }
  177. printf("\r\n");
  178. *customer = data;
  179. data = 0;
  180. offset = 0;
  181. printf("data: ");
  182. for(uint8_t line = 2; line < LINE_SIZE; line++) {
  183. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  184. uint32_t bit = buf[line * (ROW_SIZE + 1) + col];
  185. data |= bit << (31 - offset);
  186. printf("%ld ", bit);
  187. offset++;
  188. }
  189. }
  190. printf("\r\n");
  191. *em_data = data;
  192. }
  193. int32_t lf_rfid_workaround(void* p) {
  194. osMessageQueueId_t event_queue = osMessageQueueNew(2, sizeof(AppEvent), NULL);
  195. // create pin
  196. GpioPin pull_pin = {.pin = RFID_PULL_Pin, .port = RFID_PULL_GPIO_Port};
  197. // TODO open record
  198. GpioPin* pull_pin_record = &pull_pin;
  199. gpio_init(pull_pin_record, GpioModeOutputPushPull);
  200. gpio_init(&debug_0, GpioModeOutputPushPull);
  201. gpio_init(&debug_1, GpioModeOutputPushPull);
  202. // pulldown iBtn pin to prevent interference from ibutton
  203. gpio_init((GpioPin*)&ibutton_gpio, GpioModeOutputOpenDrain);
  204. gpio_write((GpioPin*)&ibutton_gpio, false);
  205. // init ctx
  206. ComparatorCtx comp_ctx;
  207. // internal buffer
  208. uint8_t int_bufer[64];
  209. comp_ctx.stream_buffer = xStreamBufferCreate(64, 64);
  210. comp_ctx.int_buffer = int_bufer;
  211. comp_ctx.event_queue = event_queue;
  212. init_comp_ctx(&comp_ctx);
  213. if(comp_ctx.stream_buffer == NULL) {
  214. printf("cannot create stream buffer\r\n");
  215. return 255;
  216. }
  217. // start comp
  218. HAL_COMP_Start(&hcomp1);
  219. uint8_t raw_data[64];
  220. for(size_t i = 0; i < 64; i++) {
  221. raw_data[i] = 0;
  222. }
  223. State _state;
  224. _state.freq_khz = 125;
  225. _state.on = false;
  226. _state.customer_id = 00;
  227. _state.em_data = 4378151;
  228. _state.dirty = true;
  229. _state.dirty_freq = true;
  230. ValueMutex state_mutex;
  231. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  232. printf("cannot create mutex\r\n");
  233. return 255;
  234. }
  235. ViewPort* view_port = view_port_alloc();
  236. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  237. view_port_input_callback_set(view_port, input_callback, event_queue);
  238. // Open GUI and register view_port
  239. Gui* gui = furi_record_open("gui");
  240. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  241. AppEvent event;
  242. while(1) {
  243. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 1024 / 8);
  244. if(event.type == EventTypeRx && event_status == osOK) {
  245. size_t received = xStreamBufferReceive(comp_ctx.stream_buffer, raw_data, 64, 0);
  246. printf("received: %d\r\n", received);
  247. if(received == 64) {
  248. if(even_check(&raw_data[9])) {
  249. State* state = (State*)acquire_mutex_block(&state_mutex);
  250. extract_data(&raw_data[9], &state->customer_id, &state->em_data);
  251. printf("customer: %02d, data: %010lu\n", state->customer_id, state->em_data);
  252. release_mutex(&state_mutex, state);
  253. view_port_update(view_port);
  254. api_hal_light_set(LightGreen, 0xFF);
  255. osDelay(50);
  256. api_hal_light_set(LightGreen, 0x00);
  257. }
  258. }
  259. } else {
  260. State* state = (State*)acquire_mutex_block(&state_mutex);
  261. if(event_status == osOK) {
  262. if(event.type == EventTypeKey) {
  263. // press events
  264. if(event.value.input.type == InputTypePress &&
  265. event.value.input.key == InputKeyBack) {
  266. hal_pwmn_stop(&TIM_C, TIM_CHANNEL_1); // TODO: move to furiac_onexit
  267. api_interrupt_remove(
  268. comparator_trigger_callback, InterruptTypeComparatorTrigger);
  269. gpio_init(pull_pin_record, GpioModeInput);
  270. gpio_init((GpioPin*)&ibutton_gpio, GpioModeInput);
  271. // TODO remove all view_ports create by app
  272. view_port_enabled_set(view_port, false);
  273. return 255;
  274. }
  275. if(event.value.input.type == InputTypePress &&
  276. event.value.input.key == InputKeyUp) {
  277. state->dirty_freq = true;
  278. state->freq_khz += 10;
  279. }
  280. if(event.value.input.type == InputTypePress &&
  281. event.value.input.key == InputKeyDown) {
  282. state->dirty_freq = true;
  283. state->freq_khz -= 10;
  284. }
  285. if(event.value.input.type == InputTypePress &&
  286. event.value.input.key == InputKeyLeft) {
  287. }
  288. if(event.value.input.type == InputTypePress &&
  289. event.value.input.key == InputKeyRight) {
  290. }
  291. if(event.value.input.type == InputTypePress &&
  292. event.value.input.key == InputKeyOk) {
  293. state->dirty = true;
  294. state->on = !state->on;
  295. }
  296. }
  297. } else {
  298. // event timeout
  299. }
  300. if(state->dirty) {
  301. if(state->on) {
  302. gpio_write(pull_pin_record, false);
  303. init_comp_ctx(&comp_ctx);
  304. api_interrupt_add(
  305. comparator_trigger_callback, InterruptTypeComparatorTrigger, &comp_ctx);
  306. } else {
  307. prepare_data(state->em_data, state->customer_id, raw_data);
  308. api_interrupt_remove(
  309. comparator_trigger_callback, InterruptTypeComparatorTrigger);
  310. }
  311. state->dirty_freq = true; // config new PWM next
  312. state->dirty = false;
  313. }
  314. if(state->dirty_freq) {
  315. hal_pwmn_set(
  316. state->on ? 0.5 : 0.0, (float)(state->freq_khz * 1000), &LFRFID_TIM, LFRFID_CH);
  317. state->dirty_freq = false;
  318. }
  319. if(!state->on) {
  320. em4100_emulation(raw_data, pull_pin_record);
  321. }
  322. release_mutex(&state_mutex, state);
  323. view_port_update(view_port);
  324. }
  325. }
  326. return 0;
  327. }