/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to ipecamera/ipecamera.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-04-20 20:01:04 UTC
  • Revision ID: csa@suren.me-20150420200104-b5xny65io8lvoz3w
Big redign of model structures

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#define _IPECAMERA_IMAGE_C
2
 
#define _BSD_SOURCE
3
 
#define _GNU_SOURCE
4
 
 
5
 
#include <stdio.h>
6
 
#include <stdlib.h>
7
 
#include <unistd.h>
8
 
#include <string.h>
9
 
#include <sys/time.h>
10
 
#include <pthread.h>
11
 
#include <assert.h>
12
 
 
13
 
#include <ufodecode.h>
14
 
 
15
 
#include "../tools.h"
16
 
#include "../error.h"
17
 
#include "../event.h"
18
 
 
19
 
#include "pcilib.h"
20
 
#include "private.h"
21
 
#include "model.h"
22
 
#include "reader.h"
23
 
#include "events.h"
24
 
#include "data.h"
25
 
 
26
 
 
27
 
#include "dma/nwl.h"
28
 
 
29
 
#define FIND_REG(var, bank, name)  \
30
 
        ctx->var = pcilib_find_register(pcilib, bank, name); \
31
 
        if (ctx->var ==  PCILIB_REGISTER_INVALID) { \
32
 
            err = PCILIB_ERROR_NOTFOUND; \
33
 
            pcilib_error("Unable to find a %s register", name); \
34
 
        }
35
 
    
36
 
 
37
 
#define GET_REG(reg, var) \
38
 
    if (!err) { \
39
 
        err = pcilib_read_register_by_id(pcilib, ctx->reg, &var); \
40
 
        if (err) { \
41
 
            pcilib_error("Error reading %s register", ipecamera_registers[ctx->reg].name); \
42
 
        } \
43
 
    }
44
 
 
45
 
#define SET_REG(reg, val) \
46
 
    if (!err) { \
47
 
        err = pcilib_write_register_by_id(pcilib, ctx->reg, val); \
48
 
        if (err) { \
49
 
            pcilib_error("Error writting %s register", ipecamera_registers[ctx->reg].name); \
50
 
        } \
51
 
    }
52
 
 
53
 
#define CHECK_REG(reg, check) \
54
 
    if (!err) { \
55
 
        err = pcilib_read_register_by_id(pcilib, ctx->reg, &value); \
56
 
        if (err) { \
57
 
            pcilib_error("Error reading %s register", ipecamera_registers[ctx->reg].name); \
58
 
        } \
59
 
        if (value != check) { \
60
 
            pcilib_error("Unexpected value (0x%lx) of register %s", value, ipecamera_registers[ctx->reg].name); \
61
 
            err = PCILIB_ERROR_INVALID_DATA; \
62
 
        } \
63
 
    }
64
 
 
65
 
#define IPECAMERA_GET_EXPECTED_STATUS(ctx) ((ctx->firmware == 4)?IPECAMERA_EXPECTED_STATUS_4:IPECAMERA_EXPECTED_STATUS)
66
 
#define CHECK_STATUS_REG() CHECK_REG(status_reg, IPECAMERA_GET_EXPECTED_STATUS(ctx))
67
 
 
68
 
#define CHECK_VALUE(value, val) \
69
 
    if ((!err)&&(value != val)) { \
70
 
        pcilib_error("Unexpected value (0x%x) in data stream (0x%x is expected)", value, val); \
71
 
        err = PCILIB_ERROR_INVALID_DATA; \
72
 
    }
73
 
 
74
 
#define CHECK_FLAG(flag, check, ...) \
75
 
    if ((!err)&&(!(check))) { \
76
 
        pcilib_error("Unexpected value (0x%x) of " flag,  __VA_ARGS__); \
77
 
        err = PCILIB_ERROR_INVALID_DATA; \
78
 
    }
79
 
 
80
 
 
81
 
pcilib_context_t *ipecamera_init(pcilib_t *pcilib) {
82
 
    int err = 0; 
83
 
    
84
 
    ipecamera_t *ctx = malloc(sizeof(ipecamera_t));
85
 
 
86
 
    if (ctx) {
87
 
        pcilib_register_value_t value;
88
 
        
89
 
        memset(ctx, 0, sizeof(ipecamera_t));
90
 
 
91
 
        ctx->buffer_size = IPECAMERA_DEFAULT_BUFFER_SIZE;
92
 
        ctx->dim.bpp = sizeof(ipecamera_pixel_t) * 8;
93
 
 
94
 
            // We need DMA engine initialized to resolve DMA registers
95
 
//      FIND_REG(packet_len_reg, "fpga", "xrawdata_packet_length");
96
 
        
97
 
        FIND_REG(status_reg, "fpga", "status");
98
 
        FIND_REG(control_reg, "fpga", "control");
99
 
 
100
 
        FIND_REG(status3_reg, "fpga", "status3");
101
 
 
102
 
        FIND_REG(n_lines_reg, "cmosis", "cmosis_number_lines");
103
 
        FIND_REG(line_reg, "cmosis", "cmosis_start1");
104
 
        FIND_REG(exposure_reg, "cmosis", "cmosis_exp_time");
105
 
        FIND_REG(flip_reg, "cmosis", "cmosis_image_flipping");
106
 
        
107
 
        FIND_REG(firmware_version_reg, "fpga", "firmware_version");
108
 
        FIND_REG(adc_resolution_reg, "fpga", "adc_resolution");
109
 
        FIND_REG(output_mode_reg, "fpga", "output_mode");
110
 
        
111
 
        FIND_REG(max_frames_reg, "fpga", "ddr_max_frames");
112
 
        FIND_REG(num_frames_reg, "fpga", "ddr_num_frames");
113
 
 
114
 
 
115
 
        GET_REG(firmware_version_reg, value);
116
 
        switch (value) {
117
 
         case 4:
118
 
         case 5:
119
 
            ctx->firmware = value;
120
 
            break;
121
 
         default:
122
 
//          pcilib_error("Unsupported version of firmware (%lu)", value);
123
 
            ctx->firmware = 0;
124
 
        }
125
 
 
126
 
#ifdef IPECAMERA_BUG_POSTPONED_READ
127
 
        GET_REG(max_frames_reg, value);
128
 
        if ((value + IPECAMERA_RESERVE_BUFFERS + 3) > ctx->buffer_size) {
129
 
            ctx->buffer_size = (value + 1) + IPECAMERA_RESERVE_BUFFERS + 2;
130
 
        }
131
 
#endif /* IPECAMERA_BUG_POSTPONED_READ */
132
 
 
133
 
 
134
 
        ctx->rdma = PCILIB_DMA_ENGINE_INVALID;
135
 
        ctx->wdma = PCILIB_DMA_ENGINE_INVALID;
136
 
 
137
 
        if (err) {
138
 
            free(ctx);
139
 
            return NULL;
140
 
        }
141
 
    }
142
 
    
143
 
    return (pcilib_context_t*)ctx;
144
 
}
145
 
 
146
 
void ipecamera_free(pcilib_context_t *vctx) {
147
 
    if (vctx) {
148
 
        ipecamera_t *ctx = (ipecamera_t*)vctx;
149
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
150
 
        free(ctx);
151
 
    }
152
 
}
153
 
 
154
 
pcilib_dma_context_t *ipecamera_init_dma(pcilib_context_t *vctx) {
155
 
#ifdef IPECAMERA_DMA_R3
156
 
    ipecamera_t *ctx = (ipecamera_t*)vctx;
157
 
#endif
158
 
    
159
 
    pcilib_model_description_t *model_info = pcilib_get_model_description(vctx->pcilib);
160
 
    if ((!model_info->dma_api)||(!model_info->dma_api->init)) {
161
 
        pcilib_error("The DMA engine is not configured in model");
162
 
        return NULL;
163
 
    }
164
 
 
165
 
 
166
 
#ifdef IPECAMERA_DMA_R3
167
 
    if (ctx->firmware) {
168
 
        return model_info->dma_api->init(vctx->pcilib, PCILIB_NWL_MODIFICATION_IPECAMERA, NULL);
169
 
    } else {
170
 
        return model_info->dma_api->init(vctx->pcilib, PCILIB_DMA_MODIFICATION_DEFAULT, NULL);
171
 
    }
172
 
#else
173
 
    return model_info->dma_api->init(vctx->pcilib, PCILIB_DMA_MODIFICATION_DEFAULT, NULL);
174
 
#endif
175
 
}
176
 
 
177
 
 
178
 
int ipecamera_set_buffer_size(ipecamera_t *ctx, int size) {
179
 
    if (ctx->started) {
180
 
        pcilib_error("Can't change buffer size while grabbing");
181
 
        return PCILIB_ERROR_INVALID_REQUEST;
182
 
    }
183
 
    
184
 
    if (size < 2) {
185
 
        pcilib_error("The buffer size is too small");
186
 
        return PCILIB_ERROR_INVALID_REQUEST;
187
 
    }
188
 
    
189
 
    if (((size^(size-1)) < size) < size) {
190
 
        pcilib_error("The buffer size is not power of 2");
191
 
    }
192
 
    
193
 
    ctx->buffer_size = size;
194
 
    
195
 
    return 0;
196
 
}
197
 
 
198
 
int ipecamera_reset(pcilib_context_t *vctx) {
199
 
    int err = 0;
200
 
    ipecamera_t *ctx = (ipecamera_t*)vctx;
201
 
    pcilib_t *pcilib = vctx->pcilib;
202
 
 
203
 
    pcilib_register_t control, status;
204
 
    pcilib_register_value_t value;
205
 
 
206
 
    if (!ctx) {
207
 
        pcilib_error("IPECamera imaging is not initialized");
208
 
        return PCILIB_ERROR_NOTINITIALIZED;
209
 
    }
210
 
    
211
 
    if (!ctx->firmware) {
212
 
        pcilib_warning("Unsupported version of firmware (%lu)", ctx->firmware);
213
 
        return 0;
214
 
    }
215
 
 
216
 
    pcilib = vctx->pcilib;
217
 
    control = ctx->control_reg;
218
 
    status = ctx->status_reg;
219
 
 
220
 
        // Set Reset bit to CMOSIS
221
 
    err = pcilib_write_register_by_id(pcilib, control, 0x1e4);
222
 
    if (err) {
223
 
        pcilib_error("Error setting FPGA reset bit");
224
 
        return err;
225
 
    }
226
 
    usleep(IPECAMERA_SLEEP_TIME);
227
 
 
228
 
        // Remove Reset bit to CMOSIS
229
 
    err = pcilib_write_register_by_id(pcilib, control, 0x1e1);
230
 
    if (err) {
231
 
        pcilib_error("Error reseting FPGA reset bit");
232
 
        return err;
233
 
    }
234
 
    usleep(IPECAMERA_SLEEP_TIME);
235
 
 
236
 
        // Special settings for CMOSIS v.2
237
 
    value = 01; err = pcilib_write_register_space(pcilib, "cmosis", 115, 1, &value);
238
 
    if (err) {
239
 
        pcilib_error("Error setting CMOSIS configuration");
240
 
        return err;
241
 
    }
242
 
    usleep(IPECAMERA_SLEEP_TIME);
243
 
 
244
 
    value = 07; err = pcilib_write_register_space(pcilib, "cmosis", 82, 1, &value);
245
 
    if (err) {
246
 
        pcilib_error("Error setting CMOSIS configuration");
247
 
        return err;
248
 
    }
249
 
    usleep(IPECAMERA_SLEEP_TIME);
250
 
 
251
 
        // Set default parameters
252
 
    err = pcilib_write_register_by_id(pcilib, control, IPECAMERA_IDLE);
253
 
    if (err) {
254
 
        pcilib_error("Error bringing FPGA in default mode");
255
 
        return err;
256
 
    }
257
 
 
258
 
    usleep(10000);
259
 
 
260
 
    err = pcilib_read_register_by_id(pcilib, status, &value);
261
 
    if (err) {
262
 
        pcilib_error("Error reading status register");
263
 
        return err;
264
 
    }
265
 
 
266
 
    if (value != IPECAMERA_GET_EXPECTED_STATUS(ctx)) {
267
 
        pcilib_error("Unexpected value (%lx) of status register, expected %lx", value, IPECAMERA_GET_EXPECTED_STATUS(ctx));
268
 
        return PCILIB_ERROR_VERIFY;
269
 
    }
270
 
 
271
 
    return 0;    
272
 
}
273
 
 
274
 
 
275
 
int ipecamera_start(pcilib_context_t *vctx, pcilib_event_t event_mask, pcilib_event_flags_t flags) {
276
 
    int i;
277
 
    int err = 0;
278
 
    ipecamera_t *ctx = (ipecamera_t*)vctx;
279
 
    pcilib_t *pcilib = vctx->pcilib;
280
 
    pcilib_register_value_t value;
281
 
    
282
 
    pthread_attr_t attr;
283
 
    struct sched_param sched;
284
 
    
285
 
    if (!ctx) {
286
 
        pcilib_error("IPECamera imaging is not initialized");
287
 
        return PCILIB_ERROR_NOTINITIALIZED;
288
 
    }
289
 
 
290
 
    if (!ctx->firmware) {
291
 
        pcilib_error("Unsupported version of firmware (%lu)", ctx->firmware);
292
 
        return PCILIB_ERROR_INVALID_REQUEST;
293
 
    }
294
 
    
295
 
    if (ctx->started) {
296
 
        pcilib_error("IPECamera grabbing is already started");
297
 
        return PCILIB_ERROR_INVALID_REQUEST;
298
 
    }
299
 
 
300
 
        // Allow readout and clean the FRAME_REQUEST mode if set for some reason
301
 
    GET_REG(control_reg, value);
302
 
    SET_REG(control_reg, value|IPECAMERA_READOUT_FLAG);
303
 
    usleep(IPECAMERA_SLEEP_TIME);
304
 
    if (value&0x1000) ctx->fr_mode = 1;
305
 
    else {
306
 
        ctx->fr_mode = 0;
307
 
        CHECK_STATUS_REG();
308
 
        if (err) return err;
309
 
    }
310
 
 
311
 
    ctx->event_id = 0;
312
 
    ctx->preproc_id = 0;
313
 
    ctx->reported_id = 0;
314
 
    ctx->buffer_pos = 0;
315
 
    ctx->parse_data = (flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY)?0:1;
316
 
    ctx->cur_size = 0;
317
 
    
318
 
    ctx->dim.width = IPECAMERA_WIDTH;
319
 
    ctx->dim.height = IPECAMERA_MAX_LINES;
320
 
//    GET_REG(n_lines_reg, ctx->dim.height);
321
 
    
322
 
    GET_REG(output_mode_reg, value);
323
 
    switch (value) {
324
 
     case IPECAMERA_MODE_16_CHAN_IO:
325
 
        ctx->cmosis_outputs = 16;
326
 
        break;
327
 
     case IPECAMERA_MODE_4_CHAN_IO:
328
 
        ctx->cmosis_outputs = 4;
329
 
        break;
330
 
     default:
331
 
        pcilib_error("IPECamera reporting invalid output_mode 0x%lx", value);
332
 
        return PCILIB_ERROR_INVALID_STATE;
333
 
    }
334
 
    
335
 
    ipecamera_compute_buffer_size(ctx, ctx->dim.height);
336
 
 
337
 
    ctx->raw_size = ctx->cur_raw_size;
338
 
    ctx->full_size = ctx->cur_full_size;
339
 
    ctx->padded_size = ctx->cur_padded_size;
340
 
 
341
 
    ctx->image_size = ctx->dim.width * ctx->dim.height;
342
 
 
343
 
    
344
 
    GET_REG(max_frames_reg, value);
345
 
    ctx->max_frames = value;
346
 
 
347
 
    ctx->buffer = malloc(ctx->padded_size * ctx->buffer_size);
348
 
    if (!ctx->buffer) {
349
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
350
 
        pcilib_error("Unable to allocate ring buffer (%lu bytes)", ctx->padded_size * ctx->buffer_size);
351
 
        return PCILIB_ERROR_MEMORY;
352
 
    }
353
 
 
354
 
    ctx->image = (ipecamera_pixel_t*)malloc(ctx->image_size * ctx->buffer_size * sizeof(ipecamera_pixel_t));
355
 
    if (!ctx->image) {
356
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
357
 
        pcilib_error("Unable to allocate image buffer (%lu bytes)", ctx->image_size * ctx->buffer_size * sizeof(ipecamera_pixel_t));
358
 
        return PCILIB_ERROR_MEMORY;
359
 
    }
360
 
 
361
 
    ctx->cmask = malloc(ctx->dim.height * ctx->buffer_size * sizeof(ipecamera_change_mask_t));
362
 
    if (!ctx->cmask) {
363
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
364
 
        pcilib_error("Unable to allocate change-mask buffer");
365
 
        return PCILIB_ERROR_MEMORY;
366
 
    }
367
 
 
368
 
    ctx->frame = (ipecamera_frame_t*)malloc(ctx->buffer_size * sizeof(ipecamera_frame_t));
369
 
    if (!ctx->frame) {
370
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
371
 
        pcilib_error("Unable to allocate frame-info buffer");
372
 
        return PCILIB_ERROR_MEMORY;
373
 
    }
374
 
    
375
 
    memset(ctx->frame, 0, ctx->buffer_size * sizeof(ipecamera_frame_t));
376
 
    
377
 
    for (i = 0; i < ctx->buffer_size; i++) {
378
 
        err = pthread_rwlock_init(&ctx->frame[i].mutex, NULL);
379
 
        if (err) break;
380
 
    }
381
 
 
382
 
    ctx->frame_mutex_destroy = i;
383
 
 
384
 
    if (err) {
385
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
386
 
        pcilib_error("Initialization of rwlock mutexes for frame synchronization has failed");
387
 
        return PCILIB_ERROR_FAILED;
388
 
    }
389
 
    
390
 
    ctx->ipedec = ufo_decoder_new(ctx->dim.height, ctx->dim.width, NULL, 0);
391
 
    if (!ctx->ipedec) {
392
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
393
 
        pcilib_error("Unable to initialize IPECamera decoder library");
394
 
        return PCILIB_ERROR_FAILED;
395
 
    }
396
 
 
397
 
    if (!err) {
398
 
        ctx->rdma = pcilib_find_dma_by_addr(vctx->pcilib, PCILIB_DMA_FROM_DEVICE, IPECAMERA_DMA_ADDRESS);
399
 
        if (ctx->rdma == PCILIB_DMA_ENGINE_INVALID) {
400
 
            err = PCILIB_ERROR_NOTFOUND;
401
 
            pcilib_error("The C2S channel of IPECamera DMA Engine (%u) is not found", IPECAMERA_DMA_ADDRESS);
402
 
        } else {
403
 
            err = pcilib_start_dma(vctx->pcilib, ctx->rdma, PCILIB_DMA_FLAGS_DEFAULT);
404
 
            if (err) {
405
 
                ctx->rdma = PCILIB_DMA_ENGINE_INVALID;
406
 
                pcilib_error("Failed to initialize C2S channel of IPECamera DMA Engine (%u)", IPECAMERA_DMA_ADDRESS);
407
 
            }
408
 
        }
409
 
    }
410
 
    
411
 
/*    
412
 
    if (!err) {
413
 
        ctx->wdma = pcilib_find_dma_by_addr(vctx->pcilib, PCILIB_DMA_TO_DEVICE, IPECAMERA_DMA_ADDRESS);
414
 
        if (ctx->wdma == PCILIB_DMA_ENGINE_INVALID) {
415
 
            err = PCILIB_ERROR_NOTFOUND;
416
 
            pcilib_error("The S2C channel of IPECamera DMA Engine (%u) is not found", IPECAMERA_DMA_ADDRESS);
417
 
        } else {
418
 
            err = pcilib_start_dma(vctx->pcilib, ctx->wdma, PCILIB_DMA_FLAGS_DEFAULT);
419
 
            if (err) {
420
 
                ctx->wdma = PCILIB_DMA_ENGINE_INVALID;
421
 
                pcilib_error("Failed to initialize S2C channel of IPECamera DMA Engine (%u)", IPECAMERA_DMA_ADDRESS);
422
 
            }
423
 
        }
424
 
    }
425
 
*/    
426
 
 
427
 
/*
428
 
    SET_REG(packet_len_reg, IPECAMERA_DMA_PACKET_LENGTH);
429
 
*/
430
 
 
431
 
    if (err) {
432
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
433
 
        return err;
434
 
    }
435
 
 
436
 
        // Clean DMA
437
 
#ifndef IPECAMERA_BUG_POSTPONED_READ
438
 
    err = pcilib_skip_dma(vctx->pcilib, ctx->rdma);
439
 
    if (err) {
440
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
441
 
        pcilib_error("Can't start grabbing, device continuously writes unexpected data using DMA engine");
442
 
        return err;
443
 
    }
444
 
#endif /* ! IPECAMERA_BUG_POSTPONED_READ */
445
 
 
446
 
    if (vctx->params.autostop.duration) {
447
 
        gettimeofday(&ctx->autostop.timestamp, NULL);
448
 
        ctx->autostop.timestamp.tv_usec += vctx->params.autostop.duration % 1000000;
449
 
        if (ctx->autostop.timestamp.tv_usec > 999999) {
450
 
            ctx->autostop.timestamp.tv_sec += 1 + vctx->params.autostop.duration / 1000000;
451
 
            ctx->autostop.timestamp.tv_usec -= 1000000;
452
 
        } else {
453
 
            ctx->autostop.timestamp.tv_sec += vctx->params.autostop.duration / 1000000;
454
 
        }
455
 
    }
456
 
    
457
 
    if (vctx->params.autostop.max_events) {
458
 
        ctx->autostop.evid = vctx->params.autostop.max_events;
459
 
    }
460
 
    
461
 
    if ((ctx->parse_data)&&(flags&PCILIB_EVENT_FLAG_PREPROCESS)) {
462
 
        ctx->n_preproc = pcilib_get_cpu_count();
463
 
        
464
 
            // it would be greate to detect hyperthreading cores and ban them
465
 
        switch (ctx->n_preproc) {
466
 
            case 1: break;
467
 
            case 2 ... 3: ctx->n_preproc -= 1; break;
468
 
            default: ctx->n_preproc -= 2; break;
469
 
        }
470
 
 
471
 
        if ((vctx->params.parallel.max_threads)&&(vctx->params.parallel.max_threads < ctx->n_preproc))
472
 
            ctx->n_preproc = vctx->params.parallel.max_threads;
473
 
 
474
 
        ctx->preproc = (ipecamera_preprocessor_t*)malloc(ctx->n_preproc * sizeof(ipecamera_preprocessor_t));
475
 
        if (!ctx->preproc) {
476
 
            ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
477
 
            pcilib_error("Unable to allocate memory for preprocessor contexts");
478
 
            return PCILIB_ERROR_MEMORY;
479
 
        }
480
 
 
481
 
        memset(ctx->preproc, 0, ctx->n_preproc * sizeof(ipecamera_preprocessor_t));
482
 
 
483
 
        err = pthread_mutex_init(&ctx->preproc_mutex, NULL);
484
 
        if (err) {
485
 
            ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
486
 
            pcilib_error("Failed to initialize event mutex");
487
 
            return PCILIB_ERROR_FAILED;
488
 
        }
489
 
        ctx->preproc_mutex_destroy = 1;
490
 
        
491
 
 
492
 
        ctx->run_preprocessors = 1;
493
 
        for (i = 0; i < ctx->n_preproc; i++) {
494
 
            ctx->preproc[i].i = i;
495
 
            ctx->preproc[i].ipecamera = ctx;
496
 
            err = pthread_create(&ctx->preproc[i].thread, NULL, ipecamera_preproc_thread, ctx->preproc + i);
497
 
            if (err) {
498
 
                err = PCILIB_ERROR_FAILED;
499
 
                break;
500
 
            } else {
501
 
                ctx->preproc[i].started = 1;
502
 
            }
503
 
        }
504
 
        
505
 
        if (err) {
506
 
            ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
507
 
            pcilib_error("Failed to schedule some of the preprocessor threads");
508
 
            return err;
509
 
        }
510
 
    } else {
511
 
        ctx->n_preproc = 0;
512
 
    }
513
 
 
514
 
    ctx->started = 1;
515
 
    ctx->run_reader = 1;
516
 
 
517
 
    pthread_attr_init(&attr);
518
 
 
519
 
    if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) {
520
 
        pcilib_warning("Can't schedule a real-time thread, you may consider running as root");
521
 
    } else {
522
 
        sched.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;  // Let 1 priority for something really critcial
523
 
        pthread_attr_setschedparam(&attr, &sched);
524
 
    }
525
 
 
526
 
    if (pthread_create(&ctx->rthread, &attr, &ipecamera_reader_thread, (void*)ctx)) {
527
 
        ctx->started = 0;
528
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
529
 
        err = PCILIB_ERROR_FAILED;
530
 
    }
531
 
    
532
 
    pthread_attr_destroy(&attr);    
533
 
 
534
 
    return err;
535
 
}
536
 
 
537
 
 
538
 
int ipecamera_stop(pcilib_context_t *vctx, pcilib_event_flags_t flags) {
539
 
    int i;
540
 
    int err;
541
 
    void *retcode;
542
 
    ipecamera_t *ctx = (ipecamera_t*)vctx;
543
 
 
544
 
    if (!ctx) {
545
 
        pcilib_error("IPECamera imaging is not initialized");
546
 
        return PCILIB_ERROR_NOTINITIALIZED;
547
 
    }
548
 
 
549
 
    if (flags&PCILIB_EVENT_FLAG_STOP_ONLY) {
550
 
        ctx->run_reader = 0;
551
 
        return 0;
552
 
    }
553
 
 
554
 
    if (ctx->started) {
555
 
        ctx->run_reader = 0;
556
 
        err = pthread_join(ctx->rthread, &retcode);
557
 
        if (err) pcilib_error("Error joining the reader thread");
558
 
    }
559
 
    
560
 
    if (ctx->preproc) {
561
 
        ctx->run_preprocessors = 0;
562
 
        
563
 
        for (i = 0; i < ctx->n_preproc; i++) {
564
 
            if (ctx->preproc[i].started) {
565
 
                pthread_join(ctx->preproc[i].thread, &retcode);
566
 
                ctx->preproc[i].started = 0;
567
 
            }
568
 
        }
569
 
 
570
 
        if (ctx->preproc_mutex_destroy) {
571
 
            pthread_mutex_destroy(&ctx->preproc_mutex);
572
 
            ctx->preproc_mutex_destroy = 0;
573
 
        }
574
 
        
575
 
        free(ctx->preproc);
576
 
        ctx->preproc = NULL;
577
 
    }
578
 
    
579
 
    if (ctx->frame_mutex_destroy) {
580
 
        for (i = 0; i < ctx->frame_mutex_destroy; i++) {
581
 
            pthread_rwlock_destroy(&ctx->frame[i].mutex);
582
 
        }
583
 
        ctx->frame_mutex_destroy = 0;
584
 
    }
585
 
    
586
 
 
587
 
    if (ctx->wdma != PCILIB_DMA_ENGINE_INVALID) {
588
 
        pcilib_stop_dma(vctx->pcilib, ctx->wdma, PCILIB_DMA_FLAGS_DEFAULT);
589
 
        ctx->wdma = PCILIB_DMA_ENGINE_INVALID;
590
 
    }
591
 
 
592
 
    if (ctx->rdma != PCILIB_DMA_ENGINE_INVALID) {
593
 
        pcilib_stop_dma(vctx->pcilib, ctx->rdma, PCILIB_DMA_FLAGS_DEFAULT);
594
 
        ctx->rdma = PCILIB_DMA_ENGINE_INVALID;
595
 
    }
596
 
 
597
 
    while (ctx->streaming) {
598
 
        usleep(IPECAMERA_NOFRAME_SLEEP);
599
 
    }
600
 
 
601
 
    if (ctx->ipedec) {
602
 
        ufo_decoder_free(ctx->ipedec);
603
 
        ctx->ipedec = NULL;
604
 
    }
605
 
 
606
 
    if (ctx->frame) {
607
 
        free(ctx->frame);
608
 
        ctx->frame = NULL;
609
 
    }
610
 
 
611
 
    if (ctx->cmask) {
612
 
        free(ctx->cmask);
613
 
        ctx->cmask = NULL;
614
 
    }
615
 
 
616
 
    if (ctx->image) {
617
 
        free(ctx->image);
618
 
        ctx->image = NULL;
619
 
    }
620
 
 
621
 
    if (ctx->buffer) {
622
 
        free(ctx->buffer);
623
 
        ctx->buffer = NULL;
624
 
    }
625
 
    
626
 
 
627
 
    memset(&ctx->autostop, 0, sizeof(ipecamera_autostop_t));
628
 
 
629
 
    ctx->event_id = 0;
630
 
    ctx->reported_id = 0;
631
 
    ctx->buffer_pos = 0; 
632
 
    ctx->started = 0;
633
 
 
634
 
    return 0;
635
 
}
636
 
 
637
 
 
638
 
int ipecamera_trigger(pcilib_context_t *vctx, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
639
 
    int err = 0;
640
 
    pcilib_register_value_t value;
641
 
 
642
 
    ipecamera_t *ctx = (ipecamera_t*)vctx;
643
 
    pcilib_t *pcilib = vctx->pcilib;
644
 
 
645
 
    if (!ctx) {
646
 
        pcilib_error("IPECamera imaging is not initialized");
647
 
        return PCILIB_ERROR_NOTINITIALIZED;
648
 
    }
649
 
 
650
 
    if (!ctx->firmware) {
651
 
        pcilib_error("Unsupported version of firmware (%lu)", ctx->firmware);
652
 
        return PCILIB_ERROR_INVALID_REQUEST;
653
 
    }
654
 
 
655
 
    pcilib_sleep_until_deadline(&ctx->next_trigger);
656
 
 
657
 
    GET_REG(num_frames_reg, value);
658
 
    if (value == ctx->max_frames) {
659
 
        return PCILIB_ERROR_BUSY;
660
 
    }
661
 
/*
662
 
    do {
663
 
        usleep(10);
664
 
        GET_REG(status3_reg, value);
665
 
    } while (value&0x20000000);
666
 
*/
667
 
 
668
 
    GET_REG(control_reg, value); 
669
 
    SET_REG(control_reg, value|IPECAMERA_FRAME_REQUEST);
670
 
    usleep(IPECAMERA_WAIT_FRAME_RCVD_TIME);
671
 
    //CHECK_REG(status_reg, IPECAMERA_EXPECTED_STATUS);
672
 
    SET_REG(control_reg, value);
673
 
 
674
 
 
675
 
    pcilib_calc_deadline(&ctx->next_trigger, IPECAMERA_NEXT_FRAME_DELAY);
676
 
 
677
 
    return 0;
678
 
}