Driver for the AD5270 digipot

Dependents:   ad5270-helloworld CN0357 CN0396

For additional information check out the mbed page of the Analog Devices wiki: https://wiki.analog.com/resources/tools-software/mbed-drivers-all

Committer:
adisuciu
Date:
Tue May 03 12:55:30 2016 +0000
Revision:
0:18d434f6f122
Child:
1:9f9dd8c48e8a
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adisuciu 0:18d434f6f122 1 /**
adisuciu 0:18d434f6f122 2 * @file AD5270.cpp
adisuciu 0:18d434f6f122 3 * @brief Source file for AD5270 rheostat
adisuciu 0:18d434f6f122 4 * @author Analog Devices Inc.
adisuciu 0:18d434f6f122 5 *
adisuciu 0:18d434f6f122 6 * For support please go to:
adisuciu 0:18d434f6f122 7 * Github: https://github.com/analogdevicesinc/mbed-adi
adisuciu 0:18d434f6f122 8 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
adisuciu 0:18d434f6f122 9 * Product: http://www.analog.com/ad5270
adisuciu 0:18d434f6f122 10 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
adisuciu 0:18d434f6f122 11
adisuciu 0:18d434f6f122 12 ********************************************************************************
adisuciu 0:18d434f6f122 13 * Copyright 2016(c) Analog Devices, Inc.
adisuciu 0:18d434f6f122 14 *
adisuciu 0:18d434f6f122 15 * All rights reserved.
adisuciu 0:18d434f6f122 16 *
adisuciu 0:18d434f6f122 17 * Redistribution and use in source and binary forms, with or without
adisuciu 0:18d434f6f122 18 * modification, are permitted provided that the following conditions are met:
adisuciu 0:18d434f6f122 19 * - Redistributions of source code must retain the above copyright
adisuciu 0:18d434f6f122 20 * notice, this list of conditions and the following disclaimer.
adisuciu 0:18d434f6f122 21 * - Redistributions in binary form must reproduce the above copyright
adisuciu 0:18d434f6f122 22 * notice, this list of conditions and the following disclaimer in
adisuciu 0:18d434f6f122 23 * the documentation and/or other materials provided with the
adisuciu 0:18d434f6f122 24 * distribution.
adisuciu 0:18d434f6f122 25 * - Neither the name of Analog Devices, Inc. nor the names of its
adisuciu 0:18d434f6f122 26 * contributors may be used to endorse or promote products derived
adisuciu 0:18d434f6f122 27 * from this software without specific prior written permission.
adisuciu 0:18d434f6f122 28 * - The use of this software may or may not infringe the patent rights
adisuciu 0:18d434f6f122 29 * of one or more patent holders. This license does not release you
adisuciu 0:18d434f6f122 30 * from the requirement that you obtain separate licenses from these
adisuciu 0:18d434f6f122 31 * patent holders to use this software.
adisuciu 0:18d434f6f122 32 * - Use of the software either in source or binary form, must be run
adisuciu 0:18d434f6f122 33 * on or directly connected to an Analog Devices Inc. component.
adisuciu 0:18d434f6f122 34 *
adisuciu 0:18d434f6f122 35 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
adisuciu 0:18d434f6f122 36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
adisuciu 0:18d434f6f122 37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
adisuciu 0:18d434f6f122 38 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
adisuciu 0:18d434f6f122 39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
adisuciu 0:18d434f6f122 40 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
adisuciu 0:18d434f6f122 41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
adisuciu 0:18d434f6f122 42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
adisuciu 0:18d434f6f122 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
adisuciu 0:18d434f6f122 44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
adisuciu 0:18d434f6f122 45 *
adisuciu 0:18d434f6f122 46 ********************************************************************************/
adisuciu 0:18d434f6f122 47
adisuciu 0:18d434f6f122 48
adisuciu 0:18d434f6f122 49 #include "mbed.h"
adisuciu 0:18d434f6f122 50 #include "AD5270.h"
adisuciu 0:18d434f6f122 51
adisuciu 0:18d434f6f122 52 /**
adisuciu 0:18d434f6f122 53 * @brief AD5270 constructor, sets CS pin and SPI format
adisuciu 0:18d434f6f122 54 * @param CS - (optional)chip select of the AD5270
adisuciu 0:18d434f6f122 55 * @param max_resistance - (optional) nominal resistance of the AD5270 chip in ohms
adisuciu 0:18d434f6f122 56 * @param MOSI - (optional)pin of the SPI interface
adisuciu 0:18d434f6f122 57 * @param MISO - (optional)pin of the SPI interface
adisuciu 0:18d434f6f122 58 * @param SCK - (optional)pin of the SPI interface
adisuciu 0:18d434f6f122 59 */
adisuciu 0:18d434f6f122 60 AD5270::AD5270(PinName CS, float max_resistance, PinName MOSI, PinName MISO, PinName SCK):
adisuciu 0:18d434f6f122 61 ad5270(MOSI, MISO, SCK), cs(CS), _max_resistance(max_resistance)
adisuciu 0:18d434f6f122 62 {
adisuciu 0:18d434f6f122 63 cs = true;
adisuciu 0:18d434f6f122 64 ad5270.format(8, _SPI_MODE);
adisuciu 0:18d434f6f122 65 }
adisuciu 0:18d434f6f122 66
adisuciu 0:18d434f6f122 67 /**
adisuciu 0:18d434f6f122 68 * @brief Compute for the nearest RDAC value from given resistance
adisuciu 0:18d434f6f122 69 * @param resistance - resistor
adisuciu 0:18d434f6f122 70 * @return RDAC value - closest possible to given resistance
adisuciu 0:18d434f6f122 71 */
adisuciu 0:18d434f6f122 72 uint16_t AD5270::calc_RDAC(float resistance)
adisuciu 0:18d434f6f122 73 {
adisuciu 0:18d434f6f122 74 return static_cast<uint16_t>( (resistance / _max_resistance) * 1024.0 );
adisuciu 0:18d434f6f122 75 }
adisuciu 0:18d434f6f122 76
adisuciu 0:18d434f6f122 77 /**
adisuciu 0:18d434f6f122 78 * @brief sets a new value for the RDAC
adisuciu 0:18d434f6f122 79 * @param resistance new value for the resistance
adisuciu 0:18d434f6f122 80 * @return actual value of the resistance in the RDAC
adisuciu 0:18d434f6f122 81 */
adisuciu 0:18d434f6f122 82 float AD5270::write_RDAC(float resistance)
adisuciu 0:18d434f6f122 83 {
adisuciu 0:18d434f6f122 84 // Compute for the RDAC code nearest to the required feedback resistance
adisuciu 0:18d434f6f122 85 uint16_t RDAC_val = calc_RDAC(resistance);
adisuciu 0:18d434f6f122 86 float RDAC_Value = ((static_cast<float> (RDAC_val) * _max_resistance) / 1024.0); // inverse operation to get actual resistance in the RDAC
adisuciu 0:18d434f6f122 87 write_wiper_reg(RDAC_val);
adisuciu 0:18d434f6f122 88 return RDAC_Value;
adisuciu 0:18d434f6f122 89 }
adisuciu 0:18d434f6f122 90
adisuciu 0:18d434f6f122 91 /**
adisuciu 0:18d434f6f122 92 * Reads the RDAC register
adisuciu 0:18d434f6f122 93 * @return RDAC resistor value
adisuciu 0:18d434f6f122 94 */
adisuciu 0:18d434f6f122 95 float AD5270::read_RDAC()
adisuciu 0:18d434f6f122 96 {
adisuciu 0:18d434f6f122 97 uint16_t RDAC_val = read_wiper_reg();
adisuciu 0:18d434f6f122 98 return ((static_cast<float> (RDAC_val) * _max_resistance) / 1024.0);
adisuciu 0:18d434f6f122 99 }
adisuciu 0:18d434f6f122 100
adisuciu 0:18d434f6f122 101 /**
adisuciu 0:18d434f6f122 102 * @brief Puts the AD5270 SDO line in to Hi-Z mode
adisuciu 0:18d434f6f122 103 * @return none
adisuciu 0:18d434f6f122 104 */
adisuciu 0:18d434f6f122 105 void AD5270::set_SDO_HiZ(void)
adisuciu 0:18d434f6f122 106 {
adisuciu 0:18d434f6f122 107 write_reg(HI_Z_Cmd);
adisuciu 0:18d434f6f122 108 wait_us(2);
adisuciu 0:18d434f6f122 109 write_reg(NO_OP_cmd);
adisuciu 0:18d434f6f122 110 }
adisuciu 0:18d434f6f122 111
adisuciu 0:18d434f6f122 112 /**
adisuciu 0:18d434f6f122 113 * @brief Set AD5270 SPI frequency
adisuciu 0:18d434f6f122 114 * @param hz - SPI bus frequency in hz
adisuciu 0:18d434f6f122 115 * @return none
adisuciu 0:18d434f6f122 116 */
adisuciu 0:18d434f6f122 117 void AD5270::frequency(int hz)
adisuciu 0:18d434f6f122 118 {
adisuciu 0:18d434f6f122 119 ad5270.frequency(hz);
adisuciu 0:18d434f6f122 120 }
adisuciu 0:18d434f6f122 121
adisuciu 0:18d434f6f122 122 /**
adisuciu 0:18d434f6f122 123 * @brief Issues AD5270 a command
adisuciu 0:18d434f6f122 124 * @param command - command to be sent
adisuciu 0:18d434f6f122 125 * @param data - (optional)value for the requested command
adisuciu 0:18d434f6f122 126 * @return response form the AD5270
adisuciu 0:18d434f6f122 127 */
adisuciu 0:18d434f6f122 128 uint16_t AD5270::write_cmd(uint8_t command, uint16_t data)
adisuciu 0:18d434f6f122 129 {
adisuciu 0:18d434f6f122 130 /* build 16 bit data to be written - Command + Value */
adisuciu 0:18d434f6f122 131 uint16_t ui16Command = ((command & 0x3C) << 8) | (data & 0x3FF);
adisuciu 0:18d434f6f122 132 return write_reg(ui16Command);
adisuciu 0:18d434f6f122 133 }
adisuciu 0:18d434f6f122 134
adisuciu 0:18d434f6f122 135 /**
adisuciu 0:18d434f6f122 136 * Enables the 50TP memory programming
adisuciu 0:18d434f6f122 137 */
adisuciu 0:18d434f6f122 138 void AD5270::enable_50TP_programming()
adisuciu 0:18d434f6f122 139 {
adisuciu 0:18d434f6f122 140 uint8_t regVal = read_ctrl_reg();
adisuciu 0:18d434f6f122 141 write_cmd(WRITE_CTRL_REG, regVal | PROGRAM_50TP_ENABLE); // RDAC register write protect - allow update of wiper position through digital interface
adisuciu 0:18d434f6f122 142 }
adisuciu 0:18d434f6f122 143
adisuciu 0:18d434f6f122 144 /**
adisuciu 0:18d434f6f122 145 * Stores current RDAC content to the 50TP memory
adisuciu 0:18d434f6f122 146 */
adisuciu 0:18d434f6f122 147 void AD5270::store_50TP()
adisuciu 0:18d434f6f122 148 {
adisuciu 0:18d434f6f122 149 write_cmd(STORE_50TP);
adisuciu 0:18d434f6f122 150 wait_ms(_WRITE_OPERATION_50TP_TIMEOUT);
adisuciu 0:18d434f6f122 151 }
adisuciu 0:18d434f6f122 152
adisuciu 0:18d434f6f122 153 /**
adisuciu 0:18d434f6f122 154 * Disables the 50TP memory programming
adisuciu 0:18d434f6f122 155 */
adisuciu 0:18d434f6f122 156 void AD5270::disable_50TP_programming()
adisuciu 0:18d434f6f122 157 {
adisuciu 0:18d434f6f122 158 uint8_t regVal = read_ctrl_reg();
adisuciu 0:18d434f6f122 159 write_cmd(WRITE_CTRL_REG, regVal & (~PROGRAM_50TP_ENABLE));
adisuciu 0:18d434f6f122 160 }
adisuciu 0:18d434f6f122 161
adisuciu 0:18d434f6f122 162 /**
adisuciu 0:18d434f6f122 163 * @brief Writes 16bit data to the AD5270 SPI interface
adisuciu 0:18d434f6f122 164 * @param data to be written
adisuciu 0:18d434f6f122 165 * @return data returned by the AD5270
adisuciu 0:18d434f6f122 166 */
adisuciu 0:18d434f6f122 167 uint16_t AD5270::write_reg(uint16_t data)
adisuciu 0:18d434f6f122 168 {
adisuciu 0:18d434f6f122 169 uint16_t result;
adisuciu 0:18d434f6f122 170 uint8_t upper_byte = (data >> 8) & 0xFF;
adisuciu 0:18d434f6f122 171 uint8_t lower_byte = data & 0xFF;
adisuciu 0:18d434f6f122 172 ad5270.format(8, _SPI_MODE);
adisuciu 0:18d434f6f122 173 cs = false;
adisuciu 0:18d434f6f122 174 result = ((ad5270.write(upper_byte)) << 8);
adisuciu 0:18d434f6f122 175 result |= ad5270.write(lower_byte);
adisuciu 0:18d434f6f122 176 cs = true;
adisuciu 0:18d434f6f122 177 return result;
adisuciu 0:18d434f6f122 178 }
adisuciu 0:18d434f6f122 179
adisuciu 0:18d434f6f122 180 /**
adisuciu 0:18d434f6f122 181 * @brief Gets maximum resistance of the AD5270 digital rheostat
adisuciu 0:18d434f6f122 182 * @return maximum resistance in ohms
adisuciu 0:18d434f6f122 183 */
adisuciu 0:18d434f6f122 184 float AD5270::get_max_resistance()
adisuciu 0:18d434f6f122 185 {
adisuciu 0:18d434f6f122 186 return _max_resistance;
adisuciu 0:18d434f6f122 187 }
adisuciu 0:18d434f6f122 188
adisuciu 0:18d434f6f122 189 /**
adisuciu 0:18d434f6f122 190 * Writes the wiper register. This includes reading the control register,
adisuciu 0:18d434f6f122 191 * setting write protect off, writing the wiper, and reverting the settings
adisuciu 0:18d434f6f122 192 * to the control reg.
adisuciu 0:18d434f6f122 193 * @param data to be written
adisuciu 0:18d434f6f122 194 */
adisuciu 0:18d434f6f122 195 void AD5270::write_wiper_reg(uint16_t data)
adisuciu 0:18d434f6f122 196 {
adisuciu 0:18d434f6f122 197 uint8_t reg_val = read_ctrl_reg();
adisuciu 0:18d434f6f122 198 write_cmd(WRITE_CTRL_REG, reg_val | RDAC_WRITE_PROTECT); // RDAC register write protect - allow update of wiper position through digital interface
adisuciu 0:18d434f6f122 199 write_cmd(WRITE_RDAC, data); // write data to the RDAC register
adisuciu 0:18d434f6f122 200 write_cmd(WRITE_CTRL_REG, reg_val); // RDAC register write protect - allow update of wiper position through digital interface
adisuciu 0:18d434f6f122 201 }
adisuciu 0:18d434f6f122 202
adisuciu 0:18d434f6f122 203 /**
adisuciu 0:18d434f6f122 204 * Reads the wiper register value
adisuciu 0:18d434f6f122 205 * @return value of the wiper register
adisuciu 0:18d434f6f122 206 */
adisuciu 0:18d434f6f122 207 uint16_t AD5270::read_wiper_reg(void)
adisuciu 0:18d434f6f122 208 {
adisuciu 0:18d434f6f122 209 uint16_t RDAC_val;
adisuciu 0:18d434f6f122 210 write_cmd(READ_RDAC);
adisuciu 0:18d434f6f122 211 wait_us(_REG_OPERATION_TIMEOUT);
adisuciu 0:18d434f6f122 212 RDAC_val = write_cmd(NO_OP);
adisuciu 0:18d434f6f122 213 return RDAC_val;
adisuciu 0:18d434f6f122 214 }
adisuciu 0:18d434f6f122 215
adisuciu 0:18d434f6f122 216 /**
adisuciu 0:18d434f6f122 217 * Reads the last programmed value of the 50TP memory
adisuciu 0:18d434f6f122 218 * @return last programmed value
adisuciu 0:18d434f6f122 219 */
adisuciu 0:18d434f6f122 220 uint8_t AD5270::read_50TP_last_address(void)
adisuciu 0:18d434f6f122 221 {
adisuciu 0:18d434f6f122 222 uint8_t ret_val;
adisuciu 0:18d434f6f122 223 write_cmd(READ_50TP_ADDRESS);
adisuciu 0:18d434f6f122 224 wait_us(_MEMORY_OPERATION_TIMEOUT);
adisuciu 0:18d434f6f122 225 ret_val = write_cmd(NO_OP);
adisuciu 0:18d434f6f122 226 return ret_val;
adisuciu 0:18d434f6f122 227 }
adisuciu 0:18d434f6f122 228
adisuciu 0:18d434f6f122 229 /**
adisuciu 0:18d434f6f122 230 * Reads the content of a 50TP memory address
adisuciu 0:18d434f6f122 231 * @param address memory to be read
adisuciu 0:18d434f6f122 232 * @return value stored in the 50TP address
adisuciu 0:18d434f6f122 233 */
adisuciu 0:18d434f6f122 234 uint16_t AD5270::read_50TP_memory(uint8_t address)
adisuciu 0:18d434f6f122 235 {
adisuciu 0:18d434f6f122 236 uint16_t ret_val;
adisuciu 0:18d434f6f122 237 write_cmd(READ_50TP_CONTENTS, address);
adisuciu 0:18d434f6f122 238 wait_us(_MEMORY_OPERATION_TIMEOUT);
adisuciu 0:18d434f6f122 239 ret_val = write_cmd(NO_OP);
adisuciu 0:18d434f6f122 240 return ret_val;
adisuciu 0:18d434f6f122 241 }
adisuciu 0:18d434f6f122 242
adisuciu 0:18d434f6f122 243 /**
adisuciu 0:18d434f6f122 244 * Writes the control register
adisuciu 0:18d434f6f122 245 * @param data to be written
adisuciu 0:18d434f6f122 246 */
adisuciu 0:18d434f6f122 247 void AD5270::write_ctrl_reg(uint8_t data)
adisuciu 0:18d434f6f122 248 {
adisuciu 0:18d434f6f122 249 write_cmd(WRITE_CTRL_REG, data);
adisuciu 0:18d434f6f122 250 }
adisuciu 0:18d434f6f122 251
adisuciu 0:18d434f6f122 252 /**
adisuciu 0:18d434f6f122 253 * Reads the control register
adisuciu 0:18d434f6f122 254 * @return value of the control register
adisuciu 0:18d434f6f122 255 */
adisuciu 0:18d434f6f122 256 uint8_t AD5270::read_ctrl_reg(void)
adisuciu 0:18d434f6f122 257 {
adisuciu 0:18d434f6f122 258 uint8_t ret_val;
adisuciu 0:18d434f6f122 259 write_cmd(READ_CTRL_REG);
adisuciu 0:18d434f6f122 260 wait_us(_REG_OPERATION_TIMEOUT);
adisuciu 0:18d434f6f122 261 ret_val = write_cmd(NO_OP);
adisuciu 0:18d434f6f122 262 return ret_val;
adisuciu 0:18d434f6f122 263 }
adisuciu 0:18d434f6f122 264
adisuciu 0:18d434f6f122 265 /**
adisuciu 0:18d434f6f122 266 * Resets the wiper register value to the data last written in the 50TP
adisuciu 0:18d434f6f122 267 */
adisuciu 0:18d434f6f122 268 void AD5270::reset_RDAC(void)
adisuciu 0:18d434f6f122 269 {
adisuciu 0:18d434f6f122 270 write_cmd(SW_RST);
adisuciu 0:18d434f6f122 271 }
adisuciu 0:18d434f6f122 272
adisuciu 0:18d434f6f122 273 /**
adisuciu 0:18d434f6f122 274 * Changes the device mode, enabled or shutdown
adisuciu 0:18d434f6f122 275 * @param mode - new mode of the device
adisuciu 0:18d434f6f122 276 */
adisuciu 0:18d434f6f122 277 void AD5270::change_mode(AD5270Modes_t mode)
adisuciu 0:18d434f6f122 278 {
adisuciu 0:18d434f6f122 279 write_cmd(SW_SHUTDOWN, static_cast<uint8_t>(mode));
adisuciu 0:18d434f6f122 280 }
adisuciu 0:18d434f6f122 281
adisuciu 0:18d434f6f122 282
adisuciu 0:18d434f6f122 283