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

Files at this revision

API Documentation at this revision

Comitter:
Adrian Suciu
Date:
Fri Apr 29 17:34:07 2016 +0300
Parent:
16:b8a49ec95bad
Child:
18:7d35420ff4aa
Child:
19:fb92949e59c9
Commit message:
Fixed code style and added doxygen tags

Changed in this revision

examples/cn0216_example/main.cpp Show annotated file Show diff for this revision Revisions of this file
examples/drvdiag/driver_wrapper/cn0216_diag.cpp Show annotated file Show diff for this revision Revisions of this file
examples/drvdiag/main.cpp Show annotated file Show diff for this revision Revisions of this file
libraries/ad7790/AD7790.cpp Show annotated file Show diff for this revision Revisions of this file
libraries/ad7791/AD7791.cpp Show annotated file Show diff for this revision Revisions of this file
libraries/ad7791/AD7791.h Show annotated file Show diff for this revision Revisions of this file
libraries/cn0216/CN0216.cpp Show annotated file Show diff for this revision Revisions of this file
libraries/cn0216/CN0216.h Show annotated file Show diff for this revision Revisions of this file
--- a/examples/cn0216_example/main.cpp	Fri Apr 29 17:16:55 2016 +0300
+++ b/examples/cn0216_example/main.cpp	Fri Apr 29 17:34:07 2016 +0300
@@ -53,8 +53,8 @@
 
 void flush_serial_buffer(void)
 {
-	while (pc.readable()) pc.getc();
-	return;
+    while (pc.readable()) pc.getc();
+    return;
 }
 
 void display_data(uint32_t data, float weight)
@@ -75,7 +75,7 @@
 int main()
 {
     /* Main variables */
-    CN0216 cn0216;    
+    CN0216 cn0216;
 #ifdef SINGLE_CONVERSION
     cn0216.init(CAL_WEIGHT);
 #elif defined CONTINOUS_CONVERSION
@@ -84,7 +84,7 @@
 #error define SINGLE_CONVERSION or CONTINOUS_CONVERSION, but not both
 #endif
     /* Calibration sequence */
-    
+
     pc.printf("\r\n Calibrating zero scale. Remove all weights from scale. Press any key to begin ..");
     while(!pc.readable());
     flush_serial_buffer();
@@ -100,14 +100,14 @@
     pc.printf("done ! ");
 
     pc.printf("\r\n Calibration successful ");
-    cn0216.calibrate(CN0216::COMPUTE_GRAM_PER_BIT);
+    cn0216.calibrate(CN0216::COMPUTE_UNITS_PER_BIT);
 
     /* Infinite loop */
     while (1) {
         wait_ms(1000);
         {
             uint32_t data = cn0216.read_u32();
-            float weight    = cn0216.compute_weight(data); //  Convert ADC data to voltage            
+            float weight    = cn0216.compute_weight(data); //  Convert ADC data to voltage
             display_data(data, weight); //  Display data thru UART
         }
     }
--- a/examples/drvdiag/driver_wrapper/cn0216_diag.cpp	Fri Apr 29 17:16:55 2016 +0300
+++ b/examples/drvdiag/driver_wrapper/cn0216_diag.cpp	Fri Apr 29 17:34:07 2016 +0300
@@ -16,16 +16,16 @@
 void CN0216_Diag::init(void)
 {
     uint16_t weight = strtol(cmdbuffer[1].c_str(), NULL, 10);
-    pc.printf("CN0216 initialized with %d calibration weight",weight);
+    pc.printf("CN0216 initialized with %d calibration weight", weight);
     dut.init(weight);
 }
 
 void CN0216_Diag::calibrate(void)
 {
     uint8_t step = strtol(cmdbuffer[1].c_str(), NULL, 16);
-    pc.printf("Calibrating step %d ..",step);
+    pc.printf("Calibrating step %d ..", step);
     dut.calibrate(static_cast<CN0216::CalibrationStep_t>(step));
-    pc.printf(".. DONE",step);
+    pc.printf(".. DONE", step);
 }
 
 void CN0216_Diag::read_weight(void)
--- a/examples/drvdiag/main.cpp	Fri Apr 29 17:16:55 2016 +0300
+++ b/examples/drvdiag/main.cpp	Fri Apr 29 17:34:07 2016 +0300
@@ -203,7 +203,7 @@
 int main()
 
 {
-	ad7791.frequency(100000);
+    ad7791.frequency(100000);
     pc.printf("\r\n#### DrvDiag ####\r\n");
 
     while(1) {
--- a/libraries/ad7790/AD7790.cpp	Fri Apr 29 17:16:55 2016 +0300
+++ b/libraries/ad7790/AD7790.cpp	Fri Apr 29 17:34:07 2016 +0300
@@ -61,7 +61,7 @@
                PinName MOSI,
                PinName MISO,
                PinName SCK) :
-			   miso(MISO), ad7790(MOSI, MISO, SCK), cs(CS), _vref(reference_voltage), _PGA_gain(1)
+    miso(MISO), ad7790(MOSI, MISO, SCK), cs(CS), _vref(reference_voltage), _PGA_gain(1)
 {
     cs = true; // cs is active low
     ad7790.format(8, _SPI_MODE);
--- a/libraries/ad7791/AD7791.cpp	Fri Apr 29 17:16:55 2016 +0300
+++ b/libraries/ad7791/AD7791.cpp	Fri Apr 29 17:34:07 2016 +0300
@@ -61,7 +61,7 @@
                PinName MOSI,
                PinName MISO,
                PinName SCK) :
-			   miso(MISO), ad7791(MOSI, MISO, SCK), cs(CS),  _vref(reference_voltage)
+    miso(MISO), ad7791(MOSI, MISO, SCK), cs(CS),  _vref(reference_voltage)
 {
     cs = true; // cs is active low
     ad7791.format(8, _SPI_MODE);
@@ -111,8 +111,8 @@
     } else {
         _continous_conversion = false;
     }
-/*  uint8_t range = (reg_val & 0x30);
-    _PGA_gain = 1 << (range >> 4);*/
+    /*  uint8_t range = (reg_val & 0x30);
+        _PGA_gain = 1 << (range >> 4);*/
 
 }
 
@@ -251,8 +251,8 @@
 
 uint16_t AD7791::read_u16(void)
 {
-  uint32_t data = read_u32();
-  return static_cast<uint16_t>((data & 0xffff00) >> 8);
+    uint32_t data = read_u32();
+    return static_cast<uint16_t>((data & 0xffff00) >> 8);
 }
 
 /**
--- a/libraries/ad7791/AD7791.h	Fri Apr 29 17:16:55 2016 +0300
+++ b/libraries/ad7791/AD7791.h	Fri Apr 29 17:34:07 2016 +0300
@@ -104,13 +104,13 @@
         FS0 = 0x01, ///< Update rate bit 0
     } FilterRegisterBits_t;
 
-/*  typedef enum    {
-        RANGE_VREF = 0,
-        RANGE_VREF_DIV_2,
-        RANGE_VREF_DIV_4,
-        RANGE_VREF_DIV_8,
-    } AnalogInputRange_t;
-*/
+    /*  typedef enum    {
+            RANGE_VREF = 0,
+            RANGE_VREF_DIV_2,
+            RANGE_VREF_DIV_4,
+            RANGE_VREF_DIV_8,
+        } AnalogInputRange_t;
+    */
     /** SPI configuration & constructor */
     AD7791( float reference_voltage, PinName CS = SPI_CS, PinName MOSI = SPI_MOSI, PinName MISO = SPI_MISO, PinName SCK = SPI_SCK);
     void frequency(int hz);
@@ -132,7 +132,7 @@
 
     /** Reference voltage methods */
     void  set_reference_voltage(float ref);
-  	float get_reference_voltage(void);
+    float get_reference_voltage(void);
 
     /** Voltage read methods */
     float read_voltage(void);
--- a/libraries/cn0216/CN0216.cpp	Fri Apr 29 17:16:55 2016 +0300
+++ b/libraries/cn0216/CN0216.cpp	Fri Apr 29 17:34:07 2016 +0300
@@ -50,16 +50,29 @@
 #include "CN0216.h"
 extern Serial pc;
 
-  CN0216::CN0216(PinName CSAD7791, PinName MOSI, PinName MISO, PinName SCK) : ad7791(1.2, CSAD7791, MOSI, MISO, SCK)
-  {
-	_cal_weight = 0;
+/**
+ * CN0216 constructor
+ * @param CSAD7791 - Chipselect of the AD7791
+ * @param MOSI - MOSI line of the SPI bus
+ * @param MISO - MISO line of the SPI bus
+ * @param SCK - SCK line of the SPI bus
+ */
+CN0216::CN0216(PinName CSAD7791, PinName MOSI, PinName MISO, PinName SCK) : ad7791(1.2, CSAD7791, MOSI, MISO, SCK)
+{
+    _cal_weight = 0;
     _zero_scale_value = 0;
     _full_scale_value = 0;
-    _grams_per_bit = 0;
+    _weight_units_per_bit = 0;
+}
 
-  }
-  void CN0216::init(float cal_weight, uint8_t mode_val, uint8_t filter_val)
-  {
+/**
+ * Initializes the mode and filter values of the AD7791 and sets the weight to be used in calibration
+ * @param cal_weight - weight used in calibration
+ * @param mode_val - value of the mode register
+ * @param filter_val - value of the filter register
+ */
+void CN0216::init(float cal_weight, uint8_t mode_val, uint8_t filter_val)
+{
     _cal_weight = cal_weight;
     ad7791.frequency(500000);
     wait_ms(50);
@@ -69,62 +82,80 @@
     wait_us(2);
     ad7791.write_filter_reg(filter_val);
     wait_ms(50);
-  }
+}
 
-  void CN0216::calibrate(CalibrationStep_t cal)
-  {
+/**
+ * Calibrates the CN0216 weigh scale
+ * @param cal - calibration step.
+ * Step CN0216::ZERO_SCALE_CALIBRATION will take CN0216::_NUMBER_OF_SAMPLES samples and use the minimum as value for the zero scale
+ * Step CN0216::FULL_SCALE_CALIBRATION will take CN0216::_NUMBER_OF_SAMPLES samples and use the average as value for the full scale
+ * Step COMPUTE_UNITS_PER_BIT will compute the grams per bit used in weight computation.
+ */
+void CN0216::calibrate(CalibrationStep_t cal)
+{
     uint64_t sum = 0;
     uint32_t min = 0xFFFFFFFF;
     uint32_t sample = 0;
-    switch(cal)
-    {
-    case ZERO_SCALE_CALIBRATION:
-    case FULL_SCALE_CALIBRATION:
-      for(int i = 0;i < _NUMBER_OF_SAMPLES;i++)
-      {
-    	  sample = ad7791.read_u32();
-    	  min = (min<sample) ? min : sample;
-    	  sum += ad7791.read_u32();
-		  wait_us(5);
-      }
-      if(cal == ZERO_SCALE_CALIBRATION)
-      {
-    	 // pc.printf("ZERO SCALE VALUE = %x",sum);
-    	  _zero_scale_value = min;
-      }
-      else
-      {
-    	//  pc.printf("FULL SCALE VALUE = %x",sum);
-    	  sum = sum / _NUMBER_OF_SAMPLES;
-    	  _full_scale_value = sum;
-      }
-    break;
+    switch(cal) {
+        case ZERO_SCALE_CALIBRATION:
+        case FULL_SCALE_CALIBRATION:
+            for(int i = 0; i < _NUMBER_OF_SAMPLES; i++) {
+                sample = ad7791.read_u32();
+                min = (min < sample) ? min : sample;
+                sum += ad7791.read_u32();
+                wait_us(5);
+            }
+            if(cal == ZERO_SCALE_CALIBRATION) {
+                // pc.printf("ZERO SCALE VALUE = %x",sum);
+                _zero_scale_value = min;
+            } else {
+                //  pc.printf("FULL SCALE VALUE = %x",sum);
+                sum = sum / _NUMBER_OF_SAMPLES;
+                _full_scale_value = sum;
+            }
+            break;
 
-    case COMPUTE_GRAM_PER_BIT:
-      _grams_per_bit = _cal_weight / (static_cast<float> (_full_scale_value - _zero_scale_value));  /* Calculate number of grams per LSB */
-     // pc.printf("GRAMS/LSB = %f", _grams_per_bit);
-    break;
-    default:
-    break;
+        case COMPUTE_UNITS_PER_BIT:
+            _weight_units_per_bit = _cal_weight / (static_cast<float> (_full_scale_value - _zero_scale_value));  /* Calculate number of grams per LSB */
+            // pc.printf("GRAMS/LSB = %f", _grams_per_bit);
+            break;
+        default:
+            break;
     }
 
-  }
+}
 
-  float CN0216::compute_weight(uint32_t data)
-  {
+/**
+ * Computes the weight based on the formula
+ * weight = (data - zeroscale) * weight_units_per_bit
+ * @param data read from the ADC
+ * @return weight based on data
+ */
+float CN0216::compute_weight(uint32_t data)
+{
 //	pc.printf("\r\nFULL_SCALE_VALUE = %x\r\nZERO_SCALE_VALUE = %x\r\nDATA READ = %x\r\nGRAMS/LSB = %f\r\n",_full_scale_value,data,_zero_scale_value,_grams_per_bit);
-	if(data<_zero_scale_value)
-		data = _zero_scale_value; // clamp data to 0
-	float weight_in_grams =  (static_cast<float>((data) - _zero_scale_value)) * _grams_per_bit;         /* Calculate weight */
+    if(data < _zero_scale_value)
+        data = _zero_scale_value; // clamp data to 0
+    float weight_in_grams =  (static_cast<float>((data) - _zero_scale_value)) * _weight_units_per_bit;         /* Calculate weight */
     return weight_in_grams;
-  }
-  uint32_t CN0216::read_u32()
-  {
+}
+
+/**
+ * Reads the AD7791
+ * @return value read by the ADC
+ */
+uint32_t CN0216::read_u32()
+{
     return ad7791.read_u32();
-  }
-  float CN0216::read_weight()  
-  {
-	uint32_t weight =  read_u32();
+}
+
+/**
+ * Reads the ADC and computes the weight based on the formula described above.
+ * @return weight
+ */
+float CN0216::read_weight()
+{
+    uint32_t weight =  read_u32();
     return compute_weight(weight);
-  }
-  
+}
+
--- a/libraries/cn0216/CN0216.h	Fri Apr 29 17:16:55 2016 +0300
+++ b/libraries/cn0216/CN0216.h	Fri Apr 29 17:34:07 2016 +0300
@@ -8,33 +8,32 @@
 class CN0216
 {
 public:
-  typedef enum
-  {
-    ZERO_SCALE_CALIBRATION,
-    FULL_SCALE_CALIBRATION,
-    COMPUTE_GRAM_PER_BIT
-  } CalibrationStep_t;
+    typedef enum {
+        ZERO_SCALE_CALIBRATION, ///< Calibration of the zero scale value
+        FULL_SCALE_CALIBRATION, ///< Calibration of the full scale value
+        COMPUTE_UNITS_PER_BIT   ///< Units per LSB computation
+    } CalibrationStep_t;
 
-  CN0216(PinName CSAD7791 = D8, PinName MOSI = SPI_MOSI, PinName MISO = SPI_MISO, PinName SCK = SPI_SCK);
-  void init(float cal_weight = _DEFAULT_CAL_WEIGHT, uint8_t mode_val = _DEFAULT_MODE_VAL, uint8_t filter_val = _DEFAULT_FILTER_VAL);
-  void calibrate(CalibrationStep_t cal);
-  float compute_weight(uint32_t data);
-  uint32_t read_u32();
-  float read_weight()  ;
+    CN0216(PinName CSAD7791 = D8, PinName MOSI = SPI_MOSI, PinName MISO = SPI_MISO, PinName SCK = SPI_SCK);
+    void init(float cal_weight = _DEFAULT_CAL_WEIGHT, uint8_t mode_val = _DEFAULT_MODE_VAL, uint8_t filter_val = _DEFAULT_FILTER_VAL);
+    void calibrate(CalibrationStep_t cal);
+    float compute_weight(uint32_t data);
+    uint32_t read_u32();
+    float read_weight();
 
 private:
 
-  const static int _DEFAULT_MODE_VAL = AD7791::MD1 | AD7791::MD0; // POWERDOWN MODE
-  const static int _DEFAULT_FILTER_VAL = AD7791::FS0 | AD7791::FS1 | AD7791::FS2;
-  const static int _NUMBER_OF_SAMPLES = 50;
-  const static int _DEFAULT_CAL_WEIGHT = 1000.0;
+    const static int _NUMBER_OF_SAMPLES = 20; ///< Number of samples used in calibration
+    const static int _DEFAULT_MODE_VAL = AD7791::MD1 | AD7791::MD0; // POWERDOWN MODE
+    const static int _DEFAULT_FILTER_VAL = AD7791::FS0 | AD7791::FS1 | AD7791::FS2;
+    const static int _DEFAULT_CAL_WEIGHT = 1000.0;
 
-  AD7791 ad7791;
-  float _cal_weight;
-  uint32_t _zero_scale_value;
-  uint32_t _full_scale_value;
-  float _grams_per_bit;
-               
+    AD7791 ad7791;
+    float _cal_weight;
+    uint32_t _zero_scale_value;
+    uint32_t _full_scale_value;
+    float _weight_units_per_bit;
+
 };
 
 #endif