Line data Source code
1 : // This file (my_cntl.cc) was created by Ron Rechenmacher <ron@fnal.gov> on
2 : // Feb 6, 2014. "TERMS AND CONDITIONS" governing this file are in the README
3 : // or COPYING file. If you do not have such a file, one can be obtained by
4 : // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
5 : // $RCSfile: .emacs.gnu,v $
6 : static char* rev = (char*)"$Revision: 1.23 $$Date: 2012/01/23 15:32:40 $";
7 :
8 : #include <fcntl.h> // open, O_RDONLY
9 : #include <getopt.h> // getopt_long
10 : #include <libgen.h> // basename
11 : #include <stdio.h> // printf
12 : #include <stdlib.h> // strtoul
13 : #include <string.h> // strcmp
14 :
15 : #include "mu2e_driver/mu2e_mmap_ioctl.h" // m_ioc_cmd_t
16 : #include "mu2edev.h" // mu2edev
17 :
18 : #define USAGE \
19 : "\
20 : usage: %s <start|stop|dump|spy|hash>\n\
21 : %s read <offset>\n\
22 : %s write <offset> <val>\n\
23 : %s spy [optsmask] # see code for optsmask\n\
24 : %s hash [hostname]\n\
25 : examples: %s start\n\
26 : options:\n\
27 : -d<dtvdevnum> \n\
28 : -l<loops>\n\
29 : -c<channel> 0=ROC data, 1=DCS\n\
30 : -t test/check - currently for read\n\
31 : ", \
32 : basename(argv[0]), basename(argv[0]), basename(argv[0]), basename(argv[0]), basename(argv[0]), basename(argv[0])
33 :
34 0 : int main(int argc, char* argv[])
35 : {
36 0 : int sts = 0;
37 0 : int fd = -1;
38 : char* cmd;
39 : m_ioc_cmd_t ioc_cmd;
40 : m_ioc_reg_access_t reg_access;
41 0 : mu2edev dev;
42 : char devfile[11];
43 0 : int dtc = -1;
44 :
45 0 : int opt_v = 0, opt_chn = 0;
46 : int opt;
47 : // int opt_packets = 8;
48 0 : unsigned opt_loops = 1; // saying -l10 will cause the thing to happen 10 times (i.e "loop back around 9 times")
49 0 : unsigned opt_test_check = 0;
50 : while (1)
51 : {
52 0 : int option_index = 0;
53 : static struct option long_options[] = {
54 : /* name, has_arg, int* flag, val_for_flag */
55 : {"mem-to-malloc", 1, 0, 'm'},
56 : {0, 0, 0, 0},
57 : };
58 0 : opt = getopt_long(argc, argv, "?hm:Vvp:c:d:l:t", long_options, &option_index);
59 0 : if (opt == -1) break;
60 0 : switch (opt)
61 : {
62 0 : case '?':
63 : case 'h':
64 0 : printf(USAGE);
65 0 : exit(0);
66 : break;
67 0 : case 'V':
68 0 : printf("%s\n", rev);
69 0 : return (0);
70 : break;
71 0 : case 'v':
72 0 : opt_v++;
73 0 : break;
74 0 : case 'p':
75 : // opt_packets = strtoul(optarg, NULL, 0);
76 0 : break;
77 0 : case 'd':
78 0 : dtc = strtol(optarg, NULL, 0);
79 0 : break;
80 0 : case 'c':
81 0 : opt_chn = strtol(optarg, NULL, 0);
82 0 : break;
83 0 : case 'l':
84 0 : opt_loops = strtol(optarg, NULL, 0);
85 0 : break;
86 0 : case 't':
87 0 : opt_test_check = 1;
88 0 : break;
89 0 : default:
90 0 : printf("?? getopt returned character code 0%o ??\n", opt);
91 : }
92 0 : }
93 0 : if (argc - optind < 1)
94 : {
95 0 : printf("Need cmd\n");
96 0 : printf(USAGE);
97 0 : exit(0);
98 : }
99 0 : cmd = argv[optind++];
100 0 : TRACE(TLVL_TRACE, "cmd=%s", cmd);
101 : // TRACE(2,"opt_packets=%i", opt_packets);
102 :
103 0 : if (dtc == -1)
104 : {
105 0 : char* dtcE = getenv("DTCLIB_DTC");
106 0 : if (dtcE != NULL)
107 0 : dtc = strtol(dtcE, NULL, 0);
108 : else
109 0 : dtc = 0;
110 : }
111 :
112 0 : if (strcmp(cmd, "start") == 0 ||
113 0 : strcmp(cmd, "stop") == 0 ||
114 0 : strcmp(cmd, "dump") == 0 ||
115 0 : strcmp(cmd, "read") == 0)
116 : {
117 0 : snprintf(devfile, 11, "/dev/" MU2E_DEV_FILE, dtc);
118 :
119 0 : fd = open(devfile, O_RDONLY);
120 0 : if (fd == -1)
121 : {
122 0 : perror("open");
123 0 : return (1);
124 : }
125 : }
126 0 : else if (strcmp(cmd, "write") == 0 ||
127 0 : strcmp(cmd, "spy") == 0)
128 : {
129 0 : dev.init(DTCLib::DTC_SimMode_Disabled, dtc);
130 : }
131 0 : else if (strcmp(cmd, "hash") == 0)
132 : {
133 0 : char* hostname = NULL;
134 0 : uint32_t hash = 0;
135 :
136 0 : if (argc - optind > 0)
137 : {
138 0 : hostname = argv[optind];
139 : }
140 0 : hash = mu2e_host_hash(dtc, hostname);
141 0 : printf("hash: %x\n", hash);
142 0 : return (0);
143 : }
144 : else
145 : {
146 0 : printf("unknown cmd %s\n", cmd);
147 0 : return (1);
148 : }
149 :
150 0 : if (strcmp(cmd, "start") == 0)
151 : {
152 0 : sts = ioctl(fd, M_IOC_TEST_START, &ioc_cmd);
153 : }
154 0 : else if (strcmp(cmd, "stop") == 0)
155 : {
156 0 : sts = ioctl(fd, M_IOC_TEST_STOP, &ioc_cmd);
157 : }
158 0 : else if (strcmp(cmd, "read") == 0)
159 : {
160 0 : if (argc - optind < 1)
161 : {
162 0 : printf(USAGE);
163 0 : return (1);
164 : }
165 0 : reg_access.reg_offset = strtoul(argv[optind], NULL, 0);
166 0 : reg_access.access_type = 0;
167 0 : int need_initial = 1;
168 : unsigned prv_val;
169 0 : int val_changed = 0;
170 0 : for (unsigned ll = 0; ll < opt_loops; ++ll)
171 : {
172 0 : sts = ioctl(fd, M_IOC_REG_ACCESS, ®_access);
173 0 : if (sts)
174 : {
175 0 : perror("ioctl M_IOC_REG_ACCESS read");
176 0 : return (1);
177 : }
178 0 : if (opt_test_check)
179 : {
180 0 : if (need_initial)
181 : {
182 0 : need_initial = 0;
183 0 : prv_val = reg_access.val;
184 0 : printf("initial val: 0x%08x\n", reg_access.val);
185 : }
186 0 : else if (reg_access.val != prv_val)
187 : {
188 0 : printf("val changed from 0x%08x - new val: 0x%08x\n", prv_val, reg_access.val);
189 0 : prv_val = reg_access.val;
190 0 : val_changed++;
191 : }
192 : }
193 : else
194 0 : printf("0x%08x\n", reg_access.val);
195 : }
196 0 : if (val_changed)
197 0 : printf("value changed %d times\n", val_changed);
198 0 : else if (opt_test_check)
199 0 : printf("value did not change during %u loops\n", opt_loops);
200 : }
201 0 : else if (strcmp(cmd, "write") == 0)
202 : {
203 0 : if (argc - optind < 2)
204 : {
205 0 : printf(USAGE);
206 0 : return (1);
207 : }
208 :
209 0 : uint32_t writeAddress = strtoul(argv[optind++], NULL, 0);
210 0 : uint32_t writeValue = strtoul(argv[optind++], NULL, 0);
211 : uint32_t readbackValue;
212 0 : dev.write_register_checked(writeAddress, 100, writeValue,
213 : &readbackValue);
214 0 : if (writeValue != readbackValue)
215 0 : printf("NOTE: Write value mismatch 0x%X vs readback 0x%X\n", writeValue, readbackValue);
216 : }
217 0 : else if (strcmp(cmd, "dump") == 0)
218 : {
219 0 : sts = ioctl(fd, M_IOC_DUMP); // dumps to kernel TRACE (/proc/trace/buffer or printk (NAME=mu2e_main))
220 0 : if (sts)
221 : {
222 0 : perror("ioctl M_IOC_REG_ACCESS write");
223 0 : return (1);
224 : }
225 : }
226 0 : else if (strcmp(cmd, "spy") == 0)
227 : {
228 0 : unsigned optsmsk = 0;
229 0 : if (argc - optind >= 1) optsmsk = strtoul(argv[optind], NULL, 0);
230 0 : std::cout << "Calling dev.spy(chn) with optsmsk=" << optsmsk << "\n";
231 0 : dev.spy(opt_chn, optsmsk);
232 : }
233 : else
234 : {
235 0 : printf("unknown cmd %s\n", cmd);
236 0 : return (1);
237 : }
238 :
239 0 : printf("sts=%d\n", sts);
240 0 : return (0);
241 0 : } // main
|