-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_hrpt.c
executable file
·438 lines (367 loc) · 11.9 KB
/
process_hrpt.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*******************************************************************************
process_hrpt.c
*******************************************************************************/
#define FRAME_WAIT 0
#define FRAME_STARTED 1
/*******************************************************************************
includes
*******************************************************************************/
#ifdef FILLER_RTAI
#include <rtai_malloc.h>
#else
#include <stdlib.h>
#define rt_malloc malloc
#define rt_free free
//#include <string.h>
#endif
#include "controlshm.h"
#include "debugshm.h"
#include "datashm.h"
#include "needfullthings.h"
#include "process_common.h"
#include "sinqhm_errors.h"
/*******************************************************************************
defines
*******************************************************************************/
#define HRPT_NUM_WIRES 1600
// Monitor count directly after wires
#define HRPT_MONITOR_BIN (HRPT_NUM_WIRES + 0)
/*******************************************************************************
file wide global vars
*******************************************************************************/
extern volatile unsigned int *shm_cfg_ptr ;
static volatile uint32 *histoDataPtr = 0;
static volatile uint32 *frameDataPtr = 0;
static volatile uint32 *cnt_low_ptr = 0;
static volatile uint32 *cnt_high_ptr = 0;
static uint32 axislength = 0;
static uint32 last_valid_processed_frame_num;
static uint32 mon_count_low_frame_num;
static uint32 frame_num;
static int last_valid_processed_strobopos;
static uint32 skip_frame;
static uint32 state = FRAME_WAIT;
static uint32 channel_count;
static uint32 picture_length;
static uint32 monitor_count;
static int Hi_bin;
static int Lo_bin;
static unsigned int strobo_mode;
// axis 0 is X axis
// static process_axis_type axis_0;
// axis 1 is strobo axis
static process_axis_type axis_1;
/*******************************************************************************
function prototypes;
*******************************************************************************/
void process_hrpt_init_daq(void);
void process_hrpt(packet_type *p);
void process_hrpt_destruct(void);
/*******************************************************************************
function declarations
*******************************************************************************/
/*******************************************************************************
*
* FUNCTION
* process_hrpt_construct
*
* DESCRIPTION
*
*
* PARAMETERS
*
*
* RETURNS
*
*
******************************************************************************/
int process_hrpt_construct(void)
{
volatile histo_descr_type *histo_descr_ptr;
volatile bank_descr_type *bank_descr_ptr;
volatile axis_descr_type *axis_descr_ptr;
int status;
histo_descr_ptr = getShmHistoPtr();
if(!histo_descr_ptr)
{
dbg_printf(DBGMSG_ERROR, "process_hrpt_construct(): can not get data pointer\n");
return SINQHM_ERR_NO_HISTO_DESCR_PTR;
}
if (histo_descr_ptr->nBank != 1)
{
dbg_printf(DBGMSG_ERROR, "process_hrpt_construct(): nbank must be 1 for this filler type\n");
return SINQHM_ERR_WRONG_NUMBER_OF_BANKS;
}
bank_descr_ptr = getBankDescription(0);
if(!bank_descr_ptr)
{
dbg_printf(DBGMSG_ERROR, "process_hrpt_construct(): can not get bank 0 description pointer\n");
return SINQHM_ERR_NO_BANK_DESCR_PTR;
}
if ((bank_descr_ptr->rank != 1) && (bank_descr_ptr->rank != 2))
{
dbg_printf(DBGMSG_ERROR, "process_hrpt_construct(): naxis must be 1 or 2 for this filler type\n");
return SINQHM_ERR_WRONG_NUMBER_OF_AXIS;
}
if (bank_descr_ptr->rank == 2)
{
strobo_mode=1;
}
else
{
strobo_mode=0;
}
process_packet_fcn = process_hrpt;
process_init_daq_fcn = process_hrpt_init_daq;
process_destruct_fcn = process_hrpt_destruct;
// bank 0, axis 0 is x - axis
axis_descr_ptr = getAxisDescription(0, 0);
axislength = axis_descr_ptr->length;
cnt_low_ptr =&(axis_descr_ptr->cnt_low);
cnt_high_ptr =&(axis_descr_ptr->cnt_high);
// status = SetAxisMapping(axis_descr_ptr, &axis_0, DO_RANGE_CHECK);
if (axis_descr_ptr->type != AXDIRECT)
{
dbg_printf(DBGMSG_ERROR, "process_hrpt_construct(): mapping for axis 0 must be direct\n");
return SINQHM_ERR_WRONG_AXIS_MAPPING;
}
if (strobo_mode)
{
// bank 0, axis 2 is tof - axis
axis_descr_ptr = getAxisDescription(0, 1);
status = SetAxisMapping(axis_descr_ptr, &axis_1, DO_RANGE_CHECK);
if (status<0) return status;
}
histoDataPtr=getHistDataPtr();
if(!histoDataPtr)
{
dbg_printf(DBGMSG_ERROR, "process_hrpt_construct(): can not get histo data pointer\n");
return SINQHM_ERR_NO_HISTO_DATA_PTR;
}
frameDataPtr = (volatile uint32 *)rt_malloc( (axislength * sizeof(int)));
if(!frameDataPtr)
{
dbg_printf(DBGMSG_ERROR, "process_hrpt_construct(): can not allocate Frame Data\n");
return SINQHM_ERR_FIL_MALLOC_FAILED;
}
memset((void*)frameDataPtr,0,(axislength * sizeof(int)) );
state = FRAME_WAIT;
Lo_bin=0;
if (axislength > HRPT_NUM_WIRES)
{
Hi_bin=HRPT_NUM_WIRES - 1;
}
else
{
Hi_bin=axislength - 1;
}
return SINQHM_OK;
}
/******************************************************************************/
void process_hrpt_init_daq(void)
{
*cnt_low_ptr = 0;
*cnt_high_ptr = 0;
// *(axis_0.cnt_low_ptr) = 0;
// *(axis_0.cnt_high_ptr) = 0;
if (strobo_mode)
{
*(axis_1.cnt_low_ptr) = 0;
*(axis_1.cnt_high_ptr) = 0;
}
state = FRAME_WAIT;
picture_length = 0;
channel_count = 0;
last_valid_processed_frame_num = 0xffffffff;
}
/*******************************************************************************
*
* FUNCTION
* process_hrpt_destruct
*
* DESCRIPTION
*
*
* PARAMETERS
*
*
* RETURNS
*
*
******************************************************************************/
void process_hrpt_destruct(void)
{
histoDataPtr = 0;
if (frameDataPtr)
{
rt_free((void*)frameDataPtr);
frameDataPtr=0;
}
}
/*******************************************************************************
*
* FUNCTION
* process_packet_hrpt
*
* DESCRIPTION
*
*
* PARAMETERS
*
*
* RETURNS
*
*
******************************************************************************/
void process_hrpt(packet_type *p)
{
uint32 header_type;
int i,wire, binpos;
uint32 timestamp;
int strobopos;
unsigned int error_flag;
// print_packet(p);
header_type = p->data[0] & LWL_HDR_TYPE_MASK;
if (header_type == LWL_HM_NC_C3)
{
wire = ((p->data[1]) & 0x0fff) >> 1;
error_flag = !(p->data[1] & 0x01); // Note: Error Bit 0: error occured 1: no error
if (wire < Lo_bin) /* Out-of-range? */
{
UINT32_INC_CEIL(*cnt_low_ptr);
}
else if (wire <= Hi_bin)
{
if (wire == Lo_bin) /* Start of new frame? */
{
if (state != FRAME_WAIT)
{
dbg_printf(DBGMSG_ERROR, "process_hrpt(): incomplete Frame\n");
shm_cfg_ptr[CFG_FIL_EVT_ERROR]++;
}
memset((void*)frameDataPtr,0,(axislength * sizeof(int)) );
state = FRAME_STARTED; /* Yes. Note our new state */
frame_num = p->data[3];
skip_frame = 0;
mon_count_low_frame_num = 0xffffffff;
channel_count = 1;
}
else
{
if (state == FRAME_STARTED) /* Yes. Check state */
{
if (wire != channel_count) /* And check frame # */
{
dbg_printf (DBGMSG_ERROR, "process_hrpt(): Wire sequence error: expected %d but got %d\n",channel_count,wire);
state = FRAME_WAIT;
shm_cfg_ptr[CFG_FIL_EVT_ERROR]++;
}
if (p->data[3] != frame_num) /* And check frame # */
{
dbg_printf (DBGMSG_ERROR, "process_hrpt(): Frame sequence error: expected %d but got %d\n",frame_num,p->data[3]);
state = FRAME_WAIT;
shm_cfg_ptr[CFG_FIL_EVT_ERROR]++;
}
else
{
channel_count++;
}
}
}
frameDataPtr[wire] = (p->data[2]) & 0x03ff;
skip_frame = skip_frame || ((p->data[0] & hdr_daq_mask) != hdr_daq_active) || error_flag;
if ((wire == Hi_bin) && (state == FRAME_STARTED)) /* End of frame? */
{
if(channel_count != (Hi_bin+1))
{
dbg_printf(DBGMSG_ERROR, "process_hrpt(): incomplete Frame\n");
shm_cfg_ptr[CFG_FIL_EVT_ERROR]++;
}
else if (skip_frame)
{
// skip
shm_cfg_ptr[CFG_FIL_EVT_SKIPPED]++;
}
else
{
shm_cfg_ptr[CFG_FIL_EVT_PROCESSED]++;
last_valid_processed_frame_num = frame_num;
if(strobo_mode)
{
timestamp = p->data[0] & LWL_HDR_TS_MASK;
strobopos = axis_1.fcn(timestamp, &axis_1);
last_valid_processed_strobopos = strobopos;
if (strobopos>=0)
{
for (i = 0; i <= Hi_bin; i++)
{
binpos = ((Hi_bin - i) * axis_1.len) + strobopos; /* Reflect */
UINT32_INC_X_CEIL_CNT_OVL(histoDataPtr[binpos], frameDataPtr[i], shm_cfg_ptr[CFG_FIL_BIN_OVERFLOWS]);
}
}
}
else
{
for (i = 0; i <= Hi_bin; i++)
{
binpos = Hi_bin - i; /* Reflect */
UINT32_INC_X_CEIL_CNT_OVL(histoDataPtr[binpos], frameDataPtr[i], shm_cfg_ptr[CFG_FIL_BIN_OVERFLOWS]);
}
}
}
state = FRAME_WAIT;
}
}
else if (wire == (HRPT_NUM_WIRES + 0))
{
// info currently not used
picture_length = p->data[2];
}
else if (wire == (HRPT_NUM_WIRES + 1))
{
picture_length |= p->data[2] << 16;
shm_cfg_ptr[CFG_FIL_PIC_LENGTH] = picture_length;
}
else if (wire == (HRPT_NUM_WIRES + 2))
{
monitor_count = p->data[2]; // lower 16 bit of monitor count
mon_count_low_frame_num = p->data[3];
}
else if (wire == (HRPT_NUM_WIRES + 3))
{
frame_num = p->data[3];
monitor_count |= p->data[2] << 16;
if ( (!skip_frame) &&
(axislength > HRPT_MONITOR_BIN) &&
(frame_num == mon_count_low_frame_num) &&
(frame_num == last_valid_processed_frame_num)
)
{
if(strobo_mode)
{
if (last_valid_processed_strobopos>=0)
{
binpos = (HRPT_MONITOR_BIN * axis_1.len) + last_valid_processed_strobopos;
UINT32_INC_X_CEIL_CNT_OVL(histoDataPtr[binpos], monitor_count, shm_cfg_ptr[CFG_FIL_BIN_OVERFLOWS]);
}
}
else
{
binpos = HRPT_MONITOR_BIN;
UINT32_INC_X_CEIL_CNT_OVL(histoDataPtr[binpos], monitor_count, shm_cfg_ptr[CFG_FIL_BIN_OVERFLOWS]);
}
}
}
else // if (wire > Hi_bin)
{
UINT32_INC_CEIL(*cnt_high_ptr);
}
}
else if (!process_tsi(p))
{
// unknown packet
UINT32_INC_CEIL(shm_cfg_ptr[CFG_FIL_PKG_UNKNOWN]);
UINT32_INC_CEIL(shm_cfg_ptr[CFG_FIL_SUM_PKG_UNKNOWN]);
}
}
/*******************************************************************************/