Post Go back to editing

Triggered SPI Tx/Rx in ADALM2000 w/ libm2k

Hi, all. I am now trying to get a sensor data through SPI periodically by using m2k with libm2k (python).
This periodic acquisition should be strict, for example few kHz driven by an external clock, not using software timing.
This means, I would like to implement a function that SPI Tx/Rx takes place with the trigger of external clock source (via DIO0 or TI port?)

Does anyone know whether it can be realized by m2k (w/ libm2k) or not?

Thanks

  • Hi,

    We did not actually try this scenario but we gathered some information on how this could work, in theory. Check out the following:
    The first thing to do would be to enable external clocking in the libm2k API:
    There is a configuration in libm2k API for enabling the external clock. You can find an example for external clock here: https://github.com/analogdevicesinc/libm2k/blob/fe56053ee4eed0f7d010dd274b2ad481beef5b9c/bindings/python/examples/external_clocksource.py#L74
    The pin used for this is DIO0.

    Then, there are a few mentions for the SPI configuration:
    For the SPI - you can check this example: https://github.com/analogdevicesinc/libm2k/blob/master/bindings/python/examples/spi.py
    It is important that the m2kSpiInit.clock is set to a different pin from the DIO0, since that one is used for the external clock.
    The spiInitParam.max_speed_hz should be set to 50 MHz.

    Could you let us know if this configuration works for you?
    Thank you!

    -Alexandra

  • Hi,


    Thank you for comments. I tried to run the following code to check a SPI Tx synchronized with an external clock through DIO0.

    import libm2k
    
    # Context setup
    uri="ip:192.168.2.1"
    ctx = libm2k.m2kOpen(uri)
    
    # Digital Setup (DIO0=ExtCLK)
    dig = ctx.getDigital()
    dig.reset()
    dig.setExternalClocksource(True)
    dig.setSampleRateIn(100000000)
    dig.setDirection(0, libm2k.DIO_INPUT)
    dig.enableChannel(0, True)
    
    # SPI setup (DIO1=SCK, DIO2=CS, DIO3=MISO, DIO4=MOSI)
    m2k_spi_init = libm2k.m2k_spi_init()
    m2k_spi_init.clock = 1
    m2k_spi_init.mosi = 3
    m2k_spi_init.miso = 4
    m2k_spi_init.bit_numbering = libm2k.MSB
    m2k_spi_init.cs_polarity = libm2k.ACTIVE_LOW
    m2k_spi_init.context = ctx
    spi_init_param = libm2k.spi_init_param()
    spi_init_param.max_speed_hz = 50000000
    spi_init_param.mode = libm2k.SPI_MODE_0
    spi_init_param.chip_select = 2
    spi_init_param.extra = m2k_spi_init
    spi_desc = libm2k.spi_init(spi_init_param)
    
    # SPI Tx
    data = bytearray([0x4A, 0xF1]) # 0100_1010_1111_0001
    buffer = libm2k.spi_create_buffer(spi_desc, data)
    libm2k.spi_write_and_read_samples(spi_desc, buffer, data)
    
    input("Enter if finished")
    
    libm2k.contextClose(ctx, True)
    libm2k.spi_remove(spi_desc)

    The result is attached as bellow, but only SCK is synced not CS, and SPI Tx was carried out only one time.
    My expectation is also illustrated in the same figure, where the SPI Tx(and Rx) is hopefully done with sync of ExtCLK. This means we can get MISO data periodically (1kS/s) from a sensor.

    Also, I speculate here we may need to buffer the MISO data in a memory, and then dump to a list format in python after finishing acquisition like getSamples method. Is it right?

    Thanks, Ipce