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 AD7790.cpp
Suciu 1:c1f0670bb370 3 * @brief Source file for AD7790 ADC
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 <stdint.h>
Suciu 1:c1f0670bb370 10 #include "mbed.h"
Suciu 1:c1f0670bb370 11 #include "ad7790.h"
Suciu 1:c1f0670bb370 12
Suciu 1:c1f0670bb370 13
Suciu 1:c1f0670bb370 14 /**
Suciu 1:c1f0670bb370 15 * @brief AD7790 constructor, sets CS pin and SPI format
Suciu 1:c1f0670bb370 16 * @param CS - (optional)chip select of the AD7790
Suciu 1:c1f0670bb370 17 * @param MOSI - (optional)pin of the SPI interface
Suciu 1:c1f0670bb370 18 * @param MISO - (optional)pin of the SPI interface
Suciu 1:c1f0670bb370 19 * @param SCK - (optional)pin of the SPI interface
Suciu 1:c1f0670bb370 20 */
Suciu 1:c1f0670bb370 21 AD7790::AD7790(PinName CS,
Suciu 1:c1f0670bb370 22 PinName MOSI,
Suciu 1:c1f0670bb370 23 PinName MISO,
Suciu 1:c1f0670bb370 24 PinName SCK) :
Suciu 1:c1f0670bb370 25 ad7790(MOSI, MISO, SCK),cs(CS)
Suciu 1:c1f0670bb370 26 {
Suciu 1:c1f0670bb370 27 ad7790.format(8,3);
Suciu 1:c1f0670bb370 28 cs = true; // cs is active low
Suciu 1:c1f0670bb370 29 }
Suciu 1:c1f0670bb370 30
Suciu 1:c1f0670bb370 31 /**
Suciu 1:c1f0670bb370 32 * @brief Set AD7790 SPI frequency
Suciu 1:c1f0670bb370 33 * @param hz - SPI bus frequency in hz
Suciu 1:c1f0670bb370 34 * @return none
Suciu 1:c1f0670bb370 35 */
Suciu 1:c1f0670bb370 36 void AD7790::frequency(int hz)
Suciu 1:c1f0670bb370 37 {
Suciu 1:c1f0670bb370 38 ad7790.frequency(hz);
Suciu 1:c1f0670bb370 39 }
Suciu 1:c1f0670bb370 40
Suciu 1:c1f0670bb370 41 /**
Suciu 1:c1f0670bb370 42 * @brief Resets the AD7790
Suciu 1:c1f0670bb370 43 * @return none
Suciu 1:c1f0670bb370 44 */
Suciu 1:c1f0670bb370 45 void AD7790::reset()
Suciu 1:c1f0670bb370 46 {
Suciu 1:c1f0670bb370 47 cs = false;
Suciu 1:c1f0670bb370 48 ad7790.write(_RESET);
Suciu 1:c1f0670bb370 49 ad7790.write(_RESET);
Suciu 1:c1f0670bb370 50 ad7790.write(_RESET);
Suciu 1:c1f0670bb370 51 ad7790.write(_RESET);
Suciu 1:c1f0670bb370 52 cs = true;
Suciu 1:c1f0670bb370 53 }
Suciu 1:c1f0670bb370 54
Suciu 1:c1f0670bb370 55 /**
Suciu 1:c1f0670bb370 56 * @brief Reads the data register of the AD7790
Suciu 1:c1f0670bb370 57 * @return value of the register
Suciu 1:c1f0670bb370 58 */
Suciu 1:c1f0670bb370 59 uint16_t AD7790::read_data(void)
Suciu 1:c1f0670bb370 60 {
Suciu 1:c1f0670bb370 61 uint16_t dataResult = 0;
Suciu 1:c1f0670bb370 62
Suciu 1:c1f0670bb370 63 cs = false;
Suciu 1:c1f0670bb370 64 ad7790.write(_DATA_READ);
Suciu 1:c1f0670bb370 65 dataResult = ((ad7790.write(_DUMMY_BYTE)) << 8);
Suciu 1:c1f0670bb370 66 dataResult |= (ad7790.write(_DUMMY_BYTE));
Suciu 1:c1f0670bb370 67 cs = true;
Suciu 1:c1f0670bb370 68
Suciu 1:c1f0670bb370 69 return dataResult;
Suciu 1:c1f0670bb370 70 }
Suciu 1:c1f0670bb370 71
Suciu 1:c1f0670bb370 72 /**
Suciu 1:c1f0670bb370 73 * @brief Reads a register of the AD7790
Suciu 1:c1f0670bb370 74 * @param regAddress - address of the register
Suciu 1:c1f0670bb370 75 * @return value of the register
Suciu 1:c1f0670bb370 76 */
Suciu 1:c1f0670bb370 77 uint16_t AD7790::read_reg(AD7790Registers_t regAddress)
Suciu 1:c1f0670bb370 78 {
Suciu 1:c1f0670bb370 79 uint16_t data = regAddress << 12;
Suciu 1:c1f0670bb370 80 data |= _DUMMY_BYTE;
Suciu 1:c1f0670bb370 81 data |= _READ_FLAG;
Suciu 1:c1f0670bb370 82 return write_spi(data);
Suciu 1:c1f0670bb370 83 }
Suciu 1:c1f0670bb370 84
Suciu 1:c1f0670bb370 85 /**
Suciu 1:c1f0670bb370 86 * @brief Writes a register of the AD7790
Suciu 1:c1f0670bb370 87 * @param regAddress - address of the register
Suciu 1:c1f0670bb370 88 * @param regValue - value to be written
Suciu 1:c1f0670bb370 89 * @return none
Suciu 1:c1f0670bb370 90 *
Suciu 1:c1f0670bb370 91 */
Suciu 1:c1f0670bb370 92 void AD7790::write_reg(AD7790Registers_t regAddress,uint8_t regValue)
Suciu 1:c1f0670bb370 93 {
Suciu 1:c1f0670bb370 94 uint16_t data = regAddress << 12;
Suciu 1:c1f0670bb370 95 data |= regValue;
Suciu 1:c1f0670bb370 96 write_spi(data);
Suciu 1:c1f0670bb370 97 }
Suciu 1:c1f0670bb370 98
Suciu 1:c1f0670bb370 99 /**
Suciu 1:c1f0670bb370 100 * @brief Writes 16bit data to the AD7790 SPI interface
Suciu 1:c1f0670bb370 101 * @param data to be written
Suciu 1:c1f0670bb370 102 * @return data returned by the AD7790
Suciu 1:c1f0670bb370 103 */
Suciu 1:c1f0670bb370 104 uint16_t AD7790::write_spi(uint16_t data)
Suciu 1:c1f0670bb370 105 {
Suciu 1:c1f0670bb370 106 uint16_t result;
Suciu 1:c1f0670bb370 107 uint8_t upper_byte = (data >> 8) & 0xFF;
Suciu 1:c1f0670bb370 108 uint8_t lower_byte = data & 0xFF;
Suciu 1:c1f0670bb370 109 cs = false;
Suciu 1:c1f0670bb370 110 result = (ad7790.write(upper_byte) << 8);
Suciu 1:c1f0670bb370 111 result |= ad7790.write(lower_byte);
Suciu 1:c1f0670bb370 112 cs = true;
Suciu 1:c1f0670bb370 113 return result;
Suciu 1:c1f0670bb370 114 }
Suciu 1:c1f0670bb370 115
Suciu 1:c1f0670bb370 116
Suciu 1:c1f0670bb370 117