This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_2k.c
340 lines (282 loc) · 8.06 KB
/
run_2k.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*****************************************************************************
bmc_ltest.c Boston Micromachines Corp. library test program
Modified to integrate with cacao
Nov 1, 2014 CIW/SJT original entry, � Copyright 2014, Boston Micromachines Corporation. All rights reserved.
2015-2018 Modified for cacao support
2022 Shrink down to minimal SCExAO functionality
To build:
./compile-exec.sh (sudo prompt will show up, that's for suid permission)
Required files:
bmc_ltest.c (this file)
bmc_mdlib.h
../../lib/libbmcmd.a
ImageStreamIO shared library
To run loop, execute :
./bmc_ltest -K # initialize board for 2k DM
./bmc_ltest -D 0 # set driver spin delay count to 0
./bmc_ltest -L # real time control loop
Follow the compile-exec.sh for include path
*****************************************************************************/
/*==========================================================================*/
//#include <stdio.h>
/*
#include <stdarg.h>
#include <math.h>
#include <semaphore.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <sys/file.h>
#include <errno.h>
#include <sys/mman.h>
*/
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
#include "funcs_2k.h"
#define DMVOLT_FILENAME "dmvolt"
static int status_exit = 0; // program exit code
static int end = 0; // termination flag for funcs
struct timespec tnow;
double tnowdouble;
double tlastupdatedouble;
double dt_update; // time since last update
double dt_update_lim = 3600.0; // if no command is received during this time, set DM to zero V [sec]
IMAGE *SMdmvolt;
// Termination function for SIGINT callback
static void endme(int dummy)
{
end = 1;
}
static char *sArgv0 = NULL; // name of executable
static void show_help(void)
{
SHOW(("%s of %s at %s\n", sArgv0, __DATE__, __TIME__));
SHOW((" arguments:\n"));
SHOW((" -D count set driver spin delay count\n"));
SHOW((" -K initialize board for 2k DM\n"));
SHOW((" -L real time control loop\n"));
SHOW((" -A Run -K, -D 0, -L\n"));
}
static int dm2k_realtimeloop()
{
long ActIndex[50][50];
IMAGE_METADATA *map;
char *mapv; // pointed cast in bytes
long DMStartRow[50] = {18, 15, 13, 11, 10, 8, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 10, 11, 13, 15, 18};
long DMEndRow[50] = {31, 34, 36, 38, 39, 41, 42, 43, 44, 44, 45, 46, 46, 47, 47, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 48, 48, 48, 47, 47, 46, 46, 45, 44, 44, 43, 42, 41, 39, 38, 36, 34, 31};
struct stat file_stat;
long cntloop;
int sx, sy, s1;
int SMdmvoltfd;
long framecnt;
long cnt0;
int Index = 0;
short s;
long ii;
tE err;
unsigned short int temp[2500];
int RT_priority = 90; // any number from 0-99
struct sched_param schedpar;
int r;
// register interrupt signal to terminate the main loop
signal(SIGINT, endme);
printf("Starting loop\n");
fflush(stdout);
// build up mapping
for (sx = 0; sx < 50; sx++)
for (sy = 0; sy < 50; sy++)
ActIndex[sx][sy] = -1;
s = 1;
for (sy = 0; sy < 50; sy++)
{
// printf("RANGE %3ld: %ld - %ld\n", sy, DMStartRow[sy], DMEndRow[sy]);
for (sx = 0; sx < 50; sx++)
{
if ((sx >= DMStartRow[sy]) && (sx <= DMEndRow[sy]))
{
ActIndex[sx][sy] = s;
printf("[%4ld %4ld -> %4ld]", (long)sx, (long)sy, (long)ActIndex[sx][sy]);
s++;
if (s % 8 == 7)
{
printf("\n");
}
}
}
}
printf("\n");
SMdmvolt = (IMAGE *)malloc(sizeof(IMAGE));
// CONNECT TO VOLT MAP SHARED MEMORY
ImageStreamIO_read_sharedmem_image_toIMAGE(DMVOLT_FILENAME, &SMdmvolt[0]);
framecnt = -1;
// MAIN LOOP
printf("ENTERING LOOP\n");
fflush(stdout);
cntloop = 0;
while ((end == 0) && (SMdmvolt[0].md[0].status < 100)) //&&(cntloop<10000))
{
cnt0 = SMdmvolt[0].md[0].cnt0;
if ((cnt0 > framecnt) && (SMdmvolt[0].md[0].write == 0)) // new frame is here... apply on DM
{
// LOG DM FILE
s = 0;
s1 = 0;
memcpy(temp, SMdmvolt[0].array.UI16, 2500 * sizeof(unsigned short int));
for (sx = 0; sx < 50; sx++)
for (sy = 0; sy < 50; sy++)
{
Index = ActIndex[sx][sy];
if (Index >= 0)
sActValue[Index] = (short)temp[s1];
s1++;
}
if (kBMCEnoErr != (err = BMCburstHVA(sBMC, 1024, sActValue)))
{
ERR(("decode_args:BMCburstHVA %p gave %s\n",
sBMC, BMCgetErrStr(err)));
}
framecnt = cnt0;
SMdmvolt[0].md[0].cnt1++;
clock_gettime(CLOCK_REALTIME, &tnow);
// tnowdouble = 1.0*tnow.tv_sec + 1.0e-9*tnow.tv_nsec;
// dt_update = tnowdouble - tlastupdatedouble;
tlastupdatedouble = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
cntloop++;
}
else // do not apply anything... wait 10us
{
usleep(10); // 10 us
clock_gettime(CLOCK_REALTIME, &tnow);
tnowdouble = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
dt_update = tnowdouble - tlastupdatedouble;
if (dt_update > dt_update_lim)
{
// set DM to zero
printf("DM going to sleep ... \n");
SMdmvolt[0].md[0].write = 1;
for (ii = 0; ii < 2500; ii++)
SMdmvolt[0].array.UI16[ii] = 0;
SMdmvolt[0].md[0].write = 0;
SMdmvolt[0].md[0].cnt0++;
}
}
}
printf("EXITING MAIN LOOP\n");
printf("[%10ld] SENT TO DM : %ld / %ld (%6.2f%%)\n", cntloop, SMdmvolt[0].md[0].cnt1, SMdmvolt[0].md[0].cnt0, 100.0 * SMdmvolt[0].md[0].cnt1 / SMdmvolt[0].md[0].cnt0);
fflush(stdout);
return 0;
}
static void decode_args(int argc, char **argv)
/*
** Parse the input arguments.
*/
{
char *str;
int a1, a2, a3, a4, a5, i;
tU32 x;
tU32 data32[kBIGDATA];
tU16 data16[kBIGDATA];
tD ad, add;
argv += 1;
argc -= 1; /* skip program name */
if (argc == 0)
{
show_help();
return;
}
while (argc-- > 0)
{
str = *argv++;
if (str[0] != '-')
{
ERR(("Do not know arg '%s'\n", str));
show_help();
exit(1);
}
switch (str[1])
{
case 'D':
(void)sscanf(*argv++, "%d", &a1);
argc -= 1;
make_open();
set_spin_delay(a1);
break;
/* ADDED BY OLIVIER GUYON 2015-07-14: 2k DM support */
case 'K':
make_open();
init_hva();
break;
case 'L':
make_open();
printf("2k DM real time control\n");
dm2k_realtimeloop();
break;
case 'A':
SHOW(("Running -K, -D 0, -L: init+start main loop\n"));
make_open();
init_hva();
set_spin_delay(0);
dm2k_realtimeloop();
break;
default:
show_help();
break;
}
}
return;
}
/*
** Manage UID to promote to cset and RT priority
*/
static void set_rt_priority()
{
uid_t ruid; // Real UID (= user launching process at startup)
uid_t euid; // Effective UID (= owner of executable at startup)
uid_t suid; // Saved UID (= owner of executable at startup)
int RT_priority = 93; // any number from 0-99
struct sched_param schedpar;
int ret;
getresuid(&ruid, &euid, &suid);
// This sets it to the privileges of the normal user
ret = seteuid(ruid);
if (ret != 0)
{
SHOW(("setuid error\n"));
}
schedpar.sched_priority = RT_priority;
if (ret != 0)
{
SHOW(("setuid error\n"));
}
ret = seteuid(euid); // This goes up to maximum privileges
sched_setscheduler(0, SCHED_FIFO,
&schedpar); // other option is SCHED_RR, might be faster
ret = seteuid(ruid); // Go back to normal privileges
if (ret != 0)
{
SHOW(("setuid error\n"));
}
}
/*
** Main
*/
int main(int argc, char **argv)
{
SHOW(("%d", size_2k));
set_rt_priority();
sArgv0 = *argv;
sBMC = NULL;
decode_args(argc, argv);
if (NULL != sBMC)
{
(void)BMCclose(sBMC);
}
return (status_exit);
}
/*==========================================================================*/