Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for AXP192 #2104

Merged
merged 3 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,402 changes: 22 additions & 1,380 deletions src/Iot.Device.Bindings/CompatibilitySuppressions.xml

Large diffs are not rendered by default.

332 changes: 289 additions & 43 deletions src/devices/Axp192/Axp192.cs

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions src/devices/Axp192/Axp192.sln
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31613.86
# Visual Studio Version 17
VisualStudioVersion = 17.7.33808.371
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{8F604D83-94DE-4729-84B0-62363CEE5A3D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Axp192", "Axp192.csproj", "{46FCB389-3AEA-44DA-B59A-45DE665DF7DC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Axp192.samples", "samples\Axp192.samples.csproj", "{A3130CD3-7FD8-4C0A-A0DA-1FD9CB493012}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Axp192.samples", "samples\Axp192.samples.csproj", "{A3130CD3-7FD8-4C0A-A0DA-1FD9CB493012}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Arduino", "..\Arduino\Arduino.csproj", "{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -23,6 +25,10 @@ Global
{A3130CD3-7FD8-4C0A-A0DA-1FD9CB493012}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3130CD3-7FD8-4C0A-A0DA-1FD9CB493012}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3130CD3-7FD8-4C0A-A0DA-1FD9CB493012}.Release|Any CPU.Build.0 = Release|Any CPU
{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
11 changes: 8 additions & 3 deletions src/devices/Axp192/BatteryStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ namespace Iot.Device.Axp192
[Flags]
public enum BatteryStatus
{
/// <summary>Overwinered</summary>
Overwinered = 0b1000_000,
/// <summary>
/// Empty value
/// </summary>
None = 0,

/// <summary>AXP192 is overheated</summary>
Overheated = 0b1000_000,

/// <summary>Charging</summary>
Charging = 0b0100_0000,
Expand All @@ -23,7 +28,7 @@ public enum BatteryStatus
/// <summary>Battery Activation Mode</summary>
BatteryActivationMode = 0b0000_1000,

/// <summary>Charging CurrentLess Than Expected</summary>
/// <summary>Charging Current Less Than Expected</summary>
ChargingCurrentLessThanExpected = 0b0000_0100,

/// <summary>Mode B, when not present, the Mode A (sleep) is activated</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/devices/Axp192/Gpio0Behavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Iot.Device.Axp192
public enum Gpio0Behavior
{
/// <summary>NMOS Leak Open Output</summary>
MnosLeakOpenOutput = 0,
NmosLeakOpenOutput = 0,

/// <summary>Universal Input Function</summary>
UniversalInputFunction = 1,
Expand Down
32 changes: 32 additions & 0 deletions src/devices/Axp192/GpioBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Iot.Device.Axp192
{
/// <summary>
/// GPIO1-4 behavior
/// </summary>
public enum GpioBehavior
{
/// <summary>NMOS Leak Open Output</summary>
NmosLeakOpenOutput = 0,

/// <summary>Universal Input Function</summary>
UniversalInputFunction = 1,

/// <summary>PWM output</summary>
PwmOut = 2,

/// <summary>Reserved</summary>
Reserved = 3,

/// <summary>ADC Input</summary>
AdcInput = 4,

/// <summary>Low Output</summary>
LowOutput = 5,

/// <summary>Floating</summary>
Floating = 6,
}
}
2 changes: 1 addition & 1 deletion src/devices/Axp192/LongPressTiming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum LongPressTiming
/// <summary>2 seconds</summary>
S2 = 0b0010_0000,

/// <summary>1.5 seconds</summary>
/// <summary>2.5 seconds</summary>
S2_5 = 0b0011_0000,
}
}
163 changes: 163 additions & 0 deletions src/devices/Axp192/PowerControlData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnitsNet;

namespace Iot.Device.Axp192
{
/// <summary>
/// This class provides a convenient way of retrieving important status
/// information from the Axp192 in a single call
/// </summary>
public class PowerControlData
{
private const double EmptyBatteryVoltage = 3.2;

/// <summary>
/// The internal temperature of the power controller
/// </summary>
public Temperature Temperature { get; init; }

/// <summary>
/// The input current, when powered via bus
/// </summary>
public ElectricCurrent InputCurrent { get; init; }

/// <summary>
/// The input voltage, when powered via bus
/// </summary>
public ElectricPotential InputVoltage { get; init; }

/// <summary>
/// The input status. Use this to determine the current power source
/// </summary>
public PowerStatus InputStatus { get; init; }

/// <summary>
/// The USB input voltage
/// </summary>
public ElectricPotential InputUsbVoltage { get; init; }

/// <summary>
/// The USB input current
/// </summary>
public ElectricCurrent InputUsbCurrent { get; init; }

/// <summary>
/// The charging current of the battery
/// </summary>
public ElectricCurrent BatteryChargingCurrent { get; init; }

/// <summary>
/// The status of the battery
/// </summary>
public BatteryStatus BatteryChargingStatus { get; init; }

/// <summary>
/// The battery discharge current
/// </summary>
public ElectricCurrent BatteryDischargeCurrent { get; init; }

/// <summary>
/// The power currently delivered by the battery
/// </summary>
public Power BatteryInstantaneousPower { get; init; }

/// <summary>
/// The current battery voltage
/// </summary>
public ElectricPotential BatteryVoltage { get; init; }

/// <summary>
/// Indicates whether a battery is present
/// </summary>
public bool BatteryPresent { get; init; }

/// <summary>
/// Returns the charge level of the battery.
/// Only valid if a battery is present.
/// </summary>
public Ratio BatteryLevel
{
get
{
ElectricPotential value = BatteryVoltage - ElectricPotential.FromVolts(EmptyBatteryVoltage);
return Ratio.FromPercent(MathExtensions.Clamp(value.Volts * 100, 0, 100));
}
}

/// <summary>
/// Returns the status of the battery as user-readable english text
/// </summary>
/// <returns>A user-readable string describing the status of the battery</returns>
public string GetBatteryStatusAsText()
{
var flags = BatteryChargingStatus;
List<string> parts = new List<string>();
if (!BatteryPresent)
{
parts.Add("No Battery present");
}
else if ((flags & BatteryStatus.BatteryConnected) == BatteryStatus.None)
{
// Probably corresponds to the bit above, therefore only print either message
parts.Add("No battery connected");
}

if ((flags & BatteryStatus.Overheated) != BatteryStatus.None)
{
parts.Add("Power controller overheated");
}

if ((flags & BatteryStatus.Charging) != BatteryStatus.None)
{
parts.Add("Charging");
}

if ((flags & BatteryStatus.BatteryActivationMode) != BatteryStatus.None)
{
parts.Add("Active");
}
else
{
parts.Add("Inactive");
}

if ((flags & BatteryStatus.ChargingCurrentLessThanExpected) != BatteryStatus.None)
{
parts.Add("Charging current low");
}
else
{
parts.Add("Charging current as expected");
}

// Bit 1 has nothing to do with the battery, but is dependent on the wiring and determines the powering sequence
return string.Join(", ", parts);
}

/// <summary>
/// Returns a printable representation of this structure
/// </summary>
/// <returns>A multiline-string</returns>
public override string ToString()
{
return @$"Chip Temperature: {Temperature}
Input Current: {InputCurrent}
Input Voltage: {InputVoltage}
Input Status: {InputStatus}
Input Usb Voltage: {InputUsbVoltage}
Input Usb Current: {InputUsbCurrent}
Battery Charging Current: {BatteryChargingCurrent}
Battery Charging Status: {GetBatteryStatusAsText()}
Battery Discharge Current: {BatteryDischargeCurrent}
Battery Instantaneous Power: {BatteryInstantaneousPower}
Battery Voltage: {BatteryVoltage} ({BatteryLevel.Percent:F0}%)";
}
}
}
4 changes: 2 additions & 2 deletions src/devices/Axp192/PowerStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public enum PowerStatus
/// <summary>Acin Available</summary>
AcinAvailable = 0b0100_0000,

/// <summary>Vbux Exists</summary>
VbuxExists = 0b0010_0000,
/// <summary>Vbus Exists</summary>
VbusExists = 0b0010_0000,

/// <summary>Vbus Available</summary>
VbusAvailable = 0b0001_0000,
Expand Down
10 changes: 8 additions & 2 deletions src/devices/Axp192/Register.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ internal enum Register
Storage4 = 0x09,
Storage5 = 0x0A,
Storage6 = 0x0B,
SwitchControleDcDC1_3LDO2_3 = 0x12,
SwitchControleDcDc1_3LDO2_3 = 0x12,

VoltageSettingDcDc1 = 0x26,
VoltageSettingDcDC3 = 0x27,
VoltageSettingDcDc2 = 0x23,
VoltageSettingDcDc3 = 0x27,
VoltageSettingLdo2_3 = 0x28,

PathSettingVbus = 0x30,
Expand Down Expand Up @@ -68,6 +69,11 @@ internal enum Register

ControlGpio0 = 0x90,
VoltageOutputSettingGpio0Ldo = 0x91,
ControlGpio1 = 0x92,
ControlGpio2 = 0x93,
ReadWriteGpio012 = 0x94,
ControlGpio34 = 0x95,
ReadWriteGpio34 = 0x96,

CoulombCounterChargingData1 = 0xB0,
CoulombCounterChargingData2 = 0xB1,
Expand Down
6 changes: 3 additions & 3 deletions src/devices/Axp192/samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
// This allows to understand the selection dome.
// Set LDO2 & LDO3(TFT_LED & TFT) 3.0V
// I2cWrite(Register.VoltageSettingLdo2_3, 0xcc);
power.SetLDO2Output(0xC);
power.SetLDO3Output(0xC);
power.SetLdoOutput(2, ElectricPotential.FromMillivolts(3000));
power.SetLdoOutput(3, ElectricPotential.FromMillivolts(3000));
// Set ADC sample rate to 200hz
// I2cWrite(Register.AdcFrequency, 0xF2);
power.AdcFrequency = AdcFrequency.Frequency200Hz;
Expand All @@ -45,7 +45,7 @@
power.PinOutputVoltage = PinOutputVoltage.V3_3;
// Set GPIO0 to LDO
// I2cWrite(Register.ControlGpio0, 0x02);
power.SetGPIO0(Gpio0Behavior.LowNoiseLDO, 0);
power.SetGPIO0(Gpio0Behavior.LowNoiseLDO);
// Disable vbus hold limit
// I2cWrite(Register.PathSettingVbus, 0x80);
power.SetVbusSettings(true, false, VholdVoltage.V4_0, false, VbusCurrentLimit.MilliAmper500);
Expand Down
16 changes: 16 additions & 0 deletions src/devices/M5Stack/M5Stack.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(DefaultBindingTfms)</TargetFrameworks>
<EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>
<ItemGroup>
<Compile Include="*.cs" />
</ItemGroup>
<ItemGroup>
<None Include="README.md" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Arduino\Arduino.csproj" />
<ProjectReference Include="..\Axp192\Axp192.csproj" />
</ItemGroup>
</Project>
39 changes: 39 additions & 0 deletions src/devices/M5Stack/M5Stack.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.33808.371
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{8F604D83-94DE-4729-84B0-62363CEE5A3D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "M5Stack", "M5Stack.csproj", "{46FCB389-3AEA-44DA-B59A-45DE665DF7DC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Arduino", "..\Arduino\Arduino.csproj", "{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Axp192", "..\Axp192\Axp192.csproj", "{4C4E652C-F589-4040-B535-35F7AB2F6365}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{46FCB389-3AEA-44DA-B59A-45DE665DF7DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{46FCB389-3AEA-44DA-B59A-45DE665DF7DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46FCB389-3AEA-44DA-B59A-45DE665DF7DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46FCB389-3AEA-44DA-B59A-45DE665DF7DC}.Release|Any CPU.Build.0 = Release|Any CPU
{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1A1F396-39D1-4A8F-AC59-33FC1DD7321A}.Release|Any CPU.Build.0 = Release|Any CPU
{4C4E652C-F589-4040-B535-35F7AB2F6365}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C4E652C-F589-4040-B535-35F7AB2F6365}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C4E652C-F589-4040-B535-35F7AB2F6365}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C4E652C-F589-4040-B535-35F7AB2F6365}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1BD14274-4E83-4F3E-B0B1-B7B0FF2D670F}
EndGlobalSection
EndGlobal
Loading