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
66
67
68
71
72
75
76
79
80
83
84
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
132
137
138
142
147
148
151
156
157
160
165
166
170
171
172
173
174
175
176
177
178
179
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
211
212
213
214
215
216
217
218
219
220
221
228
229
230
231
232
233
234
235
236
237
238
245
246
249
253
254
257
261
262
267
268
269
270
271
272
289
290
291
292
293
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
376
380
381
384
390
391
395
399
400
403
409
410
414
418
419
422
428
429
433
437
438
441
447
448
451
452
455
456
459
460
/* ... */
/* ... */
#include "stm324xg_eval_camera.h"
/* ... */
/* ... */
/* ... */
/* ... */
static DCMI_HandleTypeDef hdcmi_eval;
CAMERA_DrvTypeDef *camera_drv;
uint32_t current_resolution;
/* ... */
/* ... */
static uint32_t GetSize(uint32_t resolution);
/* ... */
/* ... */
/* ... */
uint8_t BSP_CAMERA_Init(uint32_t Resolution)
{
DCMI_HandleTypeDef *phdcmi;
uint8_t ret = CAMERA_ERROR;
phdcmi = &hdcmi_eval;
phdcmi->Init.CaptureRate = DCMI_CR_ALL_FRAME;
phdcmi->Init.HSPolarity = DCMI_HSPOLARITY_LOW;
phdcmi->Init.SynchroMode = DCMI_SYNCHRO_HARDWARE;
phdcmi->Init.VSPolarity = DCMI_VSPOLARITY_LOW;
phdcmi->Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B;
phdcmi->Init.PCKPolarity = DCMI_PCKPOLARITY_RISING;
phdcmi->Instance = DCMI;
BSP_CAMERA_MspInit();
HAL_DCMI_Init(phdcmi);
if(ov2640_drv.ReadID(CAMERA_I2C_ADDRESS) == OV2640_ID)
{
camera_drv = &ov2640_drv;
camera_drv->Init(CAMERA_I2C_ADDRESS, Resolution);
ret = CAMERA_OK;
}if (ov2640_drv.ReadID(CAMERA_I2C_ADDRESS) == OV2640_ID) { ... }
current_resolution = Resolution;
return ret;
}{ ... }
/* ... */
void BSP_CAMERA_ContinuousStart(uint8_t *buff)
{
HAL_DCMI_Start_DMA(&hdcmi_eval, DCMI_MODE_CONTINUOUS, (uint32_t)buff, GetSize(current_resolution));
}{ ... }
/* ... */
void BSP_CAMERA_SnapshotStart(uint8_t *buff)
{
HAL_DCMI_Start_DMA(&hdcmi_eval, DCMI_MODE_SNAPSHOT, (uint32_t)buff, GetSize(current_resolution));
}{ ... }
/* ... */
void BSP_CAMERA_Suspend(void)
{
HAL_DCMI_Suspend(&hdcmi_eval);
}{ ... }
/* ... */
void BSP_CAMERA_Resume(void)
{
HAL_DCMI_Resume(&hdcmi_eval);
}{ ... }
/* ... */
uint8_t BSP_CAMERA_Stop(void)
{
DCMI_HandleTypeDef *phdcmi;
uint8_t ret = CAMERA_ERROR;
phdcmi = &hdcmi_eval;
if(HAL_DCMI_Stop(phdcmi) == HAL_OK)
{
ret = CAMERA_OK;
}if (HAL_DCMI_Stop(phdcmi) == HAL_OK) { ... }
return ret;
}{ ... }
/* ... */
void BSP_CAMERA_ContrastBrightnessConfig(uint32_t contrast_level, uint32_t brightness_level)
{
if(camera_drv->Config != NULL)
{
camera_drv->Config(CAMERA_I2C_ADDRESS, 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(CAMERA_I2C_ADDRESS, 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(CAMERA_I2C_ADDRESS, CAMERA_COLOR_EFFECT, Effect, 0);
}if (camera_drv->Config != NULL) { ... }
}{ ... }
/* ... */
void BSP_CAMERA_IRQHandler(void)
{
HAL_DCMI_IRQHandler(&hdcmi_eval);
}{ ... }
/* ... */
void BSP_CAMERA_DMA_IRQHandler(void)
{
HAL_DMA_IRQHandler(hdcmi_eval.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:
default:
{
break;
...}default
}switch (resolution) { ... }
return size;
}{ ... }
/* ... */
__weak void BSP_CAMERA_MspInit(void)
{
static DMA_HandleTypeDef hdma;
GPIO_InitTypeDef GPIO_Init_Structure;
DCMI_HandleTypeDef *hdcmi = &hdcmi_eval;
__HAL_RCC_DCMI_CLK_ENABLE();
__HAL_RCC_DMA2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
GPIO_Init_Structure.Pin = 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_8 | GPIO_PIN_9 | GPIO_PIN_10 |\
GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_14;
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(GPIOH, &GPIO_Init_Structure);
GPIO_Init_Structure.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 |\
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(GPIOI, &GPIO_Init_Structure);
hdma.Init.Channel = DMA_CHANNEL_1;
hdma.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma.Init.PeriphInc = DMA_PINC_DISABLE;
hdma.Init.MemInc = DMA_MINC_ENABLE;
hdma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma.Init.Mode = DMA_CIRCULAR;
hdma.Init.Priority = DMA_PRIORITY_HIGH;
hdma.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma.Init.MemBurst = DMA_MBURST_SINGLE;
hdma.Init.PeriphBurst = DMA_PBURST_SINGLE;
hdma.Instance = DMA2_Stream1;
__HAL_LINKDMA(hdcmi, DMA_Handle, hdma);
HAL_NVIC_SetPriority(DCMI_IRQn, 0x0F, 0);
HAL_NVIC_EnableIRQ(DCMI_IRQn);
HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 0x0F, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
HAL_DMA_Init(hdcmi->DMA_Handle);
}{ ... }
/* ... */
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)
{
/* ... */
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */