File size: 1,998 Bytes
cb65407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# This file is public domain, it can be freely copied without restrictions.
# SPDX-License-Identifier: CC0-1.0

import cocotb
from afe import AFE
from cocotb.clock import Clock
from cocotb.queue import Queue
from cocotb.triggers import Edge, RisingEdge, Timer

"""
This example uses the Python model of an Analog Front-End (AFE)
which contains a Programmable Gain Amplifier (PGA)
and an Analog-to-Digital Converter (ADC).

The digital part (in HDL) monitors the measurement value converted by the ADC
and selects the gain of the PGA based on the received value.
"""


async def gain_select(digital, afe) -> None:
    """Set gain factor of PGA when gain select from the HDL changes."""

    while True:
        await Edge(digital.pga_high_gain)
        if digital.pga_high_gain.value == 0:
            afe.pga.gain = 5.0
        else:
            afe.pga.gain = 10.0


@cocotb.test()
async def test_analog_model(digital) -> None:
    """Exercise an Analog Front-end and its digital controller."""

    clock = Clock(digital.clk, 1, units="us")  # create a 1us period clock on port clk
    cocotb.start_soon(clock.start())  # start the clock

    afe_in_queue = Queue()
    afe_out_queue = Queue()
    afe = AFE(
        in_queue=afe_in_queue, out_queue=afe_out_queue
    )  # instantiate the analog front-end

    cocotb.start_soon(gain_select(digital, afe))

    for in_V in [0.1, 0.1, 0.0, 0.25, 0.25]:
        # set the input voltage
        await afe_in_queue.put(in_V)

        # get the converted digital value
        afe_out = await afe_out_queue.get()

        digital._log.info(f"AFE converted input value {in_V}V to {int(afe_out)}")

        # hand digital value over as "meas_val" to digital part (HDL)
        # "meas_val_valid" pulses for one clock cycle
        await RisingEdge(digital.clk)
        digital.meas_val.value = afe_out
        digital.meas_val_valid.value = 1
        await RisingEdge(digital.clk)
        digital.meas_val_valid.value = 0
        await Timer(3.3, "us")