AD5593r- I2C connectivity

I am trying to use the AD5593r driver provided at   https://wiki.analog.com/resources/tools-software/uc-drivers/ad5592r. However, I am unable to properly initialise the device driver. Would you be so kind to provide me with a code snipped to initialise the driver? Please.

Top Replies

  • 0
    •  Analog Employees 
    on Jul 24, 2019 8:52 AM

    Hey,

    The driver for AD5593r is:

    github.com/.../ad5593r.c

    Typically what you would need to do first, is make sure that the I2C code initializes correctly and you can read/write with I2C, before calling ad5593r_init().

    After calling ad5593r_init() you should be able to use ad5593r_read_adc() & ad5593r_write_dac() to talk to the chip.

    Thanks

    Alex

  • +1
    •  Analog Employees 
    on Jul 24, 2019 8:56 AM

    Hello Raul,

    I think the simplest way to do this is to just instantiate the handler structure and make sure to assign the ops pointer and initialize the phy, in this case I2C, as FormerMember pointed out.

    Here is a code snippet that might help out.

    /*****************************************************************************
     * ad5593r_quick_init.c
     *****************************************************************************/
    
    #include <sys/platform.h> /* Platform reliant, not part of this example */
    #include "adi_initialize.h" /* Platform reliant, not part of this example */
    #include "ad5593r.h" /* Include the specific driver functions */
    #include "ad5592r-base.h" /* Include the base driver */
    
    #define AD5593R_A0_STATE 0 /* Assumed the A0 pin state is LOW. Change to '1' if HIGH. */
    
    extern const struct ad5592r_rw_ops ad5593r_rw_ops; /* This is found in the specific driver */
    
    int main(int argc, char *argv[])
    {
    	/**
    	 * Initialize the device handler. Most of the structure members can be
    	 * assigned later as needed, bit the ops member must be initialized, because
    	 * it is labeled 'const'. However, if there is need to assign it later
    	 * it can be done by having a temporary struct ad5592r_rw_ops pointer and
    	 * doing the following:
    	 *
    	 * struct ad5592r_rw_ops *temp;
    	 *
    	 * temp = (void *)ad5593r_handle.ops;
    	 * temp->write_dac = new_function_pointer;
    	 *
    	 * Doing this can circumvent the 'const' label and allow modification.
    	 */
    	struct ad5592r_dev ad5593r_handle = {
    			.ops = &ad5593r_rw_ops
    	};
    
    	/**
    	 * The initialization structure contains only one parameter. It is used to
    	 * decide if the internal reference is used or not. Set accordingly. Here it
    	 * is assumed that the internal reference is used.
    	 */
    	struct ad5592r_init_param ad5593r_init_param = {
    			.int_ref = true
    	};
    
    	/**
    	 * The I2C initialization structure. This is just an example using the
    	 * platform_drivers module. It can have any implementation you need.
    	 */
    	struct i2c_init_param i2c_init_param = {
    			.id = 0,
    			.max_speed_hz = 100000,
    			.slave_address = 0x10 | AD5593R_A0_STATE,
    			.type = 0
    	};
    
    	/**
    	 * Initialize platform. This is not important to this example. Platform and
    	 * implementations will vary.
    	 */
    	adi_initComponents();
    	
    	/**
    	 * I2C initialization function done in the platform_drivers module.
    	 * Here I assumed it is responsible for allocating the memory for the i2c
    	 * pointer (struct i2c_desc).
    	 */
    	i2c_init(&ad5593r_handle.i2c, &i2c_init_param);
    
    	ad5593r_handle.num_channels = 8; /* Set the number of active channels you want */
    	ad5593r_handle.channel_modes[0] = CH_MODE_ADC; /* Channel 0 is ADC */
    	ad5593r_handle.channel_modes[1] = CH_MODE_ADC; /* Channel 1 is ADC */
    	ad5593r_handle.channel_modes[2] = CH_MODE_DAC; /* Channel 2 is DAC */
    	ad5593r_handle.channel_modes[3] = CH_MODE_DAC; /* Channel 3 is DAC */
    
    	/**
    	 * To set a channel as GPIO output you have to set it to CH_MODE_UNUSED and
    	 * set the corresponding channel_offstate to CH_OFFSTATE_OUT_HIGH or
    	 * CH_OFFSTATE_OUT_LOW.
    	 */
    	ad5593r_handle.channel_modes[4] = CH_MODE_UNUSED; /* Channel 4 is GPIO output */
    	ad5593r_handle.channel_offstate[4] = CH_OFFSTATE_OUT_HIGH;
    	ad5593r_handle.channel_modes[5] = CH_MODE_UNUSED; /* Channel 5 is GPIO output */
    	ad5593r_handle.channel_offstate[5] = CH_OFFSTATE_OUT_HIGH;
    
    	ad5593r_handle.channel_modes[6] = CH_MODE_GPIO; /* Channel 6 is GPIO input */
    	ad5593r_handle.channel_modes[7] = CH_MODE_GPIO; /* Channel 7 is GPIO input */
    
    	/**
    	 * This does a software reset, the channel configuration and internal
    	 * reference activation. The communication phy, in this case I2C, must
    	 * already be initialized and the ops parameter populated. Also channel
    	 * modes and number must be set.
    	 */
    	ad5593r_init(&ad5593r_handle, &ad5593r_init_param);
    
    	return 0;
    }
    
    

    If you have any more questions let us know.

    Regards,

    Andrei.

  • Hi Andrei,

    Thank you very much for your help and time.

    Unfortunately, the driver doesn't work off the box. I got the following compilation, unknown filed 'id' specified in initializer. The problems lies on the github code inside the I2C.h file, taken from https://github.com/analogdevicesinc/no-OS/blob/master/include/i2c.h . The struct I2c_init_param does not have an id or type member.

    As per the comments in the code above  "The I2C initialization structure. This is just an example using the
    * platform_drivers module. It can have any implement"

    Will the driver work ok if I don't initialize these members inside the struct  i2c_init_param inside I2C.h?

    struct i2c_init_param i2c_init_param = {
    //.id = 0,
    .max_speed_hz = 100000,
    .slave_address = 0x10 | AD5593R_A0_STATE,
    //.type = 0
    };

    Kind regards

    Raul

  • 0
    •  Analog Employees 
    on Jul 25, 2019 6:54 AM in reply to RaulJHF

    Hello,

    The i2c driver is not meant to work out of the box. It is just a skeleton meant to be used as a guidance to creating your own implementation of the I2C suited for the platform you are using. It has no actual code in it as it is impossible to have I2C implementation for every platform in existence.

    You should implement the I2C driver you want and the initialization structure can have any, all or none of the current members, as you see fit.

    We have a number of I2C examples in our Github repository that may help you further.

    I should point out that the AD5593R driver does work out of the box, provided that it has a working I2C driver to rely on.

    Regards,

    Andrei.

  • Hi Andreid,

    Last night I had the time to have a proper look at the AD driver and I released that it needs a bit of code to interface the AD5593 with the uC, my fault I should have look at it more closely.

    Thank you very much for your fast response, your help and your time.

    kind regards

    Raul