A collection of Analog Devices drivers for the mbed platform

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

Committer:
Adrian Suciu
Date:
Tue Apr 19 17:55:14 2016 +0300
Revision:
4:5c1b28aff7e1
Parent:
3:1a8c14043a4e
Driver and application redesign

Who changed what in which revision?

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