forked from usedbytes/camera-pico-ov7670
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera.c
439 lines (354 loc) · 10.4 KB
/
camera.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
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
/**
* Copyright (c) 2021-2022 Brian Starkey <stark3y@gmail.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdint.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "hardware/clocks.h"
#include "hardware/dma.h"
#include "hardware/irq.h"
#include "hardware/pio.h"
#include "camera/camera.h"
#include "camera/format.h"
#include "camera/ov7670.h"
#include "camera.pio.h"
#define CAMERA_PIO_FRAME_SM 0
struct camera *volatile irq_ctxs[2];
static inline void __camera_isr(struct camera *camera)
{
if (!camera || !camera->pending) {
return;
}
if (camera->pending_cb) {
camera->pending_cb(camera->pending, camera->cb_data);
}
camera->pending = NULL;
}
static void camera_isr_pio0(void)
{
struct camera *camera = irq_ctxs[0];
__camera_isr(camera);
pio_interrupt_clear(pio0, 0);
}
static void camera_isr_pio1(void)
{
struct camera *camera = irq_ctxs[1];
__camera_isr(camera);
pio_interrupt_clear(pio1, 0);
}
static void camera_pio_init(struct camera *camera)
{
struct camera_platform_config *platform = camera->driver_host.platform;
PIO pio = platform->pio;
hard_assert(pio == pio0 || pio == pio1);
camera->shift_byte_offset = pio_add_program(pio, &camera_pio_byte_program);
camera->frame_offset = pio_add_program(pio, &camera_pio_frame_program);
for (int i = 0; i < 4; i++) {
camera_pio_init_gpios(pio, i, platform->base_pin);
}
pio->inte0 = (1 << (8 + CAMERA_PIO_FRAME_SM));
if (pio == pio0) {
irq_ctxs[0] = camera;
irq_set_exclusive_handler(PIO0_IRQ_0, camera_isr_pio0);
irq_set_enabled(PIO0_IRQ_0, true);
} else if (pio == pio1) {
irq_ctxs[1] = camera;
irq_set_exclusive_handler(PIO1_IRQ_0, camera_isr_pio1);
irq_set_enabled(PIO1_IRQ_0, true);
}
}
static bool camera_detect(struct camera_platform_config *platform)
{
const uint8_t reg = OV7670_REG_PID;
uint8_t val = 0;
int tries = 5;
while (tries--) {
int ret = platform->i2c_write_blocking(platform->i2c_handle, OV7670_ADDR, ®, 1);
if (ret != 1) {
sleep_ms(100);
continue;
}
ret = platform->i2c_read_blocking(platform->i2c_handle, OV7670_ADDR, &val, 1);
if (ret != 1) {
sleep_ms(100);
continue;
}
if (val == 0x76) {
break;
}
sleep_ms(100);
}
return val == 0x76;
}
int camera_init(struct camera *camera, struct camera_platform_config *params)
{
OV7670_status status;
*camera = (struct camera){ 0 };
clock_gpio_init(params->xclk_pin, CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLK_SYS, params->xclk_divider);
camera->driver_host = (OV7670_host){
.pins = &(OV7670_pins){
.enable = -1,
.reset = -1,
},
.platform = params,
};
// Some settling time for the clock
sleep_ms(300);
// Try and check that the camera is present
if (!camera_detect(params)) {
return -1;
}
// Note: Frame rate is ignored
status = OV7670_begin(&camera->driver_host, OV7670_COLOR_YUV, OV7670_SIZE_DIV8, 0.0);
if (status != OV7670_STATUS_OK) {
return -1;
}
if (params->base_dma_channel >= 0) {
for (int i = 0; i < CAMERA_MAX_N_PLANES; i++) {
dma_channel_claim(params->base_dma_channel + i);
camera->dma_channels[i] = params->base_dma_channel + i;
}
} else {
for (int i = 0; i < CAMERA_MAX_N_PLANES; i++) {
camera->dma_channels[i] = dma_claim_unused_channel(true);
}
}
camera_pio_init(camera);
// TODO: Set an initial frame config here?
// camera_configure(camera, FORMAT_RGB565, CAMERA_WIDTH_DIV8, CAMERA_HEIGHT_DIV8);
return 0;
}
void camera_term(struct camera *camera)
{
// TODO :-)
}
static OV7670_colorspace ov7670_colorspace_from_format(uint32_t format)
{
switch (format) {
case FORMAT_RGB565:
return OV7670_COLOR_RGB;
case FORMAT_YUYV:
/* Fallthrough */
case FORMAT_YUV422:
return OV7670_COLOR_YUV;
}
return 0;
}
static enum dma_channel_transfer_size camera_transfer_size(uint32_t format, uint8_t plane)
{
switch (format) {
case FORMAT_YUYV:
/* Fallthrough */
case FORMAT_RGB565:
return DMA_SIZE_32;
case FORMAT_YUV422:
return plane ? DMA_SIZE_8 : DMA_SIZE_16;
default:
return 0;
}
}
static uint8_t __dma_transfer_size_to_bytes(enum dma_channel_transfer_size dma)
{
switch (dma) {
case DMA_SIZE_8:
return 1;
case DMA_SIZE_16:
return 2;
case DMA_SIZE_32:
return 4;
}
return 0;
}
static const pio_program_t *camera_get_pixel_loop(uint32_t format)
{
switch (format) {
case FORMAT_YUYV:
return &pixel_loop_yuyv_program;
case FORMAT_RGB565:
// XXX: Should really use a 16-bit loop and swap to 2 bytes
// per chunk instead of 4.
return &pixel_loop_yuyv_program;
case FORMAT_YUV422:
return &pixel_loop_yu16_program;
default:
return NULL;
}
}
static void camera_pio_configure(struct camera *camera)
{
struct camera_platform_config *platform = camera->driver_host.platform;
// First disable everything
pio_set_sm_mask_enabled(platform->pio, 0xf, false);
// Then reset everything
pio_restart_sm_mask(platform->pio, 0xf);
// Drain the FIFOs
for (int i = 0; i < 4; i++) {
pio_sm_clear_fifos(platform->pio, i);
}
// Patch the pixel loop
const pio_program_t *pixel_loop = camera_get_pixel_loop(camera->config.format);
camera_pio_patch_pixel_loop(platform->pio, camera->frame_offset, pixel_loop);
// Finally we can configure and enable the state machines
uint8_t num_planes = format_num_planes(camera->config.format);
for (int i = 0; i < num_planes; i++) {
pio_sm_init(platform->pio, i + 1, camera->shift_byte_offset, &camera->config.sm_cfgs[i + 1]);
pio_sm_set_enabled(platform->pio, i + 1, true);
}
pio_sm_init(platform->pio, CAMERA_PIO_FRAME_SM, camera->frame_offset, &camera->config.sm_cfgs[CAMERA_PIO_FRAME_SM]);
pio_sm_set_enabled(platform->pio, CAMERA_PIO_FRAME_SM, true);
}
int camera_configure(struct camera *camera, uint32_t format, uint16_t width, uint16_t height)
{
if (width != CAMERA_WIDTH_DIV8 || height != CAMERA_HEIGHT_DIV8) {
// TODO: Handle other sizes
return -1;
}
struct camera_platform_config *platform = camera->driver_host.platform;
OV7670_set_format(platform, ov7670_colorspace_from_format(format));
OV7670_set_size(platform, OV7670_SIZE_DIV8);
camera->config.sm_cfgs[CAMERA_PIO_FRAME_SM] =
camera_pio_get_frame_sm_config(platform->pio, CAMERA_PIO_FRAME_SM, camera->frame_offset, platform->base_pin);
uint8_t num_planes = format_num_planes(format);
for (int i = 0; i < num_planes; i++) {
enum dma_channel_transfer_size xfer_size = camera_transfer_size(format, i);
dma_channel_config c = dma_channel_get_default_config(camera->dma_channels[i]);
channel_config_set_transfer_data_size(&c, xfer_size);
channel_config_set_read_increment(&c, false);
channel_config_set_write_increment(&c, true);
channel_config_set_dreq(&c, pio_get_dreq(platform->pio, i + 1, false));
camera->config.dma_cfgs[i] = c;
uint8_t xfer_bytes = __dma_transfer_size_to_bytes(xfer_size);
camera->config.dma_offset[i] = 4 - xfer_bytes,
camera->config.dma_transfers[i] = format_plane_size(format, i, width, height) / xfer_bytes,
camera->config.sm_cfgs[i + 1] = camera_pio_get_byte_sm_config(platform->pio, i + 1,
camera->shift_byte_offset, platform->base_pin,
xfer_bytes * 8);
}
camera->config.format = format;
camera->config.width = width;
camera->config.height = height;
camera_pio_configure(camera);
return 0;
}
static uint8_t camera_pixels_per_chunk(uint32_t format)
{
switch (format) {
case FORMAT_YUYV:
/* Fallthrough */
case FORMAT_RGB565:
return 2;
case FORMAT_YUV422:
return 2;
default:
return 1;
}
}
static int camera_do_frame(struct camera *camera, struct camera_buffer *buf, camera_frame_cb complete_cb, void *cb_data,
bool allow_reconfigure, bool blocking)
{
if (camera->pending) {
return -2;
}
if ((camera->config.format != buf->format) ||
(camera->config.width != buf->width) ||
(camera->config.height != buf->height)) {
if (allow_reconfigure) {
camera_configure(camera, buf->format, buf->width, buf->height);
} else {
return -1;
}
}
struct camera_platform_config *platform = camera->driver_host.platform;
uint8_t num_planes = format_num_planes(camera->config.format);
for (int i = 0; i < num_planes; i++) {
dma_channel_configure(camera->dma_channels[i],
&camera->config.dma_cfgs[i],
buf->data[i],
((char *)&platform->pio->rxf[i + 1]) + camera->config.dma_offset[i],
camera->config.dma_transfers[i],
true);
}
uint32_t num_loops = buf->width / camera_pixels_per_chunk(buf->format);
camera->pending = buf;
camera->pending_cb = complete_cb;
camera->cb_data = cb_data;
camera_pio_trigger_frame(platform->pio, num_loops, buf->height);
if (blocking) {
while (camera->pending) {
sleep_ms(1);
}
}
return 0;
}
int camera_capture_blocking(struct camera *camera, struct camera_buffer *into, bool allow_reconfigure)
{
return camera_do_frame(camera, into, NULL, NULL, allow_reconfigure, true);
}
int camera_capture_with_cb(struct camera *camera, struct camera_buffer *into, bool allow_reconfigure,
camera_frame_cb complete_cb, void *cb_data)
{
return camera_do_frame(camera, into, complete_cb, cb_data, allow_reconfigure, false);
}
struct camera_buffer *camera_buffer_alloc(uint32_t format, uint16_t width, uint16_t height)
{
struct camera_buffer *buf = malloc(sizeof(*buf));
if (!buf) {
return NULL;
}
*buf = (struct camera_buffer){
.format = format,
.width = width,
.height = height,
};
uint8_t num_planes = format_num_planes(format);
for (int i = 0; i < num_planes; i++) {
buf->strides[i] = format_stride(format, i, width);
buf->sizes[i] = format_plane_size(format, i, width, height);
buf->data[i] = malloc(buf->sizes[i]);
if (!buf->data[i]) {
goto error;
}
}
return buf;
error:
for (int i = 0; i < num_planes; i++) {
if (buf->data[i]) {
free(buf->data[i]);
}
}
free(buf);
return NULL;
}
void camera_buffer_free(struct camera_buffer *buf)
{
if (buf == NULL) {
return;
}
uint8_t num_planes = format_num_planes(buf->format);
for (int i = 0; i < num_planes; i++) {
if (buf->data[i]) {
free(buf->data[i]);
}
}
free(buf);
}
// Adafruit driver functions
void OV7670_print(char *str)
{
// TODO
}
int OV7670_read_register(void *platform, uint8_t reg)
{
struct camera_platform_config *pcfg = (struct camera_platform_config *)platform;
uint8_t value;
pcfg->i2c_write_blocking(pcfg->i2c_handle, OV7670_ADDR, ®, 1);
pcfg->i2c_read_blocking(pcfg->i2c_handle, OV7670_ADDR, &value, 1);
return value;
}
void OV7670_write_register(void *platform, uint8_t reg, uint8_t value)
{
struct camera_platform_config *pcfg = (struct camera_platform_config *)platform;
pcfg->i2c_write_blocking(pcfg->i2c_handle, OV7670_ADDR, (uint8_t[]){ reg, value }, 2);
}