1 # This file is Copyright (c) 2019 Florent Kermarrec <florent@enjoy-digital.fr>
8 from litex
.soc
.cores
.spi
import SPIMaster
, SPISlave
11 class TestSPI(unittest
.TestCase
):
12 def test_spi_master_syntax(self
):
13 spi_master
= SPIMaster(pads
=None, data_width
=32, sys_clk_freq
=100e6
, spi_clk_freq
=5e6
)
14 self
.assertEqual(hasattr(spi_master
, "pads"), 1)
16 def test_spi_master_xfer_loopback_32b_32b(self
):
18 yield dut
.loopback
.eq(1)
19 yield dut
.clk_divider
.eq(2)
20 yield dut
.mosi
.eq(0xdeadbeef)
21 yield dut
.length
.eq(32)
26 while (yield dut
.done
) == 0:
29 self
.assertEqual(hex((yield dut
.miso
)), hex(0xdeadbeef))
31 dut
= SPIMaster(pads
=None, data_width
=32, sys_clk_freq
=100e6
, spi_clk_freq
=5e6
, with_csr
=False)
32 run_simulation(dut
, generator(dut
))
34 def test_spi_master_xfer_loopback_32b_16b(self
):
36 yield dut
.loopback
.eq(1)
37 yield dut
.mosi
.eq(0xbeef)
38 yield dut
.length
.eq(16)
43 while (yield dut
.done
) == 0:
46 self
.assertEqual(hex((yield dut
.miso
)), hex(0xbeef))
48 dut
= SPIMaster(pads
=None, data_width
=32, sys_clk_freq
=100e6
, spi_clk_freq
=5e6
, with_csr
=False, mode
="aligned")
49 run_simulation(dut
, generator(dut
))
51 def test_spi_slave_syntax(self
):
52 spi_slave
= SPISlave(pads
=None, data_width
=32)
53 self
.assertEqual(hasattr(spi_slave
, "pads"), 1)
55 def test_spi_slave_xfer(self
):
58 pads
= Record([("clk", 1), ("cs_n", 1), ("mosi", 1), ("miso", 1)])
59 self
.submodules
.master
= SPIMaster(pads
, data_width
=32,
60 sys_clk_freq
=100e6
, spi_clk_freq
=5e6
,
62 self
.submodules
.slave
= SPISlave(pads
, data_width
=32)
64 def master_generator(dut
):
67 yield dut
.master
.mosi
.eq(0xdeadbeef)
68 yield dut
.master
.length
.eq(32)
69 yield dut
.master
.start
.eq(1)
71 yield dut
.master
.start
.eq(0)
73 while (yield dut
.master
.done
) == 0:
76 self
.assertEqual(hex((yield dut
.master
.miso
)), hex(0x12345678))
78 def slave_generator(dut
):
81 yield dut
.slave
.miso
.eq(0x12345678)
82 while (yield dut
.slave
.start
) == 0:
84 while (yield dut
.slave
.done
) == 0:
87 self
.assertEqual(hex((yield dut
.slave
.mosi
)), hex(0xdeadbeef))
88 self
.assertEqual((yield dut
.slave
.length
), 32)
91 run_simulation(dut
, [master_generator(dut
), slave_generator(dut
)])