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
87
93
94
95
96
97
98
101
102
105
106
109
110
113
114
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
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
195
196
197
198
199
200
204
205
206
207
208
209
210
211
212
213
217
222
223
227
232
233
236
241
242
245
250
251
255
256
257
258
259
260
261
262
263
264
268
269
270
271
272
273
274
275
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
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
343
344
345
346
347
348
349
350
351
352
353
360
361
362
363
364
365
366
367
368
369
370
377
378
381
385
386
389
393
394
399
400
401
402
403
404
405
406
411
416
421
426
430
431
432
433
434
435
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
555
556
557
561
565
566
569
575
576
580
584
585
588
594
595
599
603
604
607
613
614
618
622
623
626
632
633
636
637
640
641
644
645
/* ... */
/* ... */
#include "stm32446e_eval_camera.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
CAMERA_DrvTypeDef *camera_drv;
static DCMI_HandleTypeDef hDcmiEval;
uint32_t currentResolution;
static uint32_t cameraHwAddress;
/* ... */
/* ... */
static uint32_t GetSize(uint32_t Resolution);
/* ... */
/* ... */
/* ... */
uint8_t BSP_CAMERA_Init(uint32_t Resolution)
{
DCMI_HandleTypeDef *phdcmi;
uint8_t ret = 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->Init.ByteSelectMode = DCMI_BSM_ALL;
phdcmi->Init.ByteSelectStart = DCMI_OEBS_ODD;
phdcmi->Init.LineSelectMode = DCMI_LSM_ALL;
phdcmi->Init.LineSelectStart = DCMI_OELS_ODD;
phdcmi->Instance = DCMI;
BSP_IO_Init();
BSP_CAMERA_HwReset();
if(BSP_IO_ReadPin(CAM_PLUG_PIN))
{
return CAMERA_ERROR;
}if (BSP_IO_ReadPin(CAM_PLUG_PIN)) { ... }
if (s5k5cag_ReadID(CAMERA_I2C_ADDRESS) == S5K5CAG_ID)
{
camera_drv = &s5k5cag_drv;
cameraHwAddress = CAMERA_I2C_ADDRESS;
BSP_CAMERA_MspInit(&hDcmiEval, NULL);
HAL_DCMI_Init(phdcmi);
camera_drv->Init(cameraHwAddress, Resolution);
currentResolution = Resolution;
ret = CAMERA_OK;
}if (s5k5cag_ReadID(CAMERA_I2C_ADDRESS) == S5K5CAG_ID) { ... }
else if(ov5640_ReadID(CAMERA_I2C_ADDRESS_2) == OV5640_ID)
{
camera_drv = &ov5640_drv;
cameraHwAddress = CAMERA_I2C_ADDRESS_2;
BSP_CAMERA_MspInit(&hDcmiEval, NULL);
HAL_DCMI_Init(phdcmi);
camera_drv->Init(cameraHwAddress, Resolution);
currentResolution = Resolution;
ret = CAMERA_OK;
}else if (ov5640_ReadID(CAMERA_I2C_ADDRESS_2) == OV5640_ID) { ... }
else
{
ret = CAMERA_ERROR;
}else { ... }
return ret;
}{ ... }
/* ... */
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(currentResolution));
}{ ... }
/* ... */
void BSP_CAMERA_SnapshotStart(uint8_t *buff)
{
HAL_DCMI_Start_DMA(&hDcmiEval, DCMI_MODE_SNAPSHOT, (uint32_t)buff, GetSize(currentResolution));
}{ ... }
/* ... */
void BSP_CAMERA_Suspend(void)
{
HAL_DCMI_Suspend(&hDcmiEval);
}{ ... }
/* ... */
void BSP_CAMERA_Resume(void)
{
HAL_DCMI_Resume(&hDcmiEval);
}{ ... }
/* ... */
uint8_t BSP_CAMERA_Stop(void)
{
DCMI_HandleTypeDef *phdcmi;
uint8_t ret = CAMERA_ERROR;
phdcmi = &hDcmiEval;
if(HAL_DCMI_Stop(phdcmi) == HAL_OK)
{
ret = CAMERA_OK;
}if (HAL_DCMI_Stop(phdcmi) == HAL_OK) { ... }
BSP_CAMERA_PwrDown();
return ret;
}{ ... }
/* ... */
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(camera_drv->Config != NULL)
{
camera_drv->Config(cameraHwAddress, CAMERA_CONTRAST_BRIGHTNESS, contrast_level, brightness_level);
}if (camera_drv->Config != NULL) { ... }
}{ ... }
/* ... */
void BSP_CAMERA_BlackWhiteConfig(uint32_t Mode)
{
if(camera_drv->Config != NULL)
{
camera_drv->Config(cameraHwAddress, CAMERA_BLACK_WHITE, Mode, 0);
}if (camera_drv->Config != NULL) { ... }
}{ ... }
/* ... */
void BSP_CAMERA_ColorEffectConfig(uint32_t Effect)
{
if(camera_drv->Config != NULL)
{
camera_drv->Config(cameraHwAddress, CAMERA_COLOR_EFFECT, Effect, 0);
}if (camera_drv->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);
#if !defined(USE_STM32446E_EVAL_REVA)
gpio_init_structure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
#else
gpio_init_structure.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
#endif
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_10 | GPIO_PIN_11 |\
GPIO_PIN_12;
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);
#if !defined(USE_STM32446E_EVAL_REVA)
gpio_init_structure.Pin = GPIO_PIN_2 | GPIO_PIN_6;
#else
gpio_init_structure.Pin = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_6;
#endif
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);
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)
{
/* ... */
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */