import spidev
from libpmux import *
import gpiod

#Manually control the chip select line to do larger transactions than 231 bytes. 
chip = gpiod.chip("gpiochip0")
cs = chip.get_line(PL_IO.NUM_9)

config = gpiod.line_request()
config.consumer = "SPI_CS_MANUAL"
config.request_type = gpiod.line_request.DIRECTION_OUTPUT
cs.request(config,1)
cs.set_value(1)

p=pmux()                                    
#Let Peripheral manage enable line. Good for small transactions < 231 bytes. 
#p.set(pmux_type.SPI,0,pmux_pin_func.SPI_EN, PL_IO.NUM_9)    
p.set(pmux_type.SPI,0,pmux_pin_func.SPI_CLK, PL_IO.NUM_8)   
p.set(pmux_type.SPI,0,pmux_pin_func.SPI_MOSI, PL_IO.NUM_0)    
p.set(pmux_type.SPI,0,pmux_pin_func.SPI_MISO, PL_IO.NUM_16)   


spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 500000   #Clock can be set from 500Khz to 25Mhz 
spi.bits_per_word=8         #The driver supports 8 bits pwer word only
to_send = []
for x in range(0,2048):
    to_send += (x).to_bytes(2, byteorder='big')
print(to_send)
cs.set_value(0)
print(spi.xfer(to_send))
cs.set_value(1)

