|
| 1 | +// Package axp192 provides a driver for the axp192 I2C Enhanced single Cell |
| 2 | +// Li-Battery and Power System Management IC. |
| 3 | +// |
| 4 | +// http://www.x-powers.com/en.php/Info/product_detail/article_id/29 |
| 5 | +// Datasheet: https://github.com/m5stack/M5-Schematic/blob/master/Core/AXP192%20Datasheet_v1.1_en_draft_2211.pdf |
| 6 | +// |
| 7 | +package axp192 // import "tinygo.org/x/drivers/axp192" |
| 8 | + |
| 9 | +import ( |
| 10 | + "tinygo.org/x/drivers" |
| 11 | +) |
| 12 | + |
| 13 | +type Error uint8 |
| 14 | + |
| 15 | +const ( |
| 16 | + ErrInvalidID Error = 0x1 |
| 17 | +) |
| 18 | + |
| 19 | +func (e Error) Error() string { |
| 20 | + switch e { |
| 21 | + case ErrInvalidID: |
| 22 | + return "Invalid chip ID" |
| 23 | + default: |
| 24 | + return "Unknown error" |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +type Device struct { |
| 29 | + bus drivers.I2C |
| 30 | + buf []byte |
| 31 | + Address uint8 |
| 32 | +} |
| 33 | + |
| 34 | +// New returns AXP192 device for the provided I2C bus using default address. |
| 35 | +func New(i2c drivers.I2C) *Device { |
| 36 | + return &Device{ |
| 37 | + bus: i2c, |
| 38 | + buf: make([]byte, 2), |
| 39 | + Address: Address, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +type Config struct { |
| 44 | +} |
| 45 | + |
| 46 | +// Configure the AXP192 device. |
| 47 | +func (d *Device) Configure(config Config) error { |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +// ReadPowerSupplyStatus reads power supply status. |
| 52 | +func (d *Device) ReadPowerSupplyStatus() uint8 { |
| 53 | + return d.read8bit(RegPowerSupplyStatus) |
| 54 | +} |
| 55 | + |
| 56 | +// SetVbusIPSOutAccessManagement sets VBUS-IPSOUT access management. |
| 57 | +func (d *Device) SetVbusIPSOutAccessManagement(a uint8) { |
| 58 | + d.write1Byte(RegVbusIPSOutAccessManagement, a) |
| 59 | +} |
| 60 | + |
| 61 | +// GetVbusIPSOutAccessManagement gets VBUS-IPSOUT access management. |
| 62 | +func (d *Device) GetVbusIPSOutAccessManagement() uint8 { |
| 63 | + return d.read8bit(RegVbusIPSOutAccessManagement) |
| 64 | +} |
| 65 | + |
| 66 | +// SetGPIO1Control sets GPIO1 function. |
| 67 | +func (d *Device) SetGPIO1Control(a uint8) { |
| 68 | + d.write1Byte(RegGPIO1Control, a) |
| 69 | +} |
| 70 | + |
| 71 | +// GetGPIO1Control gets GPIO1 function. |
| 72 | +func (d *Device) GetGPIO1Control() uint8 { |
| 73 | + return d.read8bit(RegGPIO1Control) |
| 74 | +} |
| 75 | + |
| 76 | +// SetGPIO2Control sets GPIO2 function. |
| 77 | +func (d *Device) SetGPIO2Control(a uint8) { |
| 78 | + d.write1Byte(RegGPIO2Control, a) |
| 79 | +} |
| 80 | + |
| 81 | +// GetGPIO2Control gets GPIO2 function. |
| 82 | +func (d *Device) GetGPIO2Control() uint8 { |
| 83 | + return d.read8bit(RegGPIO2Control) |
| 84 | +} |
| 85 | + |
| 86 | +// SetGPIO20SignalStatus sets GPIO[2:0] signal status. |
| 87 | +func (d *Device) SetGPIO20SignalStatus(a uint8) { |
| 88 | + d.write1Byte(RegGPIO20SignalStatus, a) |
| 89 | +} |
| 90 | + |
| 91 | +// GetGPIO20SignalStatus gets GPIO[2:0] signal status. |
| 92 | +func (d *Device) GetGPIO20SignalStatus() uint8 { |
| 93 | + return d.read8bit(RegGPIO20SignalStatus) |
| 94 | +} |
| 95 | + |
| 96 | +// SetBackupBatteryChargingControl sets backup battery charge control. |
| 97 | +func (d *Device) SetBackupBatteryChargingControl(a uint8) { |
| 98 | + d.write1Byte(RegBackupBatteryChargingControl, a) |
| 99 | +} |
| 100 | + |
| 101 | +// GetBackupBatteryChargingControl gets backup battery charge control. |
| 102 | +func (d *Device) GetBackupBatteryChargingControl() uint8 { |
| 103 | + return d.read8bit(RegBackupBatteryChargingControl) |
| 104 | +} |
| 105 | + |
| 106 | +// SetDCDC1VoltageSet sets DC-DC1 output voltage. |
| 107 | +func (d *Device) SetDCDC1VoltageSet(a uint8) { |
| 108 | + d.write1Byte(RegDCDC1VoltageSet, a) |
| 109 | +} |
| 110 | + |
| 111 | +// GetDCDC1VoltageSet gets DC-DC1 output voltage. |
| 112 | +func (d *Device) GetDCDC1VoltageSet() uint8 { |
| 113 | + return d.read8bit(RegDCDC1VoltageSet) |
| 114 | +} |
| 115 | + |
| 116 | +// SetDCDC2VoltageSet sets DC-DC2 dynamic voltage parameter. |
| 117 | +func (d *Device) SetDCDC2VoltageSet(a uint8) { |
| 118 | + d.write1Byte(RegDCDC2VoltageSet, a) |
| 119 | +} |
| 120 | + |
| 121 | +// GetDCDC2VoltageSet gets DC-DC2 dynamic voltage parameter. |
| 122 | +func (d *Device) GetDCDC2VoltageSet() uint8 { |
| 123 | + return d.read8bit(RegDCDC2VoltageSet) |
| 124 | +} |
| 125 | + |
| 126 | +// SetDCDC3VoltageSet sets DC-DC3 output voltage. |
| 127 | +func (d *Device) SetDCDC3VoltageSet(a uint8) { |
| 128 | + d.write1Byte(RegDCDC3VoltageSet, a) |
| 129 | +} |
| 130 | + |
| 131 | +// GetDCDC3VoltageSet gets DC-DC3 output voltage. |
| 132 | +func (d *Device) GetDCDC3VoltageSet() uint8 { |
| 133 | + return d.read8bit(RegDCDC3VoltageSet) |
| 134 | +} |
| 135 | + |
| 136 | +// SetLDO23VoltageSet sets LDO2/3 output voltage. |
| 137 | +func (d *Device) SetLDO23VoltageSet(a uint8) { |
| 138 | + d.write1Byte(RegLDO23VoltageSet, a) |
| 139 | +} |
| 140 | + |
| 141 | +// GetLDO23VoltageSet gets LDO2/3 output voltage. |
| 142 | +func (d *Device) GetLDO23VoltageSet() uint8 { |
| 143 | + return d.read8bit(RegLDO23VoltageSet) |
| 144 | +} |
| 145 | + |
| 146 | +// SetDCDC13LDO23Switch sets DC-DC1/3 & LOD2/3 output control. |
| 147 | +func (d *Device) SetDCDC13LDO23Switch(a uint8) { |
| 148 | + d.write1Byte(RegDCDC13LDO23Switch, a) |
| 149 | +} |
| 150 | + |
| 151 | +// GetDCDC13LDO23Switch gets DC-DC1/3 & LOD2/3 output control. |
| 152 | +func (d *Device) GetDCDC13LDO23Switch() uint8 { |
| 153 | + return d.read8bit(RegDCDC13LDO23Switch) |
| 154 | +} |
| 155 | + |
| 156 | +// SetGPIO43FunctionControl sets GPIO[4:3] pin function. |
| 157 | +func (d *Device) SetGPIO43FunctionControl(a uint8) { |
| 158 | + d.write1Byte(RegGPIO43FunctionControl, a) |
| 159 | +} |
| 160 | + |
| 161 | +// GetGPIO43FunctionControl gets GPIO[4:3] pin function. |
| 162 | +func (d *Device) GetGPIO43FunctionControl() uint8 { |
| 163 | + return d.read8bit(RegGPIO43FunctionControl) |
| 164 | +} |
| 165 | + |
| 166 | +// SetPEKParameterSet sets PEK press key parameter. |
| 167 | +func (d *Device) SetPEKParameterSet(a uint8) { |
| 168 | + d.write1Byte(RegPEKParameterSet, a) |
| 169 | +} |
| 170 | + |
| 171 | +// GetPEKParameterSet gets PEK press key parameter. |
| 172 | +func (d *Device) GetPEKParameterSet() uint8 { |
| 173 | + return d.read8bit(RegPEKParameterSet) |
| 174 | +} |
| 175 | + |
| 176 | +// SetADCEnableSet sets ADC enable 1. |
| 177 | +func (d *Device) SetADCEnableSet(a uint8) { |
| 178 | + d.write1Byte(RegADCEnableSet, a) |
| 179 | +} |
| 180 | + |
| 181 | +// GetADCEnableSet gets ADC enable 1. |
| 182 | +func (d *Device) GetADCEnableSet() uint8 { |
| 183 | + return d.read8bit(RegADCEnableSet) |
| 184 | +} |
| 185 | + |
| 186 | +// SetGPIO43SignalStatus sets GPIO[4:3] signal status. |
| 187 | +func (d *Device) SetGPIO43SignalStatus(a uint8) { |
| 188 | + d.write1Byte(RegGPIO43SignalStatus, a) |
| 189 | +} |
| 190 | + |
| 191 | +// GetGPIO43SignalStatus gets GPIO[4:3] signal status. |
| 192 | +func (d *Device) GetGPIO43SignalStatus() uint8 { |
| 193 | + return d.read8bit(RegGPIO43SignalStatus) |
| 194 | +} |
| 195 | + |
| 196 | +// SetDCVoltage sets DC voltage. |
| 197 | +func (d *Device) SetDCVoltage(number uint8, voltage uint16) { |
| 198 | + if voltage < 700 { |
| 199 | + voltage = 0 |
| 200 | + } else { |
| 201 | + voltage = (voltage - 700) / 25 |
| 202 | + } |
| 203 | + |
| 204 | + switch number { |
| 205 | + case 0: |
| 206 | + v := d.GetDCDC1VoltageSet() |
| 207 | + d.SetDCDC1VoltageSet((v & 0x80) | (uint8(voltage) & 0x7F)) |
| 208 | + case 1: |
| 209 | + v := d.GetDCDC2VoltageSet() |
| 210 | + d.SetDCDC2VoltageSet((v & 0x80) | (uint8(voltage) & 0x7F)) |
| 211 | + case 2: |
| 212 | + v := d.GetDCDC3VoltageSet() |
| 213 | + d.SetDCDC3VoltageSet((v & 0x80) | (uint8(voltage) & 0x7F)) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +// SetLDOVoltage sets LDO voltage. |
| 218 | +func (d *Device) SetLDOVoltage(number uint8, voltage uint16) { |
| 219 | + if voltage > 3300 { |
| 220 | + voltage = 15 |
| 221 | + } else { |
| 222 | + voltage = (voltage / 100) - 18 |
| 223 | + } |
| 224 | + |
| 225 | + switch number { |
| 226 | + case 2: |
| 227 | + v := d.GetLDO23VoltageSet() |
| 228 | + d.SetLDO23VoltageSet((v & 0x0F) | (uint8(voltage) << 4)) |
| 229 | + break |
| 230 | + case 3: |
| 231 | + v := d.GetLDO23VoltageSet() |
| 232 | + d.SetLDO23VoltageSet((v & 0xF0) | uint8(voltage)) |
| 233 | + break |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +// SetLDOEnable enable LDO. |
| 238 | +func (d *Device) SetLDOEnable(number uint8, state bool) { |
| 239 | + mark := uint8(0x01) |
| 240 | + mark <<= number |
| 241 | + switch number { |
| 242 | + case 2: |
| 243 | + v := d.GetDCDC13LDO23Switch() |
| 244 | + d.SetDCDC13LDO23Switch(v | mark) |
| 245 | + case 3: |
| 246 | + v := d.GetDCDC13LDO23Switch() |
| 247 | + d.SetDCDC13LDO23Switch(v & (^mark)) |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +func (d *Device) write1Byte(reg, data uint8) { |
| 252 | + d.bus.WriteRegister(d.Address, reg, []byte{data}) |
| 253 | +} |
| 254 | + |
| 255 | +func (d *Device) read8bit(reg uint8) uint8 { |
| 256 | + d.bus.ReadRegister(d.Address, reg, d.buf[:1]) |
| 257 | + return d.buf[0] |
| 258 | +} |
0 commit comments