Skip to content

Commit

Permalink
Merge pull request #10 from zacharyweiss/settings
Browse files Browse the repository at this point in the history
GPIO Settings
  • Loading branch information
zacharyweiss authored Apr 28, 2024
2 parents 3ad981c + ec38afd commit 6f05f69
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 124 deletions.
111 changes: 60 additions & 51 deletions helpers/mag_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

#define TAG "MagHelpers"

// Haviv Board - pins gpio_ext_pa7 & gpio_ext_pa6 was swapped.
#define GPIO_PIN_A &gpio_ext_pa7
#define GPIO_PIN_B &gpio_ext_pa6
#define GPIO_PIN_ENABLE &gpio_ext_pa4
#define RFID_PIN_OUT &gpio_rfid_carrier_out

#define ZERO_PREFIX 25 // n zeros prefix
#define ZERO_BETWEEN 53 // n zeros between tracks
#define ZERO_SUFFIX 25 // n zeros suffix
Expand All @@ -19,18 +13,18 @@ const int sublen[] = {32, 48, 48};

uint8_t last_value = 2;

void play_halfbit(bool value, MagSetting* setting) {
switch(setting->tx) {
void play_halfbit(bool value, MagState* state) {
switch(state->tx) {
case MagTxStateRFID:
furi_hal_gpio_write(RFID_PIN_OUT, value);
furi_hal_gpio_write(&gpio_rfid_carrier_out, value);
/*furi_hal_gpio_write(RFID_PIN_OUT, !value);
furi_hal_gpio_write(RFID_PIN_OUT, value);
furi_hal_gpio_write(RFID_PIN_OUT, !value);
furi_hal_gpio_write(RFID_PIN_OUT, value);*/
break;
case MagTxStateGPIO:
furi_hal_gpio_write(GPIO_PIN_A, value);
furi_hal_gpio_write(GPIO_PIN_B, !value);
furi_hal_gpio_write(mag_state_enum_to_pin(state->pin_input), value);
furi_hal_gpio_write(mag_state_enum_to_pin(state->pin_output), !value);
break;
case MagTxStatePiezo:
furi_hal_gpio_write(&gpio_speaker, value);
Expand All @@ -41,7 +35,7 @@ void play_halfbit(bool value, MagSetting* setting) {

break;
case MagTxStateLF_P:
furi_hal_gpio_write(RFID_PIN_OUT, value);
furi_hal_gpio_write(&gpio_rfid_carrier_out, value);
furi_hal_gpio_write(&gpio_speaker, value);

/* // Weaker but cleaner signal
Expand Down Expand Up @@ -88,7 +82,7 @@ void play_halfbit(bool value, MagSetting* setting) {
last_value = value;
}

void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagSetting* setting, bool reverse) {
void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagState* state, bool reverse) {
for(uint16_t i = 0; i < n_bits; i++) {
uint16_t j = (reverse) ? (n_bits - i - 1) : i;
uint8_t byte = j / 8;
Expand Down Expand Up @@ -117,9 +111,9 @@ void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagSetting* setting,
// for DWT->CYCCNT. Note timer is aliased to 64us as per
// #define FURI_HAL_CORTEX_INSTRUCTIONS_PER_MICROSECOND (SystemCoreClock / 1000000) | furi_hal_cortex.c

play_halfbit(bit, setting);
furi_delay_us(setting->us_clock);
// if (i % 2 == 1) furi_delay_us(setting->us_interpacket);
play_halfbit(bit, state);
furi_delay_us(state->us_clock);
// if (i % 2 == 1) furi_delay_us(state->us_interpacket);
}
}

Expand All @@ -131,7 +125,7 @@ void tx_init_rfid() {
// furi_hal_ibutton_start_drive();
furi_hal_ibutton_pin_write(false);

// Initializing at GpioSpeedLow seems sufficient for our needs; no improvements seen by increasing speed setting
// Initializing at GpioSpeedLow seems sufficient for our needs; no improvements seen by increasing speed state

// this doesn't seem to make a difference, leaving it in
furi_hal_gpio_init(&gpio_rfid_data_in, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
Expand All @@ -143,14 +137,14 @@ void tx_init_rfid() {
furi_hal_gpio_init(&gpio_nfc_irq_rfid_pull, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_write(&gpio_nfc_irq_rfid_pull, false);

furi_hal_gpio_init(RFID_PIN_OUT, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(&gpio_rfid_carrier_out, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);

furi_delay_ms(300);
}

void tx_deinit_rfid() {
// reset RFID system
furi_hal_gpio_write(RFID_PIN_OUT, 0);
furi_hal_gpio_write(&gpio_rfid_carrier_out, 0);

furi_hal_rfid_pins_reset();
}
Expand Down Expand Up @@ -179,19 +173,31 @@ void tx_deinit_piezo() {
furi_hal_gpio_init(&gpio_speaker, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
}

bool tx_init(MagSetting* setting) {
bool tx_init(MagState* state) {
// Initialize configured TX method
switch(setting->tx) {
switch(state->tx) {
case MagTxStateRFID:
tx_init_rfid();
break;
case MagTxStateGPIO:
// gpio_item_configure_all_pins(GpioModeOutputPushPull);
furi_hal_gpio_init(GPIO_PIN_A, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(GPIO_PIN_B, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(GPIO_PIN_ENABLE, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);

furi_hal_gpio_write(GPIO_PIN_ENABLE, 1);
furi_hal_gpio_init(
mag_state_enum_to_pin(state->pin_input),
GpioModeOutputPushPull,
GpioPullNo,
GpioSpeedLow);
furi_hal_gpio_init(
mag_state_enum_to_pin(state->pin_output),
GpioModeOutputPushPull,
GpioPullNo,
GpioSpeedLow);
furi_hal_gpio_init(
mag_state_enum_to_pin(state->pin_enable),
GpioModeOutputPushPull,
GpioPullNo,
GpioSpeedLow);

furi_hal_gpio_write(mag_state_enum_to_pin(state->pin_enable), 1);

// had some issues with ~300; bumped higher temporarily
furi_delay_ms(500);
Expand Down Expand Up @@ -219,21 +225,24 @@ bool tx_init(MagSetting* setting) {
return true;
}

bool tx_deinit(MagSetting* setting) {
bool tx_deinit(MagState* state) {
// Reset configured TX method
switch(setting->tx) {
switch(state->tx) {
case MagTxStateRFID:
tx_deinit_rfid();
break;
case MagTxStateGPIO:
furi_hal_gpio_write(GPIO_PIN_A, 0);
furi_hal_gpio_write(GPIO_PIN_B, 0);
furi_hal_gpio_write(GPIO_PIN_ENABLE, 0);
furi_hal_gpio_write(mag_state_enum_to_pin(state->pin_input), 0);
furi_hal_gpio_write(mag_state_enum_to_pin(state->pin_output), 0);
furi_hal_gpio_write(mag_state_enum_to_pin(state->pin_enable), 0);

// set back to analog output mode? - YES
furi_hal_gpio_init(GPIO_PIN_A, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(GPIO_PIN_B, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(GPIO_PIN_ENABLE, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(
mag_state_enum_to_pin(state->pin_input), GpioModeAnalog, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(
mag_state_enum_to_pin(state->pin_output), GpioModeAnalog, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(
mag_state_enum_to_pin(state->pin_enable), GpioModeAnalog, GpioPullNo, GpioSpeedLow);

//gpio_item_configure_all_pins(GpioModeAnalog);
break;
Expand Down Expand Up @@ -262,7 +271,7 @@ bool tx_deinit(MagSetting* setting) {
}

void mag_spoof(Mag* mag) {
MagSetting* setting = mag->setting;
MagState* state = &mag->state;

// TODO: cleanup this section. Possibly move precompute + tx_init to emulate_on_enter?
FuriString* ft1 = mag->mag_dev->dev_data.track[0].str;
Expand Down Expand Up @@ -320,47 +329,47 @@ void mag_spoof(Mag* mag) {
last_value = 2;
bool bit = false;

if(!tx_init(setting)) return;
if(!tx_init(state)) return;

FURI_CRITICAL_ENTER();
for(uint16_t i = 0; i < (ZERO_PREFIX * 2); i++) {
// is this right?
if(!!(i % 2)) bit ^= 1;
play_halfbit(bit, setting);
furi_delay_us(setting->us_clock);
play_halfbit(bit, state);
furi_delay_us(state->us_clock);
}

if((setting->track == MagTrackStateOneAndTwo) || (setting->track == MagTrackStateOne))
play_track((uint8_t*)bits_t1_manchester, bits_t1_count, setting, false);
if((state->track == MagTrackStateOneAndTwo) || (state->track == MagTrackStateOne))
play_track((uint8_t*)bits_t1_manchester, bits_t1_count, state, false);

if((setting->track == MagTrackStateOneAndTwo))
if((state->track == MagTrackStateOneAndTwo))
for(uint16_t i = 0; i < (ZERO_BETWEEN * 2); i++) {
if(!!(i % 2)) bit ^= 1;
play_halfbit(bit, setting);
furi_delay_us(setting->us_clock);
play_halfbit(bit, state);
furi_delay_us(state->us_clock);
}

if((setting->track == MagTrackStateOneAndTwo) || (setting->track == MagTrackStateTwo))
if((state->track == MagTrackStateOneAndTwo) || (state->track == MagTrackStateTwo))
play_track(
(uint8_t*)bits_t2_manchester,
bits_t2_count,
setting,
(setting->reverse == MagReverseStateOn));
state,
(state->reverse == MagReverseStateOn));

if((setting->track == MagTrackStateThree))
play_track((uint8_t*)bits_t3_manchester, bits_t3_count, setting, false);
if((state->track == MagTrackStateThree))
play_track((uint8_t*)bits_t3_manchester, bits_t3_count, state, false);

for(uint16_t i = 0; i < (ZERO_SUFFIX * 2); i++) {
if(!!(i % 2)) bit ^= 1;
play_halfbit(bit, setting);
furi_delay_us(setting->us_clock);
play_halfbit(bit, state);
furi_delay_us(state->us_clock);
}
FURI_CRITICAL_EXIT();

free(data1);
free(data2);
free(data3);
tx_deinit(setting);
tx_deinit(state);
}

uint16_t add_bit(bool value, uint8_t* out, uint16_t count) {
Expand Down
8 changes: 4 additions & 4 deletions helpers/mag_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#include <stdio.h>
#include <string.h>

void play_halfbit(bool value, MagSetting* setting);
void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagSetting* setting, bool reverse);
void play_halfbit(bool value, MagState* state);
void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagState* state, bool reverse);

void tx_init_rf(int hz);
void tx_init_rfid();
void tx_init_piezo();
bool tx_init(MagSetting* setting);
bool tx_init(MagState* state);
void tx_deinit_piezo();
void tx_deinit_rfid();
bool tx_deinit(MagSetting* setting);
bool tx_deinit(MagState* state);

uint16_t add_bit(bool value, uint8_t* out, uint16_t count);
uint16_t add_bit_manchester(bool value, uint8_t* out, uint16_t count);
Expand Down
33 changes: 3 additions & 30 deletions mag.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

#define TAG "Mag"

#define SETTING_DEFAULT_REVERSE MagReverseStateOff
#define SETTING_DEFAULT_TRACK MagTrackStateOneAndTwo
#define SETTING_DEFAULT_TX MagTxStateGPIO
#define SETTING_DEFAULT_US_CLOCK 240
#define SETTING_DEFAULT_US_INTERPACKET 10

static bool mag_debug_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
Mag* mag = context;
Expand All @@ -20,19 +14,6 @@ static bool mag_debug_back_event_callback(void* context) {
return scene_manager_handle_back_event(mag->scene_manager);
}

static MagSetting* mag_setting_alloc() {
// temp hardcoded defaults
MagSetting* setting = malloc(sizeof(MagSetting));
setting->reverse = SETTING_DEFAULT_REVERSE;
setting->track = SETTING_DEFAULT_TRACK;
setting->tx = SETTING_DEFAULT_TX;
setting->us_clock = SETTING_DEFAULT_US_CLOCK;
setting->us_interpacket = SETTING_DEFAULT_US_INTERPACKET;
setting->is_debug = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);

return setting;
}

static Mag* mag_alloc() {
Mag* mag = malloc(sizeof(Mag));

Expand All @@ -53,7 +34,7 @@ static Mag* mag_alloc() {
mag->view_dispatcher, mag_debug_back_event_callback);

mag->mag_dev = mag_device_alloc();
mag->setting = mag_setting_alloc();
mag_state_load(&mag->state);

// Open GUI record
mag->gui = furi_record_open(RECORD_GUI);
Expand Down Expand Up @@ -98,13 +79,9 @@ static Mag* mag_alloc() {
mag->expansion = furi_record_open(RECORD_EXPANSION);
expansion_disable(mag->expansion);

return mag;
}

static void mag_setting_free(MagSetting* setting) {
furi_assert(setting);
// Move UART here? conditional upon setting?

free(setting);
return mag;
}

static void mag_free(Mag* mag) {
Expand All @@ -118,10 +95,6 @@ static void mag_free(Mag* mag) {
mag_device_free(mag->mag_dev);
mag->mag_dev = NULL;

// Mag setting
mag_setting_free(mag->setting);
mag->setting = NULL;

// Submenu
view_dispatcher_remove_view(mag->view_dispatcher, MagViewSubmenu);
submenu_free(mag->submenu);
Expand Down
12 changes: 2 additions & 10 deletions mag_i.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "mag_device.h"
#include "mag_state.h"
//#include "helpers/mag_helpers.h"
#include "helpers/mag_types.h"

Expand Down Expand Up @@ -53,15 +54,6 @@ enum MagCustomEvent {
MagEventPopupClosed,
};

typedef struct {
MagTxState tx;
MagTrackState track;
MagReverseState reverse;
uint32_t us_clock;
uint32_t us_interpacket;
bool is_debug;
} MagSetting;

typedef struct {
ViewDispatcher* view_dispatcher;
Gui* gui;
Expand All @@ -76,7 +68,7 @@ typedef struct {
FuriString* file_name;
FuriString* args;

MagSetting* setting;
MagState state;

// Common views
Submenu* submenu;
Expand Down
Loading

0 comments on commit 6f05f69

Please sign in to comment.