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
|
#define _IPECAMERA_IMAGE_C
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <assert.h>
#include "../tools.h"
#include "../error.h"
#include "pcilib.h"
#include "model.h"
#include "image.h"
#define IPECAMERA_SLEEP_TIME 250000
struct ipecamera_s {
pcilib_t *pcilib;
char *data;
size_t size;
pcilib_callback_t cb;
void *cb_user;
int width;
int height;
pcilib_event_id_t event_id;
pcilib_register_t control_reg, status_reg;
pcilib_register_t start_reg, end_reg;
pcilib_register_t lines_reg;
pcilib_register_t exposure_reg;
void *buffer;
};
#define FIND_REG(var, bank, name) \
ctx->var = pcilib_find_register(pcilib, bank, name); \
if (ctx->var == PCILIB_REGISTER_INVALID) { \
err = -1; \
pcilib_error("Unable to find a %s register", name); \
}
#define GET_REG(reg, var) \
err = pcilib_read_register_by_id(pcilib, ctx->reg, &var); \
if (err) { \
pcilib_error("Error reading %s register", ipecamera_registers[ctx->reg].name); \
return err; \
}
#define SET_REG(reg, val) \
err = pcilib_write_register_by_id(pcilib, ctx->reg, val); \
if (err) { \
pcilib_error("Error writting %s register", ipecamera_registers[ctx->reg].name); \
return err; \
}
#define CHECK_REG(reg, check) \
err = pcilib_read_register_by_id(pcilib, ctx->reg, &value); \
if (err) { \
pcilib_error("Error reading %s register", ipecamera_registers[ctx->reg].name); \
return err; \
} \
if (!(check)) { \
pcilib_error("Unexpected value (%li) of register %s", value, ipecamera_registers[ctx->reg].name); \
return err; \
}
void *ipecamera_init(pcilib_t *pcilib) {
int err = 0;
ipecamera_t *ctx = malloc(sizeof(ipecamera_t));
if (ctx) {
ctx->pcilib = pcilib;
ctx->data = pcilib_resolve_data_space(pcilib, 0, &ctx->size);
if (!ctx->data) {
err = -1;
pcilib_error("Unable to resolve the data space");
}
ctx->buffer = malloc(1088 * 2048 * 2);
if (!ctx->buffer) {
err = -1;
pcilib_error("Unable to allocate ring buffer");
}
FIND_REG(status_reg, "fpga", "status");
FIND_REG(control_reg, "fpga", "control");
FIND_REG(start_reg, "fpga", "start_address");
FIND_REG(end_reg, "fpga", "end_address");
FIND_REG(lines_reg, "cmosis", "number_lines");
FIND_REG(exposure_reg, "cmosis", "exp_time");
if (err) {
free(ctx);
return NULL;
}
}
return (void*)ctx;
}
void ipecamera_free(void *vctx) {
if (vctx) {
ipecamera_t *ctx = (ipecamera_t*)vctx;
if (ctx->buffer) free(ctx->buffer);
free(ctx);
}
}
int ipecamera_reset(void *vctx) {
int err;
pcilib_t *pcilib;
ipecamera_t *ctx = (ipecamera_t*)vctx;
pcilib_register_t control, status;
pcilib_register_value_t value;
if (!ctx) {
pcilib_error("IPECamera imaging is not initialized");
return PCILIB_ERROR_NOTINITIALIZED;
}
pcilib = ctx->pcilib;
control = ctx->control_reg;
status = ctx->status_reg;
// Set Reset bit to CMOSIS
err = pcilib_write_register_by_id(pcilib, control, 5);
if (err) {
pcilib_error("Error setting CMOSIS reset bit");
return err;
}
usleep(IPECAMERA_SLEEP_TIME);
// Remove Reset bit to CMOSIS
err = pcilib_write_register_by_id(pcilib, control, 1);
if (err) {
pcilib_error("Error reseting CMOSIS reset bit");
return err;
}
usleep(IPECAMERA_SLEEP_TIME);
// Special settings for CMOSIS v.2
value = 01; err = pcilib_write_register_space(pcilib, "cmosis", 115, 1, &value);
if (err) {
pcilib_error("Error setting CMOSIS configuration");
return err;
}
usleep(IPECAMERA_SLEEP_TIME);
value = 07; err = pcilib_write_register_space(pcilib, "cmosis", 82, 1, &value);
if (err) {
pcilib_error("Error setting CMOSIS configuration");
return err;
}
usleep(IPECAMERA_SLEEP_TIME);
// This is temporary for verification purposes
memset(ctx->data, 0, ctx->size);
err = pcilib_read_register_by_id(pcilib, status, &value);
if (err) {
pcilib_error("Error reading status register");
return err;
}
if (value != 0x0849FFFF) {
pcilib_error("Unexpected value (%lx) of status register", value);
return PCILIB_ERROR_VERIFY;
}
return 0;
}
int ipecamera_start(void *vctx, pcilib_event_t event_mask, pcilib_callback_t cb, void *user) {
ipecamera_t *ctx = (ipecamera_t*)vctx;
if (!ctx) {
pcilib_error("IPECamera imaging is not initialized");
return PCILIB_ERROR_NOTINITIALIZED;
}
ctx->cb = cb;
ctx->cb_user = user;
ctx->event_id = 0;
ctx->width = 1270;
ctx->height = 1088; //GET_REG(lines_reg, lines);
// allocate memory
return 0;
}
int ipecamera_stop(void *vctx) {
ipecamera_t *ctx = (ipecamera_t*)vctx;
if (!ctx) {
pcilib_error("IPECamera imaging is not initialized");
return PCILIB_ERROR_NOTINITIALIZED;
}
return 0;
}
static int ipecamera_get_line(ipecamera_t *ctx, int line) {
int err;
pcilib_t *pcilib = ctx->pcilib;
pcilib_register_value_t ptr, size, value;
ipecamera_reset((void*)ctx);
SET_REG(lines_reg, 1);
SET_REG(control_reg, 149);
usleep(IPECAMERA_SLEEP_TIME);
CHECK_REG(status_reg, 0x0849FFFF);
GET_REG(start_reg, ptr);
GET_REG(end_reg, size);
size -= ptr;
printf("%i: %i %i\n", line, ptr, size);
SET_REG(control_reg, 141);
usleep(IPECAMERA_SLEEP_TIME);
CHECK_REG(status_reg, 0x0849FFFF);
}
static int ipecamera_get_image(ipecamera_t *ctx) {
int err;
int i;
pcilib_t *pcilib = ctx->pcilib;
for (i = 0; i < 1088; i++) {
ipecamera_get_line(ctx, i);
}
ctx->event_id++;
}
int ipecamera_trigger(void *vctx, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
int err;
pcilib_t *pcilib;
ipecamera_t *ctx = (ipecamera_t*)vctx;
if (!ctx) {
pcilib_error("IPECamera imaging is not initialized");
return PCILIB_ERROR_NOTINITIALIZED;
}
err = ipecamera_get_image(ctx);
if (!err) err = ctx->cb(event, ctx->event_id, ctx->cb_user);
return err;
}
void* ipecamera_get(void *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t *size) {
if (size) *size = ctx->width * ctx->height * 2;
return ctx->buffer;
}
int ipecamera_return(void *ctx, pcilib_event_id_t event_id) {
return 0;
}
|