diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fed3f81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +saved_packets/* +src/__pycache__ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index c1860fa..8ed7c59 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - + "python.analysis.stubPath": "./typings" } \ No newline at end of file diff --git a/README.md b/README.md index 63ab42b..c1dbca9 100644 --- a/README.md +++ b/README.md @@ -106,4 +106,4 @@ The known frequency range is 2400 – 2483.5 MHz, and my SDR has a 20 MHZ bandwi ## Run -`python3 read.py` \ No newline at end of file +`python3 -m src.main` \ No newline at end of file diff --git a/__pycache__/init_regs_value.cpython-311.pyc b/__pycache__/init_regs_value.cpython-311.pyc deleted file mode 100644 index b3c4f18..0000000 Binary files a/__pycache__/init_regs_value.cpython-311.pyc and /dev/null differ diff --git a/__pycache__/regs_addr.cpython-311.pyc b/__pycache__/regs_addr.cpython-311.pyc deleted file mode 100644 index f937fb2..0000000 Binary files a/__pycache__/regs_addr.cpython-311.pyc and /dev/null differ diff --git a/__pycache__/util.cpython-311.pyc b/__pycache__/util.cpython-311.pyc deleted file mode 100644 index 8470d9c..0000000 Binary files a/__pycache__/util.cpython-311.pyc and /dev/null differ diff --git a/freq.txt b/freq.txt deleted file mode 100644 index 6d3f457..0000000 --- a/freq.txt +++ /dev/null @@ -1,12 +0,0 @@ -Handheld Init -2.4312 GHZ (mid) -2.439256 (strong) -2.447 GHZ (strong) -2.4632 GHZ (strong) -2.471 GHZ (weak) - -Test Pts -2410000.000 -2430000.000 -2450000.000 -2470000.000 \ No newline at end of file diff --git a/src/common.py b/src/common.py index ee5b570..1845ffb 100644 --- a/src/common.py +++ b/src/common.py @@ -83,7 +83,7 @@ def reset(spi): version = read_register(spi, VERSION) print(f"CC2500 VERSION register: 0x{version:02X}") # if version != 0x2F: - # raise Exception("Expected Version was 0x0F!! Quitting") + # raise Exception("Expected Version was 0x2F!! Quitting") def test_read_write_reg(spi, dbg=False): diff --git a/src/receive.py b/src/receive.py index d8a3ba5..b18875f 100644 --- a/src/receive.py +++ b/src/receive.py @@ -1,3 +1,5 @@ +from datetime import datetime +from pathlib import Path from .util import sleep, get_addr, digital_read, GDO0_PIN, GDO2_PIN, debug, delay import time from .common import strobe, read_register @@ -12,9 +14,21 @@ def print_packet(spi, packet_length): for i in range(packet_length): print(f", byte: {0}: 0x{1}", i, read_register(spi, RXFIFO)) -#TODO -def save_packet(): - print() +# Save packet to a timestamped binary file +def save_packet(packet: list): + # Create directory to store packets if it doesn't exist + packet_dir = Path("saved_packets") + packet_dir.mkdir(parents=True, exist_ok=True) + + # Create timestamped filename + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + filename = packet_dir / f"packet_{timestamp}.bin" + + # Save packet as binary data + with open(filename, "wb") as f: + f.write(bytearray(packet)) + + print(f"Packet saved to {filename}") def get_rssi_from_pkt(packet): @@ -61,57 +75,13 @@ def rx_data_rf(spi): gdo2_state = digital_read(GDO2_PIN) delay(100) packet_length: int = read_register(spi, RXFIFO) - # print("Packet Length {0}".format(packet_length)) packet: list = [read_register(spi, RXFIFO) for _ in range(packet_length)] rssi_raw = get_rssi_from_pkt(packet) strength = get_signal_strength_rssi_raw(rssi_raw) print("Length: {0} bytes\t Signal: {1} dBm".format(packet_length, strength) ) - + save_packet(packet) # Make sure that the radio is in IDLE state before flushing the FIFO # (Unless RXOFF_MODE has been changed, the radio should be in IDLE state at this point) strobe(spi, SIDLE) # Flush RX FIFO strobe(spi, SFRX) - -# def burst_read(spi, addr, length): -# """Read multiple bytes""" -# READ_SINGLE = get_addr("READ_SINGLE") -# READ_BURST = get_addr("READ_BURST") -# return spi.xfer2([addr | READ_SINGLE | READ_BURST] + [0x00] * length) - - - -# def read_fifo(spi): -# # Burst read RX FIFO -# READ_BURST = get_addr("READ_BURST") -# RXFIFO = get_addr("RXFIFO") -# fifo = spi.xfer2([RXFIFO | READ_BURST] + [0x00]*64) # Max 64 bytes -# return fifo[1:] - - -# def receive_packet(spi): -# SFRX = get_addr("SFRX") -# RXBYTES = get_addr("RXBYTES") -# SRX = get_addr("SRX") - -# # Flush RX FIFO -# strobe(spi, SFRX) -# time.sleep(0.5) # 1 ms delay to allow flush -# # Go into RX mode -# strobe(spi, SRX) - -# # Wait for data (use GDO0 in real app) -# sleep(0.5) - -# # Check RXBYTES - -# timeout = time.time() + 2 # 2-second timeout -# while time.time() < timeout: -# rx_bytes = read_register(spi, RXBYTES) & 0x7F -# if rx_bytes > 0: -# data = read_fifo(spi) -# print(f"Received: {data}") -# return data -# time.sleep(0.01) - -# print("Timeout: no data received.") diff --git a/src/util.py b/src/util.py index f6852a1..bd5baa9 100644 --- a/src/util.py +++ b/src/util.py @@ -19,14 +19,7 @@ def print_gdo_state(GDO0_PIN=17, GDO2_PIN=27): sleep(0.1) def digital_read(GDO_PIN: int): - bit = GPIO.input(GDO_PIN) - # print("bit " + str(bit)) - if bit == 1: - return True - elif bit == 0: - return False - else: - raise Exception("Unexpected digital read GDO_PIN{GDO_PIN} {bit}") + return GPIO.input(GDO_PIN) def get_addr(name): addr = "" diff --git a/typings/spidev/__init__.pyi b/typings/spidev/__init__.pyi new file mode 100644 index 0000000..fbdbaae --- /dev/null +++ b/typings/spidev/__init__.pyi @@ -0,0 +1,6 @@ +class SpiDev: + def open(self, bus: int, device: int) -> None: ... + def close(self) -> None: ... + def xfer2(self, data: list[int]) -> list[int]: ... + max_speed_hz: int + mode: int \ No newline at end of file diff --git a/wiring.txt b/wiring.txt deleted file mode 100644 index 6bc9535..0000000 --- a/wiring.txt +++ /dev/null @@ -1,31 +0,0 @@ -CC2500 Pins to Raspberry Pi GPIO Pins -GND (CC2500) → Ground (Pin 6 on Raspberry Pi GPIO header) - -VCC (CC2500) → 3.3V Power (Pin 1 or Pin 17 on Raspberry Pi GPIO header) - -Ensure the CC2500 module operates at 3.3V. Connecting it to 5V may damage it. - -SPI Pins: -SI (CC2500) → MOSI (GPIO 10) (Pin 19 on Raspberry Pi GPIO header) - -SCK (CC2500) → SCLK (GPIO 11) (Pin 23 on Raspberry Pi GPIO header) - -CSN (CC2500) → CE0 (GPIO 8) (Pin 24 on Raspberry Pi GPIO header) - -This is the Chip Select line. - -SO (CC2500) → MISO (GPIO 9) (Pin 21 on Raspberry Pi GPIO header) - -Optional Pins: -GDO0 (CC2500) → Any GPIO Pin (e.g., GPIO 18) (Pin 12 on Raspberry Pi GPIO header) - -Use this for interrupt handling or data-ready signals if required. - -GDO2 (CC2500) → Any GPIO Pin (e.g., GPIO 25) (Pin 22 on Raspberry Pi GPIO header) - -Additional interrupt or data pin. - -Power Amplifier Pins (If Applicable): -PA_EN (CC2500) → Any GPIO Pin (Optional, depending on whether you control the amplifier externally or not). - -PA_EX (CC2500) → Not Connected (unless needed for a specific configuration) \ No newline at end of file