Skip to content

Instantly share code, notes, and snippets.

@mattvenn
Created October 30, 2020 15:23
Show Gist options
  • Save mattvenn/33dc041357e0912dc759461391e244c7 to your computer and use it in GitHub Desktop.
Save mattvenn/33dc041357e0912dc759461391e244c7 to your computer and use it in GitHub Desktop.
class CocoSPI()
def __init__(self, dut):
super().__init__()
self.dut = dut
# duplicates the behaviour of real spi driver
async def xfer(self, data):
await ClockCycles(self.dut.clk, SPI_CLOCK_PERIODS)
return_data = [0] * len(data)
for bit in range(8):
self.dut.spi_copi <= data[bit]
return_data[bit] = self.dut.spi_cipo
return return_data
class SPIDriver():
def __init__():
# real hardware
self.spi = SpiDev.spi(0)
def read_register(self, reg)
self.spi.xfer(reg)
data = self.spi.xfer(0)
# do some data processing on data before returning it
val = struct.unpack('>B', data)
return val
class CocoSPIDriver(SPIDriver):
def __init__(dut):
# simulated hardware
self.dut = dut
self.spi = CocoSPI(dut)
# I want to avoid duplicating all the super class methods
async def read_register(self, reg)
self.spi.xfer(reg)
data = await self.spi.xfer(0)
# do some data processing on data before returning it
val = struct.unpack('>B', data)
return val
# simulated test
@cocotb.test()
async def test_read(dut):
clock = Clock(dut.clk, CLOCK_PERIOD, units="ns")
cocotb.fork(clock.start()) # Start the clock
spi = CocoSPIDriver(dut)
value = await spi.read_register(0x40)
assert value == 10
# hardware in the loop test
if __name__ == '__main__':
spi = SPIDriver()
value = spi.read_register(0x40)
assert value == 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment