John Garlitos / LTC26X6

Files at this revision

API Documentation at this revision

Comitter:
jngarlitos
Date:
Mon Dec 06 05:17:03 2021 +0000
Parent:
0:91316dbef1b6
Commit message:
Initial Commit

Changed in this revision

LTC26X6.cpp Show diff for this revision Revisions of this file
LTC26X6.h Show diff for this revision Revisions of this file
ltc26x6.c Show annotated file Show diff for this revision Revisions of this file
ltc26x6.h Show annotated file Show diff for this revision Revisions of this file
diff -r 91316dbef1b6 -r cab5535713ee LTC26X6.cpp
diff -r 91316dbef1b6 -r cab5535713ee LTC26X6.h
diff -r 91316dbef1b6 -r cab5535713ee ltc26x6.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ltc26x6.c	Mon Dec 06 05:17:03 2021 +0000
@@ -0,0 +1,155 @@
+/***************************************************************************//**
+ *   @file   ltc26x6.c
+ *   @brief  Implementation of LTC2606,LTC2616,LTC2626 Driver.
+********************************************************************************
+ * Copyright 2021(c) Analog Devices, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *  - Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  - Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *  - Neither the name of Analog Devices, Inc. nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *  - The use of this software may or may not infringe the patent rights
+ *    of one or more patent holders.  This license does not release you
+ *    from the requirement that you obtain separate licenses from these
+ *    patent holders to use this software.
+ *  - Use of the software either in source or binary form, must be run
+ *    on or directly connected to an Analog Devices Inc. component.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*******************************************************************************/
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <math.h>
+
+#include "ltc26x6.h"
+
+/**
+ * Initialize the device.
+ * @param device - The device structure.
+ * @param init_param - The structure that contains the device initial
+ *                     parameters.
+ * @return 0 in case of success, negative error code otherwise.
+ */
+int32_t ltc26x6_init(struct ltc26x6_dev **device,
+             struct ltc26x6_init_param init_param)
+{
+    struct ltc26x6_dev *dev;
+    int32_t ret;
+
+    dev = (struct ltc26x6_dev *)malloc(sizeof(*dev));
+    if (!dev)
+        return -1;
+
+    ret = i2c_init(&dev->i2c_desc, &init_param.i2c_init);
+    dev->resolution = init_param.resolution;
+    dev->vref = init_param.vref;
+    dev->typical_offset = init_param.typical_offset;
+    *device = dev;
+    return ret;
+}
+
+/**
+ * Calculates an LTC26X6 DAC code for the desired output voltage.
+ * Based on the desired output voltage, the offset, and lsb parameters, return the corresponding DAC code that should be written to the LTC26X6.
+ * @param dac_voltage       Desired output voltage
+ * @param *code             Returned DAC code
+ * @return 0 in case of success, negative error code otherwise.
+ */
+int16_t ltc26x6_voltage_to_code(struct ltc26x6_dev *device, float dac_voltage,
+                uint16_t *code)
+{
+    uint32_t ltc26x6_full_scale = (2 << device->resolution) - 1;
+    // The LTC26X6 least significant bit value with given voltage reference
+    float   ltc26x6_lsb = (float)(device->vref/ltc26x6_full_scale); 
+    int32_t dac_code, ret;
+    float float_code;
+    
+    // 1) Calculate the DAC code: (DAC voltage/LSB)- typical offset volatge
+    float_code = (dac_voltage / ltc26x6_lsb) - device->typical_offset;
+    // 2) Round
+    float_code = (float_code > (floor(float_code) + 0.5)) ? ceil(float_code) : floor(float_code); 
+    // 3) Convert to unsigned integer      
+    dac_code = (int32_t)(float_code);                                                                   
+
+    if(dac_code >  ltc26x6_full_scale) { 
+        // Requesetd voltage is bigger than reference voltage  
+        // Return fullscale code                                                         
+        dac_code =  ltc26x6_full_scale; 
+        // Return Overflow error                                                                              
+        ret = LTC26X6_CODE_OVERFLOW;
+    } else if(dac_code < 0) {  
+        // Requested voltage is lower than offset voltage                                                                                                      
+        dac_code = 0;     
+        // Return Underflow error                                                                              
+        ret = LTC26X6_CODE_UNDERFLOW;
+    } else // Requestedd volage is in supported range                                                                                               
+        ret = SUCCESS;
+
+    *code = ((uint16_t)(dac_code));
+    return ret;
+}
+
+/**
+ * Write code to LTC26X6
+ * @param dev               The device structure.
+ * @param dac_command       Command for the DAC
+ *                          Accepted values:    write_command
+ *                                              write_update_command
+ *
+ * @param dac_code          16-bit DAC output voltage code
+ * @return 0 in case of success, negative error code otherwise.
+ */
+int32_t ltc26x6_write_code(struct ltc26x6_dev *dev,
+               enum ltc26x6_write_command write_command, uint16_t dac_code)
+{
+    uint8_t data[3];
+
+    data[0] = write_command;      // Write command
+    data[1] = dac_code >>8;       // MSB code
+    data[2] = dac_code & 0x00FF;  // LSB code
+
+    return(i2c_write(dev->i2c_desc, data, 3, 1));
+}
+
+/**
+ * Power down the device
+ * @param device - The device structure.
+ * @return 0 in case of success, negative error code otherwise.
+ */
+int32_t ltc26x6_power_down(struct ltc26x6_dev *dev)
+{
+    uint8_t command = LTC26X6_POWER_DOWN_COMMAND;
+    return (i2c_write(dev->i2c_desc, &command, 1, 1));
+}
+
+/**
+ * Power up the device
+ * @param device - The device structure.
+ * @return 0 in case of success, negative error code otherwise.
+ */
+int32_t ltc26x6_power_up(struct ltc26x6_dev *dev)
+{
+    uint8_t command = LTC26X6_UPDATE_COMMAND;
+    return (i2c_write(dev->i2c_desc, &command, 1, 1));
+}
+
diff -r 91316dbef1b6 -r cab5535713ee ltc26x6.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ltc26x6.h	Mon Dec 06 05:17:03 2021 +0000
@@ -0,0 +1,128 @@
+/***************************************************************************//**
+ *   @file   ltc26x6.c
+ *   @brief  Header file of LTC2606,LTC2616,LTC2626 Driver.
+********************************************************************************
+ * Copyright 2021(c) Analog Devices, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *  - Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  - Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *  - Neither the name of Analog Devices, Inc. nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *  - The use of this software may or may not infringe the patent rights
+ *    of one or more patent holders.  This license does not release you
+ *    from the requirement that you obtain separate licenses from these
+ *    patent holders to use this software.
+ *  - Use of the software either in source or binary form, must be run
+ *    on or directly connected to an Analog Devices Inc. component.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*******************************************************************************/
+#ifndef _LTC26X6_H_
+#define _LTC26X6_H_
+
+#include <stdint.h>
+#include "platform_drivers.h"
+
+/******************************************************************************/
+/**************         MACROS AND CONSTANT DEFINITIONS          **************/
+/******************************************************************************/
+
+// LTC2606 I2C Address                 //  AD2       AD1       AD0
+// #define LTC2606_I2C_ADDRESS 0x10    //  LOW       LOW       LOW
+// #define LTC2606_I2C_ADDRESS 0x11    //  LOW       LOW       Float
+// #define LTC2606_I2C_ADDRESS 0x12    //  LOW       LOW       HIGH
+// #define LTC2606_I2C_ADDRESS 0x13    //  LOW       Float     LOW
+// #define LTC2606_I2C_ADDRESS 0x20    //  LOW       Float     Float
+// #define LTC2606_I2C_ADDRESS 0x21    //  LOW       Float     High 
+// #define LTC2606_I2C_ADDRESS 0x22    //  LOW       HIGH      LOW
+// #define LTC2606_I2C_ADDRESS 0x23    //  LOW       HIGH      Float
+// #define LTC2606_I2C_ADDRESS 0x30    //  LOW       High      HIGH
+// #define LTC2606_I2C_ADDRESS 0x31    //  Float     LOW       LOW
+// #define LTC2606_I2C_ADDRESS 0x32    //  Float     LOW       Float
+// #define LTC2606_I2C_ADDRESS 0x33    //  Float     LOW       HIGH
+// #define LTC2606_I2C_ADDRESS 0x40    //  Float     Float     LOW
+// #define LTC2606_I2C_ADDRESS 0x41    //  Float     Float     Float
+// #define LTC2606_I2C_ADDRESS 0x42    //  Float     Float     HIGH
+// #define LTC2606_I2C_ADDRESS 0x43    //  Float     High      LOW
+// #define LTC2606_I2C_ADDRESS 0x50    //  Float     High      Float
+// #define LTC2606_I2C_ADDRESS 0x51    //  Float     High      HIGH
+// #define LTC2606_I2C_ADDRESS 0x52    //  High      LOW       LOW
+// #define LTC2606_I2C_ADDRESS 0x53    //  High      LOW       Float
+// #define LTC2606_I2C_ADDRESS 0x60    //  High      LOW       High
+// #define LTC2606_I2C_ADDRESS 0x61    //  High      Float     LOW
+// #define LTC2606_I2C_ADDRESS 0x62    //  High      Float     Float
+// #define LTC2606_I2C_ADDRESS 0x63    //  High      Float     High
+// #define LTC2606_I2C_ADDRESS 0x70    //  High      High      LOW
+// #define LTC2606_I2C_ADDRESS 0x71    //  High      High      Float
+// #define LTC2606_I2C_ADDRESS 0x72    //  High      High      High
+
+
+#define LTC26X6_I2C_GLOBAL_ADDRESS      0x73            //  Global Address
+
+#define LTC26X6_UPDATE_COMMAND          0x10            // Command to update (and power up) LTC2606. Output voltage will be set to the value stored in the internal register by previous write command.
+#define LTC26X6_POWER_DOWN_COMMAND      0x40            // Command to power down the LTC2606.
+
+#define LTC26X6_WRITE_ADDRESS(x)        (x << 1)        // I2C addres shift, to make R/W bit LOW by default
+
+// Return error values
+#define LTC26X6_CODE_OVERFLOW           -2
+#define LTC26X6_CODE_UNDERFLOW          -3
+
+/*****************************************************************************/
+/*************************** Types Declarations *******************************/
+/******************************************************************************/
+enum ltc26x6_write_command         // SDP-K1 GPIO number assignment
+{
+    write_command = 0x00,          // Command to update (and power up) LTC26X6. Output voltage will be set to the value stored in the internal register by previous write command.
+    write_update_command = 0x30    // Command to write and update (and power up) the LTC26X6. The output voltage will immediate change to the value being written to the internal register.
+};
+
+struct ltc26x6_dev {
+    /* I2C */
+    i2c_desc    *i2c_desc;
+    /* Device Settings */
+    uint8_t     resolution;     //LTC2606-16bits, LTC2616-14bit, LTC2626-12bits
+    float       vref;           //DAC reference voltage 
+    float       typical_offset; //DAC typical offset
+};
+
+struct ltc26x6_init_param {
+    /* I2C */
+    i2c_init_param  i2c_init;
+    /* Device Settings */
+    uint8_t     resolution;     //LTC2606-16bits, LTC2616-14bit, LTC2626-12bits
+    float       vref;           //DAC reference voltage 
+    float       typical_offset; //DAC typical offset
+};
+
+/******************************************************************************/
+/************************ Functions Declarations ******************************/
+/******************************************************************************/
+
+int32_t ltc26x6_init(struct ltc26x6_dev **device, struct ltc26x6_init_param init_param);
+int16_t ltc26x6_voltage_to_code(struct ltc26x6_dev *device, float dac_voltage, uint16_t *code);
+int32_t ltc26x6_write_code(struct ltc26x6_dev *dev, enum ltc26x6_write_command write_command, uint16_t dac_code);
+int32_t ltc26x6_power_down(struct ltc26x6_dev *dev);
+int32_t ltc26x6_power_up(struct ltc26x6_dev *dev);
+
+#endif // !_LTC26X6_H_
+
+