Solid experiment slow control.

Dependencies:   AD5384 SWSPI S_SCTRL_SMlib T_adt7320 adc_ad9249 mbed sscm_comm

Fork of sscm by wimbeaumont Project

Committer:
wbeaumont
Date:
Thu Oct 02 10:49:22 2014 +0000
Revision:
2:d18b1a1643e8
Parent:
1:f792767b2223
Child:
3:badf265a8898
added DAC support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:dcd70d1b89b1 1 #include "mbed.h"
wbeaumont 1:f792767b2223 2 /* SOLID SM1 Slow Control firmware
wbeaumont 1:f792767b2223 3 *
wbeaumont 1:f792767b2223 4
wbeaumont 1:f792767b2223 5 * V 1.0? initial version release
wbeaumont 1:f792767b2223 6 * v 1.11 version , added status field
wbeaumont 1:f792767b2223 7 * v 1.13 corrected error in ADC register write
wbeaumont 2:d18b1a1643e8 8 * v 1.14 added heartbeat off / in
wbeaumont 2:d18b1a1643e8 9 * v 1.20 added DAC
wbeaumont 1:f792767b2223 10 */
wbeaumont 0:dcd70d1b89b1 11
wbeaumont 0:dcd70d1b89b1 12 #include "SWSPI_BI.h"
wbeaumont 0:dcd70d1b89b1 13
wbeaumont 2:d18b1a1643e8 14
wbeaumont 0:dcd70d1b89b1 15 #include "solid_sctrl_def.h"
wbeaumont 0:dcd70d1b89b1 16 #include "S_SCTRL_SM1_PinDef.h"
wbeaumont 0:dcd70d1b89b1 17 #include "S_SCTRL_SM1_hwfunct.h"
wbeaumont 2:d18b1a1643e8 18 #include "AD9249.h"
wbeaumont 2:d18b1a1643e8 19 #include "AD5384.h"
wbeaumont 0:dcd70d1b89b1 20
wbeaumont 2:d18b1a1643e8 21 #define VERSION "1.20"
wbeaumont 0:dcd70d1b89b1 22
wbeaumont 0:dcd70d1b89b1 23 #define DEBUGPF(x) printf((x));
wbeaumont 0:dcd70d1b89b1 24
wbeaumont 0:dcd70d1b89b1 25
wbeaumont 0:dcd70d1b89b1 26 #define MAXCMDLLENGTH 256
wbeaumont 0:dcd70d1b89b1 27
wbeaumont 0:dcd70d1b89b1 28 #include "sscm_comm.h"
wbeaumont 0:dcd70d1b89b1 29 using namespace sscm_comm ;
wbeaumont 0:dcd70d1b89b1 30
wbeaumont 0:dcd70d1b89b1 31
wbeaumont 0:dcd70d1b89b1 32
wbeaumont 0:dcd70d1b89b1 33 Serial pc(USBTX,USBRX);
wbeaumont 0:dcd70d1b89b1 34
wbeaumont 0:dcd70d1b89b1 35
wbeaumont 0:dcd70d1b89b1 36 char cmdin[MAXCMDLLENGTH];
wbeaumont 0:dcd70d1b89b1 37 u8 cmdready;
wbeaumont 0:dcd70d1b89b1 38 bool cmdstart=false;
wbeaumont 0:dcd70d1b89b1 39
wbeaumont 0:dcd70d1b89b1 40
wbeaumont 0:dcd70d1b89b1 41
wbeaumont 0:dcd70d1b89b1 42
wbeaumont 0:dcd70d1b89b1 43 void pc_callback() {
wbeaumont 0:dcd70d1b89b1 44 // Note: you need to actually read from the serial to clear the RX interrupt
wbeaumont 0:dcd70d1b89b1 45 static unsigned char cnt;
wbeaumont 0:dcd70d1b89b1 46
wbeaumont 0:dcd70d1b89b1 47 char c;
wbeaumont 0:dcd70d1b89b1 48 c=pc.getc();
wbeaumont 0:dcd70d1b89b1 49 if( c== CMDSTART && !cmdready ) { cnt=0; cmdstart=true; cmdready=false; }
wbeaumont 0:dcd70d1b89b1 50 if ( cmdstart) {
wbeaumont 0:dcd70d1b89b1 51 cmdin[cnt]=c; cnt++;
wbeaumont 0:dcd70d1b89b1 52 if (c== CMDSTOP ) { cmdready=true;}
wbeaumont 0:dcd70d1b89b1 53 }
wbeaumont 0:dcd70d1b89b1 54
wbeaumont 0:dcd70d1b89b1 55
wbeaumont 0:dcd70d1b89b1 56
wbeaumont 0:dcd70d1b89b1 57 //printf("%c:",c );
wbeaumont 0:dcd70d1b89b1 58 }
wbeaumont 0:dcd70d1b89b1 59
wbeaumont 0:dcd70d1b89b1 60
wbeaumont 0:dcd70d1b89b1 61
wbeaumont 0:dcd70d1b89b1 62
wbeaumont 0:dcd70d1b89b1 63 int main() {
wbeaumont 0:dcd70d1b89b1 64 pc.attach(pc_callback);
wbeaumont 0:dcd70d1b89b1 65 ssc_cmd cmd;
wbeaumont 1:f792767b2223 66 bool heartbeat=true;
wbeaumont 0:dcd70d1b89b1 67 int i = 0;
wbeaumont 0:dcd70d1b89b1 68 HWlines hwl ;
wbeaumont 0:dcd70d1b89b1 69 assignports( &hwl );
wbeaumont 0:dcd70d1b89b1 70 setdefault(hwl );
wbeaumont 2:d18b1a1643e8 71 SWSPI spi(hwl.mosi[0],hwl.miso[0],hwl.sclk[0]); // mosi, miso, sclk
wbeaumont 2:d18b1a1643e8 72 SWSPI spi2(hwl.mosi[1],hwl.miso[1],hwl.sclk[1]); // mosi, miso, sclk
wbeaumont 0:dcd70d1b89b1 73
wbeaumont 0:dcd70d1b89b1 74 SWSPI_BI spi_adc(hwl.msio[0],hwl.direction[0],hwl.stio_mo[0] ,hwl.sclk[0]); // msio, dir , sclk
wbeaumont 0:dcd70d1b89b1 75 SWSPI_BI spi_adc2(hwl.msio[1],hwl.direction[1],hwl.stio_mo[1],hwl.sclk[1]); // msio, dir , sclk
wbeaumont 0:dcd70d1b89b1 76 // initalize ADC classes
wbeaumont 0:dcd70d1b89b1 77 AD9249 adc[2][2]={AD9249( &spi_adc,hwl.csb1[0]), AD9249( &spi_adc,hwl.csb2[0]),
wbeaumont 0:dcd70d1b89b1 78 AD9249( &spi_adc2,hwl.csb1[1]), AD9249 ( &spi_adc2,hwl.csb2[1])};
wbeaumont 2:d18b1a1643e8 79 AD5384 dac[2][1]={AD5384(&spi,hwl.dac_cs[0]), AD5384(&spi2,hwl.dac_cs[1]) };
wbeaumont 0:dcd70d1b89b1 80
wbeaumont 0:dcd70d1b89b1 81
wbeaumont 0:dcd70d1b89b1 82
wbeaumont 0:dcd70d1b89b1 83
wbeaumont 0:dcd70d1b89b1 84
wbeaumont 0:dcd70d1b89b1 85 pc.printf("\nSOLID SLOW CONTROL for SM1 version %s %s %s \n\r",VERSION,__DATE__,__TIME__ );
wbeaumont 0:dcd70d1b89b1 86 cmdready=false;
wbeaumont 0:dcd70d1b89b1 87 while (1) {
wbeaumont 0:dcd70d1b89b1 88 wait(0.5);
wbeaumont 0:dcd70d1b89b1 89 if (cmdready) {
wbeaumont 0:dcd70d1b89b1 90 cmdready=false;
wbeaumont 0:dcd70d1b89b1 91 int decresult=decode_cmd(cmdin,&cmd);
wbeaumont 0:dcd70d1b89b1 92 printf("decode result = %d \n\r" ,decresult);
wbeaumont 1:f792767b2223 93 if( decresult){ continue; }
wbeaumont 0:dcd70d1b89b1 94 char cmdoutstr[100];
wbeaumont 0:dcd70d1b89b1 95
wbeaumont 0:dcd70d1b89b1 96 u8 do8; // dataout
wbeaumont 0:dcd70d1b89b1 97 u16 do16;
wbeaumont 1:f792767b2223 98 cmd.status=1; // use it for the moment as error handling ,
wbeaumont 1:f792767b2223 99 switch ( cmd.dev) {
wbeaumont 2:d18b1a1643e8 100
wbeaumont 1:f792767b2223 101 case ADC :
wbeaumont 1:f792767b2223 102 cmd.status=2;
wbeaumont 1:f792767b2223 103 pc.printf("ADC cmd = %s \n\r",cmd.cmd);
wbeaumont 1:f792767b2223 104 pc.printf("check now ranges %d %d \n\r",cmd.con, cmd.devnr);
wbeaumont 1:f792767b2223 105 if (!strcmp( cmd.cmd, "spa1")) { adc[cmd.con-1][cmd.devnr-1].setPattern1(cmd.datain);cmd.dataout=cmd.datain;cmd.status=0;}
wbeaumont 1:f792767b2223 106 if (!strcmp( cmd.cmd, "spa2")) { adc[cmd.con-1][cmd.devnr-1].setPattern2(cmd.datain);cmd.dataout=cmd.datain;cmd.status=0;}
wbeaumont 2:d18b1a1643e8 107 if (!strcmp( cmd.cmd, "rpa1")) { adc[cmd.con-1][cmd.devnr-1].readPattern1(do16 ); cmd.dataout=do16; cmd.status=0; }
wbeaumont 2:d18b1a1643e8 108 if (!strcmp( cmd.cmd, "rpa2")) { adc[cmd.con-1][cmd.devnr-1].readPattern2(do16 ); cmd.dataout=do16; cmd.status=0; }
wbeaumont 2:d18b1a1643e8 109 if (!strcmp( cmd.cmd, "rr08")) {adc[cmd.con-1][cmd.devnr-1].readReg8(cmd.ch,do8 ); cmd.dataout=(u16)do8; cmd.status=0; }
wbeaumont 1:f792767b2223 110 if (!strcmp( cmd.cmd, "rr16")) {adc[cmd.con-1][cmd.devnr-1].readReg16(cmd.ch,do16 ); cmd.dataout=(u32)do16;cmd.status=0; }
wbeaumont 1:f792767b2223 111 if (!strcmp( cmd.cmd, "sr08")) { adc[cmd.con-1][cmd.devnr-1].setReg8(cmd.ch, (u8)cmd.datain ); cmd.dataout=cmd.datain; cmd.status=0; }
wbeaumont 1:f792767b2223 112 if (!strcmp( cmd.cmd, "sr16")) { adc[cmd.con-1][cmd.devnr-1].setReg16(cmd.ch, (u16)cmd.datain ); cmd.dataout=cmd.datain; cmd.status=0; }
wbeaumont 2:d18b1a1643e8 113 break;
wbeaumont 2:d18b1a1643e8 114 case DAC :
wbeaumont 2:d18b1a1643e8 115 cmd.status=2;
wbeaumont 2:d18b1a1643e8 116 if (!strcmp( cmd.cmd, "sdac")) { dac[cmd.con-1][cmd.devnr-1].set_dac(cmd.ch,cmd.datain);cmd.dataout=cmd.datain;cmd.status=0;}
wbeaumont 2:d18b1a1643e8 117 if (!strcmp( cmd.cmd, "rdac")) { do16=dac[cmd.con-1][cmd.devnr-1].get_dac(cmd.ch);cmd.dataout=do16;cmd.status=0;}
wbeaumont 1:f792767b2223 118 break;
wbeaumont 1:f792767b2223 119 case SSCCM:
wbeaumont 1:f792767b2223 120 if (!strcmp( cmd.cmd, "t_hb")) { heartbeat=!heartbeat;}
wbeaumont 1:f792767b2223 121 break;
wbeaumont 1:f792767b2223 122 default : cmd.status=1;
wbeaumont 1:f792767b2223 123 break;
wbeaumont 1:f792767b2223 124 } // end switch
wbeaumont 0:dcd70d1b89b1 125 encode_cmd ( cmdoutstr, &cmd);
wbeaumont 0:dcd70d1b89b1 126 pc.printf( "%s", cmdoutstr);
wbeaumont 0:dcd70d1b89b1 127 }//end if cmdready
wbeaumont 0:dcd70d1b89b1 128 else {
wbeaumont 1:f792767b2223 129 if ( heartbeat) printf( "%03d>%s\n\r",(i++ % 1000),cmdin);
wbeaumont 0:dcd70d1b89b1 130 }
wbeaumont 0:dcd70d1b89b1 131 }// end while
wbeaumont 0:dcd70d1b89b1 132
wbeaumont 0:dcd70d1b89b1 133 }