Maxim Integrated 7-bit Sink/Source Current DAC. DS4424, DS4422 input/output current Digital-to-Analog Converter Driver/library code.

Dependents:   DS4424_Hello_Current_DAC_on_MAX32630FTHR

Committer:
phonemacro
Date:
Wed Jan 23 00:43:09 2019 +0000
Revision:
5:fc75fced724f
Parent:
4:3824afaf0d61
Add methods for processed data (picoAmps). Reverse the parameter order in the protected method read_register.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 0:8002303900e6 1 /*******************************************************************************
phonemacro 0:8002303900e6 2 * @file DS4424.h
phonemacro 5:fc75fced724f 3 * Copyright (C) 2018-2019 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 0:8002303900e6 4 *
phonemacro 0:8002303900e6 5 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 0:8002303900e6 6 * copy of this software and associated documentation files (the "Software"),
phonemacro 0:8002303900e6 7 * to deal in the Software without restriction, including without limitation
phonemacro 0:8002303900e6 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 0:8002303900e6 9 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 0:8002303900e6 10 * Software is furnished to do so, subject to the following conditions:
phonemacro 0:8002303900e6 11 *
phonemacro 0:8002303900e6 12 * The above copyright notice and this permission notice shall be included
phonemacro 0:8002303900e6 13 * in all copies or substantial portions of the Software.
phonemacro 0:8002303900e6 14 *
phonemacro 0:8002303900e6 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 0:8002303900e6 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 0:8002303900e6 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 0:8002303900e6 18 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 0:8002303900e6 19 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 0:8002303900e6 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 0:8002303900e6 21 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 0:8002303900e6 22 *
phonemacro 0:8002303900e6 23 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 0:8002303900e6 24 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 0:8002303900e6 25 * Products, Inc. Branding Policy.
phonemacro 0:8002303900e6 26 *
phonemacro 0:8002303900e6 27 * The mere transfer of this software does not imply any licenses
phonemacro 0:8002303900e6 28 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 0:8002303900e6 29 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 0:8002303900e6 30 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 0:8002303900e6 31 * ownership rights.
phonemacro 0:8002303900e6 32 *******************************************************************************
phonemacro 0:8002303900e6 33 */
phonemacro 4:3824afaf0d61 34 #ifndef DS4424_H
phonemacro 0:8002303900e6 35 #define DS4424_H
phonemacro 0:8002303900e6 36
phonemacro 0:8002303900e6 37 #include "mbed.h"
phonemacro 0:8002303900e6 38
phonemacro 0:8002303900e6 39 #define DS4424_NO_ERROR 0
phonemacro 0:8002303900e6 40 #define DS4424_ERROR -1
phonemacro 0:8002303900e6 41
phonemacro 0:8002303900e6 42 #define DS4424_MAX_DAC_CHANNELS 4
phonemacro 0:8002303900e6 43 #define DS4422_MAX_DAC_CHANNELS 2
phonemacro 0:8002303900e6 44
phonemacro 0:8002303900e6 45 #define DS4424_DAC_ADDR(chan) ((chan) + 0xf8)
phonemacro 0:8002303900e6 46 #define DS4424_SOURCE_I 1
phonemacro 0:8002303900e6 47 #define DS4424_SINK_I 0
phonemacro 0:8002303900e6 48
phonemacro 5:fc75fced724f 49 #define DS4424_MAX_RFS 160000
phonemacro 5:fc75fced724f 50 #define DS4424_MIN_RFS 40000
phonemacro 5:fc75fced724f 51 #define DS4424_IFS_SCALE 61000 /* 61000*100= 6100000 = 100,000,000 * .976/16 */
phonemacro 5:fc75fced724f 52 #define DS4424_MAX_PICOAMPS 200000000
phonemacro 5:fc75fced724f 53
phonemacro 0:8002303900e6 54 /**
phonemacro 2:b7a81b724561 55 * @brief Four/two Channel I2C Sink/Source Current DAC
phonemacro 5:fc75fced724f 56 * @version 1.0000.301
phonemacro 2:b7a81b724561 57 *
phonemacro 2:b7a81b724561 58 * @details The DS4424/DS422 contains four/two I2C
phonemacro 2:b7a81b724561 59 * programmable current DACs. This driver enables
phonemacro 2:b7a81b724561 60 * the writing of source/since values to the channel
phonemacro 2:b7a81b724561 61 * registers.
phonemacro 2:b7a81b724561 62 *
phonemacro 2:b7a81b724561 63 * @code
phonemacro 2:b7a81b724561 64 * #include "mbed.h"
phonemacro 2:b7a81b724561 65 * #include "DS4424.h"
phonemacro 2:b7a81b724561 66 * #include "max32630fthr.h"
phonemacro 2:b7a81b724561 67 * MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
phonemacro 2:b7a81b724561 68 * I2C i2cBus(P3_4, P3_5);
phonemacro 2:b7a81b724561 69 * DS4424 ds4424_dac(i2cBus, DS4424::DS4424_I2C_SLAVE_ADRS0, DS4424::DS4424_IC);
phonemacro 0:8002303900e6 70
phonemacro 2:b7a81b724561 71 * int main()
phonemacro 2:b7a81b724561 72 * {
phonemacro 2:b7a81b724561 73 * int32_t value[4]={0,0,0,0};
phonemacro 2:b7a81b724561 74 * int ret;
phonemacro 2:b7a81b724561 75 *
phonemacro 2:b7a81b724561 76 * i2cBus.frequency(400000);
phonemacro 2:b7a81b724561 77 *
phonemacro 2:b7a81b724561 78 * // read channel 0 register
phonemacro 2:b7a81b724561 79 * ret = ds4424_dac.read_raw(value[0], DS4424::REG_OUT0);
phonemacro 2:b7a81b724561 80 *
phonemacro 2:b7a81b724561 81 * //... rest of application
phonemacro 2:b7a81b724561 82 * }
phonemacro 2:b7a81b724561 83 * @endcode
phonemacro 2:b7a81b724561 84 */
phonemacro 0:8002303900e6 85
phonemacro 0:8002303900e6 86 class DS4424
phonemacro 0:8002303900e6 87 {
phonemacro 0:8002303900e6 88 public:
phonemacro 0:8002303900e6 89
phonemacro 0:8002303900e6 90
phonemacro 0:8002303900e6 91 /**
phonemacro 3:5070d2f614d5 92 * @brief IC's supported with this driver
phonemacro 3:5070d2f614d5 93 * @details DS4424, DS4422
phonemacro 3:5070d2f614d5 94 */
phonemacro 0:8002303900e6 95 typedef enum
phonemacro 0:8002303900e6 96 {
phonemacro 0:8002303900e6 97 DS4424_IC = 0,
phonemacro 0:8002303900e6 98 DS4422_IC
phonemacro 0:8002303900e6 99 }DS4424_ic_t;
phonemacro 0:8002303900e6 100
phonemacro 0:8002303900e6 101
phonemacro 0:8002303900e6 102 /**
phonemacro 3:5070d2f614d5 103 * @brief Valid 7-bit I2C addresses
phonemacro 3:5070d2f614d5 104 * @details The 8 valid I2C addresses set via A0, and A1 pins
phonemacro 3:5070d2f614d5 105 * of the DS4424
phonemacro 3:5070d2f614d5 106 */
phonemacro 0:8002303900e6 107 typedef enum
phonemacro 0:8002303900e6 108 {
phonemacro 0:8002303900e6 109 DS4424_I2C_SLAVE_ADRS0 = (int32_t)(0x20 >> 1), // code uses the 7 bit address
phonemacro 0:8002303900e6 110 DS4424_I2C_SLAVE_ADRS1 = (int32_t)(0x60 >> 1),
phonemacro 0:8002303900e6 111 DS4424_I2C_SLAVE_ADRS2 = (int32_t)(0xA0 >> 1),
phonemacro 0:8002303900e6 112 DS4424_I2C_SLAVE_ADRS3 = (int32_t)(0xe0 >> 1)
phonemacro 0:8002303900e6 113 }DS4424_i2c_adrs_t;
phonemacro 0:8002303900e6 114
phonemacro 0:8002303900e6 115
phonemacro 0:8002303900e6 116 /**
phonemacro 2:b7a81b724561 117 * @brief Register Addresses
phonemacro 2:b7a81b724561 118 * @details Enumerated MAX20303 register addresses
phonemacro 2:b7a81b724561 119 */
phonemacro 0:8002303900e6 120 enum ChannelRegAddr_e {
phonemacro 0:8002303900e6 121 REG_OUT0 = 0xF8, // channel 0
phonemacro 0:8002303900e6 122 REG_OUT1 = 0xF9,
phonemacro 0:8002303900e6 123 REG_OUT2 = 0xFA,
phonemacro 0:8002303900e6 124 REG_OUT3 = 0xFB
phonemacro 0:8002303900e6 125 };
phonemacro 0:8002303900e6 126
phonemacro 0:8002303900e6 127
phonemacro 0:8002303900e6 128 /**********************************************************//**
phonemacro 2:b7a81b724561 129 * @brief Constructor for DS4424 Class.
phonemacro 2:b7a81b724561 130 *
phonemacro 2:b7a81b724561 131 * @details Allows user to use existing I2C object
phonemacro 2:b7a81b724561 132 *
phonemacro 2:b7a81b724561 133 * On Entry:
phonemacro 2:b7a81b724561 134 * @param[in] i2c_bus - pointer to existing I2C object
phonemacro 2:b7a81b724561 135 * @param[in] i2c_adrs - 7-bit slave address of DS4424
phonemacro 2:b7a81b724561 136 * @param[in] ic_variant - which type of DS44xx is used
phonemacro 2:b7a81b724561 137 *
phonemacro 2:b7a81b724561 138 * On Exit:
phonemacro 2:b7a81b724561 139 *
phonemacro 2:b7a81b724561 140 * @return None
phonemacro 2:b7a81b724561 141 **************************************************************/
phonemacro 0:8002303900e6 142 DS4424(I2C &i2c_bus, DS4424_i2c_adrs_t slaveAddress, DS4424_ic_t ic_variant);
phonemacro 0:8002303900e6 143
phonemacro 0:8002303900e6 144
phonemacro 0:8002303900e6 145
phonemacro 0:8002303900e6 146 /**********************************************************//**
phonemacro 2:b7a81b724561 147 * @brief Default destructor for DS4424 Class.
phonemacro 2:b7a81b724561 148 *
phonemacro 2:b7a81b724561 149 * @details Destroys I2C object if owner
phonemacro 2:b7a81b724561 150 *
phonemacro 2:b7a81b724561 151 * On Entry:
phonemacro 2:b7a81b724561 152 *
phonemacro 2:b7a81b724561 153 * On Exit:
phonemacro 2:b7a81b724561 154 *
phonemacro 2:b7a81b724561 155 * @return None
phonemacro 2:b7a81b724561 156 **************************************************************/
phonemacro 0:8002303900e6 157 ~DS4424();
phonemacro 0:8002303900e6 158
phonemacro 2:b7a81b724561 159 /**
phonemacro 5:fc75fced724f 160 * @brief Read raw value of a channel using two's complement.
phonemacro 2:b7a81b724561 161 * @param result - Value that is read from the register
phonemacro 5:fc75fced724f 162 * Negative values are sink; Positive values are source.
phonemacro 2:b7a81b724561 163 * @param chan_addr - Channel 0 to 3
phonemacro 2:b7a81b724561 164 * @return 0 on success, negative number on failure
phonemacro 2:b7a81b724561 165 */
phonemacro 1:a5d963a3c298 166 int read_raw(int32_t &result, ChannelRegAddr_e chan_addr);
phonemacro 0:8002303900e6 167
phonemacro 2:b7a81b724561 168 /**
phonemacro 5:fc75fced724f 169 * @brief Read raw value of a channel, using the hardware format
phonemacro 5:fc75fced724f 170 * @param *result - Value that is read from the register.
phonemacro 5:fc75fced724f 171 * MSb is 0 for sink; MSb is 1 for source.
phonemacro 5:fc75fced724f 172 * @param chan_addr - Channel 0 to 3
phonemacro 5:fc75fced724f 173 * @return 0 on success, negative number on failure
phonemacro 5:fc75fced724f 174 */
phonemacro 5:fc75fced724f 175 int read_hw_raw(uint8_t &result, ChannelRegAddr_e chan_addr);
phonemacro 5:fc75fced724f 176
phonemacro 5:fc75fced724f 177 /**
phonemacro 5:fc75fced724f 178 * @brief Write two's complement raw value to a channel.
phonemacro 5:fc75fced724f 179 * @param value - Two's complement value that is stored to the register.
phonemacro 5:fc75fced724f 180 * Sink values are negative.
phonemacro 2:b7a81b724561 181 * @param chan_addr - Channel 0 to 3
phonemacro 2:b7a81b724561 182 * @return 0 on success, negative number on failure
phonemacro 2:b7a81b724561 183 */
phonemacro 1:a5d963a3c298 184 int write_raw(int32_t value, ChannelRegAddr_e chan_addr);
phonemacro 5:fc75fced724f 185
phonemacro 5:fc75fced724f 186 /**
phonemacro 5:fc75fced724f 187 * @brief Write raw value to a channel, using the hardware format
phonemacro 5:fc75fced724f 188 * @param value - Value that is stored to the register
phonemacro 5:fc75fced724f 189 * MSb is 0 for sink; MSb is 1 for source.
phonemacro 5:fc75fced724f 190 * @param chan_addr - Channel 0 to 3
phonemacro 5:fc75fced724f 191 * @return 0 on success, negative number on failure
phonemacro 5:fc75fced724f 192 */
phonemacro 5:fc75fced724f 193 int write_hw_raw(uint8_t value, ChannelRegAddr_e chan_addr);
phonemacro 5:fc75fced724f 194
phonemacro 5:fc75fced724f 195 /**
phonemacro 5:fc75fced724f 196 * @brief Convert picoAmps to raw values
phonemacro 5:fc75fced724f 197 * @param *val_out - 8-bit raw value
phonemacro 5:fc75fced724f 198 * @param picoAmps - signed value in picoAmps
phonemacro 5:fc75fced724f 199 * Negative values are sink; Positive values are source.
phonemacro 5:fc75fced724f 200 * @param rfs_resistor - external resistor values in Ohms
phonemacro 5:fc75fced724f 201 * @return 0 on success, negative number on failure
phonemacro 5:fc75fced724f 202 */
phonemacro 5:fc75fced724f 203 int convert_picoAmps_to_hw_raw(uint8_t *val_out,
phonemacro 5:fc75fced724f 204 int32_t picoAmps, uint32_t rfs_resistor);
phonemacro 5:fc75fced724f 205
phonemacro 5:fc75fced724f 206 /**
phonemacro 5:fc75fced724f 207 * @brief Convert raw values to picoAmps
phonemacro 5:fc75fced724f 208 * @param *val_out - value in picoAmps
phonemacro 5:fc75fced724f 209 * @param raw_in - 8 bit raw value, two's complement
phonemacro 5:fc75fced724f 210 * Negative values are sink; Positive values are source.
phonemacro 5:fc75fced724f 211 * @param rfs_resistor - external resistor values in Ohms
phonemacro 5:fc75fced724f 212 * @return 0 on success, negative number on failure
phonemacro 5:fc75fced724f 213 */
phonemacro 5:fc75fced724f 214 int convert_raw_to_picoAmps(int32_t *val_out,
phonemacro 5:fc75fced724f 215 int8_t raw_in, uint32_t rfs_resistor);
phonemacro 0:8002303900e6 216
phonemacro 0:8002303900e6 217 protected:
phonemacro 0:8002303900e6 218
phonemacro 0:8002303900e6 219 /**
phonemacro 2:b7a81b724561 220 * @brief Read register of device at slave address
phonemacro 5:fc75fced724f 221 * @param[out] value - Read data on success
phonemacro 2:b7a81b724561 222 * @param reg - Register address
phonemacro 2:b7a81b724561 223 * @return 0 on success, negative number on failure
phonemacro 2:b7a81b724561 224 */
phonemacro 5:fc75fced724f 225 int read_register(uint8_t &value, ChannelRegAddr_e reg);
phonemacro 0:8002303900e6 226
phonemacro 0:8002303900e6 227 /**
phonemacro 2:b7a81b724561 228 * @brief Write register of device at slave address
phonemacro 2:b7a81b724561 229 * @param reg - Register address
phonemacro 2:b7a81b724561 230 * @param value - Value to write
phonemacro 2:b7a81b724561 231 * @return 0 on success, negative number on failure
phonemacro 2:b7a81b724561 232 */
phonemacro 1:a5d963a3c298 233 int write_register(ChannelRegAddr_e reg, uint8_t value);
phonemacro 0:8002303900e6 234
phonemacro 0:8002303900e6 235 private:
phonemacro 2:b7a81b724561 236 /** I2C object */
phonemacro 0:8002303900e6 237 I2C &m_i2c;
phonemacro 2:b7a81b724561 238 /** Device slave addresses */
phonemacro 0:8002303900e6 239 uint8_t m_writeAddress, m_readAddress;
phonemacro 2:b7a81b724561 240 /** Address of the Maximum Register for this device */
phonemacro 0:8002303900e6 241 ChannelRegAddr_e m_max_ch_reg_addr;
phonemacro 0:8002303900e6 242 };
phonemacro 0:8002303900e6 243
phonemacro 0:8002303900e6 244 #endif/* DS4424_H */