test program to test / develop the SOLID slow control

Dependencies:   AD5384 SWSPI S25FL216K S_SCTRL_SMlib T_adt7320 adc_ad9249 sscm_comm mbed

Files at this revision

API Documentation at this revision

Comitter:
wbeaumont
Date:
Tue Sep 23 08:25:38 2014 +0000
Child:
1:17b3c72d8357
Commit message:
version with response from ADC

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
S25FL216K.lib Show annotated file Show diff for this revision Revisions of this file
SWSPI.lib Show annotated file Show diff for this revision Revisions of this file
S_SCTRL_SMlib.lib Show annotated file Show diff for this revision Revisions of this file
adc_ad9249.lib Show annotated file Show diff for this revision Revisions of this file
adt7320.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AD5384.cpp	Tue Sep 23 08:25:38 2014 +0000
@@ -0,0 +1,68 @@
+#include "AD5384.h"
+
+
+#include "mbed.h"
+
+#define  nrch 40 // nr channels 
+#define  p2_14  163834
+#define  p2_13  8192
+
+#define C_ACTIVE 0
+#define C_DEACTIVE  1
+
+
+// spi mode has to be set for each transmission as spi bus can be shared 
+
+
+
+
+AD5384::AD5384(SWSPI *spiinterface ,DigitalOut* chipselect) {
+        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 ) {  
+    //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);
+    //  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);
+                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 word= (rw&1) << 22;
+            u32 shift = ((u32)ch &0x1F) << 14;
+            word = word | shift;
+            shift = ((u32)mode & 0x3) << 13;
+            word = word | shift;
+            word = word | (data & 0x3FF);
+            return word;
+}
+             
+void AD5384::set_spi_mode(){
+    spi->format(24,0);
+    spi->frequency(10000000);             
+}
+             
+ void AD5384::set_volt(u8 ch, float vout ){
+     volt[ch]=vout;
+     u16 dacin=calculate_dac_setting(ch,  vout );
+     set_spi_mode();
+     u32 data=format_word(3,ch,0,dacin); 
+     cs->write(C_ACTIVE);
+     spi->write(data);
+     cs->write(C_DEACTIVE);   
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AD5384.h	Tue Sep 23 08:25:38 2014 +0000
@@ -0,0 +1,37 @@
+#ifndef AD5384_H
+#define AD5384_H
+
+#include "solid_sctrl_def.h"
+#include "SWSPI.h"
+
+/*
+ * class to set and readback the AD5384
+ * to minimize the access to the device there is a shadow of the DAC, GAIN and OFFSET values 
+    
+ * V 0.1  inital development to see if reading / writing is possible    
+*/ 
+class SWSPI;
+#include "mbed.h"
+//class DigitalOut;
+
+class AD5384  {
+    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) ;
+    public: 
+        AD5384(SWSPI *spiinterface ,DigitalOut* chipselect );
+        
+        u16 dac[40];
+        u16 gain[40];
+        u16 offset[40];
+        float volt[40];
+        
+        
+        void  set_volt(u8 nr, float vout );
+};
+
+#endif 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/S25FL216K.lib	Tue Sep 23 08:25:38 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Sissors/code/S25FL216K/#2bcefc9e64f8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SWSPI.lib	Tue Sep 23 08:25:38 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/davervw/code/SWSPI/#02327a96a5e2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/S_SCTRL_SMlib.lib	Tue Sep 23 08:25:38 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/wbeaumont/code/S_SCTRL_SMlib/#20f21cb7792e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adc_ad9249.lib	Tue Sep 23 08:25:38 2014 +0000
@@ -0,0 +1,1 @@
+adc_ad9249#9efb460e962b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adt7320.h	Tue Sep 23 08:25:38 2014 +0000
@@ -0,0 +1,23 @@
+#ifndef ADT7320_H
+#define ADT7320_H
+
+/* 
+
+ adt7320 interface 
+
+*/
+class spi_v; 
+
+class at7320 {
+
+    
+public:
+
+float getTemperature();    
+    
+};
+
+
+
+
+#endif 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Sep 23 08:25:38 2014 +0000
@@ -0,0 +1,118 @@
+#include "mbed.h"
+#include "mbed.h"
+#include "SWSPI.h"
+#include "SWSPI_BI.h"
+#include "S25FL216K.h"
+#include "AD5384.h"
+#include "AD9249.h"
+#include "S_SCTRL_SM1_PinDef.h" 
+#include "S_SCTRL_SM1_hwfunct.h" 
+
+#define VERSION 1
+#define SUBVERSION 3
+#define DEBUGPF(x) printf((x));
+// pin function         pin id 
+// SPI 1 
+
+
+S25FL216K flash(F_MOSI, F_MISO, F_SCLK,F_CS); 
+Serial pc(USBTX,USBRX);
+
+
+char c='.';
+void callback() {
+    // Note: you need to actually read from the serial to clear the RX interrupt  
+    c = pc.getc();
+    printf("%c:",c );    
+ }
+
+
+//++++++++++++++++++++++
+  int main()   {   
+        
+       pc.attach(&callback);// handles the input on the RS232 interface  
+    HWlines  hwl ;
+    assignports( &hwl );
+    setdefault(hwl );
+    
+    SWSPI spi(hwl.mosi[0],hwl.miso[0],hwl.sclk[0]); // mosi, miso, sclk
+    SWSPI spi2(hwl.mosi[1],hwl.miso[1],hwl.sclk[1]); // mosi, miso, sclk
+    SWSPI_BI spi_adc(hwl.msio[0],hwl.direction[0],hwl.stio_mo[0] ,hwl.sclk[0]); // msio, dir , sclk 
+    SWSPI_BI spi_adc2(hwl.msio[1],hwl.direction[1],hwl.stio_mo[1],hwl.sclk[1]); // msio, dir , sclk 
+  
+    
+      AD9249 adc[2][2]={AD9249( &spi_adc,hwl.csb1[0]), AD9249( &spi_adc,hwl.csb2[0]),
+                        AD9249( &spi_adc2,hwl.csb1[1]),  AD9249  ( &spi_adc2,hwl.csb2[1])};
+     
+      AD5384 dac[2]={AD5384(&spi,hwl.dac_cs[0]), AD5384(&spi2,hwl.dac_cs[1]) };
+      
+      //float humi;
+      
+      
+      
+      DEBUGPF("start");
+      printf(" version  %d.%02d  compiled %s %s \n\r" , VERSION , SUBVERSION, __DATE__, __TIME__ );
+      printf(" boardserialnr %d \n\r", get_serialnr(&hwl));
+      u8 consel1=0;
+      u8 consel2=1;
+      u8 conls=0, conle=0;  // connector select loop start , connector select loop stop
+      // cc == connector counter
+            if( consel1==1 && consel2== 0) { conls=0; conle=1;} // only first connector connected 
+      else  if( consel1==0 && consel2== 1) { conls=1; conle=2;} // only second  connector connected 
+      else  if( consel1==1 && consel2== 1) { conls=0; conle=2;} // 
+      else                                 { conls=0; conle=0;} // no connectors connected 
+      unsigned char id, grade ;
+      
+      
+     /* for (u8 cc= conls; cc < conle ; cc++) {
+        adc[cc][0].getDevInfo(id,grade);printf(" %d %d ",id, grade);
+        adc[cc][1].getDevInfo(id,grade);printf(" %d %d ",id, grade);
+        
+      }*/
+      u32 count =0;
+      while(1)   {
+        for (u8 cc= conls; cc < conle ; cc++) {
+        u16 rb,rb2;      
+        adc[cc][0].getDevId(id);
+        adc[cc][0].getGrade(grade);
+        //adc[cc][0].getDevInfo(id,grade,rb);
+        printf("id %02X grade %02X",id, grade );
+        printf("\n\r");
+        adc[cc][0].setPattern1(0x1235);
+        adc[cc][0].setPattern2(0xA5FF);
+        adc[cc][0].readPattern1(rb);       
+        adc[cc][0].readPattern2(rb2);
+        printf("pattern1 %04X  pattern2 %04X",rb,rb2);
+        printf("\n\r");
+       // dac[cc].set_volt(count%32,count%163834);
+       }
+        wait(.5);count++;
+        
+        /*
+        DEBUGPF("spi2\n\r");
+        spi.format(8, 0);
+        spi.frequency(10000000);
+        cs.write(0);
+        spi.write(0x9f);
+        jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0);
+        cs.write(1);      
+        printf( "Jedec %d /n/r", jedecid);
+        
+        DEBUGPF("spi3\n\r");      
+        spi_adc.format(8,0);
+        spi_adc.frequency(10000000);
+        cs_adc.write(0);
+        spi_adc.write(0x9f);
+        jedecid = (spi_adc.write(0) << 16) | (spi_adc.write(0) << 8) | spi_adc.write(0);
+        cs_adc.write(1);
+        char c=0x12;
+        flash.write(0,&c,1);
+        DEBUGPF("dacset");
+        (void) dac.set_dac(4,2.21);  
+        humi=ain1.read();
+        printf("humid %f \n ",humi);   
+        */
+      }
+  }
+ 
+ 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Sep 23 08:25:38 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1
\ No newline at end of file