-
Notifications
You must be signed in to change notification settings - Fork 0
/
arducam.c
330 lines (299 loc) · 9.44 KB
/
arducam.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
/*
* File: arducam.c
* Author: Peter Thornton
* Purpose: Functions to interface with Arducam 2MP Plus camera
* Includes both I2C interface for OV2640 registers, and
* SPI interface for image capture and retrieval
* Created on: 17 November 2019
*/
#include "xc.h"
#include "clock.h"
#include "i2c.h"
#include "spi.h"
#include "arducam.h"
#define ARDU_ADDR 0x30 // address for Arducam image chip OV2640 on I2C bus
#define ARDU_DELAY 1 // 1 msec delay after a stop to allow
// device state to update
// common I2C address values used by all commands
int ardu_add_w = ARDU_ADDR << 1; // Arducam address with write flag set (0)
int ardu_add_r = (ARDU_ADDR << 1) | 0b1; // Arducam address with read flag set (1)
// timer variables
long wait;
// use the I2C interface to write register values to the OV2640 chip on camera #1
void write_ov2640_reg_cam1(int reg, int val)
{
// ensure i2c bus is in idle state
idle_i2c1();
// initiate an i2c bus start, and wait for it to complete
start_i2c1();
// transmit device address byte with write flag
transmit_i2c1(ardu_add_w);
// transmit the register ID
transmit_i2c1(reg);
// transmit the register value
transmit_i2c1(val);
// initiate a stop and wait for it to complete
stop_i2c1(ARDU_DELAY);
}
// use the I2C interface to write register values to the OV2640 chip on camera #2
void write_ov2640_reg_cam2(int reg, int val)
{
// ensure i2c bus is in idle state
idle_i2c2();
// initiate an i2c bus start, and wait for it to complete
start_i2c2();
// transmit device address byte with write flag
transmit_i2c2(ardu_add_w);
// transmit the register ID
transmit_i2c2(reg);
// transmit the register value
transmit_i2c2(val);
// initiate a stop and wait for it to complete
stop_i2c2(ARDU_DELAY);
}
// use the I2C interface to read register values from the OV2640 chip on camera #1
void read_ov2640_reg_cam1(int reg, int *val)
{
// ensure i2c bus is in idle state
idle_i2c1();
// initiate an i2c bus start, and wait for it to complete
start_i2c1();
// transmit device address byte with write flag
transmit_i2c1(ardu_add_w);
// transmit the register ID
transmit_i2c1(reg);
// initiate a stop and wait for it to complete
stop_i2c1(ARDU_DELAY);
// ensure i2c bus is in idle state
idle_i2c1();
// initiate an i2c bus start, and wait for it to complete
start_i2c1();
// transmit device address byte with read flag
transmit_i2c1(ardu_add_r);
// receive the register value and acknowledge with NACK
receive_i2c1_nack(val);
// initiate a stop and wait for it to complete
stop_i2c1(ARDU_DELAY);
}
// use the I2C interface to read register values from the OV2640 chip on camera #2
void read_ov2640_reg_cam2(int reg, int *val)
{
// ensure i2c bus is in idle state
idle_i2c2();
// initiate an i2c bus start, and wait for it to complete
start_i2c2();
// transmit device address byte with write flag
transmit_i2c2(ardu_add_w);
// transmit the register ID
transmit_i2c2(reg);
// initiate a stop and wait for it to complete
stop_i2c2(ARDU_DELAY);
// ensure i2c bus is in idle state
idle_i2c2();
// initiate an i2c bus start, and wait for it to complete
start_i2c2();
// transmit device address byte with read flag
transmit_i2c2(ardu_add_r);
// receive the register value and acknowledge with NACK
receive_i2c2_nack(val);
// initiate a stop and wait for it to complete
stop_i2c2(ARDU_DELAY);
}
// reset the OV2640 registers to factory defaults on camera #1
void reset_ov2640_regs_cam1()
{
// switch to second register bank, set bit 7 of register 0x12 (COM7)
// to perform a device reset of the OV2640 chip
write_ov2640_reg_cam1(0xff, 0x01);
write_ov2640_reg_cam1(0x12, 0x80);
wait = 100 * DELAYMSEC;
TMR1=0;
while (TMR1 < wait);
}
// reset the OV2640 registers to factory defaults on camera #2
void reset_ov2640_regs_cam2()
{
// switch to second register bank, set bit 7 of register 0x12 (COM7)
// to perform a device reset of the OV2640 chip
write_ov2640_reg_cam2(0xff, 0x01);
write_ov2640_reg_cam2(0x12, 0x80);
wait = 100 * DELAYMSEC;
TMR1=0;
while (TMR1 < wait);
}
// Write an array of values to 8 bit register address on camera #1
void init_ov2640_regs_cam1(const struct sensor_reg reglist[])
{
int i=0;
int reg_addr = 0;
int reg_val = 0;
while ((reglist[i].reg != 0xff) | (reglist[i].val != 0xff))
{
reg_addr = reglist[i].reg;
reg_val = reglist[i].val;
write_ov2640_reg_cam1(reg_addr, reg_val);
i++;
}
}
// Write an array of values to 8 bit register address on camera #2
void init_ov2640_regs_cam2(const struct sensor_reg reglist[])
{
int i=0;
int reg_addr = 0;
int reg_val = 0;
while ((reglist[i].reg != 0xff) | (reglist[i].val != 0xff))
{
reg_addr = reglist[i].reg;
reg_val = reglist[i].val;
write_ov2640_reg_cam2(reg_addr, reg_val);
i++;
}
}
void arduchip_write_reg(int reg, int val, int cs)
{
// cs is a chip select index to camera #1 or camera #2
// select the device
switch (cs)
{
case 1:
CS_CAM1 = 0;
break;
case 2:
CS_CAM2 = 0;
break;
default:
CS_CAM1 = 0;
}
// write the value to the register
// setting bit 7 of the address indicates write operation
write_spi2(reg | 0x80);
write_spi2(val);
// deselect the device
switch (cs)
{
case 1:
CS_CAM1 = 1;
break;
case 2:
CS_CAM2 = 1;
break;
default:
CS_CAM1 = 1;
}
}
int arduchip_read_reg(int reg, int cs)
{
int val;
// cs is a chip select index to camera #1 or camera #2
// select the device
switch (cs)
{
case 1:
CS_CAM1 = 0;
break;
case 2:
CS_CAM2 = 0;
break;
default:
CS_CAM1 = 0;
}
// write address, then read value
write_spi2(reg);
val = write_spi2(0);
// deselect the device
switch (cs)
{
case 1:
CS_CAM1 = 1;
break;
case 2:
CS_CAM2 = 1;
break;
default:
CS_CAM1 = 1;
}
return val;
}
// test write and read to ArduChip register
int arduchip_testreg(int testin, int cs)
{
// perform a test write and read to the arduchip TEST register
// to test operation of the SPI interface
// Returns testout, and the user should compare to testval.
// cs is the chip select for arducam #1 (1) or arducam #2 (2)
int testreg = 0x00; // address of the arduchip TEST register
// make sure that testout is initialized different than testin
int testout = ~(testin);
// write the testval to test register
arduchip_write_reg(testreg, testin, cs);
// read the value in test register
testout = arduchip_read_reg(testreg, cs);
return testout;
}
// reset the Arducam CPLD (arduchip)
void arduchip_reset(int cs)
{
// cs is the chip select for arducam #1 (1) or arducam #2 (2)
arduchip_write_reg(0x07, 0x80, cs);
wait = 100 * DELAYMSEC;
TMR1 = 0;
while (TMR1 < wait);
arduchip_write_reg(0x07, 0x00, cs);
TMR1 = 0;
while (TMR1 < wait);
}
// clear the arduchip FIFO "write done" flag
void arduchip_clear_fifo(int cs)
{
// cs is the chip select for arducam #1 (1) or arducam #2 (2)
arduchip_write_reg(0x04, 0x01, cs);
}
// set the number of frames to capture
void arduchip_set_nframes(int n, int cs)
{
// cs is the chip select for arducam #1 (1) or arducam #2 (2)
int nframes = n-1; // register value + 1 = # of frames to write
arduchip_write_reg(0x01, nframes, cs);
}
// start an image capture
long arduchip_start_capture(int cs)
{
// cs is the chip select for arducam #1 (1) or arducam #2 (2)
long msec;
arduchip_write_reg(0x04, 0x02, cs);
TMR1 = 0;
while (!(arduchip_capture_done(cs)));
msec = TMR1 / DELAYMSEC;
return msec;
}
// check if the image capture is done (0=no, 1=yes)
int arduchip_capture_done(int cs)
{
// cs is the chip select for arducam #1 (1) or arducam #2 (2)
int done_bit; // bit 3 of register 0x41 indicates write to FIFO is done
int regval;
regval = arduchip_read_reg(0x41, cs);
done_bit = regval & 0x08;
return (done_bit != 0);
}
long arduchip_fifo_length(int *outlen1, int *outlen2, int *outlen3, int cs)
{
// cs is the chip select for arducam #1 (1) or arducam #2 (2)
int len1,len2,len3;
long llen1, llen2, llen3;
long length=0;
wait = 50 * DELAYMSEC;
TMR1=0;
while (TMR1 < wait);
len1 = arduchip_read_reg(0x42, cs);
len2 = arduchip_read_reg(0x43, cs);
len3 = arduchip_read_reg(0x44, cs);
llen1 = (long)len1;
llen2 = (long)len2;
llen3 = (long)len3;
length = ((llen3 << 16) | (llen2 << 8) | llen1);
*outlen1 = len1;
*outlen2 = len2;
*outlen3 = len3;
return length;
}