| 1 | /***************************************************************************//** |
| 2 | * @file AD8366.c |
| 3 | * @brief Implementation of AD8366 Driver. |
| 4 | * @author DBogdan ([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2012(c) Analog Devices, Inc. |
| 7 | * |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions are met: |
| 12 | * - Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * - Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * - The use of this software may or may not infringe the patent rights |
| 22 | * of one or more patent holders. This license does not release you |
| 23 | * from the requirement that you obtain separate licenses from these |
| 24 | * patent holders to use this software. |
| 25 | * - Use of the software either in source or binary form, must be run |
| 26 | * on or directly connected to an Analog Devices Inc. component. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
| 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | * |
| 39 | ******************************************************************************** |
| 40 | * SVN Revision: $WCREV$ |
| 41 | *******************************************************************************/ |
| 42 | |
| 43 | /******************************************************************************/ |
| 44 | /***************************** Include Files **********************************/ |
| 45 | /******************************************************************************/ |
| 46 | #include "spi_interface.h" |
| 47 | #include "AD8366.h" |
| 48 | |
| 49 | /******************************************************************************/ |
| 50 | /************************ Local variables and types ***************************/ |
| 51 | /******************************************************************************/ |
| 52 | struct ad8366_state |
| 53 | { |
| 54 | uint8_t ch[2]; |
| 55 | }ad8366_st; |
| 56 | |
| 57 | /***************************************************************************//** |
| 58 | * @brief Initializes the AD8366. |
| 59 | * |
| 60 | * @return Negative error code or 0 in case of success. |
| 61 | *******************************************************************************/ |
| 62 | int32_t ad8366_setup() |
| 63 | { |
| 64 | int32_t ret; |
| 65 | |
| 66 | /* Sets the gain of channel A to 4.5 dB */ |
| 67 | ret = ad8366_out_voltage0_hardwaregain(4500); |
| 68 | if(ret < 0) |
| 69 | return -1; |
| 70 | |
| 71 | /* Sets the gain of channel B to 4.5 dB */ |
| 72 | ret = ad8366_out_voltage1_hardwaregain(4500); |
| 73 | if(ret < 0) |
| 74 | return -1; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /***************************************************************************//** |
| 80 | * @brief Writes data nto the AD8366 |
| 81 | * |
| 82 | * @param chAgain - gain value for channel A |
| 83 | * @param chBgain - gain value for channel B |
| 84 | * |
| 85 | * @return Returns 0 in case of success or negative error code |
| 86 | *******************************************************************************/ |
| 87 | int32_t ad8366_write(uint8_t chAgain, uint8_t chBgain) |
| 88 | { |
| 89 | uint8_t bit = 0; |
| 90 | uint8_t reversedChAgain = 0; |
| 91 | uint8_t reversedChBgain = 0; |
| 92 | uint8_t regAddr = 0; |
| 93 | uint16_t regValue = 0; |
| 94 | |
| 95 | /* Maximum value of the gain can be 0x3F. */ |
| 96 | chAgain = (chAgain & 0x3F); |
| 97 | chBgain = (chBgain & 0x3F); |
| 98 | /* AD8366 accepts the LSB first transfer format. */ |
| 99 | for(bit = 0; bit < 8; bit++) |
| 100 | { |
| 101 | reversedChAgain = (reversedChAgain << 1) + (chAgain & 1); |
| 102 | reversedChBgain = (reversedChBgain << 1) + (chBgain & 1); |
| 103 | chAgain >>= 1; |
| 104 | chBgain >>= 1; |
| 105 | } |
| 106 | |
| 107 | regValue = (reversedChBgain << 4) | (reversedChAgain >> 2); |
| 108 | return SPI_Write(SPI_SEL_AD8366, regAddr, regValue); |
| 109 | } |
| 110 | |
| 111 | /***************************************************************************//** |
| 112 | * @brief Reads data from the AD8366 |
| 113 | * |
| 114 | * @param channel - channel number (0 or 1) |
| 115 | * @param val1000 - pointer to store the data x1000 |
| 116 | * @param m - the code of the data to read |
| 117 | * |
| 118 | * @return Returns 0 in case of success or negative error code |
| 119 | *******************************************************************************/ |
| 120 | int32_t ad8366_read_raw(int32_t channel, |
| 121 | int32_t *val1000, |
| 122 | int32_t m) |
| 123 | { |
| 124 | struct ad8366_state *st = &ad8366_st; |
| 125 | int32_t ret; |
| 126 | uint32_t code; |
| 127 | |
| 128 | switch (m) |
| 129 | { |
| 130 | case 0: |
| 131 | code = st->ch[channel]; |
| 132 | |
| 133 | /* Values in dB */ |
| 134 | *val1000 = code * 253 + 4500; |
| 135 | ret = 0; |
| 136 | break; |
| 137 | default: |
| 138 | ret = -1; |
| 139 | } |
| 140 | |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | /***************************************************************************//** |
| 145 | * @brief Writes data to the AD8366 |
| 146 | * |
| 147 | * @param channel - channel number (0 or 1) |
| 148 | * @param val1000 - the value to write x1000 |
| 149 | * @param m - the code of the data to write |
| 150 | * |
| 151 | * @return Returns 0 in case of success or negative error code |
| 152 | *******************************************************************************/ |
| 153 | static int32_t ad8366_write_raw(int32_t channel, |
| 154 | int32_t val1000, |
| 155 | long mask) |
| 156 | { |
| 157 | struct ad8366_state *st = &ad8366_st; |
| 158 | uint32_t code; |
| 159 | int32_t ret; |
| 160 | |
| 161 | /* Values in dB */ |
| 162 | if (val1000 > AD8366_MAX_GAIN || val1000 < AD8366_MIN_GAIN) |
| 163 | return -1; |
| 164 | |
| 165 | code = (val1000 - 4500) / 253; |
| 166 | |
| 167 | if((val1000 - (4500 + code*253)) > ((4500 + (code+1)*253) - val1000)) |
| 168 | { |
| 169 | code++; |
| 170 | } |
| 171 | |
| 172 | switch (mask) |
| 173 | { |
| 174 | case 0: |
| 175 | st->ch[channel] = code; |
| 176 | ret = ad8366_write(st->ch[0], st->ch[1]); |
| 177 | break; |
| 178 | default: |
| 179 | ret = -1; |
| 180 | } |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | /***************************************************************************//** |
| 186 | * @brief Sets the gain of channel A. |
| 187 | * |
| 188 | * @param gain_dB - the gain to be set in dB * 1000 |
| 189 | * |
| 190 | * @return Returns the actual set gain * 1000 or negative error code |
| 191 | *******************************************************************************/ |
| 192 | int32_t ad8366_out_voltage0_hardwaregain(int32_t gain1000_dB) |
| 193 | { |
| 194 | int32_t ret; |
| 195 | int32_t act_gain1000; |
| 196 | |
| 197 | if((gain1000_dB <= AD8366_MAX_GAIN) && (gain1000_dB >= AD8366_MIN_GAIN)) |
| 198 | { |
| 199 | ret = ad8366_write_raw(0, gain1000_dB, 0); |
| 200 | if(ret < 0) |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | ret = ad8366_read_raw(0, &act_gain1000, 0); |
| 205 | if(ret < 0) |
| 206 | return -1; |
| 207 | |
| 208 | return act_gain1000; |
| 209 | } |
| 210 | |
| 211 | /***************************************************************************//** |
| 212 | * @brief Sets the gain of channel B. |
| 213 | * |
| 214 | * @param gain1000_dB - the gain to be set in dB * 1000 |
| 215 | * |
| 216 | * @return Returns the actual set gain * 1000 or negative error code |
| 217 | *******************************************************************************/ |
| 218 | int32_t ad8366_out_voltage1_hardwaregain(int32_t gain1000_dB) |
| 219 | { |
| 220 | int32_t ret; |
| 221 | int32_t act_gain1000; |
| 222 | |
| 223 | if((gain1000_dB <= AD8366_MAX_GAIN) && (gain1000_dB >= AD8366_MIN_GAIN)) |
| 224 | { |
| 225 | ret = ad8366_write_raw(1, gain1000_dB, 0); |
| 226 | if(ret < 0) |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | ret = ad8366_read_raw(1, &act_gain1000, 0); |
| 231 | if(ret < 0) |
| 232 | return -1; |
| 233 | |
| 234 | return act_gain1000; |
| 235 | } |