Files
kunlun/dtest/k3d_demo_test/camera.c
2024-09-28 14:24:04 +08:00

380 lines
10 KiB
C

/****************************************************************************
*
* Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
*
* This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics Ltd and MAY NOT
* be copied by any method or incorporated into another program without
* the express written consent of Aerospace C.Power. This Information or any portion
* thereof remains the property of Aerospace C.Power. The Information contained herein
* is believed to be accurate and Aerospace C.Power assumes no responsibility or
* liability for its use in any way and conveys no license or title under
* any patent or copyright and makes no representation or warranty that this
* Information is free from patent or copyright infringement.
*
* ****************************************************************************/
/* os shim includes */
#include "os_types.h"
#include "os_task.h"
#include "os_utils.h"
#include "iot_errno_api.h"
/* common includes */
#include "iot_io.h"
#include "iot_bitops.h"
#include "iot_pkt_api.h"
#include "iot_ipc.h"
#include "iot_plc_lib.h"
#include "iot_dbglog_api.h"
#include "iot_config.h"
/* driver includes */
#include "iot_clock.h"
#include "iot_uart.h"
#include "iot_led.h"
/* cli includes */
#include "iot_cli.h"
#include "iot_uart_h.h"
/* debug includes*/
#include "dbg_io.h"
#include "gpio.h"
#include "apb_glb_reg.h"
#include "hw_reg_api.h"
#include "gpio_mtx_reg.h"
#include "pin_rf.h"
#include "apb.h"
#include "gpio_mtx.h"
#include "gpio_reg.h"
#include "i2c_reg.h"
#include "iot_i2c_api.h"
#include "iot_gpio_api.h"
#include "xc7027_reg.h"
#include "ov9282_reg.h"
#define CAMERA_DEBUG_ENA 0
struct mp3336_module_reg_t {
uint8_t reg;
uint8_t val;
};
const struct mp3336_module_reg_t mp3336_flash_mode[] = {
{0x1, 0xE6},
//{0x6, 0x33},
{0x6, 200},
{0x3, 0x18},
{0x1, 0xF6},
};
iot_i2c_module_cfg_t g_cfg = {0};
iot_i2c_module_cfg_t g_sgm_cfg = {0};
int xc7027_gpio_rst(uint8_t gpio)
{
uint8_t r = ERR_FAIL;
r = iot_gpio_open_as_output(gpio);
if(r != 0)
{
iot_printf("\ngpio_set_direction failed!\n");
}
if (0 != iot_gpio_value_set(gpio, 1)) {
iot_printf("\n WRITE 0 FAILED\n");
r = ERR_FAIL;
} else {
os_delay(100);
if (0 != iot_gpio_value_set(gpio, 0)) {
iot_printf("\n WRITE 1 FAILED\n");
r = ERR_FAIL;
} else {
r = ERR_OK;
}
os_delay(100);
if (0 != iot_gpio_value_set(gpio, 1)) {
iot_printf("\n WRITE 1 FAILED\n");
r = ERR_FAIL;
} else {
r = ERR_OK;
}
}
return r;
}
int xc7027_write_command(uint8_t addr, uint8_t reg1, uint8_t reg2, uint8_t val)
{
uint8_t ret = 0;
char buf[4] = {0};
buf[0] = reg1;
buf[1] = reg2;
buf[2] = val;
ret = iot_i2c_write(g_cfg.port, addr, buf, 3);
os_delay(5);
return ret;
}
int ov9282_write_command(uint8_t addr, uint16_t reg, uint8_t val)
{
uint8_t ret = 0;
char buf[4] = {0};
buf[0] = reg >> 8;
buf[1] = reg & 0xff;
buf[2] = val;
ret = iot_i2c_write(g_cfg.port, addr, buf, 3);
os_delay(5);
return ret;
}
int xc7027_read_command(uint8_t addr, uint16_t reg, uint8_t *data, uint8_t len)
{
uint8_t ret = 0;
char buf[4] = {0};
buf[0] = reg >> 8;
buf[1] = reg & 0xff;
ret = iot_i2c_write(g_cfg.port, addr, buf, 2);
if (ret) {
iot_printf("xc7027_read_command write addr error\n");
return ret;
}
os_delay(5);
ret = iot_i2c_read(g_cfg.port, addr, (char *)data, len);
if (ret) {
iot_printf("xc7027_read_command get value error\n");
return ret;
}
return ret;
}
int mp3336_write_command(uint8_t addr, uint8_t reg, uint8_t val)
{
uint8_t ret = 0;
char buf[4] = {0};
buf[0] = reg;
buf[1] = val;
ret = iot_i2c_write(g_sgm_cfg.port, addr, buf, 2);
os_delay(5);
return ret;
}
int mp3336_read_command(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len)
{
uint8_t ret = 0;
char buf[4] = {0};
buf[0] = reg;
ret = iot_i2c_write(g_sgm_cfg.port, addr, buf, 1);
if (ret) {
iot_printf("xc7027_read_command write addr error\n");
return ret;
}
os_delay(5);
ret = iot_i2c_read(g_sgm_cfg.port, addr, (char *)data, len);
if (ret) {
iot_printf("xc7027_read_command get value error\n");
return ret;
}
return ret;
}
uint8_t g_xc7027_addr = 0x1b;
uint8_t g_ov9282_addr = 0x60;
uint8_t g_mp3336_addr = 0x27;
uint8_t g_xc7027_rst_gpio = 56;
uint8_t g_cfg_test = 0;
void mp3336_flash_mode_cfg()
{
uint8_t val = 0;
int reg_len = 0;
uint8_t reg;
reg_len = sizeof(mp3336_flash_mode)/sizeof(struct mp3336_module_reg_t);
for(uint32_t i = 0; i<reg_len; i++) {
reg = mp3336_flash_mode[i].reg;
if(mp3336_read_command(g_mp3336_addr, reg, &val, 1)) {
iot_printf("mp3336_read_command error\n");
} else {
iot_printf("read value: %02x, write mp3336 reg[%02x: %02x]\n",
val, reg, mp3336_flash_mode[i].val);
val = mp3336_flash_mode[i].val;
if(mp3336_write_command(g_mp3336_addr, reg, val)) {
iot_printf("write mp3336_flash_mode regs error[%d]\n", i);
}
#if CAMERA_DEBUG_ENA
os_delay(10);
if (mp3336_read_command(g_mp3336_addr, reg, &val, 1)) {
iot_printf("mp3336_read_command error\n");
} else {
iot_printf("read value: %02x\n", val);
}
#endif
}
}
}
void camera_module_init()
{
g_cfg.port = IOT_I2C_PORT_0;
g_cfg.nack_wait_num = 1;
g_cfg.baud = 50;
g_cfg.gpio.scl = 57;
g_cfg.gpio.sda = 58;
iot_i2c_module_init(&g_cfg);
g_sgm_cfg.port = IOT_I2C_PORT_1;
g_sgm_cfg.nack_wait_num = 1;
g_sgm_cfg.baud = 50;
g_sgm_cfg.gpio.scl = 59;
g_sgm_cfg.gpio.sda = 60;
iot_i2c_module_init(&g_sgm_cfg);
if (xc7027_gpio_rst(g_xc7027_rst_gpio)) {
iot_printf("xc7027 gpio reset failed\n");
}
// read 0xfffb value
iot_printf("\n[read xc7027 reg]\n");
uint8_t val = 0;
if(xc7027_read_command(g_xc7027_addr, 0xfffb, &val, 1)) {
iot_printf("xc7027_read_command error\n");
} else {
iot_printf("xc7027_read_command value: %02x\n", val);
}
os_delay(10);
iot_printf("[read xc7027 reg]\n");
// send xc7027 pattern
iot_printf("\n[send xc7027 pattern]\n");
int reg_len = 0;
int ret = 0;
uint16_t reg = 0;
reg_len = sizeof(XC7027_default_regs);
for(uint32_t i = 0; i<reg_len/3; i++)
{
reg = (uint16_t)(XC7027_default_regs[i*3] << 8) + XC7027_default_regs[i*3+1];
iot_printf("write config reg[%04x: %02x]\n", reg, XC7027_default_regs[i*3+2]);
ret = xc7027_write_command(g_xc7027_addr, XC7027_default_regs[i*3],
XC7027_default_regs[i*3+1],
XC7027_default_regs[i*3+2]);
if (ret) {
iot_printf("write XC7027_default_regs regs error[%d]\n", i);
}
#if CAMERA_DEBUG_ENA
if(xc7027_read_command(g_xc7027_addr, reg, &val, 1)) {
iot_printf("xc7027_read_command error\n");
} else {
iot_printf("xc7027_read_command value: %02x\n", val);
if (val != XC7027_default_regs[i*3+2]) {
iot_printf("write 7027 failed, i=%d, reg:%04x, value:%02x-%02x\n",
i, reg, XC7027_default_regs[i*3+2], val);
}
}
#endif
}
iot_printf("[send xc7027 pattern]\n");
iot_printf("\n[send xc7027 bypass]\n");
reg_len = sizeof(bypass_on);
for(uint32_t i = 0; i<reg_len/3; i++)
{
reg = (uint16_t)(bypass_on[i*3] << 8) + bypass_on[i*3+1];
iot_printf("write bypass reg[%04x: %02x]\n", reg, bypass_on[i*3+2]);
ret = xc7027_write_command(g_xc7027_addr, bypass_on[i*3],
bypass_on[i*3+1],
bypass_on[i*3+2]);
if (ret) {
iot_printf("write bypass_on regs error[%d]\n", i);
}
#if CAMERA_DEBUG_ENA
if(xc7027_read_command(g_xc7027_addr, reg, &val, 1)) {
iot_printf("xc7027_read_command error\n");
} else {
iot_printf("xc7027_read_command value: %02x\n", val);
if (val != bypass_on[i*3+2]) {
iot_printf("write bypass failed, i=%d, reg:%04x, value:%02x-%02x\n",
i, reg, bypass_on[i*3+2], val);
//while(1);
}
}
#endif
}
iot_printf("[send xc7027 bypass]\n");
// reset pin
iot_printf("\n[reset xc7027 pin]\n");
ret = xc7027_write_command(g_xc7027_addr, 0x00, 0x58, 0x01);
if (ret) {
iot_printf("reset pin error\n");
}
iot_printf("[reset xc7027 pin]\n");
os_delay(10);
// read ov9282 ID reg: 0x300a
iot_printf("\n[read ov9282 reg]\n");
if(xc7027_read_command(g_ov9282_addr, 0x300a, &val, 1)) {
iot_printf("ov9282 read command error\n");
} else {
iot_printf("ov9282 read command value: %02x\n", val);
}
if(xc7027_read_command(g_ov9282_addr, 0x300b, &val, 1)) {
iot_printf("ov9282 read command error\n");
} else {
iot_printf("ov9282 read command value: %02x\n", val);
}
iot_printf("[read ov9282 reg]\n");
// write ov9282 default reg
os_delay(10);
iot_printf("\n[write ov9282 pattern]\n");
reg_len = sizeof(ov9282_init_tab_720_1280_30fps)/sizeof(struct ov_camera_module_reg);
for(uint32_t i = 0; i<reg_len; i++)
{
reg = ov9282_init_tab_720_1280_30fps[i].reg;
iot_printf("write 9282 reg[%04x: %02x]\n", reg, ov9282_init_tab_720_1280_30fps[i].val);
ret = ov9282_write_command(g_ov9282_addr,
ov9282_init_tab_720_1280_30fps[i].reg,
ov9282_init_tab_720_1280_30fps[i].val);
if (ret) {
iot_printf("write ov9282_init_tab_720_1280_30fps regs error[%d]\n", i);
}
#if CAMERA_DEBUG_ENA
os_delay(10);
if(xc7027_read_command(g_ov9282_addr, reg, &val, 1)) {
iot_printf("ov9282 read command error\n");
} else {
iot_printf("ov9282 read command value: %02x\n", val);
if (val != ov9282_init_tab_720_1280_30fps[i].val) {
iot_printf("write 9282 failed, i=%d, reg:%04x, value:%02x-%02x\n",
i, reg, ov9282_init_tab_720_1280_30fps[i].val, val);
//while(1);
}
}
#endif
}
// write mp3336 reg, flash mode
mp3336_flash_mode_cfg();
}