| 1 | /****************************************************************************** |
| 2 | * @file AD5172.c |
| 3 | * @brief Implementation of AD5172 Driver for Microblaze processor. |
| 4 | * @author Istvan Csomortani ([email protected]) |
| 5 | * |
| 6 | ******************************************************************************* |
| 7 | * Copyright 2013(c) Analog Devices, Inc. |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without modification, |
| 12 | * are permitted provided that the following conditions are met: |
| 13 | * - Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * - Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 20 | * contributors may be used to endorse or promote products derived |
| 21 | * from this software without specific prior written permission. |
| 22 | * - The use of this software may or may not infringe the patent rights |
| 23 | * of one or more patent holders. This license does not release you |
| 24 | * from the requirement that you obtain separate licenses from these |
| 25 | * patent holders to use this software. |
| 26 | * - Use of the software either in source or binary form, must be run |
| 27 | * on or directly connected to an Analog Devices Inc. component. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED |
| 30 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY |
| 31 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 32 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 34 | * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 35 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 36 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 38 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | * |
| 40 | ******************************************************************************/ |
| 41 | |
| 42 | /*****************************************************************************/ |
| 43 | /* Include Files */ |
| 44 | /*****************************************************************************/ |
| 45 | #include "ad5172.h" |
| 46 | #include "Communication.h" |
| 47 | |
| 48 | #define ADDR_MASK_1B 0x1 |
| 49 | #define ADDR_MASK_2B 0x3 |
| 50 | /*****************************************************************************/ |
| 51 | /* Private variables */ |
| 52 | /*****************************************************************************/ |
| 53 | struct ad5172_chip_info { |
| 54 | unsigned char num_channels; |
| 55 | comm_type_t comm_type; |
| 56 | }; |
| 57 | |
| 58 | static const struct ad5172_chip_info AD5172_CHIP_INFO[] = { |
| 59 | [ID_AD5162] = { |
| 60 | .num_channels = 2, |
| 61 | .comm_type = SPI, |
| 62 | }, |
| 63 | [ID_AD5172] = { |
| 64 | .num_channels = 2, |
| 65 | .comm_type = I2C, |
| 66 | }, |
| 67 | [ID_AD5173] = { |
| 68 | .num_channels = 2, |
| 69 | .comm_type = I2C, |
| 70 | }, |
| 71 | [ID_AD8403] = { |
| 72 | .num_channels = 4, |
| 73 | .comm_type = SPI, |
| 74 | }, |
| 75 | [ID_AD8402] = { |
| 76 | .num_channels = 2, |
| 77 | .comm_type = SPI, |
| 78 | }, |
| 79 | [ID_AD8400] = { |
| 80 | .num_channels = 1, |
| 81 | .comm_type = SPI, |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | /* Actual device type */ |
| 86 | AD5172_type_t this_device; |
| 87 | /**************************************************************************//** |
| 88 | * @brief Initialize SPI and Initial Values for AD5172 Board. |
| 89 | * |
| 90 | * @param The current device type. |
| 91 | * |
| 92 | * @return success |
| 93 | ******************************************************************************/ |
| 94 | char AD5172_Init(AD5172_type_t device) |
| 95 | { |
| 96 | char status = -1; |
| 97 | |
| 98 | this_device = device; |
| 99 | |
| 100 | if(AD5172_CHIP_INFO[this_device].comm_type == SPI) |
| 101 | { |
| 102 | status = SPI_Init(0, 100000, 0, 0); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | status = I2C_Init(100000); |
| 107 | } |
| 108 | |
| 109 | /* Deactivate Hardware Reset */ |
| 110 | AD5172_RESET_OUT; |
| 111 | AD5172_RESET_HIGH; |
| 112 | /* Deactivate Shutdown Pin */ |
| 113 | AD5172_SHUTDOWN_OUT; |
| 114 | AD5172_SHUTDOWN_HIGH; |
| 115 | |
| 116 | return status; |
| 117 | } |
| 118 | |
| 119 | /****************************************************************************** |
| 120 | * @brief Send a command to the device. |
| 121 | * |
| 122 | * @param channel - will be used to set the channel to be programmed |
| 123 | * (valid values 0-1) |
| 124 | * command - will be used to set special futures in case of |
| 125 | * AD5172/AD5173 |
| 126 | * @param data - will be used to set the value to be set on the channel |
| 127 | * (valid values 0-255) |
| 128 | * |
| 129 | * @return None |
| 130 | ******************************************************************************/ |
| 131 | |
| 132 | void AD5172_Write(unsigned char channel, |
| 133 | unsigned char command, |
| 134 | unsigned char data) |
| 135 | { |
| 136 | unsigned char dataBuffer[2] = {0,}; |
| 137 | unsigned char i2cSlaveAddr = 0; |
| 138 | |
| 139 | if(AD5172_CHIP_INFO[this_device].comm_type == SPI) |
| 140 | { |
| 141 | if(AD5172_CHIP_INFO[this_device].num_channels == 2) |
| 142 | { |
| 143 | dataBuffer[0] = channel & ADDR_MASK_1B; |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | dataBuffer[0] = channel & ADDR_MASK_2B; |
| 148 | } |
| 149 | |
| 150 | dataBuffer[1] = data; |
| 151 | SPI_Write(AD5172_SLAVE_ID, |
| 152 | dataBuffer, |
| 153 | 2); |
| 154 | } |
| 155 | else |
| 156 | { /* Setup I2C address */ |
| 157 | i2cSlaveAddr |= (this_device == ID_AD5173) ? (AD5172_I2C_PIN_ADDR1 << 1) | \ |
| 158 | AD5172_I2C_PIN_ADDR0 : \ |
| 159 | AD5172_I2C_PIN_ADDR_MASK; |
| 160 | i2cSlaveAddr |= ((AD5172_I2C_HARD_ADDR << 2)); |
| 161 | dataBuffer[0] |= (channel << AD5172_CNTR_A0_OFFSET) | \ |
| 162 | (command & AD5172_CNTR_MASK); |
| 163 | dataBuffer[1] = data; |
| 164 | |
| 165 | I2C_Write(i2cSlaveAddr, |
| 166 | dataBuffer, |
| 167 | 2, |
| 168 | 1); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /****************************************************************************** |
| 173 | * @brief Reading data from a previously selected RDAC register in write mode, |
| 174 | * the fuseStatus will store the OTP validation bits |
| 175 | * |
| 176 | * @param channel - will be used to set the channel to be programmed |
| 177 | * (valid values 0-1) |
| 178 | * fuseStatus - a char pointer, will be used to store the OTP |
| 179 | * validation bits |
| 180 | * |
| 181 | * @return The value of the previously selected wiper register |
| 182 | ******************************************************************************/ |
| 183 | short AD5172_Read(unsigned char channel, |
| 184 | unsigned char* fuseStatus) |
| 185 | { |
| 186 | |
| 187 | unsigned char dataBuffer[2] = {0,}; |
| 188 | unsigned char i2cSlaveAddr = 0; |
| 189 | unsigned char readStatus = 0; |
| 190 | |
| 191 | if(AD5172_CHIP_INFO[this_device].comm_type == I2C) |
| 192 | { |
| 193 | /* Setup I2C address pin-programmable address bits */ |
| 194 | i2cSlaveAddr |= (this_device == ID_AD5173) ? (AD5172_I2C_PIN_ADDR1 << 1) |\ |
| 195 | AD5172_I2C_PIN_ADDR0 : \ |
| 196 | AD5172_I2C_PIN_ADDR_MASK; |
| 197 | /* Setup the read bit */ |
| 198 | i2cSlaveAddr |= (AD5172_I2C_HARD_ADDR << 2); |
| 199 | readStatus = I2C_Read(i2cSlaveAddr, |
| 200 | dataBuffer, |
| 201 | 2, |
| 202 | 1); |
| 203 | *fuseStatus = (dataBuffer[1] >> AD5172_OTP_VALID_OFFSET) & \ |
| 204 | AD5172_OTP_VALID_MASK; |
| 205 | |
| 206 | if(!readStatus) |
| 207 | { |
| 208 | return -1; |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | return (short)dataBuffer[0]; |
| 213 | } |
| 214 | } |
| 215 | return 0; |
| 216 | } |