feat(master : nicsensor) update to 1.9.1 【重要】

1.新增适配传感器ina226
2.优化help功能
3.优化代码可读性
This commit is contained in:
leimingsheng 2026-01-09 16:47:11 +08:00
parent d91a9f87ae
commit 559263cc2d

@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh
SCRIPT_VERSION="1.9" SCRIPT_VERSION="1.9.1"
# --------------------------------------------------------- # ---------------------------------------------------------
# Project Feature Varible (Change if need) # Project Feature Varible (Change if need)
# --------------------------------------------------------- # ---------------------------------------------------------
@ -52,13 +52,16 @@ TMP468_Channel8_name="Remote8"
# TMP112 sensor name # TMP112 sensor name
TMP112_Sensor_name="Temperature" TMP112_Sensor_name="Temperature"
# INA226 channel name
INA226_Channel0_name="Channel0"
# fru basic offset # fru basic offset
fru_offset="0x00 0x00" fru_offset="0x00 0x00"
fru_size=256 fru_size=256
# --------------------------------------------------------- # ---------------------------------------------------------
# Global Settings # Global Settings
# --------------------------------------------------------- # ---------------------------------------------------------
Support_Sensor_List="emc1413, adc128, ina3221, tmp468, tmp112" Support_Sensor_List="emc1413, adc128, ina3221, ina226, tmp468, tmp112"
Support_Server_List="5280m7, 5468m7, 5688m7, donghu, yichun, qiandaohu" Support_Server_List="5280m7, 5468m7, 5688m7, donghu, yichun, qiandaohu"
# Which server will use this script # Which server will use this script
server_type="auto" server_type="auto"
@ -112,6 +115,8 @@ REG_ina3221_ch3="0x05"
REG_ina3221_bus1="0x02" REG_ina3221_bus1="0x02"
REG_ina3221_bus2="0x04" REG_ina3221_bus2="0x04"
REG_ina3221_bus3="0x06" REG_ina3221_bus3="0x06"
REG_ina226_ch0_current="0x01"
REG_ina226_ch0_voltage="0x02"
REG_tmp112="0x00" REG_tmp112="0x00"
REG_tmp468="0x80" REG_tmp468="0x80"
@ -147,14 +152,38 @@ hex_pmbus_size=0
# --------------------------------------------------------- # ---------------------------------------------------------
search_plugin_help(){ search_plugin_help(){
PMBUS_PLUGIN="./pmbus_cmd_list.sh" PMBUS_PLUGIN="./pmbus_cmd_list.sh"
plugin_flag=0
if [ -e ${PMBUS_PLUGIN} ];then if [ -e ${PMBUS_PLUGIN} ];then
$PMBUS_PLUGIN "help" $PMBUS_PLUGIN "help"
plugin_flag=1
fi
if [ $plugin_flag -eq 0 ];then
echo " No Plugin found in file system"
fi fi
} }
print_usage_guide(){
echo ""
echo "====================>>> User Gudie <<<======================="
echo " Tool Format : ./nicsensor.sh Param1 Param2 Param3 ..."
echo " See more help info by: ./nicsensor.sh help <index>"
echo " Index info:"
echo " all : Show all help"
echo " ex : Show plugin help"
echo " 1 : Read Sensor Value"
echo " 2 : Detect I2C for each slot"
echo " 3 : Send an i2c command to chip on Nic"
echo " 4 : FRU Read and Wirte"
echo " 5 : Properties Settings"
echo ""
echo " E.G. : ./nicsensor.sh help 1"
}
#@Param1 - help index
print_usage(){ print_usage(){
echo "" echo ""
echo "=====================>>> nicsensor script usage <<<======================" echo "=====================>>> nicsensor script usage <<<======================"
if [ "$1" = "1" ] || [ "$1" = "all" ];then
echo " Function 1 - Read Sensor Value" echo " Function 1 - Read Sensor Value"
echo " 1) Command Format : ./nicsensor.sh [slot] [sensor tpye] [slave]" echo " 1) Command Format : ./nicsensor.sh [slot] [sensor tpye] [slave]"
echo " 2) Option Detail" echo " 2) Option Detail"
@ -165,6 +194,8 @@ print_usage(){
echo " ./nicsensor.sh pcie1 ina3221 0x42" echo " ./nicsensor.sh pcie1 ina3221 0x42"
echo " ./nicsensor.sh ocp0 emc1413 0x4c" echo " ./nicsensor.sh ocp0 emc1413 0x4c"
echo "" echo ""
fi
if [ "$1" = "2" ] || [ "$1" = "all" ];then
echo " Function 2 - Detect I2C for each slot" echo " Function 2 - Detect I2C for each slot"
echo " 1) Command Format : ./nicsensor.sh detect [pcie/ocp]" echo " 1) Command Format : ./nicsensor.sh detect [pcie/ocp]"
echo " 2) E.G. : ./nicsensor.sh detect pcie" echo " 2) E.G. : ./nicsensor.sh detect pcie"
@ -172,6 +203,8 @@ print_usage(){
echo " 3) Note : This function now only support on server:" echo " 3) Note : This function now only support on server:"
echo " $Support_Server_List" echo " $Support_Server_List"
echo "" echo ""
fi
if [ "$1" = "3" ] || [ "$1" = "all" ];then
echo " Function 3 - Send an i2c command to chip on Nic" echo " Function 3 - Send an i2c command to chip on Nic"
echo " 1) Command Format : ./nicsensor.sh [slot] chip [slave] [command]" echo " 1) Command Format : ./nicsensor.sh [slot] chip [slave] [command]"
echo " 2) Option Detail" echo " 2) Option Detail"
@ -180,6 +213,8 @@ print_usage(){
echo " - [command] : The i2c command which will set to the chip" echo " - [command] : The i2c command which will set to the chip"
echo " 3) E.G. : ./nicsensor.sh pcie1 chip 0x10 'i2ctransfer -y 12 w1@0x10 0x00 r2'" echo " 3) E.G. : ./nicsensor.sh pcie1 chip 0x10 'i2ctransfer -y 12 w1@0x10 0x00 r2'"
echo "" echo ""
fi
if [ "$1" = "4" ] || [ "$1" = "all" ];then
echo " Function 4 - FRU Read and Wirte" echo " Function 4 - FRU Read and Wirte"
echo " 1) Command Format : ./nicsensor.sh [slot] fru [slave] [option] [frudata]" echo " 1) Command Format : ./nicsensor.sh [slot] fru [slave] [option] [frudata]"
echo " 2) Option Detail" echo " 2) Option Detail"
@ -191,7 +226,9 @@ print_usage(){
echo " : ./nicsensor.sh pcie0 fru 0x57 read" echo " : ./nicsensor.sh pcie0 fru 0x57 read"
echo " : ./nicsensor.sh pcie0 fru 0x57 write /tmp/fru.bin" echo " : ./nicsensor.sh pcie0 fru 0x57 write /tmp/fru.bin"
echo "" echo ""
echo " Function 5 - Settings" fi
if [ "$1" = "5" ] || [ "$1" = "all" ];then
echo " Function 5 - Properties Settings"
echo " 1) Command Format : ./nicsensor.sh [get/set] [property] [value]" echo " 1) Command Format : ./nicsensor.sh [get/set] [property] [value]"
echo " 2) Option Detail" echo " 2) Option Detail"
echo " - [property] : debug, n_detect, server, fru_size" echo " - [property] : debug, n_detect, server, fru_size"
@ -200,7 +237,10 @@ print_usage(){
echo " : ./nicsensor.sh set debug 1" echo " : ./nicsensor.sh set debug 1"
echo " : ./nicsensor.sh set n_detect 1" echo " : ./nicsensor.sh set n_detect 1"
echo "" echo ""
fi
if [ "$1" = "ex" ] || [ "$1" = "all" ];then
search_plugin_help search_plugin_help
fi
} }
# print format message to console|debuglog # print format message to console|debuglog
@ -235,6 +275,10 @@ do_i2c_detect(){
# @Param1 i2c bus number # @Param1 i2c bus number
# @Param2 pca9548 slave address # @Param2 pca9548 slave address
reset_pca9548(){ reset_pca9548(){
# In Debug mode, skip this function
if [ $DEBUG_MODE -eq 1 ];then
return
fi
i2ctransfer -y $1 w1@$2 0x00 i2ctransfer -y $1 w1@$2 0x00
} }
@ -368,6 +412,8 @@ init_debuglog(){
fmt_print "log" $INFO "Operation Type : get property value" fmt_print "log" $INFO "Operation Type : get property value"
elif [ "$Param1" = "set" ];then elif [ "$Param1" = "set" ];then
fmt_print "log" $INFO "Operation Type : set property value" fmt_print "log" $INFO "Operation Type : set property value"
elif [ "$Param1" = "help" ];then
fmt_print "log" $INFO "Operation Type : help info"
elif [ $param_num -lt 2 ];then elif [ $param_num -lt 2 ];then
fmt_print "log" $INFO "Operation Type : help info" fmt_print "log" $INFO "Operation Type : help info"
else else
@ -392,6 +438,7 @@ record_config_info(){
fi fi
} }
# Desc : Gain control of the PCA9641 chip
get_pca9641_control(){ get_pca9641_control(){
# Request 9641 lock # Request 9641 lock
res_lock=`i2ctransfer -y $i2c_bus w2@$pca9641_slave $REG_pca9641_controll 0x81 r1` res_lock=`i2ctransfer -y $i2c_bus w2@$pca9641_slave $REG_pca9641_controll 0x81 r1`
@ -412,6 +459,7 @@ get_pca9641_control(){
fi fi
} }
# Desc : Switch the channel of the 9548 chip.
switch_pca9548_channel(){ switch_pca9548_channel(){
# set 9548 channel # set 9548 channel
res_setchannel=`i2ctransfer -y $i2c_bus w1@$pca9548_slave $pca9548_channel` res_setchannel=`i2ctransfer -y $i2c_bus w1@$pca9548_slave $pca9548_channel`
@ -423,10 +471,19 @@ switch_pca9548_channel(){
fi fi
} }
# pre execute hook function # Desc : Pre-execute hook function
# Function 1 : Parameter Integrity Check
# Function 2 : Partial Data Preprocessing
# Function 3 : Runtime Environment Initialization
# Function 4 : Implementation of Parameter Setting Function
pre_exec_hook(){ pre_exec_hook(){
init_debuglog init_debuglog
if [ "$Param1" = "help" ] && [ $param_num -eq 2 ];then
print_usage $Param2
exit 0
fi
if [ "$Param1" = "get" ] || [ "$Param1" = "set" ];then if [ "$Param1" = "get" ] || [ "$Param1" = "set" ];then
get_set_properties get_set_properties
fi fi
@ -1129,6 +1186,75 @@ process_tmp112(){
read_tmp112_value read_tmp112_value
} }
# --------------------------------------------------------- # ---------------------------------------------------------
# Chip INA226
# ---------------------------------------------------------
# @Param1 ina226 voltage data high 8bit
# @Param2 ina226 voltage data low 8bit
# @Param3 ina226 current data high 8bit
# @Param4 ina226 current data low 8bit
# @Param5 customization sensor name
convert_ina226_data(){
if [ "$5" = "disable" ];then
return
fi
hex_voltage_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
hex_voltage_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
hex_current_value1=$(echo "$3" | awk '{sub(/^0x/,""); print}')
hex_current_value2=$(echo "$4" | awk '{sub(/^0x/,""); print}')
merge_voltage_value="${hex_voltage_value1}${hex_voltage_value2}"
merge_current_value="${hex_current_value1}${hex_current_value2}"
upper_hex_voltage_value=$(echo "$merge_voltage_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 ""
}')
dec_voltage_val=$(echo "ibase=16; $upper_hex_voltage_value" | bc)
upper_hex_current_value=$(echo "$merge_current_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 ""
}')
dec_current_val=$(echo "ibase=16; $upper_hex_current_value" | bc)
real_voltage=$(echo "scale=4; $dec_voltage_val * 1.25 / 1000 " | bc)
real_current=$(echo "scale=4; $dec_current_val * 2.5 * 0.167 / 1000 " | bc)
format_voltage=$(echo "$real_voltage" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
format_current=$(echo "$real_current" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
real_power=$(echo "scale=4; $format_voltage * $format_current" | bc)
format_power=$(echo "$real_power" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
echo "$5 Voltage: ${format_voltage}V"
echo "$5 Current: ${format_current}A"
echo "$5 Power : ${format_power}W"
}
read_ina226_value(){
fmt_print "log" $INFO "Start Read INA226 channel0 data ..."
hex_voltage_res=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina226_ch0_voltage r2`
hex_current_res=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina226_ch0_current r2`
reset_pca9548 $i2c_bus $pca9548_slave
fmt_print "log" $INFO "INA226 raw volt : $hex_voltage_res"
fmt_print "log" $INFO "INA226 raw volt : $hex_current_res"
convert_ina226_data $hex_voltage_res $hex_current_res "$INA226_Channel0_name"
}
process_ina226(){
read_ina226_value
}
# ---------------------------------------------------------
# CHIP # CHIP
# --------------------------------------------------------- # ---------------------------------------------------------
# do a customization command # do a customization command
@ -1328,6 +1454,9 @@ start_get_sensor(){
"ina3221") "ina3221")
process_ina3221 process_ina3221
;; ;;
"ina226")
process_ina226
;;
"tmp468") "tmp468")
process_tmp468 process_tmp468
;; ;;
@ -1551,7 +1680,7 @@ if [ "$1" = "detect" ];then
exit 0 exit 0
fi fi
if [ $param_num -le 2 ];then if [ $param_num -le 2 ];then
print_usage print_usage_guide
else else
start_get_sensor start_get_sensor
fi fi