feat: 添加摄像头初始化的axi寄存器配置

This commit is contained in:
SikongJueluo 2025-07-10 16:16:17 +08:00
parent c7c8cbaeb8
commit fad37ba922
No known key found for this signature in database
1 changed files with 341 additions and 291 deletions

View File

@ -5,8 +5,11 @@ namespace Peripherals.CameraClient;
static class CameraAddr
{
public const UInt32 Base = 0x0000_0000;
public const UInt32 FrameLength = 0x25800;
public const UInt32 BASE = 0x7000_0000;
public const UInt32 STORE_ADDR = BASE + 12;
public const UInt32 STORE_NUM = BASE + 13;
public const UInt32 CAPTURE_ON = BASE + 14;
}
class Camera
@ -19,32 +22,16 @@ class Camera
readonly string address;
private IPEndPoint ep;
/// <summary>
/// 初始化摄像头客户端
/// </summary>
/// <param name="address">摄像头设备IP地址</param>
/// <param name="port">摄像头设备端口</param>
/// <param name="timeout">超时时间(毫秒)</param>
public Camera(string address, int port, int timeout = 2000)
{
if (timeout < 0)
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
this.address = address;
this.port = port;
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
this.timeout = timeout;
}
public async ValueTask<Result<bool>> Init()
{
var i2c = new Peripherals.I2cClient.I2c(this.address, this.port, this.timeout);
const uint devAddr = 0x78;
const Peripherals.I2cClient.I2cProtocol proto = Peripherals.I2cClient.I2cProtocol.I2c;
const uint CAM_I2C_ADDR = 0x78;
const Peripherals.I2cClient.I2cProtocol CAM_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[][] {
const UInt32 FrameLength = V_CMOS_DISP * H_CMOS_DISP * 16 / 32;
static byte[][] data = new byte[][] {
// 软件复位寄存器,恢复初始值
new byte[] {0x30, 0x0a, 0x00}, // 0x300a = 0x00
new byte[] {0x30, 0x0b, 0x00}, // 0x300b = 0x00
@ -323,19 +310,82 @@ class Camera
new byte[] { 0x30, 0x19, 0x00 }, // 0x3019 = 0x00, 关闭闪光灯
};
/// <summary>
/// 初始化摄像头客户端
/// </summary>
/// <param name="address">摄像头设备IP地址</param>
/// <param name="port">摄像头设备端口</param>
/// <param name="timeout">超时时间(毫秒)</param>
public Camera(string address, int port, int timeout = 2000)
{
if (timeout < 0)
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
this.address = address;
this.port = port;
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
this.timeout = timeout;
}
public async ValueTask<Result<bool>> Init()
{
{
var ret = await UDPClientPool.WriteAddr(this.ep, 2, CameraAddr.STORE_ADDR, 0x00);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to write STORE_ADDR to camera at {this.address}:{this.port}, error: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"STORE_ADDR write returned false for camera at {this.address}:{this.port}");
return new(new Exception($"STORE_ADDR write returned false for camera at {this.address}:{this.port}"));
}
}
{
var ret = await UDPClientPool.WriteAddr(this.ep, 2, CameraAddr.STORE_NUM, FrameLength);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to write STORE_NUM to camera at {this.address}:{this.port}, error: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"STORE_NUM write returned false for camera at {this.address}:{this.port}");
return new(new Exception($"STORE_NUM write returned false for camera at {this.address}:{this.port}"));
}
}
{
var ret = await UDPClientPool.WriteAddr(this.ep, 2, CameraAddr.CAPTURE_ON, 0x01);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to write CAPTURE_ON to camera at {this.address}:{this.port}, error: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"CAPTURE_ON write returned false for camera at {this.address}:{this.port}");
return new(new Exception($"CAPTURE_ON write returned false for camera at {this.address}:{this.port}"));
}
}
var i2c = new Peripherals.I2cClient.I2c(this.address, this.port, this.timeout);
foreach (var cmd in data)
{
var ret = await i2c.WriteData(devAddr, cmd, proto);
var ret = await i2c.WriteData(CAM_I2C_ADDR, cmd, CAM_PROTO);
if (!ret.IsSuccessful)
{
logger.Error($"I2C write 0x{devAddr.ToString("X")} failed: {BitConverter.ToString(cmd)} error: {ret.Error}");
logger.Error($"I2C write 0x{CAM_I2C_ADDR.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)}");
logger.Error($"I2C write 0x{CAM_I2C_ADDR.ToString("X")} returned false: {BitConverter.ToString(cmd)}");
return false;
}
}
@ -358,8 +408,8 @@ class Camera
var result = await UDPClientPool.ReadAddr4Bytes(
this.ep,
2, // taskID
CameraAddr.Base,
(int)CameraAddr.FrameLength,
CameraAddr.BASE,
((int)FrameLength),
this.timeout);
if (!result.IsSuccessful)