comparison kk-data-provider.c @ 250:9b35a2b2c759

Implement selection of gate type (PI or LAMBDA) in configuration file
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 04 May 2015 17:48:07 +0200
parents ec81395bf08d
children
comparison
equal deleted inserted replaced
249:b3581a75a0b7 250:9b35a2b2c759
8 8
9 #include "config.h" 9 #include "config.h"
10 #include "data-provider.h" 10 #include "data-provider.h"
11 #include "KKFX80E.h" 11 #include "KKFX80E.h"
12 12
13
14 #define FX_MODE_PI 0x02
15 #define FX_MODE_LAMBDA 0x03
16
13 #define DEFAULT_PORT "COM4:115200" 17 #define DEFAULT_PORT "COM4:115200"
14 #define DEFAULT_NCHAN 4 18 #define DEFAULT_NCHAN 4
19 #define DEFAULT_MODE FX_MODE_PI
15 #define TIMEOUT 1 /* seconds */ 20 #define TIMEOUT 1 /* seconds */
16 #define MAXRETRY 100 21 #define MAXRETRY 100
17 22
18 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0) 23 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0)
24
25
26 const char * FX_ModeString(int mode)
27 {
28 const static char *modes[] = {
29 [FX_MODE_PI] = "PI",
30 [FX_MODE_LAMBDA] = "LAMBDA",
31 };
32
33 return modes[mode];
34 }
19 35
20 36
21 static char * FX_Command(unsigned char cmd, unsigned mask, unsigned value) 37 static char * FX_Command(unsigned char cmd, unsigned mask, unsigned value)
22 { 38 {
23 char *resp; 39 char *resp;
47 int CVICALLBACK KKDataProvider (void *functionData) 63 int CVICALLBACK KKDataProvider (void *functionData)
48 { 64 {
49 int main_thread_id; 65 int main_thread_id;
50 int rv; 66 int rv;
51 char *resp; 67 char *resp;
68 int nchan = DEFAULT_NCHAN;
69 int mode = DEFAULT_MODE;
70 char port[256];
71 char buffer[16];
72 char header[16];
52 struct event event; 73 struct event event;
53 int nchan;
54 char port[256];
55 74
56 /* get main thread id to post messages to it */ 75 /* get main thread id to post messages to it */
57 main_thread_id = CmtGetMainThreadID(); 76 main_thread_id = CmtGetMainThreadID();
58 77
59 /* configuration file path */ 78 /* configuration file path */
62 81
63 /* load configuration file */ 82 /* load configuration file */
64 IniText configuration = Ini_New(TRUE); 83 IniText configuration = Ini_New(TRUE);
65 Ini_ReadFromFile(configuration, path); 84 Ini_ReadFromFile(configuration, path);
66 85
67 /* get serial port name configuration */ 86 /* serial port name */
68 rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port)); 87 rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port));
69 if (rv < 1) 88 if (rv < 1)
70 strncpy(port, DEFAULT_PORT, sizeof(port)); 89 strncpy(port, DEFAULT_PORT, sizeof(port));
90
91 /* gate type */
92 rv = Ini_GetStringIntoBuffer(configuration, "KK", "mode", buffer, sizeof(buffer));
93 if (rv < 1)
94 mode = DEFAULT_MODE;
95 else if (stricmp(buffer, "PI") == 0)
96 mode = FX_MODE_PI;
97 else if (stricmp(buffer, "LAMBDA") == 0)
98 mode = FX_MODE_LAMBDA;
99 else
100 send_message(main_thread_id, "KK Counter: unrecognised mode parameter '%s'", buffer);
71 101
72 /* channel number */ 102 /* channel number */
73 rv = Ini_GetInt(configuration, "KK", "nchan", &nchan); 103 rv = Ini_GetInt(configuration, "KK", "nchan", &nchan);
74 if (rv < 1) 104 if (rv < 1)
75 nchan = DEFAULT_NCHAN; 105 nchan = DEFAULT_NCHAN;
91 resp = FX_Command(0x81, 0xFFFF, 0x7001); 121 resp = FX_Command(0x81, 0xFFFF, 0x7001);
92 if (! resp) { 122 if (! resp) {
93 send_message(main_thread_id, FX_Error()); 123 send_message(main_thread_id, FX_Error());
94 goto error; 124 goto error;
95 } 125 }
96 send_message(main_thread_id, "KK Counter version: %s", resp + 4); 126 send_message(main_thread_id, "KK Counter: version %s", resp + 4);
97 127
98 /* set report interval 1sec */ 128 /* set report interval 1sec */
99 resp = FX_Command(0x29, 0x0F00, 0x0900); 129 resp = FX_Command(0x29, 0x0F00, 0x0900);
100 if (! resp) { 130 if (! resp) {
101 send_message(main_thread_id, FX_Error()); 131 send_message(main_thread_id, FX_Error());
102 goto error; 132 goto error;
103 } 133 }
104 134
105 /* set mode to instantaneous frequency measurement */ 135 /* set mode */
106 resp = FX_Command(0x42, 0x6F00, 0x2900); 136 resp = FX_Command(0x40 + mode, 0x7000, (mode << 12));
107 if (! resp) { 137 if (! resp) {
108 send_message(main_thread_id, FX_Error()); 138 send_message(main_thread_id, FX_Error());
109 goto error; 139 goto error;
110 } 140 }
111 141 send_message(main_thread_id, "KK Counter: set mode %s", FX_ModeString(mode));
142
112 /* disable scrambler */ 143 /* disable scrambler */
113 resp = FX_Command(0x50, 0x6FFF, 0x2900); 144 resp = FX_Command(0x50, 0xFFFF, (mode << 12) + 0x0900);
114 if (! resp) { 145 if (! resp) {
115 send_message(main_thread_id, FX_Error()); 146 send_message(main_thread_id, FX_Error());
116 goto error; 147 goto error;
117 } 148 }
118 149
119 /* read nchan channels */ 150 /* read nchan channels */
120 resp = FX_Command(0x30 + nchan, 0x6FFF, 0x2900); 151 resp = FX_Command(0x30 + nchan, 0xFFFF, (mode << 12) + 0x0900);
121 if (! resp) { 152 if (! resp) {
122 send_message(main_thread_id, FX_Error()); 153 send_message(main_thread_id, FX_Error());
123 goto error; 154 goto error;
124 } 155 }
125 156
127 resp = FX_Command(0x0F, 0x00, 0x00); 158 resp = FX_Command(0x0F, 0x00, 0x00);
128 if (! resp) { 159 if (! resp) {
129 send_message(main_thread_id, FX_Error()); 160 send_message(main_thread_id, FX_Error());
130 goto error; 161 goto error;
131 } 162 }
163
164 /* expected packet header */
165 snprintf(header, sizeof(header), "%X", (mode << 12) + 0x0900);
132 166
133 while (acquiring) { 167 while (acquiring) {
134 /* receive data from counter */ 168 /* receive data from counter */
135 FX_Recv(&resp, TIMEOUT); 169 FX_Recv(&resp, TIMEOUT);
136 if (! resp) { 170 if (! resp) {
137 send_message(main_thread_id, FX_Error()); 171 send_message(main_thread_id, FX_Error());
138 break; 172 break;
139 } 173 }
140 174
141 /* data packets */ 175 /* data packets */
142 if (strneq(resp, "2900", 2)) { 176 if (strneq(resp, header, 4)) {
143 177
144 /* timestamp */ 178 /* timestamp */
145 gettimeofday(&event.time, NULL); 179 gettimeofday(&event.time, NULL);
146 180
147 /* parse received data */ 181 /* parse received data */
167 for each data packet. it probably contains a sample count along 201 for each data packet. it probably contains a sample count along
168 with some other information */ 202 with some other information */
169 } else if (strneq(resp, "7020", 4)) { 203 } else if (strneq(resp, "7020", 4)) {
170 /* undocumented packet. it probably reports the header for 204 /* undocumented packet. it probably reports the header for
171 subsequent data packets */ 205 subsequent data packets */
172 send_message(main_thread_id, "KK Counter packet header: %s", resp + 7); 206 send_message(main_thread_id, "KK Counter: packet header %s", resp + 7);
173 } else if (strneq(resp, "7F51", 4)) { 207 } else if (strneq(resp, "7F51", 4)) {
174 /* measurement interval synchronized */ 208 /* measurement interval synchronized */
175 send_message(main_thread_id, "KK Counter measurement interval synchronized"); 209 send_message(main_thread_id, "KK Counter: measurement interval synchronized");
176 } else { 210 } else {
177 /* send message to the main thread */ 211 /* send message to the main thread */
178 send_message(main_thread_id, "KK Counter: %s", resp); 212 send_message(main_thread_id, "KK Counter: %s", resp);
179 } 213 }
180 } 214 }