From f1c4a190e6ca863ebd5ca0a1629dda007f230af0 Mon Sep 17 00:00:00 2001 From: Derek Jamison Date: Tue, 2 May 2023 13:02:09 -0400 Subject: [PATCH] Add MOSFET support to Wiegand app. --- gpio/wiegand/README.md | 29 +++++++++++++++++++--- gpio/wiegand/scenes/wiegand_instructions.c | 18 +++++++++++++- gpio/wiegand/scenes/wiegand_play.c | 18 +++++++++++--- gpio/wiegand/wiegand.c | 2 ++ gpio/wiegand/wiegand.h | 2 ++ 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/gpio/wiegand/README.md b/gpio/wiegand/README.md index b492bce..bb3464a 100644 --- a/gpio/wiegand/README.md +++ b/gpio/wiegand/README.md @@ -2,9 +2,32 @@ This application supports W4, W8, W24, W26, W32, W34, W37 and W40 formats. -This application can be used to test Wiegand readers and keypads. It can -save the data to a file, and can load and replay the data. Timings are -measured and displayed; which can be used to help debug Wiegand readers. +This application can be used to test Wiegand readers and keypads. It can save the data to a file, and can load and replay the data. Timings are measured and displayed; which can be used to help debug Wiegand readers. + + +## Wiring +The D0 and D1 wires of the Wiegand must be about 1K or higher for the Flipper to be able to effectively pull them down to ground. If the line has a lower resistance, like 100 ohms, then you need to add MOSFETs to help pull the lines low. The following [YouTube video](https://youtu.be/OVyd3ffnZ0M) is a demonstration of how to correctly wire the device. + +In all configurations: +- Pin A7 goes to your Wiegand D1 (white) wire. +- Pin A4 goes to your Wiegand D0 (green) wire. +- Pin GND goes to your Wiegand GND (black) wire. +This is sufficient for reading signals with both this application and the Debug Accessor application. + +If the pull-up resistors on your card reader are less than 1K, you also need the following: +- Two additional MOSFETs are required. I used IRF540 (but perhaps IRL540 would be better?) +- Two additional Pull-down resitors are required. I used 5K, but 4.7K would be fine as well. +- I'm still learning, but perhaps a MOSFET driver / optocoupler would provide even more protection to the Flipper GPIO pins? Use this circuit at your own risk. :) + +Here is how you wire up the MOSFET and the pull-down resistors: +- The source pin of each MOSFET connects to ground. +- The gate pin connects to one side of a resistor. The other side of the resistor goes to ground. +- For the MOSFET used for D1: + - The gate pin of the MOSFET for D1 goes to pin A6 on the Flipper. (It should also already be connected to the pull-down resistor) + - The drain pin of the MOSFET for D1 goes to pin A7 on the Flipper. +- For the MOSFET used for D0: + - The gate pin of the MOSFET for D0 goes to pin B3 on the Flipper. (It should also already be connected to the pull-down resistor) + - The drain pin of the MOSFET for D0 goes to pin A4 on the Flipper. ## W4: 4-bit Wiegand diff --git a/gpio/wiegand/scenes/wiegand_instructions.c b/gpio/wiegand/scenes/wiegand_instructions.c index 1cc1a57..1338565 100644 --- a/gpio/wiegand/scenes/wiegand_instructions.c +++ b/gpio/wiegand/scenes/wiegand_instructions.c @@ -14,6 +14,7 @@ void wiegand_instructions_scene_on_enter(void* context) { "Connect D0 (Green) to pin A4\n" "Connect D1 (White) to pin A7\n" "Connect GND (Black) to GND\n" + "\nOption 1 (no mosfet):\n" "Add a 10K inline resistor on D0\n" "between keypad and door.\n" "Connect Flipper on door\n" @@ -22,6 +23,21 @@ void wiegand_instructions_scene_on_enter(void* context) { "door. Connect Flipper on\n" "door side. Do not inturrupt\n" "the D0/D1 connections while\n" - "adding the resistors."); + "adding the resistors." + "\n\nOption 2 (mosfet):\n" + "Connect pin A6 to gate of\n" + "mosfet for D1. Connect 5K\n" + "resistor from gate to GND.\n" + "Connect pin A7 to drain of\n" + "mosfet for D1. Connect source\n" + "of mosfet for D1 to GND.\n" + "Connect pin B3 to gate of\n" + "mosfet for D0. Connect 5K\n" + "resistor from gate to GND.\n" + "Connect pin A4 to drain of\n" + "mosfet for D0. Connect\n" + "source of mosfet for D0 to\n" + "GND.\n" + ); view_dispatcher_switch_to_view(app->view_dispatcher, WiegandWidgetView); } diff --git a/gpio/wiegand/scenes/wiegand_play.c b/gpio/wiegand/scenes/wiegand_play.c index 7d62b17..8a62c85 100644 --- a/gpio/wiegand/scenes/wiegand_play.c +++ b/gpio/wiegand/scenes/wiegand_play.c @@ -24,8 +24,12 @@ void wiegand_play() { furi_hal_gpio_write(pinD0, true); furi_hal_gpio_init(pinD0, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh); + furi_hal_gpio_write(pinD0mosfet, false); + furi_hal_gpio_init(pinD0mosfet, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); furi_hal_gpio_write(pinD1, true); furi_hal_gpio_init(pinD1, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh); + furi_hal_gpio_write(pinD1mosfet, false); + furi_hal_gpio_init(pinD1mosfet, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); single_vibro(); furi_delay_ms(500); @@ -33,18 +37,24 @@ void wiegand_play() { int j = 0; for(int i = 0; i < bit_count; i++) { if(data[i]) { - furi_hal_gpio_write(pinD1, false); + furi_hal_gpio_write(pinD1mosfet, true); // Activate the mosfet to ground wire + furi_hal_gpio_write(pinD1, false); // Ground the open-drain wire furi_delay_us(delays[j++]); - furi_hal_gpio_write(pinD1, true); + furi_hal_gpio_write(pinD1, true); // Float the wire + furi_hal_gpio_write(pinD1mosfet, false); // Deactivate the mosfet furi_delay_us(delays[j++]); } else { - furi_hal_gpio_write(pinD0, false); + furi_hal_gpio_write(pinD0mosfet, true); // Activate the mosfet to ground wire + furi_hal_gpio_write(pinD0, false); // Ground the open-drain wire furi_delay_us(delays[j++]); - furi_hal_gpio_write(pinD0, true); + furi_hal_gpio_write(pinD0, true); // Float the wire + furi_hal_gpio_write(pinD0mosfet, false); // Deactivate the mosfet furi_delay_us(delays[j++]); } } furi_hal_gpio_init_simple(pinD0, GpioModeAnalog); furi_hal_gpio_init_simple(pinD1, GpioModeAnalog); + furi_hal_gpio_init_simple(pinD0mosfet, GpioModeAnalog); + furi_hal_gpio_init_simple(pinD1mosfet, GpioModeAnalog); } \ No newline at end of file diff --git a/gpio/wiegand/wiegand.c b/gpio/wiegand/wiegand.c index e4a6207..f9e0a06 100644 --- a/gpio/wiegand/wiegand.c +++ b/gpio/wiegand/wiegand.c @@ -1,7 +1,9 @@ #include "wiegand.h" const GpioPin* const pinD0 = &gpio_ext_pa4; +const GpioPin* const pinD0mosfet = &gpio_ext_pb3; const GpioPin* const pinD1 = &gpio_ext_pa7; +const GpioPin* const pinD1mosfet = &gpio_ext_pa6; volatile int bit_count = 0; volatile bool data[MAX_BITS]; volatile uint32_t data_fall[MAX_BITS]; diff --git a/gpio/wiegand/wiegand.h b/gpio/wiegand/wiegand.h index c9452ec..e55d19f 100644 --- a/gpio/wiegand/wiegand.h +++ b/gpio/wiegand/wiegand.h @@ -14,7 +14,9 @@ #include extern const GpioPin* const pinD0; +extern const GpioPin* const pinD0mosfet; extern const GpioPin* const pinD1; +extern const GpioPin* const pinD1mosfet; extern volatile int bit_count; #define MAX_BITS 42 extern volatile bool data[];