functions for the communication between SOLID slow control modules and Cprograms

Dependents:   sscm SPItest sscm

Committer:
wbeaumont
Date:
Fri Oct 23 11:22:36 2015 +0000
Revision:
12:cf007e9619e1
Parent:
10:0675c2e3d330
production version archiving

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 2:1f9887337a1b 1 #if defined (TARGET_KL25Z)
wbeaumont 2:1f9887337a1b 2 // in mbed problems to call strtol
wbeaumont 2:1f9887337a1b 3 #include "mbed.h" // for the strtol
wbeaumont 2:1f9887337a1b 4 #else
wbeaumont 2:1f9887337a1b 5 #include <stlib.h> // for the strtol
wbeaumont 2:1f9887337a1b 6 #endif
wbeaumont 1:288db0531b1f 7
wbeaumont 9:4bfcfa029dff 8
wbeaumont 9:4bfcfa029dff 9 /*****
wbeaumont 9:4bfcfa029dff 10 * version history
wbeaumont 9:4bfcfa029dff 11 * v3.31 first version history entry
wbeaumont 9:4bfcfa029dff 12 * v3.32 changed the return values for decode and range checking
wbeaumont 10:0675c2e3d330 13 * v3.33 corrected range checking temperature
wbeaumont 10:0675c2e3d330 14 * v3.40 added float to datain , dataout conversion
wbeaumont 10:0675c2e3d330 15 * v3.41 bug status decoding corrected
wbeaumont 12:cf007e9619e1 16 * (C) Wim Beaumont Univeristeit Antwerpen 2014 , 2015
wbeaumont 9:4bfcfa029dff 17 *******/
wbeaumont 9:4bfcfa029dff 18
wbeaumont 1:288db0531b1f 19 #include "sscm_comm.h"
wbeaumont 0:2afae79ea9ca 20
wbeaumont 0:2afae79ea9ca 21
wbeaumont 9:4bfcfa029dff 22
wbeaumont 9:4bfcfa029dff 23
wbeaumont 10:0675c2e3d330 24 #define SSCM_COMM_LIB_SRC_VERSION "3.40"
wbeaumont 0:2afae79ea9ca 25
wbeaumont 1:288db0531b1f 26 using namespace sscm_comm;
wbeaumont 0:2afae79ea9ca 27
wbeaumont 0:2afae79ea9ca 28 int sscm_comm::decode_cmd(char* input, ssc_cmd * sc){
wbeaumont 0:2afae79ea9ca 29 char part[6];
wbeaumont 0:2afae79ea9ca 30 int rv=0;
wbeaumont 0:2afae79ea9ca 31
wbeaumont 0:2afae79ea9ca 32 part[0]=input[0];part[1]=input[1];part[2]=0;sc->module = (u8)strtol(part, NULL, 16);
wbeaumont 2:1f9887337a1b 33 if( input[3] != SEP ) rv =-1; // tbd check for module range
wbeaumont 0:2afae79ea9ca 34 else {
wbeaumont 5:357fa8928d63 35 u16 ic;//input counter
wbeaumont 0:2afae79ea9ca 36 part[0]=input[4];part[1]=input[5];part[2]=0;sc->con = (u8)strtol(part, NULL, 16);
wbeaumont 2:1f9887337a1b 37 if( input[6] != SEP ) rv= -2; // tbd check for connector range
wbeaumont 0:2afae79ea9ca 38 else {
wbeaumont 0:2afae79ea9ca 39 part[0]=input[7];part[1]=input[8];part[2]=0;sc->dev = (ssc_dev)strtol(part, NULL, 16);
wbeaumont 0:2afae79ea9ca 40 if( input[9] != SEP ) rv= -3;
wbeaumont 2:1f9887337a1b 41 else { ic=10;
wbeaumont 2:1f9887337a1b 42 part[0]=input[ic++];part[1]=input[ic++];part[2]=0;sc->devnr = (u8)strtol(part, NULL, 16);
wbeaumont 2:1f9887337a1b 43 if( input[ic++] != SEP ) rv= -9;
wbeaumont 2:1f9887337a1b 44
wbeaumont 2:1f9887337a1b 45 else {
wbeaumont 3:e9053e5e7175 46 sc->cmd[0]=input[ic++];sc->cmd[1]=input[ic++];sc->cmd[2]=input[ic++];sc->cmd[3]=input[ic++];sc->cmd[4]='\0';
wbeaumont 2:1f9887337a1b 47 if( input[ic++] != SEP ) rv= -4;
wbeaumont 2:1f9887337a1b 48 else {
wbeaumont 2:1f9887337a1b 49 part[0]=input[ic++];part[1]=input[ic++];part[2]=0;sc->ch = (u8)strtol(part, NULL, 16);
wbeaumont 2:1f9887337a1b 50 if( input[ic++] != SEP ) rv= -5;
wbeaumont 0:2afae79ea9ca 51 else{
wbeaumont 5:357fa8928d63 52 part[0]=input[ic++];part[1]=input[ic++];part[2]=input[ic++];part[3]=input[ic++];part[4]=0;sc->datain = (u16)strtol(part, NULL, 16);
wbeaumont 2:1f9887337a1b 53 if( input[ic++] != SEP ) rv= -6;
wbeaumont 0:2afae79ea9ca 54 else {
wbeaumont 5:357fa8928d63 55 part[0]=input[ic++];part[1]=input[ic++];part[2]=input[ic++];part[3]=input[ic++];part[4]=0;sc->dataout = (u16)strtol(part, NULL, 16);
wbeaumont 2:1f9887337a1b 56 if( input[ic++] != SEP ) rv= -7;
wbeaumont 4:66d1a4200f67 57 else {
wbeaumont 10:0675c2e3d330 58 part[0]=input[ic++];part[1]=input[ic++];part[3]=0;sc->status = (u8)strtol(part, NULL, 16);
wbeaumont 4:66d1a4200f67 59 if( input[ic++] != SEP ) rv= -8;
wbeaumont 0:2afae79ea9ca 60 else rv=0;
wbeaumont 0:2afae79ea9ca 61
wbeaumont 4:66d1a4200f67 62 } } } } }} } }
wbeaumont 9:4bfcfa029dff 63 if ( rv ==0) {
wbeaumont 9:4bfcfa029dff 64 int crr=check_ranges( sc);
wbeaumont 9:4bfcfa029dff 65 if ( crr < 0 ) rv=crr ;
wbeaumont 9:4bfcfa029dff 66 }
wbeaumont 9:4bfcfa029dff 67 // clean input string
wbeaumont 9:4bfcfa029dff 68 for( int i=0;i<27;i++) input[i]=0;
wbeaumont 0:2afae79ea9ca 69
wbeaumont 0:2afae79ea9ca 70 return rv;
wbeaumont 0:2afae79ea9ca 71 }
wbeaumont 0:2afae79ea9ca 72
wbeaumont 2:1f9887337a1b 73 int sscm_comm::check_ranges( ssc_cmd* sc ){
wbeaumont 2:1f9887337a1b 74
wbeaumont 2:1f9887337a1b 75 int rv =1; // no check done
wbeaumont 2:1f9887337a1b 76 // devnr check
wbeaumont 9:4bfcfa029dff 77 if ( sc->con <1 || sc->con >2 ){ rv= -0x10; goto RVOUT;}
wbeaumont 2:1f9887337a1b 78 switch ( sc->dev) {
wbeaumont 9:4bfcfa029dff 79 case ADC : if(sc->devnr < 1 || sc->devnr > 2 ){ rv=-0x11; break; }
wbeaumont 6:d9a96735d0fb 80 break;
wbeaumont 2:1f9887337a1b 81
wbeaumont 10:0675c2e3d330 82 case TEMP : if( sc->devnr <1 || sc->devnr >3) {rv =-0x12 ;break;}
wbeaumont 6:d9a96735d0fb 83 break;
wbeaumont 6:d9a96735d0fb 84 default : rv=1;
wbeaumont 2:1f9887337a1b 85 }
wbeaumont 3:e9053e5e7175 86 RVOUT: return rv;
wbeaumont 2:1f9887337a1b 87
wbeaumont 2:1f9887337a1b 88 }
wbeaumont 2:1f9887337a1b 89
wbeaumont 0:2afae79ea9ca 90 void sscm_comm::encode_cmd ( char *output , ssc_cmd *sc) {
wbeaumont 4:66d1a4200f67 91 sprintf(output,"%c%02X%c%02X%c%02X%c%02X%c%c%c%c%c%c%02X%c%04X%c%04X%c%02X%c%c\0",
wbeaumont 2:1f9887337a1b 92 CMDSTART,sc->module,SEP,sc->con,SEP,sc->dev,SEP,sc->devnr,SEP,sc->cmd[0],sc->cmd[1],sc->cmd[2],sc->cmd[3],
wbeaumont 4:66d1a4200f67 93 SEP,sc->ch,SEP,sc->datain,SEP,sc->dataout,SEP,sc->status,SEP,CMDSTOP);
wbeaumont 0:2afae79ea9ca 94 }
wbeaumont 0:2afae79ea9ca 95
wbeaumont 0:2afae79ea9ca 96
wbeaumont 0:2afae79ea9ca 97 void sscm_comm::getmodulecordinate(u8 plane, u8& modulenr, u8& connr ,u8& serialnr ){
wbeaumont 1:288db0531b1f 98 // to be implemented
wbeaumont 1:288db0531b1f 99 modulenr=0;
wbeaumont 6:d9a96735d0fb 100 }
wbeaumont 6:d9a96735d0fb 101
wbeaumont 0:2afae79ea9ca 102
wbeaumont 6:d9a96735d0fb 103
wbeaumont 6:d9a96735d0fb 104 void sscm_comm::get_mppc_dac_chnr(u8 plane, u8 x , u8 y, u8& ch,u8& modulenr, u8& connr ,u8&dacch, u8& serialnr ) {};
wbeaumont 6:d9a96735d0fb 105 void sscm_comm::get_mppc_adc_chnr(u8 plane, u8 x , u8 y, u8& ch,u8& modulenr, u8& connr , u8& adcdevnr, u8& adcchnr , u8& serialnr ){};
wbeaumont 7:6d3c421026df 106
wbeaumont 10:0675c2e3d330 107 void sscm_comm::float2_2u16( float in, u16& msb, u16& lsb){
wbeaumont 10:0675c2e3d330 108 float* inp=&in;
wbeaumont 10:0675c2e3d330 109 u16* pi=(u16*)inp;
wbeaumont 10:0675c2e3d330 110 msb=*pi; pi++;
wbeaumont 10:0675c2e3d330 111 lsb=*pi;
wbeaumont 10:0675c2e3d330 112 }
wbeaumont 7:6d3c421026df 113
wbeaumont 7:6d3c421026df 114
wbeaumont 10:0675c2e3d330 115 float sscm_comm::u162float( u16 msb, u16 lsb){
wbeaumont 10:0675c2e3d330 116 float rv=-288;
wbeaumont 10:0675c2e3d330 117 float* fp=&rv;
wbeaumont 10:0675c2e3d330 118 u16* rp=(u16*)fp ;
wbeaumont 10:0675c2e3d330 119 *rp=msb; rp++;
wbeaumont 10:0675c2e3d330 120 *rp=lsb;
wbeaumont 10:0675c2e3d330 121 return rv;
wbeaumont 10:0675c2e3d330 122 }
wbeaumont 10:0675c2e3d330 123
wbeaumont 10:0675c2e3d330 124
wbeaumont 10:0675c2e3d330 125
wbeaumont 7:6d3c421026df 126 sscm_comm::getsscmVersion::getsscmVersion():getVersion( SSCM_COMM_LIB_HDR_VERSION,SSCM_COMM_LIB_SRC_VERSION, __TIME__, __DATE__){};