import spidev import time import Adafruit_BBIO.GPIO as GPIO CS_PIN = 'P9_17' CLK_PIN = 'P9_22' MOSI_PIN = 'P9_18' MISO_PIN = 'P9_21' GPIO.setup(CS_PIN, GPIO.IN) GPIO.setup(CLK_PIN, GPIO.IN) GPIO.setup(MOSI_PIN, GPIO.IN) GPIO.setup(MISO_PIN, GPIO.OUT) spi = spidev.SpiDev() spi.open(0, 0) spi.mode = 0 # Set SPI mode to 0 (CPOL=0, CPHA=0) spi.max_speed_hz = 500000 def receive_byte(): byte = 0x0000 for _ in range(15): # Wait for clock signal to go high while GPIO.input(CLK_PIN) == GPIO.LOW: pass # Wait for clock signal to fall low while GPIO.input(CLK_PIN) == GPIO.HIGH: pass # Read MISO_PIN state if GPIO.input(MOSI_PIN): byte |= 0x0001 # Shift byte left by 1 bit byte <<= 1 time.sleep(0.0001) # Wait for clock signal to fall low before reading the 16th bit while GPIO.input(CLK_PIN) == GPIO.HIGH: pass # Read MISO_PIN state for the 16th bit if GPIO.input(MOSI_PIN): byte |= 0x0001 return byte try: while True: if GPIO.input(CS_PIN) == GPIO.LOW: received_data = [] while True: # Continue receiving data until a condition is met received_byte = receive_byte() received_data.append(received_byte) print(f"Received byte: 0x{received_byte:04X}") except KeyboardInterrupt: pass # Graceful exit on Ctrl+C spi.close() GPIO.cleanup()