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