diff --git a/server/src/Peripherals/CameraClient.cs b/server/src/Peripherals/CameraClient.cs index 9dffbee..3bf0ffb 100644 --- a/server/src/Peripherals/CameraClient.cs +++ b/server/src/Peripherals/CameraClient.cs @@ -38,17 +38,306 @@ class Camera public async ValueTask> Init() { var i2c = new Peripherals.I2cClient.I2c(this.address, this.port, this.timeout); - var ret = await i2c.WriteData(0x78, new byte[] { 0x30, 0x08, 0x02 }, Peripherals.I2cClient.I2cProtocol.I2c); - if (!ret.IsSuccessful) - { - logger.Error($"I2C write failed during camera initialization for {this.address}:{this.port}, error: {ret.Error}"); - return new(ret.Error); - } + const uint devAddr = 0x78; + const Peripherals.I2cClient.I2cProtocol proto = Peripherals.I2cClient.I2cProtocol.I2c; + const UInt16 V_CMOS_DISP = 480; + const UInt16 H_CMOS_DISP = 640; + const UInt16 TOTAL_H_PIXEL = 2570; + const UInt16 TOTAL_V_PIXEL = 980; + var data = new byte[][] { + // 软件复位寄存器,恢复初始值 + new byte[] {0x30, 0x0a, 0x00}, // 0x300a = 0x00 + new byte[] {0x30, 0x0b, 0x00}, // 0x300b = 0x00 + new byte[] {0x30, 0x08, 0x82}, // 0x3008 = 0x82, Bit[7]:复位 Bit[6]:电源休眠 + new byte[] {0x30, 0x08, 0x02}, // 0x3008 = 0x02, 正常工作模式 + new byte[] {0x31, 0x03, 0x02}, // 0x3103 = 0x02, Bit[1]:1 PLL Clock - if (!ret.Value) + // 引脚输入/输出控制 + new byte[] {0x30, 0x17, 0xff}, // 0x3017 = 0xff, FREX/VSYNC/HREF/PCLK/D[9:6] + new byte[] {0x30, 0x18, 0xff}, // 0x3018 = 0xff, D[5:0]/GPIO1/GPIO0 + new byte[] {0x30, 0x37, 0x13}, // 0x3037 = 0x13, PLL分频控制 + new byte[] {0x31, 0x08, 0x01}, // 0x3108 = 0x01, 系统根分频器 + new byte[] {0x36, 0x30, 0x36}, // 0x3630 = 0x36 + new byte[] {0x36, 0x31, 0x0e}, // 0x3631 = 0x0e + new byte[] {0x36, 0x32, 0xe2}, // 0x3632 = 0xe2 + new byte[] {0x36, 0x33, 0x12}, // 0x3633 = 0x12 + new byte[] {0x36, 0x21, 0xe0}, // 0x3621 = 0xe0 + new byte[] {0x37, 0x04, 0xa0}, // 0x3704 = 0xa0 + new byte[] {0x37, 0x03, 0x5a}, // 0x3703 = 0x5a + new byte[] {0x37, 0x15, 0x78}, // 0x3715 = 0x78 + new byte[] {0x37, 0x17, 0x01}, // 0x3717 = 0x01 + new byte[] {0x37, 0x0b, 0x60}, // 0x370b = 0x60 + new byte[] {0x37, 0x05, 0x1a}, // 0x3705 = 0x1a + new byte[] {0x39, 0x05, 0x02}, // 0x3905 = 0x02 + new byte[] {0x39, 0x06, 0x10}, // 0x3906 = 0x10 + new byte[] {0x39, 0x01, 0x0a}, // 0x3901 = 0x0a + new byte[] {0x37, 0x31, 0x12}, // 0x3731 = 0x12 + new byte[] {0x36, 0x00, 0x08}, // 0x3600 = 0x08, VCM控制,用于自动聚焦 + new byte[] {0x36, 0x01, 0x33}, // 0x3601 = 0x33, VCM控制,用于自动聚焦 + new byte[] {0x30, 0x2d, 0x60}, // 0x302d = 0x60, 系统控制 + new byte[] {0x36, 0x20, 0x52}, // 0x3620 = 0x52 + new byte[] {0x37, 0x1b, 0x20}, // 0x371b = 0x20 + new byte[] {0x47, 0x1c, 0x50}, // 0x471c = 0x50 + new byte[] {0x3a, 0x13, 0x43}, // 0x3a13 = 0x43, AEC(自动曝光控制) + new byte[] {0x3a, 0x18, 0x00}, // 0x3a18 = 0x00, AEC增益上限 + new byte[] {0x3a, 0x19, 0xf8}, // 0x3a19 = 0xf8, AEC增益上限 + new byte[] {0x36, 0x35, 0x13}, // 0x3635 = 0x13 + new byte[] {0x36, 0x36, 0x03}, // 0x3636 = 0x03 + new byte[] {0x36, 0x34, 0x40}, // 0x3634 = 0x40 + new byte[] {0x36, 0x22, 0x01}, // 0x3622 = 0x01 + new byte[] {0x3c, 0x01, 0x34}, // 0x3c01 = 0x34 + new byte[] {0x3c, 0x04, 0x28}, // 0x3c04 = 0x28 + new byte[] {0x3c, 0x05, 0x98}, // 0x3c05 = 0x98 + new byte[] {0x3c, 0x06, 0x00}, // 0x3c06 = 0x00, light meter 1 阈值[15:8] + new byte[] {0x3c, 0x07, 0x08}, // 0x3c07 = 0x08, light meter 1 阈值[7:0] + new byte[] {0x3c, 0x08, 0x00}, // 0x3c08 = 0x00, light meter 2 阈值[15:8] + new byte[] {0x3c, 0x09, 0x1c}, // 0x3c09 = 0x1c, light meter 2 阈值[7:0] + new byte[] {0x3c, 0x0a, 0x9c}, // 0x3c0a = 0x9c, sample number[15:8] + new byte[] {0x3c, 0x0b, 0x40}, // 0x3c0b = 0x40, sample number[7:0] + new byte[] {0x38, 0x10, 0x00}, // 0x3810 = 0x00, Timing Hoffset[11:8] + new byte[] {0x38, 0x11, 0x10}, // 0x3811 = 0x10, Timing Hoffset[7:0] + new byte[] {0x38, 0x12, 0x00}, // 0x3812 = 0x00, Timing Voffset[10:8] + new byte[] {0x37, 0x08, 0x64}, // 0x3708 = 0x64 + new byte[] {0x40, 0x01, 0x02}, // 0x4001 = 0x02, BLC(黑电平校准)补偿起始行号 + new byte[] {0x40, 0x05, 0x1a}, // 0x4005 = 0x1a, BLC(黑电平校准)补偿始终更新 + new byte[] {0x30, 0x00, 0x00}, // 0x3000 = 0x00, 系统块复位控制 + new byte[] {0x30, 0x04, 0xff}, // 0x3004 = 0xff, 时钟使能控制 + new byte[] {0x43, 0x00, 0x61}, // 0x4300 = 0x61, 格式控制 RGB565 + new byte[] {0x50, 0x1f, 0x01}, // 0x501f = 0x01, ISP RGB + new byte[] {0x44, 0x0e, 0x00}, // 0x440e = 0x00 + new byte[] {0x50, 0x00, 0xa7}, // 0x5000 = 0xa7, ISP控制 + new byte[] {0x3a, 0x0f, 0x30}, // 0x3a0f = 0x30, AEC控制;stable range in high + new byte[] {0x3a, 0x10, 0x28}, // 0x3a10 = 0x28, AEC控制;stable range in low + new byte[] {0x3a, 0x1b, 0x30}, // 0x3a1b = 0x30, AEC控制;stable range out high + new byte[] {0x3a, 0x1e, 0x26}, // 0x3a1e = 0x26, AEC控制;stable range out low + new byte[] {0x3a, 0x11, 0x60}, // 0x3a11 = 0x60, AEC控制; fast zone high + new byte[] {0x3a, 0x1f, 0x14}, // 0x3a1f = 0x14, AEC控制; fast zone low + + // LENC(镜头校正)控制 0x5800~0x583d + new byte[] {0x58, 0x00, 0x23}, + new byte[] {0x58, 0x01, 0x14}, + new byte[] {0x58, 0x02, 0x0f}, + new byte[] {0x58, 0x03, 0x0f}, + new byte[] {0x58, 0x04, 0x12}, + new byte[] {0x58, 0x05, 0x26}, + new byte[] {0x58, 0x06, 0x0c}, + new byte[] {0x58, 0x07, 0x08}, + new byte[] {0x58, 0x08, 0x05}, + new byte[] {0x58, 0x09, 0x05}, + new byte[] {0x58, 0x0a, 0x08}, + new byte[] {0x58, 0x0b, 0x0d}, + new byte[] {0x58, 0x0c, 0x08}, + new byte[] {0x58, 0x0d, 0x03}, + new byte[] {0x58, 0x0e, 0x00}, + new byte[] {0x58, 0x0f, 0x00}, + new byte[] {0x58, 0x10, 0x03}, + new byte[] {0x58, 0x11, 0x09}, + new byte[] {0x58, 0x12, 0x07}, + new byte[] {0x58, 0x13, 0x03}, + new byte[] {0x58, 0x14, 0x00}, + new byte[] {0x58, 0x15, 0x01}, + new byte[] {0x58, 0x16, 0x03}, + new byte[] {0x58, 0x17, 0x08}, + new byte[] {0x58, 0x18, 0x0d}, + new byte[] {0x58, 0x19, 0x08}, + new byte[] {0x58, 0x1a, 0x05}, + new byte[] {0x58, 0x1b, 0x06}, + new byte[] {0x58, 0x1c, 0x08}, + new byte[] {0x58, 0x1d, 0x0e}, + new byte[] {0x58, 0x1e, 0x29}, + new byte[] {0x58, 0x1f, 0x17}, + new byte[] {0x58, 0x20, 0x11}, + new byte[] {0x58, 0x21, 0x11}, + new byte[] {0x58, 0x22, 0x15}, + new byte[] {0x58, 0x23, 0x28}, + new byte[] {0x58, 0x24, 0x46}, + new byte[] {0x58, 0x25, 0x26}, + new byte[] {0x58, 0x26, 0x08}, + new byte[] {0x58, 0x27, 0x26}, + new byte[] {0x58, 0x28, 0x64}, + new byte[] {0x58, 0x29, 0x26}, + new byte[] {0x58, 0x2a, 0x24}, + new byte[] {0x58, 0x2b, 0x22}, + new byte[] {0x58, 0x2c, 0x24}, + new byte[] {0x58, 0x2d, 0x24}, + new byte[] {0x58, 0x2e, 0x06}, + new byte[] {0x58, 0x2f, 0x22}, + new byte[] {0x58, 0x30, 0x40}, + new byte[] {0x58, 0x31, 0x42}, + new byte[] {0x58, 0x32, 0x24}, + new byte[] {0x58, 0x33, 0x26}, + new byte[] {0x58, 0x34, 0x24}, + new byte[] {0x58, 0x35, 0x22}, + new byte[] {0x58, 0x36, 0x22}, + new byte[] {0x58, 0x37, 0x26}, + new byte[] {0x58, 0x38, 0x44}, + new byte[] {0x58, 0x39, 0x24}, + new byte[] {0x58, 0x3a, 0x26}, + new byte[] {0x58, 0x3b, 0x28}, + new byte[] {0x58, 0x3c, 0x42}, + new byte[] {0x58, 0x3d, 0xce}, + + // AWB(自动白平衡控制) 0x5180~0x519e + new byte[] {0x51, 0x80, 0xff}, + new byte[] {0x51, 0x81, 0xf2}, + new byte[] {0x51, 0x82, 0x00}, + new byte[] {0x51, 0x83, 0x14}, + new byte[] {0x51, 0x84, 0x25}, + new byte[] {0x51, 0x85, 0x24}, + new byte[] {0x51, 0x86, 0x09}, + new byte[] {0x51, 0x87, 0x09}, + new byte[] {0x51, 0x88, 0x09}, + new byte[] {0x51, 0x89, 0x75}, + new byte[] {0x51, 0x8a, 0x54}, + new byte[] {0x51, 0x8b, 0xe0}, + new byte[] {0x51, 0x8c, 0xb2}, + new byte[] {0x51, 0x8d, 0x42}, + new byte[] {0x51, 0x8e, 0x3d}, + new byte[] {0x51, 0x8f, 0x56}, + new byte[] {0x51, 0x90, 0x46}, + new byte[] {0x51, 0x91, 0xf8}, + new byte[] {0x51, 0x92, 0x04}, + new byte[] {0x51, 0x93, 0x70}, + new byte[] {0x51, 0x94, 0xf0}, + new byte[] {0x51, 0x95, 0xf0}, + new byte[] {0x51, 0x96, 0x03}, + new byte[] {0x51, 0x97, 0x01}, + new byte[] {0x51, 0x98, 0x04}, + new byte[] {0x51, 0x99, 0x12}, + new byte[] {0x51, 0x9a, 0x04}, + new byte[] {0x51, 0x9b, 0x00}, + new byte[] {0x51, 0x9c, 0x06}, + new byte[] {0x51, 0x9d, 0x82}, + new byte[] {0x51, 0x9e, 0x38}, + + // Gamma(伽马)控制 0x5480~0x5490 + new byte[] {0x54, 0x80, 0x01}, + new byte[] {0x54, 0x81, 0x08}, + new byte[] {0x54, 0x82, 0x14}, + new byte[] {0x54, 0x83, 0x28}, + new byte[] {0x54, 0x84, 0x51}, + new byte[] {0x54, 0x85, 0x65}, + new byte[] {0x54, 0x86, 0x71}, + new byte[] {0x54, 0x87, 0x7d}, + new byte[] {0x54, 0x88, 0x87}, + new byte[] {0x54, 0x89, 0x91}, + new byte[] {0x54, 0x8a, 0x9a}, + new byte[] {0x54, 0x8b, 0xaa}, + new byte[] {0x54, 0x8c, 0xb8}, + new byte[] {0x54, 0x8d, 0xcd}, + new byte[] {0x54, 0x8e, 0xdd}, + new byte[] {0x54, 0x8f, 0xea}, + new byte[] {0x54, 0x90, 0x1d}, + + // CMX(彩色矩阵控制) 0x5381~0x538b + new byte[] {0x53, 0x81, 0x1e}, + new byte[] {0x53, 0x82, 0x5b}, + new byte[] {0x53, 0x83, 0x08}, + new byte[] {0x53, 0x84, 0x0a}, + new byte[] {0x53, 0x85, 0x7e}, + new byte[] {0x53, 0x86, 0x88}, + new byte[] {0x53, 0x87, 0x7c}, + new byte[] {0x53, 0x88, 0x6c}, + new byte[] {0x53, 0x89, 0x10}, + new byte[] {0x53, 0x8a, 0x01}, + new byte[] {0x53, 0x8b, 0x98}, + + // SDE(特殊数码效果)控制 0x5580~0x558b + new byte[] {0x55, 0x80, 0x06}, + new byte[] {0x55, 0x83, 0x40}, + new byte[] {0x55, 0x84, 0x10}, + new byte[] {0x55, 0x89, 0x10}, + new byte[] {0x55, 0x8a, 0x00}, + new byte[] {0x55, 0x8b, 0xf8}, + + new byte[] {0x50, 0x1d, 0x40}, //ISP MISC + + // CIP(颜色插值)控制 0x5300~0x530c + new byte[] {0x53, 0x00, 0x08}, + new byte[] {0x53, 0x01, 0x30}, + new byte[] {0x53, 0x02, 0x10}, + new byte[] {0x53, 0x03, 0x00}, + new byte[] {0x53, 0x04, 0x08}, + new byte[] {0x53, 0x05, 0x30}, + new byte[] {0x53, 0x06, 0x08}, + new byte[] {0x53, 0x07, 0x16}, + new byte[] {0x53, 0x09, 0x08}, + new byte[] {0x53, 0x0a, 0x30}, + new byte[] {0x53, 0x0b, 0x04}, + new byte[] {0x53, 0x0c, 0x06}, + new byte[] {0x50, 0x25, 0x00}, + + new byte[] {0x30, 0x35, 0x11}, // 0x3035 = 0x11, 系统时钟分频 input clock =24Mhz, PCLK = 48Mhz + new byte[] {0x30, 0x36, 0x3c}, // 0x3036 = 0x3c, PLL倍频 + new byte[] {0x3c, 0x07, 0x08}, // 0x3c07 = 0x08 + + // 时序控制 0x3800~0x3821 + new byte[] {0x38, 0x20, 0x46}, // 0x3820 = 0x46 + new byte[] {0x38, 0x21, 0x01}, // 0x3821 = 0x01 + new byte[] {0x38, 0x14, 0x31}, // 0x3814 = 0x31 + new byte[] {0x38, 0x15, 0x31}, // 0x3815 = 0x31 + new byte[] {0x38, 0x00, 0x00}, // 0x3800 = 0x00 + new byte[] {0x38, 0x01, 0x00}, // 0x3801 = 0x00 + new byte[] {0x38, 0x02, 0x00}, // 0x3802 = 0x00 + new byte[] {0x38, 0x03, 0x04}, // 0x3803 = 0x04 + new byte[] {0x38, 0x04, 0x0a}, // 0x3804 = 0x0a + new byte[] {0x38, 0x05, 0x3f}, // 0x3805 = 0x3f + new byte[] {0x38, 0x06, 0x07}, // 0x3806 = 0x07 + new byte[] {0x38, 0x07, 0x9b}, // 0x3807 = 0x9b + + // 设置输出像素个数 + new byte[] {0x38, 0x08, unchecked((byte)((H_CMOS_DISP >> 8) & 0x0F))}, // 0x3808, DVP输出水平像素点数高4位 + new byte[] {0x38, 0x09, unchecked((byte)(H_CMOS_DISP & 0xFF))}, // 0x3809, DVP输出水平像素点数低8位 + new byte[] {0x38, 0x0A, unchecked((byte)((V_CMOS_DISP >> 8) & 0x07))}, // 0x380a, DVP输出垂直像素点数高3位 + new byte[] {0x38, 0x0B, unchecked((byte)(V_CMOS_DISP & 0xFF))}, // 0x380b, DVP输出垂直像素点数低8位 + new byte[] {0x38, 0x0C, unchecked((byte)((TOTAL_H_PIXEL >> 8) & 0x1F))}, // 0x380c, 水平总像素大小高5位 + new byte[] {0x38, 0x0D, unchecked((byte)(TOTAL_H_PIXEL & 0xFF))}, // 0x380d, 水平总像素大小低8位 + new byte[] {0x38, 0x0E, unchecked((byte)((TOTAL_V_PIXEL >> 8) & 0x1F))}, // 0x380e, 垂直总像素大小高5位 + new byte[] {0x38, 0x0F, unchecked((byte)(TOTAL_V_PIXEL & 0xFF))}, // 0x380f, 垂直总像素大小低8位 + + new byte[] {0x38, 0x13, 0x06}, // 0x3813 = 0x06 + new byte[] {0x36, 0x18, 0x00}, // 0x3618 = 0x00 + new byte[] {0x36, 0x12, 0x29}, // 0x3612 = 0x29 + new byte[] {0x37, 0x09, 0x52}, // 0x3709 = 0x52 + new byte[] {0x37, 0x0c, 0x03}, // 0x370c = 0x03 + new byte[] {0x3a, 0x02, 0x17}, // 0x3a02 = 0x17, 60Hz max exposure + new byte[] {0x3a, 0x03, 0x10}, // 0x3a03 = 0x10, 60Hz max exposure + new byte[] {0x3a, 0x14, 0x17}, // 0x3a14 = 0x17, 50Hz max exposure + new byte[] {0x3a, 0x15, 0x10}, // 0x3a15 = 0x10, 50Hz max exposure + new byte[] {0x40, 0x04, 0x02}, // 0x4004 = 0x02, BLC(背光) 2 lines + new byte[] {0x47, 0x13, 0x03}, // 0x4713 = 0x03, JPEG mode 3 + new byte[] {0x44, 0x07, 0x04}, // 0x4407 = 0x04, 量化标度 + new byte[] {0x46, 0x0c, 0x22}, // 0x460c = 0x22 + new byte[] {0x48, 0x37, 0x22}, // 0x4837 = 0x22, DVP CLK divider + new byte[] {0x38, 0x24, 0x02}, // 0x3824 = 0x02, DVP CLK divider + new byte[] {0x50, 0x01, 0xa3}, // 0x5001 = 0xa3, ISP控制 + new byte[] {0x3b, 0x07, 0x0a}, // 0x3b07 = 0x0a, 帧曝光模式 + + // 彩条测试使能 + new byte[] {0x50, 0x3d, 0x00}, // 0x503d = 0x00, 0x00:正常模式 0x80:彩条显示 + + // 测试闪光灯功能 + new byte[] {0x30, 0x16, 0x02}, // 0x3016 = 0x02 + new byte[] {0x30, 0x1c, 0x02}, // 0x301c = 0x02 + new byte[] {0x30, 0x19, 0x02}, // 0x3019 = 0x02, 打开闪光灯 + new byte[] {0x30, 0x19, 0x00}, // 0x3019 = 0x00, 关闭闪光灯 + }; + + foreach (var cmd in data) { - logger.Error($"I2C write returned unsuccessful result during camera initialization for {this.address}:{this.port}"); - return false; + var ret = await i2c.WriteData(devAddr, cmd, proto); + + if (!ret.IsSuccessful) + { + logger.Error($"I2C write 0x{devAddr.ToString("X")} failed: {BitConverter.ToString(cmd)} error: {ret.Error}"); + return new(ret.Error); + } + + if (!ret.Value) + { + logger.Error($"I2C write 0x{devAddr.ToString("X")} returned false: {BitConverter.ToString(cmd)}"); + return false; + } } return true;