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