forked from biomimetics/imageproc-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcam.c
More file actions
446 lines (346 loc) · 12.1 KB
/
cam.c
File metadata and controls
446 lines (346 loc) · 12.1 KB
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
439
440
441
442
443
444
445
446
/*
* Copyright (c) 2011-2012, Regents of the University of California
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the University of California, Berkeley nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
* Image capture device interface
*
* by Humphrey Hu
*
* v2.0
*
* Revisions:
* Humphrey Hu 2011-10-26 Initial implementation
* 2012-01-31 Release
* 2012-02-16 Slight restructuring
* 2012-02-21 Changed to use pooled frames
* 2012-06-02 Changed to static frames
*/
#include "timer.h"
#include "counter.h"
#include "ovcam.h"
#include "utils.h"
#include "carray.h"
#include <stdlib.h>
#include <string.h>
#include "cam.h"
#include "sys_clock.h"
#include "ov7660.h"
// Default camera capture timings for QQVGA no subsampling, 25 fps
#define ROW_ROW_TIME (32)
#define VSYNC_ROW_TIME (12800)
#define ROW_VSYNC_TIME (2800)
#define VSYNC_VSYNC_TIME (25000)
// Amount of time before an event to trigger timer
#define ROW_ROW_OFFSET (6) // 384 cycles
#define VSYNC_ROW_OFFSET (6) // 384 cycles
#define ROW_VSYNC_OFFSET (10) // 640 cycles
#define VSYNC_VSYNC_OFFSET (8) // 512 cycles
// The timer states describe what the timer is waiting for
// i.e. VSYNC state means timer is waiting for VSYNC event
// The NOT_SYNC state means that the timer has not been synchronized
// yet and is not ready for operation.
typedef enum {
CT_NOT_SYNC,
CT_WAIT_VSYNC,
CT_WAIT_ROW,
CT_SKIP_FRAME,
}CTimerState;
// ==== Function Stubs ========================================================
static void setupTimer7(void);
void _T7Interrupt(void);
void camCaptureRow(void);
static void processRow(void);
static CamFrame getEmptyFrame(void);
static void enqueueEmptyFrame(CamFrame frame);
static CamFrame getOldestFullFrame(void);
static void enqueueFullFrame(CamFrame frame);
// ==== Static Variables ======================================================
// Driver validity
static unsigned char is_ready;
// Asynchronous capture state
static CTimerState ct_state = CT_NOT_SYNC;
static unsigned long frame_start, frame_period;
// Asynchronous capture timing parameters
static unsigned int row_row_time = ROW_ROW_TIME;
static unsigned int vsync_row_time = VSYNC_ROW_TIME;
static unsigned int row_vsync_time = ROW_VSYNC_TIME;
static unsigned int vsync_vsync_time = VSYNC_VSYNC_TIME;
// Protected counters
static Counter frame_counter;
static Counter row_counter;
// Row capture buffer
static unsigned char row_buff[NATIVE_IMAGE_COLS];
// Frame buffering variables
static CamFrame current_frame;
static unsigned int next_row_index;
static unsigned char has_new_frame, frame_started;
static CircArray empty_frame_pool, full_frame_pool;
// Driver config'd function pointers
static CamIrqHandler irq_handler;
static CamRowGetter row_getter;
static CamFrameWaiter frame_waiter;
// ==== Public functions ======================================================
void camSetup(CamFrame frames, unsigned int num_frames) {
unsigned int i;
setupTimer7(); // Set up timer peripheral
next_row_index = 0;
has_new_frame = 0;
current_frame = NULL;
frame_counter = cntrCreate(); // Frame counter allocation
if(frame_counter == NULL) { return; }
row_counter = cntrCreate(); // Row counter allocation
if(row_counter == NULL) { return; }
empty_frame_pool = carrayCreate(num_frames); // Initialize frame pool
if(empty_frame_pool == NULL) { return; }
full_frame_pool = carrayCreate(num_frames); // Initialize frame pool
if(full_frame_pool == NULL) { return; }
for(i = 0; i < num_frames; i++) {
camReturnFrame(&frames[i]);
}
ovcamSetup(); // Set up camera device
irq_handler = NULL; // Set up function pointers
row_getter = &ovcamGetPixels;
frame_waiter = &ovcamWaitForNewFrame;
frame_started = 0;
is_ready = 1;
camRunCalib(); // Measure timing parameters
}
void camGetParams(CamParam params) {
if(params == NULL) { return; }
params->type = 0; // Not implemented yet!
params->active = is_ready;
params->frame_start = frame_start;
params->frame_period = frame_period;
}
// Syncs the timer with the frame start event and begins the
// capture process.
void camStart(void) {
if(!is_ready) { return; }
DisableIntT7; // Disable interrupt while syncing
frame_waiter(); // Avoid clock drift
PR7 = VSYNC_ROW_TIME; // Set wait time
WriteTimer7(0); // Reset timer
frame_start = sclockGetGlobalTicks();
cntrSet(row_counter, 0); // Reset row counter
cntrSet(frame_counter, 0); // Reset frame counter
ct_state = CT_WAIT_ROW; // Wait for first row
EnableIntT7; // Re-enable interrupt
}
void camStop(void) {
DisableIntT7;
}
// Measures camera timing parameters
void camRunCalib(void) {
unsigned int tic, capture_time, i;
unsigned long t1, t2;
if(!is_ready) { return; }
// Approximately 8*pixels cycles per row
// Using 64:1 prescale
capture_time = (NATIVE_IMAGE_COLS)/(8);
DisableIntT7;
// VSYNC to VSYNC timing
frame_waiter();
WriteTimer7(0);
t1 = sclockGetGlobalTicks();
frame_waiter();
tic = ReadTimer7();
t2 = sclockGetGlobalTicks();
vsync_vsync_time = tic - VSYNC_VSYNC_OFFSET;
frame_period = t2 - t1;
// VSYNC to row timing
frame_waiter();
WriteTimer7(0);
for(i = 0; i < WINDOW_START_ROW; i++) {
camCaptureRow();
}
camCaptureRow();
tic = ReadTimer7();
vsync_row_time = tic - capture_time - VSYNC_ROW_OFFSET;
// row to row timing
frame_waiter();
camCaptureRow();
WriteTimer7(0);
for(i = 0; i < DS_ROW; i++) {
camCaptureRow();
}
tic = ReadTimer7();
row_row_time = tic - capture_time - ROW_ROW_OFFSET;
// row to VSYNC timing
frame_waiter();
for(i = 0; i < WINDOW_END_ROW; i++) {
camCaptureRow();
}
WriteTimer7(0);
frame_waiter();
tic = ReadTimer7();
row_vsync_time = tic - ROW_VSYNC_OFFSET;
}
void camSetIrqHandler(CamIrqHandler irq) {
irq_handler = irq;
}
unsigned char camHasNewFrame(void) {
return has_new_frame;
}
CamFrame camGetFrame(void) {
return getOldestFullFrame();
}
void camReturnFrame(CamFrame frame) {
if(frame == NULL) { return; }
enqueueEmptyFrame(frame);
}
unsigned int camGetFrameNum(void) {
return cntrRead(frame_counter);
}
// =========== Private Functions ==============================================
// Interrupt handler for Timer 7
// Syncs frame timings and captures camera rows.
void __attribute__((interrupt, no_auto_psv)) _T7Interrupt(void) {
if(ct_state == CT_WAIT_VSYNC) {
frame_waiter(); // Avoid clock drift
WriteTimer7(0); // Reset timer
cntrIncrement(frame_counter);
cntrSet(row_counter, 0); // Reset row counter
if(cntrRead(frame_counter) % DS_FRAME == 0) {
ct_state = CT_WAIT_ROW; // Wait for first row
PR7 = vsync_row_time; // Set wait time
} else {
// ct_state == CT_WAIT_VSYNC (unchanged)
PR7 = vsync_vsync_time; // Wait for next frame
}
frame_start = sclockGetGlobalTicks();
} else if(ct_state == CT_WAIT_ROW) {
camCaptureRow(); // Capture row
WriteTimer7(0); // Reset timer
processRow(); // Process row
cntrAdd(row_counter, DS_ROW); // Increment row count
// Transition if captured last row
if(cntrRead(row_counter) >= NATIVE_IMAGE_ROWS) {
ct_state = CT_WAIT_VSYNC;
PR7 = row_vsync_time;
if(irq_handler != NULL) {
irq_handler(CAM_IRQ_FRAME_DONE);
}
} else { // Else wait for next row
PR7 = row_row_time;
if(irq_handler != NULL) {
irq_handler(CAM_IRQ_ROW_DONE);
}
}
}
_T7IF = 0;
}
void camCaptureRow(void) {
CRITICAL_SECTION_START;
row_getter(row_buff, NATIVE_IMAGE_COLS);
CRITICAL_SECTION_END;
}
void processRow(void) {
unsigned int i, j, k, acc;
unsigned char *src_data, *dst_data;
if(cntrRead(row_counter) == 0) {
if(current_frame == NULL) {
current_frame = getEmptyFrame(); // Load new frame
}
}
if(current_frame == NULL) { return; }
dst_data = current_frame->pixels[next_row_index]; // Write into current frame
src_data = row_buff;
k = WINDOW_START_COL;
for(i = 0; i < DS_IMAGE_COLS; i++) {
acc = 0;
for(j = 0; j < DS_COL; j++) {
acc += row_buff[k++];
}
dst_data[i] = acc/DS_COL;
}
next_row_index++;
// If all rows are filled, add the frame to the full frame buffer
if(next_row_index >= DS_IMAGE_ROWS) {
current_frame->frame_num = cntrRead(frame_counter); // write frame number
current_frame->timestamp = sclockGetLocalTicks();
enqueueFullFrame(current_frame); // Add to output queue
current_frame = NULL;
next_row_index = 0;
}
}
/**
* Get the next available empty frame. If no frames are available, automatically
* dequeues and returns the oldest full frame.
*
* @return Next available frame for writing
*/
static CamFrame getEmptyFrame(void) {
CamFrame frame;
frame = carrayPopHead(empty_frame_pool);
if(frame == NULL) {
frame = getOldestFullFrame(); // If no more empty frames, get oldest full
}
return frame;
}
/**
* Enqueues a frame for writing into.
*
* @param frame CamFrame object to enqueue
*/
static void enqueueEmptyFrame(CamFrame frame) {
carrayAddTail(empty_frame_pool, frame);
}
/**
* Returns the oldest full frame in the outgoing buffer.
*
* @return Oldest full frame object
*/
static CamFrame getOldestFullFrame(void) {
CamFrame frame;
frame = carrayPopHead(full_frame_pool);
if(carrayIsEmpty(full_frame_pool)) {
has_new_frame = 0;
}
return frame;
}
/**
* Enqueues a full frame object in the outgoing buffer.
*
* @param frame CamFrame object to enqueue
*/
static void enqueueFullFrame(CamFrame frame) {
carrayAddTail(full_frame_pool, frame);
has_new_frame = 1;
}
// Camera acquisition timer setup
static void setupTimer7(void) {
unsigned int con_reg;
con_reg = T7_ON & // Enable module
T7_IDLE_CON & // Continue running when idle
T7_GATE_OFF & // Time accumulation disable
T7_PS_1_64 & // Prescale 1:64
T7_SOURCE_INT; // Internal clock
_T7IF = 0;
OpenTimer7(con_reg, 0); // Configure timer
ConfigIntTimer7(T7_INT_PRIOR_6 & T7_INT_OFF);
}