/alps/ipecamera

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

« back to all changes in this revision

Viewing changes to base.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-04-27 00:28:57 UTC
  • Revision ID: csa@suren.me-20150427002857-82fk6r3e8rfgy4wr
First stand-alone ipecamera implementation

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
#include <ufodecode.h>
14
14
 
15
 
#include "../tools.h"
16
 
#include "../error.h"
17
 
#include "../event.h"
 
15
#include <pcilib.h>
 
16
#include <pcilib/tools.h>
 
17
#include <pcilib/error.h>
 
18
#include <pcilib/event.h>
18
19
 
19
 
#include "pcilib.h"
20
20
#include "private.h"
21
21
#include "model.h"
22
22
#include "reader.h"
24
24
#include "data.h"
25
25
 
26
26
 
27
 
#include "dma/nwl.h"
28
 
 
29
27
#define FIND_REG(var, bank, name)  \
30
28
        ctx->var = pcilib_find_register(pcilib, bank, name); \
31
29
        if (ctx->var ==  PCILIB_REGISTER_INVALID) { \
38
36
    if (!err) { \
39
37
        err = pcilib_read_register_by_id(pcilib, ctx->reg, &var); \
40
38
        if (err) { \
41
 
            pcilib_error("Error reading %s register", ipecamera_registers[ctx->reg].name); \
 
39
            pcilib_error("Error reading %s register", model_info->registers[ctx->reg].name); \
42
40
        } \
43
41
    }
44
42
 
46
44
    if (!err) { \
47
45
        err = pcilib_write_register_by_id(pcilib, ctx->reg, val); \
48
46
        if (err) { \
49
 
            pcilib_error("Error writting %s register", ipecamera_registers[ctx->reg].name); \
 
47
            pcilib_error("Error writting %s register", model_info->registers[ctx->reg].name); \
50
48
        } \
51
49
    }
52
50
 
54
52
    if (!err) { \
55
53
        err = pcilib_read_register_by_id(pcilib, ctx->reg, &value); \
56
54
        if (err) { \
57
 
            pcilib_error("Error reading %s register", ipecamera_registers[ctx->reg].name); \
 
55
            pcilib_error("Error reading %s register", model_info->registers[ctx->reg].name); \
58
56
        } \
59
57
        if (value != check) { \
60
 
            pcilib_error("Unexpected value (0x%lx) of register %s", value, ipecamera_registers[ctx->reg].name); \
 
58
            pcilib_error("Unexpected value (0x%lx) of register %s", value, model_info->registers[ctx->reg].name); \
61
59
            err = PCILIB_ERROR_INVALID_DATA; \
62
60
        } \
63
61
    }
64
62
 
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))
 
63
#define CHECK_STATUS()
 
64
            //CHECK_REG(status_reg, IPECAMERA_GET_EXPECTED_STATUS(ctx))
67
65
 
68
66
#define CHECK_VALUE(value, val) \
69
67
    if ((!err)&&(value != val)) { \
81
79
pcilib_context_t *ipecamera_init(pcilib_t *pcilib) {
82
80
    int err = 0; 
83
81
    
 
82
    const pcilib_model_description_t *model_info = pcilib_get_model_description(pcilib);
 
83
 
84
84
    ipecamera_t *ctx = malloc(sizeof(ipecamera_t));
85
85
 
86
86
    if (ctx) {
115
115
 
116
116
        GET_REG(firmware_version_reg, value);
117
117
        switch (value) {
118
 
         case 4:
119
118
         case 5:
120
119
            ctx->firmware = value;
121
120
            break;
122
121
         default:
123
 
//          pcilib_error("Unsupported version of firmware (%lu)", value);
124
 
            ctx->firmware = 0;
 
122
            ctx->firmware = 5;
 
123
            pcilib_warning("Unsupported version of firmware (%lu)", value);
125
124
        }
126
125
 
127
126
#ifdef IPECAMERA_BUG_POSTPONED_READ
153
152
}
154
153
 
155
154
pcilib_dma_context_t *ipecamera_init_dma(pcilib_context_t *vctx) {
156
 
#ifdef IPECAMERA_DMA_R3
157
155
    ipecamera_t *ctx = (ipecamera_t*)vctx;
158
 
#endif
159
156
    
160
 
    pcilib_model_description_t *model_info = pcilib_get_model_description(vctx->pcilib);
161
 
    if ((!model_info->dma_api)||(!model_info->dma_api->init)) {
 
157
    const pcilib_model_description_t *model_info = pcilib_get_model_description(vctx->pcilib);
 
158
    if ((!model_info->dma)||(!model_info->dma->api)||(!model_info->dma->api->init)) {
162
159
        pcilib_error("The DMA engine is not configured in model");
163
160
        return NULL;
164
161
    }
165
162
 
166
 
 
167
 
#ifdef IPECAMERA_DMA_R3
168
163
    if (ctx->firmware) {
169
 
        return model_info->dma_api->init(vctx->pcilib, PCILIB_NWL_MODIFICATION_IPECAMERA, NULL);
 
164
        return model_info->dma->api->init(vctx->pcilib, NULL, NULL);
170
165
    } else {
171
 
        return model_info->dma_api->init(vctx->pcilib, PCILIB_DMA_MODIFICATION_DEFAULT, NULL);
 
166
        return model_info->dma->api->init(vctx->pcilib, "pci", NULL);
172
167
    }
173
 
#else
174
 
    return model_info->dma_api->init(vctx->pcilib, PCILIB_DMA_MODIFICATION_DEFAULT, NULL);
175
 
#endif
176
168
}
177
169
 
178
170
 
258
250
 
259
251
    usleep(10000);
260
252
 
261
 
    err = pcilib_read_register_by_id(pcilib, status, &value);
 
253
 
 
254
    CHECK_STATUS();
262
255
    if (err) {
263
 
        pcilib_error("Error reading status register");
264
 
        return err;
265
 
    }
266
 
 
267
 
    if (value != IPECAMERA_GET_EXPECTED_STATUS(ctx)) {
268
 
        pcilib_error("Unexpected value (%lx) of status register, expected %lx", value, IPECAMERA_GET_EXPECTED_STATUS(ctx));
 
256
        err = pcilib_read_register_by_id(pcilib, status, &value);
 
257
 
 
258
        if (err) pcilib_error("Error reading status register");
 
259
        else pcilib_error("Camera returns unexpected status (status: %lx)", value);
 
260
 
269
261
        return PCILIB_ERROR_VERIFY;
270
262
    }
271
263
 
 
264
    // DS: Get rid of pending DMA data
 
265
 
272
266
    return 0;    
273
267
}
274
268
 
276
270
int ipecamera_start(pcilib_context_t *vctx, pcilib_event_t event_mask, pcilib_event_flags_t flags) {
277
271
    int i;
278
272
    int err = 0;
 
273
 
279
274
    ipecamera_t *ctx = (ipecamera_t*)vctx;
280
275
    pcilib_t *pcilib = vctx->pcilib;
281
276
    pcilib_register_value_t value;
282
277
    
 
278
    const pcilib_model_description_t *model_info = pcilib_get_model_description(pcilib);
 
279
 
283
280
    pthread_attr_t attr;
284
281
    struct sched_param sched;
285
282
    
298
295
        return PCILIB_ERROR_INVALID_REQUEST;
299
296
    }
300
297
 
301
 
        // Allow readout and clean the FRAME_REQUEST mode if set for some reason
302
 
    usleep(IPECAMERA_SLEEP_TIME);
303
 
    if (value&0x1000) ctx->fr_mode = 1;
304
 
    else {
305
 
        ctx->fr_mode = 0;
306
 
        
307
 
//      CHECK_STATUS_REG();
308
 
//      if (err) return err;
309
 
    }
310
 
 
311
298
    ctx->event_id = 0;
312
299
    ctx->preproc_id = 0;
313
300
    ctx->reported_id = 0;
642
629
    ipecamera_t *ctx = (ipecamera_t*)vctx;
643
630
    pcilib_t *pcilib = vctx->pcilib;
644
631
 
 
632
    const pcilib_model_description_t *model_info = pcilib_get_model_description(pcilib);
 
633
 
645
634
    if (!ctx) {
646
635
        pcilib_error("IPECamera imaging is not initialized");
647
636
        return PCILIB_ERROR_NOTINITIALIZED;
685
674
    GET_REG(control_reg, value); 
686
675
    SET_REG(control_reg, value|IPECAMERA_FRAME_REQUEST);
687
676
    usleep(IPECAMERA_WAIT_FRAME_RCVD_TIME);
688
 
    //CHECK_REG(status_reg, IPECAMERA_EXPECTED_STATUS);
 
677
    //DS: CHECK_REG(status_reg, IPECAMERA_EXPECTED_STATUS);
689
678
    SET_REG(control_reg, value);
690
679
 
691
680
        // We need to compute it differently, on top of that add exposure time and the time FPGA takes to read frame from CMOSIS