Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
AD5270.cpp
00001 /** 00002 * @file AD5270.cpp 00003 * @brief Source file for AD5270 rheostat 00004 * @author Analog Devices Inc. 00005 * 00006 * For support please go to: 00007 * Github: https://github.com/analogdevicesinc/mbed-adi 00008 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers 00009 * Product: http://www.analog.com/ad5270 00010 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all 00011 00012 ******************************************************************************** 00013 * Copyright 2016(c) Analog Devices, Inc. 00014 * 00015 * All rights reserved. 00016 * 00017 * Redistribution and use in source and binary forms, with or without 00018 * modification, are permitted provided that the following conditions are met: 00019 * - Redistributions of source code must retain the above copyright 00020 * notice, this list of conditions and the following disclaimer. 00021 * - Redistributions in binary form must reproduce the above copyright 00022 * notice, this list of conditions and the following disclaimer in 00023 * the documentation and/or other materials provided with the 00024 * distribution. 00025 * - Neither the name of Analog Devices, Inc. nor the names of its 00026 * contributors may be used to endorse or promote products derived 00027 * from this software without specific prior written permission. 00028 * - The use of this software may or may not infringe the patent rights 00029 * of one or more patent holders. This license does not release you 00030 * from the requirement that you obtain separate licenses from these 00031 * patent holders to use this software. 00032 * - Use of the software either in source or binary form, must be run 00033 * on or directly connected to an Analog Devices Inc. component. 00034 * 00035 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 00036 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 00037 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00038 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 00039 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00040 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 00041 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00042 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00043 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00044 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00045 * 00046 ********************************************************************************/ 00047 00048 00049 #include "mbed.h" 00050 #include "AD5270.h" 00051 00052 /** 00053 * @brief AD5270 constructor, sets CS pin and SPI format 00054 * @param CS - (optional)chip select of the AD5270 00055 * @param max_resistance - (optional) nominal resistance of the AD5270 chip in ohms 00056 * @param MOSI - (optional)pin of the SPI interface 00057 * @param MISO - (optional)pin of the SPI interface 00058 * @param SCK - (optional)pin of the SPI interface 00059 */ 00060 AD5270::AD5270(PinName CS, float max_resistance, PinName MOSI, PinName MISO, PinName SCK): 00061 ad5270(MOSI, MISO, SCK), cs(CS), _max_resistance(max_resistance) 00062 { 00063 cs = true; 00064 ad5270.format(8, _SPI_MODE); 00065 } 00066 00067 /** 00068 * @brief Compute for the nearest RDAC value from given resistance 00069 * @param resistance - resistor 00070 * @return RDAC value - closest possible to given resistance 00071 */ 00072 uint16_t AD5270::calc_RDAC(float resistance) 00073 { 00074 return static_cast<uint16_t>( (resistance / _max_resistance) * 1024.0 ); 00075 } 00076 00077 /** 00078 * @brief sets a new value for the RDAC 00079 * @param resistance new value for the resistance 00080 * @return actual value of the resistance in the RDAC 00081 */ 00082 float AD5270::write_RDAC(float resistance) 00083 { 00084 // Compute for the RDAC code nearest to the required feedback resistance 00085 uint16_t RDAC_val = calc_RDAC(resistance); 00086 float RDAC_Value = ((static_cast<float> (RDAC_val) * _max_resistance) / 1024.0); // inverse operation to get actual resistance in the RDAC 00087 write_wiper_reg(RDAC_val); 00088 return RDAC_Value; 00089 } 00090 00091 /** 00092 * Reads the RDAC register 00093 * @return RDAC resistor value 00094 */ 00095 float AD5270::read_RDAC() 00096 { 00097 uint16_t RDAC_val = read_wiper_reg(); 00098 return ((static_cast<float> (RDAC_val) * _max_resistance) / 1024.0); 00099 } 00100 00101 /** 00102 * @brief Puts the AD5270 SDO line in to Hi-Z mode 00103 * @return none 00104 */ 00105 void AD5270::set_SDO_HiZ(void) 00106 { 00107 write_reg(HI_Z_Cmd); 00108 wait_us(2); 00109 write_reg(NO_OP_cmd); 00110 } 00111 00112 /** 00113 * @brief Set AD5270 SPI frequency 00114 * @param hz - SPI bus frequency in hz 00115 * @return none 00116 */ 00117 void AD5270::frequency(int hz) 00118 { 00119 ad5270.frequency(hz); 00120 } 00121 00122 /** 00123 * @brief Issues AD5270 a command 00124 * @param command - command to be sent 00125 * @param data - (optional)value for the requested command 00126 * @return response form the AD5270 00127 */ 00128 uint16_t AD5270::write_cmd(uint8_t command, uint16_t data, bool toggle_cs) 00129 { 00130 /* build 16 bit data to be written - Command + Value */ 00131 uint16_t ui16Command = ((command & 0x3C) << 8) | (data & 0x3FF); 00132 return write_reg(ui16Command, toggle_cs); 00133 } 00134 00135 /** 00136 * Enables the 50TP memory programming 00137 */ 00138 void AD5270::enable_50TP_programming() 00139 { 00140 uint8_t regVal = read_ctrl_reg(); 00141 write_cmd(WRITE_CTRL_REG, regVal | PROGRAM_50TP_ENABLE); // RDAC register write protect - allow update of wiper position through digital interface 00142 } 00143 00144 /** 00145 * Stores current RDAC content to the 50TP memory 00146 */ 00147 void AD5270::store_50TP() 00148 { 00149 write_cmd(STORE_50TP); 00150 wait_ms(_WRITE_OPERATION_50TP_TIMEOUT); 00151 } 00152 00153 /** 00154 * Disables the 50TP memory programming 00155 */ 00156 void AD5270::disable_50TP_programming() 00157 { 00158 uint8_t regVal = read_ctrl_reg(); 00159 write_cmd(WRITE_CTRL_REG, regVal & (~PROGRAM_50TP_ENABLE)); 00160 } 00161 00162 /** 00163 * @brief Writes 16bit data to the AD5270 SPI interface 00164 * @param data to be written 00165 * @return data returned by the AD5270 00166 */ 00167 uint16_t AD5270::write_reg(uint16_t data, bool toggle_cs) 00168 { 00169 uint16_t result; 00170 uint8_t upper_byte = (data >> 8) & 0xFF; 00171 uint8_t lower_byte = data & 0xFF; 00172 ad5270.format(8, _SPI_MODE); 00173 cs = false & toggle_cs; 00174 result = ((ad5270.write(upper_byte)) << 8); 00175 result |= ad5270.write(lower_byte); 00176 cs = true & toggle_cs; 00177 return result; 00178 } 00179 00180 /** 00181 * @brief Gets maximum resistance of the AD5270 digital rheostat 00182 * @return maximum resistance in ohms 00183 */ 00184 float AD5270::get_max_resistance() 00185 { 00186 return _max_resistance; 00187 } 00188 00189 /** 00190 * Writes the wiper register. This includes reading the control register, 00191 * setting write protect off, writing the wiper, and reverting the settings 00192 * to the control reg. 00193 * @param data to be written 00194 */ 00195 void AD5270::write_wiper_reg(uint16_t data) 00196 { 00197 uint8_t reg_val = read_ctrl_reg(); 00198 write_cmd(WRITE_CTRL_REG, reg_val | RDAC_WRITE_PROTECT); // RDAC register write protect - allow update of wiper position through digital interface 00199 write_cmd(WRITE_RDAC, data); // write data to the RDAC register 00200 write_cmd(WRITE_CTRL_REG, reg_val); // RDAC register write protect - allow update of wiper position through digital interface 00201 } 00202 00203 /** 00204 * Reads the wiper register value 00205 * @return value of the wiper register 00206 */ 00207 uint16_t AD5270::read_wiper_reg(void) 00208 { 00209 uint16_t RDAC_val; 00210 write_cmd(READ_RDAC); 00211 wait_us(_REG_OPERATION_TIMEOUT); 00212 RDAC_val = write_cmd(NO_OP); 00213 return RDAC_val; 00214 } 00215 00216 /** 00217 * Reads the last programmed value of the 50TP memory 00218 * @return last programmed value 00219 */ 00220 uint8_t AD5270::read_50TP_last_address(void) 00221 { 00222 uint8_t ret_val; 00223 write_cmd(READ_50TP_ADDRESS); 00224 wait_us(_MEMORY_OPERATION_TIMEOUT); 00225 ret_val = write_cmd(NO_OP); 00226 return ret_val; 00227 } 00228 00229 /** 00230 * Reads the content of a 50TP memory address 00231 * @param address memory to be read 00232 * @return value stored in the 50TP address 00233 */ 00234 uint16_t AD5270::read_50TP_memory(uint8_t address) 00235 { 00236 uint16_t ret_val; 00237 write_cmd(READ_50TP_CONTENTS, address); 00238 wait_us(_MEMORY_OPERATION_TIMEOUT); 00239 ret_val = write_cmd(NO_OP); 00240 return ret_val; 00241 } 00242 00243 /** 00244 * Writes the control register 00245 * @param data to be written 00246 */ 00247 void AD5270::write_ctrl_reg(uint8_t data) 00248 { 00249 write_cmd(WRITE_CTRL_REG, data); 00250 } 00251 00252 /** 00253 * Reads the control register 00254 * @return value of the control register 00255 */ 00256 uint8_t AD5270::read_ctrl_reg(void) 00257 { 00258 uint8_t ret_val; 00259 write_cmd(READ_CTRL_REG); 00260 wait_us(_REG_OPERATION_TIMEOUT); 00261 ret_val = write_cmd(NO_OP); 00262 return ret_val; 00263 } 00264 00265 /** 00266 * Resets the wiper register value to the data last written in the 50TP 00267 */ 00268 void AD5270::reset_RDAC(void) 00269 { 00270 write_cmd(SW_RST); 00271 } 00272 00273 /** 00274 * Changes the device mode, enabled or shutdown 00275 * @param mode - new mode of the device 00276 */ 00277 void AD5270::change_mode(AD5270Modes_t mode) 00278 { 00279 write_cmd(SW_SHUTDOWN, static_cast<uint8_t>(mode)); 00280 } 00281 00282 void AD5270::daisy_chain(uint8_t *buffer, uint8_t size) 00283 { 00284 cs = 0; 00285 for(int i = 0; i < size; i++) 00286 buffer[i] = ad5270.write(buffer[i]); 00287 cs = 1; 00288 } 00289
Generated on Tue Jul 12 2022 17:59:51 by
1.7.2
CN0357 - Toxic gas measurement
CN0216 - Weight Scale