这篇按照例程学习测试DCI驱动OV2640摄像头,显示在LCD屏幕上。
一、数字摄像头接口--DCI
1.1、DCI简介
数字摄像头接口是一个同步并行接口,可以从数字摄像头捕获视频和图像信息。它支持不同的颜色空间图像,例如YUV/RGB,以及压缩格式如JPEG。支持CCIR656视频解码器格式并执行额外的图像处理。
1.2. 主要特性
数字视频和图像的捕获;
支持8位、 10位、 12位或14位并行接口;
DMA高效传输;
支持视频和图像裁剪;
支持不同的像素数字编码格式,如YCbCr422/RGB565/YUV420/Bayer;
支持JPEG压缩格式;
支持内嵌码同步和硬件同步;
支持CCIR656视频接口和传统传感器接口。
1.3、结构框图
二、硬件部分
DCI电路图部分
测试使用的OV2640摄像头,使用8位并行总线。测试DCI时,需要将相应的跳线到DCI功能。
三、程序部分
主要程序部分如下:
3.1、dci_ov2640.c
这部分程序对DCI端口的配置,ov2640摄像头初始化、参数的设置及读取。
/*!
\file dci_ov2640.c
\brief DCI config file
\version 2024-01-05, V1.2.0, demo for GD32H7xx
*/
/*
Copyright (c) 2024, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "dci_ov2640.h"
#include "dci_ov2640_init_table.h"
#include "gd32h7xx.h"
#include "systick.h"
/*!
\brief configure the DCI to interface with the camera module
\param[in] none
\param[out] none
\retval none
*/
void dci_config(void)
{
dci_parameter_struct dci_struct;
dma_single_data_parameter_struct dma_single_struct;;
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_GPIOE);
rcu_periph_clock_enable(RCU_GPIOH);
rcu_periph_clock_enable(RCU_GPIOG);
rcu_periph_clock_enable(RCU_DCI);
/* DCI GPIO AF configuration */
/* configure DCI_PIXCLK(PE3), DCI_VSYNC(PB7), DCI_HSYNC(PA4) */
gpio_af_set(GPIOE, GPIO_AF_13, GPIO_PIN_3);
gpio_af_set(GPIOB, GPIO_AF_13, GPIO_PIN_7);
gpio_af_set(GPIOA, GPIO_AF_13, GPIO_PIN_4);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_3);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_7);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_4);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_4);
/* configure DCI_D0(PC6), DCI_D1(PH10) DCI_D2(PC8), DCI_D3(PC9), DCI_D4(PE4), DCI_D5(PB6), DCI_D6(PE5), DCI_D7(PE6) */
gpio_af_set(GPIOC, GPIO_AF_13, GPIO_PIN_6);
gpio_af_set(GPIOC, GPIO_AF_13, GPIO_PIN_8);
gpio_af_set(GPIOC, GPIO_AF_13, GPIO_PIN_9);
gpio_af_set(GPIOB, GPIO_AF_13, GPIO_PIN_6);
gpio_af_set(GPIOE, GPIO_AF_13, GPIO_PIN_4);
gpio_af_set(GPIOE, GPIO_AF_13, GPIO_PIN_5);
gpio_af_set(GPIOE, GPIO_AF_13, GPIO_PIN_6);
gpio_af_set(GPIOH, GPIO_AF_13, GPIO_PIN_10);
gpio_mode_set(GPIOH, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
gpio_output_options_set(GPIOH, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_10);
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_6);
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_9);
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_8);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_6);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_4);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_4);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_5);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_6);
/* DCI configuration */
dci_struct.capture_mode = DCI_CAPTURE_MODE_CONTINUOUS;
dci_struct.clock_polarity = DCI_CK_POLARITY_RISING;
dci_struct.hsync_polarity = DCI_HSYNC_POLARITY_LOW;
dci_struct.vsync_polarity = DCI_VSYNC_POLARITY_LOW;
dci_struct.frame_rate = DCI_FRAME_RATE_ALL;
dci_struct.interface_format = DCI_INTERFACE_FORMAT_8BITS;
dci_init(&dci_struct);
/* DCI DMA configuration */
rcu_periph_clock_enable(RCU_DMA1);
rcu_periph_clock_enable(RCU_DMAMUX);
dma_single_data_para_struct_init(&dma_single_struct);
dma_deinit(DMA1, DMA_CH7);
dma_single_struct.request = DMA_REQUEST_DCI;
dma_single_struct.periph_addr = (uint32_t)DCI_DATA_ADDRESS;
dma_single_struct.memory0_addr = (uint32_t) 0xC1000000;//0xC0000000;
dma_single_struct.direction = DMA_PERIPH_TO_MEMORY;
dma_single_struct.number = 38400;
dma_single_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_single_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_single_struct.periph_memory_width = DMA_PERIPH_WIDTH_32BIT;
dma_single_struct.circular_mode = DMA_CIRCULAR_MODE_ENABLE;
dma_single_struct.priority = DMA_PRIORITY_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH7, &dma_single_struct);
}
/*!
\brief DCI camera outsize set
\param[in] width: outsize width
\param[in] height: outsize height
\param[out] none
\retval 0x00 or 0xFF
*/
uint8_t ov2640_outsize_set(uint16_t width, uint16_t height)
{
uint16_t outh;
uint16_t outw;
uint8_t temp;
if(width % 4) {
return 0xFF;
}
if(height % 4) {
return 0xFF;
}
outw = width / 4;
outh = height / 4;
dci_byte_write(0xFF, 0x00);
dci_byte_write(0xE0, 0x04);
dci_byte_write(0x5A, outw & 0xFF);
dci_byte_write(0x5B, outh & 0xFF);
temp = (outw >> 8) & 0x03;
temp |= (outh >> 6) & 0x04;
dci_byte_write(0x5C, temp);
dci_byte_write(0xE0, 0x00);
return 0;
}
/*!
\brief DCI camera initialization
\param[in] none
\param[out] none
\retval 0x00 or 0xFF
*/
uint8_t dci_ov2640_init(void)
{
uint8_t i;
sccb_config();
dci_config();
ckout0_init();
delay_1ms(100);
/* OV2640 reset */
if(dci_byte_write(0xFF, 0x01) != 0) {
return 0xFF;
}
if(dci_byte_write(0x12, 0x80) != 0) {
return 0xFF;
}
delay_1ms(10);
for(i = 0; i < sizeof(ov2640_svga_init_reg_tbl) / 2; i++) {
if(0 != dci_byte_write(ov2640_svga_init_reg_tbl[i][0], ov2640_svga_init_reg_tbl[i][1])) {
return 0xFF;
}
}
delay_1ms(100);
for(i = 0; i < (sizeof(ov2640_rgb565_reg_tbl) / 2); i++) {
if(0 != dci_byte_write(ov2640_rgb565_reg_tbl[i][0], ov2640_rgb565_reg_tbl[i][1])) {
return 0xFF;
}
}
delay_1ms(100);
ov2640_outsize_set(320, 240);
return 0;
}
/*!
\brief ckout0 initialization
\param[in] none
\param[out] none
\retval none
*/
void ckout0_init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
gpio_af_set(GPIOA, GPIO_AF_CKOUT, GPIO_PIN_8);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_8);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_8);
rcu_ckout0_config(RCU_CKOUT0SRC_HXTAL, RCU_CKOUT0_DIV2);
}
/*!
\brief read the ov2640 manufacturer identifier
\param[in] ov2640id: pointer to the ov2640 manufacturer struct
\param[out] none
\retval 0x00 or 0xFF
*/
uint8_t dci_ov2640_id_read(ov2640_id_struct *ov2640id)
{
uint8_t temp;
dci_byte_write(0xFF, 0x01);
if(dci_byte_read(OV2640_MIDH, &temp) != 0) {
return 0xFF;
}
ov2640id->manufacturer_id1 = temp;
if(dci_byte_read(OV2640_MIDL, &temp) != 0) {
return 0xFF;
}
ov2640id->manufacturer_id2 = temp;
if(dci_byte_read(OV2640_VER, &temp) != 0) {
return 0xFF;
}
ov2640id->version = temp;
if(dci_byte_read(OV2640_PID, &temp) != 0) {
return 0xFF;
}
ov2640id->pid = temp;
return 0x00;
}
3.2、main.c
初始化ov2640、lcd、DMA、DCI中断。
#include "main.h"
ov2640_id_struct ov2640id;
extern void dci_hdl(void);
void cache_enable(void);
int main(void)
{
cache_enable();
systick_config();
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(DMA1_Channel7_IRQn, 0U, 1U);
gd_eval_lcd_init();
init_usart(115200);
dci_ov2640_init();
dci_ov2640_id_read(&ov2640id);
/* DMA interrupt and channel enable */
dma_interrupt_enable(DMA1, DMA_CH7, DMA_CHXCTL_FTFIE);
dma_channel_enable(DMA1, DMA_CH7);
/* DCI enable */
dci_enable();
dci_capture_enable();
while(1)
{
dci_hdl();
}
}
void cache_enable(void)
{
/* enable i-cache */
SCB_EnableICache();
/* enable d-cache */
SCB_EnableDCache();
}
3.3、gd32h7xx_it.c部分程序
这里主要处理DMA中断,将DCI数据转存到LCD缓存。
/*!
\brief this function handles SysTick exception
\param[in] none
\param[out] none
\retval none
*/
void SysTick_Handler(void)
{
delay_decrement();
}
uint8_t ff=0;
void DMA1_Channel7_IRQHandler(void)
{
if(dma_interrupt_flag_get(DMA1, DMA_CH7, DMA_INT_FLAG_FTF)) {
dma_channel_disable(DMA1, DMA_CH7);
ff=1;
dma_interrupt_flag_clear(DMA1, DMA_CH7, DMA_INT_FLAG_FTF);
dma_channel_enable(DMA1, DMA_CH7);
}
}
void dci_hdl(void)
{
int i = 0, x = 0, y = 0;
if(ff==1)
{
ff=0;
for(x = 0; x < 320; x++)
{
for(y = 0; y < 240; y++)
{
*(uint16_t *)(0xC0000000 + 2 * ((800 * y) + x)) = *(uint16_t *)(0xC1000000 + 2 * ((320 * y) + x));
}
}
}
}
例程是在DMA中断里面处理数据转存,我将要实现的功能在DMA中断中处理时,数据转换一次后,后面不在处理,这里将数据转存的过程移到外部函数处理。
四、运行结果
下载程序后,显示屏显示如下
五、附件
程序源码: