Initial commit of Arduino libraries

This commit is contained in:
Sam
2025-05-23 10:47:41 +10:00
commit 5bfce5fc3e
2476 changed files with 1108481 additions and 0 deletions

View File

@@ -0,0 +1,312 @@
'''
@license MIT License
Copyright (c) 2022 lewis he
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@file AXP192_AllFunction.py
@author Lewis He (lewishe@outlook.com)
@date 2022-10-20
'''
from AXP192 import *
import time
SDA = None
SCL = None
IRQ = None
I2CBUS = None
if implementation.name == 'micropython':
from machine import Pin, I2C
SDA = 21
SCL = 22
IRQ = 35
I2CBUS = I2C(0, scl=Pin(SCL), sda=Pin(SDA))
if implementation.name == 'circuitpython':
import digitalio
from board import *
import busio
SDA = IO15
SCL = IO7
IRQ = IO6
I2CBUS = busio.I2C(SCL, SDA)
pmu_flag = False
irq = None
def __callback(args):
global pmu_flag
pmu_flag = True
# print('callback')
PMU = AXP192(I2CBUS, addr=AXP192_SLAVE_ADDRESS)
print('getID:%s' % hex(PMU.getChipID()))
# Set the minimum system operating voltage inside the PMU,
# below this value will shut down the PMU
# Range: 2600~3300mV
PMU.setSysPowerDownVoltage(2600)
# Set the minimum common working voltage of the PMU VBUS input,
# below this value will turn off the PMU
PMU.setVbusVoltageLimit(PMU.XPOWERS_AXP192_VBUS_VOL_LIM_4V5)
# Turn off USB input current limit
PMU.setVbusCurrentLimit(PMU.XPOWERS_AXP192_VBUS_CUR_LIM_OFF)
# DC1 700~3500mV, IMAX=1.2A
PMU.setDC1Voltage(3300)
# print('DC1 : %s Voltage:%u mV ' % PMU.isEnableDC1() ? '+': '-', PMU.getDC1Voltage())
print('DC1 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC1()], PMU.getDC1Voltage()))
# DC2 700~2750 mV, IMAX=1.6A
PMU.setDC2Voltage(2750)
print(PMU.isEnableDC2())
print('DC2 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC2()], PMU.getDC2Voltage()))
# DC3 700~3500mV,IMAX=0.7A
PMU.setDC3Voltage(3300)
print('DC3 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC3()], PMU.getDC3Voltage()))
# LDO2 1800~3300 mV, 100mV/step, IMAX=200mA
PMU.setLDO2Voltage(1800)
# LDO3 1800~3300 mV, 100mV/step, IMAX=200mA
PMU.setLDO3Voltage(1800)
# LDOio 1800~3300 mV, 100mV/step, IMAX=50mA
PMU.setLDOioVoltage(3300)
# Enable power output channel
PMU.enableDC1()
PMU.enableDC2()
PMU.enableDC3()
PMU.enableLDO2()
PMU.enableLDO3()
PMU.enableLDOio()
print('===================================')
print('DC1 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC1()], PMU.getDC1Voltage()))
print('DC2 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC2()], PMU.getDC2Voltage()))
print('DC3 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC3()], PMU.getDC3Voltage()))
print('===================================')
print('LDO2 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableLDO2()], PMU.getLDO2Voltage()))
print('LDO3 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableLDO3()], PMU.getLDO3Voltage()))
print('LDOio : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableLDOio()], PMU.getLDOioVoltage()))
print('===================================')
# Set the time of pressing the button to turn off
powerOff = ['4', '6', '8', '10']
PMU.setPowerKeyPressOffTime(PMU.XPOWERS_POWEROFF_6S)
opt = PMU.getPowerKeyPressOffTime()
print('PowerKeyPressOffTime: %s Sceond' % powerOff[opt])
# Set the button power-on press time
powerOn = ['128ms', '512ms', '1000ms', '2000ms']
PMU.setPowerKeyPressOnTime(PMU.XPOWERS_POWERON_2S)
opt = PMU.getPowerKeyPressOnTime()
print('PowerKeyPressOnTime: %s ' % powerOn[opt])
print('===================================')
# It is necessary to disable the detection function of the TS pin on the board
# without the battery temperature detection function, otherwise it will cause abnormal charging
PMU.disableTSPinMeasure()
PMU.enableTemperatureMeasure()
# PMU.disableTemperatureMeasure()
# Enable internal ADC detection
PMU.enableBattDetection()
PMU.enableVbusVoltageMeasure()
PMU.enableBattVoltageMeasure()
PMU.enableSystemVoltageMeasure()
'''
The default setting is CHGLED is automatically controlled by the PMU.
- XPOWERS_CHG_LED_OFF,
- XPOWERS_CHG_LED_BLINK_1HZ,
- XPOWERS_CHG_LED_BLINK_4HZ,
- XPOWERS_CHG_LED_ON,
- XPOWERS_CHG_LED_CTRL_CHG,
'''
PMU.setChargingLedMode(PMU.XPOWERS_CHG_LED_OFF)
# Disable all interrupts
PMU.disableIRQ(PMU.XPOWERS_AXP192_ALL_IRQ)
# Clear all interrupt flags
PMU.clearIrqStatus()
# Enable the required interrupt function
PMU.enableIRQ(
PMU.XPOWERS_AXP192_BAT_INSERT_IRQ | PMU.XPOWERS_AXP192_BAT_REMOVE_IRQ | # BATTERY
PMU.XPOWERS_AXP192_VBUS_INSERT_IRQ | PMU.XPOWERS_AXP192_VBUS_REMOVE_IRQ | # VBUS
PMU.XPOWERS_AXP192_PKEY_SHORT_IRQ | PMU.XPOWERS_AXP192_PKEY_LONG_IRQ | # POWER KEY
PMU.XPOWERS_AXP192_BAT_CHG_DONE_IRQ | PMU.XPOWERS_AXP192_BAT_CHG_START_IRQ | # CHARGE
# PMU.XPOWERS_AXP192_PKEY_NEGATIVE_IRQ | PMU.XPOWERS_AXP192_PKEY_POSITIVE_IRQ | # POWER KEY
PMU.XPOWERS_AXP192_TIMER_TIMEOUT_IRQ # Timer
)
# Set constant current charge current limit
PMU.setChargerConstantCurr(PMU.XPOWERS_AXP192_CHG_CUR_280MA)
# Set stop charging termination current
PMU.setChargerTerminationCurr(PMU.XPOWERS_AXP192_CHG_ITERM_LESS_10_PERCENT)
# Set charge cut-off voltage
PMU.setChargeTargetVoltage(PMU.XPOWERS_AXP192_CHG_VOL_4V2)
PMU.clearIrqStatus()
# Set the timing after one minute, the isWdtExpireIrq will be triggered in the loop interrupt function
PMU.setTimerout(1)
data = [1, 2, 3, 4]
print('Write buffer to pmu')
PMU.writeDataBuffer(data, 4)
print('Read buffer from pmu')
tmp = PMU.readDataBuffer(4)
print(tmp)
if implementation.name == 'micropython':
irq = Pin(IRQ, Pin.IN, Pin.PULL_UP)
irq.irq(trigger=Pin.IRQ_FALLING, handler=__callback)
if implementation.name == 'circuitpython':
irq = digitalio.DigitalInOut(IRQ)
irq.switch_to_input()
while True:
if implementation.name == 'circuitpython':
if irq.value == False:
pmu_flag = True
if pmu_flag:
pmu_flag = False
mask = PMU.getIrqStatus()
print('pmu_flag:', end='')
print(bin(mask))
if PMU.isAcinOverVoltageIrq():
print("IRQ ---> isAcinOverVoltageIrq")
if PMU.isAcinInserIrq():
print("IRQ ---> isAcinInserIrq")
if PMU.isAcinRemoveIrq():
print("IRQ ---> isAcinRemoveIrq")
if PMU.isVbusOverVoltageIrq():
print("IRQ ---> isVbusOverVoltageIrq")
if PMU.isVbusInsertIrq():
print("IRQ ---> isVbusInsertIrq")
if PMU.isVbusRemoveIrq():
print("IRQ ---> isVbusRemoveIrq")
if PMU.isVbusLowVholdIrq():
print("IRQ ---> isVbusLowVholdIrq")
if PMU.isBatInsertIrq():
print("IRQ ---> isBatInsertIrq")
if PMU.isBatRemoveIrq():
print("IRQ ---> isBatRemoveIrq")
if PMU.isBattEnterActivateIrq():
print("IRQ ---> isBattEnterActivateIrq")
if PMU.isBattExitActivateIrq():
print("IRQ ---> isBattExitActivateIrq")
if PMU.isBatChargeStartIrq():
print("IRQ ---> isBatChargeStartIrq")
if PMU.isBatChargeDoneIrq():
print("IRQ ---> isBatChargeDoneIrq")
if PMU.isBattTempHighIrq():
print("IRQ ---> isBattTempHighIrq")
if PMU.isBattTempLowIrq():
print("IRQ ---> isBattTempLowIrq")
if PMU.isChipOverTemperatureIrq():
print("IRQ ---> isChipOverTemperatureIrq")
if PMU.isChargingCurrentLessIrq():
print("IRQ ---> isChargingCurrentLessIrq")
if PMU.isDC1VoltageLessIrq():
print("IRQ ---> isDC1VoltageLessIrq")
if PMU.isDC2VoltageLessIrq():
print("IRQ ---> isDC2VoltageLessIrq")
if PMU.isDC3VoltageLessIrq():
print("IRQ ---> isDC3VoltageLessIrq")
if PMU.isPekeyShortPressIrq():
print("IRQ ---> isPekeyShortPress")
if PMU.isPekeyLongPressIrq():
print("IRQ ---> isPekeyLongPress")
if PMU.isNOEPowerOnIrq():
print("IRQ ---> isNOEPowerOnIrq")
if PMU.isNOEPowerDownIrq():
print("IRQ ---> isNOEPowerDownIrq")
if PMU.isVbusEffectiveIrq():
print("IRQ ---> isVbusEffectiveIrq")
if PMU.isVbusInvalidIrq():
print("IRQ ---> isVbusInvalidIrq")
if PMU.isVbusSessionIrq():
print("IRQ ---> isVbusSessionIrq")
if PMU.isVbusSessionEndIrq():
print("IRQ ---> isVbusSessionEndIrq")
if PMU.isLowVoltageLevel2Irq():
print("IRQ ---> isLowVoltageLevel2Irq")
if PMU.isWdtExpireIrq():
print("IRQ ---> isWdtExpire")
# Clear the timer state and continue to the next timer
PMU.clearTimerFlag()
if PMU.isGpio2EdgeTriggerIrq():
print("IRQ ---> isGpio2EdgeTriggerIrq")
if PMU.isGpio1EdgeTriggerIrq():
print("IRQ ---> isGpio1EdgeTriggerIrq")
if PMU.isGpio0EdgeTriggerIrq():
print("IRQ ---> isGpio0EdgeTriggerIrq")
# Clear PMU Interrupt Status Register
PMU.clearIrqStatus()
PMU.setChargingLedMode((PMU.XPOWERS_CHG_LED_OFF, PMU.XPOWERS_CHG_LED_ON)[
PMU.getChargingLedMode() == PMU.XPOWERS_CHG_LED_OFF])
print("getBattVoltage:{0}mV".format(PMU.getBattVoltage()))
print("getSystemVoltage:{0}mV".format(PMU.getSystemVoltage()))
print("getBatteryPercent:{0}%".format(PMU.getBatteryPercent()))
print("getTemperature:{0}%".format(PMU.getTemperature()))
print("isCharging:{0}".format(PMU.isCharging()))
print("isDischarge:{0}".format(PMU.isDischarge()))
print("isVbusIn:{0}".format(PMU.isVbusIn()))
time.sleep(0.8)

View File

@@ -0,0 +1,64 @@
'''
@license MIT License
Copyright (c) 2022 lewis he
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@file AXP192_AnalogRead.py
@author Lewis He (lewishe@outlook.com)
@date 2022-10-20
'''
from AXP192 import *
import time
SDA = None
SCL = None
I2CBUS = None
if implementation.name == 'micropython':
from machine import Pin, I2C
SDA = 21
SCL = 22
IRQ = 35
I2CBUS = I2C(scl=Pin(SCL), sda=Pin(SDA))
if implementation.name == 'circuitpython':
from board import *
import busio
SDA = IO15
SCL = IO7
IRQ = IO6
I2CBUS = busio.I2C(SCL, SDA)
PMU = AXP192(I2CBUS, addr=AXP192_SLAVE_ADDRESS)
print('getID:%s' % hex(PMU.getChipID()))
val = 0
while True:
IO0 = PMU.analogRead(PMU.PMU_GPIO0)
IO1 = PMU.analogRead(PMU.PMU_GPIO1)
# IO2 = PMU.analogRead(PMU_GPIO2) # not support
# IO3 = PMU.analogRead(PMU_GPIO3) # not support
# IO4 = PMU.analogRead(PMU_GPIO4) # not support
# IO5 = PMU.analogRead(PMU_GPIO5) # not support
print('IO0:{0} IO1:{1} '.format(IO0, IO1))
time.sleep(1.5)

View File

@@ -0,0 +1,74 @@
'''
@license MIT License
Copyright (c) 2022 lewis he
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@file AXP192_GpioInput.py
@author Lewis He (lewishe@outlook.com)
@date 2022-10-20
'''
from AXP192 import *
import time
SDA = None
SCL = None
IRQ = None
I2CBUS = None
if implementation.name == 'micropython':
from machine import Pin, I2C
SDA = 21
SCL = 22
IRQ = 35
I2CBUS = I2C(scl=Pin(SCL), sda=Pin(SDA))
if implementation.name == 'circuitpython':
from board import *
import busio
SDA = IO15
SCL = IO7
IRQ = IO6
I2CBUS = busio.I2C(SCL, SDA)
PMU = AXP192(I2CBUS, addr=AXP192_SLAVE_ADDRESS)
print('getID:%s' % hex(PMU.getChipID()))
PMU.pinMode(PMU.PMU_GPIO0, PMU.INPUT_PULLDOWN)
PMU.pinMode(PMU.PMU_GPIO1, PMU.INPUT_PULLDOWN)
PMU.pinMode(PMU.PMU_GPIO2, PMU.INPUT_PULLDOWN)
PMU.pinMode(PMU.PMU_GPIO3, PMU.INPUT)
PMU.pinMode(PMU.PMU_GPIO4, PMU.INPUT)
PMU.pinMode(PMU.PMU_GPIO5, PMU.INPUT)
print('AXP192 GPIO 3 ~ 5 is a floating input, please ensure there is an external pull-up or pull-down resistor')
while True:
IO0 = PMU.digitalRead(PMU.PMU_GPIO0)
IO1 = PMU.digitalRead(PMU.PMU_GPIO1)
IO2 = PMU.digitalRead(PMU.PMU_GPIO2)
IO3 = PMU.digitalRead(PMU.PMU_GPIO3)
IO4 = PMU.digitalRead(PMU.PMU_GPIO4)
IO5 = PMU.digitalRead(PMU.PMU_GPIO5)
print('IO0:{0} IO1:{1} IO2:{2} IO3:{3} IO4:{4} IO5:{5} '.format(
IO0, IO1, IO2, IO3, IO4, IO5))
time.sleep(1.5)

View File

@@ -0,0 +1,369 @@
'''
@license MIT License
Copyright (c) 2022 lewis he
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@file AXP2101_AllFunction.py
@author Lewis He (lewishe@outlook.com)
@date 2022-10-20
'''
from AXP2101 import *
import time
SDA = None
SCL = None
IRQ = None
I2CBUS = None
if implementation.name == 'micropython':
from machine import Pin, I2C
SDA = 21
SCL = 22
IRQ = 35
I2CBUS = I2C(scl=Pin(SCL), sda=Pin(SDA))
if implementation.name == 'circuitpython':
import digitalio
import board
import busio
SDA = board.IO42
SCL = board.IO41
IRQ = board.IO6
I2CBUS = busio.I2C(SCL, SDA)
pmu_flag = False
irq = None
def __callback(args):
global pmu_flag
pmu_flag = True
# print('callback')
PMU = AXP2101(I2CBUS, addr=AXP2101_SLAVE_ADDRESS)
id = PMU.getChipID()
if id != XPOWERS_AXP2101_CHIP_ID:
print("PMU is not online...")
while True:
pass
print('getID:%s' % hex(PMU.getChipID()))
# Set the minimum common working voltage of the PMU VBUS input,
# below this value will turn off the PMU
PMU.setVbusVoltageLimit(PMU.XPOWERS_AXP2101_VBUS_VOL_LIM_4V36)
# Set the maximum current of the PMU VBUS input,
# higher than this value will turn off the PMU
PMU.setVbusCurrentLimit(PMU.XPOWERS_AXP2101_VBUS_CUR_LIM_1500MA)
# Get the VSYS shutdown voltage
vol = PMU.getSysPowerDownVoltage()
print('-> getSysPowerDownVoltage:%u' % vol)
# Set VSY off voltage as 2600mV, Adjustment range 2600mV ~ 3300mV
PMU.setSysPowerDownVoltage(2600)
vol = PMU.getSysPowerDownVoltage()
print('-> getSysPowerDownVoltage:%u' % vol)
# DC1 IMAX = 2A
# 1500~3400mV, 100mV/step, 20steps
PMU.setDC1Voltage(3300)
# print('DC1 : %s Voltage:%u mV ' % PMU.isEnableDC1() ? '+': '-', PMU.getDC1Voltage())
print('DC1 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC1()], PMU.getDC1Voltage()))
# DC2 IMAX = 2A
# 500~1200mV 10mV/step, 71steps
# 1220~1540mV 20mV/step, 17steps
PMU.setDC2Voltage(1000)
print(PMU.isEnableDC2())
print('DC2 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC2()], PMU.getDC2Voltage()))
# DC3 IMAX = 2A
# 500~1200mV, 10mV/step, 71steps
# 1220~1540mV, 20mV/step, 17steps
# 1600~3400mV, 100mV/step, 19steps
PMU.setDC3Voltage(3300)
print('DC3 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC3()], PMU.getDC3Voltage()))
# DCDC4 IMAX = 1.5A
# 500~1200mV, 10mV/step, 71steps
# 1220~1840mV, 20mV/step, 32steps
PMU.setDC4Voltage(1000)
print('DC4 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC4()], PMU.getDC4Voltage()))
# DC5 IMAX = 2A
# 1200mV
# 1400~3700mV, 100mV/step, 24steps
PMU.setDC5Voltage(3300)
print('DC5 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC5()], PMU.getDC5Voltage()))
# ALDO1 IMAX = 300mA
# 500~3500mV, 100mV/step, 31steps
PMU.setALDO1Voltage(3300)
# ALDO2 IMAX = 300mA
# 500~3500mV, 100mV/step, 31steps
PMU.setALDO2Voltage(3300)
# ALDO3 IMAX = 300mA
# 500~3500mV, 100mV/step, 31steps
PMU.setALDO3Voltage(3300)
# ALDO4 IMAX = 300mA
# 500~3500mV, 100mV/step, 31steps
PMU.setALDO4Voltage(3300)
# BLDO1 IMAX = 300mA
# 500~3500mV, 100mV/step, 31steps
PMU.setBLDO1Voltage(3300)
# BLDO2 IMAX = 300mA
# 500~3500mV, 100mV/step, 31steps
PMU.setBLDO2Voltage(3300)
# CPUSLDO IMAX = 30mA
# 500~1400mV, 50mV/step, 19steps
PMU.setCPUSLDOVoltage(1000)
# DLDO1 IMAX = 300mA
# 500~3400mV, 100mV/step, 29steps
PMU.setDLDO1Voltage(3300)
# DLDO2 IMAX = 300mA
# 500~1400mV, 50mV/step, 2steps
PMU.setDLDO2Voltage(3300)
# PMU.enableDC1()
PMU.enableDC2()
PMU.enableDC3()
PMU.enableDC4()
PMU.enableDC5()
PMU.enableALDO1()
PMU.enableALDO2()
PMU.enableALDO3()
PMU.enableALDO4()
PMU.enableBLDO1()
PMU.enableBLDO2()
PMU.enableCPUSLDO()
PMU.enableDLDO1()
PMU.enableDLDO2()
print('===================================')
print('DC1 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC1()], PMU.getDC1Voltage()))
print('DC2 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC2()], PMU.getDC2Voltage()))
print('DC3 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC3()], PMU.getDC3Voltage()))
print('DC4 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC4()], PMU.getDC4Voltage()))
print('DC5 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDC5()], PMU.getDC5Voltage()))
print('===================================')
print('ALDO1 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableALDO1()], PMU.getALDO1Voltage()))
print('ALDO2 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableALDO2()], PMU.getALDO2Voltage()))
print('ALDO3 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableALDO3()], PMU.getALDO3Voltage()))
print('ALDO4 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableALDO4()], PMU.getALDO4Voltage()))
print('===================================')
print('BLDO1 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableBLDO1()], PMU.getBLDO1Voltage()))
print('BLDO2 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableBLDO2()], PMU.getBLDO2Voltage()))
print('===================================')
print('CPUSLDO: {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableCPUSLDO()], PMU.getCPUSLDOVoltage()))
print('===================================')
print('DLDO1 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDLDO1()], PMU.getDLDO1Voltage()))
print('DLDO2 : {0} Voltage:{1} mV '.format(
('-', '+')[PMU.isEnableDLDO2()], PMU.getDLDO2Voltage()))
print('===================================')
# Set the time of pressing the button to turn off
powerOff = ['4', '6', '8', '10']
PMU.setPowerKeyPressOffTime(PMU.XPOWERS_POWEROFF_6S)
opt = PMU.getPowerKeyPressOffTime()
print('PowerKeyPressOffTime: %s Sceond' % powerOff[opt])
# Set the button power-on press time
powerOn = ['128ms', '512ms', '1000ms', '2000ms']
PMU.setPowerKeyPressOnTime(PMU.XPOWERS_POWERON_2S)
opt = PMU.getPowerKeyPressOnTime()
print('PowerKeyPressOnTime: %s ' % powerOn[opt])
print('===================================')
# DCDC 120 % (130 %) high voltage turn off PMIC function
en = PMU.getDCHighVoltagePowerDownEn()
print('getDCHighVoltagePowerDownEn:%s' % ('DISABLE', 'ENABLE')[en])
# DCDC1 85 % low voltage turn off PMIC function
en = PMU.getDC1LowVoltagePowerDownEn()
print('getDC1LowVoltagePowerDownEn:%s' % ('DISABLE', 'ENABLE')[en])
# DCDC2 85 % low voltage turn off PMIC function
en = PMU.getDC2LowVoltagePowerDownEn()
print('getDC2LowVoltagePowerDownEn:%s' % ('DISABLE', 'ENABLE')[en])
# DCDC3 85 % low voltage turn off PMIC function
en = PMU.getDC3LowVoltagePowerDownEn()
print('getDC3LowVoltagePowerDownEn:%s' % ('DISABLE', 'ENABLE')[en])
# DCDC4 85 % low voltage turn off PMIC function
en = PMU.getDC4LowVoltagePowerDownEn()
print('getDC4LowVoltagePowerDownEn:%s' % ('DISABLE', 'ENABLE')[en])
# DCDC5 85 % low voltage turn off PMIC function
en = PMU.getDC5LowVoltagePowerDownEn()
print('getDC5LowVoltagePowerDownEn:%s' % ('DISABLE', 'ENABLE')[en])
# PMU.setDCHighVoltagePowerDown(true)
# PMU.setDC1LowVoltagePowerDown(true)
# PMU.setDC2LowVoltagePowerDown(true)
# PMU.setDC3LowVoltagePowerDown(true)
# PMU.setDC4LowVoltagePowerDown(true)
# PMU.setDC5LowVoltagePowerDown(true)
# It is necessary to disable the detection function of the TS pin on the board
# without the battery temperature detection function, otherwise it will cause abnormal charging
PMU.disableTSPinMeasure()
# PMU.enableTemperatureMeasure()
# Enable internal ADC detection
PMU.enableBattDetection()
PMU.enableVbusVoltageMeasure()
PMU.enableBattVoltageMeasure()
PMU.enableSystemVoltageMeasure()
'''
The default setting is CHGLED is automatically controlled by the PMU.
- XPOWERS_CHG_LED_OFF,
- XPOWERS_CHG_LED_BLINK_1HZ,
- XPOWERS_CHG_LED_BLINK_4HZ,
- XPOWERS_CHG_LED_ON,
- XPOWERS_CHG_LED_CTRL_CHG,
'''
PMU.setChargingLedMode(PMU.XPOWERS_CHG_LED_OFF)
# Disable all interrupts
PMU.disableIRQ(PMU.XPOWERS_AXP2101_ALL_IRQ)
# Clear all interrupt flags
PMU.clearIrqStatus()
# Enable the required interrupt function
PMU.enableIRQ(
PMU.XPOWERS_AXP2101_BAT_INSERT_IRQ | PMU.XPOWERS_AXP2101_BAT_REMOVE_IRQ | # BATTERY
PMU.XPOWERS_AXP2101_VBUS_INSERT_IRQ | PMU.XPOWERS_AXP2101_VBUS_REMOVE_IRQ | # VBUS
PMU.XPOWERS_AXP2101_PKEY_SHORT_IRQ | PMU.XPOWERS_AXP2101_PKEY_LONG_IRQ | # POWER KEY
PMU.XPOWERS_AXP2101_BAT_CHG_DONE_IRQ | PMU.XPOWERS_AXP2101_BAT_CHG_START_IRQ # CHARGE
# PMU.XPOWERS_AXP2101_PKEY_NEGATIVE_IRQ | PMU.XPOWERS_AXP2101_PKEY_POSITIVE_IRQ | # POWER KEY
)
# Set the precharge charging current
PMU.setPrechargeCurr(PMU.XPOWERS_AXP2101_PRECHARGE_50MA)
# Set constant current charge current limit
PMU.setChargerConstantCurr(PMU.XPOWERS_AXP2101_CHG_CUR_200MA)
# Set stop charging termination current
PMU.setChargerTerminationCurr(PMU.XPOWERS_AXP2101_CHG_ITERM_25MA)
# Set charge cut-off voltage
PMU.setChargeTargetVoltage(PMU.XPOWERS_AXP2101_CHG_VOL_4V1)
# Set the watchdog trigger event type
PMU.setWatchdogConfig(PMU.XPOWERS_AXP2101_WDT_IRQ_TO_PIN)
# Set watchdog timeout
PMU.setWatchdogTimeout(PMU.XPOWERS_AXP2101_WDT_TIMEOUT_4S)
# Enable watchdog to trigger interrupt event
# PMU.enableWatchdog()
PMU.disableWatchdog()
PMU.clearIrqStatus()
data = [1, 2, 3, 4]
print('Write buffer to pmu')
PMU.writeDataBuffer(data, 4)
print('Read buffer from pmu')
tmp = PMU.readDataBuffer(4)
print(tmp)
if implementation.name == 'micropython':
irq = Pin(IRQ, Pin.IN, Pin.PULL_UP)
irq.irq(trigger=Pin.IRQ_FALLING, handler=__callback)
if implementation.name == 'circuitpython':
irq = digitalio.DigitalInOut(IRQ)
irq.switch_to_input()
while True:
if implementation.name == 'circuitpython':
if irq.value == False:
pmu_flag = True
if pmu_flag:
pmu_flag = False
mask = PMU.getIrqStatus()
print('pmu_flag:', end='')
print(bin(mask))
if PMU.isPekeyShortPressIrq():
print("IRQ ---> isPekeyShortPress")
if PMU.isPekeyLongPressIrq():
print("IRQ ---> isPekeyLongPress")
if PMU.isPekeyNegativeIrq():
print("IRQ ---> isPekeyNegative")
if PMU.isPekeyPositiveIrq():
print("IRQ ---> isPekeyPositive")
if PMU.isWdtExpireIrq():
print("IRQ ---> isWdtExpire")
PMU.clearIrqStatus()
PMU.setChargingLedMode((PMU.XPOWERS_CHG_LED_OFF, PMU.XPOWERS_CHG_LED_ON)[
PMU.getChargingLedMode() == PMU.XPOWERS_CHG_LED_OFF])
print("getBattVoltage:{0}mV".format(PMU.getBattVoltage()))
print("getSystemVoltage:{0}mV".format(PMU.getSystemVoltage()))
print("getBatteryPercent:{0}%".format(PMU.getBatteryPercent()))
print("isCharging:{0}".format(PMU.isCharging()))
print("isDischarge:{0}".format(PMU.isDischarge()))
print("isStandby:{0}".format(PMU.isStandby()))
time.sleep(0.8)

View File

@@ -0,0 +1,184 @@
'''
@license MIT License
Copyright (c) 2022 lewis he
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@file AXP2101_Interrupt.py
@author Lewis He (lewishe@outlook.com)
@date 2022-10-20
'''
from AXP2101 import *
import time
SDA = None
SCL = None
I2CBUS = None
if implementation.name == 'micropython':
from machine import Pin, I2C
SDA = 21
SCL = 22
IRQ = 35
I2CBUS = I2C(scl=Pin(SCL), sda=Pin(SDA))
if implementation.name == 'circuitpython':
import digitalio
import board
import busio
SDA = board.IO42
SCL = board.IO41
IRQ = board.IO6
I2CBUS = busio.I2C(SCL, SDA)
irq = None
pmu_flag = False
def __callback(args):
global pmu_flag
pmu_flag = True
PMU = AXP2101(I2CBUS, addr=AXP2101_SLAVE_ADDRESS)
print('getID:%s' % hex(PMU.getChipID()))
PMU.disableIRQ(PMU.XPOWERS_AXP2101_ALL_IRQ)
# Clear all interrupt flags
PMU.clearIrqStatus()
PMU.printIntRegister()
PMU.enableIRQ(PMU.XPOWERS_AXP2101_PKEY_SHORT_IRQ |
PMU.XPOWERS_AXP2101_PKEY_NEGATIVE_IRQ)
# Print AXP2101 interrupt control register
PMU.printIntRegister()
# PMU.enableIRQ(
# PMU.XPOWERS_AXP2101_BAT_INSERT_IRQ | PMU.XPOWERS_AXP2101_BAT_REMOVE_IRQ | # BATTERY
# PMU.XPOWERS_AXP2101_VBUS_INSERT_IRQ | PMU.XPOWERS_AXP2101_VBUS_REMOVE_IRQ | # VBUS
# PMU.XPOWERS_AXP2101_PKEY_SHORT_IRQ | PMU.XPOWERS_AXP2101_PKEY_LONG_IRQ | # POWER KEY
# PMU.XPOWERS_AXP2101_BAT_CHG_DONE_IRQ | PMU.XPOWERS_AXP2101_BAT_CHG_START_IRQ # CHARGE
# )
PMU.enableIRQ(PMU.XPOWERS_AXP2101_BAT_NOR_UNDER_TEMP_IRQ)
# Print AXP2101 interrupt control register
PMU.printIntRegister()
PMU.enableIRQ(PMU.XPOWERS_AXP2101_PKEY_SHORT_IRQ |
PMU.XPOWERS_AXP2101_PKEY_NEGATIVE_IRQ)
# Print AXP2101 interrupt control register
PMU.printIntRegister()
PMU.enableIRQ(PMU.XPOWERS_AXP2101_BAT_OVER_VOL_IRQ)
# Print AXP2101 interrupt control register
PMU.printIntRegister()
if implementation.name == 'micropython':
irq = Pin(IRQ, Pin.IN, Pin.PULL_UP)
irq.irq(trigger=Pin.IRQ_FALLING, handler=__callback)
if implementation.name == 'circuitpython':
irq = digitalio.DigitalInOut(IRQ)
irq.switch_to_input()
while True:
if implementation.name == 'circuitpython':
if irq.value == False:
pmu_flag = True
if pmu_flag:
pmu_flag = False
# Get PMU Interrupt Status Register
status = PMU.getIrqStatus()
print("STATUS => HEX:")
print(hex(status))
if PMU.isDropWarningLevel2Irq():
print("isDropWarningLevel2")
if PMU.isDropWarningLevel1Irq():
print("isDropWarningLevel1")
if PMU.isGaugeWdtTimeoutIrq():
print("isWdtTimeout")
if PMU.isBatChargerOverTemperatureIrq():
print("isBatChargeOverTemperature")
if PMU.isBatWorkOverTemperatureIrq():
print("isBatWorkOverTemperature")
if PMU.isBatWorkUnderTemperatureIrq():
print("isBatWorkUnderTemperature")
if PMU.isVbusInsertIrq():
print("isVbusInsert")
if PMU.isVbusRemoveIrq():
print("isVbusRemove")
if PMU.isBatInsertIrq():
print("isBatInsert")
if PMU.isBatRemoveIrq():
print("isBatRemove")
if PMU.isPekeyShortPressIrq():
print("isPekeyShortPress")
if PMU.isPekeyLongPressIrq():
print("isPekeyLongPress")
if PMU.isPekeyNegativeIrq():
print("isPekeyNegative")
if PMU.isPekeyPositiveIrq():
print("isPekeyPositive")
if PMU.isWdtExpireIrq():
print("isWdtExpire")
if PMU.isLdoOverCurrentIrq():
print("isLdoOverCurrentIrq")
if PMU.isBatfetOverCurrentIrq():
print("isBatfetOverCurrentIrq")
if PMU.isBatChargeDoneIrq():
print("isBatChargeDone")
if PMU.isBatChargeStartIrq():
print("isBatChargeStart")
if PMU.isBatDieOverTemperatureIrq():
print("isBatDieOverTemperature")
if PMU.isChargeOverTimeoutIrq():
print("isChargeOverTimeout")
if PMU.isBatOverVoltageIrq():
print("isBatOverVoltage")
# Clear PMU Interrupt Status Register
PMU.clearIrqStatus()
# Print AXP2101 interrupt control register
PMU.printIntRegister()
time.sleep(0.2)