188 lines
6.6 KiB
Bash
188 lines
6.6 KiB
Bash
#!/bin/sh
|
|
|
|
mode=$1
|
|
i2c_bus=$2
|
|
chip_slave=$3
|
|
server_platform=$4
|
|
server_type=$5
|
|
|
|
test_data1=$2
|
|
test_data2=$3
|
|
test_resistor=$4
|
|
|
|
path=`pwd`
|
|
fmt_print="${path}/format_print.sh"
|
|
i2c_script="${path}/i2c_${server_platform}.sh"
|
|
server_script="${path}/platform_${server_type}.sh"
|
|
|
|
# INA3221 shunt resistor(unit: mohm)
|
|
shunt_resistor_0="2"
|
|
shunt_resistor_1="2"
|
|
shunt_resistor_2="5"
|
|
|
|
# INA3221 channel name
|
|
INA3221_Channel0_name="Channel0"
|
|
INA3221_Channel1_name="Channel1"
|
|
INA3221_Channel2_name="Channel2"
|
|
|
|
ina3221_ch0_volt="0"
|
|
ina3221_ch1_volt="0"
|
|
ina3221_ch2_volt="0"
|
|
ina3221_ch0_current="0"
|
|
ina3221_ch1_current="0"
|
|
ina3221_ch2_current="0"
|
|
|
|
REG_ina3221_ch1="0x01"
|
|
REG_ina3221_ch2="0x03"
|
|
REG_ina3221_ch3="0x05"
|
|
REG_ina3221_bus1="0x02"
|
|
REG_ina3221_bus2="0x04"
|
|
REG_ina3221_bus3="0x06"
|
|
|
|
INA3221_SHUNT_VOLT=0
|
|
INA3221_BUS_VOLT=1
|
|
INA3221_POWER=2
|
|
|
|
print_help(){
|
|
echo "---------------------------------------------------------"
|
|
echo "<option> read"
|
|
echo " Format : ./plugin_ina3221.sh read [i2c-bus] [slave] [platform]"
|
|
echo " Example: ./plugin_ina3221.sh read 13 0x1f m7"
|
|
echo "---------------------------------------------------------"
|
|
echo "<option> test"
|
|
echo " Format : ./plugin_ina3221.sh test [data1] [data2] [resistor]"
|
|
echo " Example: ./plugin_ina3221.sh test 0xc7 0x00 5"
|
|
echo "---------------------------------------------------------"
|
|
echo "<option> help"
|
|
echo " Format : ./plugin_ina3221.sh help"
|
|
echo " Example: ./plugin_ina3221.sh help"
|
|
echo "---------------------------------------------------------"
|
|
}
|
|
|
|
# @Param1 ina3221 data high 8bit
|
|
# @Param2 ina3221 data high 8bit
|
|
# @Param3 channel number
|
|
# @Param4 mode select 0 - shunt volt
|
|
# 1 - bus volt
|
|
# @Param5 shunt resistor(only used in mode shunt volt)
|
|
# @Param6 customization channel name
|
|
convert_ina3221_data(){
|
|
|
|
hex_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
|
|
hex_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
|
|
merge_value="${hex_value1}${hex_value2}"
|
|
|
|
# bc calculator recognized upper case only, change data to upper case
|
|
upper_hex_value=$(echo "$merge_value" | awk '{
|
|
for(i=1; i<=length($0); i++){
|
|
if(tolower(substr($0,i,1)) ~ /^[a-f]$/)
|
|
printf toupper(substr($0,i,1));
|
|
else
|
|
printf substr($0,i,1);
|
|
}
|
|
print ""
|
|
}')
|
|
|
|
# change data from hex to dec
|
|
dec_val=$(echo "ibase=16; $upper_hex_value" | bc)
|
|
|
|
if [ $4 -eq $INA3221_BUS_VOLT ];then
|
|
volt=$(echo "scale=4; $dec_val / 8 * 40 / 10000 * 2" | bc)
|
|
format_volt=$(echo "$volt" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
|
|
echo "$6 : $format_volt V, hex value: $upper_hex_value"
|
|
|
|
if [ $3 -eq 0 ];then
|
|
ina3221_ch0_volt=$format_volt
|
|
elif [ $3 -eq 1 ];then
|
|
ina3221_ch1_volt=$format_volt
|
|
else
|
|
ina3221_ch2_volt=$format_volt
|
|
fi
|
|
|
|
elif [ $4 -eq $INA3221_SHUNT_VOLT ];then
|
|
current_mv=$(echo "scale=4; $dec_val / 8 * 40 / 1000" | bc)
|
|
current=$(echo "scale=4; $current_mv / $5" | bc)
|
|
|
|
format_current=$(echo "$current" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
|
|
format_current_mv=$(echo "$current_mv" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
|
|
|
|
echo "$6 : $format_current A, shunt volt: $format_current_mv mV, shunt resistor: $5 mOhm, hex value: $upper_hex_value"
|
|
|
|
if [ $3 -eq 0 ];then
|
|
ina3221_ch0_current=$format_current
|
|
elif [ $3 -eq 1 ];then
|
|
ina3221_ch1_current=$format_current
|
|
else
|
|
ina3221_ch2_current=$format_current
|
|
fi
|
|
|
|
elif [ $4 -eq $INA3221_POWER ];then
|
|
# calculate power
|
|
power_ch0=$(echo "scale=4; $ina3221_ch0_volt * $ina3221_ch0_current" | bc)
|
|
power_ch1=$(echo "scale=4; $ina3221_ch1_volt * $ina3221_ch1_current" | bc)
|
|
power_ch2=$(echo "scale=4; $ina3221_ch2_volt * $ina3221_ch2_current" | bc)
|
|
|
|
format_power_ch0=$(echo "$power_ch0" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
|
|
format_power_ch1=$(echo "$power_ch1" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
|
|
format_power_ch2=$(echo "$power_ch2" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
|
|
|
|
total_power=$(echo "scale=4; $power_ch0 + $power_ch1 + $power_ch2" | bc)
|
|
|
|
echo "$INA3221_Channel0_name : $format_power_ch0 W"
|
|
echo "$INA3221_Channel1_name : $format_power_ch1 W"
|
|
echo "$INA3221_Channel2_name : $format_power_ch2 W"
|
|
echo "total power: $total_power W"
|
|
fi
|
|
}
|
|
|
|
read_ina3221_data(){
|
|
res_ch0=`$i2c_script $i2c_bus 1 $chip_slave $REG_ina3221_ch1 r2`
|
|
res_ch1=`$i2c_script $i2c_bus 1 $chip_slave $REG_ina3221_ch2 r2`
|
|
res_ch2=`$i2c_script $i2c_bus 1 $chip_slave $REG_ina3221_ch3 r2`
|
|
res_bus0=`$i2c_script $i2c_bus 1 $chip_slave $REG_ina3221_bus1 r2`
|
|
res_bus1=`$i2c_script $i2c_bus 1 $chip_slave $REG_ina3221_bus2 r2`
|
|
res_bus2=`$i2c_script $i2c_bus 1 $chip_slave $REG_ina3221_bus3 r2`
|
|
|
|
# After read option , reset pca9548
|
|
$server_script reset
|
|
|
|
$fmt_print "log" "Info" "[plugin_ina3221] channel 0 shunt volt: $res_ch0"
|
|
$fmt_print "log" "Info" "[plugin_ina3221] channel 1 shunt volt: $res_ch1"
|
|
$fmt_print "log" "Info" "[plugin_ina3221] channel 2 shunt volt: $res_ch2"
|
|
$fmt_print "log" "Info" "[plugin_ina3221] channel 0 bus volt: $res_bus0"
|
|
$fmt_print "log" "Info" "[plugin_ina3221] channel 0 bus volt: $res_bus1"
|
|
$fmt_print "log" "Info" "[plugin_ina3221] channel 0 bus volt: $res_bus2"
|
|
|
|
$fmt_print "console" "Info" "The INA3221 shunt value is :"
|
|
convert_ina3221_data $res_ch0 0 $INA3221_SHUNT_VOLT $shunt_resistor_0 "$INA3221_Channel0_name"
|
|
convert_ina3221_data $res_ch1 1 $INA3221_SHUNT_VOLT $shunt_resistor_1 "$INA3221_Channel1_name"
|
|
convert_ina3221_data $res_ch2 2 $INA3221_SHUNT_VOLT $shunt_resistor_2 "$INA3221_Channel2_name"
|
|
|
|
$fmt_print "console" "Info" "The INA3221 bus value is :"
|
|
convert_ina3221_data $res_bus0 0 $INA3221_BUS_VOLT null "$INA3221_Channel0_name"
|
|
convert_ina3221_data $res_bus1 1 $INA3221_BUS_VOLT null "$INA3221_Channel1_name"
|
|
convert_ina3221_data $res_bus2 2 $INA3221_BUS_VOLT null "$INA3221_Channel2_name"
|
|
|
|
$fmt_print "console" "Info" "The INA3221 bus power is :"
|
|
convert_ina3221_data 0 0 0 $INA3221_POWER
|
|
}
|
|
|
|
test_ina3221_value(){
|
|
convert_ina3221_data $test_data1 $test_data2 0 $INA3221_SHUNT_VOLT $test_resistor "INA3221"
|
|
}
|
|
|
|
case "${mode}" in
|
|
"read")
|
|
read_ina3221_data
|
|
;;
|
|
"test")
|
|
test_ina3221_value
|
|
;;
|
|
"help")
|
|
print_help
|
|
;;
|
|
*)
|
|
$fmt_print "console" "Error" "[plugin_ina3221]Unexpected Input Param : $mode"
|
|
print_help
|
|
;;
|
|
esac |