-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharducam_mega.c
407 lines (358 loc) · 11.4 KB
/
arducam_mega.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
/* MIT License */
/* Copyright (c) [year] [fullname] */
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files (the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */
/* The above copyright notice and this permission notice shall be included in all */
/* copies or substantial portions of the Software. */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
#include "arducam_mega.h"
#include <zephyr/device.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/fs/fs.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/printk.h>
#include <zephyr/syscall_handler.h>
#include <zephyr/types.h>
#define LOG_MODULE_NAME arducam_mega
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define MAX_PATH 51200
#define CONCAT_BUFF_LEN 30
#define BUFFER_SIZE 0xff
int received_length;
struct spi_config spi_cfg = {
.frequency = DT_PROP(DT_NODELABEL(spi0), clock_frequency),
.operation = SPI_LOCK_ON | SPI_HOLD_ON_CS | SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB |
SPI_WORD_SET(8) | SPI_LINES_SINGLE,
.cs = SPI_CS_CONTROL_INIT(DT_NODELABEL(spi0), 0u),
.slave = 0,
};
#define SPI_DEV_NAME DEVICE_DT_NAME(DT_NODELABEL(spi0))
const struct device *spi;
#define LED DT_ALIAS(csled)
struct gpio_dt_spec spec = GPIO_DT_SPEC_GET(LED, gpios);
uint8_t camera_bus_read(uint8_t address)
{
int ret;
struct spi_buf tx_buf[1];
tx_buf[0].buf = &address;
tx_buf[0].len = 1;
struct spi_buf_set tx_bufs = {.buffers = tx_buf, .count = 1};
spi_cfg.operation |= SPI_HOLD_ON_CS;
spi_cfg.operation |= SPI_LOCK_ON;
gpio_pin_toggle_dt(&spec);
ret = spi_write(spi, &spi_cfg, &tx_bufs);
uint8_t rxdata[2];
struct spi_buf rx_buf[1] = {
{.buf = rxdata, .len = 2},
};
struct spi_buf_set rx_bufs = {.buffers = rx_buf, .count = 2};
ret = spi_read(spi, &spi_cfg, &rx_bufs);
gpio_pin_toggle_dt(&spec);
spi_release(spi, &spi_cfg);
return rxdata[1];
}
uint8_t camera_read_reg(uint8_t addr)
{
return camera_bus_read(addr & 0x7F);
}
uint8_t camera_bus_write(uint8_t address, uint8_t value)
{
struct spi_buf tx_buf[2];
tx_buf[0].buf = &address;
tx_buf[0].len = 1;
tx_buf[1].buf = &value;
tx_buf[1].len = 1;
struct spi_buf_set tx_bufs = {.buffers = tx_buf, .count = 2};
spi_cfg.operation |= SPI_HOLD_ON_CS;
spi_cfg.operation |= SPI_LOCK_ON;
gpio_pin_toggle_dt(&spec);
spi_write(spi, &spi_cfg, &tx_bufs);
gpio_pin_toggle_dt(&spec);
k_sleep(K_MSEC(10));
return 1;
}
void camera_write_reg(uint8_t addr, uint8_t val)
{
camera_bus_write(addr | 0x80, val);
}
void camera_wait_idle()
{
while ((camera_read_reg(CAM_REG_SENSOR_STATE) & 0X03) != CAM_REG_SENSOR_STATE_IDLE) {
k_sleep(K_MSEC(2));
}
}
uint8_t camera_get_bit(uint8_t addr, uint8_t bit)
{
uint8_t temp;
temp = camera_read_reg(addr);
temp = temp & bit;
return temp;
}
uint8_t camera_read_byte()
{
int ret;
uint8_t rxdata[1];
struct spi_buf rx_buf[1] = {
{.buf = rxdata, .len = 1},
};
struct spi_buf_set rx_bufs = {.buffers = rx_buf, .count = 1};
gpio_pin_toggle_dt(&spec);
struct spi_buf tx_buf[1];
uint8_t send_cmd = SINGLE_FIFO_READ;
tx_buf[0].buf = &send_cmd;
tx_buf[0].len = 1;
struct spi_buf_set tx_bufs = {.buffers = tx_buf, .count = 1};
spi_cfg.operation |= SPI_HOLD_ON_CS;
spi_cfg.operation |= SPI_LOCK_ON;
ret = spi_write(spi, &spi_cfg, &tx_bufs);
send_cmd = 0x00;
tx_buf[0].buf = &send_cmd;
ret = spi_write(spi, &spi_cfg, &tx_bufs);
ret = spi_read(spi, &spi_cfg, &rx_bufs);
gpio_pin_toggle_dt(&spec);
received_length -= 1;
return rxdata[0];
}
void camera_save_fifo(const char *base_path, uint32_t length, char *filename)
{
uint8_t imageData = 0, imageDataNext = 0, headFlag = 0;
unsigned int i = 0;
uint8_t imageBuff[BUFFER_SIZE] = {0};
int ret;
uint8_t sd_write_counts = 0;
uint8_t file_opened = 0;
received_length = length;
char path[MAX_PATH];
struct fs_file_t file;
int base = strlen(base_path);
fs_file_t_init(&file);
if (base >= (sizeof(path) - CONCAT_BUFF_LEN)) {
LOG_ERR("Not enough concatenation buffer to create file paths");
return;
}
strncpy(path, base_path, sizeof(path));
path[base++] = '/';
path[base] = 0;
strcat(&path[base], filename);
while (received_length) {
imageData = imageDataNext;
imageDataNext = camera_read_byte();
if (headFlag == 1) {
imageBuff[i++] = imageDataNext;
if (i >= BUFFER_SIZE) {
ret = fs_write(&file, imageBuff, i);
sd_write_counts++;
i = 0;
}
}
if (imageData == 0xff && imageDataNext == 0xd8 && file_opened == 0) {
ret = fs_open(&file, path, FS_O_CREATE | FS_O_WRITE | FS_O_APPEND);
if (ret != 0) {
LOG_ERR("Failed to create file %s %d", path, ret);
return;
}
LOG_INF("Opened file successfully\n");
file_opened = 1;
headFlag = 1;
imageBuff[i++] = imageData;
imageBuff[i++] = imageDataNext;
}
if (imageData == 0xff && imageDataNext == 0xd9) {
headFlag = 0;
LOG_INF("Closed file with sd_write_counts %d\n", sd_write_counts);
fs_write(&file, imageBuff, i);
fs_close(&file);
i = 0;
break;
}
}
}
int arducam_mega_capture_image(CAM_IMAGE_MODE mode, CAM_IMAGE_PIX_FMT pixel_format)
{
camera_write_reg(CAM_REG_SENSOR_RESET, CAM_SENSOR_RESET_ENABLE);
camera_wait_idle();
camera_write_reg(0x04, 0x01);
camera_write_reg(0x04, 0x02);
camera_wait_idle();
k_sleep(K_MSEC(300));
/* Set format JPG */
camera_write_reg(CAM_REG_FORMAT,
CAM_IMAGE_PIX_FMT_JPG); // set the data format
camera_wait_idle(); // Wait I2c Idle
/* Set capture resolution */
camera_write_reg(CAM_REG_CAPTURE_RESOLUTION, CAM_SET_CAPTURE_MODE | mode);
camera_wait_idle(); // Wait I2c Idle
/* Clear fifo flags */
camera_write_reg(ARDUCHIP_FIFO, FIFO_CLEAR_ID_MASK);
/* Start capture */
camera_write_reg(ARDUCHIP_FIFO, FIFO_START_MASK);
while (camera_get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK) == 0)
;
uint32_t len1, len2, len3, length = 0;
len1 = camera_read_reg(FIFO_SIZE1);
len2 = camera_read_reg(FIFO_SIZE2);
len3 = camera_read_reg(FIFO_SIZE3);
length = ((len3 << 16) | (len2 << 8) | len1) & 0xffffff;
LOG_INF("Image length is %d\n", length);
return length;
}
int arducam_mega_save_image(char *filename, const char *mount_point, int image_length)
{
LOG_INF("Saving image\n");
camera_save_fifo(mount_point, image_length, filename);
return 0;
}
int arducam_mega_get_id()
{
uint8_t cameraID;
cameraID = camera_read_reg(CAM_REG_SENSOR_ID);
LOG_INF("Sensor camera ID is %x\n", cameraID);
return cameraID;
}
int arducam_mega_init()
{
int ret = gpio_pin_configure_dt(&spec, GPIO_OUTPUT_ACTIVE);
if (ret < 0) {
LOG_ERR("Couldn't configure CS pin");
return -1;
}
LOG_INF("Initializing the camera");
spi = device_get_binding(SPI_DEV_NAME);
if (!device_is_ready(spi)) {
LOG_ERR("Device SPI not ready, aborting test");
return -1;
}
return 0;
}
CAM_IMAGE_MODE arducam_mega_get_resolution(char *resolution)
{
if (strcmp(resolution, "2592x1944") == 0) {
return CAM_IMAGE_MODE_WQXGA2;
} else if (strcmp(resolution, "1920x1080") == 0) {
return CAM_IMAGE_MODE_FHD;
} else if (strcmp(resolution, "1600x1200") == 0) {
return CAM_IMAGE_MODE_UXGA;
} else if (strcmp(resolution, "1280x720") == 0) {
return CAM_IMAGE_MODE_HD;
} else if (strcmp(resolution, "640x480") == 0) {
return CAM_IMAGE_MODE_VGA;
} else if (strcmp(resolution, "320x240") == 0) {
return CAM_IMAGE_MODE_QVGA;
} else if (strcmp(resolution, "320x320") == 0) {
return CAM_IMAGE_MODE_320X320;
} else if (strcmp(resolution, "128x128") == 0) {
return CAM_IMAGE_MODE_128X128;
} else if (strcmp(resolution, "96x96") == 0) {
return CAM_IMAGE_MODE_96X96;
}
/* Default to highest res if value is missing/malformed */
return CAM_IMAGE_MODE_WQXGA2;
}
CAM_SATURATION_LEVEL arducam_mega_get_saturation(char *saturation)
{
saturation = saturation + 2;
saturation[strlen(saturation) - 1] = '\0';
if (strcmp(saturation, "-3") == 0) {
return CAM_SATURATION_LEVEL_MINUS_3;
} else if (strcmp(saturation, "-2") == 0) {
return CAM_SATURATION_LEVEL_MINUS_2;
} else if (strcmp(saturation, "-1") == 0) {
return CAM_SATURATION_LEVEL_MINUS_1;
} else if (strcmp(saturation, "0") == 0) {
return CAM_SATURATION_LEVEL_DEFAULT;
} else if (strcmp(saturation, "1") == 0) {
return CAM_SATURATION_LEVEL_1;
} else if (strcmp(saturation, "2") == 0) {
return CAM_SATURATION_LEVEL_2;
} else if (strcmp(saturation, "3") == 0) {
return CAM_SATURATION_LEVEL_3;
}
/* Return default if string malformed */
return CAM_SATURATION_LEVEL_DEFAULT;
}
CAM_BRIGHTNESS_LEVEL arducam_mega_get_brightness(char *brightness)
{
brightness = brightness + 2;
brightness[strlen(brightness) - 1] = '\0';
if (strcmp(brightness, "-4") == 0) {
return CAM_BRIGHTNESS_LEVEL_MINUS_4;
} else if (strcmp(brightness, "-3") == 0) {
return CAM_BRIGHTNESS_LEVEL_MINUS_3;
} else if (strcmp(brightness, "-2") == 0) {
return CAM_BRIGHTNESS_LEVEL_MINUS_2;
} else if (strcmp(brightness, "-1") == 0) {
return CAM_BRIGHTNESS_LEVEL_MINUS_1;
} else if (strcmp(brightness, "0") == 0) {
return CAM_BRIGHTNESS_LEVEL_DEFAULT;
} else if (strcmp(brightness, "1") == 0) {
return CAM_BRIGHTNESS_LEVEL_1;
} else if (strcmp(brightness, "2") == 0) {
return CAM_BRIGHTNESS_LEVEL_2;
} else if (strcmp(brightness, "3") == 0) {
return CAM_BRIGHTNESS_LEVEL_3;
} else if (strcmp(brightness, "4") == 0) {
return CAM_BRIGHTNESS_LEVEL_4;
}
/* Return default if string malformed */
return CAM_BRIGHTNESS_LEVEL_DEFAULT;
}
CAM_CONTRAST_LEVEL arducam_mega_get_contrast(char *contrast)
{
contrast = contrast + 2;
contrast[strlen(contrast) - 1] = '\0';
if (strcmp(contrast, "-3") == 0) {
return CAM_CONTRAST_LEVEL_MINUS_3;
} else if (strcmp(contrast, "-2") == 0) {
return CAM_CONTRAST_LEVEL_MINUS_2;
} else if (strcmp(contrast, "-1") == 0) {
return CAM_CONTRAST_LEVEL_MINUS_1;
} else if (strcmp(contrast, "0") == 0) {
return CAM_CONTRAST_LEVEL_DEFAULT;
} else if (strcmp(contrast, "1") == 0) {
return CAM_CONTRAST_LEVEL_1;
} else if (strcmp(contrast, "2") == 0) {
return CAM_CONTRAST_LEVEL_2;
} else if (strcmp(contrast, "3") == 0) {
return CAM_CONTRAST_LEVEL_3;
}
/* Return default if string malformed */
return CAM_CONTRAST_LEVEL_DEFAULT;
}
int arducam_mega_set_saturation(CAM_SATURATION_LEVEL saturation)
{
LOG_INF("Setting saturation to %d", saturation);
camera_write_reg(CAM_REG_SATURATION_CONTROL, saturation);
camera_wait_idle();
return 0;
}
int arducam_mega_set_autofocus(CAM_AUTO_FOCUS autofocus)
{
LOG_INF("Setting autofocus to %d", autofocus);
camera_write_reg(CAM_REG_AUTO_FOCUS_CONTROL, autofocus);
camera_wait_idle();
return 0;
}
int arducam_mega_set_contrast(CAM_CONTRAST_LEVEL contrast)
{
LOG_INF("Setting contrast to %d", contrast);
camera_write_reg(CAM_REG_CONTRAST_CONTROL, contrast);
camera_wait_idle();
return 0;
}
int arducam_mega_set_brightness(CAM_BRIGHTNESS_LEVEL brightness)
{
LOG_INF("Setting brightness to %d", brightness);
camera_write_reg(CAM_REG_BRIGHTNESS_CONTROL, brightness);
camera_wait_idle();
return 0;
}