# HG changeset patch # User Daniele Nicolodi # Date 1351518494 -3600 # Node ID 64a7a1d3d75ccd2dcd08bc2a741353839bdc0613 # Parent 0159abc1a9d8e65b81d4ab7eb84911b3028e0912 Cleanup AD9956 DDS driver diff -r 0159abc1a9d8 -r 64a7a1d3d75c DDS_Fox.c --- a/DDS_Fox.c Mon Oct 29 14:48:08 2012 +0100 +++ b/DDS_Fox.c Mon Oct 29 14:48:14 2012 +0100 @@ -2,10 +2,7 @@ #include #include "DDS_Fox.h" -//#define DDS_IP "192.168.0.3" -//#define TCP_PORT 6665 - -#define DIV 0 // divided by 1 +// #define DEBUG 1 #define CMD_SEP ";" #define BASE_TIME 0.01 @@ -25,22 +22,37 @@ #define CMD_GET_FREQ 28 #define CMD_SWEEP_SIGN 29 - -/************************* Global variables ***********************************/ -//static char Request[255]; +typedef struct { + char* action; + char* canal; + char* command; + char* value0; + char* value1; + char* value2; + char* value3; + char* value4; + char* value5; + char* value6; + char* value7; + char* value8; + char* value9; + char* value10; + char* value11; + char* value12; + char* value13; + char* value14; + char* value15; + char* value16; + char* value17; + char* value18; + char* value19; +} Command; -/************************* My own functions ***********************************/ -/*DDSParameter DDSGetParam (int panel, int control) { - DDSParameter Parameter ; - - GetCtrlVal (panel, CONTROL_DDS_IP, Parameter->ip) ; - GetCtrlVal (panel, CONTROL_DDS_PORT, &(Parameter->Port)) ; - GetCtrlVal (panel, CONTROL_DDS_CLOCK, &(Parameter->Clock)) ; - GetCtrlVal (panel, CONTROL_DDS_RATE, &(Parameter->SweepRate)) ; - GetCtrlVal (panel, CONTROL_DDS_DELTA_T, &(Parameter->Delta_T)) ; - return(Parameter); -}*/ - +static int SendCmd(char *Buffer, DDSParameter *Param); +static int RecvCmd(Command *Rd, char * Buffer, DDSParameter *Param); +static void init_command(Command *cmd); +static int parse_command(Command *cmd1, char *buf); +static void convert_double_6char (double fraction, unsigned char* octets); #define error(...) _error(__FILE__, __LINE__, __func__, __VA_ARGS__) @@ -54,90 +66,109 @@ va_end(args); } - -void DDS_Initialize (DDSParameter *Param) { - char Request[255]; - //send NOP command - sprintf(Request,"set;%i;%i",1,CMD_NOP); - SendCmd(Request, Param) ; - //reset the DDS - DDSFox_Reset(Param); - - //Set Profile Number 0 (only one used for soft frequecy sweep) -// sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,0); -// SendCmd(Request, Param) ; +void DDSFox_Initialize(DDSParameter *Param, char *ip, int port, double frequency) +{ + strcpy(Param->ip, ip); + Param->Port = 6665; + Param->Profil = 7; // configuration profile: single freq=0, sweep=7 + Param->Clock = 200000000; + Param->Delta_T = 0.01; + Param->SweepRate = 0.0; - //Set Profile Number 7 (only one used for soft frequecy sweep) - sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,7); - SendCmd(Request, Param) ; - + // reset the DDS + //DDSFox_Nop(Param); + DDSFox_Reset(Param); + // set profile number 7 soft frequecy sweep + DDSFox_SetProfile(Param); + // set clock divider to 1 + DDSFox_SetDiv(Param, 1); + // set frequency + DDSFox_SetFreqInf(Param, frequency); + // set frequency upper bound to max value + DDSFox_SetFreqMax(Param); + // set sweep rate to 0 + DDSFox_SetDT(Param); + DDSFox_SetDf(Param); + DDSFox_SetSweepSign(Param); + // start sweep + DDSFox_StartSweep(Param); + // set frequency lower bound to min value + DDSFox_SetFreqMin(Param); + + return; +} - //Set Clock Divider to 00b (which means 1) - DDSFox_SetDiv(Param); - //Set sweep rate at 0 - DDSFox_SetFreqMax (Param); - DDSFox_SetFreq (Param) ; - DDSFox_SetDT (Param) ; - DDSFox_SetDf (Param) ; - DDSFox_StartSweep (Param) ; - return ; +void DDSFox_SetSweepRate(DDSParameter *Param, double sweepRate) +{ + Param->SweepRate = sweepRate; + DDSFox_SetDf(Param); + DDSFox_SetSweepSign(Param); } -void DDS_ReInitialize (DDSParameter *Param) { - char Request[255]; - //send NOP command - sprintf(Request,"set;%i;%i",1,CMD_NOP); - SendCmd(Request, Param) ; - //reset the DDS - //DDSFox_Reset(Param); - - //Set Profile Number 0 (only one used for soft frequecy sweep) -// sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,0); -// SendCmd(Request, Param) ; - - //Set Profile Number 7 (only one used for soft frequecy sweep) - sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,7); - SendCmd(Request, Param) ; - - - //Set Clock Divider to 00b (which means 1) - DDSFox_SetDiv(Param); - //Set sweep rate at 0 - DDSFox_SetFreqMax (Param); - DDSFox_SetFreq (Param) ; - DDSFox_SetDT (Param) ; - DDSFox_SetDf (Param) ; - DDSFox_StartSweep (Param) ; - return ; +void DDSFox_Set(DDSParameter *Param, double frequency, double sweepRate) +{ + // set desired frequency + DDSFox_SetFreqInf(Param, frequency); + // set frequency upper bound to max value + DDSFox_SetFreqMax(Param); + // set sweep rate to 0 + Param->SweepRate = 0.0; + DDSFox_SetDT(Param); + DDSFox_SetDf(Param); + DDSFox_SetSweepSign(Param); + // start sweep + DDSFox_StartSweep(Param); + // set frequency lower bound to min value + DDSFox_SetFreqMin(Param); + // set desired sweep rate + Param->SweepRate = sweepRate; + DDSFox_SetDf(Param); + DDSFox_SetSweepSign(Param); } +void DDSFox_Nop (DDSParameter *Param) +{ + // send NOP command + char request[255]; + sprintf(request, "set;%i;%i", 1, CMD_NOP); + SendCmd(request, Param); +} -void DDSFox_Reset(DDSParameter *Param) { +void DDSFox_SetProfile (DDSParameter *Param) +{ + char request[255]; + sprintf(request, "set;%i;%i;%i", 1, CMD_PROFILE, Param->Profil); + SendCmd(request, Param); +} + +void DDSFox_Reset(DDSParameter *Param) +{ char Request[255]; - sprintf(Request,"set;%i;%i",1,CMD_NOP); - SendCmd(Request, Param) ; + //sprintf(Request,"set;%i;%i",1,CMD_NOP); + //SendCmd(Request, Param) ; sprintf(Request,"set;%i;%i",1,CMD_RESET_DDS); SendCmd(Request, Param) ; return ; } - -void DDSFox_StopSweep(DDSParameter *Param) { +void DDSFox_StopSweep(DDSParameter *Param) +{ char Request[255]; sprintf(Request,"set;%i;%i;%i",1,CMD_STOP_SWEEP,1); SendCmd(Request, Param); return ; } - -void DDSFox_StartSweep(DDSParameter *Param) { +void DDSFox_StartSweep(DDSParameter *Param) +{ char Request[255]; sprintf(Request,"set;%i;%i;%i",1,CMD_START_SWEEP,1); SendCmd(Request, Param); return ; } -void DDSFox_SetFreqMax (DDSParameter *Param) { +void DDSFox_SetFreqMax (DDSParameter *Param) +{ unsigned char octets[6]; char Request[255]; convert_double_6char(pow(2,48)/2,octets); // borne sup : clock / 2 @@ -148,7 +179,8 @@ return ; } -void DDSFox_SetFreqMin (DDSParameter *Param) { +void DDSFox_SetFreqMin (DDSParameter *Param) +{ unsigned char octets[6]; char Request[255]; convert_double_6char(1,octets); // borne inf : ~0 @@ -159,66 +191,49 @@ return ; } -void DDSFox_SetFreq (DDSParameter *Param) { +void DDSFox_SetFreqInf (DDSParameter *Param, double frequency) +{ + double word ; + unsigned char octets[6]; + char Request[255]; + word = pow(2,48)*(frequency / Param->Clock); + convert_double_6char(word, octets); + sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF, + *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); + SendCmd(Request, Param); +} + +void DDSFox_SetFreqSup (DDSParameter *Param, double frequency) +{ double word ; unsigned char octets[6]; - int SweepSign = -1 ; - char Request[255]; - if (Param->Profil == 0) { - word = pow(2,48)*(Param->Frequency/Param->Clock); - convert_double_6char(word, octets); // borne inf : la freq voulu - sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF, - *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); - SendCmd(Request, Param); - } - else if(Param->Profil == 7) { - SweepSign = DDSFox_GetSweepSign(Param) ; - if(SweepSign == 0){ // sweep positif - word = pow(2,48)*(Param->Frequency/Param->Clock); - convert_double_6char(word, octets); // borne inf : la freq voulu - sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF, - *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); - SendCmd(Request, Param); - DDSFox_SetFreqMax (Param); - } - else { // sweep neg - word = pow(2,48)*(Param->Frequency/Param->Clock); - convert_double_6char(word, octets); // borne sup : la freq voulue - sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_SUP, - *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); - SendCmd(Request, Param); - DDSFox_SetFreqMin (Param); - } - } - return ; + char Request[255]; + word = pow(2,48)*(frequency / Param->Clock); + convert_double_6char(word, octets); + sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_SUP, + *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); + SendCmd(Request, Param); } +static int GetSweepSign(DDSParameter *Param) +{ + if (Param->SweepRate >= 0.0) + return 0; + return 1; +} -// Change le signe de sweep en frequence. Argument: struct de param de la DDS -void DDSFox_SetSweepSign(DDSParameter *Param) { - char Request[255] ; +void DDSFox_SetSweepSign(DDSParameter *Param) +{ + char Request[255] ; int SweepSign ; - SweepSign = DDSFox_GetSweepSign(Param) ; - sprintf(Request,"set;%i;%i;%i",1,CMD_SWEEP_SIGN, SweepSign) ; + SweepSign = GetSweepSign(Param) ; + sprintf(Request, "set;%i;%i;%i", 1, CMD_SWEEP_SIGN, SweepSign); SendCmd(Request, Param) ; return ; } -/*void DDSFox_SetSweepSign(DDSParameter *Param) { - char Request[255] ; - int SweepSign ; - if (Param->Profil == 7) - Param->Frequency = DDSFox_ReadFreq(Param) ; - DDSFox_SetFreq (Param) ; - SweepSign = DDSFox_GetSweepSign(Param) ; - sprintf(Request,"set;%i;%i;%i",1,CMD_SWEEP_SIGN, SweepSign) ; - SendCmd(Request, Param) ; - return ; -}*/ - - -// Change la valeur de la base pour 'lincrement de frequence (sweep). Argument: delta T x 10ms, retourne rien. -void DDSFox_SetDT(DDSParameter *Param) { +void DDSFox_SetDT(DDSParameter *Param) +{ char Request[255]; int dt= 0; char FTW0[255]="0"; /**** sinon le fichier inf.xx est faux ***/ @@ -229,13 +244,18 @@ return ; } - // Change le facteur de division de la clock. Argument: facteur de div, retourne rien -void DDSFox_SetDiv(DDSParameter *Param) { - char Request[255]; - int Div = DIV ; +void DDSFox_SetDiv(DDSParameter *Param, int div) +{ + char Request[255]; Command Rd; - char temp; + char temp; + + int Div = 0; + // Div = log2(div) + while ((div >>= 1) > 1) + Div++; + init_command(&Rd); sprintf(Request,"get;%i;%i",1,CMD_CONFIG_REG2); if (RecvCmd(&Rd, Request, Param) < 0) @@ -249,9 +269,8 @@ return ; } - - -void DDSFox_SetDf(DDSParameter *Param) { +void DDSFox_SetDf(DDSParameter *Param) +{ char Request[255]; double word ; unsigned char *octets; @@ -265,13 +284,8 @@ return ; } -int DDSFox_GetSweepSign(DDSParameter *Param) { - if (Param->SweepRate >= 0) return(0); - else return(1); -} - - -double DDSFox_ReadFreq(DDSParameter *Param) { +double DDSFox_ReadFreq(DDSParameter *Param) +{ char Request[255]; unsigned int OctetA, OctetB, OctetC, OctetD, OctetE, OctetF ; double FreqRead ; @@ -291,27 +305,38 @@ return(FreqRead); } +// TCP communication -/************************ Fonction de communications TCP ******************/ -static const double timeout = 0.5; +static unsigned int timeout = 0; // milliseconds +int OnTCPEvent(unsigned handle, int xType, int errCode, void *callbackData) +{ + return 0; +} -int SendCmd(char *Buffer, DDSParameter *Param) { +static int SendCmd(char *Buffer, DDSParameter *Param) +{ unsigned int hConv=0; char Response[255]; - if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, 0) < 0) { +#ifdef DEBUG + fprintf(stderr, "> %s\n", Buffer); +#endif + if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, timeout) < 0) { error("Connect failed"); return -1; } - if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, 0) < 0) { + if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, timeout) < 0) { error("Write failed"); return -1; } sprintf(Response," "); - if (ClientTCPRead (hConv, Response, 255, 0) < 0) { + if (ClientTCPRead (hConv, Response, 255, timeout) < 0) { error("Read failed"); return -1; } +#ifdef DEBUG + fprintf(stderr, "< %s\n", Response); +#endif if (strncmp("OK",Response,2)) { error("Response failed %s %s", Buffer, Response); return -1; @@ -319,25 +344,30 @@ return 0; } - -int RecvCmd(Command *Rd, char * Buffer, DDSParameter *Param) { +static int RecvCmd(Command *Rd, char * Buffer, DDSParameter *Param) +{ unsigned int hConv=0; char parsebuf[255]; char Response[255]; - if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, 0) < 0) { +#ifdef DEBUG + fprintf(stderr, "> %s\n", Buffer); +#endif + if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, timeout) < 0) { error("Connect failed"); return -1; } - if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, 0) < 0) { + if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, timeout) < 0) { error("Write failed"); return -1; } sprintf(Response," "); - if (ClientTCPRead (hConv, Response, 255, 0) < 0) { + if (ClientTCPRead (hConv, Response, 255, timeout) < 0) { error("Read failed"); return -1; } - +#ifdef DEBUG + fprintf(stderr, "< %s\n", Response); +#endif if (strncmp(Buffer,Response,strlen(Buffer))) { error("Response failed %s %s", Buffer, Response); return -1; @@ -348,50 +378,18 @@ return 0; } - -int OnTCPEvent(unsigned handle, int xType, int errCode, void *callbackData) { - //char* Response = (char*)(callbackData); - //char Response[255]; - //switch(xType) { - // case TCP_DISCONNECT : - //printf("Server disconnected\n"); - // break; - // case TCP_DATAREADY : - //printf("Server data ready\n"); - //ClientTCPRead (handle, Response, 255, 5); - // break; - //} - return 0; +static void init_command(Command *cmd) +{ + memset(cmd, 0, sizeof(Command)); } -/************************ Fonction de mise en forme des données pour le transfert ******************/ - -void parse_command2(int* cmd, char* buf) +static int parse_command(Command *cmd1, char *buf) { char* pointer; int c = 0; pointer = strtok( buf, CMD_SEP ); while( pointer ){ - if ((c > 2) && ( c < 19 )) - { - cmd[c-3] = atoi(pointer); - printf("%i", cmd[c-3]); - } - - - pointer = strtok( NULL, CMD_SEP ); - c++; - } -} - - -int parse_command(Command *cmd1, char *buf){ - char* pointer; - int c = 0; - - pointer = strtok( buf, CMD_SEP ); - while( pointer ){ if( c == 0 ){ cmd1->action = pointer; } @@ -431,49 +429,11 @@ return 0; } - -int init_command(Command *cmd1){ - cmd1->action = NULL; - cmd1->canal = NULL; - cmd1->command = NULL; - cmd1->value0 = NULL; - cmd1->value1 = NULL; - cmd1->value2 = NULL; - cmd1->value3 = NULL; - cmd1->value4 = NULL; - cmd1->value5 = NULL; - cmd1->value6 = NULL; - cmd1->value7 = NULL; - cmd1->value8 = NULL; - cmd1->value9 = NULL; - cmd1->value10 = NULL; - cmd1->value11 = NULL; - cmd1->value12 = NULL; - cmd1->value13 = NULL; - cmd1->value14 = NULL; - cmd1->value15 = NULL; - cmd1->value16 = NULL; - cmd1->value17 = NULL; - cmd1->value18 = NULL; - cmd1->value19 = NULL; - return 0; +// convert double into 6 bytes sent back by an unsigned char pointer +static void convert_double_6char (double fraction, unsigned char* octets) +{ + // 64 bits integer + unsigned long long x = fraction; + for (int i = 0; i < 6; i++) + octets[5 - i] = (unsigned char)((x >> (i * 8)) & 0xFF); } - - -//Convert double into 6 bytes sent back by an unsigned char pointer -int convert_double_6char (double fraction, unsigned char* octets) { - double bita, bitb, bitc, bitd, bite, bitf; - bitf= fraction/(1099511627776.0); - *(octets+5)=(unsigned char)bitf; - bite= (fraction-1099511627776.0*(*(octets+5)))/(4294967296.0); - *(octets+4)=(unsigned char)bite; - bitd=(fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4)))/( 16777216.0); - *(octets+3)=(unsigned char)bitd; - bitc=(fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4))-16777216.0*(*(octets+3)))/( 65536.0); - *(octets+2)=(unsigned char)bitc; - bitb= (fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4))-16777216.0*(*(octets+3))-65536*(*(octets+2)))/ 256.0; - *(octets+1)=(unsigned char)bitb; - bita= (fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4))-16777216.0*(*(octets+3))-65536*(*(octets+2))-256.0*(*(octets+1)))/ 1.0; - *(octets)=(unsigned char)bita; - return 0; -} diff -r 0159abc1a9d8 -r 64a7a1d3d75c DDS_Fox.h --- a/DDS_Fox.h Mon Oct 29 14:48:08 2012 +0100 +++ b/DDS_Fox.h Mon Oct 29 14:48:14 2012 +0100 @@ -4,52 +4,23 @@ int Port; //int Divider; double Clock; - double Frequency; double SweepRate; double Delta_T; - unsigned int Profil; // numéro de profil de la DDS : single freq=0, sweep=7 -}DDSParameter; - - - - - + unsigned int Profil; +} DDSParameter; -typedef struct { - char* action; - char* canal; - char* command; - char* value0; - char* value1; - char* value2; - char* value3; - char* value4; - char* value5; - char* value6; - char* value7; - char* value8; - char* value9; - char* value10; - char* value11; - char* value12; - char* value13; - char* value14; - char* value15; - char* value16; - char* value17; - char* value18; - char* value19; - -}Command; - - -void DDS_Initialize (DDSParameter * Param) ; +void DDSFox_Initialize(DDSParameter *Param, char *ip, int port, double frequency); +void DDSFox_SetSweepRate(DDSParameter *Param, double sweepRate); +void DDSFox_Set(DDSParameter *Param, double frequency, double sweepRate); +void DDSFox_Nop(DDSParameter *Param); +void DDSFox_SetProfile(DDSParameter *Param); +void DDSFox_SetDiv(DDSParameter *Param, int div); void DDSFox_Reset(DDSParameter * Param); void DDSFox_SetFreq (DDSParameter * Param) ; +void DDSFox_SetFreqInf (DDSParameter *Param, double frequency) ; +void DDSFox_SetFreqSup (DDSParameter *Param, double frequency) ; void DDSFox_SetFreqMax (DDSParameter *Param); void DDSFox_SetFreqMin (DDSParameter *Param) ; -//void DDSFox_SetRate (DDSParameter * Param) ; -void DDSFox_SetDiv(DDSParameter * Param) ; void DDSFox_SetDT(DDSParameter * Param); void DDSFox_SetDf(DDSParameter * Param); int DDSFox_GetSweepSign(DDSParameter * Param); @@ -57,13 +28,4 @@ void DDSFox_StopSweep(DDSParameter * Param); void DDSFox_StartSweep(DDSParameter * Param); double DDSFox_ReadFreq(DDSParameter * Param); -int SendCmd(char * Buffer, DDSParameter * Param) ; -int RecvCmd2(int * cmd, char * Buffer, DDSParameter * Param) ; -int RecvCmd(Command * Rd, char * Buffer, DDSParameter * Param) ; -int OnTCPEvent(unsigned handle, int xType, int errCode, void *callbackData) ; -void parse_command2(int * cmd, char * buf ) ; -int parse_command(Command * cmd1, char * command ) ; -int convert_double_6char (double fraction, unsigned char* octects) ; -int init_command(Command * cmd1) ; -void DDS_ReInitialize (DDSParameter *Param);