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