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@4:3824afaf0d61, 2018-10-07 (annotated)
- Committer:
- phonemacro
- Date:
- Sun Oct 07 22:13:14 2018 +0000
- Revision:
- 4:3824afaf0d61
- Parent:
- 3:5070d2f614d5
- Child:
- 5:fc75fced724f
Updated comments
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 | 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 | 0:8002303900e6 | 49 | /** |
phonemacro | 2:b7a81b724561 | 50 | * @brief Four/two Channel I2C Sink/Source Current DAC |
phonemacro | 2:b7a81b724561 | 51 | * @version 1.0000.3 |
phonemacro | 2:b7a81b724561 | 52 | * |
phonemacro | 2:b7a81b724561 | 53 | * @details The DS4424/DS422 contains four/two I2C |
phonemacro | 2:b7a81b724561 | 54 | * programmable current DACs. This driver enables |
phonemacro | 2:b7a81b724561 | 55 | * the writing of source/since values to the channel |
phonemacro | 2:b7a81b724561 | 56 | * registers. |
phonemacro | 2:b7a81b724561 | 57 | * |
phonemacro | 2:b7a81b724561 | 58 | * @code |
phonemacro | 2:b7a81b724561 | 59 | * #include "mbed.h" |
phonemacro | 2:b7a81b724561 | 60 | * #include "DS4424.h" |
phonemacro | 2:b7a81b724561 | 61 | * #include "max32630fthr.h" |
phonemacro | 2:b7a81b724561 | 62 | * MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
phonemacro | 2:b7a81b724561 | 63 | * I2C i2cBus(P3_4, P3_5); |
phonemacro | 2:b7a81b724561 | 64 | * DS4424 ds4424_dac(i2cBus, DS4424::DS4424_I2C_SLAVE_ADRS0, DS4424::DS4424_IC); |
phonemacro | 0:8002303900e6 | 65 | |
phonemacro | 2:b7a81b724561 | 66 | * int main() |
phonemacro | 2:b7a81b724561 | 67 | * { |
phonemacro | 2:b7a81b724561 | 68 | * int32_t value[4]={0,0,0,0}; |
phonemacro | 2:b7a81b724561 | 69 | * int ret; |
phonemacro | 2:b7a81b724561 | 70 | * |
phonemacro | 2:b7a81b724561 | 71 | * i2cBus.frequency(400000); |
phonemacro | 2:b7a81b724561 | 72 | * |
phonemacro | 2:b7a81b724561 | 73 | * // read channel 0 register |
phonemacro | 2:b7a81b724561 | 74 | * ret = ds4424_dac.read_raw(value[0], DS4424::REG_OUT0); |
phonemacro | 2:b7a81b724561 | 75 | * |
phonemacro | 2:b7a81b724561 | 76 | * //... rest of application |
phonemacro | 2:b7a81b724561 | 77 | * } |
phonemacro | 2:b7a81b724561 | 78 | * @endcode |
phonemacro | 2:b7a81b724561 | 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 | 3:5070d2f614d5 | 87 | * @brief IC's supported with this driver |
phonemacro | 3:5070d2f614d5 | 88 | * @details DS4424, DS4422 |
phonemacro | 3:5070d2f614d5 | 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 | 3:5070d2f614d5 | 98 | * @brief Valid 7-bit I2C addresses |
phonemacro | 3:5070d2f614d5 | 99 | * @details The 8 valid I2C addresses set via A0, and A1 pins |
phonemacro | 3:5070d2f614d5 | 100 | * of the DS4424 |
phonemacro | 3:5070d2f614d5 | 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 | 2:b7a81b724561 | 112 | * @brief Register Addresses |
phonemacro | 2:b7a81b724561 | 113 | * @details Enumerated MAX20303 register addresses |
phonemacro | 2:b7a81b724561 | 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 | 2:b7a81b724561 | 124 | * @brief Constructor for DS4424 Class. |
phonemacro | 2:b7a81b724561 | 125 | * |
phonemacro | 2:b7a81b724561 | 126 | * @details Allows user to use existing I2C object |
phonemacro | 2:b7a81b724561 | 127 | * |
phonemacro | 2:b7a81b724561 | 128 | * On Entry: |
phonemacro | 2:b7a81b724561 | 129 | * @param[in] i2c_bus - pointer to existing I2C object |
phonemacro | 2:b7a81b724561 | 130 | * @param[in] i2c_adrs - 7-bit slave address of DS4424 |
phonemacro | 2:b7a81b724561 | 131 | * @param[in] ic_variant - which type of DS44xx is used |
phonemacro | 2:b7a81b724561 | 132 | * |
phonemacro | 2:b7a81b724561 | 133 | * On Exit: |
phonemacro | 2:b7a81b724561 | 134 | * |
phonemacro | 2:b7a81b724561 | 135 | * @return None |
phonemacro | 2:b7a81b724561 | 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 | 2:b7a81b724561 | 142 | * @brief Default destructor for DS4424 Class. |
phonemacro | 2:b7a81b724561 | 143 | * |
phonemacro | 2:b7a81b724561 | 144 | * @details Destroys I2C object if owner |
phonemacro | 2:b7a81b724561 | 145 | * |
phonemacro | 2:b7a81b724561 | 146 | * On Entry: |
phonemacro | 2:b7a81b724561 | 147 | * |
phonemacro | 2:b7a81b724561 | 148 | * On Exit: |
phonemacro | 2:b7a81b724561 | 149 | * |
phonemacro | 2:b7a81b724561 | 150 | * @return None |
phonemacro | 2:b7a81b724561 | 151 | **************************************************************/ |
phonemacro | 0:8002303900e6 | 152 | ~DS4424(); |
phonemacro | 0:8002303900e6 | 153 | |
phonemacro | 2:b7a81b724561 | 154 | /** |
phonemacro | 2:b7a81b724561 | 155 | * @brief Read raw value of a channel. Negative values are sink; Positive values are source. |
phonemacro | 2:b7a81b724561 | 156 | * @param result - Value that is read from the register |
phonemacro | 2:b7a81b724561 | 157 | * @param chan_addr - Channel 0 to 3 |
phonemacro | 2:b7a81b724561 | 158 | * @return 0 on success, negative number on failure |
phonemacro | 2:b7a81b724561 | 159 | */ |
phonemacro | 1:a5d963a3c298 | 160 | int read_raw(int32_t &result, ChannelRegAddr_e chan_addr); |
phonemacro | 0:8002303900e6 | 161 | |
phonemacro | 2:b7a81b724561 | 162 | /** |
phonemacro | 2:b7a81b724561 | 163 | * @brief Write raw value to a channel. |
phonemacro | 2:b7a81b724561 | 164 | * @param value - Value that is stored to the register |
phonemacro | 2:b7a81b724561 | 165 | * @param chan_addr - Channel 0 to 3 |
phonemacro | 2:b7a81b724561 | 166 | * @return 0 on success, negative number on failure |
phonemacro | 2:b7a81b724561 | 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 | 2:b7a81b724561 | 173 | * @brief Read register of device at slave address |
phonemacro | 2:b7a81b724561 | 174 | * @param reg - Register address |
phonemacro | 2:b7a81b724561 | 175 | * @param[out] value - Read data on success |
phonemacro | 2:b7a81b724561 | 176 | * @return 0 on success, negative number on failure |
phonemacro | 2:b7a81b724561 | 177 | */ |
phonemacro | 1:a5d963a3c298 | 178 | int read_register(ChannelRegAddr_e reg, uint8_t &value); |
phonemacro | 0:8002303900e6 | 179 | |
phonemacro | 0:8002303900e6 | 180 | /** |
phonemacro | 2:b7a81b724561 | 181 | * @brief Write register of device at slave address |
phonemacro | 2:b7a81b724561 | 182 | * @param reg - Register address |
phonemacro | 2:b7a81b724561 | 183 | * @param value - Value to write |
phonemacro | 2:b7a81b724561 | 184 | * @return 0 on success, negative number on failure |
phonemacro | 2:b7a81b724561 | 185 | */ |
phonemacro | 1:a5d963a3c298 | 186 | int write_register(ChannelRegAddr_e reg, uint8_t value); |
phonemacro | 0:8002303900e6 | 187 | |
phonemacro | 0:8002303900e6 | 188 | private: |
phonemacro | 2:b7a81b724561 | 189 | /** I2C object */ |
phonemacro | 0:8002303900e6 | 190 | I2C &m_i2c; |
phonemacro | 2:b7a81b724561 | 191 | /** Device slave addresses */ |
phonemacro | 0:8002303900e6 | 192 | uint8_t m_writeAddress, m_readAddress; |
phonemacro | 2:b7a81b724561 | 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 */ |