![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
firmware for the SOLID Slow Control Module for SM1
Dependencies: S_SCTRL_SMlib adc_ad9249 mbed sscm_comm AD5384 SWSPI T_adt7320
main.cpp@3:badf265a8898, 2014-10-05 (annotated)
- Committer:
- wbeaumont
- Date:
- Sun Oct 05 17:13:02 2014 +0000
- Revision:
- 3:badf265a8898
- Parent:
- 2:d18b1a1643e8
- Child:
- 4:009703f995c6
version class added new dac commands, init commandsd
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:badf265a8898 | 9 | * v 1.20 added DAC |
wbeaumont | 3:badf265a8898 | 10 | * v 1.30 added Temperature probe support |
wbeaumont | 3:badf265a8898 | 11 | * v 1.31 bug correction ,for selecting the probe the devnr has to be used |
wbeaumont | 3:badf265a8898 | 12 | * v 1.35 added version read for software modules (comm, T , ADC , DAC SWSPI) |
wbeaumont | 3:badf265a8898 | 13 | * v 1.36 added ini1 ini2 srst commands for DAC |
wbeaumont | 3:badf265a8898 | 14 | * v 1.37 new get Version class |
wbeaumont | 1:f792767b2223 | 15 | */ |
wbeaumont | 0:dcd70d1b89b1 | 16 | |
wbeaumont | 3:badf265a8898 | 17 | #define VERSION "1.37" |
wbeaumont | 0:dcd70d1b89b1 | 18 | |
wbeaumont | 2:d18b1a1643e8 | 19 | |
wbeaumont | 3:badf265a8898 | 20 | #include "SWSPI_BI.h" |
wbeaumont | 0:dcd70d1b89b1 | 21 | #include "solid_sctrl_def.h" |
wbeaumont | 0:dcd70d1b89b1 | 22 | #include "S_SCTRL_SM1_PinDef.h" |
wbeaumont | 0:dcd70d1b89b1 | 23 | #include "S_SCTRL_SM1_hwfunct.h" |
wbeaumont | 2:d18b1a1643e8 | 24 | #include "AD9249.h" |
wbeaumont | 2:d18b1a1643e8 | 25 | #include "AD5384.h" |
wbeaumont | 3:badf265a8898 | 26 | #include "adt7320.h" |
wbeaumont | 3:badf265a8898 | 27 | #include "getVersion.h" |
wbeaumont | 0:dcd70d1b89b1 | 28 | |
wbeaumont | 0:dcd70d1b89b1 | 29 | #define DEBUGPF(x) printf((x)); |
wbeaumont | 0:dcd70d1b89b1 | 30 | |
wbeaumont | 0:dcd70d1b89b1 | 31 | |
wbeaumont | 0:dcd70d1b89b1 | 32 | #define MAXCMDLLENGTH 256 |
wbeaumont | 0:dcd70d1b89b1 | 33 | |
wbeaumont | 0:dcd70d1b89b1 | 34 | #include "sscm_comm.h" |
wbeaumont | 0:dcd70d1b89b1 | 35 | using namespace sscm_comm ; |
wbeaumont | 0:dcd70d1b89b1 | 36 | |
wbeaumont | 0:dcd70d1b89b1 | 37 | |
wbeaumont | 0:dcd70d1b89b1 | 38 | |
wbeaumont | 0:dcd70d1b89b1 | 39 | Serial pc(USBTX,USBRX); |
wbeaumont | 0:dcd70d1b89b1 | 40 | |
wbeaumont | 0:dcd70d1b89b1 | 41 | |
wbeaumont | 0:dcd70d1b89b1 | 42 | char cmdin[MAXCMDLLENGTH]; |
wbeaumont | 0:dcd70d1b89b1 | 43 | u8 cmdready; |
wbeaumont | 0:dcd70d1b89b1 | 44 | bool cmdstart=false; |
wbeaumont | 0:dcd70d1b89b1 | 45 | |
wbeaumont | 0:dcd70d1b89b1 | 46 | |
wbeaumont | 0:dcd70d1b89b1 | 47 | void pc_callback() { |
wbeaumont | 0:dcd70d1b89b1 | 48 | // Note: you need to actually read from the serial to clear the RX interrupt |
wbeaumont | 0:dcd70d1b89b1 | 49 | static unsigned char cnt; |
wbeaumont | 0:dcd70d1b89b1 | 50 | |
wbeaumont | 0:dcd70d1b89b1 | 51 | char c; |
wbeaumont | 0:dcd70d1b89b1 | 52 | c=pc.getc(); |
wbeaumont | 0:dcd70d1b89b1 | 53 | if( c== CMDSTART && !cmdready ) { cnt=0; cmdstart=true; cmdready=false; } |
wbeaumont | 0:dcd70d1b89b1 | 54 | if ( cmdstart) { |
wbeaumont | 0:dcd70d1b89b1 | 55 | cmdin[cnt]=c; cnt++; |
wbeaumont | 0:dcd70d1b89b1 | 56 | if (c== CMDSTOP ) { cmdready=true;} |
wbeaumont | 0:dcd70d1b89b1 | 57 | } |
wbeaumont | 0:dcd70d1b89b1 | 58 | |
wbeaumont | 0:dcd70d1b89b1 | 59 | |
wbeaumont | 0:dcd70d1b89b1 | 60 | |
wbeaumont | 0:dcd70d1b89b1 | 61 | //printf("%c:",c ); |
wbeaumont | 0:dcd70d1b89b1 | 62 | } |
wbeaumont | 0:dcd70d1b89b1 | 63 | |
wbeaumont | 0:dcd70d1b89b1 | 64 | |
wbeaumont | 0:dcd70d1b89b1 | 65 | |
wbeaumont | 0:dcd70d1b89b1 | 66 | |
wbeaumont | 0:dcd70d1b89b1 | 67 | int main() { |
wbeaumont | 0:dcd70d1b89b1 | 68 | pc.attach(pc_callback); |
wbeaumont | 0:dcd70d1b89b1 | 69 | ssc_cmd cmd; |
wbeaumont | 1:f792767b2223 | 70 | bool heartbeat=true; |
wbeaumont | 0:dcd70d1b89b1 | 71 | int i = 0; |
wbeaumont | 0:dcd70d1b89b1 | 72 | HWlines hwl ; |
wbeaumont | 0:dcd70d1b89b1 | 73 | assignports( &hwl ); |
wbeaumont | 0:dcd70d1b89b1 | 74 | setdefault(hwl ); |
wbeaumont | 3:badf265a8898 | 75 | // reuse getVesion |
wbeaumont | 3:badf265a8898 | 76 | getVersion gv( VERSION, VERSION , __TIME__, __DATE__ ); |
wbeaumont | 3:badf265a8898 | 77 | getVersion gvc; |
wbeaumont | 3:badf265a8898 | 78 | getsscmVersion gvsscm; |
wbeaumont | 2:d18b1a1643e8 | 79 | SWSPI spi(hwl.mosi[0],hwl.miso[0],hwl.sclk[0]); // mosi, miso, sclk |
wbeaumont | 2:d18b1a1643e8 | 80 | SWSPI spi2(hwl.mosi[1],hwl.miso[1],hwl.sclk[1]); // mosi, miso, sclk |
wbeaumont | 0:dcd70d1b89b1 | 81 | |
wbeaumont | 0:dcd70d1b89b1 | 82 | SWSPI_BI spi_adc(hwl.msio[0],hwl.direction[0],hwl.stio_mo[0] ,hwl.sclk[0]); // msio, dir , sclk |
wbeaumont | 0:dcd70d1b89b1 | 83 | SWSPI_BI spi_adc2(hwl.msio[1],hwl.direction[1],hwl.stio_mo[1],hwl.sclk[1]); // msio, dir , sclk |
wbeaumont | 0:dcd70d1b89b1 | 84 | // initalize ADC classes |
wbeaumont | 0:dcd70d1b89b1 | 85 | AD9249 adc[2][2]={AD9249( &spi_adc,hwl.csb1[0]), AD9249( &spi_adc,hwl.csb2[0]), |
wbeaumont | 0:dcd70d1b89b1 | 86 | AD9249( &spi_adc2,hwl.csb1[1]), AD9249 ( &spi_adc2,hwl.csb2[1])}; |
wbeaumont | 2:d18b1a1643e8 | 87 | AD5384 dac[2][1]={AD5384(&spi,hwl.dac_cs[0]), AD5384(&spi2,hwl.dac_cs[1]) }; |
wbeaumont | 0:dcd70d1b89b1 | 88 | |
wbeaumont | 3:badf265a8898 | 89 | adt7320 temp[2][3]= { adt7320(&spi, hwl.t_cs[0]),adt7320(&spi, hwl.tc_cs1[0]),adt7320(&spi, hwl.tc_cs2[0]), |
wbeaumont | 3:badf265a8898 | 90 | adt7320(&spi2, hwl.t_cs[1]),adt7320(&spi2, hwl.tc_cs1[1]),adt7320(&spi2, hwl.tc_cs2[1]) }; |
wbeaumont | 0:dcd70d1b89b1 | 91 | |
wbeaumont | 0:dcd70d1b89b1 | 92 | |
wbeaumont | 0:dcd70d1b89b1 | 93 | |
wbeaumont | 0:dcd70d1b89b1 | 94 | pc.printf("\nSOLID SLOW CONTROL for SM1 version %s %s %s \n\r",VERSION,__DATE__,__TIME__ ); |
wbeaumont | 0:dcd70d1b89b1 | 95 | cmdready=false; |
wbeaumont | 0:dcd70d1b89b1 | 96 | while (1) { |
wbeaumont | 0:dcd70d1b89b1 | 97 | wait(0.5); |
wbeaumont | 3:badf265a8898 | 98 | char cmdoutstr[100]; |
wbeaumont | 0:dcd70d1b89b1 | 99 | if (cmdready) { |
wbeaumont | 0:dcd70d1b89b1 | 100 | cmdready=false; |
wbeaumont | 0:dcd70d1b89b1 | 101 | int decresult=decode_cmd(cmdin,&cmd); |
wbeaumont | 0:dcd70d1b89b1 | 102 | printf("decode result = %d \n\r" ,decresult); |
wbeaumont | 3:badf265a8898 | 103 | if( decresult){ cmd.status=abs(decresult); encode_cmd ( cmdoutstr, &cmd); continue; } |
wbeaumont | 3:badf265a8898 | 104 | |
wbeaumont | 0:dcd70d1b89b1 | 105 | |
wbeaumont | 0:dcd70d1b89b1 | 106 | u8 do8; // dataout |
wbeaumont | 0:dcd70d1b89b1 | 107 | u16 do16; |
wbeaumont | 1:f792767b2223 | 108 | cmd.status=1; // use it for the moment as error handling , |
wbeaumont | 1:f792767b2223 | 109 | switch ( cmd.dev) { |
wbeaumont | 2:d18b1a1643e8 | 110 | |
wbeaumont | 1:f792767b2223 | 111 | case ADC : |
wbeaumont | 1:f792767b2223 | 112 | cmd.status=2; |
wbeaumont | 1:f792767b2223 | 113 | pc.printf("ADC cmd = %s \n\r",cmd.cmd); |
wbeaumont | 1:f792767b2223 | 114 | pc.printf("check now ranges %d %d \n\r",cmd.con, cmd.devnr); |
wbeaumont | 1:f792767b2223 | 115 | 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 | 116 | 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 | 117 | if (!strcmp( cmd.cmd, "rpa1")) { adc[cmd.con-1][cmd.devnr-1].readPattern1(do16 ); cmd.dataout=do16; cmd.status=0; } |
wbeaumont | 2:d18b1a1643e8 | 118 | if (!strcmp( cmd.cmd, "rpa2")) { adc[cmd.con-1][cmd.devnr-1].readPattern2(do16 ); cmd.dataout=do16; cmd.status=0; } |
wbeaumont | 2:d18b1a1643e8 | 119 | 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 | 120 | 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 | 121 | 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 | 122 | 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 | 3:badf265a8898 | 123 | if (!strcmp( cmd.cmd, "ghrv")) {do16=adc[cmd.con-1][cmd.devnr-1].getHdrVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 124 | if (!strcmp( cmd.cmd, "gscv")) {do16=adc[cmd.con-1][cmd.devnr-1].getSrcVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 125 | if (!strcmp( cmd.cmd, "ini1")) { adc[cmd.con-1][cmd.devnr-1].init1();cmd.dataout=cmd.datain;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 126 | if (!strcmp( cmd.cmd, "ini2")) { adc[cmd.con-1][cmd.devnr-1].init2();cmd.dataout=cmd.datain;cmd.status=0;} |
wbeaumont | 2:d18b1a1643e8 | 127 | break; |
wbeaumont | 2:d18b1a1643e8 | 128 | case DAC : |
wbeaumont | 2:d18b1a1643e8 | 129 | cmd.status=2; |
wbeaumont | 2:d18b1a1643e8 | 130 | 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 | 131 | if (!strcmp( cmd.cmd, "rdac")) { do16=dac[cmd.con-1][cmd.devnr-1].get_dac(cmd.ch);cmd.dataout=do16;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 132 | if (!strcmp( cmd.cmd, "ini1")) { dac[cmd.con-1][cmd.devnr-1].init1();cmd.dataout=cmd.datain;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 133 | if (!strcmp( cmd.cmd, "ini2")) { dac[cmd.con-1][cmd.devnr-1].init2();cmd.dataout=cmd.datain;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 134 | if (!strcmp( cmd.cmd, "srst")) { dac[cmd.con-1][cmd.devnr-1].soft_rst();cmd.dataout=cmd.datain;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 135 | if (!strcmp( cmd.cmd, "ghrv")) {do16=dac[cmd.con-1][cmd.devnr-1].getHdrVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 136 | if (!strcmp( cmd.cmd, "gscv")) {do16=dac[cmd.con-1][cmd.devnr-1].getSrcVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 137 | |
wbeaumont | 1:f792767b2223 | 138 | break; |
wbeaumont | 3:badf265a8898 | 139 | case TEMP : cmd.status=2; |
wbeaumont | 3:badf265a8898 | 140 | if (!strcmp( cmd.cmd, "rtpd")){ do16=temp[cmd.con-1][cmd.devnr-1].get_T();cmd.dataout=do16;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 141 | if (!strcmp( cmd.cmd, "gtid")){ do8=temp[cmd.con-1][cmd.devnr-1].getId();cmd.dataout=do8;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 142 | if (!strcmp( cmd.cmd, "rtcr")){ do16=temp[cmd.con-1][cmd.devnr-1].get_TcritSP();cmd.dataout=do16;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 143 | if (!strcmp( cmd.cmd, "stcr")){ temp[cmd.con-1][cmd.devnr-1].set_TcritSP(cmd.datain);cmd.dataout=cmd.datain;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 144 | if (!strcmp( cmd.cmd, "ghrv")) {do16=temp[cmd.con-1][cmd.devnr-1].getHdrVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 145 | if (!strcmp( cmd.cmd, "gscv")) {do16=temp[cmd.con-1][cmd.devnr-1].getSrcVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 146 | if (!strcmp( cmd.cmd, "ini1")) { temp[cmd.con-1][cmd.devnr-1].init1();cmd.dataout=cmd.datain;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 147 | if (!strcmp( cmd.cmd, "ini2")) { temp[cmd.con-1][cmd.devnr-1].init2();cmd.dataout=cmd.datain;cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 148 | break; |
wbeaumont | 3:badf265a8898 | 149 | case SSCM: |
wbeaumont | 3:badf265a8898 | 150 | cmd.status=2; |
wbeaumont | 3:badf265a8898 | 151 | if (!strcmp( cmd.cmd, "t_hb")) { heartbeat=!heartbeat;cmd.status=2;} |
wbeaumont | 3:badf265a8898 | 152 | if (!strcmp( cmd.cmd, "gmpv")) {do16=gv.getHdrVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 153 | |
wbeaumont | 3:badf265a8898 | 154 | if (!strcmp( cmd.cmd, "gvhv")) {do16=gvc.getHdrVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 155 | if (!strcmp( cmd.cmd, "msnr")) {do8=get_serialnr(&hwl);cmd.dataout=(u16)do8; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 156 | if (!strcmp( cmd.cmd, "gchv")) {do16=gvsscm.getHdrVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 157 | if (!strcmp( cmd.cmd, "gcsv")) {do16=gvsscm.getSrcVersion();cmd.dataout=do16; cmd.status=0;} |
wbeaumont | 3:badf265a8898 | 158 | |
wbeaumont | 1:f792767b2223 | 159 | break; |
wbeaumont | 1:f792767b2223 | 160 | default : cmd.status=1; |
wbeaumont | 1:f792767b2223 | 161 | break; |
wbeaumont | 1:f792767b2223 | 162 | } // end switch |
wbeaumont | 0:dcd70d1b89b1 | 163 | encode_cmd ( cmdoutstr, &cmd); |
wbeaumont | 0:dcd70d1b89b1 | 164 | pc.printf( "%s", cmdoutstr); |
wbeaumont | 0:dcd70d1b89b1 | 165 | }//end if cmdready |
wbeaumont | 0:dcd70d1b89b1 | 166 | else { |
wbeaumont | 1:f792767b2223 | 167 | if ( heartbeat) printf( "%03d>%s\n\r",(i++ % 1000),cmdin); |
wbeaumont | 0:dcd70d1b89b1 | 168 | } |
wbeaumont | 0:dcd70d1b89b1 | 169 | }// end while |
wbeaumont | 0:dcd70d1b89b1 | 170 | |
wbeaumont | 0:dcd70d1b89b1 | 171 | } |