Skip to content

Commit 51c3aa2

Browse files
sago35deadprogram
authored andcommitted
axp192: add support for AXP192 single Cell Li-Battery and power system management IC
1 parent 520234e commit 51c3aa2

File tree

6 files changed

+573
-2
lines changed

6 files changed

+573
-2
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,14 @@ endif
213213
@md5sum ./build/test.hex
214214
tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/i2csoft/adt7410/
215215
@md5sum ./build/test.hex
216+
tinygo build -size short -o ./build/test.elf -target=wioterminal ./examples/axp192/m5stack-core2-blinky/
217+
@md5sum ./build/test.hex
216218

217219
DRIVERS = $(wildcard */)
218220
NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 microphone mcp3008 gps microbitmatrix \
219221
hcsr04 ssd1331 ws2812 thermistor apa102 easystepper ssd1351 ili9341 wifinina shifter hub75 \
220222
hd44780 buzzer ssd1306 espat l9110x st7735 bmi160 l293x dht keypad4x4 max72xx p1am tone tm1637 \
221-
pcf8563 mcp2515 servo sdcard rtl8720dn image cmd i2csoft hts221 lps22hb apds9960
223+
pcf8563 mcp2515 servo sdcard rtl8720dn image cmd i2csoft hts221 lps22hb apds9960 axp192
222224
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))
223225

224226
unit-test:

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252

5353
## Currently supported devices
5454

55-
The following 72 devices are supported.
55+
The following 73 devices are supported.
5656

5757
| Device Name | Interface Type |
5858
|----------|-------------|
@@ -63,6 +63,7 @@ The following 72 devices are supported.
6363
| [APA102 RGB LED](https://cdn-shop.adafruit.com/product-files/2343/APA102C.pdf) | SPI |
6464
| [APDS9960 Digital proximity, ambient light, RGB and gesture sensor](https://cdn.sparkfun.com/assets/learn_tutorials/3/2/1/Avago-APDS-9960-datasheet.pdf) | I2C |
6565
| [AT24CX 2-wire serial EEPROM](https://www.openimpulse.com/blog/wp-content/uploads/wpsc/downloadables/24C32-Datasheet.pdf) | I2C |
66+
| [AXP192 single Cell Li-Battery and Power System Management](https://github.com/m5stack/M5-Schematic/blob/master/Core/AXP192%20Datasheet_v1.1_en_draft_2211.pdf) | I2C |
6667
| [BBC micro:bit LED matrix](https://github.com/bbcmicrobit/hardware/blob/master/SCH_BBC-Microbit_V1.3B.pdf) | GPIO |
6768
| [BH1750 ambient light sensor](https://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf) | I2C |
6869
| [BlinkM RGB LED](http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf) | I2C |

axp192/axp192.go

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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

Comments
 (0)