Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
function enable_2v_adc_on_gpio0 {
# docs: http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
v=$(i2cget -f -y 0 0x34 0x83 b)
/usr/sbin/i2cset -f -y 0 0x34 0x83 $((v&0xF7)) # disable ADC input on GPIO0
/usr/sbin/i2cset -f -y 0 0x34 0x90 0x04 # set GPIO0 to 12 bit ADC input
u=$(i2cget -f -y 0 0x34 0x85 b)
# /usr/sbin/i2cset -f -y 0 0x34 0x85 $((u|0x01)) # set ADC input range to 0.7-2.7475v
/usr/sbin/i2cset -f -y 0 0x34 0x85 $((u&0xFE)) # set ADC input range to 0-2v
/usr/sbin/i2cset -f -y 0 0x34 0x83 $((v|0x08)) # enable ADC input on GPIO0
}
function enable_2v_adc_on_gpio1 {
# docs: http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
v=$(i2cget -f -y 0 0x34 0x83 b)
/usr/sbin/i2cset -f -y 0 0x34 0x83 $((v&0xFB)) # disable ADC input on GPIO1
/usr/sbin/i2cset -f -y 0 0x34 0x92 0x04 # set GPIO1 to 12 bit ADC input
u=$(i2cget -f -y 0 0x34 0x85 b)
# /usr/sbin/i2cset -f -y 0 0x34 0x85 $((u|0x02)) # set ADC input range to 0.7-2.7475v
/usr/sbin/i2cset -f -y 0 0x34 0x85 $((u&0xFD)) # set ADC input range to 0-2v
/usr/sbin/i2cset -f -y 0 0x34 0x83 $((v|0x04)) # enable ADC input on GPIO1
}
function reset_gpio0_to_vcc1v8 {
/usr/sbin/i2cset -f -y 0 0x34 0x83 0x80 # disable ADC input on GPIO0
/usr/sbin/i2cset -f -y 0 0x34 0x90 0x03
/usr/sbin/i2cset -f -y 0 0x34 0x91 0x00 #calculation is output voltage = 1.8v + high_4_bit(0x91) * 0.1v
}
function enable_adc_on_ts_pin {
# 25 Hz sample rate, no current output
/usr/sbin/i2cset -f -y 0 0x34 0x84 0x04
# 200 Hz sample rate, no current output
# /usr/sbin/i2cset -f -y 0 0x34 0x84 0xC4
}
function read_gpio0_adc {
# read GPIO0 as 12-bit ADC:
# get the ADC input range: 0.7v offset from register 85H
p=$(i2cget -f -y 0 0x34 0x85 b)
rhigh=$(i2cget -f -y 0 0x34 0x64 b)
rlow=$(expr substr "$(i2cget -f -y 0 0x34 0x65 b)" 4 1)
r="${rhigh}${rlow}"
echo $(( (r * (20475 + p*7000))/4096 ))
}
function read_gpio1_adc {
# read GPIO1 as 12-bit ADC:
# get the ADC input range: 0.7v offset from register 85H
p=$(i2cget -f -y 0 0x34 0x85 b)
rhigh=$(i2cget -f -y 0 0x34 0x66 b)
rlow=$(expr substr "$(i2cget -f -y 0 0x34 0x67 b)" 4 1)
r="${rhigh}${rlow}"
echo $(( (r * (20475 + p*3500))/4096 ))
}
function read_ts_adc {
# read TS as 12-bit ADC:
rhigh=$(i2cget -f -y 0 0x34 0x62 b)
rlow=$(expr substr "$(i2cget -f -y 0 0x34 0x63 b)" 4 1)
r="${rhigh}${rlow}"
# echo $(($(date +%s%N)/1000000)) $(( ((r*32760)/4096) ))
echo $(( ((r*32760)/4096) ))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.