Files
RTCSync/RTCSync.cli/Services/DeviceWriters.cs
T
2026-06-25 12:19:29 +03:00

47 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);
}
}