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:
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?

UserRevisionLine numberNew contents of line
phonemacro 0:8002303900e6 1 /*******************************************************************************
phonemacro 0:8002303900e6 2 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 0:8002303900e6 3 *
phonemacro 0:8002303900e6 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 0:8002303900e6 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 0:8002303900e6 6 * to deal in the Software without restriction, including without limitation
phonemacro 0:8002303900e6 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 0:8002303900e6 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 0:8002303900e6 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 0:8002303900e6 10 *
phonemacro 0:8002303900e6 11 * The above copyright notice and this permission notice shall be included
phonemacro 0:8002303900e6 12 * in all copies or substantial portions of the Software.
phonemacro 0:8002303900e6 13 *
phonemacro 0:8002303900e6 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 0:8002303900e6 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 0:8002303900e6 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 0:8002303900e6 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 0:8002303900e6 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 0:8002303900e6 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 0:8002303900e6 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 0:8002303900e6 21 *
phonemacro 0:8002303900e6 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 0:8002303900e6 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 0:8002303900e6 24 * Products, Inc. Branding Policy.
phonemacro 0:8002303900e6 25 *
phonemacro 0:8002303900e6 26 * The mere transfer of this software does not imply any licenses
phonemacro 0:8002303900e6 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 0:8002303900e6 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 0:8002303900e6 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 0:8002303900e6 30 * ownership rights.
phonemacro 0:8002303900e6 31 *******************************************************************************
phonemacro 0:8002303900e6 32 */
phonemacro 0:8002303900e6 33
phonemacro 0:8002303900e6 34
phonemacro 0:8002303900e6 35 #include "DS4424.h"
phonemacro 0:8002303900e6 36
phonemacro 0:8002303900e6 37 /*
phonemacro 0:8002303900e6 38 * DS4424 DAC control register 8 bits
phonemacro 0:8002303900e6 39 * [7] 0: to sink; 1: to source
phonemacro 0:8002303900e6 40 * [6:0] steps to sink/source
phonemacro 0:8002303900e6 41 * bit[7] looks like a sign bit, but the value of the register is
phonemacro 0:8002303900e6 42 * not a two's complement code considering the bit[6:0] is a absolute
phonemacro 0:8002303900e6 43 * distance from the zero point.
phonemacro 0:8002303900e6 44 */
phonemacro 0:8002303900e6 45 union ds4424_raw_data {
phonemacro 0:8002303900e6 46 struct {
phonemacro 0:8002303900e6 47 uint8_t dx:7;
phonemacro 0:8002303900e6 48 uint8_t source_bit:1;
phonemacro 0:8002303900e6 49 };
phonemacro 0:8002303900e6 50 uint8_t bits;
phonemacro 0:8002303900e6 51 };
phonemacro 0:8002303900e6 52
phonemacro 0:8002303900e6 53 //******************************************************************************
phonemacro 0:8002303900e6 54
phonemacro 0:8002303900e6 55 DS4424::DS4424(I2C &i2c_bus, DS4424_i2c_adrs_t slaveAddress, DS4424_ic_t ic_variant):
phonemacro 0:8002303900e6 56 m_i2c(i2c_bus),
phonemacro 0:8002303900e6 57 m_writeAddress(slaveAddress <<1),
phonemacro 0:8002303900e6 58 m_readAddress ((slaveAddress << 1) | 1)
phonemacro 0:8002303900e6 59 {
phonemacro 0:8002303900e6 60 switch(ic_variant) {
phonemacro 0:8002303900e6 61 case DS4424_IC:
phonemacro 0:8002303900e6 62 m_max_ch_reg_addr = REG_OUT3;
phonemacro 0:8002303900e6 63 break;
phonemacro 0:8002303900e6 64 case DS4422_IC:
phonemacro 0:8002303900e6 65 m_max_ch_reg_addr = REG_OUT1;
phonemacro 0:8002303900e6 66 break;
phonemacro 0:8002303900e6 67 default:
phonemacro 0:8002303900e6 68 m_max_ch_reg_addr = REG_OUT3;
phonemacro 0:8002303900e6 69 break;
phonemacro 0:8002303900e6 70 }
phonemacro 0:8002303900e6 71 }
phonemacro 0:8002303900e6 72
phonemacro 0:8002303900e6 73
phonemacro 0:8002303900e6 74 //******************************************************************************
phonemacro 0:8002303900e6 75 DS4424::~DS4424(void)
phonemacro 0:8002303900e6 76 {
phonemacro 0:8002303900e6 77 //empty block
phonemacro 0:8002303900e6 78 }
phonemacro 0:8002303900e6 79
phonemacro 0:8002303900e6 80
phonemacro 0:8002303900e6 81 //******************************************************************************
phonemacro 0:8002303900e6 82 int DS4424::readRaw(int32_t &result, ChannelRegAddr_e chan_addr)
phonemacro 0:8002303900e6 83 {
phonemacro 0:8002303900e6 84 uint8_t value;
phonemacro 0:8002303900e6 85 int ret = DS4424_ERROR;
phonemacro 0:8002303900e6 86 union ds4424_raw_data raw;
phonemacro 0:8002303900e6 87
phonemacro 0:8002303900e6 88 if (chan_addr >= REG_OUT0 && chan_addr <= m_max_ch_reg_addr)
phonemacro 0:8002303900e6 89 ret = readRegister(chan_addr, value);
phonemacro 0:8002303900e6 90 if (ret != 0)
phonemacro 0:8002303900e6 91 return DS4424_ERROR;
phonemacro 0:8002303900e6 92
phonemacro 0:8002303900e6 93
phonemacro 0:8002303900e6 94 raw.bits = value;
phonemacro 0:8002303900e6 95 result = raw.dx;
phonemacro 0:8002303900e6 96 if (raw.source_bit == DS4424_SINK_I) // Sinking will be negative values
phonemacro 0:8002303900e6 97 result = -result;
phonemacro 0:8002303900e6 98 return DS4424_NO_ERROR;
phonemacro 0:8002303900e6 99
phonemacro 0:8002303900e6 100 }
phonemacro 0:8002303900e6 101
phonemacro 0:8002303900e6 102
phonemacro 0:8002303900e6 103 //******************************************************************************
phonemacro 0:8002303900e6 104 int DS4424::writeRaw(int32_t value, ChannelRegAddr_e chan_addr)
phonemacro 0:8002303900e6 105 {
phonemacro 0:8002303900e6 106 #define U8_MAX ((uint8_t)~0U)
phonemacro 0:8002303900e6 107 #define S8_MAX ((int8_t)(U8_MAX>>1))
phonemacro 0:8002303900e6 108 #define S8_MIN ((int8_t)(-S8_MAX - 1))
phonemacro 0:8002303900e6 109 int ret = DS4424_ERROR;
phonemacro 0:8002303900e6 110 union ds4424_raw_data raw;
phonemacro 0:8002303900e6 111
phonemacro 0:8002303900e6 112 if ((chan_addr >= REG_OUT0 && chan_addr <= m_max_ch_reg_addr) &&
phonemacro 0:8002303900e6 113 (value >= S8_MIN && value <= S8_MAX)) {
phonemacro 0:8002303900e6 114 if (value > 0) {
phonemacro 0:8002303900e6 115 raw.source_bit = DS4424_SOURCE_I;
phonemacro 0:8002303900e6 116 raw.dx = value;
phonemacro 0:8002303900e6 117 } else {
phonemacro 0:8002303900e6 118 raw.source_bit = DS4424_SINK_I;
phonemacro 0:8002303900e6 119 raw.dx = -value;
phonemacro 0:8002303900e6 120 }
phonemacro 0:8002303900e6 121 ret = writeRegister(chan_addr, raw.bits);
phonemacro 0:8002303900e6 122
phonemacro 0:8002303900e6 123 if (ret != 0)
phonemacro 0:8002303900e6 124 return DS4424_ERROR;
phonemacro 0:8002303900e6 125 return DS4424_NO_ERROR;
phonemacro 0:8002303900e6 126 } else {
phonemacro 0:8002303900e6 127 return DS4424_ERROR;
phonemacro 0:8002303900e6 128 }
phonemacro 0:8002303900e6 129 }
phonemacro 0:8002303900e6 130
phonemacro 0:8002303900e6 131
phonemacro 0:8002303900e6 132 //******************************************************************************
phonemacro 0:8002303900e6 133 int DS4424::readRegister(ChannelRegAddr_e reg, uint8_t &value)
phonemacro 0:8002303900e6 134 {
phonemacro 0:8002303900e6 135 int32_t ret;
phonemacro 0:8002303900e6 136
phonemacro 0:8002303900e6 137 char data = reg;
phonemacro 0:8002303900e6 138
phonemacro 0:8002303900e6 139 ret = m_i2c.write(m_writeAddress, &data, sizeof(data));
phonemacro 0:8002303900e6 140 if (ret != 0) {
phonemacro 0:8002303900e6 141 printf("%s - failed - ret: %d reg: %d data: %d sizeof(data): %d\r\n", __func__, ret, (int)reg, data, sizeof(data));
phonemacro 0:8002303900e6 142 return DS4424_ERROR;
phonemacro 0:8002303900e6 143 }
phonemacro 0:8002303900e6 144
phonemacro 0:8002303900e6 145 ret = m_i2c.read(m_readAddress, &data, sizeof(data));
phonemacro 0:8002303900e6 146 if (ret != 0) {
phonemacro 0:8002303900e6 147 printf("%s - failed - ret: %d\r\n", __func__, ret);
phonemacro 0:8002303900e6 148 return DS4424_ERROR;
phonemacro 0:8002303900e6 149 }
phonemacro 0:8002303900e6 150
phonemacro 0:8002303900e6 151 value = data;
phonemacro 0:8002303900e6 152 return DS4424_NO_ERROR;
phonemacro 0:8002303900e6 153
phonemacro 0:8002303900e6 154 }
phonemacro 0:8002303900e6 155
phonemacro 0:8002303900e6 156
phonemacro 0:8002303900e6 157 //******************************************************************************
phonemacro 0:8002303900e6 158 int DS4424::writeRegister(ChannelRegAddr_e reg, uint8_t value)
phonemacro 0:8002303900e6 159 {
phonemacro 0:8002303900e6 160
phonemacro 0:8002303900e6 161 int32_t ret;
phonemacro 0:8002303900e6 162
phonemacro 0:8002303900e6 163 char cmdData[] = {static_cast<char>(reg), static_cast<char>(value)};
phonemacro 0:8002303900e6 164
phonemacro 0:8002303900e6 165 ret = m_i2c.write(m_writeAddress, cmdData, sizeof(cmdData));
phonemacro 0:8002303900e6 166
phonemacro 0:8002303900e6 167 if (ret != 0)
phonemacro 0:8002303900e6 168 return DS4424_ERROR;
phonemacro 0:8002303900e6 169
phonemacro 0:8002303900e6 170 return DS4424_NO_ERROR;
phonemacro 0:8002303900e6 171 }