-
Notifications
You must be signed in to change notification settings - Fork 38
/
DMA摄像头.c
executable file
·217 lines (179 loc) · 5.75 KB
/
DMA摄像头.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
#include "gpio.h"
#include "common.h"
#include "uart.h"
#include "dma.h"
#include "sram.h"
#include "ili9320.h"
#include "dma.h"
#include "ov7725.h"
#include "image_display.h"
#include "i2c.h"
/* 请将I2C.H中的 I2C_GPIO_SIM 改为 1 */
// 改变图像大小
//0: 80x60
//1: 160x120
//2: 240x180
#define IMAGE_SIZE 0
#if (IMAGE_SIZE == 0)
#define OV7620_W (80)
#define OV7620_H (60)
#elif (IMAGE_SIZE == 1)
#define OV7620_W (160)
#define OV7620_H (120)
#elif (IMAGE_SIZE == 2)
#define OV7620_W (240)
#define OV7620_H (180)
#else
#error "Image Size Not Support!"
#endif
// 图像内存池
uint8_t gCCD_RAM[(OV7620_H)*((OV7620_W/8)+1)]; //使用内部RAM
/* 行指针 */
uint8_t * gpHREF[OV7620_H+1];
/* 引脚定义 PCLK VSYNC HREF 接到同一个PORT上 */
#define BOARD_OV7620_PCLK_PORT HW_GPIOA
#define BOARD_OV7620_PCLK_PIN (7)
#define BOARD_OV7620_VSYNC_PORT HW_GPIOA
#define BOARD_OV7620_VSYNC_PIN (16)
#define BOARD_OV7620_HREF_PORT HW_GPIOA
#define BOARD_OV7620_HREF_PIN (17)
/*
摄像头数据引脚PTA8-PTA15 只能填入 0 8 16三个值
0 :PTA0-PTA7
8 :PTA8-PTA15
16:PTA16-PTA24
*/
#define BOARD_OV7620_DATA_OFFSET (8)
/* 状态机定义 */
typedef enum
{
TRANSFER_IN_PROCESS, //数据在处理
NEXT_FRAME, //下一帧数据
}OV7620_Status;
static void UserApp(uint32_t vcount);
/* 接收完成一场后 用户处理函数 */
static void UserApp(uint32_t vcount)
{
GUI_printf(100,0, "frame:%d", vcount);
GUI_DispCCDImage(0, 15, OV7620_W, OV7620_H, gpHREF);
//SerialDispCCDImage(OV7620_W, OV7620_H, CCDBuffer);
}
int SCCB_Init(uint32_t I2C_MAP)
{
int r;
uint32_t instance;
instance = I2C_QuickInit(I2C_MAP, 50*1000);
r = ov7725_probe(instance);
if(r)
{
return 1;
}
r = ov7725_set_image_size(IMAGE_SIZE);
if(r)
{
printf("OV7725 set image error\r\n");
return 1;
}
return 0;
}
//航中断和长中断都使用PTA中断
void OV_ISR(uint32_t index)
{
static uint8_t status = TRANSFER_IN_PROCESS;
static uint32_t h_counter, v_counter;
// uint32_t i;
/* 行中断 */
if(index & (1 << BOARD_OV7620_HREF_PIN))
{
DMA_SetDestAddress(HW_DMA_CH2, (uint32_t)gpHREF[h_counter++]);
//i = DMA_GetMajorLoopCount(HW_DMA_CH2);
DMA_SetMajorLoopCounter(HW_DMA_CH2, (OV7620_W/8)+1);
DMA_EnableRequest(HW_DMA_CH2);
return;
}
/* 场中断 */
if(index & (1 << BOARD_OV7620_VSYNC_PIN))
{
GPIO_ITDMAConfig(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_IT_FallingEdge, false);
GPIO_ITDMAConfig(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_IT_FallingEdge, false);
switch(status)
{
case TRANSFER_IN_PROCESS: //接受到一帧数据调用用户处理
UserApp(v_counter++);
//printf("i:%d %d\r\n", h_counter, i);
status = NEXT_FRAME;
h_counter = 0;
break;
case NEXT_FRAME: //等待下次传输
status = TRANSFER_IN_PROCESS;
break;
default:
break;
}
GPIO_ITDMAConfig(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_IT_FallingEdge, true);
GPIO_ITDMAConfig(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_IT_FallingEdge, true);
PORTA->ISFR = 0xFFFFFFFF;
h_counter = 0;
return;
}
}
int main(void)
{
uint32_t i;
DelayInit();
/* 打印串口及小灯 */
GPIO_QuickInit(HW_GPIOE, 6, kGPIO_Mode_OPP);
UART_QuickInit(UART0_RX_PD06_TX_PD07, 115200);
printf("OV7725 test\r\n");
//初始化GUI
CHGUI_Init();
GUI_printf(0, 0, "OV7725 test");
//检测摄像头
if(SCCB_Init(I2C0_SCL_PB00_SDA_PB01))
{
printf("no ov7725device found!\r\n");
while(1);
}
printf("OV7620 setup complete\r\n");
//每行数据指针
for(i=0; i<OV7620_H+1; i++)
{
gpHREF[i] = (uint8_t*)&gCCD_RAM[i*OV7620_W/8];
}
DMA_InitTypeDef DMA_InitStruct1 = {0};
/* 场中断 行中断 像素中断 */
GPIO_QuickInit(BOARD_OV7620_PCLK_PORT, BOARD_OV7620_PCLK_PIN, kGPIO_Mode_IPD);
GPIO_QuickInit(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_Mode_IPD);
GPIO_QuickInit(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_Mode_IPD);
/* install callback */
GPIO_CallbackInstall(BOARD_OV7620_VSYNC_PORT, OV_ISR);
GPIO_ITDMAConfig(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_IT_FallingEdge, true);
GPIO_ITDMAConfig(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_IT_FallingEdge, true);
GPIO_ITDMAConfig(BOARD_OV7620_PCLK_PORT, BOARD_OV7620_PCLK_PIN, kGPIO_DMA_RisingEdge, true);
/* 初始化数据端口 */
for(i=0;i<8;i++)
{
GPIO_QuickInit(HW_GPIOA, BOARD_OV7620_DATA_OFFSET+i, kGPIO_Mode_IFT);
}
//DMA配置
DMA_InitStruct1.chl = HW_DMA_CH2;
DMA_InitStruct1.chlTriggerSource = PORTA_DMAREQ;
DMA_InitStruct1.triggerSourceMode = kDMA_TriggerSource_Normal;
DMA_InitStruct1.minorLoopByteCnt = 1;
DMA_InitStruct1.majorLoopCnt = ((OV7620_W/8) +1);
DMA_InitStruct1.sAddr = (uint32_t)&PTA->PDIR + BOARD_OV7620_DATA_OFFSET/8;
DMA_InitStruct1.sLastAddrAdj = 0;
DMA_InitStruct1.sAddrOffset = 0;
DMA_InitStruct1.sDataWidth = kDMA_DataWidthBit_8;
DMA_InitStruct1.sMod = kDMA_ModuloDisable;
DMA_InitStruct1.dAddr = (uint32_t)gpHREF[0];
DMA_InitStruct1.dLastAddrAdj = 0;
DMA_InitStruct1.dAddrOffset = 1;
DMA_InitStruct1.dDataWidth = kDMA_DataWidthBit_8;
DMA_InitStruct1.dMod = kDMA_ModuloDisable;
/* initialize DMA moudle */
DMA_Init(&DMA_InitStruct1);
while(1)
{
}
}