SPI Bus

The SPI bus interface allows you to decode traffic sent over the SPI MOSI and MISO lines. In order to decode the traffic, a number of parameters need to be set based on your knowledge how the SPI bus should be operating.

Creating an SPI Bus

To create an SPI bus, a sck (clock) net, mosi (data) net, miso (data) net, and cs (chip select, optional) net need to be instantiated.

create_spi_bus.py
 from lager import Net, NetType, SPI

 mosi = Net.get('SPI.MOSI',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 miso = Net.get('SPI.MISO',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 sck = Net.get('SPI.SCK',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 cs = Net.get('SPI.CS',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)
 spi.enable()
Create SPI Bus
~  lager bus spi --source-sck SPI.SCK --source-mosi SPI.MOSI --source-miso SPI.MISO --source-cs SPI.CS --dut 1

Data Width

The data width of a SPI packet can vary. This allows you to set the assumed data width for data transmissions.

set_spi_data_width.py
 from lager import Net, NetType, SPI

 mosi = Net.get('SPI.MOSI',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 miso = Net.get('SPI.MISO',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 sck = Net.get('SPI.SCK',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 cs = Net.get('SPI.CS',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)
 spi.set_data_width(8)
Set Spi Data Width
~  lager bus spi --data-width 8 --dut 1

Polarity

This sets whether the MOSI, MISO, and CS lines are active HIGH or LOW.

set_spi_polarity.py
 from lager import Net, NetType, SPI

 mosi = Net.get('SPI.MOSI',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 miso = Net.get('SPI.MISO',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 sck = Net.get('SPI.SCK',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 cs = Net.get('SPI.CS',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)
 spi.set_signal_polarity(mosi=1,miso=1,cs=1) #1 is active High
Set SPI Polarity
~  lager bus spi --pol-mosi pos --pol-miso pos --pol-cs pos --dut 1

Clock Phase

This sets the assumed phase of the clock, whether it samples data on the rising edge or falling edge.

set_spi_clk_phase.py
 from lager import Net, NetType, SPI

 mosi = Net.get('SPI.MOSI',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 miso = Net.get('SPI.MISO',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 sck = Net.get('SPI.SCK',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 cs = Net.get('SPI.CS',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)
 spi.set_sck_phase_rising_edge()
Set SPI Clock Phase
~  lager bus spi --pha-sck rising --dut 1

Endianness

This sets the assumed endiannes of the SPI Bus signals. Typically this will be MSB (most significant bit).

set_spi_endianness.py
 from lager import Net, NetType, SPI

 mosi = Net.get('SPI.MOSI',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 miso = Net.get('SPI.MISO',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 sck = Net.get('SPI.SCK',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 cs = Net.get('SPI.CS',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)
 spi.set_endianness_msb()
Set SPI Bus Endianness
~  lager bus spi --endianness msb --dut 1

Capture Mode

Capture more determines if data should be captured based on a timeout after a SPI transmission starts or based on the CS Line going active and then inactive.

Timeout

set_spi_capture_timeout.py
 from lager import Net, NetType, SPI

 mosi = Net.get('SPI.MOSI',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 miso = Net.get('SPI.MISO',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 sck = Net.get('SPI.SCK',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 cs = Net.get('SPI.CS',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)
 spi.set_capture_mode_timeout(.0001)
Set SPI Bus Capture Timeout
~  lager bus spi --capture timeout --timeout .0001 --dut 1

CS

set_spi_capture_timeout.py
 spi.set_capture_mode_cs(.0001)
Set SPI Bus Capture CS
~  lager bus spi --capture cs --dut 1

Signal Threshold

This sets the assumed voltage threshold a rising or falling SPI signal should pass through.

set_spi_threshold.py
 from lager import Net, NetType, SPI

 mosi = Net.get('SPI.MOSI',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 miso = Net.get('SPI.MISO',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 sck = Net.get('SPI.SCK',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 cs = Net.get('SPI.CS',
         type=NetType.Logic,
         setup_function=setup_nets,
         teardown_function=teardown_nets)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)

 spi = SPI(sck=sck,mosi=mosi,miso=miso,cs=cs)
 spi.set_signal_threshold(mosi=1.5,miso=1.5,sck=1.5,cs=1.5)
Set SPI Bus Threshold
~  lager bus spi --level-sck 1.5 --level-mosi 1.5 --level-miso 1.5 --level-cs 1.5 --dut 1