| 1 | /**************************************************************************//** |
| 2 | * @file AD5110.c |
| 3 | * @brief Implementation of AD5110 Driver for Microblaze processor. |
| 4 | * @author Lucian Sin ([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 "ad5110.h" |
| 46 | #include "Communication.h" |
| 47 | |
| 48 | /******************************************************************************/ |
| 49 | /************************ Variables Definitions *******************************/ |
| 50 | /******************************************************************************/ |
| 51 | unsigned char ad5110DevAddr = 0; |
| 52 | |
| 53 | /***************************************************************************//** |
| 54 | * @brief Initializes the communication with the device. |
| 55 | * |
| 56 | * @param devAddr - ad5110 device address. |
| 57 | * Example: ad5110_I2C_ADDR - Model ad5110 BCPZ Y; |
| 58 | * ad5110_1_I2C_ADDR - Model ad5110 BCPZ Y-1. |
| 59 | * @return status - Result of the initialization procedure. |
| 60 | * Example: 0 - if initialization was successful; |
| 61 | * -1 - if initialization was unsuccessful. |
| 62 | *******************************************************************************/ |
| 63 | char ad5110_Init(unsigned char devAddr) |
| 64 | { |
| 65 | char status = -1; |
| 66 | |
| 67 | ad5110DevAddr = devAddr; |
| 68 | status = I2C_Init(100000); |
| 69 | |
| 70 | return status; |
| 71 | } |
| 72 | |
| 73 | /***************************************************************************//** |
| 74 | * @brief Write the content of serial register data to RDAC. |
| 75 | * |
| 76 | * @param rdacValue - Value of the serial register. |
| 77 | * |
| 78 | * @return none. |
| 79 | *******************************************************************************/ |
| 80 | void ad5110_WriteRdac(unsigned char rdacValue) |
| 81 | { |
| 82 | unsigned char dataBuffer[2]; |
| 83 | |
| 84 | dataBuffer[0] = CMD_WR_RDAC; |
| 85 | dataBuffer[1] = rdacValue; |
| 86 | I2C_Write(ad5110DevAddr, |
| 87 | dataBuffer, |
| 88 | 2, |
| 89 | 1); |
| 90 | } |
| 91 | |
| 92 | /***************************************************************************//** |
| 93 | * @brief Read the content of RDAC register. |
| 94 | * |
| 95 | * @return rdacValue - Value read from register. |
| 96 | *******************************************************************************/ |
| 97 | unsigned char ad5110_ReadRdac(void) |
| 98 | { |
| 99 | unsigned char dataBuffer[2]; |
| 100 | |
| 101 | dataBuffer[0] = CMD_RD_RDAC; |
| 102 | dataBuffer[1] = 0; |
| 103 | I2C_Write(ad5110DevAddr, |
| 104 | dataBuffer, |
| 105 | 2, |
| 106 | 1); |
| 107 | |
| 108 | I2C_Read(ad5110DevAddr, |
| 109 | dataBuffer, |
| 110 | 1, |
| 111 | 1); |
| 112 | |
| 113 | return dataBuffer[0]; |
| 114 | } |
| 115 | |
| 116 | /***************************************************************************//** |
| 117 | * @brief Write the content of RDAC register to EEPROM. |
| 118 | * |
| 119 | * @return none. |
| 120 | *******************************************************************************/ |
| 121 | void ad5110_WriteRdacEeprom(void) |
| 122 | { |
| 123 | unsigned char dataBuffer[2]; |
| 124 | |
| 125 | dataBuffer[0] = CMD_WR_RDAC_EEPROM; |
| 126 | dataBuffer[1] = 0; |
| 127 | I2C_Write(ad5110DevAddr, |
| 128 | dataBuffer, |
| 129 | 2, |
| 130 | 1); |
| 131 | } |
| 132 | |
| 133 | /***************************************************************************//** |
| 134 | * @brief Read wiper position from EEPROM. |
| 135 | * |
| 136 | * @return wiperValue - Value read from EEPROM. |
| 137 | *******************************************************************************/ |
| 138 | unsigned char ad5110_ReadWiper(void) |
| 139 | { |
| 140 | unsigned char dataBuffer[2]; |
| 141 | |
| 142 | dataBuffer[0] = CMD_RD_EEPROM; |
| 143 | dataBuffer[1] = WIPER_POSITION; |
| 144 | I2C_Write(ad5110DevAddr, |
| 145 | dataBuffer, |
| 146 | 2, |
| 147 | 1); |
| 148 | |
| 149 | I2C_Read(ad5110DevAddr, |
| 150 | dataBuffer, |
| 151 | 1, |
| 152 | 1); |
| 153 | |
| 154 | return dataBuffer[0]; |
| 155 | } |
| 156 | |
| 157 | /***************************************************************************//** |
| 158 | * @brief Read resistor tolerance from EEPROM. |
| 159 | * |
| 160 | * @return resTolerance - Value read from EEPROM. |
| 161 | *******************************************************************************/ |
| 162 | unsigned char ad5110_ReadResTolerance(void) |
| 163 | { |
| 164 | unsigned char dataBuffer[2]; |
| 165 | |
| 166 | dataBuffer[0] = CMD_RD_EEPROM; |
| 167 | dataBuffer[1] = RESISTOR_TOLERANCE; |
| 168 | I2C_Write(ad5110DevAddr, |
| 169 | dataBuffer, |
| 170 | 2, |
| 171 | 1); |
| 172 | |
| 173 | I2C_Read(ad5110DevAddr, |
| 174 | dataBuffer, |
| 175 | 1, |
| 176 | 1); |
| 177 | |
| 178 | return dataBuffer[0]; |
| 179 | } |
| 180 | |
| 181 | /***************************************************************************//** |
| 182 | * @brief Software reset; makes a refresh of RDAC register with EEPROM. |
| 183 | * |
| 184 | * @return none. |
| 185 | *******************************************************************************/ |
| 186 | void ad5110_Reset(void) |
| 187 | { |
| 188 | unsigned char dataBuffer[2]; |
| 189 | |
| 190 | dataBuffer[0] = CMD_RESET; |
| 191 | dataBuffer[1] = 0; |
| 192 | I2C_Write(ad5110DevAddr, |
| 193 | dataBuffer, |
| 194 | 2, |
| 195 | 1); |
| 196 | } |
| 197 | |
| 198 | /***************************************************************************//** |
| 199 | * @brief Software shutdown. |
| 200 | * |
| 201 | * @param value - the value written to shut down register. |
| 202 | * Example: ShutDownOff - the device is on. |
| 203 | * ShutDownOn - the device is shut down. |
| 204 | * |
| 205 | * @return none. |
| 206 | *******************************************************************************/ |
| 207 | void ad5110_ShutDown(unsigned char value) |
| 208 | { |
| 209 | unsigned char dataBuffer[2]; |
| 210 | |
| 211 | dataBuffer[0] = CMD_SHUT_DOWN; |
| 212 | dataBuffer[1] = value; |
| 213 | I2C_Write(ad5110DevAddr, |
| 214 | dataBuffer, |
| 215 | 2, |
| 216 | 1); |
| 217 | } |