Skip to content

Commit 366030e

Browse files
committed
Debug logging and minor cleanup
1 parent d84391b commit 366030e

File tree

3 files changed

+48
-83
lines changed

3 files changed

+48
-83
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Internal TX improvements:
3434

3535
External RX options (What is simplest read module?):
3636
- Some UART mag reader (bulky, but likely easiest to read over GPIO, and means one can read all tracks)
37-
- Square audio jack mag reader (compact, but will be harder to decode from GPIO. Also only will read track 2 without modification)
37+
- Square audio jack mag reader (this may be DOA; seems like newer versions of the Square modules have some form of preprocessing, that also modifies the signal, perhaps in an effort to discourage folks using their hardware independent of their software. Thanks @[arha](https://github.com/arha) for your work investigating this)
38+
- Some read-head directly connected to GPIO, ADC'd, and parsed all on the Flipper. Likely the most compact and cheapest module option, but also would require the most work.
3839
- USB HID input feasible? Flipper seemingly can't act as an HID host, is there any way to circumvent this or is it due to a hardware incompatibility? This would be the easiest / best option all-around if feasible.
3940

4041
----

helpers/mag_helpers.c

+46-80
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#include "mag_helpers.h"
2-
#include "../mag_i.h"
2+
3+
#define TAG "MagHelpers"
34

45
#define GPIO_PIN_A &gpio_ext_pa6
56
#define GPIO_PIN_B &gpio_ext_pa7
6-
#define RFID_PIN &gpio_rfid_carrier_out
7+
#define GPIO_PIN_ENABLE &gpio_ext_pa4
8+
#define RFID_PIN_OUT &gpio_rfid_carrier_out
79

810
#define ZERO_PREFIX 25 // n zeros prefix
911
#define ZERO_BETWEEN 53 // n zeros between tracks
1012
#define ZERO_SUFFIX 25 // n zeros suffix
11-
//#define US_CLOCK 240
12-
//#define US_INTERPACKET 10
1313

1414
// bits per char on a given track
1515
const uint8_t bitlen[] = {7, 5, 5};
@@ -20,12 +20,12 @@ uint8_t bit_dir = 0;
2020
void play_bit_rfid(uint8_t send_bit, MagSetting* setting) {
2121
// internal TX over RFID coil
2222
bit_dir ^= 1;
23-
furi_hal_gpio_write(RFID_PIN, bit_dir);
23+
furi_hal_gpio_write(RFID_PIN_OUT, bit_dir);
2424
furi_delay_us(setting->us_clock);
2525

2626
if(send_bit) {
2727
bit_dir ^= 1;
28-
furi_hal_gpio_write(RFID_PIN, bit_dir);
28+
furi_hal_gpio_write(RFID_PIN_OUT, bit_dir);
2929
}
3030
furi_delay_us(setting->us_clock);
3131

@@ -51,11 +51,14 @@ void play_bit_gpio(uint8_t send_bit, MagSetting* setting) {
5151

5252
bool play_bit(uint8_t send_bit, MagSetting* setting) {
5353
// Initialize configured TX method
54-
if(setting->tx == MagTxStateRFID) {
54+
switch(setting->tx) {
55+
case MagTxStateRFID:
5556
play_bit_rfid(send_bit, setting);
56-
} else if(setting->tx == MagTxStateGPIOA6A7) {
57+
break;
58+
case MagTxStateGPIOA6A7:
5759
play_bit_gpio(send_bit, setting);
58-
} else {
60+
break;
61+
default:
5962
return false;
6063
}
6164

@@ -81,15 +84,15 @@ void tx_init_rfid() {
8184
furi_hal_gpio_init(&gpio_nfc_irq_rfid_pull, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
8285
furi_hal_gpio_write(&gpio_nfc_irq_rfid_pull, false);
8386

84-
furi_hal_gpio_init(RFID_PIN, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
87+
furi_hal_gpio_init(RFID_PIN_OUT, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
8588

8689
// confirm this delay is needed / sufficient? legacy from hackathon...
8790
furi_delay_ms(300);
8891
}
8992

9093
void tx_reset_rfid() {
9194
// reset RFID system
92-
furi_hal_gpio_write(RFID_PIN, 0);
95+
furi_hal_gpio_write(RFID_PIN_OUT, 0);
9396

9497
furi_hal_rfid_pins_reset();
9598
furi_hal_power_disable_otg();
@@ -100,23 +103,33 @@ void tx_init_gpio() {
100103
// gpio_item_configure_all_pins(GpioModeOutputPushPull);
101104
furi_hal_gpio_init(GPIO_PIN_A, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
102105
furi_hal_gpio_init(GPIO_PIN_B, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
106+
furi_hal_gpio_init(GPIO_PIN_ENABLE, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
107+
108+
furi_delay_ms(300);
109+
110+
furi_hal_gpio_write(GPIO_PIN_ENABLE, 1);
103111
}
104112

105113
void tx_reset_gpio() {
106114
furi_hal_gpio_write(GPIO_PIN_A, 0);
107115
furi_hal_gpio_write(GPIO_PIN_B, 0);
116+
furi_hal_gpio_write(GPIO_PIN_ENABLE, 0);
108117

118+
// set back to analog output mode?
109119
//gpio_item_configure_all_pins(GpioModeAnalog);
110120
furi_hal_power_disable_otg();
111121
}
112122

113123
bool tx_init(MagSetting* setting) {
114124
// Initialize configured TX method
115-
if(setting->tx == MagTxStateRFID) {
125+
switch(setting->tx) {
126+
case MagTxStateRFID:
116127
tx_init_rfid();
117-
} else if(setting->tx == MagTxStateGPIOA6A7) {
128+
break;
129+
case MagTxStateGPIOA6A7:
118130
tx_init_gpio();
119-
} else {
131+
break;
132+
default:
120133
return false;
121134
}
122135

@@ -125,11 +138,14 @@ bool tx_init(MagSetting* setting) {
125138

126139
bool tx_reset(MagSetting* setting) {
127140
// Reset configured TX method
128-
if(setting->tx == MagTxStateRFID) {
141+
switch(setting->tx) {
142+
case MagTxStateRFID:
129143
tx_reset_rfid();
130-
} else if(setting->tx == MagTxStateGPIOA6A7) {
144+
break;
145+
case MagTxStateGPIOA6A7:
131146
tx_reset_gpio();
132-
} else {
147+
break;
148+
default:
133149
return false;
134150
}
135151

@@ -174,79 +190,29 @@ void track_to_bits(uint8_t* bit_array, const char* track_data, uint8_t track_ind
174190
bit_array[i] = 2;
175191
i++;
176192

177-
//bool is_correct_length = (i == (strlen(track_data) * bitlen[track_index]));
178-
//furi_assert(is_correct_length);
179-
}
180-
181-
/*
182-
void mag_spoof_single_track_rfid(FuriString* track_str, uint8_t track_index) {
183-
// Quick testing...
184-
185-
tx_init_rfid();
186-
187-
size_t from;
188-
size_t to;
189-
190-
// TODO ';' in first track case
191-
if(track_index == 0) {
192-
from = furi_string_search_char(track_str, '%');
193-
to = furi_string_search_char(track_str, '?', from);
194-
} else if(track_index == 1) {
195-
from = furi_string_search_char(track_str, ';');
196-
to = furi_string_search_char(track_str, '?', from);
197-
} else {
198-
from = 0;
199-
to = furi_string_size(track_str);
200-
}
201-
if(from >= to) {
202-
return;
193+
// Log the output
194+
char output[100] = {0x0};
195+
//FURI_LOG_D(TAG, "%s", bit_array);
196+
FuriString* tmp_str;
197+
tmp_str = furi_string_alloc();
198+
for(uint8_t j = 0; bit_array[j] != 2; j++) {
199+
furi_string_printf(tmp_str, "%d", (bit_array[j] & 1));
200+
strcat(output, furi_string_get_cstr(tmp_str));
203201
}
204-
furi_string_mid(track_str, from, to - from + 1);
202+
FURI_LOG_D(TAG, "Track %d: %s", (track_index + 1), output);
203+
furi_string_free(tmp_str);
205204

206-
const char* data = furi_string_get_cstr(track_str);
207-
uint8_t bit_array[(strlen(data) * bitlen[track_index]) + 1];
208-
track_to_bits(bit_array, data, track_index);
209-
210-
FURI_CRITICAL_ENTER();
211-
for(uint8_t i = 0; i < ZERO_PREFIX; i++) play_bit_rfid(0);
212-
for(uint8_t i = 0; bit_array[i] != 2; i++) play_bit_rfid(bit_array[i] & 1);
213-
for(uint8_t i = 0; i < ZERO_SUFFIX; i++) play_bit_rfid(0);
214-
FURI_CRITICAL_EXIT();
215-
216-
tx_reset_rfid();
205+
//bool is_correct_length = (i == (strlen(track_data) * bitlen[track_index]));
206+
//furi_assert(is_correct_length);
217207
}
218208

219-
void mag_spoof_two_track_rfid(FuriString* track1, FuriString* track2) {
220-
// Quick testing...
221-
222-
tx_init_rfid();
223-
224-
const char* data1 = furi_string_get_cstr(track1);
225-
uint8_t bit_array1[(strlen(data1) * bitlen[0]) + 1];
226-
const char* data2 = furi_string_get_cstr(track2);
227-
uint8_t bit_array2[(strlen(data2) * bitlen[1]) + 1];
228-
229-
track_to_bits(bit_array1, data1, 0);
230-
track_to_bits(bit_array2, data2, 1);
231-
232-
FURI_CRITICAL_ENTER();
233-
for(uint8_t i = 0; i < ZERO_PREFIX; i++) play_bit_rfid(0);
234-
for(uint8_t i = 0; bit_array1[i] != 2; i++) play_bit_rfid(bit_array1[i] & 1);
235-
for(uint8_t i = 0; i < ZERO_BETWEEN; i++) play_bit_rfid(0);
236-
for(uint8_t i = 0; bit_array2[i] != 2; i++) play_bit_rfid(bit_array2[i] & 1);
237-
for(uint8_t i = 0; i < ZERO_SUFFIX; i++) play_bit_rfid(0);
238-
FURI_CRITICAL_EXIT();
239-
240-
tx_reset_rfid();
241-
}*/
242-
243209
void mag_spoof(Mag* mag) {
244210
MagSetting* setting = mag->setting;
245211

246212
// precompute tracks (WIP; ignores reverse and 3rd track)
247213
// likely will be reworked to Samy's bitmap method anyway...
248214
const char* data1 = furi_string_get_cstr(mag->mag_dev->dev_data.track[0].str);
249-
const char* data2 = furi_string_get_cstr(mag->mag_dev->dev_data.track[0].str);
215+
const char* data2 = furi_string_get_cstr(mag->mag_dev->dev_data.track[1].str);
250216
uint8_t bit_array1[(strlen(data1) * bitlen[0]) + 1];
251217
uint8_t bit_array2[(strlen(data2) * bitlen[1]) + 1];
252218
track_to_bits(bit_array1, data1, 0);

helpers/mag_helpers.h

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ void tx_reset_gpio();
1212
bool tx_init(MagSetting* setting);
1313
bool tx_reset(MagSetting* setting);
1414
void track_to_bits(uint8_t* bit_array, const char* track_data, uint8_t track_index);
15-
//void mag_spoof_single_track_rfid(FuriString* track_str, uint8_t track_index);
16-
//void mag_spoof_two_track_rfid(FuriString* track1, FuriString* track2);
1715
void mag_spoof(Mag* mag);
1816
void set_bit(uint8_t* b, uint32_t blen, uint32_t bitpos, bool val);
1917
bool get_bit(uint8_t* b, uint32_t blen, uint32_t bitpos);

0 commit comments

Comments
 (0)