├── HOWTO_PART1.md ├── HOWTO_PART2.md ├── LICENSE.md ├── README.md ├── TROUBLESHOOTING.md ├── ZUHF-RFID.md ├── ZUHF-RFID ├── ConsoleMenu.h ├── UHF-RFID.h └── ZUHF-RFID.ino ├── cli └── zuhf-cli.py ├── images ├── ASK-Modulation-RF-envelope.png ├── PIE-Symbols.png ├── cc1101_868MHz_module.jpg ├── connections_tx_image.jpg ├── cthuloid_pcb.jpg ├── gnuradio_setup.png ├── holistical_pcb.png ├── improvisedSetup.jpg ├── lock_cmd.png ├── powerdownup-waveform-parameters.png ├── query-signal.jpg ├── query_queryrep-sequence.jpg ├── rfid-setup.jpg ├── rn16_signal.png ├── signal from chafon reader.png ├── successfull_tag_epc_signal.jpg ├── tag-RN16-response.jpg ├── tagRN16-example.png ├── tag_data_epc.jpg ├── tears_001.png ├── tears_002.png ├── tx-connection.png ├── uhf-rfid_part1_signal01.png ├── uhf-rfid_part1_signal02.png ├── uhf-rfid_part1_signal03.png ├── uhf-rfid_part1_signal04.png ├── uhfread3.png ├── uhfreader1.png ├── uhfreader2.png └── uhfreader3.png ├── libraries ├── SPI_UART_CC1101.zip ├── SPI_UART_CC1101 │ ├── SPI_UART_CC1101.cpp │ ├── SPI_UART_CC1101.h │ └── SPI_UART_CC1101.zip ├── ZUHF_CC1101.zip └── ZUHF_CC1101 │ ├── ZUHF_BUFFER.h │ ├── ZUHF_CC1101.cpp │ ├── ZUHF_CC1101.h │ ├── ZUHF_CC1101.zip │ ├── ZUHF_CC1101_REGS.h │ ├── ZUHF_CRC.h │ ├── ZUHF_MENU.h │ └── ZUHF_VARS.h └── uhf-rfid_part1 ├── UHF-RFID.h └── uhf-rfid_part1.ino /HOWTO_PART1.md: -------------------------------------------------------------------------------- 1 | # Howto build a UHF-RFID-Reader 2 | 3 | Welcome to this tutorial. I want to share with you my experiences I collected the past (ugh it is already almost half a year) several months trying to build an UHF-RFID-Reader. If you found this HOWTO guide you will probably already have seen the related README with some additional information on the project. (https://github.com/zaphoxx/zuhf-rfid/blob/main/README.md). The guide will be structured in several steps. As of today not all steps are available yet. 4 | The guide describes a UHF-RFID-Reader the way I build it. I do NOT say this is the only way to do it. There are other options and if you feel like deviating from the path (e.g. using someting else then Arduino Due as MCU or design your own RF modules.) If you do so feel free to let me know I would be most curious how you did and if it shows any improvements. Now I guess lets get started! 5 | 6 | ## Things you need 7 | 8 | There are a few things that you will need to purchase before being able to put an UHF-RFID Reader together 9 | 10 | * Arduino Due - you can use different boards here (e.g. teensy should work to) but you will then have to rewrite most of the code. So unless you are up for an extra challenge and you know exactly what you are doing I would recommend to stick to the Arduino Due for now. The price for this Arduino Board is ~15-20 Euros depending where you purchase it. 11 | * RF-Modules - you will need at least two RF-modules (one as receiver and one as transceiver). The project is based on the TI CC1101 Chip and the RF modules I used have that chip onboard. !!! ATTENTION !!! The important piece here is that you need to make sure that the RF-module is designed for the frequency you want to use which should be either 868MHz or 915MHz. I used 868MHz but 915MHz should (with no guarantees) work as well. I made that mistake to and purchased rf-modules with the CC1101 chip which were not designed for 868MHz (but 433MHz) and went through a lot of pain. Be careful some vendors put 868MHz in the title but in the fineprint you will often find the other frequency. An example for modules that do work at the correct frequency can be found here: (https://www.amazon.de/gp/product/B084BSMCQG/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1); There are lots of other options around. You could even design and build your own RF-modules using e.g. (https://easyeda.com/); a set of 5 modules costs around 30 Euros on Amazon. 12 | * Antenna for your rf-module - depending on where you purchase your rf-module an antenna might be or might be not part of it. Also some modules are designed with an SMA connector and some are not. For the modules as mentioned in the link above the antenna is part of it and you just need to solder it to the board. These antennas work fine and do the job. Once you have everything up and running you might want to modify antenna design to improve signals and range a bit. 13 | * one or two UHF-RFID tags/cards to play with; make sure you pick the correct ones which operate at 868MHz, typically the cards operate in a wide range from ~860MHz-960MHz. !(https://www.amazon.de/-/en/Yaron-Iso18000-960MHz-H3-Alien-Plastic/dp/B072562JLS/ref=sr_1_3?dchild=1&keywords=uhf+rfid+karte&qid=1617356757&sr=8-3) 14 | * libraries and example Arduino sketches from here (https://github.com/zaphoxx/zuhf-rfid) 15 | 16 | 17 | ## Things you might want 18 | These are the things that are not necessary for the build but you might consider them as things will be much more difficult especially when trying to find the root cause of an issue. 19 | 20 | * HackRF One - Thats the one I used because I had it at hand. You dont need that specifically. Any SDR (Software Defined Radio) that can be used with gnuradio and which can receive at the working frequency of 868MHz should do. Check out the following website if you dont have one (https://blog.bliley.com/10-popular-software-defined-radios-sdr). It will give you a good overview of what is available to what price. The SDR is basically just for monitoring the signals send by the reader and by the tag/card. It will definitely make things much easier when you can monitor the signals the reader and tags send out. I wouldnt recommend going through this project without that. It also helps to understand what is happening under the hood. 21 | * GnuRadio-Companion - Software to use your HackRF One or other SDR with. You can download the software here (https://wiki.gnuradio.org/index.php/InstallingGR) 22 | * PCB Board with the necessary wiring. This is basically just a 'UHF-RFID-Shield' for your Arduino Due. You might get away using cables and a breadboard (it does work). But it is much better if you do have a pcb board with the necessary wiring where things are fix and there is no danger of cables getting loose and accidently frying the Arduino Board (this did happen...). The downside is of course that you wont be able to quickly change input/output pins and similar. Maybe want to do the first steps with a breadboard and once you are confident things work as expected you can switch to a custom shield. 23 | * some basic familiarity with gnuradio, arduino, soldering and programming in general. 24 | * e.g. wires, solderiron, etc. things you need to get your module connected to the arduino. 25 | * patience and persistance - thats a big one especially when things dont work as expected :) 26 | 27 | ## Things to keep in mind 28 | 29 | * Reader Range - you might have read or heard that UHF-RFID plays big with large scan ranges (few meters). Well, you wont get that here. If you are lucky you will get something in the order of about 1-2cm. So dont put your expectations to high regarding the reader range. Once you have the reader working you can experiment with different antennas, positioning etc. to see if you can improve the scan range. Also the area in which the reader can read/write the tag might be in a very small window in space. So if it doesnt work right away dont fret you might need to find the sweet spot where the reader can read/write the card. 30 | * Reader Commands - the code as it is at the moment does not support all the possible commands a commercial reader can send to the tag/card. Check out the README.md for details on that. Feel free to add new commands to your code and improve your setup. 31 | 32 | ## Things connected - a first check 33 | 34 | In this section I will go through how the RF-Modules should be connected to the Arduino Due starting with RF-Module that will be the tranceiver part. At the end you will be able to see the request the module sends to the card and hopefully the tag response with the 'HackRf One' (or anotherone). (this step you can try on a breadboard). So you need 35 | * Arduino Due (for this first part you can also use an Arduino Nano, but the wiring and code is slightly different. I will add some schematics and code for the Nano further below.) 36 | * RF-Module 37 | * Shield or Breadboard and cables 38 | 39 | The CC1101 Chip on the RF-Module can be programmed using SPI communication (https://www.arduino.cc/en/reference/SPI)(https://en.wikipedia.org/wiki/Serial_Peripheral_Interface). But we are NOT going to connect the first RF-Module (TX) to the SPI pins on the Arduino Board. Instead we will connect it to the USART1 (RX2/TX2 on the Board) and reprogram the USART1 so it can be used as SPI. We will save the SPI pins for later for the RX RF-Module. 40 | Now for the TX you need to connect the MISO to RX2 and MOSI to TX2 pin respectively of the Arduino Due. The CLK needs to be connected to PIN A0 (PIN 78) on the Arduino Board. The select PIN CSN needs to be connected to RTS1 which is PIN 23 (A.14) on the Board. The CC1101 operates at a voltage of 3.0 - 3.7 V so the RF-Modules VDD should be connected to the 3.3V connector on the Arduino (not the 5V connector). 41 | The CC1101 Chip has two output PINS called GDO0 and GDO2 (there is also a GDO1 if you are wondering but it is not broken out on the RF-module board and we wont need it) which also need to be connected to the Arduino. These are free to choose but see below for the settings I use. For reference you can also check check out the Arduino Due Pin Layout her ![PinLayout](https://www.14core.com/wp-content/uploads/2015/06/Arduino-Due-Pinout-Diagram-Illustration-Complete-Pin-Diagram-1.jpg). 42 | For the setup I use with the Arduino I have the following connections: 43 | 44 | * RF-MODULE CSN --> Arduino Pin 23 45 | * RF-MODULE MISO --> Arduino RX2 46 | * RF-MODULE MOSI --> Arduino TX2 47 | * RF-MODULE CLK --> Arduino A0 48 | * RF-MODULE GDO0 --> Arduino Pin 34 49 | * RF-MODULE GDO2 --> Arduino Pin 36 50 | * RF-MODULE VDD --> Arduino 3.3V connector 51 | * RF-MODULE GND --> Arduino GND 52 | Reference numbers are as printed on the board. 53 | ![Wiring](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/connections_tx_image.jpg) 54 | With the wiring complete we will next take a look on how to get the Arduino IDE started and a load a first test program. Keep fingers pressed so we will see a tag response in our SDR monitor. 55 | 56 | The CC1101 Chip will be configured by the Arduino library with the following main parameters: 57 | 58 | * Working Freq. 868MHz 59 | * Bandwidth ~200kHz 60 | * Datarate 80kBaud (corresponding to a 12,5µs pulse width) 61 | * Modulation OOK/ASK) 62 | 63 | ## Things get started - observe your first tag response (hopefully!) 64 | 65 | Ok, so now you have the wiring complete and are eager to see the rf module in action. Ok! 66 | Fire up your Arduino IDE and make sure you have the libraries (SPI_UART_CC1101 and ZUHF_CC1101) imported. You find these in the github repository under '/libraries/'. Check out (https://www.arduino.cc/en/guide/libraries) on how to manually import libraries. 67 | Once you have the libraries setup you need to load the Arduino sketch 'uhf-rfid_PART1' into the IDE. Make sure the additional file 'UHF-RFID.h' is present in the same folder. 68 | I am assuming you are using the same pins as described above if not this will not work right away and you will have to make adjustments in the appropriate files (e.g. SPI_UART_CC1101.h). 69 | 70 | Now start your SDR device with gnuradio-companion. For an example setup with HackRF One see image below (![gnuradio_setup](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/gnuradio_setup.png)). Make sure you are using either 'Complex to Mag' or 'Complex to Mag_square'. The source to use will depend on your SDR but with HackRF the osmocom source is good to go. Set the frequency to 868MHz and the sample rate to 1e6 (you can go lower if you are having issues here, but 1e6 should be good in most cases). For visualization I chose 'QT Gui Time Sink' with Number of Points set to 1e6 (same here lower it down if you are experiencing problems). Start your gnuradio setup to start listening. 71 | 72 | Once you uploaded the sketch to the arduino due and all is connected properly you should start seeing a signal on gnuradios monitor window (similar the images). On both images you see the request signal from the rf-module but not response from a tag yet. ![signal1](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/uhf-rfid_part1_signal01.png)![signal1](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/uhf-rfid_part1_signal02.png) The arduino sketch is setup in a way that it keeps repeatedly sending a simple query request. Once you hold a uhf rfid card / tag close to your antenna you should start seeing a tag response on the monitor window similar to the images below. Both images show a clear tag response from the card right after the request from the rf-module. If you zoom further into the signal you can manually decode the tags response which is apart from the extended preamble just an RN16 (see also details in README.md). ![signal3](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/uhf-rfid_part1_signal03.png)![signal4](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/uhf-rfid_part1_signal04.png). 73 | 74 | Don't worry if you do not see a tag response right away. As long as you can observe a proper query signal it might just take some patience to find the right distance and orientation to the antenna (tag <-> antenna <-> hackrf antenna). Once you get a signal play around with the antennas orientation (SDR antenna vs. rf-module antenna vs. tag orientation) to improve your observed signal. 75 | 76 | Congratulations! Your rf-module can "communicate" with UHF-RFID card/tag. 77 | -------------------------------------------------------------------------------- /HOWTO_PART2.md: -------------------------------------------------------------------------------- 1 | ## Recap 2 | 3 | In the previous HOWTO_PART2 I described what you need and how to get your first interaction with a uhf-rfid tag going. Hopefully you could observere a tag response with your SDR hardware. 4 | I did not go into detail about what the arduino sketch is actually doing and was is happening when the reader talks to the tag. In the following I will try to shed some light on this. 5 | 6 | ## Things behind the curtain - what is happening ? 7 | 8 | In this section I will briefly go thru the main code of the arduino sketch, what it does and what you see in the signals you observed. 9 | 10 | Usually the first important component of an arduino sketch is the ```setup()``` function. However in our case there is not happening much in there. 11 | ```c 12 | void setup() 13 | { 14 | // delay to avoid known reset issue - see also https://forum.arduino.cc/index.php?topic=256771.75 15 | delay(1000); 16 | /* SERIAL CONNECT */ 17 | Serial.begin(250000); 18 | for (int i = 0; i < sizeof(CWA); i++) CWA[i]=0xff; 19 | reader_state = R_INIT; 20 | } 21 | ``` 22 | We establish a serial communication with a 250000 baudrate, preset the array CWA and set the reader_state to R_INIT. The more interesting action happens then in the function ```loop()```. 23 | 24 | This is the full loop() function: 25 | 26 | ```c 27 | void loop() 28 | { 29 | switch(reader_state) 30 | { 31 | case R_INIT: 32 | counter = 0; 33 | tx_version = TX_UNIT.Init(); 34 | delay(10); 35 | if ((tx_version == 0x14 or tx_version == 0x04)) 36 | { 37 | Serial.println("[CC1101] Modules Check - OK"); 38 | TX_UNIT.SpiStrobe(CC1101_SFSTXON); 39 | delay(500); 40 | Serial.println("**** START RUN ****"); 41 | reader_state = R_START; 42 | } 43 | else 44 | { 45 | Serial.println("[CC1101] Error on CC1101 module initialization"); 46 | reader_state = R_WAIT; 47 | delay(100); 48 | } 49 | break; 50 | 51 | case R_START: 52 | /* LET FIFOBUFFER EMPTY FROM A PREVIOUS RUN AND SET TX INTO IDLE MODE */ 53 | while ((TX_UNIT.SpiReadStatus(CC1101_TXBYTES) & 0x7f) > 0); 54 | TX_UNIT.SpiStrobe(CC1101_SIDLE); 55 | delay(5); 56 | /* ********** START TX AND SEND CW FOR TAG SETTLE ********** */ 57 | TX_UNIT.SpiStrobe(CC1101_SFTX); 58 | delay(5); 59 | TX_UNIT.SpiStrobe(CC1101_STX); 60 | TX_UNIT.SpiWriteBurstReg(CC1101_TXFIFO, CWA, 20); 61 | /* ********** *********************************** ********** */ 62 | reader_state = R_QUERY; 63 | break; 64 | 65 | case R_QUERY: 66 | send_default_query(); 67 | TX_UNIT.SpiWriteBurstReg(CC1101_TXFIFO, CWA, 40); 68 | reader_state = R_START; 69 | delay(20); 70 | break; 71 | 72 | default: 73 | reader_state = R_INIT; 74 | break; 75 | } 76 | } 77 | ``` 78 | But lets take a look step by step. First thing you will notice is the switch statement. This basically controls in which state the reader is in for the current loop. For this example the reader can be in three states: R_INIT, R_START and R_QUERY. 79 | 80 | Let's check the R_INIT state first. In this state the RF-Module is first initialized using 81 | ```TX_UNIT.init()```. Which means the registers of the CC1101 chip are configured as needed (e.g. set working frequency to 868MHz) and an initial reset of the chip. In addition the init() function returns a value which corresponds to the chips versionnumber. This value can be used to check if chip is still working fine. This check is done in the following if statement. If everything is ok the reader will flush the CC1101 fifobuffer ```TX_UNIT.SpiStrobe(CC1101_SFTX);``` and switch the chip into TX mode ```TX_UNIT.SpiStrobe(CC1101_STX);```. Now the chip is ready to send out signals. 82 | The first signal we send out is a continous wave signal for a length of 20bytes which corresponds in our case 20 x 4 x 12.5 [µs] = 1000 [µs]. ```TX_UNIT.SpiWriteBurstReg(CC1101_TXFIFO, CWA, 20);``` (we write the data into the fifo buffer and the chip takes care of actually sending the signal out). This cw signal serves as activation signal to powerup and activate the tag. Right after sending the cw (continues wave) the reader switches into R_QUERY state and we send the actual query to the chip ```send_default_query();```. This function builds a default query signal so we dont have to worry about any specific parameters for now. Once this is out of the way we send out another cw signal ```TX_UNIT.SpiWriteBurstReg(CC1101_TXFIFO, CWA, 40);``` and put the reader back into R_START status. After that the cycle repeats until we poweroff the arduino. 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | zuhf-rfid - sketch and library files to control a selfbuild UHF-RFID Reader 2 | 3 | Copyright (C) 2021 Manfred Heinz 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | 18 | 19 | GNU GENERAL PUBLIC LICENSE 20 | Version 3, 29 June 2007 21 | 22 | Copyright © 2007 Free Software Foundation, Inc. 23 | 24 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 25 | 26 | Preamble 27 | The GNU General Public License is a free, copyleft license for software and other kinds of works. 28 | 29 | The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. 30 | 31 | When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. 32 | 33 | To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. 34 | 35 | For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. 36 | 37 | Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. 38 | 39 | For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. 40 | 41 | Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. 42 | 43 | Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. 44 | 45 | The precise terms and conditions for copying, distribution and modification follow. 46 | 47 | TERMS AND CONDITIONS 48 | 0. Definitions. 49 | “This License” refers to version 3 of the GNU General Public License. 50 | 51 | “Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. 52 | 53 | “The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations. 54 | 55 | To “modify” a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a “modified version” of the earlier work or a work “based on” the earlier work. 56 | 57 | A “covered work” means either the unmodified Program or a work based on the Program. 58 | 59 | To “propagate” a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. 60 | 61 | To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. 62 | 63 | An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 64 | 65 | 1. Source Code. 66 | The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work. 67 | 68 | A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. 69 | 70 | The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. 71 | 72 | The “Corresponding Source” for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. 73 | 74 | The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. 75 | 76 | The Corresponding Source for a work in source code form is that same work. 77 | 78 | 2. Basic Permissions. 79 | All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. 80 | 81 | You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. 82 | 83 | Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 84 | 85 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 86 | No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. 87 | 88 | When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 89 | 90 | 4. Conveying Verbatim Copies. 91 | You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. 92 | 93 | You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 94 | 95 | 5. Conveying Modified Source Versions. 96 | You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: 97 | 98 | a) The work must carry prominent notices stating that you modified it, and giving a relevant date. 99 | b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to “keep intact all notices”. 100 | c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. 101 | d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. 102 | A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 103 | 104 | 6. Conveying Non-Source Forms. 105 | You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: 106 | 107 | a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. 108 | b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. 109 | c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. 110 | d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. 111 | e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. 112 | A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. 113 | 114 | A “User Product” is either (1) a “consumer product”, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, “normally used” refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. 115 | 116 | “Installation Information” for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. 117 | 118 | If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). 119 | 120 | The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. 121 | 122 | Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 123 | 124 | 7. Additional Terms. 125 | “Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. 126 | 127 | When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. 128 | 129 | Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: 130 | 131 | a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or 132 | b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or 133 | c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or 134 | d) Limiting the use for publicity purposes of names of licensors or authors of the material; or 135 | e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or 136 | f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. 137 | All other non-permissive additional terms are considered “further restrictions” within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. 138 | 139 | If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. 140 | 141 | Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 142 | 143 | 8. Termination. 144 | You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). 145 | 146 | However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. 147 | 148 | Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. 149 | 150 | Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 151 | 152 | 9. Acceptance Not Required for Having Copies. 153 | You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 154 | 155 | 10. Automatic Licensing of Downstream Recipients. 156 | Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. 157 | 158 | An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. 159 | 160 | You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 161 | 162 | 11. Patents. 163 | A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”. 164 | 165 | A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. 166 | 167 | Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. 168 | 169 | In the following three paragraphs, a “patent license” is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To “grant” such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. 170 | 171 | If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. “Knowingly relying” means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. 172 | 173 | If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. 174 | 175 | A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. 176 | 177 | Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 178 | 179 | 12. No Surrender of Others' Freedom. 180 | If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 181 | 182 | 13. Use with the GNU Affero General Public License. 183 | Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 184 | 185 | 14. Revised Versions of this License. 186 | The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. 187 | 188 | Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. 189 | 190 | If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. 191 | 192 | Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 193 | 194 | 15. Disclaimer of Warranty. 195 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 196 | 197 | 16. Limitation of Liability. 198 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 199 | 200 | 17. Interpretation of Sections 15 and 16. 201 | If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. 202 | 203 | END OF TERMS AND CONDITIONS 204 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Creating Your Own UHF RFID Reader From Scratch 2 | The code in this repo is actually outdated and should be replaced by the repo https://github.com/zaphoxx/zaphoxx-uhf-rfid which has some minor advantages and enhancements. 3 | ## TL;DR 4 | This project has already been going for a while with longer breaks inbetween and I decided to put the information I learned along the way down as a resource mainly for myself but also to make it available for others who might be interested in building a UHF RFID reader/writer from scratch. The image below shows the UHF-Reader. It works fine on very small distances (1-2cm) for the following commands. 5 | * READ - read from any unlocked memory area 6 | * WRITE - write to any unlocked memory area 7 | * LOCK - specify memory area to password protect or permalock (the exact functionality depends on the memory area itself) 8 | * ACCESS - read/write/lock password protected memory areas 9 | * TEARS - this is a special functionality to test if tearing during e.g. the write operation shows some interesting behaviour (DANGEROUS! This can make your card useless!) 10 | * TEARLOCK - similar as TEARS but in conjunction with the LOCK functionality (DANGEROUS! This can make your card useless!) 11 | 12 | The commands have been tested on the following types of tags: Impinj Monza 4E, Alien Higgs 3, Impinj Monza R6 (limited functionality as there is no user memory and effectively no reserved memory, lock mechanism works differently for this chip; see also chips datasheet). I have another couple of tags where the commands work all fine but I do not have any information on the chip type for these. 13 | 14 | I started putting a HOWTO guide (https://github.com/zaphoxx/zuhf-rfid/blob/main/HOWTO.md) together in case you are interested in going into that direction. As of today Part1 and Part2 of the HOWTO is ready, any feedback for improvements is highly appreciated. Other than that enjoy. Further sections will be added in the future as soon as i have the time for it. 15 | 16 | If you already put the hardware together and you just want to get started reading/writing to a tag you can refer to (https://github.com/zaphoxx/zuhf-rfid/blob/main/ZUHF-RFID.md) which briefly describes how to use the cli zuhf-cli.py. 17 | 18 | ![UHF-READER-SHIELD](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/uhfreader2.png) 19 | 20 | ## Background 21 | The whole project came out of another project related to UHF-RFID but using SDR. The goal of the other project was to get an RFID reader up and running using SDR. Work has already been done on this field by nkargas and the project can be found here https://github.com/nkargas/Gen2-UHF-RFID-Reader. Based on nkargas project I could get a very basic RFID reader up and running. However the reader as developed by nkargas only reads out the tags EPC and basically stops there. Adam Laurie has started to take this a step further using a bladeRF and has published his fork of the original reader here https://github.com/AdamLaurie/Gen2-UHF-RFID-Reader. See also Adam Laurie great talk about the topic here: https://www.youtube.com/watch?v=QKi1OH8Zstk . 22 | With SDR the critical part is usually the time between receiving the RN16 value from the tag and responding with an ACK (with the received RN16) to receive the actual EPC. Due to latencys this can be difficult to satisfy and makes the SDR solution sometimes unreliable.This problem has been outlined by the original authors and by others trying to implement the uhf rfid reader with SDR. 23 | 24 | ## Another Approach 25 | A friend noted that there is a TI Chip CC1101 which can be used (as stated in the datasheet) for 26 | * Ultra low-power wireless applications operating in the 315/433/**868**/**915** MHz ISM/SRD bands 27 | * Wireless alarm and security systems 28 | * Industrial monitoring and control 29 | * ... 30 | In addition it can handle ASK modulation which is the used modulation technique for UHF RFID and has a maximum baudrate of 500 kBauds. So this sounds promising and theoretically one should be able to do at least the same as with the above mentioned SDR solution avoiding the reliability issues. 31 | Also there exists an Arduino library for the CC1101. Therefore using an Arduino (e.g. the Nano) for controlling and programming the CC1101 seems like a straightforward choice. 32 | 33 | ## Roadblock(s) 34 | There are a few roadblocks on getting to a working solution. The first and most important one would be that even though a could reproduce the above mentioned SDR solution I actually have only negligible experience with RF not even talking about how the CC1101 works and how it can "programmed" to do my bidding. Which means there will be (and already was as I am already in the middle of it) tons of research and learning necessary on the way. That is the main reason I started this block hoping someone will stumple accross it and perhaps see the mistakes I made (and believe me I did a lot already) and perhaps she has some additional insight and can give me some helpful advice. 35 | Another issue I encountered was that even though it seems like some people have already tried the same before there is very little information around about how specifically they did realize it. There are some nice publications available but these dont go into to much detail regarding the technical realization. TI has a forum where you can find some information in particular regarding operation of the cc1101 but often the advice found will be to use the SmartRF software (it is free to download and use but you have to login and sign with blood that you wont use it for any mischief) to e.g. find valid register settings. 36 | 37 | With using the Arduino there is the caveat that the memory available is quite limited and you have to be careful on how you are using your resources when programming the Firmware. At the moment it looks like I should get along with the memory space available. But in case I run into memory issues there is the option to use e.g ~~a Raspberry Pi~~ Arduino Due or similar as alternative. ~~For the Raspberry Pi there are also cc1101 libraries available and transferring the code from Arduino to the Pi shouldnt be an issue.~~ 38 | 39 | ## Resources 40 | This a list of resources I have been using for this project. It is not a final list and new links or documents might be added in time. The list is not strictly in order of importance but I divided it into two sections where the top list are necessary resources like datasheets and specifications, whereas the second list is a list of helpful resources I found during my research. 41 | 42 | SDR projects on UHF-RFID on github 43 | * https://github.com/nkargas/Gen2-UHF-RFID-Reader - I have been using this as main source for general uhr-rfid related settings, code snippets etc. (in combination with the specification documents on gen2v2 uhf listed below). 44 | * https://github.com/AdamLaurie/Gen2-UHF-RFID-Reader - this is a fork of the previous link but focusing more on the implementation on a bladerf rather then the usrp. 45 | 46 | Datasheets/Specifications/libs 47 | * CC1101 Datasheet https://www.ti.com/lit/ds/symlink/cc1101.pdf 48 | * https://www.gs1.org/sites/default/files/docs/epc/gs1-epc-gen2v2-uhf-airinterface_i21_r_2018-09-04.pdf 49 | * Arduino Lib for CC1101: I started by using https://github.com/simonmonk/CC1101_arduino and modifying it as needed in the further process. There are other CC1101 libraries for Arduino available but this seemed to me the simplest to get started with and to explore the capabilities of the cc1101 chip. 50 | * cc1101 - Programming Output Powers https://www.ti.com/lit/an/swra151a/swra151a.pdf 51 | * cc1101 - setting registers for ASK/OOK modulation https://www.ti.com/lit/pdf/swra215 52 | * cc1101 - usage of gdox pins - https://www.ti.com/lit/an/swra121a/swra121a.pdf?ts=1609398291195&ref_url=https%253A%252F%252Fwww.ti.com%252Fproduct%252FCC1101%253FkeyMatch%253DASK%2BMODULATION%2BCC1101%2526tisearch%253DSearch-EN-everything%2526usecase%253DGPN 53 | * cc1101 - packet transmission basics - https://www.ti.com/lit/an/swra109c/swra109c.pdf?ts=1609543498401&ref_url=https%253A%252F%252Fwww.ti.com%252Fproduct%252FCC1101%253Futm_source%253Dgoogle%2526utm_medium%253Dcpc%2526utm_campaign%253Depd-null-null-GPN_EN-cpc-pf-google-wwe%2526utm_content%253DCC1101%2526ds_k%253D%257B_dssearchterm%257D%2526DCM%253Dyes%2526gclsrc%253Daw.ds%2526%2526gclid%253DCjwKCAiArbv_BRA8EiwAYGs23Cd4vk5J0NyYSean69jrkUv2pj_sYKPD1O72e1YVFU3YXXbui-JnrhoCMYsQAvD_BwE 54 | * Arduino library for CC1101 55 | 56 | 57 | Helpful resources 58 | * https://www.eetimes.com/tutorial-radio-basics-for-uhf-rfid-part-i/ 59 | * https://www.grspy.com/cloning-the-remote-control-of-an-rc-switch-using-ti-cc1101/ 60 | * https://e2e.ti.com/support/wireless-connectivity/other-wireless/f/667/t/157309 61 | * https://home.zhaw.ch/~kunr/NTM1/praktikum/standards_and_literature/E2E_chapter03-rfid-handbook.pdf 62 | * https://greatscottgadgets.com/sdr/ 63 | * https://www.ti.com/product/CC1101?keyMatch=ASK%20MODULATION%20CC1101&tisearch=Search-EN-everything&usecase=GPN 64 | 65 | ## Hardware 66 | I am currently using the following hardware: 67 | * Instead of purchasing just the chip and having to solder things myself I decided to opt out for a already available module ~~https://www.alibaba.com/product-detail/Taidacent-RF-Wireless-Transceiver-Module-Radio_1859941763.html~~ for example from here (https://www.amazon.de/gp/product/B084BSMCQG/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1) for a few $ per piece. 68 | * ~~Arduino Nano as MCU~~ Arduino Due 69 | * HackRF One (mainly for monitoring and basic analysis of the signals produced by the CC1101 module) 70 | * (optional) a custom pcb board to avoid pins getting loose and to enhance connection of the spi wirings. (but all can also be done by using wires and a breadboard.) 71 | 72 | ![Cthuloid UHF-RFID](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/cthuloid_pcb.jpg) 73 | ![Holistical UHF-RFID](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/holistical_pcb.png) 74 | 75 | ## Basic Settings 76 | 77 | To operate the CC1101 you have to set registers to the appropriate values. It can be very confusing on how the registers need to be setup and the cc1101 datasheet is quite impressive but the explanations in the documentation are mainly targeted towards a reader that already understands how these things work in general. However an approach that was quite helpful was to use the smartRF software to find some basic settings and then manually adjust some additional registers as needed. 78 | 79 | To get an idea of the settings needed for UHF RFID in general the specification for gs1-epc-gen2v2-uhf can be used as guideline (https://www.gs1.org/sites/default/files/docs/epc/gs1-epc-gen2v2-uhf-airinterface_i21_r_2018-09-04.pdf). 80 | 81 | ![ASK Envelope](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/ASK-Modulation-RF-envelope.png) 82 | 83 | ![POWERDOWN/UP](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/powerdownup-waveform-parameters.png) 84 | 85 | ![PIE Symbols](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/PIE-Symbols.png) 86 | 87 | ``` 88 | CC1101 Freq = 868MHz 89 | CC1101 DataRate = 80kBaud 90 | CC1101 Modulation = ASK/OOK 91 | byte paTable[8] = {0x27,0xc0,0x00,0x00,0x00,0x00,0x00,0x00}; // putting the low end to 0x27 is not necessary and you can also use 0x00 instead 92 | 93 | 1 Pulsewidth = 12.5µs 94 | 1 TARI = 25µs (DATA0) 95 | const byte DATA0[] = {1,0}; 96 | const byte DATA1[] = {1,1,1,0}; 97 | const byte DELIM[] = {0}; 98 | const byte RTCAL[] = {1,1,1,1,1,0}; // length of (data0 + data1) 99 | const byte TRCAL[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}; // max 3 * length(RTCAL) 16*12.5µs = 200µs --> BLF = 8/200µs = 40kHz 100 | ``` 101 | To make live easier I ordered a very simply pcb to have the arduino and the cc1101 module properly connected and soldered on the same board. 102 | ![pcb board setup](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/rfid-setup.jpg) 103 | 104 | After some trial and error and getting the coding to work properly I finally got a query signal going out. Unfortunately I do not get a RN16 response from tag. This is the point where I seem to hit a brick wall. From what I can say the settings look good. I also tried to send out a query followed by several queryrep commands but nothing seems to get the tag to give me any response. (see query signal below). 105 | The current full signal is (1500µs settle-time for the tag + query command + 250µs cw + x times queryrep with cw + powerdown) and this sending out repeatedly. 106 | I do monitor the signals with the HackRF. I checked the SDR solution and there I can clearly see the tag response to the query/queryrep. So I will need to do some more analyzing of the SDR solution to see if I am still missing something. (if you have any idea on why I do not getting a signal, feel free to drop me a message). 107 | ![query signal](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/query-signal.jpg) 108 | ![full sequence](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/query_queryrep-sequence.jpg) 109 | 110 | From what i see comparing the sdr approach with the cc1101 approach the main difference are the powerlevels reaching the tag. The usrp provides a much higher powerlevel then the cc1101 does. So i might need to amplify cc1101 output power to get a tag response. 111 | Not sure if this is it but its the one difference that sticks out. 112 | 113 | ## Dont trust the products headline ## 114 | Painful lesson learned. The modules I ordered are not designed for 868 MHz but for 430 MHz instead. So if you are also going into that direction make sure the modules you order are specifically for 868 MHz. Don't trust the main description, checkout the detailed description. In my case the description stated 868 MHz but in the detailed description it did say 430MHz instead. It can be really frustrating (and time and coffee consuming) when the module you have does not work the way you think it should. Until I get the new actual modules I will use the ones I have as workaround. As you can see in the next section, you can get a signal but you have to basically glue the modules right to the tag at the right location on the tag and even then you might still not get a signal. (a little tinfoil at the right location might help to get more of the signal reflected on the the tag). 115 | 116 | ## First Success - Seeing a Tag Response ## 117 | Woohoo! I finally got a tag response. The setup is quite improvised and the tag and antennas are basically in direct contact but now that I do have a signal I can work with that and see how to improve distance, power etc. 118 | 119 | ![TagResponse](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/tag-RN16-response.jpg) 120 | 121 | ![Improvised Setup](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/improvisedSetup.jpg) 122 | 123 | So lets check a response and see if the structure (FM0 encoded reply) makes sense with what we would expect. From the image below you can see that the signal we identified as tag response aligns with the expectations. The tag response consists of a FM0 preamble, a RN16 and a dummy data1 bit. 124 | 125 | ![TagResponse Example](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/tagRN16-example.png) 126 | 127 | ## New Considerations ## 128 | So as I already mentioned above the space availabe especially for dynamic memory is quite tight on the Arduino. Therefore I am currently considering if I should move over to an old RaspberryPi now before moving on with the actual UHF-RFID protocol. There is a raspberry library available by spaceteddy (https://github.com/SpaceTeddy/CC1101) which I will take a look into to see how much additional work this might take. The thing is that I will have to port to raspberry later anyway so it would make sense to do it right away as I am not an experienced programmer and trying to squeeze everything into the Arduino might cause me more headaches then necessary and it would distract me from the actual fun part of this project anyways. 129 | 130 | ## The RaspberryPi debacle ## 131 | Back to square one. After some reading and experimantation with an old raspberry pi I had at hand I realized that this is not a good option at all. The raspberry is not suited for this kind of realtime application. What happens is that due to the interrupts on the raspi the module drops the TX and suddenly stops sending anymore which is really bad when you try to interact with an passive tag that keeps itself alive relying on the same. After 3 days of trying to still convince the raspiberry NOT to drop the signal I decided to go back to the arduino which did work fine so far. 132 | 133 | ## Getting the RN16 ## 134 | Still working with the current modules (the ones for the 430MHz) and messy setup (everything taped together in close proximity). What I do have now is the following: 135 | 136 | * TX unit: Arduino + cc1101 module 137 | * RX unit: Arduino + cc1101 module 138 | 139 | (The other option would be to connect the RX module as second slave to the arduino which does work fine but the wiring is getting very messy then.) 140 | Both units can communicate via the serial connection and are additionally connected to use a digital trigger (to trigger the RX on/off); 141 | The serial connection will be necessary to communicate the captured RN16 to the TX module so the reader can send a valid ACK signal to the tag. 142 | 143 | The current status is that I can read out (not very reliable yet due to the used modules but it works) the RN16. From the screenshots below you can see that the receiver can read the full tag response as expected. You can see the FM0 preamble and the actual RN16 differently colored in the image. The FM0 preamble is expected be (001011011100) followed by the RN16 + 1 dummy bit (which in this case is just 11 at the end; the arduino serial.print() omits leading zeros, so these need to be added in case the byte shows less then 8 bits). 144 | 145 | ![RN16 Signal from RX module](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/rn16_signal.png) 146 | 147 | ## Time for upgrades ## 148 | The modules I used before were specifically designed for around 430MHz. I ordered new ones that are clearly designed for 868MHz instead. Also I decided to upgraded the Arduino to an Arduino Due, to be on the safe side and to have the option to add some controls, display etc. in the long run. ~~The receiver part can still use the Arduino Nano and communicate with the TX unit via serial communication to exchange e.g. the captured RN16 and other information.~~ Unfortunately the Serial Communication does not necessarily within the same loop. So instead of using a second arduino unit I connect both cc1101 modules to the arduino due. This requires reprogramming of the USART0 (or USART1) to function as SPI interface. (More information on how to do that can be found here https://forum.arduino.cc/index.php?topic=283766.0 and in the Atmel SAM3X8x datasheet https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjqw-nd8N3uAhUUnBQKHSFfDK0QFjAAegQIAhAC&url=https%3A%2F%2Fww1.microchip.com%2Fdownloads%2Fen%2FDeviceDoc%2FAtmel-11057-32-bit-Cortex-M3-Microcontroller-SAM3X-SAM3A_Datasheet.pdf&usg=AOvVaw1rt7QIPIgPCcuY6Eg1SAzz) 149 | 150 | The new modules arrived and if they work (some don't which is again very frustrating as lot of time goes into finding the root cause) but if they work the signal is as expected much better. The tag/card still needs to be VERY close (1-2 mm) to the TX antenna to get the tags RN16 response. I guess I will have to live with that for now until I to my own design and allow for different antennas to be attached. 151 | 152 | ## One step foward two steps back ## 153 | Unfortunately I accidently killed my Arduino Board. A wire got loose, touched the board somewhere and ** poof ** lights out. Will need to order a new one which will take a couple of days before I will be able to continue working on the project. Seems like I am stuck at this stage for a while now. Hopefully by the weekend I will have all I need to get things foward next week. 154 | 155 | ## Getting tags EPC ## 156 | ![Reader-Tag-Communication](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/successfull_tag_epc_signal.jpg) 157 | ![Tag Data From Response](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/tag_data_epc.jpg) 158 | 159 | # Putting in all together 160 | It has been around 3 months now since I started this project and I have come a long way. It is finally working. I do have a simple but functional UHF RFID reader for 868MHz. Tags that are not password protected can be modified with these commands. For password protected tag I still need to implement the ACCESS command which will be next on the list. Currently the following commands are fully functional and implemented: 161 | 162 | * QUERY 163 | * ACK 164 | * REQ_RN 165 | * READ 166 | * WRITE 167 | * LOCK 168 | 169 | TODO: 170 | * ACCESS 171 | * KILL 172 | * AUTHENTICATE and related commands 173 | * ... 174 | 175 | In addition to the above commands I will start putting together a simple gui so that it is more user friendly to use. 176 | The "reader" can read the tag in a distance range of about 1-2 cm. For the write operation to work properly the tag needs to be in the 1cm range as the tags chip needs more power for write operations. 177 | 178 | I will be starting to setup a Howto guide in case someones wants to rebuild the reader for her own experiments. Below some screenshots / fotos of the reader in operation. 179 | 180 | ![a](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/uhfreader1.png) 181 | ![b](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/uhfreader3.png) 182 | ![c](https://github.com/zaphoxx/zuhf-rfid/blob/main/images/uhfread3.png) 183 | 184 | -------------------------------------------------------------------------------- /TROUBLESHOOTING.md: -------------------------------------------------------------------------------- 1 | # TROUBLESHOOTING 2 | 3 | ## ISSUE - Tag does not respond to an REQ_RN request 4 | 5 | ### Description 6 | If the tag properly responds to the QUERY request and confirms the interrogators ACK with a valid EPC response but fails to respond to an subsequent REQ_RN request then 7 | this might be a timing issue. Once the tag replies to your REQ_RN timing is no problem anymore but up to this point the interrogator has to respond or send requests within ~ 500ms 8 | after tags last reply. 9 | 10 | ### Solution 11 | So for the specific issue where you get a proper EPC response the timing within the function ```read_epc()``` in the ZUHF_CC1101 library might be too long. Check the description 12 | in the function and try to lower the settings for the variables x1, x2 within the function. See function snippet and description below. 13 | I found x1=12,x2=32 or x1=14,x2=30 to be good working values. I experienced that for x2=32 the timing might not work for some rfid chips (e.g. MONZA4) but x2=30 worked all good. 14 | Whereas for a Alien Higgs3 Chip the x2=32 value worked all good. 15 | See also function snippet below for more information: 16 | 17 | ``` 18 | /******************************************************************************************************************************** 19 | * FUNCTION NAME: read_epc() 20 | * FUNCTION : searches and reads epc data from response 21 | * INPUT : epc_data structure to be filled in 22 | * OUTPUT : returns true if crc16 check is ok; if not it will still fill the 23 | * epc_data structure but the crc16 flag will set to false; 24 | * INFO : pay special attention to the variables x1,x2 below; 25 | **********************************************************************************************************************************/ 26 | bool read_epc(EPC_DATA *tag_epc){ 27 | //Serial.println("[READ EPC] *"); 28 | #define PLEN 32 29 | bool found = false; 30 | bool crc_ok = false; 31 | byte epc_data[PLEN]; 32 | byte epc_bits[4*PLEN]; 33 | byte epc_bytes[PLEN/2]; 34 | 35 | RX_UNIT.SpiWriteReg(CC1101_PKTLEN,PLEN); 36 | RX_UNIT.SpiStrobe(CC1101_SRX); 37 | 38 | /* If the Reader has difficulties finding the epc or performing other actions after reading the epc 39 | * then you might try to tune the value for TX_UNIT.SendCW(x1/x2) a bit. Try to make it as low as 40 | * possible. Values > 14 are typically to long and will violate the max repsonse time reader -> tag. 41 | * If the value is to low then the reader might not have enough time to capture the radiosignal. 42 | */ 43 | 44 | // Sweet spot values are currently x1=12, x2=32 (or x1=14, x2=30) for the SendCW(x1/x2) values below 45 | byte x1 = 12; 46 | byte x2 = 32; 47 | TX_UNIT.SendCW(x1); // x1: critical value, change only if really necessary and you know what you are doing! 48 | while(TX_GDO0_STATE) 49 | { 50 | if (RX_GDO0_STATE) 51 | { 52 | // x2: TX_UNIT.SendCW(32) 53 | // If tags with another chip (e.g. Monza or Alien Higgs) dont respond to an req_rn then this CW timing of 54 | // the read_epc might be too large which causes the tag to fall back into its initial state 55 | // (from acknowledged --> arbitrate); the req_rn command will then be ignored by the tag 56 | // In order to stay within the response time restraints you can try to lower the value from below TX_UNIT.SendCW(xx) 57 | TX_UNIT.SendCW(x2); // x2: similar to above but not as sensitive as above. 58 | while(RX_GDO0_STATE); 59 | RX_UNIT.SpiReadBurstReg(CC1101_RXFIFO, epc_data, PLEN); 60 | found = true; 61 | break; 62 | } 63 | } 64 | ``` 65 | -------------------------------------------------------------------------------- /ZUHF-RFID.md: -------------------------------------------------------------------------------- 1 | # ZUHF-RFID 2 | 3 | ## Name: ZUHF-RFID 4 | 5 | ##Description: Arduino Sketch to run a self build UHF RFID Reader (Read/Write); 6 | This sketch is specifically coded for the Arduino Due. It most likely will not 7 | work with other Arduino boards. 8 | 9 | ## Author: Manfred Heinz 10 | 11 | ## Last Update: 05.05.2021 12 | 13 | Copyright (C) 2021 Manfred Heinz 14 | 15 | This program is free software: you can redistribute it and/or modify 16 | it under the terms of the GNU General Public License as published by 17 | the Free Software Foundation, either version 3 of the License, or 18 | (at your option) any later version. 19 | 20 | This program is distributed in the hope that it will be useful, 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 | GNU General Public License for more details. 24 | 25 | You should have received a copy of the GNU General Public License 26 | along with this program. If not, see . 27 | 28 | ## Files: 29 | 30 | - in folder ```ZUHF-RFID/``` 31 | - ```ZUHF-RFID.ino``` 32 | - Main sketch file(s) 33 | 34 | - in folder ```library/ZUHF_CC1101/``` 35 | - library to control the RX-Module (CC1101 Chip) 36 | - library with crc5 / crc16 functions needed 37 | - definitions for the CC1101 registers 38 | - Contains also files which define global vars 39 | 40 | - in folder ```library/SPI_UART_CC1101/``` 41 | - library to control the TX-Module (CC1101 Chip) 42 | - in folder ```cli/``` 43 | - ```zuhf-cli.py``` - python commandline interface 44 | 45 | ## Installation 46 | - import the zip files (libraries) with the Arduino Ide using ```Sketch-->Include Library-->Add .ZIP Library```, select the zip files SPI_UART_CC1101.zip and ZUHF_CC1101.zip for import. 47 | - Copy the folder ```./ZUHF-RFID/``` into your Arduino Projects/Sketch folder. 48 | - Open the Arduino IDE, open the ZUHF-RFID.ino sketch. 49 | - Select your Arduino Due board in the 'Tools' section and select the port your board is connected to. 50 | - Upload the sketch to the board. 51 | - Once this is complete you can close the Arduino IDE. 52 | - Now you can use the CLI tool ```zuhf-cli.py``` as described below. Make sure you note which port your board is connected to. 53 | 54 | ## Hardware required 55 | - Arduino Due 56 | - 2 RF-Modules based on CC1101, where the RX-Module is connected to the Arduinos main SPI connectors and the TX-Module is connected to USART1 (TX2/RX2) (for more information please refer to the HOWTO_PART1); 57 | 58 | ## zuhf-cli.py 59 | zuhf-cli.py is a simple python based command line interface to control the zuhf-rfid-reader. The cli requires python3 to be installed but other then that there are no other special libraries needed other then the standarts which are included with a fresh python3 installation. The cli communicates via serial communication with the reader. Make sure you check which usb port your reader is connected to. on windows this would be e.g. comXX whereas on linux that will be e.g. /dev/ttyACMx. 60 | ### usage (zuhf-cli.py needs to be run as privileged user otherwise you wont be able to connect with the serial port) 61 | 62 | ```bash 63 | 64 | └─# ./zuhf-cli.py -h 65 | 66 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 67 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 68 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 69 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 70 | :.::.: : :.:: : : : : : : : : : : :: : : 71 | 72 | 73 | usage: zuhf-cli.py [-h] [-p PORT] [-b BAUDRATE] [-tp {17,18,19,20,21,22}] [-t TIMEOUT] 74 | [-r REPETITIONS] [-block BLOCK_ADDR] [-mem {0,1,2,3}] [-n N_WORDS] 75 | [-data DATA] [-mask LOCK_MASK] [-action LOCK_ACTION] [-access ACCESS_PWD] 76 | [-write | -lock | -read | -monza] 77 | 78 | optional arguments: 79 | -h, --help show this help message and exit 80 | -p PORT serial port e.g. COM7,COM14 81 | -b BAUDRATE baudrate of serial 82 | -tp {17,18,19,20,21,22} 83 | tx_power - index 84 | -t TIMEOUT serial read timeout 85 | -r REPETITIONS repetitions to run before terminating 86 | -block BLOCK_ADDR blockaddress to read/write from 87 | -mem {0,1,2,3} memory block to read / write from e.g. user block 88 | -n N_WORDS read n words from mem block, max is 32 words 89 | -data DATA word data to write to mem block 90 | -mask LOCK_MASK 91 | -action LOCK_ACTION 92 | -access ACCESS_PWD 93 | -write 94 | -lock 95 | -read 96 | -monza 97 | 98 | ``` 99 | #### Selecting the right port 100 | To find the right port you can use the following command 101 | ``` 102 | └─$ sudo dmesg | grep -i '\(arduino\|tty\)' 103 | [ 0.610773] printk: console [tty0] enabled 104 | [ 3.391005] 00:05: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A 105 | [ 3.402548] 00:06: ttyS1 at I/O 0x2f8 (irq = 3, base_baud = 115200) is a 16550A 106 | [ 6.541678] systemd[1]: Created slice system-getty.slice. 107 | [33709.456826] usb 2-2.2: Product: Arduino Due Prog. Port 108 | [33709.456827] usb 2-2.2: Manufacturer: Arduino (www.arduino.cc) 109 | [33709.498375] cdc_acm 2-2.2:1.0: ttyACM0: USB ACM device 110 | ``` 111 | In the example above you can see that my Arduino Due is connected at ```/dev/ttyACM0```. 112 | 113 | ### Running the reader with zuhf-cli.py 114 | 115 | #### Reading EPC information 116 | So to run a simple command you can then do (make sure you see the ```Running ...``` statement before approaching the tag to the reader. Otherwise the reader wont be ready yet.) 117 | ``` 118 | ─# ./zuhf-cli.py -p /dev/ttyACM0 119 | 120 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 121 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 122 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 123 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 124 | :.::.: : :.:: : : : : : : : : : : :: : : 125 | 126 | 127 | [info] Using following settings: 128 | ------------------------------------- 129 | port : /dev/ttyACM0 130 | baudrate : 250000 131 | tx_power : 22 132 | timeout : 3 133 | repetitions : 1000 134 | block_addr : 0 135 | mem_block : 3 136 | n_words : 1 137 | data : 1337 138 | lock_mask : 0000000000 139 | lock_action : 0000000000 140 | access_pwd : 00000000 141 | write_flag : False 142 | lock_flag : False 143 | read_flag : False 144 | monza : False 145 | ------------------------------------- 146 | 147 | 148 | Running ... 149 | [ACKNOWLEDGED] 150 | [ACKNOWLEDGED] 151 | [TAG-DATA] 152 | ############################################################## 153 | Stored PC: 0x34 0x0 154 | EPC: 300833B2DEADDEADBABABABA 155 | CRC16: 0x65 0x1e 156 | ############################################################## 157 | ``` 158 | So running the cli without other arguments will repeat 1000 times to try to read a tags EPC information. 159 | 160 | #### Reading selected memory areas 161 | The following command will read 1 word from user memory starting at the 4th block (4th word) (numbering starts with 0 ); 162 | ``` 163 | sudo python3 zuhf-cli.py -p /dev/ttyACM0 -n 1 -mem 3 -block 3 164 | ``` 165 | where parameter ```-n 1``` defines the number of words to read and ```-mem 3``` defines from which memory block the reader should pull the information. You can also define from which block address you want to start reading by using the parameter ```-block ``` where address is 0th,1st,2nd,3rd,4th,... word of memblock. 166 | Memory Blocks are usually defined as: 167 | * 0 - Reserved Memory (Kill Password and Access Password locations) 168 | * 1 - EPC Memory 169 | * 2 - TID Memory 170 | * 3 - User Memory 171 | 172 | #### writing data to the tag/card 173 | You can write data to a memory block (if not locked) using the following command 174 | ``` 175 | └─# ./zuhf-cli.py -p /dev/ttyACM0 -write -data deadbeef -mem 3 -block 0 176 | 177 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 178 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 179 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 180 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 181 | :.::.: : :.:: : : : : : : : : : : :: : : 182 | 183 | 184 | deadbeef 2 185 | [info] Using following settings: 186 | ------------------------------------- 187 | port : /dev/ttyACM0 188 | baudrate : 250000 189 | tx_power : 22 190 | timeout : 3 191 | repetitions : 1000 192 | block_addr : 0 193 | mem_block : 3 194 | n_words : 2 195 | data : deadbeef 196 | lock_mask : 0000000000 197 | lock_action : 0000000000 198 | access_pwd : 00000000 199 | write_flag : True 200 | lock_flag : False 201 | read_flag : False 202 | monza : False 203 | ------------------------------------- 204 | 205 | 206 | b'\xde\xad\xbe\xef' 207 | [+] WRITE MODE 208 | Running ... 209 | [ACKNOWLEDGED] 210 | [OPEN/SECURED] 211 | [WRITE] * 212 | [REQ_RN] * 213 | [REQ_RN(Handle)] 214 | [+] WRITE COMPLETE 215 | [REQ_RN] * 216 | [REQ_RN(Handle)] 217 | [+] WRITE COMPLETE 218 | [TAG-DATA] 219 | ############################################################## 220 | Stored PC: 0x34 0x0 221 | EPC: 300833B2DEADDEADBABABABA 222 | CRC16: 0x65 0x1e 223 | ############################################################## 224 | 225 | ``` 226 | So to write data you need to provide the ```-write``` parameter and the ```-data ``` parameter with the data to write. The data needs to be in hex format MSB first and the number of bytes provided needs to be a multiple of 2 (n words) (if you don't provide the -n parameter it will calculate n based on the provided data value). You can verify if the write was successful by trying to read the location after the write using 227 | 228 | ```bash 229 | ─# ./zuhf-cli.py -p /dev/ttyACM0 -read -mem 3 -n 2 230 | 231 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 232 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 233 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 234 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 235 | :.::.: : :.:: : : : : : : : : : : :: : : 236 | 237 | 238 | [info] Using following settings: 239 | ------------------------------------- 240 | port : /dev/ttyACM0 241 | baudrate : 250000 242 | tx_power : 22 243 | timeout : 3 244 | repetitions : 1000 245 | block_addr : 0 246 | mem_block : 3 247 | n_words : 2 248 | data : deadbeef 249 | lock_mask : 0000000000 250 | lock_action : 0000000000 251 | access_pwd : 00000000 252 | write_flag : False 253 | lock_flag : False 254 | read_flag : True 255 | monza : False 256 | ------------------------------------- 257 | 258 | 259 | [+] Read Mode 260 | Running ... 261 | [ACKNOWLEDGED] 262 | [OPEN/SECURED] 263 | [MEM BLOCK 3] 264 | ############### 265 | 0x000000: de ad 266 | 0x000001: be ef 267 | ############### 268 | [TAG-DATA] 269 | ############################################################## 270 | Stored PC: 0x34 0x0 271 | EPC: 300833B2DEADDEADBABABABA 272 | CRC16: 0x65 0x1e 273 | ############################################################## 274 | 275 | ``` 276 | 277 | #### r/w Lock Memory Areas 278 | You can also r/w lock or permalock memory areas. If you are experimenting with that feature and you want to be on the save side then make sure you do NOT lock the access password and you do not permalock any memory area (the later is not reversible). For details on which bits need to be set please refer to pg.89 in the official documentation https://www.gs1.org/sites/default/files/docs/epc/gs1-epc-gen2v2-uhf-airinterface_i21_r_2018-09-04.pdf. 279 | For example if you want to r/w protect the 'Kill Password' you would need to send the following command. (This works if the access password is 0; once the lock is successful you still need to write an access password; See below on how to lock memory if access password is already set) 280 | 281 | ```bash 282 | └─# ./zuhf-cli.py -p /dev/ttyACM0 -lock -mask 1000000000 -action 1000000000 283 | 284 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 285 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 286 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 287 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 288 | :.::.: : :.:: : : : : : : : : : : :: : : 289 | 290 | 291 | [info] Using following settings: 292 | ------------------------------------- 293 | port : /dev/ttyACM0 294 | baudrate : 250000 295 | tx_power : 22 296 | timeout : 3 297 | repetitions : 1000 298 | block_addr : 0 299 | mem_block : 3 300 | n_words : 1 301 | data : 1337 302 | lock_mask : 1000000000 303 | lock_action : 1000000000 304 | access_pwd : 00000000 305 | write_flag : False 306 | lock_flag : True 307 | read_flag : False 308 | monza : False 309 | ------------------------------------- 310 | 311 | 312 | [+] LOCK MODE 313 | Running ... 314 | [ACKNOWLEDGED] 315 | [OPEN/SECURED] 316 | [LOCK] Successful 317 | [TAG-DATA] 318 | ############################################################## 319 | Stored PC: 0x34 0x0 320 | EPC: 300833B2DEADDEADBABABABA 321 | CRC16: 0x65 0x1e 322 | ############################################################## 323 | 324 | ``` 325 | So now access to the kill password is password protected. however you still need to set a password with the write command. once you done that the kill password is password protected. 326 | 327 | writing an access password: 328 | 329 | ```bash 330 | ─# ./zuhf-cli.py -p /dev/ttyACM0 -write -data 12345678 -mem 0 -n 2 -block 2 331 | 332 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 333 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 334 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 335 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 336 | :.::.: : :.:: : : : : : : : : : : :: : : 337 | 338 | 339 | 12345678 2 340 | [info] Using following settings: 341 | ------------------------------------- 342 | port : /dev/ttyACM0 343 | baudrate : 250000 344 | tx_power : 22 345 | timeout : 3 346 | repetitions : 1000 347 | block_addr : 2 348 | mem_block : 0 349 | n_words : 2 350 | data : 12345678 351 | lock_mask : 0000000000 352 | lock_action : 0000000000 353 | access_pwd : 00000000 354 | write_flag : True 355 | lock_flag : False 356 | read_flag : False 357 | monza : False 358 | ------------------------------------- 359 | 360 | 361 | b'\x124Vx' 362 | [+] WRITE MODE 363 | Running ... 364 | [ACKNOWLEDGED] 365 | [OPEN/SECURED] 366 | [WRITE] * 367 | 0 368 | 2 369 | 0 370 | [REQ_RN] * 371 | [REQ_RN(Handle)] 372 | [+] WRITE COMPLETE 373 | [REQ_RN] * 374 | [REQ_RN(Handle)] 375 | [+] WRITE COMPLETE 376 | [TAG-DATA] 377 | ############################################################## 378 | Stored PC: 0x34 0x0 379 | EPC: 300833B2DEADDEADBABABABA 380 | CRC16: 0x65 0x1e 381 | ############################################################## 382 | 383 | ``` 384 | 385 | Lets check if we can still read the 'Kill Password': 386 | 387 | ```bash 388 | ─# ./zuhf-cli.py -p /dev/ttyACM0 -read -mem 0 -n 4 389 | 390 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 391 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 392 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 393 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 394 | :.::.: : :.:: : : : : : : : : : : :: : : 395 | 396 | 397 | [info] Using following settings: 398 | ------------------------------------- 399 | port : /dev/ttyACM0 400 | baudrate : 250000 401 | tx_power : 22 402 | timeout : 3 403 | repetitions : 1000 404 | block_addr : 0 405 | mem_block : 0 406 | n_words : 4 407 | data : 1337 408 | lock_mask : 0000000000 409 | lock_action : 0000000000 410 | access_pwd : 00000000 411 | write_flag : False 412 | lock_flag : False 413 | read_flag : True 414 | monza : False 415 | ------------------------------------- 416 | 417 | 418 | [+] Read Mode 419 | Running ... 420 | [ACKNOWLEDGED] 421 | [OPEN/SECURED] 422 | [READ] ERROR. Code: 4 423 | [READ] Memory locked - The Tag memory location is locked or permalocked and is either not writeable1 or not readable. 424 | [MEM BLOCK 0] 425 | ############### 426 | 0x000000: ff ff 427 | 0x000001: ff ff 428 | 0x000002: ff ff 429 | 0x000003: ff ff 430 | ############### 431 | [TAG-DATA] 432 | ############################################################## 433 | Stored PC: 0x34 0x0 434 | EPC: 300833B2DEADDEADBABABABA 435 | CRC16: 0x65 0x1e 436 | ############################################################## 437 | ``` 438 | 439 | Perfect, 'Kill Password' is locked now and not readable without the access password. (ATTENTION: For this example keep in mind that the 'Access Password' is not locked. Therefore the protection can be bypassed by simply setting the access password to 0). 440 | 441 | So to read or write a 'Kill Password' you now need to provide the 'Access Password'. 442 | 443 | #### Access Password Protected Memory Areas 444 | Following on the previous example: To read the protected 'Kill Password' you can use the following command 445 | 446 | ```bash 447 | └─# ./zuhf-cli.py -p /dev/ttyACM0 -access 12345678 -read -mem 0 -n 4 448 | 449 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 450 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 451 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 452 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 453 | :.::.: : :.:: : : : : : : : : : : :: : : 454 | 455 | 456 | [info] Using following settings: 457 | ------------------------------------- 458 | port : /dev/ttyACM0 459 | baudrate : 250000 460 | tx_power : 22 461 | timeout : 3 462 | repetitions : 1000 463 | block_addr : 0 464 | mem_block : 0 465 | n_words : 4 466 | data : 1337 467 | lock_mask : 0000000000 468 | lock_action : 0000000000 469 | access_pwd : 12345678 470 | write_flag : False 471 | lock_flag : False 472 | read_flag : True 473 | monza : False 474 | ------------------------------------- 475 | 476 | 477 | 1234 5678 478 | 12 34 479 | 56 78 480 | [READACCESS] 481 | Running ... 482 | [ACKNOWLEDGED] 483 | [OPEN/SECURED] 484 | SWITCH TO ACCESS 485 | [ACCESS 1] OK 486 | [SECURED] 487 | [MEM BLOCK 0] 488 | ############### 489 | 0x000000: ca fe 490 | 0x000001: ba be 491 | 0x000002: 12 34 492 | 0x000003: 56 78 493 | ############### 494 | [TAG-DATA] 495 | ############################################################## 496 | Stored PC: 0x34 0x0 497 | EPC: 300833B2DEADDEADBABABABA 498 | CRC16: 0x65 0x1e 499 | ############################################################## 500 | 501 | ``` 502 | So to be able to access a password protected area you just add the ```-access ``` to the action (read/write/lock) you want to perform. 503 | 504 | So to unlock the 'Kill Password' and allow unrestricted access you would the perform the following command: 505 | 506 | 507 | ```bash 508 | └─# ./zuhf-cli.py -p /dev/ttyACM0 -lock -mask 1000000000 -action 0000000000 -access 12345678 509 | 510 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 511 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 512 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 513 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 514 | :.::.: : :.:: : : : : : : : : : : :: : : 515 | 516 | 517 | [info] Using following settings: 518 | ------------------------------------- 519 | port : /dev/ttyACM0 520 | baudrate : 250000 521 | tx_power : 22 522 | timeout : 3 523 | repetitions : 1000 524 | block_addr : 0 525 | mem_block : 3 526 | n_words : 1 527 | data : 1337 528 | lock_mask : 1000000000 529 | lock_action : 0000000000 530 | access_pwd : 12345678 531 | write_flag : False 532 | lock_flag : True 533 | read_flag : False 534 | monza : False 535 | ------------------------------------- 536 | 537 | 538 | 1234 5678 539 | ### LOCKACCESS ### 540 | 12 34 541 | 56 78 542 | [LOCKACCESS] 543 | Running ... 544 | [ACKNOWLEDGED] 545 | [OPEN/SECURED] 546 | SWITCH TO ACCESS 547 | [ACCESS 1] OK 548 | [SECURED] 549 | [LOCK] Successful 550 | [TAG-DATA] 551 | ############################################################## 552 | Stored PC: 0x34 0x0 553 | EPC: 300833B2DEADDEADBABABABA 554 | CRC16: 0x65 0x1e 555 | ############################################################## 556 | ``` 557 | 558 | There you go! Now you should be able to read the 'Kill Password' without providing the access command. Let's try: 559 | ```bash 560 | ─# ./zuhf-cli.py -p /dev/ttyACM0 -read -mem 0 -n 4 561 | 562 | @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@ 563 | @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@ 564 | @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@! 565 | !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!! 566 | :.::.: : :.:: : : : : : : : : : : :: : : 567 | 568 | 569 | [info] Using following settings: 570 | ------------------------------------- 571 | port : /dev/ttyACM0 572 | baudrate : 250000 573 | tx_power : 22 574 | timeout : 3 575 | repetitions : 1000 576 | block_addr : 0 577 | mem_block : 0 578 | n_words : 4 579 | data : 1337 580 | lock_mask : 0000000000 581 | lock_action : 0000000000 582 | access_pwd : 00000000 583 | write_flag : False 584 | lock_flag : False 585 | read_flag : True 586 | monza : False 587 | ------------------------------------- 588 | 589 | 590 | [+] Read Mode 591 | Running ... 592 | [ACKNOWLEDGED] 593 | [OPEN/SECURED] 594 | [MEM BLOCK 0] 595 | ############### 596 | 0x000000: ca fe 597 | 0x000001: ba be 598 | 0x000002: 12 34 599 | 0x000003: 56 78 600 | ############### 601 | [TAG-DATA] 602 | ############################################################## 603 | Stored PC: 0x34 0x0 604 | EPC: 300833B2DEADDEADBABABABA 605 | CRC16: 0x65 0x1e 606 | ############################################################## 607 | ``` 608 | Perfect! So that's all there is for now. Have fun testing it! 609 | -------------------------------------------------------------------------------- /ZUHF-RFID/ConsoleMenu.h: -------------------------------------------------------------------------------- 1 | #ifndef CONSOLEMENU_H 2 | #define CONSOLEMENU_H 3 | 4 | #include 5 | #include 6 | 7 | enum M_STATES{ 8 | M_MAIN, 9 | M_CONFIG, 10 | M_RESULT, 11 | M_EXIT 12 | }; 13 | 14 | extern M_STATES current_Menu; 15 | extern void logo(); 16 | extern byte tx_power; 17 | extern uint32_t repetitions; 18 | extern byte agc2; 19 | extern byte agc0; 20 | extern uint32_t packet_delay; 21 | 22 | extern byte tag_settle; 23 | extern byte cw1; 24 | extern byte cw2; 25 | extern byte sync1,sync0; 26 | 27 | extern TAG_INFO tags_read[1000]; 28 | extern int tags_found; 29 | 30 | 31 | bool cmd_received = 0; 32 | 33 | void runMenu(); 34 | char getInput(); 35 | void printMain(); 36 | void runMain(); 37 | void clearScreen(); 38 | 39 | void runMenu() { 40 | switch(current_Menu){ 41 | case(M_MAIN): 42 | printMain(); 43 | runMain(); 44 | break; 45 | case(M_EXIT): 46 | break; 47 | default: 48 | printMain(); 49 | runMain(); 50 | break; 51 | } 52 | } 53 | 54 | char getInput(){ 55 | char rx_byte; 56 | if (Serial.available()){ 57 | rx_byte = Serial.read(); 58 | Serial.println(rx_byte); 59 | cmd_received = 1; 60 | return rx_byte; 61 | } 62 | } 63 | 64 | void printSummary(){ 65 | clearScreen(); 66 | int unique_tags_found = 0; 67 | byte epc[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; 68 | Serial.println("\t*********************** EPC *************************"); 69 | //Serial.println(n_tag_responses,DEC); 70 | 71 | for (int i = 0; i < tags_found; i++){ 72 | if (epc[11] != tags_read[i].EPC_Data[11] and tags_read[i].CRC_OK){ 73 | memcpy(epc, tags_read[i].EPC_Data,12); 74 | Serial.print("\tEPC: "); 75 | for (int j = 0; j < 12; j++){ 76 | Serial.print(epc[j],HEX);Serial.print(" "); 77 | //Serial.print(TAGS_READ[i].EPC_Data[j],HEX);Serial.print(" "); 78 | } 79 | Serial.print("\tCRC16 OK: "); 80 | Serial.print(tags_read[i].CRC_OK,DEC); 81 | Serial.println(); 82 | 83 | byte L = ( tags_read[i].StoredPC & L_MASK ) >> 11; 84 | bool UMI = ( tags_read[i].StoredPC & UMI_MASK ) >> 10; 85 | bool XI = ( tags_read[i].StoredPC & XI_MASK ) >> 9; 86 | bool T = ( tags_read[i].StoredPC & T_MASK ) >> 8; 87 | 88 | Serial.print("\tEPC Length: ");Serial.print(L,DEC); Serial.print(" Words ("); Serial.print(L * 16);Serial.println(" Bits)"); 89 | Serial.print("\tUMI: ");Serial.print(UMI);Serial.print("\tXI: ");Serial.print(XI);Serial.print("\tT: ");Serial.println(T); 90 | } 91 | } 92 | Serial.println("\t*********************** *** *************************"); 93 | Serial.print("\t[ ");Serial.print(tags_found);Serial.print(" / ");Serial.print(repetitions); Serial.println(" ]"); 94 | Serial.print("\tRate: ");Serial.println(( (float) tags_found / (float) repetitions) * 100); 95 | Serial.println("\t********************* *** ***********************"); 96 | 97 | char rx_byte; 98 | while(rx_byte != 'x' and rx_byte != 'X'){ 99 | rx_byte = getInput(); 100 | delay(100); 101 | } 102 | } 103 | 104 | void printMain(){ 105 | clearScreen(); 106 | Serial.println("********* MainMenu ********"); 107 | Serial.println(" (c)onfiguration Menu"); 108 | Serial.println(" (l)ast Run"); 109 | Serial.println(" (s)tart Run"); 110 | Serial.println("***************************"); 111 | } 112 | 113 | 114 | void printConfig(){ 115 | clearScreen(); 116 | // 0x11 --> 0x11/0x80 (5.2 dBm) 0x12/0x27 (-9.8 dBm) 0x13/0x67 (-5.0 dBm) 0x14/0x50 (-0.3 dBm) 0x16/0xc0 (9.8 dBm) byte PaTable[8] = {0x00,0x80,0x27,0x67,0x50,0x80,0xc0,0x00}; // 117 | Serial.println("********** Configuration ***********"); 118 | Serial.println("\tTX Power"); 119 | if (tx_power == 0x12) Serial.print(" *"); 120 | Serial.println("\t(1) -9.8 dBm"); 121 | if (tx_power == 0x13) Serial.print(" *"); 122 | Serial.println("\t(2) -5.0 dBm"); 123 | if (tx_power == 0x14) Serial.print(" *"); 124 | Serial.println("\t(3) -0.3 dBm"); 125 | if (tx_power == 0x11 or tx_power == 0x15) Serial.print(" *"); 126 | Serial.println("\t(4) 5.2 dBm"); 127 | if (tx_power == 0x16) Serial.print(" *"); 128 | Serial.println("\t(5) 9.8 dBm"); 129 | Serial.println("************************************"); 130 | Serial.println("\tRepetitions"); 131 | if (repetitions == 1) Serial.print(" *"); 132 | Serial.println("\t(6) 1 "); 133 | if (repetitions == 200) Serial.print(" *"); 134 | Serial.println("\t(7) 100"); 135 | if (repetitions == 500) Serial.print(" *"); 136 | Serial.println("\t(8) 500"); 137 | if (repetitions == 1000) Serial.print(" *"); 138 | Serial.println("\t(9) 1000"); 139 | Serial.println("************************************"); 140 | Serial.println("\t(a) AGC Control"); 141 | Serial.println("\t(d) Delay Control"); 142 | Serial.println("\t(c) CW Control"); 143 | Serial.println("************************************"); 144 | } 145 | 146 | 147 | void printAGC() 148 | { 149 | clearScreen(); 150 | Serial.println("********** AGC Control ***********"); 151 | Serial.println("\tAGC0"); 152 | if (agc0 == 0x90) Serial.print(" *"); 153 | Serial.println("\t(0) 0x90"); 154 | if (agc0 == 0x91) Serial.print(" *"); 155 | Serial.println("\t(1) 0x91"); 156 | if (agc0 == 0x92) Serial.print(" *"); 157 | Serial.println("\t(2) 0x92"); 158 | Serial.println("************************************"); 159 | Serial.println("\tAGC2"); 160 | if (agc2 == 0x03) Serial.print(" *"); 161 | Serial.println("\t(3) 0x03"); 162 | if (agc2 == 0x04) Serial.print(" *"); 163 | Serial.println("\t(4) 0x04"); 164 | if (agc2 == 0x05) Serial.print(" *"); 165 | Serial.println("\t(5) 0x05"); 166 | if (agc2 == 0x06) Serial.print(" *"); 167 | Serial.println("\t(6) 0x06"); 168 | if (agc2 == 0x07) Serial.print(" *"); 169 | Serial.println("\t(7) 0x07"); 170 | Serial.println("************************************"); 171 | } 172 | 173 | 174 | void runAGC(){ 175 | char rx_byte; 176 | printAGC(); 177 | while(rx_byte != 'x' and rx_byte != 'X'){ 178 | cmd_received = 0; 179 | rx_byte = getInput(); 180 | switch(rx_byte){ 181 | /* AGC CONTROLS */ 182 | case('0'): 183 | clearScreen(); 184 | agc0 = 0x90; 185 | printAGC(); 186 | break; 187 | case('1'): 188 | clearScreen(); 189 | agc0 = 0x91; 190 | printAGC(); 191 | break; 192 | case('2'): 193 | clearScreen(); 194 | agc0 = 0x92; 195 | printAGC(); 196 | break; 197 | case('3'): 198 | clearScreen(); 199 | agc2 = 0x03; 200 | printAGC(); 201 | break; 202 | case('4'): 203 | clearScreen(); 204 | agc2 = 0x04; 205 | printAGC(); 206 | break; 207 | case('5'): 208 | clearScreen(); 209 | agc2 = 0x05; 210 | printAGC(); 211 | break; 212 | case('6'): 213 | clearScreen(); 214 | agc2 = 0x06; 215 | printAGC(); 216 | break; 217 | case('7'): 218 | clearScreen(); 219 | agc2 = 0x07; 220 | printAGC(); 221 | break; 222 | default: 223 | if (cmd_received){ 224 | printAGC(); 225 | } 226 | break; 227 | } 228 | } 229 | } 230 | 231 | 232 | void runConfig(){ 233 | char rx_byte; 234 | printConfig(); 235 | while(rx_byte != 'x' and rx_byte != 'X'){ 236 | cmd_received = 0; 237 | rx_byte = getInput(); 238 | switch(rx_byte){ 239 | /* TX POWER OPTIONS */ 240 | case('1'): 241 | clearScreen(); 242 | tx_power = 0x12; 243 | printConfig(); 244 | break; 245 | case('2'): 246 | clearScreen(); 247 | tx_power = 0x13; 248 | printConfig(); 249 | break; 250 | case('3'): 251 | clearScreen(); 252 | tx_power = 0x14; 253 | printConfig(); 254 | break; 255 | case('4'): 256 | clearScreen(); 257 | tx_power = 0x15; 258 | printConfig(); 259 | break; 260 | case('5'): 261 | clearScreen(); 262 | tx_power = 0x16; 263 | printConfig(); 264 | break; 265 | /* *************** */ 266 | case('6'): 267 | clearScreen(); 268 | repetitions = 1; 269 | printConfig(); 270 | break; 271 | case('7'): 272 | clearScreen(); 273 | repetitions = 200; 274 | printConfig(); 275 | break; 276 | case('8'): 277 | clearScreen(); 278 | repetitions = 500; 279 | printConfig(); 280 | break; 281 | case('9'): 282 | clearScreen(); 283 | repetitions = 1000; 284 | printConfig(); 285 | break; 286 | case('a'): 287 | clearScreen(); 288 | //printAGC(); 289 | runAGC(); 290 | printConfig(); 291 | break; 292 | default: 293 | if (cmd_received){ 294 | printConfig(); 295 | } 296 | break; 297 | } 298 | } 299 | } 300 | 301 | 302 | void runMain(){ 303 | char rx_byte; 304 | printMain(); 305 | while(rx_byte != 'S' and rx_byte != 's'){ 306 | cmd_received = 0; 307 | rx_byte = getInput(); 308 | switch(rx_byte){ 309 | case('C'): 310 | case('c'): 311 | Serial.println("TODO: Switch to Configuration Menu"); 312 | runConfig(); 313 | printMain(); 314 | break; 315 | case('L'): 316 | case('l'): 317 | printSummary(); 318 | printMain(); 319 | break; 320 | case('#'): 321 | clearScreen(); 322 | logo(); 323 | delay(1000); 324 | printMain(); 325 | break; 326 | default: 327 | if (cmd_received){ 328 | printMain(); 329 | } 330 | break; 331 | } 332 | delay(10); 333 | } 334 | } 335 | 336 | 337 | void clearScreen(){ 338 | if (Serial) { 339 | Serial.write(27); 340 | Serial.print("[2J"); 341 | } 342 | } 343 | 344 | #endif 345 | -------------------------------------------------------------------------------- /ZUHF-RFID/ZUHF-RFID.ino: -------------------------------------------------------------------------------- 1 | /* ZUHF-RFID - Arduino Sketch to run a self build UHF RFID Reader (Read/Write) 2 | Version: v1c - supporting CLI accessibility via zuhf-cli.py 3 | Author: Manfred Heinz 4 | Last Update: 01.06.2021 5 | Copyright (C) 2021 Manfred Heinz 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | /* Make sure you are using the DUE specific SPI library */ 29 | /* You need to install the Arduino IDE for WIN10 - do not install one of the hourly builds */ 30 | #include "UHF-RFID.h" 31 | //#include "ConsoleMenu.h" 32 | 33 | #include 34 | 35 | #define DEBUG 1 36 | #define QUERYSIZE 22 37 | #define MAXQUERY 1024 38 | /* SHIELD SPECIFIC SETTINGS */ 39 | #define LED_BLUE 53 40 | #define LED_RED 51 41 | #define LED_GREEN 49 42 | #define LEDDATA_PIN 50 // to control two WS2812B LEDs onboard for fun 43 | #define NUM_LEDS 2 44 | #define BRIGHTNESS 16 45 | #define LED_TYPE WS2812 46 | #define COLOR_ORDER GRB 47 | CRGB leds[NUM_LEDS]; 48 | /* ************************ */ 49 | 50 | //#define RN16_LEN 4 // FM0 encoded RN16 length is here 4Bytes 51 | 52 | /* ************* MAIN DEFAULT CONTROL SETTINGS ************* */ 53 | #define TX_POWER 0x16 // 0x11 --> 0x11/0x80 (5.2 dBm) 0x12/0x27 (-9.8 dBm) 0x13/0x67 (-5.0 dBm) 0x14/0x50 (-0.3 dBm) 0x16/0xc0 (10.7 dBm) byte PaTable[8] = {0x00,0x80,0x27,0x67,0x50,0x80,0xc0,0x00}; // 54 | #define REPETITIONS 100 55 | #define AGC2 0x03 56 | #define AGC0 0x90 57 | #define DELAY 200 58 | #define TAG_SETTLE 15 59 | #define CW1 14 // CW after QUERY ~14 * 100us (1.4 ms) 60 | #define CW2 40 // CW after ACK ~40 * 100us (4.0 ms) 61 | /* ******************************************** */ 62 | 63 | /* fast digital write for LEDS */ 64 | // holistical setup 65 | #define LED_BLUE_ON (PIOB->PIO_SODR = (1 << 14)) 66 | #define LED_RED_ON (PIOC->PIO_SODR = (1 << 12)) 67 | #define LED_BLUE_OFF (PIOB->PIO_CODR = (1 << 14)) 68 | #define LED_RED_OFF (PIOC->PIO_CODR = (1 << 12)) 69 | #define LED_GREEN_ON (PIOC->PIO_SODR = (1 << 14)) 70 | #define LED_GREEN_OFF (PIOC->PIO_CODR = (1 << 14)) 71 | 72 | // v1b setup 73 | /* 74 | #define LED_BLUE_ON (PIOD->PIO_SODR = (1 << 1)) 75 | #define LED_RED_ON (PIOD->PIO_SODR = (1 << 0)) 76 | #define LED_BLUE_OFF (PIOD->PIO_CODR = (1 << 1)) 77 | #define LED_RED_OFF (PIOD->PIO_CODR = (1 << 0)) 78 | #define LED_GREEN_ON (PIOD->PIO_SODR = (1 << 3)) 79 | #define LED_GREEN_OFF (PIOD->PIO_CODR = (1 << 3)) 80 | */ 81 | /* fast digital read for GDO pins */ 82 | //#define TX_GDO0_STATE (((PIOC->PIO_PDSR) & (1 << 2)) >> 2) 83 | //#define TX_GDO2_STATE (((PIOC->PIO_PDSR) & (1 << 4)) >> 4) 84 | //#define RX_GDO0_STATE (((PIOA->PIO_PDSR) & (1 << 15)) >> 15) 85 | //#define RX_GDO2_STATE (((PIOD->PIO_PDSR) & (1 << 1)) >> 1) 86 | 87 | /* configuration variables */ 88 | byte tx_power = TX_POWER; 89 | uint32_t repetitions = REPETITIONS; 90 | byte agc2 = AGC2; 91 | byte agc0 = AGC0; 92 | uint32_t packet_delay = DELAY; 93 | 94 | /* others */ 95 | byte tag_settle = TAG_SETTLE; 96 | byte cw1 = CW1; 97 | byte cw2 = CW2; 98 | byte sync1,sync0; 99 | /* ******************************************** */ 100 | 101 | 102 | 103 | //DATA_BUFFER databuffer; 104 | TAG_INFO tags_read[1000]; 105 | /* array to hold query in form of a bit array */ 106 | byte QUERY[QUERYSIZE]; 107 | /* array to hold tags response in form of a bit array */ 108 | byte TAG_RESPONSE[512]; 109 | /* array to hold the PIE encoded query in form of a bit array */ 110 | byte ENCODED_QUERY[MAXQUERY]; 111 | /* array holding the request in the final byte format which will be send to TX FIFO */ 112 | byte FIFOBUFFER[512]; 113 | /* array holding the ACK command in form of a bit array */ 114 | byte ACK_BITS[128]; 115 | /* array holding the RN16 bits */ 116 | byte RN16_Bits[16]; 117 | byte HANDLE_Bits[16]; 118 | byte tmp_bits[16]; 119 | byte REQ_RN_BITS[40]; // 1 byte command + 2 bytes RN16 + 2 bytes CRC16 120 | 121 | 122 | /* buffer to hold data received on rx module */ 123 | byte RX_BUFFER[512]; 124 | byte EPC_BUFFER[32]; 125 | 126 | //bool RN16_Found = 0; 127 | //bool EPC_Flag = 0; 128 | uint64_t EPCID; 129 | 130 | int buffer_size = 0; 131 | int irounds = 1; 132 | byte cw_counter = 0; 133 | byte encoded_size = 0; 134 | byte check = 0; 135 | uint32_t counter = 0; 136 | 137 | byte rx_version = 0; 138 | byte tx_version = 0; 139 | 140 | int tags_found = 0; 141 | 142 | int n_tag_responses = 0; 143 | byte CWA[64]; 144 | 145 | unsigned long timeStart; 146 | unsigned long timeEnd; 147 | byte words; 148 | byte cmd_bytes[256]; 149 | byte serial_data; 150 | String cmd_string; 151 | //M_STATES current_Menu; 152 | boolean write_flag=false, read_flag=false, block_flag=false, lock_flag=false, epc_found=false, monza_flag=true, access_flag; 153 | EPC_DATA tag_data; 154 | 155 | /* MEMBLOCK READ/WRITE VARS */ 156 | byte memoryblock = 3; 157 | byte blockaddr = 0; 158 | byte nWords = 1; 159 | word dataword = 0; 160 | byte data[512]; 161 | byte mask_bits[10], action_bits[10]; 162 | uint16_t lpass,hpass; 163 | byte wordcount=0; 164 | 165 | 166 | void(* resetFunc) (void) = 0; 167 | 168 | 169 | void setup() 170 | { 171 | // delay to avoid known reset issue - see also https://forum.arduino.cc/index.php?topic=256771.75 172 | delay(1000); 173 | Serial.begin(250000); 174 | pinMode(LED_BLUE, OUTPUT); 175 | pinMode(LED_RED, OUTPUT); 176 | pinMode(LED_GREEN, OUTPUT); 177 | pinMode(LEDDATA_PIN, OUTPUT); 178 | FastLED.addLeds(leds, NUM_LEDS); // GRB ordering is typical 179 | FastLED.setBrightness(BRIGHTNESS); 180 | int byteblocks; 181 | /* fill CWA array */ 182 | for (int i = 0; i < 64; i++) 183 | { 184 | CWA[i] = 0xff; 185 | } 186 | 187 | //logo(); 188 | reader_state = R_WAIT; 189 | counter = 0; 190 | // current_Menu = M_MAIN; 191 | read_flag = false; 192 | write_flag = false; 193 | block_flag = false; 194 | lock_flag = false; 195 | access_flag = false; 196 | lpass = 0; 197 | hpass = 0; 198 | for (int i = 0; i < sizeof(data); i++) data[i] = 0; 199 | } 200 | 201 | 202 | void loop() 203 | { 204 | switch(reader_state) 205 | { 206 | case R_INIT: 207 | /* reset all necessary variables */ 208 | 209 | counter = 0; 210 | epc_found = 0; 211 | tags_found = 0; 212 | tx_version = TX_UNIT.Init(); // Based on SPI_UART_CC1101.h lib 213 | rx_version = RX_UNIT.Init(); // Based on Arduino SPI.h and ZUHF_CC1101.h lib 214 | 215 | /* quick check if modules version looks ok, if modules do not work properly the version value will not match */ 216 | //tx_version = TX_UNIT.Init(); // Based on SPI_UART_CC1101.h lib 217 | //rx_version = RX_UNIT.Init(); // Based on Arduino SPI.h and ZUHF_CC1101.h lib 218 | delay(10); 219 | tx_version = TX_UNIT.SpiReadStatus(CC1101_VERSION); 220 | rx_version = RX_UNIT.SpiReadStatus(CC1101_VERSION); 221 | 222 | if ((tx_version == 0x14 or tx_version == 0x04) and (rx_version == 0x14 or rx_version == 0x04)) 223 | { 224 | Serial.println("[CC1101] Modules Check - OK"); 225 | //Serial.println("[TX] SFTXON"); 226 | TX_UNIT.SpiStrobe(CC1101_SFSTXON); 227 | //Serial.println("[RX] SFTXON"); 228 | RX_UNIT.SpiStrobe(CC1101_SFSTXON); 229 | 230 | n_tag_responses = 0; 231 | /* syncronization word - preamble + Framesync */ 232 | sync1 = 0xAD; 233 | sync0 = 0x23; 234 | /* ************************ RX UNIT REG SETTINGS ************************* */ 235 | RX_UNIT.SpiWriteReg(CC1101_MCSM0,0x28); 236 | RX_UNIT.SpiWriteReg(CC1101_MCSM1,0x35); 237 | RX_UNIT.SpiWriteReg(CC1101_FREND0,tx_power); 238 | RX_UNIT.SpiWriteReg(CC1101_PKTCTRL0, 0x00); 239 | RX_UNIT.SpiWriteReg(CC1101_PKTLEN, 0x0c); 240 | RX_UNIT.SpiWriteReg(CC1101_IOCFG0, 0x06); 241 | RX_UNIT.SpiWriteReg(CC1101_MDMCFG2, 0x32); 242 | /* syncronization word - preamble + Framesync */ 243 | RX_UNIT.SpiWriteReg(CC1101_SYNC1, sync1); 244 | RX_UNIT.SpiWriteReg(CC1101_SYNC0, sync0); 245 | RX_UNIT.SpiWriteReg(CC1101_AGCCTRL2, agc2); 246 | RX_UNIT.SpiWriteReg(CC1101_AGCCTRL1, 0x00); 247 | RX_UNIT.SpiWriteReg(CC1101_AGCCTRL0, agc0); 248 | RX_UNIT.SpiWriteReg(CC1101_FIFOTHR, 0x07); 249 | /* ************************ RX UNIT REG SETTINGS ************************* */ 250 | 251 | Serial.print("TX power : ");Serial.println(tx_power,HEX); 252 | Serial.print("AGC2 : ");Serial.println(agc2,HEX); 253 | Serial.print("AGC0 : ");Serial.println(agc0,HEX); 254 | Serial.print("PktDelay [µs] : ");Serial.println(packet_delay,DEC); 255 | Serial.print("REPETITIONS : ");Serial.println(repetitions,DEC); 256 | leds[0] = CRGB::Magenta; 257 | leds[1] = CRGB::Magenta; 258 | FastLED.show(); 259 | delay(500); 260 | Serial.println("**** START RUN ****"); 261 | reader_state = R_START; 262 | } 263 | else 264 | { 265 | Serial.println("[CC1101] Error on CC1101 module initialization"); 266 | reader_state = R_WAIT; 267 | delay(100); 268 | } 269 | break; 270 | 271 | case R_START: 272 | LED_BLUE_OFF; 273 | LED_RED_OFF; 274 | leds[0] = CRGB::Magenta; 275 | leds[1] = CRGB::Magenta; 276 | FastLED.show(); 277 | /* Set modules into idle mode */ 278 | /*this section is necessary otherwise the rx-module will not capture any more signals */ 279 | /* begin */ 280 | while ((TX_UNIT.SpiReadStatus(CC1101_TXBYTES) & 0x7f) > 0); 281 | /* Terminate TX Session - Go Into Standby */ 282 | TX_UNIT.SpiStrobe(CC1101_SIDLE); 283 | RX_UNIT.SpiStrobe(CC1101_SIDLE); 284 | delay(5); 285 | /* end */ 286 | 287 | for (int i = 0; i < 16; i++) RN16_Bits[i] = 0; 288 | for (int i = 0; i < 16; i++) HANDLE_Bits[i] = 0; 289 | wordcount = 0; 290 | LED_GREEN_ON; 291 | /* ********** START TX AND SEND CW FOR TAG SETTLE ********** */ 292 | TX_UNIT.SpiStrobe(CC1101_SFTX); 293 | RX_UNIT.SpiStrobe(CC1101_SFRX); 294 | delay(5); 295 | TX_UNIT.SpiStrobe(CC1101_STX); 296 | TX_UNIT.SpiWriteBurstReg(CC1101_TXFIFO, CWA, 20); 297 | /* ********** *********************************** ********** */ 298 | { 299 | reader_state = R_QUERY; 300 | } 301 | break; 302 | 303 | /* QUERY REQUEST 304 | * R_QUERY: 305 | * 1) send QUERY request and read tag response (RN16) 306 | * 2) send ACK and read tags EPC response / if only EPC reading then this is the last step otherwise see 3) 307 | * 3) send REQ_RN and transition tag into OPEN/SECURED state (depending on access passwd setting) 308 | * 4) delegate to next command (READ, WRITE, LOCK, ACCESS etc.) 309 | */ 310 | case R_QUERY: 311 | if (counter < repetitions) 312 | { 313 | reader_state = R_START; 314 | } 315 | else 316 | { 317 | reader_state = R_POWERDOWN; 318 | } 319 | // 1) SEND QUERY REQUEST 320 | send_default_query(); 321 | // READ TAG RESPONSE / RN16 322 | if (read_RN16(RN16_Bits)) 323 | { 324 | LED_BLUE_ON; 325 | // 2) SEND ACK 326 | send_ack(RN16_Bits); 327 | // READ TAG RESPONSE / EPC DATA 328 | if (read_epc(&tag_data)) 329 | { 330 | debug("[ACKNOWLEDGED]"); 331 | epc_found = true; 332 | LED_RED_ON; 333 | //debug("[+] EPC Found!"); 334 | 335 | if ( !read_flag && !write_flag && !lock_flag ){ 336 | counter = repetitions; 337 | } 338 | else 339 | { 340 | // 3) SEND REQ_RN 341 | send_req_rn(RN16_Bits); 342 | // READ HANDLE 343 | if (read_Handle(HANDLE_Bits)) 344 | { 345 | // TAG SHOULD NOW BE either in OPEN or SECURED STATE 346 | debug("[OPEN/SECURED]"); 347 | /* add some CW to ensure tag stays powered up */ 348 | TX_UNIT.SendCW(32); 349 | 350 | // 4) DELEGATE TO NEXT COMMAND 351 | if (access_flag) 352 | { 353 | debug("SWITCH TO ACCESS"); 354 | reader_state = R_ACCESS; 355 | counter = 0; 356 | } 357 | else if (read_flag) 358 | { 359 | reader_state = R_READ; 360 | counter = 0; 361 | } 362 | else if(write_flag) 363 | { 364 | //debug("[WRITE] *"); 365 | reader_state = R_WRITE; 366 | counter = 0; 367 | } 368 | else if(lock_flag) 369 | { 370 | reader_state = R_LOCK; 371 | counter = 0; 372 | } 373 | else 374 | { 375 | reader_state = R_POWERDOWN; 376 | counter = repetitions; 377 | } 378 | } 379 | else 380 | { 381 | counter++; 382 | leds[0] = CRGB::Orange; 383 | leds[1] = CRGB::Orange; 384 | FastLED.show(); 385 | LED_BLUE_OFF; 386 | LED_RED_OFF; 387 | } 388 | } 389 | } 390 | else 391 | { 392 | counter++; 393 | leds[0] = CRGB::Magenta; 394 | leds[1] = CRGB::Magenta; 395 | FastLED.show(); 396 | LED_BLUE_OFF; 397 | LED_RED_OFF; 398 | } 399 | } 400 | else 401 | { 402 | counter++; 403 | leds[0] = CRGB::Magenta; 404 | leds[1] = CRGB::Magenta; 405 | FastLED.show(); 406 | LED_BLUE_OFF; 407 | LED_RED_OFF; 408 | } 409 | 410 | break; 411 | 412 | case R_ACCESS: 413 | /* access sequence 414 | * 1) send req_rn / recv new RN16 415 | * 2) send access_cmd with [password (lower word) xor RN16] / check crc16 of response 416 | * 3) send req_rn / recv new RN16 417 | * 4) send access_cmd with upper word / check crc16 response 418 | * 5) if all good tag goes into secured state / if not goes back to arbitrate state (full query sequence needs to be repeated) 419 | */ 420 | if (counter < repetitions) 421 | { 422 | reader_state = R_QUERY; 423 | // in case of errors restart full query sequence 424 | } 425 | else 426 | { 427 | debug("[ACCESS] FAILED!"); 428 | reader_state = R_POWERDOWN; 429 | } 430 | 431 | /* initiate first access sequence with req_rn(handle) */ 432 | send_req_rn(HANDLE_Bits); 433 | if(read_Handle(RN16_Bits)) 434 | { 435 | //debug("[ACCESS 1] *"); 436 | send_access(lpass, HANDLE_Bits, RN16_Bits); 437 | if (search_access_ack()) 438 | { 439 | debug("[ACCESS 1] OK"); 440 | /* send second access sequence */ 441 | send_req_rn(HANDLE_Bits); 442 | if(read_Handle(RN16_Bits)) 443 | { 444 | //debug("[ACCESS 2] *"); 445 | send_access(hpass, HANDLE_Bits, RN16_Bits); 446 | if (search_access_ack()) 447 | debug("[SECURED]"); 448 | //TX_UNIT.SendCW(32); 449 | if (read_flag) 450 | { 451 | reader_state = R_READ; 452 | counter = 0; 453 | } 454 | else if (write_flag) 455 | { 456 | reader_state = R_WRITE; 457 | counter = 0; 458 | } 459 | else if (lock_flag) 460 | { 461 | reader_state = R_LOCK; 462 | counter = 0; 463 | } 464 | else 465 | { 466 | reader_state = R_POWERDOWN; 467 | counter = repetitions; 468 | } 469 | } 470 | else 471 | { 472 | counter++; 473 | LED_BLUE_OFF; 474 | LED_RED_OFF; 475 | break; 476 | } 477 | } 478 | else 479 | { 480 | counter++; 481 | LED_BLUE_OFF; 482 | LED_RED_OFF; 483 | break; 484 | } 485 | } 486 | else 487 | { 488 | counter++; 489 | LED_BLUE_OFF; 490 | LED_RED_OFF; 491 | break; 492 | } 493 | break; 494 | 495 | case R_WRITE: 496 | /* ***** WRITE DATA TO TAG ***** */ 497 | if (counter < repetitions) 498 | { 499 | reader_state = R_WRITE; 500 | } 501 | else 502 | { 503 | reader_state = R_POWERDOWN; 504 | break; 505 | } 506 | debug("[WRITE] *"); 507 | debug(String(wordcount)); 508 | debug(String(nWords)); 509 | debug(String(counter)); 510 | while ((wordcount < (nWords)) && (counter < repetitions)) 511 | { 512 | debug("[REQ_RN] *"); 513 | send_req_rn(HANDLE_Bits); 514 | if(read_Handle(RN16_Bits)) 515 | { 516 | debug("[REQ_RN(Handle)]"); 517 | dataword = data[(wordcount*2)+1] << 8 | data[(wordcount*2)]; 518 | send_write(memoryblock, blockaddr+wordcount, dataword, HANDLE_Bits, RN16_Bits); 519 | if (search_write_ack()) 520 | { 521 | TX_UNIT.SendCW(16); 522 | wordcount++; 523 | } 524 | 525 | } 526 | counter++; 527 | leds[0] = CRGB::Magenta; 528 | leds[1] = CRGB::Magenta; 529 | FastLED.show(); 530 | LED_BLUE_OFF; 531 | LED_RED_OFF; 532 | } 533 | counter = repetitions; 534 | break; 535 | 536 | case R_READ: 537 | TX_UNIT.SendCW(16); 538 | if (counter < repetitions) 539 | { 540 | reader_state = R_READ; 541 | } 542 | else 543 | { 544 | reader_state = R_POWERDOWN; 545 | } 546 | for (int i = 0; i < sizeof(data); i++) data[i]=0; 547 | /* **** READ DATA FROM TAG ***** */ 548 | //debug("[READ] *"); 549 | //debug(String(memoryblock)); 550 | //debug(String(blockaddr)); 551 | 552 | //debug(String(nWords)); 553 | send_read(memoryblock,blockaddr,nWords,HANDLE_Bits); 554 | if (read_data(data, nWords)) //add 4 bytes for crc16 and handle + 1 byte because of the 0 header 555 | { 556 | Serial.println("#READDATA"); 557 | Serial.write(data,nWords*2); 558 | reader_state = R_POWERDOWN; 559 | counter = repetitions; 560 | } 561 | else 562 | { 563 | counter++; 564 | } 565 | break; 566 | 567 | 568 | case R_LOCK: 569 | /* ******** LOCK CMD ******** */ 570 | if (counter < repetitions) 571 | { 572 | reader_state = R_LOCK; 573 | } 574 | else 575 | { 576 | reader_state = R_POWERDOWN; 577 | break; 578 | } 579 | while (counter < repetitions) 580 | { 581 | send_lock(mask_bits, action_bits, HANDLE_Bits); 582 | //debug("[+] R_LOCK"); 583 | if (search_lock_ack()) 584 | { 585 | counter = repetitions; 586 | break; 587 | } 588 | counter++; 589 | LED_BLUE_OFF; 590 | LED_RED_OFF; 591 | TX_UNIT.SendCW(16); 592 | break; 593 | } 594 | break; 595 | 596 | 597 | case R_QUERY_BAK: 598 | send_default_query(); 599 | if (read_RN16(RN16_Bits)) 600 | { 601 | LED_BLUE_ON; 602 | /* ***************** SEND ACK **************** */ 603 | send_ack(RN16_Bits); 604 | /* ******** CAPTURE TAGS EPC RESPONSE ******** */ 605 | if(read_epc(&tag_data)) 606 | { 607 | LED_RED_ON; 608 | /* ******** START ACCESS SEQUENCE ************ */ 609 | send_req_rn(RN16_Bits); 610 | if (read_Handle(HANDLE_Bits)) 611 | { 612 | TX_UNIT.SendCW(50); 613 | byte memoryblock = 3; 614 | /* ***** WRITE DATA TO TAG ***** */ 615 | send_req_rn(HANDLE_Bits); 616 | if(read_Handle(RN16_Bits)){ 617 | send_write(memoryblock, 0, 0xFECA, HANDLE_Bits, RN16_Bits); 618 | if (search_write_ack()) 619 | { 620 | leds[0] = CRGB::Blue; 621 | leds[1] = CRGB::Green; 622 | FastLED.show(); 623 | } 624 | } 625 | /* **** READ DATA FROM TAG ***** */ 626 | for (byte i = 0; i < 8;i++) 627 | { 628 | words = 1; 629 | send_read(memoryblock,i,words,HANDLE_Bits); 630 | byte data[64]; 631 | byte data_size = words * 2; 632 | read_data(data, words); //add 4 bytes for crc16 and handle + 1 byte because of the 0 header 633 | } 634 | send_ack(HANDLE_Bits); 635 | } 636 | } 637 | //TX_UNIT.SendCW(50); 638 | } 639 | reader_state = R_POWERDOWN; 640 | break; 641 | 642 | 643 | case R_POWERDOWN: 644 | if (epc_found){ 645 | Serial.println("#TAGDATA"); 646 | Serial.write(tag_data.stored_pc,2); 647 | Serial.write(tag_data.epc,12); 648 | Serial.write(tag_data.crc16,2); 649 | } 650 | /* GRACEFULLY TERMINATE TX UNIT */ 651 | /* wait till fifo is empty before going into standby */ 652 | while ((TX_UNIT.SpiReadStatus(CC1101_TXBYTES) & 0x7f) > 0); 653 | /* Terminate TX Session - Go Into Standby */ 654 | TX_UNIT.SpiStrobe(CC1101_SIDLE); 655 | RX_UNIT.SpiStrobe(CC1101_SIDLE); 656 | reader_state = R_WAIT; 657 | LED_BLUE_OFF; 658 | LED_RED_ON; 659 | LED_GREEN_OFF; 660 | leds[0] = CRGB::Magenta; 661 | leds[1] = CRGB::Magenta; 662 | FastLED.show(); 663 | Serial.println("**** END RUN ****"); 664 | Serial.println(n_tag_responses); 665 | Serial.println("#END"); 666 | break; 667 | 668 | case R_WAIT: 669 | digitalWrite(LED_RED, HIGH); 670 | //runMenu(); 671 | cmd_string = Serial.readStringUntil('#'); 672 | //debug(cmd_string); 673 | if (cmd_string.equals("RUN")) 674 | { 675 | debug("Running ..."); 676 | reader_state = R_INIT; 677 | //delay(500); 678 | } 679 | else if (cmd_string.equals("REP")) 680 | { 681 | cmd_string = Serial.readStringUntil('#'); 682 | repetitions = cmd_string.toInt(); 683 | } 684 | else if (cmd_string.equals("TXP")) 685 | { 686 | cmd_string = Serial.readStringUntil('#'); 687 | tx_power = (byte) cmd_string.toInt(); 688 | } 689 | // Reading data from memory blocks 690 | else if (cmd_string.equals("READ")) 691 | { 692 | //Serial.println("#READ#"); 693 | read_flag = true; 694 | write_flag = false; 695 | lock_flag = false; 696 | block_flag = false; 697 | access_flag = false; 698 | cmd_string = Serial.readStringUntil('#'); 699 | memoryblock = (byte) cmd_string.toInt(); 700 | cmd_string = Serial.readStringUntil('#'); 701 | blockaddr = (byte) cmd_string.toInt(); 702 | cmd_string = Serial.readStringUntil('#'); 703 | nWords = (byte) cmd_string.toInt(); 704 | debug("[+] Read Mode"); 705 | } 706 | // Writing data to tag 707 | else if (cmd_string.equals("WRITE")) 708 | { 709 | write_flag = true; 710 | block_flag = false; 711 | monza_flag =false; 712 | lock_flag = false; 713 | access_flag = false; 714 | 715 | cmd_string = Serial.readStringUntil('#'); 716 | //Serial.println(cmd_string); 717 | memoryblock = (byte) cmd_string.toInt(); 718 | cmd_string = Serial.readStringUntil('#'); 719 | //Serial.println(cmd_string); 720 | blockaddr = (byte) cmd_string.toInt(); 721 | cmd_string = Serial.readStringUntil('#'); 722 | //Serial.println(cmd_string); 723 | nWords = (byte) cmd_string.toInt(); 724 | for (int i = 0; i < sizeof(data); i++) data[i] = 0; 725 | Serial.readBytes(data,nWords * 2); 726 | //dataword = data[1] << 8 | data[0]; 727 | debug("[+] WRITE MODE"); 728 | } 729 | /* Section for processing of access sequence */ 730 | else if (cmd_string.equals("READACCESS")) 731 | { 732 | access_flag = true; 733 | read_flag = true; 734 | write_flag = false; 735 | lock_flag = false; 736 | /* Read in passwd as lower and upper word */ 737 | byte tmp[2]; 738 | Serial.readBytes(tmp,2); 739 | debug(tmp,2); 740 | lpass = (tmp[1] << 8) | tmp[0]; 741 | cmd_string = Serial.readStringUntil('#'); 742 | Serial.readBytes(tmp,2); 743 | debug(tmp,2); 744 | hpass = (tmp[1] << 8) | tmp[0]; 745 | cmd_string = Serial.readStringUntil('#'); 746 | 747 | /* read in standart parameters */ 748 | cmd_string = Serial.readStringUntil('#'); 749 | memoryblock = (byte) cmd_string.toInt(); 750 | cmd_string = Serial.readStringUntil('#'); 751 | //Serial.println(cmd_string); 752 | blockaddr = (byte) cmd_string.toInt(); 753 | //debug(String(blockaddr)); 754 | cmd_string = Serial.readStringUntil('#'); 755 | //Serial.println(cmd_string); 756 | nWords = (byte) cmd_string.toInt(); 757 | //debug(String(nWords)); 758 | debug("[READACCESS]"); 759 | } 760 | else if (cmd_string.equals("WRITEACCESS")) 761 | { 762 | access_flag = true; 763 | read_flag = false; 764 | write_flag = true; 765 | lock_flag = false; 766 | byte tmp[2]; 767 | Serial.readBytes(tmp,2); 768 | debug(tmp,2); 769 | lpass = (tmp[1] << 8) | tmp[0]; 770 | cmd_string = Serial.readStringUntil('#'); 771 | Serial.readBytes(tmp,2); 772 | debug(tmp,2); 773 | hpass = (tmp[1] << 8) | tmp[0]; 774 | cmd_string = Serial.readStringUntil('#'); 775 | 776 | cmd_string = Serial.readStringUntil('#'); 777 | memoryblock = (byte) cmd_string.toInt(); 778 | cmd_string = Serial.readStringUntil('#'); 779 | //Serial.println(cmd_string); 780 | blockaddr = (byte) cmd_string.toInt(); 781 | cmd_string = Serial.readStringUntil('#'); 782 | //Serial.println(cmd_string); 783 | nWords = (byte) cmd_string.toInt(); 784 | for (int i = 0; i < sizeof(data); i++) data[i] = 0; 785 | Serial.readBytes(data,nWords * 2); 786 | } 787 | 788 | // Section if only basic EPC read should be performed 789 | else if (cmd_string.equals("EPC")) 790 | { 791 | Serial.println("#READ_EPC"); 792 | write_flag = false; 793 | read_flag = false; 794 | block_flag = false; 795 | access_flag = false; 796 | debug("[+] Read EPC"); 797 | } 798 | else if (cmd_string.equals("LOCK")) 799 | { 800 | write_flag = false; 801 | read_flag = false; 802 | lock_flag = true; 803 | access_flag = false; 804 | 805 | 806 | // Read in the 20 mask/action bits 807 | Serial.readBytes(mask_bits,10); 808 | Serial.readBytes(action_bits,10); 809 | 810 | debug("[+] LOCK MODE"); 811 | } 812 | else if (cmd_string.equals("LOCKACCESS")) 813 | { 814 | access_flag = true; 815 | write_flag = false; 816 | read_flag = false; 817 | lock_flag = true; 818 | 819 | byte tmp[2]; 820 | Serial.readBytes(tmp,2); 821 | debug(tmp,2); 822 | lpass = (tmp[1] << 8) | tmp[0]; 823 | cmd_string = Serial.readStringUntil('#'); 824 | Serial.readBytes(tmp,2); 825 | debug(tmp,2); 826 | hpass = (tmp[1] << 8) | tmp[0]; 827 | cmd_string = Serial.readStringUntil('#'); 828 | 829 | // Read in the 20 mask/action bits 830 | Serial.readBytes(mask_bits,10); 831 | Serial.readBytes(action_bits,10); 832 | 833 | debug("[LOCKACCESS]"); 834 | } 835 | else 836 | { 837 | reader_state = R_WAIT; 838 | //flushSerial(); 839 | delay(100); 840 | } 841 | cmd_string = ""; 842 | break; 843 | 844 | default: 845 | reader_state = R_WAIT; 846 | break; 847 | } 848 | } 849 | 850 | void logo() 851 | { 852 | Serial.println(); 853 | Serial.println(); 854 | Serial.write(" @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@\n @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@\n @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@!\n !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!!\n :.::.: : :.:: : : : : : : : : : : :: : : \n"); 855 | Serial.println(); 856 | Serial.println(); 857 | } 858 | 859 | void flushSerial() 860 | { 861 | while (Serial.available()) 862 | { 863 | byte dummy = Serial.read(); 864 | } 865 | } 866 | -------------------------------------------------------------------------------- /cli/zuhf-cli.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import sys 4 | import argparse 5 | import random 6 | import string 7 | import serial 8 | import time 9 | import os 10 | from colorama import Fore, Back, Style 11 | 12 | 13 | def main(): 14 | logo() 15 | test = False 16 | args = process_args(argparse.ArgumentParser()) 17 | show_settings(args) 18 | #request, targetfilename = init(args) 19 | #check_response(request, args.basepath, targetfilename) 20 | input('\n') 21 | try: 22 | ser = serial.Serial(baudrate = args.baudrate, 23 | port = args.port, 24 | timeout = args.timeout) 25 | except: 26 | print(F"[ERROR] Could not connect serial at {args.port}. Is device plugged in? Did you select the correct port?") 27 | #print(serial.tools.list_ports()) 28 | sys.exit(1) 29 | 30 | time.sleep(2) 31 | 32 | ser.write(F'REP#{args.repetitions}#'.encode('latin-1')) 33 | ser.write(F'TXP#{args.tx_power}#'.encode('latin-1')) 34 | 35 | 36 | ''' 37 | About Access: 38 | If a access password is provided it will trigger the access sequence before 39 | reading or writing data from/to tag. 40 | An access password which is "00000000" is for the tag effectively as if no 41 | password has been set. 42 | The tag will respond to an req_rn request and transission into "secured" state 43 | if no password is set or into "open" state if password has been set. In the later 44 | case the access sequence needs to be performed to transission the tag to the 45 | "secured" state. 46 | ''' 47 | if (args.access_pwd != "00000000"): 48 | lpass = args.access_pwd[:4] 49 | hpass = args.access_pwd[4:] 50 | print(lpass, hpass) 51 | if (args.write_flag): 52 | ser.write(b"WRITEACCESS#" + \ 53 | bytes.fromhex(lpass) + \ 54 | b"#" + bytes.fromhex(hpass) + b"#" + \ 55 | F"{args.mem_block}#{args.block_addr}#{args.n_words}#".encode('latin-1') + \ 56 | bytes.fromhex(args.data) + b"#") 57 | if (args.read_flag): 58 | ser.write(b"READACCESS#" + \ 59 | bytes.fromhex(lpass) + b"#" + \ 60 | bytes.fromhex(hpass) + b"#" + \ 61 | F"{args.mem_block}#{args.block_addr}#{args.n_words}#".encode('latin-1') + b"#") 62 | if (args.lock_flag): 63 | print(Fore.CYAN + "### LOCKACCESS ###") 64 | mask_bytes = bytes([int(x) for x in args.lock_mask]) 65 | action_bytes = bytes([int(x) for x in args.lock_action]) 66 | ser.write(b"LOCKACCESS#" + \ 67 | bytes.fromhex(lpass) + b"#" + \ 68 | bytes.fromhex(hpass) + b"#" + \ 69 | mask_bytes + action_bytes + b"#") 70 | 71 | elif (args.write_flag): 72 | #ser.write(F'WRITE#'.encode('latin-1')) 73 | ser.write(b"WRITE#" + F"{args.mem_block}#{args.block_addr}#{args.n_words}#".encode('latin-1') + bytes.fromhex(args.data) + b"#") 74 | print(bytes.fromhex(args.data)) 75 | #ser.write(bytes.fromhex(args.data)+b"#") 76 | 77 | elif (args.read_flag): 78 | #ser.write(F'READ#'.encode('latin-1')) 79 | ser.write(b"READ#"+F"{args.mem_block}#{args.block_addr}#{args.n_words}#".encode('latin-1')) 80 | elif (args.lock_flag): 81 | mask_bytes = bytes([int(x) for x in args.lock_mask]) 82 | action_bytes = bytes([int(x) for x in args.lock_action]) 83 | ser.write(b"LOCK#" + mask_bytes + action_bytes + b"#") 84 | #ser.write(F'LOCK#'.encode('latin-1')) 85 | #ser.write(mask_bytes) 86 | #ser.write(action_bytes) 87 | #ser.write(b"#") 88 | elif (args.monza): 89 | ser.write(b"MONZA#") 90 | 91 | else: 92 | ser.write(b"READ_EPC#") 93 | 94 | # RUN SCAN # 95 | ser.write(b'DUMMY#') # terminate 96 | ser.write(b'RUN#') 97 | line = b"" 98 | buffer = b"" 99 | while line != b"#END": 100 | line = ser.readline().rstrip() 101 | if (line == b"#READDATA"): 102 | print(F"[MEM BLOCK {args.mem_block}]") 103 | print("#"*15) 104 | buffer = ser.read(args.n_words * 2) 105 | for i in range(args.n_words): 106 | print(Fore.CYAN + F"{i+args.block_addr:#08x}: "+" ".join(format(x, "02x") for x in buffer[(i*2):(i*2)+2])) 107 | print(Fore.WHITE + "#"*15) 108 | elif (line == b"#TAGDATA"): 109 | print(Fore.GREEN + "[TAG-DATA]") 110 | buffer = ser.read(16) 111 | print("#"*62) 112 | print(F"Stored PC: ".ljust(14) + F"{' '.join(format(x,'#02x') for x in buffer[:2])}") 113 | epc = ''.join('{0:0{1}X}'.format(x,2) for x in buffer[2:14]) 114 | print(F"EPC: ".ljust(14) + F"{epc}") 115 | print(F"CRC16: ".ljust(14) + F"{' '.join(format(x,'#02x') for x in buffer[14:])}") 116 | print("#"*62 + Fore.WHITE) 117 | 118 | elif (line == b"WRITE#OK#"): 119 | print(Fore.GREEN + F"[+] WRITE COMPLETE" + Fore.WHITE) 120 | elif (line == b"#DEBUG"): 121 | line = ser.readline().rstrip() 122 | print(Fore.BLUE + F"{line.decode('latin-1')}"+ Fore.WHITE) 123 | elif (line == b"#ERROR"): 124 | line = ser.readline().rstrip() 125 | print(Fore.RED + F"{line.decode('latin-1')}" + Fore.WHITE) 126 | 127 | else: 128 | pass 129 | #print(line.decode('latin-1').ljust(80,' '),end='\r',flush=True) 130 | 131 | if ser.isOpen(): 132 | ser.close() 133 | 134 | 135 | def update_parameters(ser, args): 136 | 137 | time.sleep(1) 138 | 139 | 140 | # process arguments 141 | def process_args(parser): 142 | parser.add_argument('-p', dest='port', help='serial port e.g. COM7,COM14', default='COM14') 143 | parser.add_argument('-b', dest='baudrate', type=int, help='baudrate of serial', default=250000) 144 | parser.add_argument('-tp', dest='tx_power', choices=[0x11,0x12,0x13,0x14,0x15,0x16],type=int,help='tx_power - index', default=0x16) 145 | parser.add_argument('-t', dest='timeout', help='serial read timeout', type=int, default=3) 146 | parser.add_argument('-r', dest='repetitions', help='repetitions to run before terminating',type=int, default=1000) 147 | 148 | # arguments used in combination with actions read/write (-data is only relevant for the write action) 149 | parser.add_argument('-block', dest = 'block_addr', help='blockaddress to read/write from', type=int ,default=0) 150 | parser.add_argument('-mem', dest = 'mem_block', help='memory block to read / write from e.g. user block',choices=[0,1,2,3], type=int, default = 3) 151 | parser.add_argument('-n',dest = 'n_words', help='read n words from mem block, max is 32 words',type=int, default = 1) 152 | parser.add_argument('-data', dest = 'data', help='word data to write to mem block', default="1337") 153 | 154 | # arguments for -lock action 155 | parser.add_argument('-mask', dest = 'lock_mask', default='0000000000') 156 | parser.add_argument('-action', dest = 'lock_action', default = '0000000000') 157 | 158 | # access / authentication actions 159 | parser.add_argument('-access', dest = 'access_pwd', default='00000000') 160 | 161 | # available actions: write,read,lock (mutually exclusive) 162 | action = parser.add_mutually_exclusive_group() 163 | action.add_argument('-write', dest = 'write_flag', action='store_true') 164 | action.add_argument('-lock', dest = 'lock_flag', action='store_true') 165 | action.add_argument('-read', dest = 'read_flag', action='store_true') 166 | action.add_argument('-monza', dest = 'monza', action='store_true') 167 | 168 | #parser.print_help() 169 | args = parser.parse_args() 170 | 171 | if args.write_flag: 172 | if args.data == None: 173 | print("[ERROR] You need to provide a data word e.g. \"-data \'CAFE\'\" for writing") 174 | sys.exit(0) 175 | # safety check ; truncate data to align with word length and set n appropriately 176 | args.data = args.data[:len(args.data)-len(args.data)%4] 177 | args.n_words = len(args.data)//4 178 | print(args.data, args.n_words) 179 | return args 180 | 181 | def show_settings(args): 182 | print('[info] Using following settings:') 183 | print('-'*37) 184 | for k,v in args.__dict__.items(): 185 | print(F'{str(k).ljust(15)}: {str(v).rjust(20)}') 186 | print('-'*37) 187 | 188 | 189 | def logo(): 190 | print(); 191 | print(" @@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@@@@@\n @@! @@! @@@ @@! @@@ @@! @@! @@@ @@! @@! @@! @@@\n @!! @!@ !@! @!@!@!@! @!!!:! @!@!!@! @!!!:! !!@ @!@ !@!\n !!: !!: !!! !!: !!! !!: !!: :!! !!: !!: !!: !!!\n :.::.: : :.:: : : : : : : : : : : :: : : \n"); 192 | print(); 193 | 194 | if __name__ == '__main__': 195 | main() 196 | 197 | 198 | -------------------------------------------------------------------------------- /images/ASK-Modulation-RF-envelope.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/ASK-Modulation-RF-envelope.png -------------------------------------------------------------------------------- /images/PIE-Symbols.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/PIE-Symbols.png -------------------------------------------------------------------------------- /images/cc1101_868MHz_module.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/cc1101_868MHz_module.jpg -------------------------------------------------------------------------------- /images/connections_tx_image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/connections_tx_image.jpg -------------------------------------------------------------------------------- /images/cthuloid_pcb.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/cthuloid_pcb.jpg -------------------------------------------------------------------------------- /images/gnuradio_setup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/gnuradio_setup.png -------------------------------------------------------------------------------- /images/holistical_pcb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/holistical_pcb.png -------------------------------------------------------------------------------- /images/improvisedSetup.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/improvisedSetup.jpg -------------------------------------------------------------------------------- /images/lock_cmd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/lock_cmd.png -------------------------------------------------------------------------------- /images/powerdownup-waveform-parameters.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/powerdownup-waveform-parameters.png -------------------------------------------------------------------------------- /images/query-signal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/query-signal.jpg -------------------------------------------------------------------------------- /images/query_queryrep-sequence.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/query_queryrep-sequence.jpg -------------------------------------------------------------------------------- /images/rfid-setup.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/rfid-setup.jpg -------------------------------------------------------------------------------- /images/rn16_signal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/rn16_signal.png -------------------------------------------------------------------------------- /images/signal from chafon reader.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/signal from chafon reader.png -------------------------------------------------------------------------------- /images/successfull_tag_epc_signal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/successfull_tag_epc_signal.jpg -------------------------------------------------------------------------------- /images/tag-RN16-response.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/tag-RN16-response.jpg -------------------------------------------------------------------------------- /images/tagRN16-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/tagRN16-example.png -------------------------------------------------------------------------------- /images/tag_data_epc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/tag_data_epc.jpg -------------------------------------------------------------------------------- /images/tears_001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/tears_001.png -------------------------------------------------------------------------------- /images/tears_002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/tears_002.png -------------------------------------------------------------------------------- /images/tx-connection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/tx-connection.png -------------------------------------------------------------------------------- /images/uhf-rfid_part1_signal01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/uhf-rfid_part1_signal01.png -------------------------------------------------------------------------------- /images/uhf-rfid_part1_signal02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/uhf-rfid_part1_signal02.png -------------------------------------------------------------------------------- /images/uhf-rfid_part1_signal03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/uhf-rfid_part1_signal03.png -------------------------------------------------------------------------------- /images/uhf-rfid_part1_signal04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/uhf-rfid_part1_signal04.png -------------------------------------------------------------------------------- /images/uhfread3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/uhfread3.png -------------------------------------------------------------------------------- /images/uhfreader1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/uhfreader1.png -------------------------------------------------------------------------------- /images/uhfreader2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/uhfreader2.png -------------------------------------------------------------------------------- /images/uhfreader3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/images/uhfreader3.png -------------------------------------------------------------------------------- /libraries/SPI_UART_CC1101.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/libraries/SPI_UART_CC1101.zip -------------------------------------------------------------------------------- /libraries/SPI_UART_CC1101/SPI_UART_CC1101.cpp: -------------------------------------------------------------------------------- 1 | /* SPI_UART_CC1101.cpp 2 | * The Code Programs the Arduino Due to use the Serial2 USART as SPI 3 | * From https://forum.arduino.cc/index.php?topic=283766.0 I used some 4 | * of the code snippets for that. 5 | * The Library is build to communicate with a CC1101 and not an all purpose 6 | * SPI_UART Library. But feel free to copy and use the code as needed. 7 | * The Library makes the following assumptions 8 | * CC1101 is used as TX Module (w/ Arduino Due Atmel SAM3X8E) 9 | * --> CSS Pin is called TX_PIN 10 | * --> Clock Divider 11 | * --> MOSI : TX2 (A.12) 12 | * --> MISO : RX2 (A.13) 13 | * --> SCK : SCK1/A0 (A.16) 14 | * --> CSS : PIN 23 (A.14) 15 | */ 16 | 17 | #ifndef SPI_UART_CC1101_cpp 18 | #define SPI_UART_CC1101_cpp 19 | 20 | 21 | #include 22 | #include 23 | 24 | /****************************************************************/ 25 | #define WRITE_BURST 0x40 //write burst 26 | #define READ_SINGLE 0x80 //read single 27 | #define READ_BURST 0xC0 //read burst 28 | #define BYTES_IN_RXFIFO 0x7F //byte number in RXfifo 29 | #define BYTES_IN_TXFIFO 0x7F //byte number in TXfifo 30 | /****************************************************************/ 31 | 32 | 33 | 34 | /**************************************************************** 35 | * FUNCTION NAME: Init 36 | * FUNCTION : configure USASRT1 for usage as SPI and Initialize CC1101 37 | * INPUT : None 38 | * OUTPUT : version of the CC1101 for verification 39 | ****************************************************************/ 40 | byte SPI_UART_CC1101::Init(void) 41 | { 42 | for (int i = 0; i < 64; i++){ 43 | CW[i] = 0xff; 44 | } 45 | 46 | pmc_enable_periph_clk(ID_USART1); 47 | USART1->US_WPMR = 0x55534100; // Unlock the USART Mode register 48 | USART1->US_CR = US_CR_RSTRX | US_CR_RSTTX; 49 | PIOA->PIO_WPMR = 0x50494F00; // Unlock PIOA Write Protect Mode Register 50 | PIOB->PIO_WPMR = 0x50494F00; // Unlock PIOB Write Protect Mode Register 51 | PIOA->PIO_ABSR |= (0u << 14); // CS: Assign A14 I/O to the Peripheral A function 52 | PIOA->PIO_PDR |= (1u << 14); // CS: Disable PIO control, enable peripheral control 53 | PIOA->PIO_ABSR |= (0u << 16); // SCK: Assign A16 I/O to the Peripheral A function 54 | PIOA->PIO_PDR |= (1u << 16); // SCK: Disable PIO control, enable peripheral control 55 | PIOA->PIO_ABSR |= (0u << 13); // MOSI: Assign PA13 I/O to the Peripheral A function 56 | PIOA->PIO_PDR |= (1u << 13); // MOSI: Disable PIO control, enable peripheral control 57 | PIOA->PIO_ABSR |= (0u << 12); // MISO: Assign A12 I/O to the Peripheral A function 58 | PIOA->PIO_PDR |= (1u << 12); // MISO: Disable PIO control, enable peripheral control 59 | 60 | USART1->US_MR = 0x409CE; // MODE0, 8_BIT, SPI_MODE 61 | USART1->US_BRGR = 21; 62 | 63 | USART1->US_CR = US_CR_RXEN; 64 | USART1->US_CR = US_CR_TXEN; 65 | 66 | // SET GDO PIN MODES 67 | pinMode(TX_GDO0_PIN, INPUT); 68 | pinMode(TX_GDO2_PIN, INPUT); 69 | //pinMode(TX_SS_PIN, OUTPUT); 70 | // RESET CC1101 71 | Serial.println("[TX] CC1101 RESET *"); 72 | digitalWrite(TX_SS_PIN, LOW); // CSS LOW 73 | delay(1); 74 | digitalWrite(TX_SS_PIN, HIGH); // CSS HIGH 75 | delay(1); 76 | digitalWrite(TX_SS_PIN, LOW); // CSS LOW 77 | while (((USART1->US_CSR) & 0x01)); // WAIT FOR MISO LOW 78 | SpiStrobe(CC1101_SRES); 79 | while (((USART1->US_CSR) & 0x01)); // WAIT FOR MISO LOW 80 | /* !!! DELAY NECESSARY TO COMPLETE RESET !!! */ 81 | delay(1); 82 | Serial.println("[TX] CC1101 RESET OK"); 83 | /* *** RESET COMPLETE *** */ 84 | 85 | // SET CONFIGURATION REGISTERS CC1101 86 | RegConfigSettings(); 87 | SpiWriteBurstReg(CC1101_PATABLE,TXPaTable,8); 88 | SpiStrobe(CC1101_SCAL); 89 | 90 | // print out basic cc1101 info: version & partnum as a simple check 91 | // partnum should be 0x14 or 0x04; version is usually 0x00 92 | byte xversion = SpiReadStatus(CC1101_VERSION); 93 | byte xpart = SpiReadStatus(CC1101_PARTNUM); 94 | Serial.print("[TX] Version: 0x");Serial.println(xversion, HEX); 95 | Serial.print("[TX] Partnumber: 0x");Serial.println(xpart, HEX); 96 | return xversion; 97 | } 98 | 99 | 100 | /**************************************************************** 101 | * FUNCTION NAME: Transfer 102 | * FUNCTION : Send byte via SPI 103 | * INPUT : b: byte value to send 104 | * OUTPUT : byte value: response from SPI 105 | ****************************************************************/ 106 | byte SPI_UART_CC1101::Transfer(byte b) 107 | { 108 | USART1->US_CR = US_CR_RXEN; 109 | USART1->US_CR = US_CR_TXEN; 110 | // wait for transmitter ready 111 | while (!(USART1->US_CSR & US_CSR_TXRDY)); 112 | USART1->US_THR = b; 113 | // wait for receiver ready 114 | while (!((USART1->US_CSR) & 0x01)); 115 | byte result = USART1->US_RHR; 116 | return result; 117 | } 118 | 119 | 120 | /**************************************************************** 121 | * FUNCTION NAME: RegCmd 122 | * FUNCTION : Send register command to CC1101 123 | * INPUT : cmd: register address, val: value to send with the command 124 | * OUTPUT : byte value: response from the CC1101 125 | ****************************************************************/ 126 | byte SPI_UART_CC1101::RegCmd(byte cmd, byte val) 127 | { 128 | USART1->US_CR = US_CR_RXEN; 129 | USART1->US_CR = US_CR_TXEN; 130 | // MANUALLY KEEP CS LOW UNTIL CMD AND VALUE HAVE BEEN TRANSFERRED 131 | // DATASHEET (pg. 805, 35.7.7.5 Character Transmission) 132 | USART1->US_CR = US_CR_RTSEN; 133 | // WAIT FOR TRANSMITTER TO BE READY 134 | while (!(USART1->US_CSR & US_CSR_TXRDY)); 135 | USART1->US_THR = cmd; 136 | 137 | while (!(USART1->US_CSR & US_CSR_TXRDY)); 138 | USART1->US_THR = val; 139 | 140 | // WAIT FOR ALL DATA TO BE TRANSMITTED 141 | while (!(USART1->US_CSR & US_CSR_TXEMPTY)); 142 | // MANUALLY PULL CS HIGH AGAIN 143 | USART1->US_CR = US_CR_RTSDIS; 144 | 145 | // wait for receiver ready 146 | while (!(USART1->US_CSR & US_CSR_RXRDY)); 147 | byte result = USART1->US_RHR; 148 | return result; 149 | } 150 | 151 | 152 | /**************************************************************** 153 | * FUNCTION NAME: ReadStatus 154 | * FUNCTION : CC1101 read status register 155 | * INPUT : addr: register address 156 | * OUTPUT : status value 157 | ****************************************************************/ 158 | byte SPI_UART_CC1101::SpiReadStatus(byte addr) 159 | { 160 | byte value,temp; 161 | temp = addr | READ_BURST; 162 | value = RegCmd(temp, 0x00); 163 | return value; 164 | } 165 | 166 | 167 | /**************************************************************** 168 | *FUNCTION NAME:SpiWriteReg 169 | *FUNCTION :CC1101 write data to register 170 | *INPUT :addr: register address; value: register value 171 | *OUTPUT :none (return value from RegCmd is not returned) 172 | ****************************************************************/ 173 | void SPI_UART_CC1101::SpiWriteReg(byte addr, byte value) 174 | { 175 | byte dummy = RegCmd(addr, value); 176 | } 177 | 178 | 179 | /**************************************************************** 180 | * FUNCTION NAME: SpiWriteBurstReg 181 | * FUNCTION : CC1101 write burst data to register 182 | * INPUT : addr: register address; buffer:register value array; num:number to write 183 | * OUTPUT : none 184 | ****************************************************************/ 185 | void SPI_UART_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num) 186 | { 187 | USART1->US_CR = US_CR_RXEN; 188 | USART1->US_CR = US_CR_TXEN; 189 | byte i, temp; 190 | temp = addr | WRITE_BURST; 191 | // MANUALLY KEEP CS LOW UNTIL CMD AND VALUE HAVE BEEN TRANSFERRED 192 | // DATASHEET (pg. 805, 35.7.7.5 Character Transmission) 193 | USART1->US_CR = US_CR_RTSEN; 194 | // SEND COMMAND 195 | while (!(USART1->US_CSR & US_CSR_TXRDY)); 196 | USART1->US_THR = temp; 197 | // WAIT FOR TRANSMITTER TO BE READY 198 | for (i = 0; i < num; i++){ 199 | while (!(USART1->US_CSR & US_CSR_TXRDY)); 200 | USART1->US_THR = buffer[i]; 201 | } 202 | // WAIT FOR ALL DATA TO BE TRANSMITTED 203 | while (!(USART1->US_CSR & US_CSR_TXEMPTY)); 204 | // MANUALLY PULL CS HIGH AGAIN 205 | USART1->US_CR = US_CR_RTSDIS; 206 | } 207 | 208 | 209 | /**************************************************************** 210 | * FUNCTION NAME: SpiStrobe 211 | * FUNCTION : CC1101 Strobe 212 | * INPUT : strobe: command; //refer define in CC1101.h// 213 | * OUTPUT : none 214 | ****************************************************************/ 215 | void SPI_UART_CC1101::SpiStrobe(byte strobe) 216 | { 217 | byte dummy = Transfer(strobe); 218 | } 219 | 220 | 221 | /**************************************************************** 222 | * FUNCTION NAME: SpiReadReg 223 | * FUNCTION : CC1101 read data from register 224 | * INPUT : addr: register address 225 | * OUTPUT : register value 226 | ****************************************************************/ 227 | byte SPI_UART_CC1101::SpiReadReg(byte addr) 228 | { 229 | byte temp, value; 230 | temp = addr | READ_SINGLE; 231 | value = Transfer(temp); 232 | return value; 233 | } 234 | 235 | 236 | /**************************************************************** 237 | * FUNCTION NAME: SpiReadBurstReg 238 | * FUNCTION : CC1101 read burst data from register 239 | * INPUT : addr: register address; buffer:array to store register value; num: number to read 240 | * OUTPUT : none 241 | ****************************************************************/ 242 | void SPI_UART_CC1101::SpiReadBurstReg(byte addr, byte *buffer, byte num) 243 | { 244 | USART1->US_CR = US_CR_RXEN; 245 | USART1->US_CR = US_CR_TXEN; 246 | byte temp; 247 | temp = addr | READ_BURST; 248 | // MANUALLY KEEP CS LOW UNTIL CMD AND VALUE HAVE BEEN TRANSFERRED 249 | // DATASHEET (pg. 805, 35.7.7.5 Character Transmission) 250 | USART1->US_CR = US_CR_RTSEN; 251 | // SEND COMMAND 252 | while (!(USART1->US_CSR & US_CSR_TXRDY)); 253 | USART1->US_THR = temp; 254 | 255 | for (byte i = 0; i < num; i++){ 256 | // WAIT FOR ALL DATA TO BE TRANSMITTED 257 | while (!(USART1->US_CSR & US_CSR_TXEMPTY)); 258 | USART1->US_THR = 0x00; 259 | // wait for receiver ready 260 | while (!(USART1->US_CSR & US_CSR_RXRDY)); 261 | buffer[i] = USART1->US_RHR; 262 | } 263 | USART1->US_CR = US_CR_RTSDIS; 264 | } 265 | 266 | 267 | /**************************************************************** 268 | * FUNCTION NAME: RegConfigSettings 269 | * FUNCTION : CC1101 register config //details refer datasheet of CC1101/CC1100// 270 | * INPUT : none 271 | *OUTPUT : none 272 | ****************************************************************/ 273 | void SPI_UART_CC1101::RegConfigSettings(void){ 274 | /* ****************************************************************************** 275 | * custom initial basic register settings for CC1101 276 | * for ASK/OOK Settings also see https://www.ti.com/lit/an/swra215e/swra215e.pdf 277 | * ****************************************************************************** */ 278 | SpiWriteReg(CC1101_MDMCFG4, 0x8B); // 203kHz Filter Bandwidth 279 | //SpiWriteReg(CC1101_MDMCFG4, 0x5B); // 325kHz Filter Bandwidth 280 | SpiWriteReg(CC1101_FSCTRL1, 0x0F); 281 | SpiWriteReg(CC1101_FREND1, 0xB6); 282 | SpiWriteReg(CC1101_TEST2, 0x81); 283 | SpiWriteReg(CC1101_TEST1, 0x35); 284 | SpiWriteReg(CC1101_FIFOTHR, 0x0f); 285 | SpiWriteReg(CC1101_AGCCTRL2, 0x03); 286 | SpiWriteReg(CC1101_AGCCTRL1, 0x00); 287 | SpiWriteReg(CC1101_AGCCTRL0, 0x91); 288 | 289 | SpiWriteReg(CC1101_FREND0, 0x11); 290 | SpiWriteReg(CC1101_PKTCTRL0, 0x02); 291 | SpiWriteReg(CC1101_IOCFG0, 0x02); 292 | SpiWriteReg(CC1101_MDMCFG2, 0x30); // ASK/OOK w/o sync+preamble 293 | 294 | SpiWriteReg(CC1101_SYNC1, 0xAD); 295 | SpiWriteReg(CC1101_SYNC0, 0x23); 296 | SpiWriteReg(CC1101_IOCFG2, 0x03); 297 | SpiWriteReg(CC1101_PKTLEN, 0x0C); 298 | SpiWriteReg(CC1101_PKTCTRL1, 0x00); 299 | SpiWriteReg(CC1101_CHANNR, 0x00); 300 | /************* 868 MHz ************/ 301 | SpiWriteReg(CC1101_FREQ2, 0x21); //Frequency Control Word, High Byte 302 | SpiWriteReg(CC1101_FREQ1, 0x62); //Frequency Control Word, Middle Byte 303 | SpiWriteReg(CC1101_FREQ0, 0x76); //Frequency Control Word, Low Byte 304 | /************* ******* ************/ 305 | SpiWriteReg(CC1101_MDMCFG3, 0x93); //Modem Configuration // DR 80kBaud 306 | SpiWriteReg(CC1101_MDMCFG1, 0x22); //channel spacing 307 | SpiWriteReg(CC1101_MDMCFG0, 0xff); // channel spacing 308 | SpiWriteReg(CC1101_MCSM1, 0x30); // default settings 309 | SpiWriteReg(CC1101_MCSM0, 0x29); //Main Radio Control State Machine Configuration 310 | SpiWriteReg(CC1101_FOCCFG, 0x1D); //Frequency Offset Compensation Configuration 311 | SpiWriteReg(CC1101_BSCFG, 0x1C); //Bit Synchronization Configuration 312 | SpiWriteReg(CC1101_FSCAL3, 0xEA); //Frequency Synthesizer Calibration 313 | SpiWriteReg(CC1101_FSCAL2, 0x2A); //Frequency Synthesizer Calibration 314 | SpiWriteReg(CC1101_FSCAL1, 0x00); //Frequency Synthesizer Calibration 315 | SpiWriteReg(CC1101_FSCAL0, 0x1F); //Frequency Synthesizer Calibration 316 | SpiWriteReg(CC1101_TEST0, 0x09); //Various Test Settings 317 | 318 | SpiStrobe(CC1101_SCAL); 319 | } 320 | 321 | 322 | /**************************************************************** 323 | * FUNCTION NAME: UpdateFifo 324 | * FUNCTION : Write n Bytes to TX Fifo 325 | * INPUT : *data: pointer to a byte buffer; nbytes: total number of bytes to write to TX Fifo 326 | *OUTPUT : none 327 | ****************************************************************/ 328 | /* updated 12.02.2021 8:44 - changed SpiWriteBurstReg to SpiWriteReg for performance */ 329 | void SPI_UART_CC1101::UpdateFifo(byte *data, int nbytes) 330 | { 331 | int index = 0; 332 | while (index < nbytes) 333 | { 334 | /* wait if tx fifo is full */ 335 | while(TX_GDO2_STATE); 336 | SpiWriteReg(CC1101_TXFIFO,data[index]); 337 | index++; 338 | } 339 | } 340 | 341 | /**************************************************************** 342 | * FUNCTION NAME: SendCW 343 | * FUNCTION : send continous high signal 344 | * INPUT : duration: number of bytes to send where 1 byte corresponds 8*12.5µs = 100µs 345 | *OUTPUT : none 346 | ****************************************************************/ 347 | void SPI_UART_CC1101::SendCW(byte duration) 348 | { 349 | for (int i = 0; i < duration; i++) 350 | { 351 | UpdateFifo(CW, 1); 352 | } 353 | } 354 | 355 | /* duration < 64 */ 356 | void SPI_UART_CC1101::SendCWBurst(byte duration) 357 | { 358 | /* limit duration < 64 */ 359 | duration = duration & 0x3f; 360 | SpiWriteBurstReg(CC1101_TXFIFO, CW, duration); 361 | } 362 | 363 | /**************************************************************** 364 | * FUNCTION NAME: SendIdle 365 | * FUNCTION : send continous low signal 366 | * INPUT : duration: number of bytes to send where 1 byte corresponds 8*12.5µs = 100µs 367 | *OUTPUT : none 368 | ****************************************************************/ 369 | void SPI_UART_CC1101::SendIdle(byte duration) 370 | { 371 | for (int i = 0; i < duration; i++) 372 | { 373 | while(digitalRead(TX_GDO2_PIN)); 374 | UpdateFifo(IDLE, 1); 375 | } 376 | } 377 | 378 | /**************************************************************** 379 | * FUNCTION NAME: SendByte 380 | * FUNCTION : send single signal 381 | * INPUT : b: byte signal to send 382 | *OUTPUT : none 383 | ****************************************************************/ 384 | void SPI_UART_CC1101::SendByte(byte b) 385 | { 386 | SpiWriteReg(CC1101_FIFOTHR, b); 387 | } 388 | SPI_UART_CC1101 TX_UNIT; 389 | #endif 390 | -------------------------------------------------------------------------------- /libraries/SPI_UART_CC1101/SPI_UART_CC1101.h: -------------------------------------------------------------------------------- 1 | /* SPI_UART_CC1101.H 2 | * The Code Programs the Arduino Due to use the Serial2 USART as SPI 3 | * From https://forum.arduino.cc/index.php?topic=283766.0 I used some 4 | * of the code snippets for that. 5 | * The Library is build to communicate with a CC1101 and not an all purpose 6 | * SPI_UART Library. But feel free to copy and use the code as needed. 7 | * The Library makes the following assumptions 8 | * CC1101 is used as TX Module (w/ Arduino Due Atmel SAM3X8E) 9 | * --> CSS Pin is called TX_PIN 10 | * --> Clock Divider is set to 42 --> 2MHz (or 21 --> 4MHz) 11 | * --> MOSI : TX2 (A.12) 12 | * --> MISO : RX2 (A.13) 13 | * --> SCK : SCK1/A0 (A.16) 14 | * --> CSS : PIN 23 (A.14) 15 | */ 16 | 17 | #ifndef SPI_UART_CC1101_h 18 | #define SPI_UART_CC1101_h 19 | #include 20 | 21 | /********************** GDO PINS ********************************/ 22 | //setup holistical 23 | #define TX_GDO0_PIN 34 // PORTC2 24 | #define TX_GDO2_PIN 36 // PORTC4 25 | 26 | //setup v1b w/ tft 27 | //#define TX_GDO0_PIN 27 // PORTD2 28 | //#define TX_GDO2_PIN 29 // PORTD6 29 | 30 | #define TX_SS_PIN 23 31 | //setup holistical 32 | #define TX_GDO0_PIO 2 33 | #define TX_GDO2_PIO 4 34 | 35 | //setup holistical 36 | //#define TX_GDO0_PIO 2 37 | //#define TX_GDO2_PIO 6 38 | 39 | //setup holistical 40 | /* fast digital read for GDO pins */ 41 | #define TX_GDO0_STATE (((PIOC->PIO_PDSR) & (1 << TX_GDO0_PIO)) >> TX_GDO0_PIO) 42 | #define TX_GDO2_STATE (((PIOC->PIO_PDSR) & (1 << TX_GDO2_PIO)) >> TX_GDO2_PIO) 43 | 44 | //setup v1b 45 | //#define TX_GDO0_STATE (((PIOD->PIO_PDSR) & (1 << TX_GDO0_PIO)) >> TX_GDO0_PIO) 46 | //#define TX_GDO2_STATE (((PIOD->PIO_PDSR) & (1 << TX_GDO2_PIO)) >> TX_GDO2_PIO) 47 | 48 | /****************************************************************/ 49 | 50 | class SPI_UART_CC1101 { 51 | private: 52 | byte TXPaTable[8] = {0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00}; 53 | byte CW[64]; 54 | byte IDLE[1] = {0x00}; 55 | byte OFF[1] = {0x00}; 56 | 57 | byte Transfer(byte b); 58 | byte RegCmd(byte b, byte val); 59 | byte Transfer(byte *_buf, size_t _count); 60 | 61 | public: 62 | byte Init(void); 63 | byte SpiReadStatus(byte cmd); 64 | void SpiWriteReg(byte addr, byte value); 65 | void SpiWriteBurstReg(byte addr, byte *buffer, byte num); 66 | void SpiStrobe(byte strobe); 67 | byte SpiReadReg(byte addr); 68 | void SpiReadBurstReg(byte addr, byte *buffer, byte num); 69 | 70 | void RegConfigSettings(void); 71 | void UpdateFifo(byte *data, int nbytes); 72 | void SendCW(byte duration); 73 | void SendCWBurst(byte duration); 74 | void SendByte(byte value); 75 | void SendIdle(byte duration); 76 | void SearchRN16(byte *buffer); 77 | }; 78 | 79 | extern SPI_UART_CC1101 TX_UNIT; 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /libraries/SPI_UART_CC1101/SPI_UART_CC1101.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/libraries/SPI_UART_CC1101/SPI_UART_CC1101.zip -------------------------------------------------------------------------------- /libraries/ZUHF_CC1101.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/libraries/ZUHF_CC1101.zip -------------------------------------------------------------------------------- /libraries/ZUHF_CC1101/ZUHF_BUFFER.h: -------------------------------------------------------------------------------- 1 | #ifndef ZUHF_BUFFER_H 2 | #define ZUHF_BUFFER_H 3 | 4 | 5 | 6 | struct DATA_BUFFER{ 7 | byte data[BUFFER_SIZE] = {}; 8 | byte data_size = 0; 9 | }; 10 | 11 | byte copy_to_buffer(DATA_BUFFER *buffer, const byte *data, byte bytes) 12 | { 13 | if (((buffer->data_size) + bytes) < BUFFER_SIZE) 14 | { 15 | memcpy(buffer->data + buffer->data_size, data, bytes); 16 | (buffer->data_size) += bytes; 17 | return buffer->data_size; 18 | }else{ 19 | if (Serial){ 20 | Serial.println("[!] databuffer full! No additional data can be appended!"); 21 | } 22 | return -1; 23 | } 24 | } 25 | 26 | #endif -------------------------------------------------------------------------------- /libraries/ZUHF_CC1101/ZUHF_CC1101.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ZUHF_CC1101_DUE 3 | Basic CC1101 library for Arduino Due. It makes use of the Arduino SPI.h library. 4 | */ 5 | #ifndef ZUHF_CC1101_CPP 6 | #define ZUHF_CC1101_CPP 7 | 8 | #include 9 | #include 10 | //#include 11 | 12 | /**************************************************************** 13 | *FUNCTION NAME:SpiInit 14 | *FUNCTION :spi communication initialization 15 | *INPUT :none 16 | *OUTPUT :none 17 | ****************************************************************/ 18 | void ZUHF_CC1101::SpiInit() 19 | { 20 | SPI.end(); 21 | 22 | pinMode(SCK_PIN, OUTPUT); 23 | pinMode(MOSI_PIN, OUTPUT); 24 | pinMode(MISO_PIN, INPUT); 25 | pinMode(RX_PIN, OUTPUT); // CSN / CSS / SS 26 | 27 | /* ********************* */ 28 | pinMode(4, OUTPUT); 29 | pinMode(10, OUTPUT); 30 | pinMode(52, OUTPUT); 31 | 32 | /* Initialize SPI For TX CC1101 */ 33 | SPI.begin(RX_PIN); 34 | SPI.setDataMode(RX_PIN, SPI_MODE0); 35 | SPI.setClockDivider(RX_PIN, 21); // 4MHz // default is 4MHz higher rates are possible but playing it safe here 36 | SPI.setBitOrder(RX_PIN, MSBFIRST); 37 | } 38 | 39 | /**************************************************************** 40 | *FUNCTION NAME: GDO_Set() 41 | *FUNCTION : set GDO0_TX,GDO2_TX pin 42 | *INPUT : none 43 | *OUTPUT : none 44 | ****************************************************************/ 45 | void ZUHF_CC1101::GDO_Set (void) 46 | { 47 | pinMode(RX_GDO0_PIN, INPUT); 48 | pinMode(RX_GDO2_PIN, INPUT); 49 | } 50 | 51 | /**************************************************************** 52 | *FUNCTION NAME:Reset 53 | *FUNCTION :CC1101 reset //details refer datasheet of CC1101/CC1100// 54 | *INPUT :none 55 | *OUTPUT :none 56 | ****************************************************************/ 57 | void ZUHF_CC1101::Reset(void) 58 | { 59 | Serial.println("[RX] ***** BEGIN RESET *****"); 60 | digitalWrite(SCK_PIN, HIGH); 61 | digitalWrite(MOSI_PIN, LOW); 62 | digitalWrite(RX_PIN, LOW); 63 | delay(1); 64 | digitalWrite(RX_PIN, HIGH); 65 | delay(1); 66 | digitalWrite(RX_PIN, LOW); 67 | while(digitalRead(MISO_PIN)); 68 | SpiStrobe(CC1101_SRES); 69 | while(digitalRead(MISO_PIN)); 70 | delay(1); 71 | Serial.println("[RX] ***** RESET COMPLETE *****"); 72 | /* 73 | Note that the above reset procedure is 74 | only required just after the power supply is 75 | first turned on. If the user wants to reset 76 | the CC1101 after this, it is only necessary to 77 | issue an SRES command strobe 78 | */ 79 | } 80 | 81 | /**************************************************************** 82 | *FUNCTION NAME:Init 83 | *FUNCTION :CC1101 initialization 84 | *INPUT :none 85 | *OUTPUT :none 86 | ****************************************************************/ 87 | byte ZUHF_CC1101::Init() 88 | { 89 | SpiInit(); 90 | GDO_Set(); 91 | //SpiStrobe(CC1101_SRES); 92 | Reset(); 93 | 94 | // print out basic cc1101 info: version & partnum as a simple check 95 | // partnum should be 0x14 or 0x04; version is usually 0x00 96 | byte xversion = SpiReadStatus(CC1101_VERSION); 97 | byte xpart = SpiReadStatus(CC1101_PARTNUM); 98 | Serial.print("[RX] CSS PIN: ");Serial.println(RX_PIN, DEC); 99 | Serial.print("[RX] MOSI PIN: ");Serial.println(MOSI_PIN, DEC); 100 | Serial.print("[RX] MISO PIN: ");Serial.println(MISO_PIN, DEC); 101 | Serial.print("[RX] SCK PIN: ");Serial.println(SCK_PIN, DEC); 102 | Serial.print("[RX] Version: 0x");Serial.println(xversion, HEX); 103 | Serial.print("[RX] Partnumber: 0x");Serial.println(xpart, HEX); 104 | 105 | RegConfigSettings(); 106 | SpiWriteBurstReg(CC1101_PATABLE,PaTable,8); 107 | SpiStrobe(CC1101_SCAL); 108 | return xversion; 109 | } 110 | 111 | 112 | /**************************************************************** 113 | *FUNCTION NAME:SpiWriteReg 114 | *FUNCTION :CC1101 write data to register 115 | *INPUT :addr: register address; value: register value 116 | *OUTPUT :none 117 | ****************************************************************/ 118 | void ZUHF_CC1101::SpiWriteReg(byte addr, byte value) 119 | { 120 | SPI.transfer(RX_PIN, addr, SPI_CONTINUE); 121 | SPI.transfer(RX_PIN, value); 122 | } 123 | 124 | /**************************************************************** 125 | *FUNCTION NAME:SpiWriteBurstReg 126 | *FUNCTION :CC1101 write burst data to register 127 | *INPUT :addr: register address; buffer:register value array; num:number to write 128 | *OUTPUT :none 129 | ****************************************************************/ 130 | void ZUHF_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num) 131 | { 132 | byte i, temp; 133 | 134 | temp = addr | WRITE_BURST; 135 | SPI.transfer(RX_PIN, temp, SPI_CONTINUE); 136 | for (i = 0; i < num; i++) 137 | { 138 | if (i == (num - 1)){ 139 | SPI.transfer(RX_PIN, buffer[i]); 140 | }else{ 141 | SPI.transfer(RX_PIN, buffer[i], SPI_CONTINUE); 142 | } 143 | } 144 | } 145 | 146 | /**************************************************************** 147 | *FUNCTION NAME:SpiStrobe 148 | *FUNCTION :CC1101 Strobe 149 | *INPUT :strobe: command; //refer define in CC1101.h// 150 | *OUTPUT :none 151 | ****************************************************************/ 152 | void ZUHF_CC1101::SpiStrobe(byte strobe) 153 | { 154 | SPI.transfer(RX_PIN, strobe); 155 | } 156 | 157 | /**************************************************************** 158 | *FUNCTION NAME:SpiReadReg 159 | *FUNCTION :CC1101 read data from register 160 | *INPUT :addr: register address 161 | *OUTPUT :register value 162 | ****************************************************************/ 163 | byte ZUHF_CC1101::SpiReadReg(byte addr) 164 | { 165 | byte temp, value; 166 | temp = addr|READ_SINGLE; 167 | value = SPI.transfer(RX_PIN, temp, SPI_CONTINUE); 168 | value = SPI.transfer(RX_PIN, 0x00); 169 | return value; 170 | } 171 | 172 | /**************************************************************** 173 | *FUNCTION NAME:SpiReadBurstReg 174 | *FUNCTION :CC1101 read burst data from register 175 | *INPUT :addr: register address; buffer:array to store register value; num: number to read 176 | *OUTPUT :none 177 | ****************************************************************/ 178 | void ZUHF_CC1101::SpiReadBurstReg(byte addr, byte *buffer, byte num) 179 | { 180 | byte i,temp; 181 | temp = addr | READ_BURST; 182 | SPI.transfer(RX_PIN, temp, SPI_CONTINUE); 183 | for(i=0;i> 1; 323 | } 324 | 325 | for (int i = 0; i < duration-1; i++) 326 | { 327 | UpdateFifo(CW, 1); 328 | } 329 | UpdateFifo(&lastbyte, 1); 330 | } 331 | else 332 | { 333 | SendCW(duration); 334 | } 335 | } 336 | */ 337 | /**************************************************************** 338 | * FUNCTION NAME: SendCWBlock 339 | * FUNCTION : send continous high signal of duration n 340 | * INPUT : duration: number of bytes to send where 1 byte corresponds 8*12.5µs = 100µs 341 | * OUTPUT : none 342 | *****************************************************************/ 343 | void ZUHF_CC1101::SendCWBurst(byte duration) 344 | { 345 | SpiWriteBurstReg(CC1101_TXFIFO, CW, duration); 346 | } 347 | 348 | /**************************************************************** 349 | * FUNCTION NAME: SendIdle 350 | * FUNCTION : send continous low signal 351 | * INPUT : duration: number of bytes to send where 1 byte corresponds 8*12.5µs = 100µs 352 | *OUTPUT : none 353 | ****************************************************************/ 354 | void ZUHF_CC1101::SendIdle(byte duration) 355 | { 356 | for (int i = 0; i < duration; i++) 357 | { 358 | UpdateFifo(OFF, 1); 359 | } 360 | } 361 | 362 | /**************************************************************** 363 | * FUNCTION NAME: SendByte 364 | * FUNCTION : send single signal 365 | * INPUT : b: byte signal to send 366 | *OUTPUT : none 367 | ****************************************************************/ 368 | /* use only if in TX mode 369 | * definition for TX_GDO0/2_PIO and TX_GDO0/2_STATE needs to be added 370 | * if used. 371 | */ 372 | /* 373 | void ZUHF_CC1101::SendByte(byte value) 374 | { 375 | while(TX_GDO2_STATE); 376 | SpiWriteReg(CC1101_TXFIFO, value); 377 | } 378 | */ 379 | 380 | /**************************************************************** 381 | * FUNCTION NAME: DecodeFM0 382 | * FUNCTION : FM0 decodes a sequence of bits 383 | * INPUT : data: pointer to a byte array of data; data_size: number of bytes that should be decoded from data 384 | * OUTPUT : bitArray: pointer to an array where the result is saved as sequence of bits 385 | ****************************************************************/ 386 | /* ATTENTION: It does not check if state is properly changed according to FM0 for each data unit yet. It assumes the 387 | provided data is a legitimate format. The lack of this check is basically compensated by the usually 388 | obligatory CRC check when communicating with a tag. 389 | */ 390 | void ZUHF_CC1101::DecodeFM0(byte *bitArray, byte *data, byte data_size) 391 | { 392 | byte bit_index = 0; 393 | byte mask = 0xC0; // 1100 0000 394 | byte FM0 = 0; 395 | byte prev = 0; 396 | byte curr = 0; 397 | for (byte i = 0; i < data_size; i++) 398 | { 399 | Serial.println(data[i],HEX); 400 | for (byte shift = 0; shift < 4; shift++) 401 | { 402 | FM0 = (data[i] & (mask >> (shift*2))) >> (6-(shift*2)); 403 | switch(FM0){ 404 | case 3: 405 | bitArray[(i*4)+shift] = 1; 406 | break; 407 | case 0: 408 | bitArray[(i*4)+shift] = 1; 409 | break; 410 | case 2: 411 | bitArray[(i*4)+shift] = 0; 412 | break; 413 | case 1: 414 | bitArray[(i*4)+shift] = 0; 415 | break; 416 | default: 417 | Serial.println("[ERROR] Error when decoding FM0 data!"); 418 | break; 419 | } 420 | } 421 | } 422 | } 423 | 424 | ZUHF_CC1101 RX_UNIT; 425 | 426 | #endif 427 | 428 | 429 | 430 | -------------------------------------------------------------------------------- /libraries/ZUHF_CC1101/ZUHF_CC1101.h: -------------------------------------------------------------------------------- 1 | /* Title: ZUHF_CC1101 2 | * Description: x 3 | * Author: Zaphoxx (Manfred Heinz) 4 | * Version: 1 5 | * License: x 6 | */ 7 | 8 | #ifndef ZUHF_CC1101_h 9 | #define ZUHF_CC1101_h 10 | 11 | #include "Arduino.h" 12 | #include 13 | 14 | //SPI PINS 15 | #define SCK_PIN 76 16 | #define MISO_PIN 74 17 | #define MOSI_PIN 75 18 | 19 | // PINS FOR TX MODULE 20 | #define RX_PIN 52 21 | #define RX_GDO0_PIN 24 // PORTA 15 22 | #define RX_GDO2_PIN 26 // PORTD 1 23 | #define RX_GDO0_PIO 15 24 | #define RX_GDO2_PIO 1 25 | 26 | #define RX_GDO0_STATE (((PIOA->PIO_PDSR) & (1 << RX_GDO0_PIO)) >> RX_GDO0_PIO) 27 | #define RX_GDO2_STATE (((PIOD->PIO_PDSR) & (1 << RX_GDO2_PIO)) >> RX_GDO2_PIO) 28 | 29 | //************************************* class **************************************************// 30 | class ZUHF_CC1101 31 | { 32 | private: 33 | byte PaTable[8] = {0x00,0x80,0x27,0x67,0x50,0x80,0xc0,0x00}; // 34 | byte CW[1] = {0xff}; 35 | byte IDLE[1] = {0x00}; 36 | byte OFF[1] = {0x00}; 37 | 38 | void SpiInit(void); 39 | void SpiMode(byte config); 40 | byte SpiTransfer(byte value); 41 | void GDO_Set (void); 42 | void Reset(void); 43 | void RegConfigSettings(void); 44 | 45 | public: 46 | void SpiWriteReg(byte addr, byte value); 47 | void SpiWriteBurstReg(byte addr, byte *buffer, byte num); 48 | void SpiStrobe(byte strobe); 49 | byte SpiReadReg(byte addr); 50 | void SpiReadBurstReg(byte addr, byte *buffer, byte num); 51 | byte SpiReadStatus(byte addr); 52 | 53 | byte Init(); 54 | /* Custom Functions */ 55 | void UpdateFifo(byte *data, int nbytes); 56 | void SendCW(byte duration); 57 | //void SendCW(byte duration, byte lastbyteduration); 58 | void SendCWBurst(byte duration); 59 | void SendByte(byte value); 60 | void SendIdle(byte duration); 61 | int16_t ReadRSSI(void); 62 | 63 | void DecodeFM0(byte *bitarray, byte *data, byte data_size); 64 | int PieEncodeData(byte *encoded, const byte *data, byte data_size); 65 | }; 66 | 67 | extern ZUHF_CC1101 RX_UNIT; 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /libraries/ZUHF_CC1101/ZUHF_CC1101.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaphoxx/zuhf-rfid/9c9271bf905c8357c7f5abddc29e939fef338a87/libraries/ZUHF_CC1101/ZUHF_CC1101.zip -------------------------------------------------------------------------------- /libraries/ZUHF_CC1101/ZUHF_CC1101_REGS.h: -------------------------------------------------------------------------------- 1 | #ifndef ZUHF_CC1101_REGS_H 2 | #define ZUHF_CC1101_REGS_H 3 | 4 | 5 | //***************************************CC1101 define**************************************************// 6 | // CC1101 CONFIG REGISTER 7 | #define CC1101_IOCFG2 0x00 // GDO2_TX output pin configuration 8 | #define CC1101_IOCFG1 0x01 // GDO1 output pin configuration 9 | #define CC1101_IOCFG0 0x02 // GDO0_TX output pin configuration 10 | #define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds 11 | #define CC1101_SYNC1 0x04 // Sync word, high INT8U 12 | #define CC1101_SYNC0 0x05 // Sync word, low INT8U 13 | #define CC1101_PKTLEN 0x06 // Packet length 14 | #define CC1101_PKTCTRL1 0x07 // Packet automation control 15 | #define CC1101_PKTCTRL0 0x08 // Packet automation control 16 | #define CC1101_ADDR 0x09 // Device address 17 | #define CC1101_CHANNR 0x0A // Channel number 18 | #define CC1101_FSCTRL1 0x0B // Frequency synthesizer control 19 | #define CC1101_FSCTRL0 0x0C // Frequency synthesizer control 20 | #define CC1101_FREQ2 0x0D // Frequency control word, high INT8U 21 | #define CC1101_FREQ1 0x0E // Frequency control word, middle INT8U 22 | #define CC1101_FREQ0 0x0F // Frequency control word, low INT8U 23 | #define CC1101_MDMCFG4 0x10 // Modem configuration 24 | #define CC1101_MDMCFG3 0x11 // Modem configuration 25 | #define CC1101_MDMCFG2 0x12 // Modem configuration 26 | #define CC1101_MDMCFG1 0x13 // Modem configuration 27 | #define CC1101_MDMCFG0 0x14 // Modem configuration 28 | #define CC1101_DEVIATN 0x15 // Modem deviation setting 29 | #define CC1101_MCSM2 0x16 // Main Radio Control State Machine configuration 30 | #define CC1101_MCSM1 0x17 // Main Radio Control State Machine configuration 31 | #define CC1101_MCSM0 0x18 // Main Radio Control State Machine configuration 32 | #define CC1101_FOCCFG 0x19 // Frequency Offset Compensation configuration 33 | #define CC1101_BSCFG 0x1A // Bit Synchronization configuration 34 | #define CC1101_AGCCTRL2 0x1B // AGC control 35 | #define CC1101_AGCCTRL1 0x1C // AGC control 36 | #define CC1101_AGCCTRL0 0x1D // AGC control 37 | #define CC1101_WOREVT1 0x1E // High INT8U Event 0 timeout 38 | #define CC1101_WOREVT0 0x1F // Low INT8U Event 0 timeout 39 | #define CC1101_WORCTRL 0x20 // Wake On Radio control 40 | #define CC1101_FREND1 0x21 // Front end RX configuration 41 | #define CC1101_FREND0 0x22 // Front end TX configuration 42 | #define CC1101_FSCAL3 0x23 // Frequency synthesizer calibration 43 | #define CC1101_FSCAL2 0x24 // Frequency synthesizer calibration 44 | #define CC1101_FSCAL1 0x25 // Frequency synthesizer calibration 45 | #define CC1101_FSCAL0 0x26 // Frequency synthesizer calibration 46 | #define CC1101_RCCTRL1 0x27 // RC oscillator configuration 47 | #define CC1101_RCCTRL0 0x28 // RC oscillator configuration 48 | #define CC1101_FSTEST 0x29 // Frequency synthesizer calibration control 49 | #define CC1101_PTEST 0x2A // Production test 50 | #define CC1101_AGCTEST 0x2B // AGC test 51 | #define CC1101_TEST2 0x2C // Various test settings 52 | #define CC1101_TEST1 0x2D // Various test settings 53 | #define CC1101_TEST0 0x2E // Various test settings 54 | 55 | //CC1101 Strobe commands 56 | #define CC1101_SRES 0x30 // Reset chip. 57 | #define CC1101_SFSTXON 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). 58 | // If in RX/TX: Go to a wait state where only the synthesizer is 59 | // running (for quick RX / TX turnaround). 60 | #define CC1101_SXOFF 0x32 // Turn off crystal oscillator. 61 | #define CC1101_SCAL 0x33 // Calibrate frequency synthesizer and turn it off 62 | // (enables quick start). 63 | #define CC1101_SRX 0x34 // Enable RX. Perform calibration first if coming from IDLE and 64 | // MCSM0.FS_AUTOCAL=1. 65 | 66 | #define CC1101_STX 0x35 // In IDLE state: Enable TX. Perform calibration first if 67 | // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled: 68 | // Only go to TX if channel is clear. 69 | #define CC1101_SIDLE 0x36 // Exit RX / TX, turn off frequency synthesizer and exit 70 | // Wake-On-Radio mode if applicable. 71 | #define CC1101_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer 72 | #define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) 73 | #define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high. 74 | #define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. 75 | #define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. 76 | #define CC1101_SWORRST 0x3C // Reset real time clock. 77 | #define CC1101_SNOP 0x3D // No operation. May be used to pad strobe commands to two 78 | // INT8Us for simpler software. 79 | //CC1101 STATUS REGSITER 80 | #define CC1101_PARTNUM 0x30 81 | #define CC1101_VERSION 0x31 82 | #define CC1101_FREQEST 0x32 83 | #define CC1101_LQI 0x33 84 | #define CC1101_RSSI 0x34 85 | #define CC1101_MARCSTATE 0x35 86 | #define CC1101_WORTIME1 0x36 87 | #define CC1101_WORTIME0 0x37 88 | #define CC1101_PKTSTATUS 0x38 89 | #define CC1101_VCO_VC_DAC 0x39 90 | #define CC1101_TXBYTES 0x3A 91 | #define CC1101_RXBYTES 0x3B 92 | 93 | //CC1101 PATABLE,TXFIFO,RXFIFO 94 | #define CC1101_PATABLE 0x3E 95 | #define CC1101_TXFIFO 0x3F 96 | #define CC1101_RXFIFO 0x3F 97 | 98 | //CC1101 MARCSTATES 99 | #define M_SRX 0x0D 100 | #define M_STX 0x13 101 | #define M_SIDLE 0x01 102 | 103 | /****************************************************************/ 104 | #define WRITE_BURST 0x40 //write burst 105 | #define READ_SINGLE 0x80 //read single 106 | #define READ_BURST 0xC0 //read burst 107 | #define BYTES_IN_RXFIFO 0x7F //byte number in RXfifo 108 | #define BYTES_IN_TXFIFO 0x7F //byte number in TXfifo 109 | /****************************************************************/ 110 | 111 | #endif -------------------------------------------------------------------------------- /libraries/ZUHF_CC1101/ZUHF_CRC.h: -------------------------------------------------------------------------------- 1 | #ifndef ZUHF_CRC_H 2 | #define ZUHF_CRC_H 3 | 4 | 5 | /* Function adapted from https://www.cgran.org/wiki/Gen2 */ 6 | /* crc check on received data */ 7 | bool check_crc16(byte *data, uint32_t num_bytes) 8 | { 9 | uint16_t crc_16 = 0x0000, rcvd_crc = 0x0000; 10 | /* save crc from data into variable for later comparison */ 11 | rcvd_crc = (data[num_bytes - 2] << 8) + data[num_bytes -1]; 12 | 13 | crc_16 = 0xFFFF; 14 | for (uint32_t i=0; i < num_bytes-2; i++) 15 | { 16 | crc_16^=data[i] << 8; 17 | for (uint32_t j=0;j<8;j++) 18 | { 19 | if (crc_16 & 0x8000) 20 | { 21 | crc_16 <<= 1; 22 | crc_16 ^= 0x1021; 23 | } 24 | else 25 | crc_16 <<= 1; 26 | } 27 | } 28 | crc_16 = ~crc_16; 29 | 30 | if(Serial){ 31 | //Serial.print("[CRC16] CALC CRC16: ");Serial.println(crc_16,HEX); 32 | //Serial.print("[CRC16] RCVD CRC16: ");Serial.println(rcvd_crc,HEX); 33 | } 34 | if(rcvd_crc != crc_16) 35 | return false; 36 | else 37 | return true; 38 | } 39 | 40 | // ========================================================================== 41 | // CRC Generation Unit - Linear Feedback Shift Register implementation 42 | // (c) Kay Gorontzi, GHSi.de, distributed under the terms of LGPL 43 | // ========================================================================== 44 | uint16_t crc16B(byte *crc16bits, byte *bits, byte num_bits) 45 | { 46 | bool CRC[16]; 47 | byte DoInvert; 48 | uint16_t crc16; 49 | for (int i=0; i<16; ++i) CRC[i] = 1; // Init before calculation 50 | 51 | for (int i=0; i < num_bits; ++i) 52 | { 53 | DoInvert = (bits[i]==1) ^ CRC[15]; 54 | CRC[15] = CRC[14]; 55 | CRC[14] = CRC[13]; 56 | CRC[13] = CRC[12]; 57 | CRC[12] = CRC[11] ^ DoInvert; 58 | CRC[11] = CRC[10]; 59 | CRC[10] = CRC[9]; 60 | CRC[9] = CRC[8]; 61 | CRC[8] = CRC[7]; 62 | CRC[7] = CRC[6]; 63 | CRC[6] = CRC[5]; 64 | CRC[5] = CRC[4] ^ DoInvert; 65 | CRC[4] = CRC[3]; 66 | CRC[3] = CRC[2]; 67 | CRC[2] = CRC[1]; 68 | CRC[1] = CRC[0]; 69 | CRC[0] = DoInvert; 70 | } 71 | 72 | //memcpy(crc16bits, CRC, sizeof(CRC)); 73 | uint16_t mask = 0x0001; 74 | crc16 = 0x0000; 75 | for (int i = 0; i < 16; i++) 76 | { 77 | CRC[i] = CRC[i] ^ 1; 78 | crc16bits[15-i] = CRC[i]; 79 | if (CRC[i]) 80 | { 81 | crc16 = crc16 | mask; 82 | } 83 | mask = mask << 1; 84 | } 85 | return(crc16); 86 | } 87 | 88 | /* Function adapted from https://www.cgran.org/wiki/Gen2 */ 89 | /* check on bitarray after FM0 decoding */ 90 | /**************************************************************** 91 | *FUNCTION NAME: crc16 92 | *FUNCTION : calculates CRC16 from a sequence of bits 93 | *INPUT : bits: bit array of data over which crc16 should be calculated; num_bits: number of bits 94 | *OUTPUT : crc16 word 95 | ****************************************************************/ 96 | uint16_t crc16special(byte *bits, uint32_t num_bits) 97 | { 98 | uint16_t crc_16, rcvd_crc; 99 | byte *data,*newbits; 100 | uint32_t num_bytes = num_bits / 8; 101 | uint32_t R = num_bits % 8; 102 | data = (uint8_t*) malloc(num_bytes); 103 | newbits = (uint8_t*) malloc(num_bits + (8 - R)); 104 | uint8_t mask; 105 | num_bytes++; 106 | 107 | //memcpy(newbits, 0, 8-R); 108 | memcpy(newbits, bits, num_bits); 109 | memcpy(newbits + num_bits, 0, 8 - R); 110 | 111 | for(uint32_t i = 0 ; i < num_bytes; i++) 112 | { 113 | mask = 0x80; 114 | data[i] = 0; 115 | for(uint32_t j = 0; j < 8; j++) 116 | { 117 | if (newbits[(i * 8) + j] == 1){ 118 | data[i] = data[i] | mask; 119 | } 120 | mask = mask >> 1; 121 | } 122 | } 123 | 124 | rcvd_crc = (data[num_bytes - 2] << 8) + data[num_bytes -1]; 125 | 126 | crc_16 = 0xFFFF; 127 | for (uint32_t i=0; i < num_bytes; i++) 128 | { 129 | crc_16^=data[i] << 8; 130 | for (uint32_t j=0;j<8;j++) 131 | { 132 | if (crc_16 & 0x8000) 133 | { 134 | crc_16 <<= 1; 135 | crc_16 ^= 0x1021; 136 | } 137 | else 138 | crc_16 <<= 1; 139 | } 140 | } 141 | crc_16 = ~crc_16; 142 | free(data); 143 | free(newbits); 144 | return crc_16; 145 | } 146 | 147 | 148 | /* Function adapted from https://www.cgran.org/wiki/Gen2 */ 149 | /* check on bitarray after FM0 decoding */ 150 | /**************************************************************** 151 | *FUNCTION NAME: crc16 152 | *FUNCTION : calculates CRC16 from a sequence of bits 153 | *INPUT : bits: bit array of data over which crc16 should be calculated; num_bits: number of bits 154 | *OUTPUT : crc16 word 155 | ****************************************************************/ 156 | uint16_t crc16(byte *bits, uint32_t num_bits) 157 | { 158 | uint16_t crc_16, rcvd_crc; 159 | byte *data; 160 | uint32_t num_bytes = num_bits / 8; 161 | 162 | data = (uint8_t*) malloc(num_bytes); 163 | 164 | uint8_t mask; 165 | 166 | 167 | for(uint32_t i = 0 ; i < num_bytes; i++) 168 | { 169 | mask = 0x80; 170 | data[i] = 0; 171 | for(uint32_t j = 0; j < 8; j++) 172 | { 173 | if (bits[(i * 8) + j] == 1){ 174 | data[i] = data[i] | mask; 175 | } 176 | mask = mask >> 1; 177 | } 178 | } 179 | 180 | rcvd_crc = (data[num_bytes - 2] << 8) + data[num_bytes -1]; 181 | 182 | crc_16 = 0xFFFF; 183 | for (uint32_t i=0; i < num_bytes; i++) 184 | { 185 | crc_16^=data[i] << 8; 186 | for (uint32_t j=0;j<8;j++) 187 | { 188 | if (crc_16 & 0x8000) 189 | { 190 | crc_16 <<= 1; 191 | crc_16 ^= 0x1021; 192 | } 193 | else 194 | crc_16 <<= 1; 195 | } 196 | } 197 | crc_16 = ~crc_16; 198 | free(data); 199 | return crc_16; 200 | } 201 | 202 | 203 | 204 | void crc5_append(byte *q, byte q_size) 205 | { 206 | byte crc[] = {1,0,0,1,0}; 207 | for(byte i = 0; i < 17; i++) 208 | { 209 | byte tmp[] = {0,0,0,0,0}; 210 | tmp[4] = crc[3]; 211 | if(crc[4] == 1) 212 | { 213 | if (q[i] == 1) 214 | { 215 | tmp[0] = 0; 216 | tmp[1] = crc[0]; 217 | tmp[2] = crc[1]; 218 | tmp[3] = crc[2]; 219 | } 220 | else 221 | { 222 | tmp[0] = 1; 223 | tmp[1] = crc[0]; 224 | tmp[2] = crc[1]; 225 | if(crc[2] == 1) 226 | { 227 | tmp[3] = 0; 228 | } 229 | else 230 | { 231 | tmp[3] = 1; 232 | } 233 | } 234 | } 235 | else 236 | { 237 | if (q[i] == 1) 238 | { 239 | tmp[0] = 1; 240 | tmp[1] = crc[0]; 241 | tmp[2] = crc[1]; 242 | if(crc[2] == 1) 243 | { 244 | tmp[3] = 0; 245 | } 246 | else 247 | { 248 | tmp[3] = 1; 249 | } 250 | } 251 | else 252 | { 253 | tmp[0] = 0; 254 | tmp[1] = crc[0]; 255 | tmp[2] = crc[1]; 256 | tmp[3] = crc[2]; 257 | } 258 | } 259 | memcpy(&crc,&tmp,5); 260 | } 261 | for (int i = 0; i < 5; i++){ 262 | memcpy(q+17+i, &crc[4-i], 1); 263 | } 264 | } 265 | 266 | #endif -------------------------------------------------------------------------------- /libraries/ZUHF_CC1101/ZUHF_MENU.h: -------------------------------------------------------------------------------- 1 | #ifndef ZUHF_MENU_H 2 | #define ZUHF_MENU_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | MCUFRIEND_kbv tft; 11 | 12 | #define LED_PIN1 40 13 | #define LED_PIN2 38 14 | 15 | #define LCD_CS A3 16 | #define LCD_CD A2 17 | #define LCD_WR A1 18 | #define LCD_RD A6 19 | #define LCD_RESET A4 20 | 21 | #define TS_MINX 100 22 | #define TS_MINY 215 //200 23 | #define TS_MAXX 907 // 875 24 | #define TS_MAXY 862 // 863 25 | 26 | #define YP A2 // must be an analog pin, use "An" notation! 27 | #define XM A3 // must be an analog pin, use "An" notation! 28 | #define YM 8 // can be a digital pin 29 | #define XP 9 // can be a digital pin 30 | 31 | #define BLACK 0x0000 32 | #define BLUE 0x001F 33 | #define RED 0xF800 34 | #define LIGHTBLUE 0x07FF 35 | #define GREEN 0x07E0 36 | #define CYAN 0x07FF 37 | #define MAGENTA 0xF81F 38 | #define YELLOW 0xFFE0 39 | #define WHITE 0xFFFF 40 | 41 | #define MAX_LINES 256 42 | 43 | enum MENU_STATES 44 | { 45 | M_MAIN, 46 | M_IMAIN, 47 | M_CONFIG, 48 | M_ICONFIG, 49 | M_RESULT, 50 | M_WAIT 51 | }; 52 | 53 | extern int repetitions; 54 | extern byte packet_delay; 55 | extern byte paPower; 56 | extern byte agc1; 57 | extern byte agc2; 58 | extern READER_STATES reader_state; 59 | extern byte sync1,sync0; 60 | 61 | MENU_STATES menuState = M_MAIN; 62 | //Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); 63 | TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); 64 | 65 | boolean lightOn = false; 66 | uint16_t bcolor = RED; 67 | String resultBuffer[MAX_LINES]; 68 | uint16_t resultLines = 0; 69 | 70 | /* Main Menu Buttons */ 71 | TS_Button configButton;// = TS_Button(&tft,&ts,0,60,480,40,RED,WHITE,WHITE,"Configurations",2,30,0); 72 | TS_Button runButton;// = TS_Button(&tft,&ts,0,100,480,40,RED,WHITE,WHITE,"Start Run",2,30,0); 73 | TS_Button syncSwitch; 74 | 75 | /* Config Menu Buttons */ 76 | TS_Button backButton; 77 | const uint8_t nPowerButtons = 5; 78 | TS_Button powerButtons[nPowerButtons]; 79 | const char *powerTexts[nPowerButtons] = {"-9.8dBm", "-5.0dBm", "-0.3dBm", "+5.2dBm", "+10.7dBm"}; 80 | const char powerVals[nPowerButtons] = {0x12,0x13,0x13,0x14,0x15}; 81 | char paVal = powerVals[0]; 82 | 83 | const uint8_t nAgc1Buttons = 3; 84 | TS_Button agc1Buttons[nAgc1Buttons]; 85 | const char *agc1Texts[nAgc1Buttons] = {"0x90","0x91","0x92"}; 86 | const char agc1Vals[nAgc1Buttons] = {0x90,0x91,0x92}; 87 | 88 | const uint8_t nAgc2Buttons = 8; 89 | TS_Button agc2Buttons[nAgc2Buttons]; 90 | const char *agc2Texts[nAgc2Buttons] = {"0","1","2","3","4","5","6","7"}; 91 | const char agc2Vals[nAgc2Buttons] = {0,1,2,3,4,5,6,7}; 92 | 93 | const uint8_t nRepButtons = 4; 94 | TS_Button repButtons[nRepButtons]; 95 | const char *repTexts[nRepButtons] = {"1","10","100","inf"}; 96 | const char repVals[nRepButtons] = {1,10,100,0}; 97 | 98 | const uint8_t nDelayButtons = 4; 99 | TS_Button delayButtons[nDelayButtons]; 100 | const char *delayTexts[nDelayButtons] = {"0us","20us","500us","1s"}; 101 | const uint16_t delayVals[nDelayButtons] = {0,20,500,1000}; 102 | 103 | 104 | 105 | 106 | 107 | void InitConfigMenu() 108 | { 109 | pinMode(XM, OUTPUT); 110 | pinMode(YP, OUTPUT); 111 | tft.fillScreen(BLACK); 112 | tft.setCursor(0,0); 113 | tft.setTextColor(WHITE); 114 | tft.setTextSize(2); 115 | 116 | tft.println("ConfigurtionMenu"); 117 | tft.println("PowerSetting"); 118 | for (int i = 0; i < nPowerButtons; i++) 119 | { 120 | powerButtons[i].Draw(powerButtons[i].selected); 121 | } 122 | 123 | tft.setCursor(0,74); 124 | tft.setTextColor(WHITE); 125 | tft.setTextSize(2); 126 | tft.println("Automatic Gain Control 0"); 127 | for (int i = 0; i < nAgc1Buttons; i++){ 128 | agc1Buttons[i].Draw(agc1Buttons[i].selected); 129 | } 130 | 131 | tft.setCursor(0,124); 132 | tft.setTextColor(WHITE); 133 | tft.setTextSize(2); 134 | tft.println("Automatic Gain Control 2"); 135 | for (int i = 0; i < nAgc2Buttons; i++){ 136 | agc2Buttons[i].Draw(agc2Buttons[i].selected); 137 | } 138 | tft.setCursor(0,174); 139 | tft.setTextColor(WHITE); 140 | tft.setTextSize(2); 141 | tft.println("Repetitions"); 142 | for (int i = 0; i < nRepButtons; i++){ 143 | repButtons[i].Draw(repButtons[i].selected); 144 | } 145 | tft.setCursor(0,224); 146 | tft.setTextColor(WHITE); 147 | tft.setTextSize(2); 148 | tft.println("Delay between Runs"); 149 | for (int i = 0; i < nDelayButtons; i++){ 150 | delayButtons[i].Draw(delayButtons[i].selected); 151 | } 152 | backButton.Draw(false); 153 | } 154 | 155 | void RunConfigMenu() 156 | { 157 | 158 | for (int i = 0; i < nPowerButtons; i++){ 159 | if (powerButtons[i].Pressed()){ 160 | paPower = powerVals[i]; 161 | if(Serial){ 162 | Serial.print("output Power: ");Serial.println(powerTexts[i]); 163 | } 164 | } 165 | } 166 | for (int i = 0; i < nAgc1Buttons; i++){ 167 | if (agc1Buttons[i].Pressed()){ 168 | agc1 = agc1Vals[i]; 169 | if(Serial){ 170 | Serial.print("AGC0: ");Serial.println(agc1Texts[i]); 171 | } 172 | } 173 | 174 | } 175 | for (int i = 0; i < nAgc2Buttons; i++){ 176 | if (agc2Buttons[i].Pressed()){ 177 | agc2 = agc2Vals[i]; 178 | if(Serial){ 179 | Serial.print("AGC2: ");Serial.println(agc2Texts[i]); 180 | } 181 | } 182 | 183 | } 184 | for (int i = 0; i < nRepButtons; i++){ 185 | if (repButtons[i].Pressed()){ 186 | repetitions = repVals[i]; 187 | if(Serial){ 188 | Serial.print("n Repetitions: ");Serial.println(repTexts[i]); 189 | } 190 | } 191 | 192 | } 193 | for (int i = 0; i < nDelayButtons; i++){ 194 | if (delayButtons[i].Pressed()){ 195 | packet_delay = delayVals[i]; 196 | if(Serial){ 197 | Serial.print("delay: ");Serial.println(delayTexts[i]); 198 | } 199 | } 200 | } 201 | if (backButton.Pressed()){ 202 | menuState = M_IMAIN; 203 | } 204 | delay(200); 205 | } 206 | 207 | void InitMainMenu() 208 | { 209 | pinMode(XM, OUTPUT); 210 | pinMode(YP, OUTPUT); 211 | tft.fillScreen(BLACK); 212 | tft.setCursor(480 / 2 - 140,10); 213 | tft.setTextColor(WHITE); 214 | tft.setTextSize(3); 215 | tft.println("*** ZUHF-RFID ***"); 216 | configButton.Draw(false); 217 | runButton.Draw(false); 218 | syncSwitch.Draw(false); 219 | } 220 | 221 | READER_STATES RunMainMenu() 222 | { 223 | READER_STATES rstate = R_WAIT; 224 | if (configButton.Pressed()){ 225 | lightOn = !lightOn; 226 | digitalWrite(LED_PIN1, lightOn); 227 | digitalWrite(LED_PIN2, HIGH); 228 | menuState = M_ICONFIG; 229 | } 230 | if (runButton.Pressed()){ 231 | digitalWrite(LED_PIN1, LOW); 232 | digitalWrite(LED_PIN2, LOW); 233 | rstate = R_START; 234 | } 235 | if (syncSwitch.Pressed()){ 236 | sync1 = ~sync1; 237 | sync0 = ~sync0; 238 | char *syncText; 239 | if (sync1 == 0xad){ 240 | syncText = "Syncword 0xAD23"; 241 | syncSwitch.text = syncText; 242 | syncSwitch.Draw(false); 243 | }else{ 244 | syncText = "Inv Syncword "; 245 | syncSwitch.text = syncText; 246 | syncSwitch.Draw(true); 247 | } 248 | 249 | } 250 | delay(200); 251 | return rstate; 252 | } 253 | 254 | 255 | void CreateButtons(uint8_t power, uint8_t agc1, uint8_t agc2, uint8_t repetitions, uint16_t delay) 256 | { 257 | 258 | configButton = TS_Button(&tft,&ts,0,60,480,40,RED,WHITE,WHITE,"Configurations",2,30,0); 259 | runButton = TS_Button(&tft,&ts,0,100,480,40,RED,WHITE,WHITE,"Start Run",2,30,0); 260 | syncSwitch = TS_Button(&tft,&ts,0,140,480,40,RED,WHITE,WHITE,"Invert SyncWord",2,30,0); 261 | 262 | for (int i = 0; i < nPowerButtons; i++) 263 | { 264 | powerButtons[i] = TS_Button(&tft,&ts,(i*(480 / nPowerButtons)),40,480 / nPowerButtons,30,WHITE,BLACK,BLACK,powerTexts[i],2,0,0); 265 | powerButtons[i].cursor_x_offset = 10; 266 | if (powerVals[i] == power){ 267 | powerButtons[i].selected = true; 268 | //powerButtons[i].Draw(true); 269 | } 270 | } 271 | 272 | tft.setCursor(0,74); 273 | tft.setTextColor(WHITE); 274 | tft.setTextSize(2); 275 | tft.println("Automatic Gain Control 0"); 276 | for (int i = 0; i < nAgc1Buttons; i++){ 277 | agc1Buttons[i] = TS_Button(&tft,&ts,(i*(480 / nAgc1Buttons)),90,480 / nAgc1Buttons,30,WHITE,BLACK,BLACK,agc1Texts[i],2,10,0); 278 | agc1Buttons[i].cursor_x_offset = 40; 279 | if (agc1Vals[i] == agc1){ 280 | agc1Buttons[i].selected = true; 281 | //agc1Buttons[i].Draw(true); 282 | } 283 | } 284 | 285 | tft.setCursor(0,124); 286 | tft.setTextColor(WHITE); 287 | tft.setTextSize(2); 288 | tft.println("Automatic Gain Control 2"); 289 | for (int i = 0; i < nAgc2Buttons; i++){ 290 | agc2Buttons[i] = TS_Button(&tft,&ts,(i*(480 / nAgc2Buttons)),140,480 / nAgc2Buttons,30,WHITE,BLACK,BLACK,agc2Texts[i],2,10,0); 291 | agc2Buttons[i].cursor_x_offset = 20; 292 | if (agc2Vals[i] == agc2){ 293 | agc2Buttons[i].selected = true; 294 | //agc2Buttons[i].Draw(true); 295 | } 296 | } 297 | tft.setCursor(0,174); 298 | tft.setTextColor(WHITE); 299 | tft.setTextSize(2); 300 | tft.println("Repetitions"); 301 | for (int i = 0; i < nRepButtons; i++){ 302 | repButtons[i] = TS_Button(&tft,&ts,(i*(480 / nRepButtons)),190,480 / nRepButtons,30,WHITE,BLACK,BLACK,repTexts[i],2,50,0); 303 | repButtons[i].cursor_x_offset = 20; 304 | if (repVals[i] == repetitions){ 305 | repButtons[i].selected = true; 306 | //repButtons[i].Draw(true); 307 | } 308 | } 309 | tft.setCursor(0,224); 310 | tft.setTextColor(WHITE); 311 | tft.setTextSize(2); 312 | tft.println("Delay between Runs"); 313 | for (int i = 0; i < nDelayButtons; i++){ 314 | delayButtons[i] = TS_Button(&tft,&ts,(i*(480 / nDelayButtons)),240,480 / nDelayButtons,30,WHITE,BLACK,BLACK,delayTexts[i],2,30,0); 315 | delayButtons[i].cursor_x_offset = 20; 316 | if (delayVals[i] == delay){ 317 | delayButtons[i].selected = true; 318 | //delayButtons[i].Draw(true); 319 | } 320 | } 321 | backButton = TS_Button(&tft,&ts,0,280,480,40,RED,WHITE,WHITE,"BACK",2,480 / 2 + 30,0); 322 | } 323 | 324 | 325 | void InitMenu() 326 | { 327 | pinMode(LED_PIN1,OUTPUT); 328 | pinMode(LED_PIN2,OUTPUT); 329 | digitalWrite(LED_PIN2,HIGH); 330 | pinMode(XM, OUTPUT); 331 | pinMode(YP, OUTPUT); 332 | tft.reset(); 333 | tft.begin(0x9486); 334 | tft.setRotation(1); 335 | tft.fillScreen(BLACK); 336 | CreateButtons(0x80,0x90,0x03,0x00,500); 337 | menuState = M_IMAIN; 338 | //InitConfigMenu(0x80,0x90,0x03,0x00,500); 339 | } 340 | 341 | void PrintResult(String msg, bool reset_buffer) 342 | { 343 | pinMode(XM, OUTPUT); 344 | pinMode(YP, OUTPUT); 345 | //tft.reset(); 346 | //tft.begin(0x9486); 347 | //tft.setRotation(1); 348 | if (reset_buffer){ 349 | for (int i = 0; i < MAX_LINES; i++){ 350 | resultBuffer[i] = ""; 351 | } 352 | resultLines = 0; 353 | } 354 | if (resultLines < MAX_LINES){ 355 | resultBuffer[resultLines] = msg; 356 | resultLines++; 357 | 358 | tft.fillScreen(BLACK); 359 | tft.setTextColor(WHITE); 360 | tft.setTextSize(2); 361 | tft.setCursor(0,0); 362 | int istart; 363 | if (resultLines < 20) { 364 | istart = 0; 365 | }else{ 366 | istart = resultLines - 20; 367 | } 368 | for (int i = istart; i < resultLines; i++){ 369 | tft.println(resultBuffer[i]); 370 | } 371 | } 372 | } 373 | 374 | 375 | 376 | void RunMenu(){ 377 | switch(menuState){ 378 | case M_IMAIN: 379 | InitMainMenu(); 380 | menuState = M_MAIN; 381 | break; 382 | 383 | case M_MAIN: 384 | reader_state = RunMainMenu(); 385 | break; 386 | 387 | case M_ICONFIG: 388 | InitConfigMenu(); 389 | menuState = M_CONFIG; 390 | break; 391 | 392 | case M_CONFIG: 393 | RunConfigMenu(); 394 | break; 395 | 396 | case M_WAIT: 397 | break; 398 | case M_RESULT: 399 | break; 400 | 401 | default: 402 | menuState = M_IMAIN; 403 | } 404 | } 405 | 406 | #endif -------------------------------------------------------------------------------- /libraries/ZUHF_CC1101/ZUHF_VARS.h: -------------------------------------------------------------------------------- 1 | /* 2 | * global vars 3 | */ 4 | #ifndef ZUHF_VARS_H 5 | #define ZUHF_VARS_H 6 | 7 | #define BUFFER_SIZE 255 8 | 9 | /* ********** TAG SPECIFIC - PC MASKS *********** */ 10 | #define L_MASK 0xF800 11 | #define T_MASK 0x0100 12 | #define XI_MASK 0x0200 13 | #define UMI_MASK 0x0400 14 | /* ********************************************** */ 15 | 16 | struct TAG_INFO{ 17 | uint16_t StoredPC = 0; 18 | uint8_t RN16[2] = {}; 19 | uint8_t EPC_Data[12] = {}; // EPC Length actually depends on StoredPC/PacketPC entry 20 | uint8_t CRC16[2] = {}; 21 | bool CRC_OK = false; 22 | }; 23 | 24 | 25 | 26 | enum READER_STATES 27 | { 28 | R_INIT, 29 | R_LOCK, 30 | R_WAIT, 31 | R_START, 32 | R_ACCESS, 33 | R_SELECT, 34 | R_QUERY, 35 | R_QUERYREP, 36 | R_REQRN, 37 | R_READ, 38 | R_WRITE, 39 | R_QUERYADJ, 40 | R_SEARCH_RN16, 41 | R_ACK, 42 | R_NAK, 43 | R_CW, 44 | R_POWERUP, 45 | R_POWERDOWN, 46 | R_READDATA, 47 | R_TEST, 48 | R_QUERY_BAK // old code as backup 49 | }; 50 | 51 | 52 | 53 | // CONSTANTS (READER CONFIGURATION) 54 | 55 | // Fixed number of slots (2^(FIXED_Q)) 56 | const byte FIXED_Q = 0; 57 | 58 | // Query command (Q is set in code) 59 | const byte QUERY_CODE[4] = {1,0,0,0}; // QUERY command 60 | const byte DR = 0; // 0: TRcal divide ratio (8); 1: TRcal divide ratio (64/3) 61 | const byte M[4][2] = {{0,0},{0,1},{1,0},{1,1}}; // cycles per symbol (FM0 encoding = {0,0}) 62 | const byte TREXT = 1; // 1: pilot tone; 0: no pilot tone 63 | const byte SEL_ALL[2] = {0,0}; // which Tags respond to the Query: ALL TAGS {0,0} and {0,1} 64 | const byte SEL_SL[4][2] = {{0,0},{0,1},{1,0},{1,1}}; // which Tags respond to the Query: SELECTED TAGS 65 | const byte SESSION[4][2] = {{0,0},{0,1},{1,0},{1,1}}; // session for the inventory round SL0,SL1,SL2,SL3 respectively 66 | const byte TARGET = 0; // inventoried flag is A or B 67 | 68 | // valid values for Q 69 | const byte Q_VALUE [16][4] = 70 | { 71 | {0,0,0,0}, {0,0,0,1}, {0,0,1,0}, {0,0,1,1}, 72 | {0,1,0,0}, {0,1,0,1}, {0,1,1,0}, {0,1,1,1}, 73 | {1,0,0,0}, {1,0,0,1}, {1,0,1,0}, {1,0,1,1}, 74 | {1,1,0,0}, {1,1,0,1}, {1,1,1,0}, {1,1,1,1} 75 | }; 76 | 77 | const byte MEM_BANK [4][2] = 78 | { 79 | {0,0}, // Reserved 80 | {0,1}, // EPC 81 | {1,0}, // TID 82 | {1,1} // USER 83 | }; 84 | 85 | 86 | /* 87 | * The settings below assume a pulsewidth of 12.5µs (cc1101 datarate at 80 kBaud) 88 | * 1 TARI = 25µs <--> data0 duration = 25µs 89 | */ 90 | const byte DATA0[] = {1,0}; 91 | const byte DATA1[] = {1,1,1,0}; 92 | const byte DELIM[] = {0}; 93 | const byte RTCAL[] = {1,1,1,1,1,0}; // length of (data0 + data1) 94 | const byte TRCAL[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}; // max 3 * length(RTCAL) 16*12.5µs = 200µs --> BLF = 8/200µs = 40kHz (BLF = DR/RTCAL) 95 | 96 | /* 97 | * FRAMESYNC: delim + data0 + rtcal 98 | */ 99 | const byte FRAMESYNC[] = {0,1,0,1,1,1,1,1,0}; // delim + data0 + rtcal 100 | /* 101 | /* 102 | * ACK 103 | */ 104 | const byte ACK[] = {0,1}; 105 | 106 | /* 107 | * REQ_RN 108 | */ 109 | const byte REQ_RN[] = {1,1,0,0,0,0,0,1}; 110 | 111 | /* 112 | * READ 113 | */ 114 | const byte READ_CMD[] = {1,1,0,0,0,0,1,0}; 115 | 116 | /* 117 | * WRITE 118 | */ 119 | const byte WRITE_CMD[] = {1,1,0,0,0,0,1,1}; 120 | 121 | /* 122 | * BLOCKWRITE 123 | */ 124 | const byte BLOCKWRITE_CMD[] = {1,1,0,0,0,1,1,1}; 125 | 126 | /* 127 | * LOCK 11000101 128 | */ 129 | const byte LOCK_CMD[] = {1,1,0,0,0,1,0,1}; 130 | 131 | /* 132 | * ACCESS 11000110 133 | */ 134 | const byte ACCESS_CMD[] = {1,1,0,0,0,1,1,0}; 135 | 136 | /* 137 | * QUERYREP: framesync + 4 x data0 138 | */ 139 | const byte QUERYREP[] = {0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,0}; 140 | 141 | /* 142 | * PREAMBLE: delim + data0 + rtcal + trcal 143 | */ 144 | const byte PREAMBLE[] = {0,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}; 145 | 146 | READER_STATES reader_state = R_START; 147 | 148 | /* 149 | * ERROR CODES 150 | */ 151 | 152 | const String ERRORCODE[] = { 153 | "Other error Catch - all for errors not covered by other codes.", 154 | "Not supported - The Tag does not support the specified parameters or feature.", 155 | "Insufficient privileges - The Interrogator did not authenticate itself with sufficient privileges for the Tag to perform the operation.", 156 | "Memory overrun - The Tag memory location does not exist, is too small, or the Tag does not support the specified EPC length.", 157 | "Memory locked - The Tag memory location is locked or permalocked and is either not writeable1 or not readable.", 158 | "Crypto suite error - Catch-all for errors specified by the cryptographic suite.", 159 | "Command not encapsulated - The Interrogator did not encapsulate the command in an AuthComm or SecureComm as required.", 160 | "ResponseBuffer overflow - The operation failed because the ResponseBuffer overflowed.", 161 | "Security timeout - The command failed because the Tag is in a security timeout.", 162 | "NA", 163 | "NA", 164 | "Insufficient power - The Tag has insufficient power to perform the operation.", 165 | "NA", 166 | "NA", 167 | "NA", 168 | "NA", 169 | "Non-specific error - The Tag does not support error-specific codes" 170 | }; 171 | 172 | #endif 173 | -------------------------------------------------------------------------------- /uhf-rfid_part1/uhf-rfid_part1.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | /* Make sure you are using the DUE specific SPI library */ 9 | /* You need to install the Arduino IDE for WIN10 - do not install one of the hourly builds */ 10 | #include "UHF-RFID.h" 11 | /* ************************ */ 12 | 13 | //#define RN16_LEN 4 // FM0 encoded RN16 length is here 4Bytes 14 | 15 | /* ************* MAIN DEFAULT CONTROL SETTINGS ************* */ 16 | #define TX_POWER 0x16 // 0x11 --> 0x11/0x80 (5.2 dBm) 0x12/0x27 (-9.8 dBm) 0x13/0x67 (-5.0 dBm) 0x14/0x50 (-0.3 dBm) 0x16/0xc0 (9.8 dBm) byte PaTable[8] = {0x00,0x80,0x27,0x67,0x50,0x80,0xc0,0x00}; // 17 | #define REPETITIONS 100 18 | #define AGC2 0x03 19 | #define AGC0 0x90 20 | #define DELAY 200 21 | #define TAG_SETTLE 15 22 | #define CW1 14 // CW after QUERY ~14 * 100us (1.4 ms) 23 | #define CW2 40 // CW after ACK ~40 * 100us (4.0 ms) 24 | /* ******************************************** */ 25 | 26 | /* configuration variables */ 27 | byte tx_power = TX_POWER; 28 | uint32_t repetitions = REPETITIONS; 29 | byte agc2 = AGC2; 30 | byte agc0 = AGC0; 31 | uint32_t packet_delay = DELAY; 32 | 33 | /* others */ 34 | byte tag_settle = TAG_SETTLE; 35 | byte cw1 = CW1; 36 | byte cw2 = CW2; 37 | /* ******************************************** */ 38 | byte tx_version = 0; 39 | int counter = 0; 40 | byte CWA[128]; 41 | String cmd_string = ""; 42 | 43 | void setup() 44 | { 45 | // delay to avoid known reset issue - see also https://forum.arduino.cc/index.php?topic=256771.75 46 | delay(1000); 47 | /* SERIAL CONNECT */ 48 | Serial.begin(250000); 49 | for (int i = 0; i < sizeof(CWA); i++) CWA[i]=0xff; 50 | reader_state = R_INIT; 51 | } 52 | 53 | void loop() 54 | { 55 | switch(reader_state) 56 | { 57 | case R_INIT: 58 | counter = 0; 59 | tx_version = TX_UNIT.Init(); 60 | delay(10); 61 | if ((tx_version == 0x14 or tx_version == 0x04)) 62 | { 63 | Serial.println("[CC1101] Modules Check - OK"); 64 | TX_UNIT.SpiStrobe(CC1101_SFSTXON); 65 | delay(500); 66 | Serial.println("**** START RUN ****"); 67 | reader_state = R_START; 68 | } 69 | else 70 | { 71 | Serial.println("[CC1101] Error on CC1101 module initialization"); 72 | reader_state = R_WAIT; 73 | delay(100); 74 | } 75 | break; 76 | 77 | case R_START: 78 | /* LET FIFOBUFFER EMPTY FROM A PREVIOUS RUN AND SET TX INTO IDLE MODE */ 79 | while ((TX_UNIT.SpiReadStatus(CC1101_TXBYTES) & 0x7f) > 0); 80 | TX_UNIT.SpiStrobe(CC1101_SIDLE); 81 | delay(5); 82 | /* ********** START TX AND SEND CW FOR TAG SETTLE ********** */ 83 | TX_UNIT.SpiStrobe(CC1101_SFTX); 84 | delay(5); 85 | TX_UNIT.SpiStrobe(CC1101_STX); 86 | TX_UNIT.SpiWriteBurstReg(CC1101_TXFIFO, CWA, 20); 87 | /* ********** *********************************** ********** */ 88 | reader_state = R_QUERY; 89 | break; 90 | 91 | case R_QUERY: 92 | send_default_query(); 93 | TX_UNIT.SpiWriteBurstReg(CC1101_TXFIFO, CWA, 40); 94 | reader_state = R_START; 95 | delay(20); 96 | break; 97 | 98 | default: 99 | reader_state = R_INIT; 100 | break; 101 | } 102 | } 103 | --------------------------------------------------------------------------------