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:
Wed May 18 16:57:57 2016 +0300
Revision:
24:dae7123d432a
Child:
28:e24b911ddd7c
Improved compatibility with Linux systems

Who changed what in which revision?

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