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:
1:cdaea8ff359c
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 * 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 1:cdaea8ff359c 55 DS4424::DS4424(I2C &i2c_bus, DS4424_i2c_adrs_t slaveAddress, DS4424_ic_t ic_variant):
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 1:cdaea8ff359c 60 switch(ic_variant) {
phonemacro 1:cdaea8ff359c 61 case DS4424_IC:
phonemacro 1:cdaea8ff359c 62 m_max_ch_reg_addr = REG_OUT3;
phonemacro 1:cdaea8ff359c 63 break;
phonemacro 1:cdaea8ff359c 64 case DS4422_IC:
phonemacro 1:cdaea8ff359c 65 m_max_ch_reg_addr = REG_OUT1;
phonemacro 1:cdaea8ff359c 66 break;
phonemacro 1:cdaea8ff359c 67 default:
phonemacro 1:cdaea8ff359c 68 m_max_ch_reg_addr = REG_OUT3;
phonemacro 1:cdaea8ff359c 69 break;
phonemacro 1:cdaea8ff359c 70 }
phonemacro 0:a010119890ed 71 }
phonemacro 0:a010119890ed 72
phonemacro 0:a010119890ed 73
phonemacro 0:a010119890ed 74 //******************************************************************************
phonemacro 0:a010119890ed 75 DS4424::~DS4424(void)
phonemacro 0:a010119890ed 76 {
phonemacro 0:a010119890ed 77 //empty block
phonemacro 0:a010119890ed 78 }
phonemacro 0:a010119890ed 79
phonemacro 0:a010119890ed 80
phonemacro 0:a010119890ed 81 //******************************************************************************
phonemacro 3:c40703da0faf 82 int DS4424::read_raw(int32_t &result, ChannelRegAddr_e chan_addr)
phonemacro 0:a010119890ed 83 {
phonemacro 0:a010119890ed 84 uint8_t value;
phonemacro 0:a010119890ed 85 int ret = DS4424_ERROR;
phonemacro 0:a010119890ed 86 union ds4424_raw_data raw;
phonemacro 0:a010119890ed 87
phonemacro 1:cdaea8ff359c 88 if (chan_addr >= REG_OUT0 && chan_addr <= m_max_ch_reg_addr)
phonemacro 3:c40703da0faf 89 ret = read_register(chan_addr, value);
phonemacro 0:a010119890ed 90 if (ret != 0)
phonemacro 0:a010119890ed 91 return DS4424_ERROR;
phonemacro 1:cdaea8ff359c 92
phonemacro 0:a010119890ed 93
phonemacro 0:a010119890ed 94 raw.bits = value;
phonemacro 0:a010119890ed 95 result = raw.dx;
phonemacro 0:a010119890ed 96 if (raw.source_bit == DS4424_SINK_I) // Sinking will be negative values
phonemacro 0:a010119890ed 97 result = -result;
phonemacro 0:a010119890ed 98 return DS4424_NO_ERROR;
phonemacro 0:a010119890ed 99
phonemacro 0:a010119890ed 100 }
phonemacro 0:a010119890ed 101
phonemacro 1:cdaea8ff359c 102
phonemacro 0:a010119890ed 103 //******************************************************************************
phonemacro 3:c40703da0faf 104 int DS4424::write_raw(int32_t value, ChannelRegAddr_e chan_addr)
phonemacro 0:a010119890ed 105 {
phonemacro 0:a010119890ed 106 #define U8_MAX ((uint8_t)~0U)
phonemacro 0:a010119890ed 107 #define S8_MAX ((int8_t)(U8_MAX>>1))
phonemacro 0:a010119890ed 108 #define S8_MIN ((int8_t)(-S8_MAX - 1))
phonemacro 0:a010119890ed 109 int ret = DS4424_ERROR;
phonemacro 0:a010119890ed 110 union ds4424_raw_data raw;
phonemacro 0:a010119890ed 111
phonemacro 1:cdaea8ff359c 112 if ((chan_addr >= REG_OUT0 && chan_addr <= m_max_ch_reg_addr) &&
phonemacro 0:a010119890ed 113 (value >= S8_MIN && value <= S8_MAX)) {
phonemacro 0:a010119890ed 114 if (value > 0) {
phonemacro 0:a010119890ed 115 raw.source_bit = DS4424_SOURCE_I;
phonemacro 0:a010119890ed 116 raw.dx = value;
phonemacro 0:a010119890ed 117 } else {
phonemacro 0:a010119890ed 118 raw.source_bit = DS4424_SINK_I;
phonemacro 0:a010119890ed 119 raw.dx = -value;
phonemacro 0:a010119890ed 120 }
phonemacro 3:c40703da0faf 121 ret = write_register(chan_addr, raw.bits);
phonemacro 0:a010119890ed 122
phonemacro 0:a010119890ed 123 if (ret != 0)
phonemacro 0:a010119890ed 124 return DS4424_ERROR;
phonemacro 0:a010119890ed 125 return DS4424_NO_ERROR;
phonemacro 0:a010119890ed 126 } else {
phonemacro 0:a010119890ed 127 return DS4424_ERROR;
phonemacro 0:a010119890ed 128 }
phonemacro 0:a010119890ed 129 }
phonemacro 0:a010119890ed 130
phonemacro 0:a010119890ed 131
phonemacro 0:a010119890ed 132 //******************************************************************************
phonemacro 3:c40703da0faf 133 int DS4424::read_register(ChannelRegAddr_e reg, uint8_t &value)
phonemacro 0:a010119890ed 134 {
phonemacro 0:a010119890ed 135 int32_t ret;
phonemacro 0:a010119890ed 136
phonemacro 0:a010119890ed 137 char data = reg;
phonemacro 0:a010119890ed 138
phonemacro 0:a010119890ed 139 ret = m_i2c.write(m_writeAddress, &data, sizeof(data));
phonemacro 0:a010119890ed 140 if (ret != 0) {
phonemacro 0:a010119890ed 141 printf("%s - failed - ret: %d reg: %d data: %d sizeof(data): %d\r\n", __func__, ret, (int)reg, data, sizeof(data));
phonemacro 0:a010119890ed 142 return DS4424_ERROR;
phonemacro 0:a010119890ed 143 }
phonemacro 0:a010119890ed 144
phonemacro 0:a010119890ed 145 ret = m_i2c.read(m_readAddress, &data, sizeof(data));
phonemacro 0:a010119890ed 146 if (ret != 0) {
phonemacro 0:a010119890ed 147 printf("%s - failed - ret: %d\r\n", __func__, ret);
phonemacro 0:a010119890ed 148 return DS4424_ERROR;
phonemacro 0:a010119890ed 149 }
phonemacro 0:a010119890ed 150
phonemacro 0:a010119890ed 151 value = data;
phonemacro 0:a010119890ed 152 return DS4424_NO_ERROR;
phonemacro 0:a010119890ed 153
phonemacro 0:a010119890ed 154 }
phonemacro 0:a010119890ed 155
phonemacro 0:a010119890ed 156
phonemacro 0:a010119890ed 157 //******************************************************************************
phonemacro 3:c40703da0faf 158 int DS4424::write_register(ChannelRegAddr_e reg, uint8_t value)
phonemacro 0:a010119890ed 159 {
phonemacro 0:a010119890ed 160
phonemacro 0:a010119890ed 161 int32_t ret;
phonemacro 0:a010119890ed 162
phonemacro 0:a010119890ed 163 char cmdData[] = {static_cast<char>(reg), static_cast<char>(value)};
phonemacro 0:a010119890ed 164
phonemacro 0:a010119890ed 165 ret = m_i2c.write(m_writeAddress, cmdData, sizeof(cmdData));
phonemacro 0:a010119890ed 166
phonemacro 0:a010119890ed 167 if (ret != 0)
phonemacro 0:a010119890ed 168 return DS4424_ERROR;
phonemacro 0:a010119890ed 169
phonemacro 0:a010119890ed 170 return DS4424_NO_ERROR;
phonemacro 0:a010119890ed 171 }