initial commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using LibUsbDotNet;
|
||||
using LibUsbDotNet.LibUsb;
|
||||
using RTCSync.Utils;
|
||||
|
||||
namespace RTCSync.Services;
|
||||
|
||||
public abstract class DeviceDispatcher
|
||||
{
|
||||
public static IUsbDevice? SetUpDevice()
|
||||
{
|
||||
var context = new UsbContext();
|
||||
context.SetDebugLevel(LogLevel.Info); // уровень логгирования winusb
|
||||
|
||||
var list = context.List();
|
||||
Console.WriteLine($"Всего USB устройств: {list.Count}");
|
||||
|
||||
foreach (var d in list)
|
||||
Console.WriteLine($" VID={d.VendorId:X4} PID={d.ProductId:X4}");
|
||||
|
||||
var device = list.FirstOrDefault(d => d.VendorId == 0x1A86 && d.ProductId == 0x5512);
|
||||
if (device == null)
|
||||
{
|
||||
Console.WriteLine("Не найдено");
|
||||
return null;
|
||||
}
|
||||
Console.WriteLine("Найдено, открываем...");
|
||||
device.Open();
|
||||
device.ClaimInterface(0);
|
||||
Console.WriteLine("Открыто успешно");
|
||||
I2CUtils.CH341Init(device);
|
||||
return device;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using LibUsbDotNet.LibUsb;
|
||||
using RTCSync.Models;
|
||||
using RTCSync.Utils;
|
||||
|
||||
namespace RTCSync.Services;
|
||||
|
||||
public class DeviceReaders
|
||||
{
|
||||
public static TimeModel GetTimeRTC(IUsbDevice device)
|
||||
{
|
||||
// Читаем все 7 байт времени начиная с регистра 0x00
|
||||
var time = I2CUtils.ReadWithRetry(device, 0x68, 0x00, 8);
|
||||
var timeModel = new TimeModel(time);
|
||||
return timeModel;
|
||||
}
|
||||
|
||||
public static void PrintTimeRTC(IUsbDevice device)
|
||||
{
|
||||
var timeModel = GetTimeRTC(device);
|
||||
PrintTimeRTC(timeModel);
|
||||
}
|
||||
|
||||
public static void PrintTimeRTC(TimeModel timeModel)
|
||||
{
|
||||
Console.WriteLine(timeModel.Time.ToString("HH:mm:ss dd.MM.yyyy"));
|
||||
}
|
||||
|
||||
|
||||
public static byte[] GetStatusRegister(IUsbDevice device)
|
||||
{
|
||||
return I2CUtils.ReadWithRetry(device, 0x68, 0x0F, 1);
|
||||
}
|
||||
|
||||
public static void PrintStatusValues(IUsbDevice device)
|
||||
{
|
||||
var status = GetStatusRegister(device);
|
||||
PrintStatusValues(status);
|
||||
}
|
||||
|
||||
public static void PrintStatusValues(byte[] status)
|
||||
{
|
||||
var statusBits = string.Concat(status.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
|
||||
Console.WriteLine("Status Register (0Fh):");
|
||||
Console.WriteLine("| OSF | 0 | 0 | 0 | EN32kHZ | BSY | A2F | A1F |");
|
||||
Console.WriteLine($"| {statusBits[0], -3} | {statusBits[1]} | {statusBits[2]} | {statusBits[3]} | {statusBits[4], -7} | {statusBits[5], -3} | {statusBits[6], -3} | {statusBits[7], -3} |");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
|
||||
public static byte[] GetControlRegister(IUsbDevice device)
|
||||
{
|
||||
return I2CUtils.ReadWithRetry(device, 0x68, 0x0E, 1);
|
||||
}
|
||||
|
||||
public static void PrintControlValues(IUsbDevice device)
|
||||
{
|
||||
var control= GetControlRegister(device);
|
||||
PrintControlValues(control);
|
||||
}
|
||||
|
||||
public static void PrintControlValues(byte[] control)
|
||||
{
|
||||
var controlBits = string.Concat(control.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
|
||||
Console.WriteLine("Control Register (0Eh):");
|
||||
Console.WriteLine("| EOSC | BBSQW | CONV | RS2 | RS1 | INTCN | A2IE | A1IE |");
|
||||
Console.WriteLine($"| {controlBits[0], -4} | {controlBits[1], -5} | {controlBits[2], -4} | {controlBits[3], -3} | {controlBits[4], -3} | {controlBits[5], -5} | {controlBits[6], -4} | {controlBits[7], -4} |");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
|
||||
public static byte[] GetTemperatureRegisters(IUsbDevice device)
|
||||
{
|
||||
return I2CUtils.ReadWithRetry(device, 0x68, 0x11, 2);
|
||||
}
|
||||
public static void PrintTemperatureValue(IUsbDevice device)
|
||||
{
|
||||
var temp = GetTemperatureRegisters(device);
|
||||
PrintTemperatureValue(temp);
|
||||
}
|
||||
public static void PrintTemperatureValue(byte[] temp)
|
||||
{
|
||||
float tempValue = temp[0];
|
||||
if (temp[1] == 0x40)
|
||||
tempValue += 0.25F;
|
||||
else if (temp[1] == 0x80)
|
||||
tempValue += 0.5F;
|
||||
else if (temp[1] == 0xC0)
|
||||
tempValue += 0.75F;
|
||||
|
||||
Console.WriteLine("Температура:");
|
||||
Console.WriteLine(tempValue);
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using LibUsbDotNet.LibUsb;
|
||||
using RTCSync.Utils;
|
||||
using static RTCSync.Utils.BinaryUtils;
|
||||
|
||||
|
||||
namespace RTCSync.Services;
|
||||
|
||||
public class DeviceWriters
|
||||
{
|
||||
public static void SetTime(IUsbDevice device, DateTime dt)
|
||||
{
|
||||
var data = new byte[]
|
||||
{
|
||||
ToBcd(dt.Second), // 0x00 секунды
|
||||
ToBcd(dt.Minute), // 0x01 минуты
|
||||
ToBcd(dt.Hour), // 0x02 часы, бит6=0 → 24h
|
||||
(byte)(dt.DayOfWeek + 1), // 0x03 день недели (в dt 0=вс, 1=пн..., а в DS3231 на 1 больше)
|
||||
ToBcd(dt.Day), // 0x04 число
|
||||
ToBcd(dt.Month), // 0x05 месяц
|
||||
ToBcd(dt.Year % 100) // 0x06 год (последние 2 цифры)
|
||||
};
|
||||
|
||||
I2CUtils.Write(device, 0x68, 0x00, data);
|
||||
}
|
||||
|
||||
public static void ResetOSF(IUsbDevice device, byte[]? status)
|
||||
{
|
||||
status ??= DeviceReaders.GetStatusRegister(device);
|
||||
status[0] &= 0x7F; // очистить бит 7
|
||||
// TODO: убрать хак
|
||||
I2CUtils.Write(device, 0x68, 0x0F, status);
|
||||
I2CUtils.Write(device, 0x68, 0x0F, status);
|
||||
I2CUtils.Write(device, 0x68, 0x0F, status);
|
||||
}
|
||||
|
||||
public static void EnableEOSC(IUsbDevice device, byte[]? control)
|
||||
{
|
||||
control ??= DeviceReaders.GetControlRegister(device);
|
||||
control[0] &= 0x7F; // EOSC = 0 (осциллятор включён) — бит 7 регистра 0x0E
|
||||
// TODO: убрать хак
|
||||
I2CUtils.Write(device, 0x68, 0x0E, control);
|
||||
I2CUtils.Write(device, 0x68, 0x0E, control);
|
||||
I2CUtils.Write(device, 0x68, 0x0E, control);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user