1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Low frequency ZX8211 funtions
17 //-----------------------------------------------------------------------------
22 #include "crc.h" // CRC-8 / Hitag1 / ZX8211
23 #include "fpgaloader.h"
25 #include "lfops.h" // turn_read_lf_on / off
27 #include "lfsampling.h" // getSamplingConfig
28 #include "pm3_cmd.h" // struct
36 ASK w 100% modulation index
38 off, its considered LOW field
39 on, its considered HIGH field
42 Binary Pulse Length Coding (BPLC)
44 ZERO = 8 off, 8 on (14-22)
45 ONE = 8 off, 28 on (26-32)
46 EOF = 8 off, 30 on (38 or more)
62 #define ZX_START_GAP 170
63 #define ZX_WAIT_GAP 90
64 #define ZX_GAP 8 // 4 - 10
72 #define ZX_RESET_GAP 35000 // 35ms
73 #define ZX_RESPONSE_GAP 208
76 #define ZX_PROG_CT 4470
79 #define ZX_SWITCH_RTF 350
82 #define LF_ZX_GET_UID 0b00110
87 static void zx8211_setup_read(void) {
89 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
91 // Make sure the tag is reset
92 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
94 // use lf config settings
95 sample_config
*sc
= getSamplingConfig();
96 LFSetupFPGAForADC(sc
->divisor
, true);
98 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_ADC_READER_FIELD
);
100 // 50ms for the resonant antenna to settle.
103 // Now set up the SSC to get the ADC samples that are now streaming at us.
104 FpgaSetupSsc(FPGA_MAJOR_MODE_LF_READER
);
106 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, LF_DIVISOR_125
);
108 // Connect the A/D to the peak-detected low-frequency path.
109 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
118 static void zx_send(const uint8_t *cmd
, uint8_t clen
) {
123 turn_read_lf_on(ZX_START_GAP
);
125 // now start writing with bitbanging the antenna.
128 turn_read_lf_off(ZX_GAP
* 8);
130 if (((*cmd
++) & 1) == 1) {
131 turn_read_lf_on(ZX_T1
* 8);
133 turn_read_lf_on(ZX_T0
* 8);
138 turn_read_lf_off(ZX_GAP
* 8);
139 turn_read_lf_on(ZX_TEOF
* 8);
142 static void zx_get(bool ledcontrol
) {
144 while (BUTTON_PRESS() == false) {
148 if (ledcontrol
&& (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
)) {
152 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
153 volatile uint8_t sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
156 // (RDV4) Test point 8 (TP8) can be used to trigger oscilloscope
157 if (ledcontrol
) LED_D_OFF();
163 int zx8211_read(zx8211_data_t
*zxd
, bool ledcontrol
) {
166 // clear buffer now so it does not interfere with timing later
167 BigBuf_Clear_ext(false);
169 if (ledcontrol
) LED_A_ON();
174 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_ADC_READER_FIELD
);
178 //uint32_t cs = CRC8Hitag1(uint8_t *buff, size_t size);
180 if (ledcontrol
) LEDsoff();
183 lf_finalize(ledcontrol
);
185 reply_ng(CMD_LF_ZX_READ
, PM3_SUCCESS
, NULL
, 0);
189 int zx8211_write(zx8211_data_t
*zxd
, bool ledcontrol
) {
193 lf_finalize(ledcontrol
);
194 //reply_ng(CMD_LF_ZX_WRITE, status, tag.data, sizeof(tag.data));