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
53
54
57
58
61
62
65
68
69
72
75
76
79
82
83
86
89
90
93
94
97
98
99
100
101
102
103
104
105
106
107
108
111
112
115
116
119
120
123
124
130
131
132
133
134
140
141
142
143
144
148
152
153
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
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
232
233
234
235
236
237
241
242
243
244
245
246
247
248
249
250
254
259
260
264
269
270
273
278
279
282
287
288
292
293
294
295
296
300
301
302
303
304
305
306
307
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
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
375
376
377
378
379
380
381
382
383
384
385
392
393
394
395
396
397
398
399
400
401
402
403
410
411
414
418
419
422
426
427
432
433
434
435
436
437
438
439
444
449
454
459
463
464
465
466
467
468
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
584
585
586
590
594
595
598
604
605
609
613
614
617
623
624
628
632
633
636
642
643
647
651
652
655
661
662
665
666
669
670
673
674
/* ... */
/* ... */
#include "stm32469i_eval_camera.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
extern DMA2D_HandleTypeDef hdma2d_eval;
/* ... */
/* ... */
static DCMI_HandleTypeDef hDcmiEval;
CAMERA_DrvTypeDef *CameraDrv;
uint32_t CameraCurrentResolution;
uint32_t CameraRotation = CAMERA_ROTATION_INVALID;
static uint32_t CameraHwAddress;
/* ... */
/* ... */
static uint32_t GetSize(uint32_t Resolution);
/* ... */
/* ... */
/* ... */
uint8_t BSP_CAMERA_SetRotation(uint32_t rotation)
{
uint8_t status = CAMERA_ERROR;
if(rotation < CAMERA_ROTATION_INVALID)
{
CameraRotation = rotation;
status = CAMERA_OK;
}if (rotation < CAMERA_ROTATION_INVALID) { ... }
return status;
}{ ... }
/* ... */
uint32_t BSP_CAMERA_GetRotation(void)
{
return(CameraRotation);
}{ ... }
/* ... */
uint8_t BSP_CAMERA_Init(uint32_t Resolution)
{
DCMI_HandleTypeDef *phdcmi;
uint8_t status = CAMERA_ERROR;
phdcmi = &hDcmiEval;
phdcmi->Init.CaptureRate = DCMI_CR_ALL_FRAME;
phdcmi->Init.HSPolarity = DCMI_HSPOLARITY_HIGH;
phdcmi->Init.SynchroMode = DCMI_SYNCHRO_HARDWARE;
phdcmi->Init.VSPolarity = DCMI_VSPOLARITY_HIGH;
phdcmi->Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B;
phdcmi->Init.PCKPolarity = DCMI_PCKPOLARITY_RISING;
phdcmi->Instance = DCMI;
BSP_IO_Init();
BSP_CAMERA_HwReset();
if(BSP_IO_ReadPin(CAM_PLUG_PIN) == BSP_IO_PIN_SET)
{
status = CAMERA_NOT_DETECTED;
return status;
}if (BSP_IO_ReadPin(CAM_PLUG_PIN) == BSP_IO_PIN_SET) { ... }
if (s5k5cag_ReadID(CAMERA_I2C_ADDRESS) == S5K5CAG_ID)
{
CameraDrv = &s5k5cag_drv;
CameraHwAddress = CAMERA_I2C_ADDRESS;
BSP_CAMERA_MspInit(&hDcmiEval, NULL);
HAL_DCMI_Init(phdcmi);
CameraDrv->Init(CameraHwAddress, Resolution);
CameraCurrentResolution = Resolution;
status = CAMERA_OK;
}if (s5k5cag_ReadID(CAMERA_I2C_ADDRESS) == S5K5CAG_ID) { ... }
else if(ov5640_ReadID(CAMERA_I2C_ADDRESS_2) == OV5640_ID)
{
CameraDrv = &ov5640_drv;
CameraHwAddress = CAMERA_I2C_ADDRESS_2;
BSP_CAMERA_MspInit(&hDcmiEval, NULL);
HAL_DCMI_Init(phdcmi);
CameraDrv->Init(CameraHwAddress, Resolution);
CameraCurrentResolution = Resolution;
status = CAMERA_OK;
}else if (ov5640_ReadID(CAMERA_I2C_ADDRESS_2) == OV5640_ID) { ... }
else
{
status = CAMERA_NOT_SUPPORTED;
}else { ... }
return status;
}{ ... }
/* ... */
uint8_t BSP_CAMERA_DeInit(void)
{
hDcmiEval.Instance = DCMI;
HAL_DCMI_DeInit(&hDcmiEval);
BSP_CAMERA_MspDeInit(&hDcmiEval, NULL);
return CAMERA_OK;
}{ ... }
/* ... */
void BSP_CAMERA_ContinuousStart(uint8_t *buff)
{
HAL_DCMI_Start_DMA(&hDcmiEval, DCMI_MODE_CONTINUOUS, (uint32_t)buff, GetSize(CameraCurrentResolution));
}{ ... }
/* ... */
void BSP_CAMERA_SnapshotStart(uint8_t *buff)
{
HAL_DCMI_Start_DMA(&hDcmiEval, DCMI_MODE_SNAPSHOT, (uint32_t)buff, GetSize(CameraCurrentResolution));
}{ ... }
/* ... */
void BSP_CAMERA_Suspend(void)
{
HAL_DCMI_Suspend(&hDcmiEval);
}{ ... }
/* ... */
void BSP_CAMERA_Resume(void)
{
HAL_DCMI_Resume(&hDcmiEval);
}{ ... }
/* ... */
uint8_t BSP_CAMERA_Stop(void)
{
uint8_t status = CAMERA_ERROR;
if(HAL_DCMI_Stop(&hDcmiEval) == HAL_OK)
{
status = CAMERA_OK;
}if (HAL_DCMI_Stop(&hDcmiEval) == HAL_OK) { ... }
BSP_CAMERA_PwrDown();
return status;
}{ ... }
/* ... */
void BSP_CAMERA_HwReset(void)
{
BSP_IO_ConfigPin(RSTI_PIN, IO_MODE_OUTPUT);
BSP_IO_ConfigPin(XSDN_PIN, IO_MODE_OUTPUT);
BSP_IO_WritePin(XSDN_PIN, BSP_IO_PIN_SET);
BSP_IO_WritePin(RSTI_PIN, BSP_IO_PIN_RESET);
HAL_Delay(100);
BSP_IO_WritePin(XSDN_PIN, BSP_IO_PIN_RESET);
HAL_Delay(3);
BSP_IO_WritePin(RSTI_PIN, BSP_IO_PIN_SET);
HAL_Delay(6);
}{ ... }
/* ... */
void BSP_CAMERA_PwrDown(void)
{
BSP_IO_ConfigPin(RSTI_PIN, IO_MODE_OUTPUT);
BSP_IO_ConfigPin(XSDN_PIN, IO_MODE_OUTPUT);
BSP_IO_WritePin(XSDN_PIN, BSP_IO_PIN_RESET);
BSP_IO_WritePin(RSTI_PIN, BSP_IO_PIN_RESET);
}{ ... }
/* ... */
void BSP_CAMERA_ContrastBrightnessConfig(uint32_t contrast_level, uint32_t brightness_level)
{
if(CameraDrv->Config != NULL)
{
CameraDrv->Config(CameraHwAddress, CAMERA_CONTRAST_BRIGHTNESS, contrast_level, brightness_level);
}if (CameraDrv->Config != NULL) { ... }
}{ ... }
/* ... */
void BSP_CAMERA_BlackWhiteConfig(uint32_t Mode)
{
if(CameraDrv->Config != NULL)
{
CameraDrv->Config(CameraHwAddress, CAMERA_BLACK_WHITE, Mode, 0);
}if (CameraDrv->Config != NULL) { ... }
}{ ... }
/* ... */
void BSP_CAMERA_ColorEffectConfig(uint32_t Effect)
{
if(CameraDrv->Config != NULL)
{
CameraDrv->Config(CameraHwAddress, CAMERA_COLOR_EFFECT, Effect, 0);
}if (CameraDrv->Config != NULL) { ... }
}{ ... }
/* ... */
void BSP_CAMERA_IRQHandler(void)
{
HAL_DCMI_IRQHandler(&hDcmiEval);
}{ ... }
/* ... */
void BSP_CAMERA_DMA_IRQHandler(void)
{
HAL_DMA_IRQHandler(hDcmiEval.DMA_Handle);
}{ ... }
/* ... */
static uint32_t GetSize(uint32_t Resolution)
{
uint32_t size = 0;
switch (Resolution)
{
case CAMERA_R160x120:
{
size = 0x2580;
...}
break;case CAMERA_R160x120:
case CAMERA_R320x240:
{
size = 0x9600;
...}
break;case CAMERA_R320x240:
case CAMERA_R480x272:
{
size = 0xFF00;
...}
break;case CAMERA_R480x272:
case CAMERA_R640x480:
{
size = 0x25800;
...}
break;case CAMERA_R640x480:
default:
{
break;
...}default
}switch (Resolution) { ... }
return size;
}{ ... }
/* ... */
__weak void BSP_CAMERA_MspInit(DCMI_HandleTypeDef *hdcmi, void *Params)
{
static DMA_HandleTypeDef hdma_eval;
GPIO_InitTypeDef gpio_init_structure;
__HAL_RCC_DCMI_CLK_ENABLE();
__HAL_RCC_DMA2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
gpio_init_structure.Pin = GPIO_PIN_4 | GPIO_PIN_6;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
gpio_init_structure.Alternate = GPIO_AF13_DCMI;
HAL_GPIO_Init(GPIOA, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_7;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
gpio_init_structure.Alternate = GPIO_AF13_DCMI;
HAL_GPIO_Init(GPIOB, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 |\
GPIO_PIN_9 | GPIO_PIN_11;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
gpio_init_structure.Alternate = GPIO_AF13_DCMI;
HAL_GPIO_Init(GPIOC, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_3 | GPIO_PIN_6;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
gpio_init_structure.Alternate = GPIO_AF13_DCMI;
HAL_GPIO_Init(GPIOD, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_5 | GPIO_PIN_6;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
gpio_init_structure.Alternate = GPIO_AF13_DCMI;
HAL_GPIO_Init(GPIOE, &gpio_init_structure);
hdma_eval.Init.Channel = DMA_CHANNEL_1;
hdma_eval.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_eval.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_eval.Init.MemInc = DMA_MINC_ENABLE;
hdma_eval.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_eval.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_eval.Init.Mode = DMA_CIRCULAR;
hdma_eval.Init.Priority = DMA_PRIORITY_HIGH;
hdma_eval.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_eval.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_eval.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_eval.Init.PeriphBurst = DMA_PBURST_SINGLE;
hdma_eval.Instance = DMA2_Stream1;
__HAL_LINKDMA(hdcmi, DMA_Handle, hdma_eval);
HAL_NVIC_SetPriority(DCMI_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DCMI_IRQn);
HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
HAL_DMA_Init(hdcmi->DMA_Handle);
}{ ... }
/* ... */
__weak void BSP_CAMERA_MspDeInit(DCMI_HandleTypeDef *hdcmi, void *Params)
{
HAL_NVIC_DisableIRQ(DCMI_IRQn);
HAL_NVIC_DisableIRQ(DMA2_Stream1_IRQn);
HAL_DMA_DeInit(hdcmi->DMA_Handle);
__HAL_RCC_DCMI_CLK_DISABLE();
/* ... */
}{ ... }
/* ... */
void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
{
BSP_CAMERA_LineEventCallback();
}{ ... }
/* ... */
__weak void BSP_CAMERA_LineEventCallback(void)
{
/* ... */
}{ ... }
/* ... */
void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
{
BSP_CAMERA_VsyncEventCallback();
}{ ... }
/* ... */
__weak void BSP_CAMERA_VsyncEventCallback(void)
{
/* ... */
}{ ... }
/* ... */
void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
{
BSP_CAMERA_FrameEventCallback();
}{ ... }
/* ... */
__weak void BSP_CAMERA_FrameEventCallback(void)
{
/* ... */
}{ ... }
/* ... */
void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
{
BSP_CAMERA_ErrorCallback();
}{ ... }
/* ... */
__weak void BSP_CAMERA_ErrorCallback(void)
{
/* ... */
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */