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