-
Notifications
You must be signed in to change notification settings - Fork 38
/
DMA摄像头+OLED.c
253 lines (211 loc) · 6.58 KB
/
DMA摄像头+OLED.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
#include "gpio.h"
#include "common.h"
#include "uart.h"
#include "dma.h"
#include "i2c.h"
#include "ov7725.h"
#include "oled_spi.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_GPIOE
#define BOARD_OV7620_PCLK_PIN (8)
#define BOARD_OV7620_VSYNC_PORT HW_GPIOE
#define BOARD_OV7620_VSYNC_PIN (10)
#define BOARD_OV7620_HREF_PORT HW_GPIOE
#define BOARD_OV7620_HREF_PIN (9)
/*
摄像头数据引脚PTA8-PTA15 只能填入 0 8 16三个值
0 :PTA0-PTA7
8 :PTA8-PTA15
16:PTA16-PTA24
*/
#define BOARD_OV7620_DATA_OFFSET (0)
static void UserApp(uint32_t vcount);
//定义一帧结束后用户函数
/* 状态机定义 */
typedef enum
{
TRANSFER_IN_PROCESS, //数据在处理
NEXT_FRAME, //下一帧数据
}OV7620_Status;
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;
}
//行中断和场中断都使用PTE中断
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);
PORTE->ISFR = 0xFFFFFFFF;
h_counter = 0;
return;
}
}
void initCamera(){
uint32_t i;
printf("OV7725 test\r\n");
//检测摄像头
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_GPIOE, BOARD_OV7620_DATA_OFFSET+i, kGPIO_Mode_IFT);
}
//DMA配置
DMA_InitStruct1.chl = HW_DMA_CH2;
DMA_InitStruct1.chlTriggerSource = PORTE_DMAREQ;
DMA_InitStruct1.triggerSourceMode = kDMA_TriggerSource_Normal;
DMA_InitStruct1.minorLoopByteCnt = 1;
DMA_InitStruct1.majorLoopCnt = ((OV7620_W/8) +1);
DMA_InitStruct1.sAddr = (uint32_t)&PTE->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);
}
uint8_t bin8_rev(uint8_t data)
{
data=((data&0xf0)>>4) | ((data&0x0f)<<4);
data=((data&0xCC)>>2) | ((data&0x33)<<2);
data=((data&0xAA)>>1) | ((data&0x55)<<1);
return data;
}
void printBin(uint8_t data){
int i;
for(i=7;i>=0;i--){
printf("%c", (data>>i)%2>0?'*':'.');
}
}
/* 接收完成一场后 用户处理函数 */
static void UserApp(uint32_t vcount)
{
for(int x=0;x<8;x++){
LED_WrCmd(0xb0 + x); //0xb0+0~7表示页0~7
LED_WrCmd(0x00); //0x00+0~16表示将128列分成16组其地址在某组中的第几列
LED_WrCmd(0x10); //0x10+0~16表示将128列分成16组其地址所在第几组
for(int y=0;y<OV7620_H-1;y++){
uint8_t rev = bin8_rev(gpHREF[y][x+1]);
LED_WrDat(rev); //输出反转二进制
}
}
}
//串口接收中断
void UART_RX_ISR(uint16_t byteRec){
for(int y=0;y<OV7620_H-1;y++){
for(int x=0;x<8;x++){
printBin(gpHREF[y][x+1]);
}
printf("\r\n");
}
printf("\r\n\r\n");
}
int main(void)
{
DelayInit();
/* 打印串口及小灯 */
GPIO_QuickInit(HW_GPIOC, 3, kGPIO_Mode_OPP);
UART_QuickInit(UART0_RX_PB16_TX_PB17, 115200);
/* 注册中断回调函数 */
UART_CallbackRxInstall(HW_UART0, UART_RX_ISR);
/* 开启UART Rx中断 */
UART_ITDMAConfig(HW_UART0, kUART_IT_Rx, true);
initOLED();
initCamera();
while(1)
{
}
}