AD5384 DAC.

Fork of AD5384 by wimbeaumont Project

Files at this revision

API Documentation at this revision

Comitter:
NickRyder
Date:
Tue Oct 07 21:17:50 2014 +0000
Parent:
1:d2d6341d3e97
Commit message:
Tidying up.

Changed in this revision

AD5384.cpp Show annotated file Show diff for this revision Revisions of this file
AD5384.h Show annotated file Show diff for this revision Revisions of this file
diff -r d2d6341d3e97 -r 0d930c475e72 AD5384.cpp
--- a/AD5384.cpp	Sun Oct 05 17:10:03 2014 +0000
+++ b/AD5384.cpp	Tue Oct 07 21:17:50 2014 +0000
@@ -59,182 +59,155 @@
 
 
 
-AD5384::AD5384(SWSPI *spiinterface ,DigitalOut* chipselect):getVersion( VERSION_AD5384_HDR,AD5384_SRC_VERSION, __TIME__, __DATE__) {
-        vref=2.5; 
-        spi=spiinterface; 
-        cs=chipselect;
-        for ( int nc=0 ; nc < nrch; nc++){
-            gain[nc]=0x3FFE;
-            offset[nc]=0x2000;
-        } 
-    };
+AD5384::AD5384(SWSPI * spiinterface, DigitalOut * chipselect):
+    getVersion(VERSION_AD5384_HDR, AD5384_SRC_VERSION, __TIME__, __DATE__)
+{
+    vref = 2.5; 
+    spi = spiinterface; 
+    cs = chipselect;
+    for (int nc = 0; nc < nrch; nc++) {
+        gain[nc] = 0x3FFE;
+        offset[nc] = 0x2000;
+    } 
+};
     
-    u16  AD5384::calculate_dac_setting(u8 nr, float vout ) {  
+u16 AD5384::calculate_dac_setting(u8 nr, float vout) {  
     //Vout = 2 * Vref * x2 / 2^n  =>  x2 = Vout * 2^14 /(2 * Vref) 
     // x2 is loaded to the DAC string  
     // x1 is the 14 bit DAC wordt written to the DAC input register 
-                if( nr >39 ) return 0;
-                float x2= vout * p2_14 /(2 *vref);
+    if (nr > 39) return 0;
+    float x2 = vout * p2_14 / (2 * vref);
     //  x2 = [(gain+2)/2^n * x1] + offset-2^13               
     // x1 = 2^14/(gain+2) *  [ x2 - offset+2^13 ]
-                u16 x1 = p2_14/(gain[nr]+1) *( x2- offset[nr]+p2_13);
-                x1= 0x3FFF & x1;
-                dac[nr]=x1 ;
-                return x1;
-             };
-
+    u16 x1 = p2_14 / (gain[nr] + 1) * (x2 - offset[nr] + p2_13);
+    x1 = 0x3FFF & x1;
+    dac[nr] = x1;
+    return x1;
+};
 
-u32 AD5384::format_word(u8 mode,u8 ch,u8 rw,u16 data) {
-      // not clear what is the MSB bit ,set  it to zero
-            u32 shift = (u32) rw&1;            
-            u32 word= shift << 22;
-           
-            shift= (u32)(ch &0x1F);
-            shift = shift << 16;
-            word = word | shift;
-           
-            shift= (u32)(mode & 0x3);
-            shift = shift << 14;
-            word = word | shift;
-            
-            word = word | (data & 0x3FFF);
-           
-            return word;
+u32 AD5384::format_word(u8 mode, u8 ch, u8 rw, u16 data) {
+    // not clear what is the MSB bit ,set  it to zero
+    u32 shift = (u32) rw & 1;            
+    u32 word = shift << 22;
+    shift = (u32) (ch & 0x1F);
+    shift = shift << 16;
+    word = word | shift;
+    shift = (u32) (mode & 0x3);
+    shift = shift << 14;
+    word = word | shift;        
+    word = word | (data & 0x3FFF);       
+    return word;
 }
-             
-void AD5384::set_spi_mode(){
-    spi->format(24,1);
+
+void AD5384::set_spi_mode() {
+    spi->format(24, 1);
     spi->frequency(10000000);             
 }
              
- u16 AD5384::set_volt(u8 ch, float vout ){
-     volt[ch]=vout;
-     u16 dacin=calculate_dac_setting(ch,  vout );
+ u16 AD5384::set_volt(u8 ch, float vout) {
+     volt[ch] = vout;
+     u16 dacin = calculate_dac_setting(ch, vout);
      set_spi_mode();
-     u32 data=format_word(M_DATA_R,ch,0,dacin); 
+     u32 data = format_word(M_DATA_R, ch, 0, dacin); 
      cs->write(C_ACTIVE);
      spi->write(data);
      cs->write(C_DEACTIVE);   
      return dacin;
 }
 
-void  AD5384::init1(){
-        u16 ctrlreg=0;
-        ctrlreg = (INT_REF_2500 | REF_SRC_INT ) & TOGGLE_DISABLE;
-       set_reg(M_SPEC_R,CTRL_REG_ADDR,ctrlreg); 
-   
+void  AD5384::init1() {
+    u16 ctrlreg = 0;
+    ctrlreg = (INT_REF_2500 | REF_SRC_INT ) & TOGGLE_DISABLE;
+    set_reg(M_SPEC_R, CTRL_REG_ADDR, ctrlreg);    
 } 
 
-void  AD5384::init2(){
-        u16 ctrlreg=0;
-        // implecite INT_REF_1250
-        ctrlreg =  REF_SRC_INT  & TOGGLE_DISABLE;
-       set_reg(M_SPEC_R,CTRL_REG_ADDR,ctrlreg); 
-   
+void  AD5384::init2() {
+    u16 ctrlreg = 0;
+    // implecite INT_REF_1250
+    ctrlreg =  REF_SRC_INT  & TOGGLE_DISABLE;
+    set_reg(M_SPEC_R, CTRL_REG_ADDR, ctrlreg);  
 }
 
-
-
-u32  AD5384::soft_clr(){
-      return set_reg(M_SPEC_R,0x02,0x2000);
+u32  AD5384::soft_clr() {
+    return set_reg(M_SPEC_R, 0x02, 0x2000);
 } 
 
-
-u32  AD5384::soft_rst(){
-      return set_reg(M_SPEC_R,0x0F,0x211F);
+u32  AD5384::soft_rst() {
+    return set_reg(M_SPEC_R, 0x0F, 0x211F);
 } 
 
-
-u32  AD5384::clear_code(){
-    return set_reg(M_SPEC_R,0x01,0x2000);
- }
+u32  AD5384::clear_code() {
+    return set_reg(M_SPEC_R, 0x01, 0x2000);
+}
  
-u16  AD5384::set_gain(u8 ch, u16 gain  ){
-     set_reg(M_GAIN_R,ch,gain);
-     return gain;
+u16  AD5384::set_gain(u8 ch, u16 gain) {
+    set_reg(M_GAIN_R, ch, gain);
+    return gain;
 } 
 
-u16  AD5384::set_offset(u8 ch, u16 gain  ){
-     set_reg(M_OFFS_R,ch,gain);
-     return gain;
-} 
-
-
-u16 AD5384::set_dac(u8 ch, u16 dac  ){
-     set_reg(M_DATA_R,ch,dac);
-     return dac;
+u16  AD5384::set_offset(u8 ch, u16 gain){
+    set_reg(M_OFFS_R, ch, gain);
+    return gain;
 } 
 
 
+u16 AD5384::set_dac(u8 ch, u16 dac){
+    set_reg(M_DATA_R, ch, dac);
+    return dac;
+} 
 
-
+u32  AD5384::set_reg(u8 mode, u8 ch, u16 value){
+    set_spi_mode();
+    value = value & 0x3FFF;
+    u32 data = format_word(mode, ch, 0, value); 
+    cs->write(C_ACTIVE);
+    spi->write(data);
+    cs->write(C_DEACTIVE);   
+    return data;
+}     
  
- u32  AD5384::set_reg(u8 mode,u8 ch, u16 value  ){
-     set_spi_mode();
-     value=value & 0x3FFF;
-     u32 data=format_word(mode,ch,0,value); 
-     cs->write(C_ACTIVE);
-     spi->write(data);
-     cs->write(C_DEACTIVE);   
-     return data;
- }     
- 
- u16 AD5384::get_reg(u8 mode, u8 ch   ){
-     set_spi_mode();
-     u32 data=format_word(mode,ch,1,0);
-     cs->write(C_ACTIVE);
-     spi->write(data);
-     cs->write(C_DEACTIVE);
-     wait( .00001);
-     cs->write(C_ACTIVE);
-     data=spi->write(NOP_INST);
-     cs->write(C_DEACTIVE);
-     return (u16) data;
+u16 AD5384::get_reg(u8 mode, u8 ch   ){
+    set_spi_mode();
+    u32 data = format_word(mode, ch, 1, 0);
+    cs->write(C_ACTIVE);
+    spi->write(data);
+    cs->write(C_DEACTIVE);
+    wait(0.00001);
+    cs->write(C_ACTIVE);
+    data=spi->write(NOP_INST);
+    cs->write(C_DEACTIVE);
+    return (u16) data;
 }
  
- u16  AD5384::get_gain(u8 ch  ){
-    return get_reg(M_GAIN_R,ch);
-     
+u16 AD5384::get_gain(u8 ch) {
+    return get_reg(M_GAIN_R, ch); 
 }    
 
-u16 AD5384::get_dac(u8 ch){
-    return get_reg(M_DATA_R,ch);
-     
+u16 AD5384::get_dac(u8 ch) {
+    return get_reg(M_DATA_R, ch);
 }   
-       
-       
-       
-       
- u16  AD5384::get_offset(u8 ch  ){
-    return get_reg(M_OFFS_R,ch);
-     
+
+u16  AD5384::get_offset(u8 ch) {
+    return get_reg(M_OFFS_R, ch);
 }        
 
 u32  AD5384::get_ctrl(){
-    
     return get_reg(M_SPEC_R, 0x0C);   
 }    
 
-  
- 
-   
 u16 AD5384::get_ch_out_reg(u8 ch) {
-     u32 data=format_word(M_DATA_R,ch,1,0);
-     cs->write(C_ACTIVE);
-     spi->write(data);
-     cs->write(C_DEACTIVE);
-     wait( .00001);
-     cs->write(C_ACTIVE);
-     data=spi->write(NOP_INST);
-     cs->write(C_DEACTIVE);
-     return (u16) data;
+    u32 data=format_word(M_DATA_R,ch,1,0);
+    cs->write(C_ACTIVE);
+    spi->write(data);
+    cs->write(C_DEACTIVE);
+    wait(0.00001);
+    cs->write(C_ACTIVE);
+    data = spi->write(NOP_INST);
+    cs->write(C_DEACTIVE);
+    return (u16) data;
 }    
 
-
-
-
-
-#include "sscm_comm.h" 
+#include "sscm_comm.h"  // Weird place for an include
 /*
  
 u16 AD5384::get_src_version_nr(){
diff -r d2d6341d3e97 -r 0d930c475e72 AD5384.h
--- a/AD5384.h	Sun Oct 05 17:10:03 2014 +0000
+++ b/AD5384.h	Tue Oct 07 21:17:50 2014 +0000
@@ -21,40 +21,37 @@
 //class DigitalOut;
 
 class AD5384 : public getVersion {
-    SWSPI *spi ;
-    DigitalOut* cs; 
+    SWSPI *spi;
+    DigitalOut * cs; 
     float vref;
     
     void set_spi_mode();
-    u16 calculate_dac_setting(u8 nr, float vout );
-    u32 format_word(u8 mode,u8 ch,u8 rw,u16 data) ;
-    u16 get_reg(u8 mode, u8 ch   );
-    u32  set_reg(u8 mode,u8 ch, u16 value  );
+    u16 calculate_dac_setting(u8 nr, float vout);
+    u32 format_word(u8 mode, u8 ch, u8 rw, u16 data);
+    u16 get_reg(u8 mode, u8 ch);
+    u32  set_reg(u8 mode, u8 ch, u16 value);
     public: 
-        AD5384(SWSPI *spiinterface ,DigitalOut* chipselect );
+        AD5384(SWSPI * spiinterface, DigitalOut * chipselect);
         
         u16 dac[40];
         u16 gain[40];
         u16 offset[40];
         float volt[40];
-       u16 get_dac(u8 ch);
-       u16 set_dac( u8 ch, u16 dac);
-       u16 get_ch_out_reg(u8 ch) ;
-       u16 set_volt(u8 nr, float vout );
-       u16 set_gain(u8 ch, u16 gain  );
-       u16 get_gain(u8 ch );
-       u16 set_offset(u8 ch, u16 gain);
-       u16 get_offset(u8 ch  );
-       // ctnrls
-       u32 get_ctrl();
-       u32 soft_clr();
-       u32 soft_rst();
-       u32 clear_code();
-       void init1();
-       void init2();
-       
-        
- 
+        u16 get_dac(u8 ch);
+        u16 set_dac(u8 ch, u16 dac);
+        u16 get_ch_out_reg(u8 ch) ;
+        u16 set_volt(u8 nr, float vout);
+        u16 set_gain(u8 ch, u16 gain);
+        u16 get_gain(u8 ch);
+        u16 set_offset(u8 ch, u16 gain);
+        u16 get_offset(u8 ch);
+        // ctnrls
+        u32 get_ctrl();
+        u32 soft_clr();
+        u32 soft_rst();
+        u32 clear_code();
+        void init1();
+        void init2();
 };
 
 #endif 
\ No newline at end of file