-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdcmi.c
378 lines (331 loc) · 11 KB
/
dcmi.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
/**
******************************************************************************
* @file DCMI/OV2640_Camera/dcmi_OV2640.c
* @author MCD Application Team
* @version V1.0.0
* @date 18-April-2011
* @brief This file includes the driver for OV2640 Camera module mounted on
* STM322xG-EVAL board RevA and RevB.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "dcmi.h"
/** @addtogroup DCMI Settings
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Bits definitions ----------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
//static void Delay(uint32_t nTime);
//static void Delay_ms(uint32_t nTime);
/**
* @brief Configures the DCMI to interface with the OV2640 camera module.
* @param None
* @retval None
*/
void DCMI_GPIO_Init(void)
{
DCMI_InitTypeDef DCMI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DCMI_CROPInitTypeDef DCMI_Crop;
/* Enable DCMI GPIOs clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC |
RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOI, ENABLE);
/* Enable DCMI clock */
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);
/* Connect DCMI pins to AF13 ************************************************/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOI, GPIO_PinSource7, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOI, GPIO_PinSource6, GPIO_AF_DCMI);
/* DCMI GPIO configuration **************************************************/
/* D0..D4(PH9/10/11/12/14), HSYNC(PH8) */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 |
// GPIO_Pin_12 | GPIO_Pin_14| GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_Init(GPIOI, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9| GPIO_Pin_11; //D0 D1 D4
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //D5
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //HSYNC
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //VSYNC
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* PCLK(PA6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* DCMI configuration *******************************************************/
//DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_SnapShot;//DCMI_CaptureMode_Continuous
DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_Continuous;//DCMI_CaptureMode_Continuous
DCMI_InitStructure.DCMI_SynchroMode = DCMI_SynchroMode_Hardware;//DCMI_SynchroMode_Hardware
DCMI_InitStructure.DCMI_PCKPolarity = DCMI_PCKPolarity_Rising;//DCMI_PCKPolarity_Falling
DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_Low; //DCMI_VSPolarity_High
DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_Low; //DCMI_HSPolarity_High
// DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_High;
// DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_High;
//DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_1of2_Frame;//DCMI_CaptureRate_All_Frame
DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_All_Frame;
DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b; //?
DCMI_Init(&DCMI_InitStructure);
//DCMI_JPEGCmd(ENABLE);
DCMI_JPEGCmd(DISABLE);
// DCMI_Crop.DCMI_CaptureCount = 460-1;
// DCMI_Crop.DCMI_HorizontalOffsetCount = 20;
// DCMI_Crop.DCMI_VerticalLineCount = 320-1;
// DCMI_Crop.DCMI_VerticalStartLine = 0;
// DCMI_CROPConfig (&DCMI_Crop);
// DCMI_CROPCmd (ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = DCMI_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* DCMI Interrupts config ***************************************************/
DCMI_ITConfig(DCMI_IT_VSYNC, ENABLE);
DCMI_ITConfig(DCMI_IT_LINE, ENABLE);
DCMI_ITConfig(DCMI_IT_FRAME, ENABLE);
// DCMI_ITConfig(DCMI_IT_ERR, ENABLE);
}
void DCMI_DMA_Init(uint32_t bufsize)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Configures the DMA2 to transfer Data from DCMI to the LCD ****************/
/* Enable DMA2 clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
/* DMA2 Stream1 Configuration */
DMA_DeInit(DMA2_Stream1);
DMA_InitStructure.DMA_Channel = DMA_Channel_1;
DMA_InitStructure.DMA_PeripheralBaseAddr = DCMI_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) (DCMI_BUF_ADDRESS);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = bufsize;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//因为是传进数组,所以要启用自增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//DCMI读出的数据是32位的
//DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//数组内的元素都是8位的
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//数组内的元素都是8位的
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
//DMA_SetCurrDataCounter(DMA2_Stream1, bufsize);
//DMA_MemoryTargetConfig(DMA2_Stream1, (uint32_t) (DCMI_BUF_ADDRESS), DMA_Memory_0);
DMA_DoubleBufferModeConfig (DMA2_Stream1, (uint32_t) (DCMI_BUF_ADDRESS + bufsize*4), DMA_Memory_0);
DMA_DoubleBufferModeCmd (DMA2_Stream1, ENABLE);
DMA_Init(DMA2_Stream1, &DMA_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_TCIF1);
DMA_ITConfig (DMA2_Stream1, DMA_IT_TC, ENABLE); // enable transfer complete event
}
uint32_t Resolution_GetBufSize(ImageResolution_TypeDef ImageRes)
{
uint32_t res = 0;
switch(ImageRes)
{
case img_160x120:
{
res = 160*120;
break;
}
case img_176x144:
{
res = 176*144;
break;
}
case img_320x240:
{
res = 320*240;
break;
}
case img_352x288:
{
res = 352*288;
break;
}
case img_408x304:
{
res = 408*304;
break;
}
case img_480x320:
{
res = 480*320;
break;
}
case img_640x480:
{
res = 640*480;
break;
}
case img_800x600:
{
res = 800*600;
break;
}
default:
{
res = 352*288;
break;
}
}
return res;
}
uint16_t Resolution_GetWidth(ImageResolution_TypeDef ImageRes)
{
uint32_t res = 0;
switch(ImageRes)
{
case img_160x120:
{
res = 160;
break;
}
case img_176x144:
{
res = 176;
break;
}
case img_320x240:
{
res = 320;
break;
}
case img_352x288:
{
res = 352;
break;
}
case img_408x304:
{
res = 408;
break;
}
case img_480x320:
{
res = 480;
break;
}
case img_640x480:
{
res = 640;
break;
}
case img_800x600:
{
res = 800;
break;
}
default:
{
res = 352;
break;
}
}
return res;
}
uint16_t Resolution_GetHeight(ImageResolution_TypeDef ImageRes)
{
uint32_t res = 0;
switch(ImageRes)
{
case img_160x120:
{
res = 120;
break;
}
case img_176x144:
{
res = 144;
break;
}
case img_320x240:
{
res = 240;
break;
}
case img_352x288:
{
res = 288;
break;
}
case img_408x304:
{
res = 304;
break;
}
case img_480x320:
{
res = 320;
break;
}
case img_640x480:
{
res = 480;
break;
}
case img_800x600:
{
res = 600;
break;
}
default:
{
res = 288;
break;
}
}
return res;
}
/**
* @}
*/
//static void Delay(uint32_t nTime)
//{
// while(nTime--);
//}
//static void Delay_ms(uint32_t nTime)
//{
// while(nTime--)
// {Delay(1000);}
//}
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/