Mercurial > hg > fxanalyse
comparison DDS_Fox.c @ 0:d9aae7d7f2c6
Import
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Tue, 03 Jul 2012 10:38:59 +0200 |
parents | |
children | adc9e2a67547 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d9aae7d7f2c6 |
---|---|
1 #include "DDS Fox control.h" | |
2 #include <userint.h> | |
3 #include <tcpsupp.h> | |
4 #include <ansi_c.h> | |
5 #include "DDS_Fox.h" | |
6 | |
7 //#define DDS_IP "192.168.0.3" | |
8 //#define TCP_PORT 6665 | |
9 | |
10 #define DIV 0 // divided by 1 | |
11 #define CMD_SEP ";" | |
12 #define BASE_TIME 0.01 | |
13 | |
14 #define CMD_CONFIG_REG1 1 // cmd d'acces direct au rergistre 1 de config de la DDS (voir datasheet) | |
15 #define CMD_CONFIG_REG2 2 // cmd d'acces direct au rergistre 1 de config de la DDS (voir datasheet) | |
16 #define CMD_SINGLE_FREQ_PROF0 7 | |
17 #define CMD_PROFILE 15 // cmd de choix du profile de fonctionnement de la DDS (voir datasheet) | |
18 #define CMD_ATTENUATION 16 | |
19 #define CMD_RESET_DDS 17 | |
20 #define CMD_NOP 20 | |
21 #define CMD_FREQ_INF 21 | |
22 #define CMD_FREQ_SUP 22 | |
23 #define CMD_DF 23 | |
24 #define CMD_DT 24 | |
25 #define CMD_START_SWEEP 25 | |
26 #define CMD_STOP_SWEEP 26 | |
27 #define CMD_GET_FREQ 28 | |
28 #define CMD_SWEEP_SIGN 29 | |
29 | |
30 | |
31 /************************* Global variables ***********************************/ | |
32 //static char Request[255]; | |
33 | |
34 /************************* My own functions ***********************************/ | |
35 /*DDSParameter DDSGetParam (int panel, int control) { | |
36 DDSParameter Parameter ; | |
37 | |
38 GetCtrlVal (panel, CONTROL_DDS_IP, Parameter->ip) ; | |
39 GetCtrlVal (panel, CONTROL_DDS_PORT, &(Parameter->Port)) ; | |
40 GetCtrlVal (panel, CONTROL_DDS_CLOCK, &(Parameter->Clock)) ; | |
41 GetCtrlVal (panel, CONTROL_DDS_RATE, &(Parameter->SweepRate)) ; | |
42 GetCtrlVal (panel, CONTROL_DDS_DELTA_T, &(Parameter->Delta_T)) ; | |
43 return(Parameter); | |
44 }*/ | |
45 | |
46 | |
47 | |
48 void DDS_Initialize (DDSParameter *Param) { | |
49 char Request[255]; | |
50 //send NOP command | |
51 sprintf(Request,"set;%i;%i",1,CMD_NOP); | |
52 SendCmd(Request, Param) ; | |
53 //reset the DDS | |
54 DDSFox_Reset(Param); | |
55 | |
56 //Set Profile Number 0 (only one used for soft frequecy sweep) | |
57 // sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,0); | |
58 // SendCmd(Request, Param) ; | |
59 | |
60 //Set Profile Number 7 (only one used for soft frequecy sweep) | |
61 sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,7); | |
62 SendCmd(Request, Param) ; | |
63 | |
64 | |
65 //Set Clock Divider to 00b (which means 1) | |
66 DDSFox_SetDiv(Param); | |
67 //Set sweep rate at 0 | |
68 DDSFox_SetFreqMax (Param); | |
69 DDSFox_SetFreq (Param) ; | |
70 DDSFox_SetDT (Param) ; | |
71 DDSFox_SetDf (Param) ; | |
72 DDSFox_StartSweep (Param) ; | |
73 return ; | |
74 } | |
75 | |
76 void DDS_ReInitialize (DDSParameter *Param) { | |
77 char Request[255]; | |
78 //send NOP command | |
79 sprintf(Request,"set;%i;%i",1,CMD_NOP); | |
80 SendCmd(Request, Param) ; | |
81 //reset the DDS | |
82 //DDSFox_Reset(Param); | |
83 | |
84 //Set Profile Number 0 (only one used for soft frequecy sweep) | |
85 // sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,0); | |
86 // SendCmd(Request, Param) ; | |
87 | |
88 //Set Profile Number 7 (only one used for soft frequecy sweep) | |
89 sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,7); | |
90 SendCmd(Request, Param) ; | |
91 | |
92 | |
93 //Set Clock Divider to 00b (which means 1) | |
94 DDSFox_SetDiv(Param); | |
95 //Set sweep rate at 0 | |
96 DDSFox_SetFreqMax (Param); | |
97 DDSFox_SetFreq (Param) ; | |
98 DDSFox_SetDT (Param) ; | |
99 DDSFox_SetDf (Param) ; | |
100 DDSFox_StartSweep (Param) ; | |
101 return ; | |
102 } | |
103 | |
104 | |
105 void DDSFox_Reset(DDSParameter *Param) { | |
106 char Request[255]; | |
107 sprintf(Request,"set;%i;%i",1,CMD_NOP); | |
108 SendCmd(Request, Param) ; | |
109 sprintf(Request,"set;%i;%i",1,CMD_RESET_DDS); | |
110 SendCmd(Request, Param) ; | |
111 return ; | |
112 } | |
113 | |
114 | |
115 void DDSFox_StopSweep(DDSParameter *Param) { | |
116 char Request[255]; | |
117 sprintf(Request,"set;%i;%i;%i",1,CMD_STOP_SWEEP,1); | |
118 SendCmd(Request, Param); | |
119 return ; | |
120 } | |
121 | |
122 | |
123 void DDSFox_StartSweep(DDSParameter *Param) { | |
124 char Request[255]; | |
125 sprintf(Request,"set;%i;%i;%i",1,CMD_START_SWEEP,1); | |
126 SendCmd(Request, Param); | |
127 return ; | |
128 } | |
129 | |
130 void DDSFox_SetFreqMax (DDSParameter *Param) { | |
131 double word ; | |
132 unsigned char octets[6]; | |
133 char Request[255]; | |
134 convert_double_6char(pow(2,48)/2,octets); // borne sup : clock / 2 | |
135 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_SUP, | |
136 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); | |
137 SendCmd(Request, Param); | |
138 DDSFox_SetSweepSign (Param); | |
139 return ; | |
140 } | |
141 | |
142 void DDSFox_SetFreqMin (DDSParameter *Param) { | |
143 double word ; | |
144 unsigned char octets[6]; | |
145 char Request[255]; | |
146 convert_double_6char(1,octets); // borne inf : ~0 | |
147 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF, | |
148 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); | |
149 SendCmd(Request, Param); | |
150 DDSFox_SetSweepSign (Param); | |
151 return ; | |
152 } | |
153 | |
154 void DDSFox_SetFreq (DDSParameter *Param) { | |
155 double word ; | |
156 unsigned char octets[6]; | |
157 int SweepSign = -1 ; | |
158 char Request[255]; | |
159 if (Param->Profil == 0) { | |
160 word = pow(2,48)*(Param->Frequency/Param->Clock); | |
161 convert_double_6char(word, octets); // borne inf : la freq voulu | |
162 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF, | |
163 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); | |
164 SendCmd(Request, Param); | |
165 } | |
166 else if(Param->Profil == 7) { | |
167 SweepSign = DDSFox_GetSweepSign(Param) ; | |
168 if(SweepSign == 0){ // sweep positif | |
169 word = pow(2,48)*(Param->Frequency/Param->Clock); | |
170 convert_double_6char(word, octets); // borne inf : la freq voulu | |
171 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF, | |
172 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); | |
173 SendCmd(Request, Param); | |
174 DDSFox_SetFreqMax (Param); | |
175 } | |
176 else { // sweep neg | |
177 word = pow(2,48)*(Param->Frequency/Param->Clock); | |
178 convert_double_6char(word, octets); // borne sup : la freq voulue | |
179 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_SUP, | |
180 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); | |
181 SendCmd(Request, Param); | |
182 DDSFox_SetFreqMin (Param); | |
183 } | |
184 } | |
185 return ; | |
186 } | |
187 | |
188 | |
189 // Change le signe de sweep en frequence. Argument: struct de param de la DDS | |
190 void DDSFox_SetSweepSign(DDSParameter *Param) { | |
191 char Request[255] ; | |
192 int SweepSign ; | |
193 SweepSign = DDSFox_GetSweepSign(Param) ; | |
194 sprintf(Request,"set;%i;%i;%i",1,CMD_SWEEP_SIGN, SweepSign) ; | |
195 SendCmd(Request, Param) ; | |
196 return ; | |
197 } | |
198 | |
199 /*void DDSFox_SetSweepSign(DDSParameter *Param) { | |
200 char Request[255] ; | |
201 int SweepSign ; | |
202 if (Param->Profil == 7) | |
203 Param->Frequency = DDSFox_ReadFreq(Param) ; | |
204 DDSFox_SetFreq (Param) ; | |
205 SweepSign = DDSFox_GetSweepSign(Param) ; | |
206 sprintf(Request,"set;%i;%i;%i",1,CMD_SWEEP_SIGN, SweepSign) ; | |
207 SendCmd(Request, Param) ; | |
208 return ; | |
209 }*/ | |
210 | |
211 | |
212 // Change la valeur de la base pour 'lincrement de frequence (sweep). Argument: delta T x 10ms, retourne rien. | |
213 void DDSFox_SetDT(DDSParameter *Param) { | |
214 char Request[255]; | |
215 int dt= 0; | |
216 char FTW0[255]="0"; /**** sinon le fichier inf.xx est faux ***/ | |
217 dt = ((Param->Delta_T)/BASE_TIME) - 1 ; | |
218 sprintf(FTW0,"%ims",dt); | |
219 sprintf(Request,"set;%i;%i;%i;%s",1,CMD_DT, dt ,FTW0); | |
220 SendCmd(Request, Param) ; | |
221 return ; | |
222 } | |
223 | |
224 | |
225 // Change le facteur de division de la clock. Argument: facteur de div, retourne rien | |
226 void DDSFox_SetDiv(DDSParameter *Param) { | |
227 char Request[255]; | |
228 int Div = DIV ; | |
229 Command Rd; | |
230 char temp; | |
231 init_command(&Rd); | |
232 sprintf(Request,"get;%i;%i",1,CMD_CONFIG_REG2); | |
233 if (RecvCmd(&Rd, Request, Param) < 0) | |
234 return; | |
235 temp = atoi(Rd.value2) ; | |
236 temp = temp & 0x9F ; | |
237 temp = temp | Div << 5 ; | |
238 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i",1,CMD_CONFIG_REG2,atoi(Rd.value0), | |
239 atoi(Rd.value1),temp,atoi(Rd.value3),atoi(Rd.value4)) ; | |
240 SendCmd(Request, Param) ; | |
241 return ; | |
242 } | |
243 | |
244 | |
245 | |
246 void DDSFox_SetDf(DDSParameter *Param) { | |
247 char Request[255]; | |
248 double word ; | |
249 unsigned char *octets; | |
250 word = pow(2,48)*(fabs(Param->SweepRate)*Param->Delta_T/Param->Clock); | |
251 octets=malloc(6*sizeof(unsigned char)); | |
252 convert_double_6char(word, octets); | |
253 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_DF, | |
254 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); | |
255 SendCmd(Request, Param); | |
256 free(octets); | |
257 return ; | |
258 } | |
259 | |
260 int DDSFox_GetSweepSign(DDSParameter *Param) { | |
261 if (Param->SweepRate >= 0) return(0); | |
262 else return(1); | |
263 } | |
264 | |
265 | |
266 double DDSFox_ReadFreq(DDSParameter *Param) { | |
267 char Request[255]; | |
268 unsigned int OctetA, OctetB, OctetC, OctetD, OctetE, OctetF ; | |
269 double FreqRead ; | |
270 int cmd[20]; | |
271 Command Rd; | |
272 init_command(&Rd); | |
273 sprintf(Request,"get;%i;%i",1,CMD_GET_FREQ); | |
274 if (RecvCmd(&Rd, Request, Param) == -1) | |
275 return -1.; | |
276 OctetA=atoi(Rd.value0); | |
277 OctetB=atoi(Rd.value1); | |
278 OctetC=atoi(Rd.value2); | |
279 OctetD=atoi(Rd.value3); | |
280 OctetE=atoi(Rd.value4); | |
281 OctetF=atoi(Rd.value5); | |
282 FreqRead=(double)(1099511627776*OctetA +4294967296*OctetB +16777216*OctetC +65536*OctetD +256*OctetE +OctetF); | |
283 FreqRead=(FreqRead*Param->Clock)/ pow(2,48); | |
284 //sprintf(parsebuf,"%fMHz",FreqRead/1000000); | |
285 return(FreqRead); | |
286 } | |
287 | |
288 | |
289 /************************ Fonction de communications TCP ******************/ | |
290 static const double timeout = 0.5; | |
291 | |
292 | |
293 int SendCmd(char *Buffer, DDSParameter *Param) { | |
294 unsigned int hConv=0; | |
295 char Response[255]; | |
296 char parsebuf[255]; | |
297 if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, 0) < 0) { | |
298 printf("Connect failed\n"); | |
299 return -1; | |
300 } | |
301 if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, 0) < 0) { | |
302 printf("Write failed\n"); | |
303 return -1; | |
304 } | |
305 sprintf(Response," "); | |
306 if (ClientTCPRead (hConv, Response, 255, 0) < 0) { | |
307 printf("Read failed\n"); | |
308 return -1; | |
309 } | |
310 if (strncmp("OK",Response,2)) { | |
311 printf("Response failed %s %s\n", Buffer, Response); | |
312 return -1; | |
313 } | |
314 return 0; | |
315 } | |
316 | |
317 | |
318 int RecvCmd(Command *Rd, char * Buffer, DDSParameter *Param) { | |
319 unsigned int hConv=0; | |
320 char parsebuf[255]; | |
321 char Response[255]; | |
322 if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, 0) < 0) { | |
323 printf("Connect failed\n"); | |
324 return -1; | |
325 } | |
326 if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, 0) < 0) { | |
327 printf("Write failed\n"); | |
328 return -1; | |
329 } | |
330 sprintf(Response," "); | |
331 if (ClientTCPRead (hConv, Response, 255, 0) < 0) { | |
332 printf("Read failed\n"); | |
333 return -1; | |
334 } | |
335 | |
336 if (strncmp(Buffer,Response,strlen(Buffer))) { | |
337 printf("Response failed %s %s\n", Buffer, Response); | |
338 return -1; | |
339 } | |
340 | |
341 sprintf(parsebuf,"%s",Response) ; | |
342 parse_command(Rd, parsebuf) ; | |
343 return 0; | |
344 } | |
345 | |
346 | |
347 int OnTCPEvent(unsigned handle, int xType, int errCode, void *callbackData) { | |
348 //char* Response = (char*)(callbackData); | |
349 //char Response[255]; | |
350 //switch(xType) { | |
351 // case TCP_DISCONNECT : | |
352 //printf("Server disconnected\n"); | |
353 // break; | |
354 // case TCP_DATAREADY : | |
355 //printf("Server data ready\n"); | |
356 //ClientTCPRead (handle, Response, 255, 5); | |
357 // break; | |
358 //} | |
359 return 0; | |
360 } | |
361 | |
362 /************************ Fonction de mise en forme des données pour le transfert ******************/ | |
363 | |
364 void parse_command2(int* cmd, char* buf) | |
365 { | |
366 char* pointer; | |
367 int c = 0; | |
368 | |
369 pointer = strtok( buf, CMD_SEP ); | |
370 while( pointer ){ | |
371 if ((c > 2) && ( c < 19 )) | |
372 { | |
373 cmd[c-3] = atoi(pointer); | |
374 printf("%i", cmd[c-3]); | |
375 } | |
376 | |
377 | |
378 pointer = strtok( NULL, CMD_SEP ); | |
379 c++; | |
380 } | |
381 } | |
382 | |
383 | |
384 int parse_command(Command *cmd1, char *buf){ | |
385 char* pointer; | |
386 int c = 0; | |
387 | |
388 pointer = strtok( buf, CMD_SEP ); | |
389 while( pointer ){ | |
390 if( c == 0 ){ | |
391 cmd1->action = pointer; | |
392 } | |
393 else if( c == 1 ){ | |
394 cmd1->canal = pointer; | |
395 } | |
396 else if( c == 2 ){ | |
397 cmd1->command = pointer; | |
398 } | |
399 else if( c == 3 ){ | |
400 cmd1->value0 = pointer; | |
401 } | |
402 else if( c == 4 ){ | |
403 cmd1->value1 = pointer; | |
404 } | |
405 else if( c == 5 ){ | |
406 cmd1->value2 = pointer; | |
407 } | |
408 else if( c == 6 ){ | |
409 cmd1->value3 = pointer; | |
410 } | |
411 else if( c == 7 ){ | |
412 cmd1->value4 = pointer; | |
413 } | |
414 else if( c == 8 ){ | |
415 cmd1->value5 = pointer; | |
416 } | |
417 else if( c == 9 ){ | |
418 cmd1->value6 = pointer; | |
419 } | |
420 else if( c == 10 ){ | |
421 cmd1->value7 = pointer; | |
422 } | |
423 pointer = strtok( NULL, CMD_SEP ); | |
424 c++; | |
425 } | |
426 return 0; | |
427 } | |
428 | |
429 | |
430 int init_command(Command *cmd1){ | |
431 cmd1->action = NULL; | |
432 cmd1->canal = NULL; | |
433 cmd1->command = NULL; | |
434 cmd1->value0 = NULL; | |
435 cmd1->value1 = NULL; | |
436 cmd1->value2 = NULL; | |
437 cmd1->value3 = NULL; | |
438 cmd1->value4 = NULL; | |
439 cmd1->value5 = NULL; | |
440 cmd1->value6 = NULL; | |
441 cmd1->value7 = NULL; | |
442 cmd1->value8 = NULL; | |
443 cmd1->value9 = NULL; | |
444 cmd1->value10 = NULL; | |
445 cmd1->value11 = NULL; | |
446 cmd1->value12 = NULL; | |
447 cmd1->value13 = NULL; | |
448 cmd1->value14 = NULL; | |
449 cmd1->value15 = NULL; | |
450 cmd1->value16 = NULL; | |
451 cmd1->value17 = NULL; | |
452 cmd1->value18 = NULL; | |
453 cmd1->value19 = NULL; | |
454 return 0; | |
455 } | |
456 | |
457 | |
458 //Convert double into 6 bytes sent back by an unsigned char pointer | |
459 int convert_double_6char (double fraction, unsigned char* octets) { | |
460 double bita, bitb, bitc, bitd, bite, bitf; | |
461 bitf= fraction/(1099511627776.0); | |
462 *(octets+5)=(unsigned char)bitf; | |
463 bite= (fraction-1099511627776.0*(*(octets+5)))/(4294967296.0); | |
464 *(octets+4)=(unsigned char)bite; | |
465 bitd=(fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4)))/( 16777216.0); | |
466 *(octets+3)=(unsigned char)bitd; | |
467 bitc=(fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4))-16777216.0*(*(octets+3)))/( 65536.0); | |
468 *(octets+2)=(unsigned char)bitc; | |
469 bitb= (fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4))-16777216.0*(*(octets+3))-65536*(*(octets+2)))/ 256.0; | |
470 *(octets+1)=(unsigned char)bitb; | |
471 bita= (fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4))-16777216.0*(*(octets+3))-65536*(*(octets+2))-256.0*(*(octets+1)))/ 1.0; | |
472 *(octets)=(unsigned char)bita; | |
473 return 0; | |
474 } |