tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/axp192/m5stack-core2-axp192/axp192.go (about) 1 package axp192 2 3 import ( 4 "time" 5 6 "tinygo.org/x/drivers" 7 axp192orig "tinygo.org/x/drivers/axp192" 8 ) 9 10 // Device wraps an I2C connection to a AXP192 device. 11 type Device struct { 12 *axp192orig.Device 13 LED Pin 14 RST Pin 15 SPK_EN Pin 16 } 17 18 // New creates a new AXP192 connection. The I2C bus must already be 19 // configured. 20 // 21 // This function only creates the Device object, it does not touch the device. 22 func New(i2c drivers.I2C) *Device { 23 d := axp192orig.New(i2c) 24 25 axp := &Device{ 26 Device: d, 27 } 28 axp.LED = Pin{pin: 1, axp: axp} 29 axp.SPK_EN = Pin{pin: 2, axp: axp} 30 axp.RST = Pin{pin: 4, axp: axp} 31 32 axp.begin() 33 34 return axp 35 } 36 37 type Config struct { 38 } 39 40 // Configure sets up the device for communication 41 func (d *Device) Configure(config Config) error { 42 return d.Device.Configure(axp192orig.Config{}) 43 } 44 45 func (d *Device) begin() { 46 d.SetVbusIPSOutAccessManagement((d.GetVbusIPSOutAccessManagement() & 0x04) | 0x02) 47 d.SetGPIO1Control(d.GetGPIO1Control() & 0xF8) 48 d.SetGPIO2Control(d.GetGPIO2Control() & 0xF8) 49 d.SetBackupBatteryChargingControl((d.GetBackupBatteryChargingControl() & 0x1C) | 0xA2) 50 d.SetESPVoltage(3350) 51 d.SetLcdVoltage(3300) 52 d.SetLDOVoltage(2, 3300) //Periph power voltage preset (LCD_logic, SD card) 53 d.SetLDOVoltage(3, 2000) //Vibrator power voltage preset 54 55 d.SetLDOEnable(2, true) 56 d.SetDCDC3(true) // LCD Backlight 57 // GPIO4 : LCD Reset 58 d.SetGPIO43FunctionControl((d.GetGPIO43FunctionControl() & 0x72) | 0x84) 59 // Power On/Off Setting 60 d.SetPEKParameterSet(0x4C) 61 d.SetADCEnableSet(0xFF) 62 63 d.RST.Low() 64 time.Sleep(100 * time.Millisecond) 65 d.RST.High() 66 time.Sleep(100 * time.Millisecond) 67 } 68 69 // ToggleLED toggles LED connected to AXP192. 70 func (d *Device) ToggleLED() { 71 v := d.GetGPIO20SignalStatus() 72 if (v & 0x02) > 0 { 73 d.SetGPIO20SignalStatus(v & 0xFD) 74 } else { 75 d.SetGPIO20SignalStatus(v | 0x02) 76 } 77 } 78 79 // SetESPVoltage sets voltage of ESP32. 80 func (d *Device) SetESPVoltage(voltage uint16) { 81 if voltage >= 3000 && voltage <= 3400 { 82 d.SetDCVoltage(0, voltage) 83 } 84 } 85 86 // SetLcdVoltage sets voltage of LCD. 87 func (d *Device) SetLcdVoltage(voltage uint16) { 88 if voltage >= 2500 && voltage <= 3300 { 89 d.SetDCVoltage(2, voltage) 90 } 91 } 92 93 // SetDCDC3 enables or disables DCDC3. 94 func (d *Device) SetDCDC3(State bool) { 95 v := d.GetDCDC13LDO23Switch() 96 if State == true { 97 v = (1 << 1) | v 98 } else { 99 v = ^(uint8(1) << 1) & v 100 } 101 d.SetDCDC13LDO23Switch(v) 102 } 103 104 // Pin is a single pin on AXP192. 105 type Pin struct { 106 pin uint8 107 axp *Device 108 } 109 110 // High sets this GPIO pin to high. 111 func (p Pin) High() { 112 switch p.pin { 113 case 1: // LED 114 v := p.axp.GetGPIO20SignalStatus() 115 p.axp.SetGPIO20SignalStatus(v | 0x02) 116 case 2: // SPK_EN 117 case 4: // RST 118 v := p.axp.GetGPIO43SignalStatus() 119 v |= uint8(0x02) 120 p.axp.SetGPIO43SignalStatus(v) 121 } 122 } 123 124 // Low sets this GPIO pin to low. 125 func (p Pin) Low() { 126 switch p.pin { 127 case 1: // LED 128 v := p.axp.GetGPIO20SignalStatus() 129 p.axp.SetGPIO20SignalStatus(v & 0xFD) 130 case 2: // SPK_EN 131 case 4: // RST 132 v := p.axp.GetGPIO43SignalStatus() 133 v &= ^uint8(0x02) 134 p.axp.SetGPIO43SignalStatus(v) 135 } 136 } 137 138 // Toggle switches an output pin from low to high or from high to low. 139 func (p Pin) Toggle() { 140 switch p.pin { 141 case 1: // LED 142 v := p.axp.GetGPIO20SignalStatus() 143 if (v & 0x02) == 0 { 144 p.axp.SetGPIO20SignalStatus(v | 0x02) 145 } else { 146 p.axp.SetGPIO20SignalStatus(v & 0xFD) 147 } 148 case 2: // SPK_EN 149 case 4: // RST 150 v := p.axp.GetGPIO43SignalStatus() 151 if (v & 0x02) == 0 { 152 v |= uint8(0x02) 153 } else { 154 v &= ^uint8(0x02) 155 } 156 p.axp.SetGPIO43SignalStatus(v) 157 } 158 }