~ruther/vhdl-spi-2

ref: aab4f8c17b621ba6592449becc2e5a51c6ac9f6c vhdl-spi-2/hdl_spi/tests/test_spi_peripheral.py -rw-r--r-- 6.0 KiB
aab4f8c1 — Rutherther feat(stm): implement proper slave initialization 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
import sys
from pathlib import Path
import logging
import random

from dataclasses import dataclass

import cocotb
import cocotb.handle
from cocotb.queue import Queue
from cocotb.clock import Clock
from cocotb.triggers import Trigger, First, Event, Timer, RisingEdge, Edge, FallingEdge
from cocotb_tools.runner import get_runner

if cocotb.simulator.is_running():
    from spi_models import SpiInterface, SpiConfig, SpiSlave

ADDR_CTRL = 0
ADDR_INTMASK = 1
ADDR_STATUS = 2
ADDR_DATA = 3

INTMASK_RX_BUFFER_FULL = 0
INTMASK_TX_BUFFER_EMPTY = 1
INTMASK_LOST_RX_DATA = 3

STATUS_RX_BUFFER_FULL = 0
STATUS_TX_BUFFER_EMPTY = 1
STATUS_BUSY = 2
STATUS_LOST_RX_DATA = 3

CTRL_EN = 0
CTRL_MASTER = 1
CTRL_TX_EN = 2
CTRL_RX_EN = 3
CTRL_CLOCK_POLARITY = 4
CTRL_CLOCK_PHASE = 5
CTRL_PULSE_CSN = 6
CTRL_LSBFIRST = 7
CTRL_SIZE_SEL = 10
CTRL_DIV_SEL = 20

async def init(dut, master: int = 1, tx_en: int = 1):
    dut._log.info("Init started!")
    dut.waddress_i.value = 0
    dut.raddress_i.value = 0
    dut.wdata_i.value = 0
    dut.write_i.value = 0
    dut.read_i.value = 0
    dut.rst_in.value = 0

    await FallingEdge(dut.clk_i)
    await FallingEdge(dut.clk_i)

    # Release reset
    dut.rst_in.value = 1

    await FallingEdge(dut.clk_i)
    await FallingEdge(dut.clk_i)

    dut._log.info("Init done!")

class DutDriver:
    def __init__(self, dut):
        self.dut = dut
        self.irq_handlers = []

    async def clear_lost_rx_data(self):
        await self.write(ADDR_INTMASK, INTMASK_LOST_RX_DATA)

    async def configure_intmask(self, rx_buffer_full, tx_buffer_full, lost_rx_data):
        await self.write(ADDR_INTMASK,
                         (rx_buffer_full << INTMASK_RX_BUFFER_FULL) |
                         (tx_buffer_full << INTMASK_TX_BUFFER_FULL) |
                         (lost_rx_data << INTMASK_LOST_RX_DATA))

    async def configure(self, en, master, tx_en, rx_en, clock_polarity, clock_phase, pulse_csn, lsbfirst, size_sel, div_sel):
        await self.write(ADDR_CTRL,
                   (en << CTRL_EN) |
                   (master << CTRL_MASTER) |
                   (tx_en << CTRL_TX_EN) |
                   (rx_en << CTRL_RX_EN) |
                   (clock_polarity << CTRL_CLOCK_POLARITY) |
                   (clock_phase << CTRL_CLOCK_PHASE) |
                   (lsbfirst << CTRL_LSBFIRST) |
                   (size_sel << CTRL_SIZE_SEL) |
                   (div_sel << CTRL_DIV_SEL))

    async def read(self, address):
        await FallingEdge(self.dut.clk_i)
        self.dut.raddress_i.value = address
        self.dut.read_i.value = 1
        await FallingEdge(self.dut.clk_i)
        self.dut.read_i.value = 0
        return self.dut.rdata_o.value

    async def write(self, address, data):
        await FallingEdge(self.dut.clk_i)
        self.dut.waddress_i.value = address
        self.dut.wdata_i.value = data
        self.dut.write_i.value = 1
        await FallingEdge(self.dut.clk_i)
        self.dut.write_i.value = 0

    def register_interrupt_handler(self, handler):
        self.irq_handlers.push(handler)

    async def coroutine(self):
        await RisingEdge(self.dut.interrupt_o)

        if self.irq_handlers.len() == 0:
            raise Exception("Got interrupt but there is no irq handler. This would be a dead loop.")

        # The handlers are called until interrupt goes down!
        while self.dut.interrupt_o.value == 1:
            for irq_handler in irq_handlers:
                irq_handler(self)

@cocotb.test()
async def single_transission(dut):
    clk = Clock(dut.clk_i, 5, "ns")
    interface = SpiInterface(dut.csn_o, dut.sck_o, dut.miso_i, dut.mosi_o)
    config = SpiConfig(16, RisingEdge, FallingEdge, 40, "ns")
    slave = SpiSlave(interface, config)
    driver = DutDriver(dut)

    await cocotb.start(clk.start())

    await init(dut)

    await cocotb.start(slave.coroutine())
    await cocotb.start(driver.coroutine())

    await driver.configure(
        en = 1,
        master = 1,
        tx_en = 1,
        rx_en = 1,
        clock_polarity = 1,
        clock_phase = 1,
        pulse_csn = 0,
        lsbfirst = 0,
        size_sel = 1,
        div_sel = 2)

    # From slave point of view
    rx = random.randint(0, 255)
    tx = random.randint(0, 255)

    await slave.send_data(tx, 16)
    await slave.expect_transaction_in(100, "ns")

    await driver.write(ADDR_DATA, rx)

    await slave.wait_all()

    await FallingEdge(dut.clk_i)

    dut_received = await driver.read(ADDR_DATA)
    assert (int(dut_received) & 0xFF) == tx

    received = await slave.received_data()
    assert int(received) & 0xFF == rx

    await Timer(100, "ns")


def spi_peripheral_tests_runner():
    hdl_toplevel_lang = "vhdl"
    sim = os.getenv("SIM", "questa")

    proj_path = Path(__file__).resolve().parent.parent
    # equivalent to setting the PYTHONPATH environment variable
    sys.path.append(str(proj_path / "models"))

    sources = [
        proj_path / "src" / "spi_pkg.vhd",
        proj_path / "src" / "rs_latch.vhd",
        proj_path / "src" / "register.vhd",
        proj_path / "src" / "shift_register.vhd",
        proj_path / "src" / "spi_clkgen.vhd",
        proj_path / "src" / "spi_clkmon.vhd",
        proj_path / "src" / "spi_slave_ctrl.vhd",
        proj_path / "src" / "spi_master_ctrl.vhd",
        proj_path / "src" / "spi_master.vhd",
        proj_path / "src" / "spi_masterslave.vhd",
        proj_path / "src" / "spi_peripheral.vhd"
    ]

    build_args = []
    extra_args = []
    if sim == "ghdl":
        extra_args = ["--std=08"]
    elif sim == "questa" or sim == "modelsim":
        build_args = ["-2008"]

    # equivalent to setting the PYTHONPATH environment variable
    sys.path.append(str(proj_path / "tests"))

    runner = get_runner(sim)
    runner.build(
        sources=sources,
        hdl_toplevel="spi_peripheral",
        always=True,
        build_args=build_args + extra_args,
    )
    runner.test(
        hdl_toplevel="spi_peripheral", hdl_toplevel_lang=hdl_toplevel_lang,
        test_module="test_spi_peripheral", test_args = extra_args,
        gui = True
    )


if __name__ == "__main__":
    spi_peripheral_tests_runner()
Do not follow this link