comparison kk-data-provider.c @ 204:d7f91b9fb515

Fix KK counter setup
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:38 +0200
parents 040658369850
children ec81395bf08d
comparison
equal deleted inserted replaced
203:4b7d1cb5100b 204:d7f91b9fb515
11 #include "KKFX80E.h" 11 #include "KKFX80E.h"
12 12
13 #define DEFAULT_PORT "COM4:115200" 13 #define DEFAULT_PORT "COM4:115200"
14 #define DEFAULT_NCHAN 4 14 #define DEFAULT_NCHAN 4
15 #define TIMEOUT 1 /* seconds */ 15 #define TIMEOUT 1 /* seconds */
16 #define MAXRETRY 20 16 #define MAXRETRY 100
17 17
18 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0) 18 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0)
19 19
20 20
21 static inline int FX_Command(char cmd) 21 static char * FX_Command(unsigned char cmd, unsigned mask, unsigned value)
22 { 22 {
23 char *resp; 23 char *resp;
24 int rv, retry = 0; 24 int rv, retry;
25 25 unsigned int header;
26 /* reset counter buffer */
27 cmd = cmd + 0x80;
28 26
29 /* send command */ 27 /* send command */
30 rv = FX_Send(cmd); 28 rv = FX_Send(cmd);
31 if (! rv) 29 if (! rv)
32 return rv; 30 return NULL;
33 31
34 /* wait successfull reply */ 32 /* wait successfull reply */
35 do { 33 for (retry = 0; retry < MAXRETRY; retry++) {
36 rv = FX_Recv(&resp, TIMEOUT); 34 rv = FX_Recv(&resp, TIMEOUT);
37 if (! rv) 35 if (! rv)
38 return rv; 36 return NULL;
39 if (retry++ > MAXRETRY) 37 header = strtoul(resp, NULL, 16);
40 return 0; 38 if ((header & mask) == value)
41 } while (! strneq(resp, "7015", 4)); 39 return resp;
40 }
42 41
43 return rv; 42 /* max retry reached */
43 return NULL;
44 } 44 }
45 45
46 46
47 int CVICALLBACK KKDataProvider (void *functionData) 47 int CVICALLBACK KKDataProvider (void *functionData)
48 { 48 {
49 int mainThreadId; 49 int mainThreadId;
50 int rv; 50 int rv;
51 char cmd;
52 char *resp; 51 char *resp;
53 struct event event; 52 struct event event;
54 int nchan; 53 int nchan;
55 int scrambler;
56 char port[256]; 54 char port[256];
57 55
58 /* get main thread id to post messages to it */ 56 /* get main thread id to post messages to it */
59 mainThreadId = CmtGetMainThreadID(); 57 mainThreadId = CmtGetMainThreadID();
60 58
74 /* channel number */ 72 /* channel number */
75 rv = Ini_GetInt(configuration, "KK", "nchan", &nchan); 73 rv = Ini_GetInt(configuration, "KK", "nchan", &nchan);
76 if (rv < 1) 74 if (rv < 1)
77 nchan = DEFAULT_NCHAN; 75 nchan = DEFAULT_NCHAN;
78 76
79 /* enable scrambler */
80 rv = Ini_GetInt(configuration, "KK", "scrambler", &scrambler);
81 if (rv < 1)
82 scrambler = 0;
83
84 /* free */ 77 /* free */
85 Ini_Dispose(configuration); 78 Ini_Dispose(configuration);
86 79
87 /* initialize library */ 80 /* initialize library */
88 FX_Init(); 81 FX_Init();
93 SendMessage(mainThreadId, FX_Error()); 86 SendMessage(mainThreadId, FX_Error());
94 goto error; 87 goto error;
95 } 88 }
96 89
97 /* get counter hardware version string */ 90 /* get counter hardware version string */
98 rv = FX_Send(0x81); 91 resp = FX_Command(0x81, 0xFFFF, 0x7001);
99 if (! rv) { 92 if (! resp) {
100 SendMessage(mainThreadId, FX_Error()); 93 SendMessage(mainThreadId, FX_Error());
101 goto error; 94 goto error;
102 } 95 }
103 /* wait for response */
104 do {
105 rv = FX_Recv(&resp, TIMEOUT);
106 if (! rv) {
107 SendMessage(mainThreadId, FX_Error());
108 goto error;
109 }
110 } while (! strneq(resp, "7001", 4));
111 SendMessage(mainThreadId, "KK Counter version: %s", resp + 4); 96 SendMessage(mainThreadId, "KK Counter version: %s", resp + 4);
112 97
113 /* set report interval 1sec */ 98 /* set report interval 1sec */
114 rv = FX_Command(0x29); 99 resp = FX_Command(0x29, 0x0F00, 0x0900);
115 if (! rv) { 100 if (! resp) {
116 SendMessage(mainThreadId, FX_Error());
117 goto error;
118 }
119
120 /* read nchan channels */
121 cmd = 0x30 + nchan;
122 rv = FX_Command(cmd);
123 if (! rv) {
124 SendMessage(mainThreadId, FX_Error()); 101 SendMessage(mainThreadId, FX_Error());
125 goto error; 102 goto error;
126 } 103 }
127 104
128 /* set mode to instantaneous frequency measurement */ 105 /* set mode to instantaneous frequency measurement */
129 rv = FX_Command(0x42); 106 resp = FX_Command(0x42, 0x6F00, 0x2900);
130 if (! rv) { 107 if (! resp) {
131 SendMessage(mainThreadId, FX_Error()); 108 SendMessage(mainThreadId, FX_Error());
132 goto error; 109 goto error;
133 } 110 }
134 111
135 /* scrambler */ 112 /* disable scrambler */
136 rv = FX_Command(0x50 + scrambler); 113 resp = FX_Command(0x50, 0x6FFF, 0x2900);
137 if (! rv) { 114 if (! resp) {
115 SendMessage(mainThreadId, FX_Error());
116 goto error;
117 }
118
119 /* read nchan channels */
120 resp = FX_Command(0x30 + nchan, 0x6FFF, 0x2900);
121 if (! resp) {
138 SendMessage(mainThreadId, FX_Error()); 122 SendMessage(mainThreadId, FX_Error());
139 goto error; 123 goto error;
140 } 124 }
141 125
142 /* enable synchronization */ 126 /* enable synchronization */
143 rv = FX_Command(0x0F); 127 resp = FX_Command(0x0F, 0x00, 0x00);
144 if (! rv) { 128 if (! resp) {
145 SendMessage(mainThreadId, FX_Error()); 129 SendMessage(mainThreadId, FX_Error());
146 goto error; 130 goto error;
147 } 131 }
148 132
149 while (acquiring) { 133 while (acquiring) {
152 if (! resp) { 136 if (! resp) {
153 SendMessage(mainThreadId, FX_Error()); 137 SendMessage(mainThreadId, FX_Error());
154 break; 138 break;
155 } 139 }
156 140
157 /* 2900 with scrambler off or 2910 with scrambler on */ 141 /* data packets */
158 if (strneq(resp, "29", 2)) { 142 if (strneq(resp, "2900", 2)) {
159 143
160 /* timestamp */ 144 /* timestamp */
161 gettimeofday(&event.time, NULL); 145 gettimeofday(&event.time, NULL);
162 146
163 /* parse received data */ 147 /* parse received data */