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:
Mon Sep 10 21:53:18 2018 +0000
Revision:
1:a5d963a3c298
Parent:
0:8002303900e6
Child:
2:b7a81b724561
Updated to use Mbed coding style for member functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 0:8002303900e6 1 /*******************************************************************************
phonemacro 0:8002303900e6 2 * @file DS4424.h
phonemacro 0:8002303900e6 3 * Copyright (C) 2018 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 1:a5d963a3c298 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 0:8002303900e6 49 /**
phonemacro 1:a5d963a3c298 50 * @brief Four/two Channel I2C Sink/Source Current DAC
phonemacro 1:a5d963a3c298 51 * @version 1.0000.2
phonemacro 1:a5d963a3c298 52 *
phonemacro 1:a5d963a3c298 53 * @details The DS4424/DS422 contains four/two I2C
phonemacro 1:a5d963a3c298 54 * programmable current DACs. This driver enables
phonemacro 1:a5d963a3c298 55 * the writing of source/since values to the channel
phonemacro 1:a5d963a3c298 56 * registers.
phonemacro 1:a5d963a3c298 57 *
phonemacro 1:a5d963a3c298 58 * @code
phonemacro 1:a5d963a3c298 59 * #include "mbed.h"
phonemacro 1:a5d963a3c298 60 * #include "DS4424.h"
phonemacro 1:a5d963a3c298 61 * #include "max32630fthr.h"
phonemacro 1:a5d963a3c298 62 * MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
phonemacro 1:a5d963a3c298 63 * I2C i2cBus(P3_4, P3_5);
phonemacro 1:a5d963a3c298 64 * DS4424 ds4424_dac(i2cBus, DS4424::DS4424_I2C_SLAVE_ADRS0, DS4424::DS4424_IC);
phonemacro 0:8002303900e6 65
phonemacro 1:a5d963a3c298 66 * int main()
phonemacro 1:a5d963a3c298 67 * {
phonemacro 1:a5d963a3c298 68 * int32_t value[4]={0,0,0,0};
phonemacro 1:a5d963a3c298 69 * int ret;
phonemacro 1:a5d963a3c298 70 *
phonemacro 1:a5d963a3c298 71 * i2cBus.frequency(400000);
phonemacro 1:a5d963a3c298 72 *
phonemacro 1:a5d963a3c298 73 * // read channel 0 register
phonemacro 1:a5d963a3c298 74 * ret = ds4424_dac.read_raw(value[0], DS4424::REG_OUT0);
phonemacro 1:a5d963a3c298 75 *
phonemacro 1:a5d963a3c298 76 * //... rest of application
phonemacro 1:a5d963a3c298 77 * }
phonemacro 1:a5d963a3c298 78 * @endcode
phonemacro 1:a5d963a3c298 79 */
phonemacro 0:8002303900e6 80
phonemacro 0:8002303900e6 81 class DS4424
phonemacro 0:8002303900e6 82 {
phonemacro 0:8002303900e6 83 public:
phonemacro 0:8002303900e6 84
phonemacro 0:8002303900e6 85
phonemacro 0:8002303900e6 86 /**
phonemacro 0:8002303900e6 87 * @brief IC's supported with this driver
phonemacro 0:8002303900e6 88 * @details DS4424, DS4422
phonemacro 0:8002303900e6 89 */
phonemacro 0:8002303900e6 90 typedef enum
phonemacro 0:8002303900e6 91 {
phonemacro 0:8002303900e6 92 DS4424_IC = 0,
phonemacro 0:8002303900e6 93 DS4422_IC
phonemacro 0:8002303900e6 94 }DS4424_ic_t;
phonemacro 0:8002303900e6 95
phonemacro 0:8002303900e6 96
phonemacro 0:8002303900e6 97 /**
phonemacro 0:8002303900e6 98 * @brief Valid 7-bit I2C addresses
phonemacro 0:8002303900e6 99 * @details The 8 valid I2C addresses set via A0, and A1 pins
phonemacro 0:8002303900e6 100 * of the DS4424
phonemacro 0:8002303900e6 101 */
phonemacro 0:8002303900e6 102 typedef enum
phonemacro 0:8002303900e6 103 {
phonemacro 0:8002303900e6 104 DS4424_I2C_SLAVE_ADRS0 = (int32_t)(0x20 >> 1), // code uses the 7 bit address
phonemacro 0:8002303900e6 105 DS4424_I2C_SLAVE_ADRS1 = (int32_t)(0x60 >> 1),
phonemacro 0:8002303900e6 106 DS4424_I2C_SLAVE_ADRS2 = (int32_t)(0xA0 >> 1),
phonemacro 0:8002303900e6 107 DS4424_I2C_SLAVE_ADRS3 = (int32_t)(0xe0 >> 1)
phonemacro 0:8002303900e6 108 }DS4424_i2c_adrs_t;
phonemacro 0:8002303900e6 109
phonemacro 0:8002303900e6 110
phonemacro 0:8002303900e6 111 /**
phonemacro 0:8002303900e6 112 * @brief Register Addresses
phonemacro 0:8002303900e6 113 * @details Enumerated MAX20303 register addresses
phonemacro 0:8002303900e6 114 */
phonemacro 0:8002303900e6 115 enum ChannelRegAddr_e {
phonemacro 0:8002303900e6 116 REG_OUT0 = 0xF8, // channel 0
phonemacro 0:8002303900e6 117 REG_OUT1 = 0xF9,
phonemacro 0:8002303900e6 118 REG_OUT2 = 0xFA,
phonemacro 0:8002303900e6 119 REG_OUT3 = 0xFB
phonemacro 0:8002303900e6 120 };
phonemacro 0:8002303900e6 121
phonemacro 0:8002303900e6 122
phonemacro 0:8002303900e6 123 /**********************************************************//**
phonemacro 0:8002303900e6 124 * @brief Constructor for DS4424 Class.
phonemacro 0:8002303900e6 125 *
phonemacro 0:8002303900e6 126 * @details Allows user to use existing I2C object
phonemacro 0:8002303900e6 127 *
phonemacro 0:8002303900e6 128 * On Entry:
phonemacro 0:8002303900e6 129 * @param[in] i2c_bus - pointer to existing I2C object
phonemacro 0:8002303900e6 130 * @param[in] i2c_adrs - 7-bit slave address of DS4424
phonemacro 0:8002303900e6 131 * @param[in] ic_variant - which type of DS44xx is used
phonemacro 0:8002303900e6 132 *
phonemacro 0:8002303900e6 133 * On Exit:
phonemacro 0:8002303900e6 134 *
phonemacro 0:8002303900e6 135 * @return None
phonemacro 0:8002303900e6 136 **************************************************************/
phonemacro 0:8002303900e6 137 DS4424(I2C &i2c_bus, DS4424_i2c_adrs_t slaveAddress, DS4424_ic_t ic_variant);
phonemacro 0:8002303900e6 138
phonemacro 0:8002303900e6 139
phonemacro 0:8002303900e6 140
phonemacro 0:8002303900e6 141 /**********************************************************//**
phonemacro 0:8002303900e6 142 * @brief Default destructor for DS4424 Class.
phonemacro 0:8002303900e6 143 *
phonemacro 0:8002303900e6 144 * @details Destroys I2C object if owner
phonemacro 0:8002303900e6 145 *
phonemacro 0:8002303900e6 146 * On Entry:
phonemacro 0:8002303900e6 147 *
phonemacro 0:8002303900e6 148 * On Exit:
phonemacro 0:8002303900e6 149 *
phonemacro 0:8002303900e6 150 * @return None
phonemacro 0:8002303900e6 151 **************************************************************/
phonemacro 0:8002303900e6 152 ~DS4424();
phonemacro 0:8002303900e6 153
phonemacro 0:8002303900e6 154 /**
phonemacro 0:8002303900e6 155 * @brief Read raw value of a channel. Negative values are sink; Positive values are source.
phonemacro 0:8002303900e6 156 * @param result - Value that is read from the register
phonemacro 0:8002303900e6 157 * @param chan_addr - Channel 0 to 3
phonemacro 0:8002303900e6 158 * @return 0 on success, non-zero on failure
phonemacro 0:8002303900e6 159 */
phonemacro 1:a5d963a3c298 160 int read_raw(int32_t &result, ChannelRegAddr_e chan_addr);
phonemacro 0:8002303900e6 161
phonemacro 0:8002303900e6 162 /**
phonemacro 0:8002303900e6 163 * @brief Write raw value to a channel.
phonemacro 0:8002303900e6 164 * @param value - Value that is stored to the register
phonemacro 0:8002303900e6 165 * @param chan_addr - Channel 0 to 3
phonemacro 0:8002303900e6 166 * @return 0 on success, non-zero on failure
phonemacro 0:8002303900e6 167 */
phonemacro 1:a5d963a3c298 168 int write_raw(int32_t value, ChannelRegAddr_e chan_addr);
phonemacro 0:8002303900e6 169
phonemacro 0:8002303900e6 170 protected:
phonemacro 0:8002303900e6 171
phonemacro 0:8002303900e6 172 /**
phonemacro 0:8002303900e6 173 * @brief Read register of device at slave address
phonemacro 0:8002303900e6 174 * @param reg - Register address
phonemacro 0:8002303900e6 175 * @param[out] value - Read data on success
phonemacro 0:8002303900e6 176 * @return 0 on success, non-zero on failure
phonemacro 0:8002303900e6 177 */
phonemacro 1:a5d963a3c298 178 int read_register(ChannelRegAddr_e reg, uint8_t &value);
phonemacro 0:8002303900e6 179
phonemacro 0:8002303900e6 180 /**
phonemacro 0:8002303900e6 181 * @brief Write register of device at slave address
phonemacro 0:8002303900e6 182 * @param reg - Register address
phonemacro 0:8002303900e6 183 * @param value - Value to write
phonemacro 0:8002303900e6 184 * @return 0 on success, non-zero on failure
phonemacro 0:8002303900e6 185 */
phonemacro 1:a5d963a3c298 186 int write_register(ChannelRegAddr_e reg, uint8_t value);
phonemacro 0:8002303900e6 187
phonemacro 0:8002303900e6 188 private:
phonemacro 0:8002303900e6 189 /// I2C object
phonemacro 0:8002303900e6 190 I2C &m_i2c;
phonemacro 0:8002303900e6 191 /// Device slave addresses
phonemacro 0:8002303900e6 192 uint8_t m_writeAddress, m_readAddress;
phonemacro 0:8002303900e6 193 /// Address of the Maximum Register for this device
phonemacro 0:8002303900e6 194 ChannelRegAddr_e m_max_ch_reg_addr;
phonemacro 0:8002303900e6 195 };
phonemacro 0:8002303900e6 196
phonemacro 0:8002303900e6 197 #endif/* DS4424_H */