From 559263cc2d19e5fa73bd9446871d404d2d8ede71 Mon Sep 17 00:00:00 2001 From: leimingsheng Date: Fri, 9 Jan 2026 16:47:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(master=20:=20nicsensor)=20update=20to=201.?= =?UTF-8?q?9.1=20=E3=80=90=E9=87=8D=E8=A6=81=E3=80=91=201.=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E9=80=82=E9=85=8D=E4=BC=A0=E6=84=9F=E5=99=A8ina226=20?= =?UTF-8?q?2.=E4=BC=98=E5=8C=96help=E5=8A=9F=E8=83=BD=203.=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nicsensor.sh | 229 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 179 insertions(+), 50 deletions(-) diff --git a/nicsensor.sh b/nicsensor.sh index 11e7f60..c71cfc4 100755 --- a/nicsensor.sh +++ b/nicsensor.sh @@ -1,5 +1,5 @@ #!/bin/sh -SCRIPT_VERSION="1.9" +SCRIPT_VERSION="1.9.1" # --------------------------------------------------------- # Project Feature Varible (Change if need) # --------------------------------------------------------- @@ -52,13 +52,16 @@ TMP468_Channel8_name="Remote8" # TMP112 sensor name TMP112_Sensor_name="Temperature" +# INA226 channel name +INA226_Channel0_name="Channel0" + # fru basic offset fru_offset="0x00 0x00" fru_size=256 # --------------------------------------------------------- # 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" # Which server will use this script server_type="auto" @@ -112,6 +115,8 @@ REG_ina3221_ch3="0x05" REG_ina3221_bus1="0x02" REG_ina3221_bus2="0x04" REG_ina3221_bus3="0x06" +REG_ina226_ch0_current="0x01" +REG_ina226_ch0_voltage="0x02" REG_tmp112="0x00" REG_tmp468="0x80" @@ -147,60 +152,95 @@ hex_pmbus_size=0 # --------------------------------------------------------- search_plugin_help(){ PMBUS_PLUGIN="./pmbus_cmd_list.sh" + plugin_flag=0 if [ -e ${PMBUS_PLUGIN} ];then $PMBUS_PLUGIN "help" + plugin_flag=1 + fi + if [ $plugin_flag -eq 0 ];then + echo " No Plugin found in file system" 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 " + 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(){ echo "" echo "=====================>>> nicsensor script usage <<<======================" - echo " Function 1 - Read Sensor Value" - echo " 1) Command Format : ./nicsensor.sh [slot] [sensor tpye] [slave]" - echo " 2) Option Detail" - echo " - [slot] : 0 1 2 3 4 5 ..." - echo " - [sensor type] : $Support_Sensor_List" - echo " - [slave] : chip slave address , please provide 7 bit address" - echo " 3) E.G. : ./nicsensor.sh 1 adc128 0x1f" - echo " ./nicsensor.sh pcie1 ina3221 0x42" - echo " ./nicsensor.sh ocp0 emc1413 0x4c" - echo "" - echo " Function 2 - Detect I2C for each slot" - echo " 1) Command Format : ./nicsensor.sh detect [pcie/ocp]" - echo " 2) E.G. : ./nicsensor.sh detect pcie" - echo " ./nicsensor.sh detect ocp [some server don't support]" - echo " 3) Note : This function now only support on server:" - echo " $Support_Server_List" - echo "" - echo " Function 3 - Send an i2c command to chip on Nic" - echo " 1) Command Format : ./nicsensor.sh [slot] chip [slave] [command]" - echo " 2) Option Detail" - echo " - [slot] : 0 1 2 3 4 5 ..." - echo " - [slave] : chip slave address , please provide 7 bit address" - 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 "" - echo " Function 4 - FRU Read and Wirte" - echo " 1) Command Format : ./nicsensor.sh [slot] fru [slave] [option] [frudata]" - echo " 2) Option Detail" - echo " - [slot] : 0 1 2 3 4 5 ..." - echo " - [slave] : chip slave address , please provide 7 bit address" - echo " - [option] : read/write, default to read" - echo " - [frudata] : fru bin file which will write to eeprom, only for write option" - echo " 3) E.G. : ./nicsensor.sh pcie0 fru 0x57" - echo " : ./nicsensor.sh pcie0 fru 0x57 read" - echo " : ./nicsensor.sh pcie0 fru 0x57 write /tmp/fru.bin" - echo "" - echo " Function 5 - Settings" - echo " 1) Command Format : ./nicsensor.sh [get/set] [property] [value]" - echo " 2) Option Detail" - echo " - [property] : debug, n_detect, server, fru_size" - echo " - [value] : property's value" - echo " 3) E.G. : ./nicsensor.sh set server 5280m7" - echo " : ./nicsensor.sh set debug 1" - echo " : ./nicsensor.sh set n_detect 1" - echo "" - search_plugin_help + if [ "$1" = "1" ] || [ "$1" = "all" ];then + echo " Function 1 - Read Sensor Value" + echo " 1) Command Format : ./nicsensor.sh [slot] [sensor tpye] [slave]" + echo " 2) Option Detail" + echo " - [slot] : 0 1 2 3 4 5 ..." + echo " - [sensor type] : $Support_Sensor_List" + echo " - [slave] : chip slave address , please provide 7 bit address" + echo " 3) E.G. : ./nicsensor.sh 1 adc128 0x1f" + echo " ./nicsensor.sh pcie1 ina3221 0x42" + echo " ./nicsensor.sh ocp0 emc1413 0x4c" + echo "" + fi + if [ "$1" = "2" ] || [ "$1" = "all" ];then + echo " Function 2 - Detect I2C for each slot" + echo " 1) Command Format : ./nicsensor.sh detect [pcie/ocp]" + echo " 2) E.G. : ./nicsensor.sh detect pcie" + echo " ./nicsensor.sh detect ocp [some server don't support]" + echo " 3) Note : This function now only support on server:" + echo " $Support_Server_List" + echo "" + fi + if [ "$1" = "3" ] || [ "$1" = "all" ];then + echo " Function 3 - Send an i2c command to chip on Nic" + echo " 1) Command Format : ./nicsensor.sh [slot] chip [slave] [command]" + echo " 2) Option Detail" + echo " - [slot] : 0 1 2 3 4 5 ..." + echo " - [slave] : chip slave address , please provide 7 bit address" + 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 "" + fi + if [ "$1" = "4" ] || [ "$1" = "all" ];then + echo " Function 4 - FRU Read and Wirte" + echo " 1) Command Format : ./nicsensor.sh [slot] fru [slave] [option] [frudata]" + echo " 2) Option Detail" + echo " - [slot] : 0 1 2 3 4 5 ..." + echo " - [slave] : chip slave address , please provide 7 bit address" + echo " - [option] : read/write, default to read" + echo " - [frudata] : fru bin file which will write to eeprom, only for write option" + echo " 3) E.G. : ./nicsensor.sh pcie0 fru 0x57" + echo " : ./nicsensor.sh pcie0 fru 0x57 read" + echo " : ./nicsensor.sh pcie0 fru 0x57 write /tmp/fru.bin" + echo "" + fi + if [ "$1" = "5" ] || [ "$1" = "all" ];then + echo " Function 5 - Properties Settings" + echo " 1) Command Format : ./nicsensor.sh [get/set] [property] [value]" + echo " 2) Option Detail" + echo " - [property] : debug, n_detect, server, fru_size" + echo " - [value] : property's value" + echo " 3) E.G. : ./nicsensor.sh set server 5280m7" + echo " : ./nicsensor.sh set debug 1" + echo " : ./nicsensor.sh set n_detect 1" + echo "" + fi + if [ "$1" = "ex" ] || [ "$1" = "all" ];then + search_plugin_help + fi } # print format message to console|debuglog @@ -235,6 +275,10 @@ do_i2c_detect(){ # @Param1 i2c bus number # @Param2 pca9548 slave address reset_pca9548(){ + # In Debug mode, skip this function + if [ $DEBUG_MODE -eq 1 ];then + return + fi i2ctransfer -y $1 w1@$2 0x00 } @@ -368,6 +412,8 @@ init_debuglog(){ fmt_print "log" $INFO "Operation Type : get property value" elif [ "$Param1" = "set" ];then 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 fmt_print "log" $INFO "Operation Type : help info" else @@ -392,6 +438,7 @@ record_config_info(){ fi } +# Desc : Gain control of the PCA9641 chip get_pca9641_control(){ # Request 9641 lock res_lock=`i2ctransfer -y $i2c_bus w2@$pca9641_slave $REG_pca9641_controll 0x81 r1` @@ -412,6 +459,7 @@ get_pca9641_control(){ fi } +# Desc : Switch the channel of the 9548 chip. switch_pca9548_channel(){ # set 9548 channel res_setchannel=`i2ctransfer -y $i2c_bus w1@$pca9548_slave $pca9548_channel` @@ -423,10 +471,19 @@ switch_pca9548_channel(){ 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(){ init_debuglog + if [ "$Param1" = "help" ] && [ $param_num -eq 2 ];then + print_usage $Param2 + exit 0 + fi + if [ "$Param1" = "get" ] || [ "$Param1" = "set" ];then get_set_properties fi @@ -1129,6 +1186,75 @@ process_tmp112(){ 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 # --------------------------------------------------------- # do a customization command @@ -1328,6 +1454,9 @@ start_get_sensor(){ "ina3221") process_ina3221 ;; + "ina226") + process_ina226 + ;; "tmp468") process_tmp468 ;; @@ -1551,7 +1680,7 @@ if [ "$1" = "detect" ];then exit 0 fi if [ $param_num -le 2 ];then - print_usage + print_usage_guide else start_get_sensor fi