How to get VBAT and internalReference voltage? I tried the following, without success:
public class VBAT
{
private AdcController adcController3;
private AdcChannel vbat;
private AdcChannel internalReferenceVoltage;
private IntPtr enable_reg;
public VBAT()
{
adcController3 = AdcController.FromName(SC20260.Adc.Controller3.Id);
vbat = adcController3.OpenChannel(SC20260.Adc.Controller3.VBAT);
internalReferenceVoltage = adcController3.OpenChannel(SC20260.Adc.Controller3.InternalReferenceVoltage);
enable_reg = (IntPtr)((0x40000000U + 0x18020000 + 0x6300 + 8));
}
public float Voltage
{
get
{
int enable_val = Marshal.ReadInt32(enable_reg);
enable_val |= (1 << 24);
Marshal.WriteInt32(enable_reg, enable_val);
// The VBATEN bit of the ADCx_CCR register is used to switch to the battery voltage. As the
// VBAT voltage could be higher than VDDA, to ensure the correct operation of the ADC, the
// VBAT pin is internally connected to a bridge divider by 4.
float voltage = (float)(vbat.ReadValue() * InternalReferenceVoltage / 65535.0 * 4.0);
enable_val &= ~(1 << 24);
Marshal.WriteInt32(enable_reg, enable_val);
return voltage;
}
}
public float InternalReferenceVoltage
{
get
{
int enable_val = Marshal.ReadInt32(enable_reg);
enable_val |= (1 << 22);
Marshal.WriteInt32(enable_reg, enable_val);
float voltage = (float)(internalReferenceVoltage.ReadValue() * 3.3 / 65535.0);
enable_val &= ~(1 << 22);
Marshal.WriteInt32(enable_reg, enable_val);
return voltage;
}
}
}
static void Main()
{
VBAT vBat = new VBAT();
do
{
Debug.WriteLine($"InternalRefeference = {vBat.InternalReferenceVoltage:f1} V, VBAT = {vBat.Voltage:f1} V");
Thread.Sleep(1000);
} while (true);
}