ADRV9009 Platform Software Architecture - ArrowElectronics/arrow-adi-intel-psg GitHub Wiki

Under construction

Software architecture

Introduction

The MitySOM-A10S® SoC FPGA module has dual core Cortex-A9® processors that delivers 5000 DMIPS of performance. The ADRV9009® reference platform runs embedded linux on this platform. Linux provides a rich set of infrastructure for networking, file access and system management. In addition it is a widely supported platform and grants the developer access to a library of system services and existing applications.

User space applications typically do not directly access hardware. This is done via system calls to drivers running in kernel space. The Industrial Input/Output (IIO) framework is used to support access to ADC and DAC devices from within a linux user space application. Analog Devices Inc. has created IIO device drivers to support many of their devices including the AD9009®.

Target boot and device discovery

The target board boots linux from the inserted SD card. The SD card is partitioned into three separate partitions.

  • binary partition which contains the uboot boot loader
  • fat partition which contains the fpga image file, the linux kernel and the devicetree
  • linux partition which contains the root file system

Devicetree and the Embedded System

The devicetree is used to dynamically describe the embedded systems memory map for Linux. Linux reads the devicetree at boot time and uses this information to install device drivers for the peripherals listed there. This includes peripherals in the HPS and FPGA portions of the SoC on the SoM module.

The devicetree for the ADRV9009 platform lists all peripherals in the embedded systems memory map that require the installation of an associated linux driver. It lists the driver name and any pertinent settings that must be passed to the driver.

For example the image below shows the instantiation of the JESD204B peripheral in the FPGA. There are two sections in this peripheral that require separate driver support. These are the link management slave and the link reconfiguration slave. The associated Device Tree entry is shown for the link management and link reconfiguration slaves. Note the reference to the address and drivers for each slave.

The snapshot of the boot log below shows these drivers being loaded and probed by the kernel. The driver probe for the link management slave is shown.

Further examination of the AD9009 ® Device Tree will show all the peripherals and their associated drivers.

One other peripheral is worth investigating. The HPS component in Platform Designer also instantiates a SPI master. This is used to communicate with the AD9258® timing device and the AD9009® transceiver on the ADRV9009® FMC board. At boot time the kernel loads and then probes the associated AD9009 and AD9258 drivers. Examining the driver source code shows that these devices associated iio_dev structures are defined at boot time when the kernel drivers are probed. These iio structures are then made available to user space applications which utilize them at run time. This is shown in the example application below.

The Linux IIO framework

The reader is encouraged to view the following presentation. It provides an overview of the IIO framework. An associated video can be viewed for more detail and a deeper understanding.

A typical Linux User Space application will utilize the Libiio API calls to access the kernel space IIO™ drivers. Example code is included in the Linux repository. It demonstrates how to configure the AD9009® and to stream data to and from the device with the use of buffers.

Devices that were discovered at boot time can be displayed by doing the following

root@analog:~# cd /sys/bus/iio/devices
root@analog:/sys/bus/iio/devices# ls
iio:device0  iio:device1  iio:device2  iio:device3  iio:device4
root@analog:/sys/bus/iio/devices# cat */name
ad9528-1
ad9009-phy
axi-ad9009-rx-obs-hpc
axi-ad9009-tx-hpc
axi-ad9009-rx-hpc

Example code

/*
 * libiio - AD9009 IIO streaming example
 *
 * Copyright (C) 2014 IABG mbH
 * Author: Michael Feilen 
 * Copyright (C) 2017 Analog Devices Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 **/

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>

#ifdef __APPLE__
#include <iio/iio.h>
#else
#include <iio.h>
#endif

/* helper macros */
#define MHZ(x) ((long long)(x*1000000.0 + .5))
#define GHZ(x) ((long long)(x*1000000000.0 + .5))

#define ASSERT(expr) { \
	if (!(expr)) { \
		(void) fprintf(stderr, "assertion failed (%s:%d)\n", __FILE__, __LINE__); \
		(void) abort(); \
	} \
}

/* RX is input, TX is output */
enum iodev { RX, TX };

/* common RX and TX streaming params */
struct stream_cfg {
	long long lo_hz; // Local oscillator frequency in Hz
};

/* static scratch mem for strings */
static char tmpstr[64];

/* IIO structs required for streaming */
static struct iio_context *ctx   = NULL;
static struct iio_channel *rx0_i = NULL;
static struct iio_channel *rx0_q = NULL;
static struct iio_channel *tx0_i = NULL;
static struct iio_channel *tx0_q = NULL;
static struct iio_buffer  *rxbuf = NULL;
static struct iio_buffer  *txbuf = NULL;

static bool stop;

/* cleanup and exit */
static void shutdown()
{
	printf("* Destroying buffers\n");
	if (rxbuf) { iio_buffer_destroy(rxbuf); }
	if (txbuf) { iio_buffer_destroy(txbuf); }

	printf("* Disabling streaming channels\n");
	if (rx0_i) { iio_channel_disable(rx0_i); }
	if (rx0_q) { iio_channel_disable(rx0_q); }
	if (tx0_i) { iio_channel_disable(tx0_i); }
	if (tx0_q) { iio_channel_disable(tx0_q); }

	printf("* Destroying context\n");
	if (ctx) { iio_context_destroy(ctx); }
	exit(0);
}

static void handle_sig(int sig)
{
	printf("Waiting for process to finish...\n");
	stop = true;
}

/* check return value of attr_write function */
static void errchk(int v, const char* what) {
	 if (v < 0) { fprintf(stderr, "Error %d writing to channel \"%s\"\nvalue may not be supported.\n", v, what); shutdown(); }
}

/* write attribute: long long int */
static void wr_ch_lli(struct iio_channel *chn, const char* what, long long val)
{
	errchk(iio_channel_attr_write_longlong(chn, what, val), what);
}

/* write attribute: long long int */
static long long rd_ch_lli(struct iio_channel *chn, const char* what)
{
	long long val;

	errchk(iio_channel_attr_read_longlong(chn, what, &val), what);

	printf("\t %s: %lld\n", what, val);
	return val;
}

#if 0
/* write attribute: string */
static void wr_ch_str(struct iio_channel *chn, const char* what, const char* str)
{
	errchk(iio_channel_attr_write(chn, what, str), what);
}
#endif

/* helper function generating channel names */
static char* get_ch_name_mod(const char* type, int id, char modify)
{
	snprintf(tmpstr, sizeof(tmpstr), "%s%d_%c", type, id, modify);
	return tmpstr;
}

/* helper function generating channel names */
static char* get_ch_name(const char* type, int id)
{
	snprintf(tmpstr, sizeof(tmpstr), "%s%d", type, id);
	return tmpstr;
}

/* returns ad9009 phy device */
static struct iio_device* get_ad9009_phy(struct iio_context *ctx)
{
	struct iio_device *dev =  iio_context_find_device(ctx, "ad9009-phy");
	ASSERT(dev && "No ad9009-phy found");
	return dev;
}

/* finds AD9009 streaming IIO devices */
static bool get_ad9009_stream_dev(struct iio_context *ctx, enum iodev d, struct iio_device **dev)
{
	switch (d) {
	case TX: *dev = iio_context_find_device(ctx, "axi-ad9009-tx-hpc"); return *dev != NULL;
	case RX: *dev = iio_context_find_device(ctx, "axi-ad9009-rx-hpc");  return *dev != NULL;
	default: ASSERT(0); return false;
	}
}

/* finds AD9009 streaming IIO channels */
static bool get_ad9009_stream_ch(struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, char modify, struct iio_channel **chn)
{
	*chn = iio_device_find_channel(dev, modify ? get_ch_name_mod("voltage", chid, modify) : get_ch_name("voltage", chid), d == TX);
	if (!*chn)
		*chn = iio_device_find_channel(dev, modify ? get_ch_name_mod("voltage", chid, modify) : get_ch_name("voltage", chid), d == TX);
	return *chn != NULL;
}

/* finds AD9009 phy IIO configuration channel with id chid */
static bool get_phy_chan(struct iio_context *ctx, enum iodev d, int chid, struct iio_channel **chn)
{
	switch (d) {
	case RX: *chn = iio_device_find_channel(get_ad9009_phy(ctx), get_ch_name("voltage", chid), false); return *chn != NULL;
	case TX: *chn = iio_device_find_channel(get_ad9009_phy(ctx), get_ch_name("voltage", chid), true);  return *chn != NULL;
	default: ASSERT(0); return false;
	}
}

/* finds AD9009 local oscillator IIO configuration channels */
static bool get_lo_chan(struct iio_context *ctx, enum iodev d, struct iio_channel **chn)
{
	switch (d) {
	 // LO chan is always output, i.e. true
	case RX: *chn = iio_device_find_channel(get_ad9009_phy(ctx), get_ch_name("altvoltage", 0), true); return *chn != NULL;
	case TX: *chn = iio_device_find_channel(get_ad9009_phy(ctx), get_ch_name("altvoltage", 1), true); return *chn != NULL;
	default: ASSERT(0); return false;
	}
}

/* applies streaming configuration through IIO */
bool cfg_ad9009_streaming_ch(struct iio_context *ctx, struct stream_cfg *cfg, enum iodev type, int chid)
{
	struct iio_channel *chn = NULL;

	// Configure phy and lo channels
	printf("* Acquiring AD9009 phy %s channel %d\n", type == TX ? "TX" : "RX", chid);
	if (!get_phy_chan(ctx, type, chid, &chn)) {	return false; }

	rd_ch_lli(chn, "rf_bandwidth");
	rd_ch_lli(chn, "sampling_frequency");

	// Configure LO channel
	printf("* Acquiring AD9009 %s lo channel\n", type == TX ? "TX" : "RX");
	if (!get_lo_chan(ctx, type, &chn)) { return false; }
	wr_ch_lli(chn, type == TX ? "TX_LO_frequency" : "RX_LO_frequency" , cfg->lo_hz);
	return true;
}

/* simple configuration and streaming */
int main (int argc, char **argv)
{
	// Streaming devices
	struct iio_device *tx;
	struct iio_device *rx;

	// RX and TX sample counters
	size_t nrx = 0;
	size_t ntx = 0;

	// Stream configurations
	struct stream_cfg rxcfg;
	struct stream_cfg txcfg;

	// Listen to ctrl+c and ASSERT
	signal(SIGINT, handle_sig);

	// RX stream config
	rxcfg.lo_hz = GHZ(2.5); // 2.5 GHz rf frequency

	// TX stream config
	txcfg.lo_hz = GHZ(2.5); // 2.5 GHz rf frequency

	printf("* Acquiring IIO context\n");
	ASSERT((ctx = iio_create_default_context()) && "No context");
	ASSERT(iio_context_get_devices_count(ctx) > 0 && "No devices");

	printf("* Acquiring AD9009 streaming devices\n");
	ASSERT(get_ad9009_stream_dev(ctx, TX, &tx) && "No tx dev found");
	ASSERT(get_ad9009_stream_dev(ctx, RX, &rx) && "No rx dev found");

	printf("* Configuring AD9009 for streaming\n");
	ASSERT(cfg_ad9009_streaming_ch(ctx, &rxcfg, RX, 0) && "RX port 0 not found");
	ASSERT(cfg_ad9009_streaming_ch(ctx, &txcfg, TX, 0) && "TX port 0 not found");

	printf("* Initializing AD9009 IIO streaming channels\n");
	ASSERT(get_ad9009_stream_ch(ctx, RX, rx, 0, 'i', &rx0_i) && "RX chan i not found");
	ASSERT(get_ad9009_stream_ch(ctx, RX, rx, 0, 'q', &rx0_q) && "RX chan q not found");
	ASSERT(get_ad9009_stream_ch(ctx, TX, tx, 0, 0, &tx0_i) && "TX chan i not found");
	ASSERT(get_ad9009_stream_ch(ctx, TX, tx, 1, 0, &tx0_q) && "TX chan q not found");

	printf("* Enabling IIO streaming channels\n");
	iio_channel_enable(rx0_i);
	iio_channel_enable(rx0_q);
	iio_channel_enable(tx0_i);
	iio_channel_enable(tx0_q);

	printf("* Creating non-cyclic IIO buffers with 1 MiS\n");
	rxbuf = iio_device_create_buffer(rx, 1024*1024, false);
	if (!rxbuf) {
		perror("Could not create RX buffer");
		shutdown();
	}
	txbuf = iio_device_create_buffer(tx, 1024*1024, false);
	if (!txbuf) {
		perror("Could not create TX buffer");
		shutdown();
	}

	printf("* Starting IO streaming (press CTRL+C to cancel)\n");
	while (!stop)
	{
		ssize_t nbytes_rx, nbytes_tx;
		char *p_dat, *p_end;
		ptrdiff_t p_inc;

		// Schedule TX buffer
		nbytes_tx = iio_buffer_push(txbuf);
		if (nbytes_tx < 0) { printf("Error pushing buf %d\n", (int) nbytes_tx); shutdown(); }

		// Refill RX buffer
		nbytes_rx = iio_buffer_refill(rxbuf);
		if (nbytes_rx < 0) { printf("Error refilling buf %d\n",(int) nbytes_rx); shutdown(); }

		// READ: Get pointers to RX buf and read IQ from RX buf port 0
		p_inc = iio_buffer_step(rxbuf);
		p_end = iio_buffer_end(rxbuf);
		for (p_dat = iio_buffer_first(rxbuf, rx0_i); p_dat < p_end; p_dat += p_inc) {
			// Example: swap I and Q
			const int16_t i = ((int16_t*)p_dat)[0]; // Real (I)
			const int16_t q = ((int16_t*)p_dat)[1]; // Imag (Q)
			((int16_t*)p_dat)[0] = q;
			((int16_t*)p_dat)[1] = i;
		}

		// WRITE: Get pointers to TX buf and write IQ to TX buf port 0
		p_inc = iio_buffer_step(txbuf);
		p_end = iio_buffer_end(txbuf);
		for (p_dat = iio_buffer_first(txbuf, tx0_i); p_dat < p_end; p_dat += p_inc) {
			// Example: fill with zeros
			// 14-bit sample needs to be MSB alligned so shift by 2
			// https://wiki.analog.com/resources/eval/user-guides/ad-fmcomms2-ebz/software/basic_iq_datafiles#binary_format
			((int16_t*)p_dat)[0] = 0 << 2; // Real (I)
			((int16_t*)p_dat)[1] = 0 << 2; // Imag (Q)
		}

		// Sample counter increment and status output
		nrx += nbytes_rx / iio_device_get_sample_size(rx);
		ntx += nbytes_tx / iio_device_get_sample_size(tx);
		printf("\tRX %8.2f MSmp, TX %8.2f MSmp\n", nrx/1e6, ntx/1e6);
	}

	shutdown();

	return 0;
}

This code can be built and run on a connected host or locally on the target.

Build code on target

The target file system includes a native compiler and the necessary iio and libiio code. Use a serial terminal to connect to the target.

  • compile the code

      $ cd /usr/local/src/libiio/examples
      $ make ad9009-iiostream.c
    
  • run the code

      $ ./ad9009-iiostream.c
    

Build code on the provided Linux VM

  • compile the code

      $ cd ~/ad9009/libiio/examples
      $ make ad9009-iiostream.c
    
  • declare the target's IP address to the IIO daemon for remote access

      $ export IIOD_REMOTE 192.168.0.2
    
  • run the code

      $ ./ad9009-iiostream.c
    

Return to Platform Architecture Table of Contents

⚠️ **GitHub.com Fallback** ⚠️