22
33#define TAG "MagHelpers"
44
5- // Haviv Board - pins gpio_ext_pa7 & gpio_ext_pa6 was swapped.
6- #define GPIO_PIN_A &gpio_ext_pa7
7- #define GPIO_PIN_B &gpio_ext_pa6
8- #define GPIO_PIN_ENABLE &gpio_ext_pa4
9- #define RFID_PIN_OUT &gpio_rfid_carrier_out
10-
115#define ZERO_PREFIX 25 // n zeros prefix
126#define ZERO_BETWEEN 53 // n zeros between tracks
137#define ZERO_SUFFIX 25 // n zeros suffix
@@ -19,18 +13,18 @@ const int sublen[] = {32, 48, 48};
1913
2014uint8_t last_value = 2 ;
2115
22- void play_halfbit (bool value , MagSetting * setting ) {
23- switch (setting -> tx ) {
16+ void play_halfbit (bool value , MagState * state ) {
17+ switch (state -> tx ) {
2418 case MagTxStateRFID :
25- furi_hal_gpio_write (RFID_PIN_OUT , value );
19+ furi_hal_gpio_write (& gpio_rfid_carrier_out , value );
2620 /*furi_hal_gpio_write(RFID_PIN_OUT, !value);
2721 furi_hal_gpio_write(RFID_PIN_OUT, value);
2822 furi_hal_gpio_write(RFID_PIN_OUT, !value);
2923 furi_hal_gpio_write(RFID_PIN_OUT, value);*/
3024 break ;
3125 case MagTxStateGPIO :
32- furi_hal_gpio_write (GPIO_PIN_A , value );
33- furi_hal_gpio_write (GPIO_PIN_B , !value );
26+ furi_hal_gpio_write (mag_state_enum_to_pin ( state -> pin_input ) , value );
27+ furi_hal_gpio_write (mag_state_enum_to_pin ( state -> pin_output ) , !value );
3428 break ;
3529 case MagTxStatePiezo :
3630 furi_hal_gpio_write (& gpio_speaker , value );
@@ -41,7 +35,7 @@ void play_halfbit(bool value, MagSetting* setting) {
4135
4236 break ;
4337 case MagTxStateLF_P :
44- furi_hal_gpio_write (RFID_PIN_OUT , value );
38+ furi_hal_gpio_write (& gpio_rfid_carrier_out , value );
4539 furi_hal_gpio_write (& gpio_speaker , value );
4640
4741 /* // Weaker but cleaner signal
@@ -88,7 +82,7 @@ void play_halfbit(bool value, MagSetting* setting) {
8882 last_value = value ;
8983}
9084
91- void play_track (uint8_t * bits_manchester , uint16_t n_bits , MagSetting * setting , bool reverse ) {
85+ void play_track (uint8_t * bits_manchester , uint16_t n_bits , MagState * state , bool reverse ) {
9286 for (uint16_t i = 0 ; i < n_bits ; i ++ ) {
9387 uint16_t j = (reverse ) ? (n_bits - i - 1 ) : i ;
9488 uint8_t byte = j / 8 ;
@@ -117,9 +111,9 @@ void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagSetting* setting,
117111 // for DWT->CYCCNT. Note timer is aliased to 64us as per
118112 // #define FURI_HAL_CORTEX_INSTRUCTIONS_PER_MICROSECOND (SystemCoreClock / 1000000) | furi_hal_cortex.c
119113
120- play_halfbit (bit , setting );
121- furi_delay_us (setting -> us_clock );
122- // if (i % 2 == 1) furi_delay_us(setting ->us_interpacket);
114+ play_halfbit (bit , state );
115+ furi_delay_us (state -> us_clock );
116+ // if (i % 2 == 1) furi_delay_us(state ->us_interpacket);
123117 }
124118}
125119
@@ -131,7 +125,7 @@ void tx_init_rfid() {
131125 // furi_hal_ibutton_start_drive();
132126 furi_hal_ibutton_pin_write (false);
133127
134- // Initializing at GpioSpeedLow seems sufficient for our needs; no improvements seen by increasing speed setting
128+ // Initializing at GpioSpeedLow seems sufficient for our needs; no improvements seen by increasing speed state
135129
136130 // this doesn't seem to make a difference, leaving it in
137131 furi_hal_gpio_init (& gpio_rfid_data_in , GpioModeOutputPushPull , GpioPullNo , GpioSpeedLow );
@@ -143,14 +137,14 @@ void tx_init_rfid() {
143137 furi_hal_gpio_init (& gpio_nfc_irq_rfid_pull , GpioModeOutputPushPull , GpioPullNo , GpioSpeedLow );
144138 furi_hal_gpio_write (& gpio_nfc_irq_rfid_pull , false);
145139
146- furi_hal_gpio_init (RFID_PIN_OUT , GpioModeOutputPushPull , GpioPullNo , GpioSpeedLow );
140+ furi_hal_gpio_init (& gpio_rfid_carrier_out , GpioModeOutputPushPull , GpioPullNo , GpioSpeedLow );
147141
148142 furi_delay_ms (300 );
149143}
150144
151145void tx_deinit_rfid () {
152146 // reset RFID system
153- furi_hal_gpio_write (RFID_PIN_OUT , 0 );
147+ furi_hal_gpio_write (& gpio_rfid_carrier_out , 0 );
154148
155149 furi_hal_rfid_pins_reset ();
156150}
@@ -179,19 +173,31 @@ void tx_deinit_piezo() {
179173 furi_hal_gpio_init (& gpio_speaker , GpioModeAnalog , GpioPullNo , GpioSpeedLow );
180174}
181175
182- bool tx_init (MagSetting * setting ) {
176+ bool tx_init (MagState * state ) {
183177 // Initialize configured TX method
184- switch (setting -> tx ) {
178+ switch (state -> tx ) {
185179 case MagTxStateRFID :
186180 tx_init_rfid ();
187181 break ;
188182 case MagTxStateGPIO :
189183 // gpio_item_configure_all_pins(GpioModeOutputPushPull);
190- furi_hal_gpio_init (GPIO_PIN_A , GpioModeOutputPushPull , GpioPullNo , GpioSpeedLow );
191- furi_hal_gpio_init (GPIO_PIN_B , GpioModeOutputPushPull , GpioPullNo , GpioSpeedLow );
192- furi_hal_gpio_init (GPIO_PIN_ENABLE , GpioModeOutputPushPull , GpioPullNo , GpioSpeedLow );
193-
194- furi_hal_gpio_write (GPIO_PIN_ENABLE , 1 );
184+ furi_hal_gpio_init (
185+ mag_state_enum_to_pin (state -> pin_input ),
186+ GpioModeOutputPushPull ,
187+ GpioPullNo ,
188+ GpioSpeedLow );
189+ furi_hal_gpio_init (
190+ mag_state_enum_to_pin (state -> pin_output ),
191+ GpioModeOutputPushPull ,
192+ GpioPullNo ,
193+ GpioSpeedLow );
194+ furi_hal_gpio_init (
195+ mag_state_enum_to_pin (state -> pin_enable ),
196+ GpioModeOutputPushPull ,
197+ GpioPullNo ,
198+ GpioSpeedLow );
199+
200+ furi_hal_gpio_write (mag_state_enum_to_pin (state -> pin_enable ), 1 );
195201
196202 // had some issues with ~300; bumped higher temporarily
197203 furi_delay_ms (500 );
@@ -219,21 +225,24 @@ bool tx_init(MagSetting* setting) {
219225 return true;
220226}
221227
222- bool tx_deinit (MagSetting * setting ) {
228+ bool tx_deinit (MagState * state ) {
223229 // Reset configured TX method
224- switch (setting -> tx ) {
230+ switch (state -> tx ) {
225231 case MagTxStateRFID :
226232 tx_deinit_rfid ();
227233 break ;
228234 case MagTxStateGPIO :
229- furi_hal_gpio_write (GPIO_PIN_A , 0 );
230- furi_hal_gpio_write (GPIO_PIN_B , 0 );
231- furi_hal_gpio_write (GPIO_PIN_ENABLE , 0 );
235+ furi_hal_gpio_write (mag_state_enum_to_pin ( state -> pin_input ) , 0 );
236+ furi_hal_gpio_write (mag_state_enum_to_pin ( state -> pin_output ) , 0 );
237+ furi_hal_gpio_write (mag_state_enum_to_pin ( state -> pin_enable ) , 0 );
232238
233239 // set back to analog output mode? - YES
234- furi_hal_gpio_init (GPIO_PIN_A , GpioModeAnalog , GpioPullNo , GpioSpeedLow );
235- furi_hal_gpio_init (GPIO_PIN_B , GpioModeAnalog , GpioPullNo , GpioSpeedLow );
236- furi_hal_gpio_init (GPIO_PIN_ENABLE , GpioModeAnalog , GpioPullNo , GpioSpeedLow );
240+ furi_hal_gpio_init (
241+ mag_state_enum_to_pin (state -> pin_input ), GpioModeAnalog , GpioPullNo , GpioSpeedLow );
242+ furi_hal_gpio_init (
243+ mag_state_enum_to_pin (state -> pin_output ), GpioModeAnalog , GpioPullNo , GpioSpeedLow );
244+ furi_hal_gpio_init (
245+ mag_state_enum_to_pin (state -> pin_enable ), GpioModeAnalog , GpioPullNo , GpioSpeedLow );
237246
238247 //gpio_item_configure_all_pins(GpioModeAnalog);
239248 break ;
@@ -262,7 +271,7 @@ bool tx_deinit(MagSetting* setting) {
262271}
263272
264273void mag_spoof (Mag * mag ) {
265- MagSetting * setting = mag -> setting ;
274+ MagState * state = & mag -> state ;
266275
267276 // TODO: cleanup this section. Possibly move precompute + tx_init to emulate_on_enter?
268277 FuriString * ft1 = mag -> mag_dev -> dev_data .track [0 ].str ;
@@ -320,47 +329,47 @@ void mag_spoof(Mag* mag) {
320329 last_value = 2 ;
321330 bool bit = false;
322331
323- if (!tx_init (setting )) return ;
332+ if (!tx_init (state )) return ;
324333
325334 FURI_CRITICAL_ENTER ();
326335 for (uint16_t i = 0 ; i < (ZERO_PREFIX * 2 ); i ++ ) {
327336 // is this right?
328337 if (!!(i % 2 )) bit ^= 1 ;
329- play_halfbit (bit , setting );
330- furi_delay_us (setting -> us_clock );
338+ play_halfbit (bit , state );
339+ furi_delay_us (state -> us_clock );
331340 }
332341
333- if ((setting -> track == MagTrackStateOneAndTwo ) || (setting -> track == MagTrackStateOne ))
334- play_track ((uint8_t * )bits_t1_manchester , bits_t1_count , setting , false);
342+ if ((state -> track == MagTrackStateOneAndTwo ) || (state -> track == MagTrackStateOne ))
343+ play_track ((uint8_t * )bits_t1_manchester , bits_t1_count , state , false);
335344
336- if ((setting -> track == MagTrackStateOneAndTwo ))
345+ if ((state -> track == MagTrackStateOneAndTwo ))
337346 for (uint16_t i = 0 ; i < (ZERO_BETWEEN * 2 ); i ++ ) {
338347 if (!!(i % 2 )) bit ^= 1 ;
339- play_halfbit (bit , setting );
340- furi_delay_us (setting -> us_clock );
348+ play_halfbit (bit , state );
349+ furi_delay_us (state -> us_clock );
341350 }
342351
343- if ((setting -> track == MagTrackStateOneAndTwo ) || (setting -> track == MagTrackStateTwo ))
352+ if ((state -> track == MagTrackStateOneAndTwo ) || (state -> track == MagTrackStateTwo ))
344353 play_track (
345354 (uint8_t * )bits_t2_manchester ,
346355 bits_t2_count ,
347- setting ,
348- (setting -> reverse == MagReverseStateOn ));
356+ state ,
357+ (state -> reverse == MagReverseStateOn ));
349358
350- if ((setting -> track == MagTrackStateThree ))
351- play_track ((uint8_t * )bits_t3_manchester , bits_t3_count , setting , false);
359+ if ((state -> track == MagTrackStateThree ))
360+ play_track ((uint8_t * )bits_t3_manchester , bits_t3_count , state , false);
352361
353362 for (uint16_t i = 0 ; i < (ZERO_SUFFIX * 2 ); i ++ ) {
354363 if (!!(i % 2 )) bit ^= 1 ;
355- play_halfbit (bit , setting );
356- furi_delay_us (setting -> us_clock );
364+ play_halfbit (bit , state );
365+ furi_delay_us (state -> us_clock );
357366 }
358367 FURI_CRITICAL_EXIT ();
359368
360369 free (data1 );
361370 free (data2 );
362371 free (data3 );
363- tx_deinit (setting );
372+ tx_deinit (state );
364373}
365374
366375uint16_t add_bit (bool value , uint8_t * out , uint16_t count ) {
0 commit comments