Maxim Integrated 7-bit Sink/Source Current DAC. Driver/library for DS4424, DS4422

Committer:
phonemacro
Date:
Thu Sep 06 21:43:36 2018 +0000
Revision:
3:c40703da0faf
Parent:
2:cead66dd3647
Updated method names to comply with Mbed coding style.; Corrected online API documentation (Doxygen) not being generated.

Who changed what in which revision?

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