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:
Suciu
Date:
Wed Mar 30 17:35:04 2016 +0300
Revision:
1:c1f0670bb370
Child:
3:1a8c14043a4e
Added CN0357-example project and related drivers

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 1:c1f0670bb370 4 * @version V0.1
Suciu 1:c1f0670bb370 5 * @author ADI
Suciu 1:c1f0670bb370 6 * @date March 2015
Suciu 1:c1f0670bb370 7 **/
Suciu 1:c1f0670bb370 8
Suciu 1:c1f0670bb370 9 #include "mbed.h"
Suciu 1:c1f0670bb370 10 #include "AD5270.h"
Suciu 1:c1f0670bb370 11
Suciu 1:c1f0670bb370 12 /**
Suciu 1:c1f0670bb370 13 * @brief AD5270 constructor, sets CS pin and SPI format
Suciu 1:c1f0670bb370 14 * @param CS - (optional)chip select of the AD5270
Suciu 1:c1f0670bb370 15 * @param max_resistance - (optional) nominal resistance of the AD5270 chip in ohms
Suciu 1:c1f0670bb370 16 * @param MOSI - (optional)pin of the SPI interface
Suciu 1:c1f0670bb370 17 * @param MISO - (optional)pin of the SPI interface
Suciu 1:c1f0670bb370 18 * @param SCK - (optional)pin of the SPI interface
Suciu 1:c1f0670bb370 19 */
Suciu 1:c1f0670bb370 20 AD5270::AD5270(PinName CS, float max_resistance, PinName MOSI,PinName MISO,PinName SCK):
Suciu 1:c1f0670bb370 21 ad5270(MOSI,MISO,SCK),cs(CS),_max_resistance(max_resistance)
Suciu 1:c1f0670bb370 22 {
Suciu 1:c1f0670bb370 23 ad5270.format(8,3);
Suciu 1:c1f0670bb370 24 cs=true;
Suciu 1:c1f0670bb370 25 }
Suciu 1:c1f0670bb370 26
Suciu 1:c1f0670bb370 27 /**
Suciu 1:c1f0670bb370 28 * @brief Compute for the nearest RDAC value from given resistance
Suciu 1:c1f0670bb370 29 * @param resistance - resistor
Suciu 1:c1f0670bb370 30 * @return RDAC value - closest possible to given resistance
Suciu 1:c1f0670bb370 31 */
Suciu 1:c1f0670bb370 32 uint16_t AD5270::calc_RDAC(float resistance)
Suciu 1:c1f0670bb370 33 {
Suciu 1:c1f0670bb370 34 return static_cast<uint16_t>( (resistance / _max_resistance) * 1024.0 );
Suciu 1:c1f0670bb370 35 }
Suciu 1:c1f0670bb370 36
Suciu 1:c1f0670bb370 37 /**
Suciu 1:c1f0670bb370 38 * @brief Puts the AD5270 SDO line in to Hi-Z mode
Suciu 1:c1f0670bb370 39 * @return none
Suciu 1:c1f0670bb370 40 */
Suciu 1:c1f0670bb370 41 void AD5270::set_SDO_HiZ(void)
Suciu 1:c1f0670bb370 42 {
Suciu 1:c1f0670bb370 43 write_reg(HI_Z_Cmd);
Suciu 1:c1f0670bb370 44 write_reg(NO_OP_cmd);
Suciu 1:c1f0670bb370 45 }
Suciu 1:c1f0670bb370 46
Suciu 1:c1f0670bb370 47 /**
Suciu 1:c1f0670bb370 48 * @brief Set AD5270 SPI frequency
Suciu 1:c1f0670bb370 49 * @param hz - SPI bus frequency in hz
Suciu 1:c1f0670bb370 50 * @return none
Suciu 1:c1f0670bb370 51 */
Suciu 1:c1f0670bb370 52 void AD5270::frequency(int hz)
Suciu 1:c1f0670bb370 53 {
Suciu 1:c1f0670bb370 54 ad5270.frequency(hz);
Suciu 1:c1f0670bb370 55 }
Suciu 1:c1f0670bb370 56
Suciu 1:c1f0670bb370 57 /**
Suciu 1:c1f0670bb370 58 * @brief Issues AD5270 a command
Suciu 1:c1f0670bb370 59 * @param command - command to be sent
Suciu 1:c1f0670bb370 60 * @param data - (optional)value for the requested command
Suciu 1:c1f0670bb370 61 * @return response form the AD5270
Suciu 1:c1f0670bb370 62 */
Suciu 1:c1f0670bb370 63 uint16_t AD5270::write_cmd(uint8_t command, uint16_t data)
Suciu 1:c1f0670bb370 64 {
Suciu 1:c1f0670bb370 65 /* build 16 bit data to be written - Command + Value */
Suciu 1:c1f0670bb370 66 uint16_t ui16Command = ((command & 0x3C) << 8) | (data & 0x3FF);
Suciu 1:c1f0670bb370 67 return write_reg(ui16Command);
Suciu 1:c1f0670bb370 68 }
Suciu 1:c1f0670bb370 69
Suciu 1:c1f0670bb370 70 /**
Suciu 1:c1f0670bb370 71 * @brief Writes 16bit data to the AD5270 SPI interface
Suciu 1:c1f0670bb370 72 * @param data to be written
Suciu 1:c1f0670bb370 73 * @return data returned by the AD5270
Suciu 1:c1f0670bb370 74 */
Suciu 1:c1f0670bb370 75 uint16_t AD5270::write_reg(uint16_t data)
Suciu 1:c1f0670bb370 76 {
Suciu 1:c1f0670bb370 77 uint16_t result;
Suciu 1:c1f0670bb370 78 uint8_t upper_byte = (data >> 8) & 0xFF;
Suciu 1:c1f0670bb370 79 uint8_t lower_byte = data & 0xFF;
Suciu 1:c1f0670bb370 80 cs = false;
Suciu 1:c1f0670bb370 81 result = ((ad5270.write(upper_byte)) << 8);
Suciu 1:c1f0670bb370 82 result |= ad5270.write(lower_byte);
Suciu 1:c1f0670bb370 83 cs = true;
Suciu 1:c1f0670bb370 84 return result;
Suciu 1:c1f0670bb370 85 }
Suciu 1:c1f0670bb370 86
Suciu 1:c1f0670bb370 87 /**
Suciu 1:c1f0670bb370 88 * @brief Gets maximum resistance of the AD5270 digital rheostat
Suciu 1:c1f0670bb370 89 * @return maximum resistance in ohms
Suciu 1:c1f0670bb370 90 */
Suciu 1:c1f0670bb370 91 float AD5270::get_max_resistance()
Suciu 1:c1f0670bb370 92 {
Suciu 1:c1f0670bb370 93 return _max_resistance;
Suciu 1:c1f0670bb370 94 }