---
# System prepended metadata

title: FAN test2

---

# FAN test2


``` bash=
#!/bin/sh

## -- Fan On low (2400 RPM) (2400/3200) * 40000 
#IPQ8072A(110C)         Trigger:93 Clear:88
#WiFi 2.4/5G RFIC(125C) Trigger:98 Clear:93
#WiFi 6G RFIC(115C)     Trigger:98 Clear:93
#SDX65(105C)            Trigger:88 Clear: 83
#Sub6 PA(85C)           Trigger:81 Clear: 76
## -- Fan On middle (2800 RPM) (2800/3200) * 40000 
#IPQ8072A(110C)	        Trigger:96 Clear:91
#WiFi 2.4/5G RFIC(125C)	Trigger:101 Clear:96
#WiFi 6G RFIC(115C)	Trigger:101 Clear:96
#SDX65(105C)	        Trigger:91 Clear: 86
#Sub6 PA(85C)           Trigger:83 Clear: 78
## -- FAN ON high (3200 RPM) (3200/3200) * 40000 
#IPQ8072A(110C)	        Trigger:99 Clear:94
#WiFi 2.4/5G RFIC(125C)	Trigger:104 Clear:99
#WiFi 6G RFIC(115C)     Trigger:104 Clear:99
#SDX65(105C)	        Trigger:93 Clear: 89
#Sub6 PA(85C)           Trigger:85 Clear: 80

## name,temp1,clear1,PWM1,temp2,clear2,PWM2,temp3,clear3,PWM3
TABLE_TEMP_DUTY="ipq,85,70,40000;
wifi2.4G,90,75,40000;
wifi5G,90,70,40000;
wifi6G,85,70,40000;
sdx65,80,65,40000;
sub6pa,80,65,40000"
TABLE_COL_NUM=$(echo "$TABLE_TEMP_DUTY" | sed -n '1p' | awk -F',' '{print NF}')
TABLE_ROW_NUM=$(echo "$TABLE_TEMP_DUTY" | wc -l)

TABLE_STYPE="ipq wifi2.4G wifi5G wifi6G sdx65 sub6pa"
TABLE_IFACES="ipq wifi0 wifi1 wifi2 sdx65 sub6pa"

SET_DUTY=0
DUTY_LIST=""
DUTY_OLD_LIST=""

CURRENT_TEMP=0
TEMP_LIST=""
TEMP_OLD_LIST=""

MIN_DUTY=0
MAX_DUTY=40000

DBG_MODE=0

dbg_print()
{
	local title="$1"
	local message="$2"
	echo "[$title] $message"
}

## usage: get_curret_temp "$type"
## input:
##	[type]: wifi0|wifi1|wifi2|ipq|sdx65|sub6pa
## output:
##	[temp]: current temp
get_curret_temp()
{
    local title="get_curret_temp"
    local stype=$1
    local enable temp

    temp=0
    case "$stype" in
        wifi*)
            # get enable
            enable=$(thermaltool -i "${stype}" -get | grep "enable" | awk -F':|,| ' '{print $5}')
            dbg_print "$title" "$stype enable: $enable"
            
            if [ "$enable" == "1" ]; then
                # get temp
                temp=$(thermaltool -i "${stype}" -get | grep "sensor temperature" | awk -F':|,| ' '{print $6}')
                dbg_print "$title" "$stype enable: $temp"
            fi
        ;;
        ipq)
            temp0=$(echo "zone5_temp/A53 core0: "$(cat /sys/devices/virtual/thermal/thermal_zone5/temp) | awk '{print $3}')
            temp1=$(echo "zone6_temp/A53 core1: "$(cat /sys/devices/virtual/thermal/thermal_zone6/temp) | awk '{print $3}')
            temp2=$(echo "zone7_temp/A53 core2: "$(cat /sys/devices/virtual/thermal/thermal_zone7/temp) | awk '{print $3}')
            temp3=$(echo "zone8_temp/A53 core3: "$(cat /sys/devices/virtual/thermal/thermal_zone8/temp) | awk '{print $3}')
            temp4=$(echo "zone9_temp/A53 MHM: "$(cat /sys/devices/virtual/thermal/thermal_zone9/temp) | awk '{print $3}')

            dbg_print "$title" "core0 temp: $temp0"
            dbg_print "$title" "core1 temp: $temp1"
            dbg_print "$title" "core2 temp: $temp2"
            dbg_print "$title" "core3 temp: $temp3"
            dbg_print "$title" "MHM temp: $temp4"

            # get max temp
            temp=$temp0
            for tt in $(seq 1 4)
            do
                eval temp_cur=\${temp$tt} #temp_cur=$temp1
                [ $temp_cur -gt $temp ] && temp=$temp_cur
            done
        ;;
        sdx65)
            temp=0
            dbg_print "$title" "$stype temp: $temp"
        ;;
		sub6pa)
            temp=0
            dbg_print "$title" "$stype temp: $temp"
        ;;
		*)
            temp=0
            dbg_print "$title" "Unkown $stype temp: $temp"
        ;;
    esac
    
    CURRENT_TEMP=$temp
    dbg_print "$title" "[$stype] temp: $CURRENT_TEMP"
}

## usage: get_duty_by_temp $type $temp
## input:
##	[type]: ipq/wifi0/wifi1/wifi2/sdx65/sub6pa
##	[temp]: current temp
## output:
##	[duty]: duty cycle
get_duty_by_temp()
{
	local title="get_duty_by_temp"
	local stype=$1
	local temp=$2
	local tbl_data=""
	local set_duty temp_old duty_old
	local idx type_idx

	## get table index by type
	type_idx=$(echo "$TABLE_TEMP_DUTY" | grep -i -n "$stype")
	type_idx=${type_idx%:*}

	tbl_data=$(echo $TABLE_TEMP_DUTY | awk -F';' '{print $'$type_idx'}')
	temp_old=$(echo "$TEMP_OLD_LIST" | awk -F',' '{print $'$type_idx'}')
    duty_old=$(echo "$DUTY_OLD_LIST" | awk -F',' '{print $'$type_idx'}')

    dbg_print "$title" "current temp: $temp , old temp: $temp_old , old duty: $duty_old"
	dbg_print "$title" "TABLE_TEMP_DUTY[$type_idx]: $tbl_data"

	if [ $temp -ge $temp_old ]; then
	        dbg_print "$title" "current:$temp > old:$temp_old --> increase flow"
	else
		dbg_print "$title" "current:$temp < old:$temp_old --> decrease flow"
	fi
	set_duty=$duty_old

	echo "============Rennydebug_tab_col:$TABLE_COL_NUM==============="
	echo "============Rennydebug_tab_row:$TABLE_ROW_NUM==============="
	
	## find duty by temp
	for idx in $(seq $(($TABLE_COL_NUM-2)) -3 2)
	do
		tbl_temp_trigger=$(echo "$tbl_data" | awk -F',' '{print $'$idx'}')
		tbl_temp_clear=$(echo "$tbl_data" | awk -F',' '{print $'$(($idx+1))'}')

		dbg_print "$title" "trigger:$tbl_temp_trigger , clear:$tbl_temp_clear"

		if [ $temp -ge $temp_old ]; then
			## current >= old --> increase
			if [ $temp -ge $tbl_temp_trigger ]; then
				## if idx over table max column, then set duty = max duty
				[ $(($idx+2)) -gt $TABLE_COL_NUM ] && tbl_duty=$MAX_DUTY || tbl_duty=$(echo "$tbl_data" | awk -F',' '{print $'$(($idx+2))'}')

				[ $tbl_duty -gt $set_duty ] && set_duty=$tbl_duty
				dbg_print "$title" "[$stype] curret temp($temp) over than or equal to table trigger temp($tbl_temp_trigger), set duty: $set_duty"
			fi
		else
			## current < old --> decrease
			if [ $temp -le $tbl_temp_clear ]; then
				## if idx less than 1, then set duty = min duty
				[ $(($idx-1)) -le 1 ] && tbl_duty=$MIN_DUTY || tbl_duty=$(echo "$tbl_data" | awk -F',' '{print $'$(($idx-1))'}')

                                [ $tbl_duty -lt $set_duty ] && set_duty=$tbl_duty
                                dbg_print "$title" "[$stype] curret temp($temp) less than table clear temp($tbl_temp_clear), set duty: $set_duty"
                        fi
		fi
	done

	## save current temp as old temp
	temp_old=$temp
	TEMP_OLD_LIST=$(echo -n "$TEMP_OLD_LIST" | tr "," "\n" | sed ''$type_idx's/.*/'$temp_old'/' | tr "\n" ",")
	dbg_print "$title" "TEMP_OLD_LIST: $TEMP_OLD_LIST"
    
    ## save current duty as old duty
	duty_old=$set_duty
	DUTY_OLD_LIST=$(echo -n "$DUTY_OLD_LIST" | tr "," "\n" | sed ''$type_idx's/.*/'$duty_old'/' | tr "\n" ",")
	dbg_print "$title" "DUTY_OLD_LIST: $DUTY_OLD_LIST"

	## save duty
	SET_DUTY=$set_duty
	dbg_print "$title" "set duty: $SET_DUTY"
}

get_duty_list()
{
	local title="get_duty_list"
	local temp_idx=1

	DUTY_LIST=""
	SET_DUTY=0
	CURRENT_TEMP=0

	for idx in $(seq 1 $TABLE_ROW_NUM)
    do
        sensor_type=$(echo "$TABLE_STYPE" | awk -F' ' '{print $'$idx'}')
		iface_type=$(echo "$TABLE_IFACES" | awk -F' ' '{print $'$idx'}')
        
        ## get temp
        #get_curret_temp "$sensor_type"
        if [ "$DBG_MODE" = "0" ]; then
            get_curret_temp "$iface_type"
        else
            CURRENT_TEMP=$(echo "$TEMP_LIST" | awk -F',' '{print $'$temp_idx'}')
            temp_idx=$(($temp_idx+1))
        fi

        ## get duty cycle by temp
        get_duty_by_temp "$sensor_type" $CURRENT_TEMP

        dbg_print "$title" "type: $sensor_type , current temp: $CURRENT_TEMP , duty: $SET_DUTY"

        [ -z "$DUTY_LIST" ] && DUTY_LIST=$SET_DUTY || DUTY_LIST="$DUTY_LIST,"$SET_DUTY
    done

    ## DUTY_LIST="35000,36000,37000,38000,39000,40000"
    dbg_print "$title" "duty list: $DUTY_LIST"
}

## usage: setPWM "$duty"
## input:
##	duty: duty cycle of PWM
setPWM()
{
	local duty=$1
    	dbg_print "setPWM" "set_duty: $duty"

    	[ $duty -gt 40000 -o $duty -lt 0 ] && return 1
    
	echo "================Rennytest $duty ============="
	echo 25 > /sys/class/gpio/export
    echo out > /sys/class/gpio/gpio25/direction
    echo 1 > /sys/class/gpio/gpio25/value
	
	# set PWM
	pwm_enable=$(cat /sys/class/pwm/pwmchip0/pwm1/enable)
	if [ ! -d "/sys/class/pwm/pwmchip0/pwm1" ]; then
		echo 1 > /sys/class/pwm/pwmchip0/export
		echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable
		echo 40000 > /sys/class/pwm/pwmchip0/pwm1/period
		echo $duty > /sys/class/pwm/pwmchip0/pwm1/duty_cycle
	elif [ "$pwm_enable" = "0" ]; then
		echo 1 > /sys/class/pwm/pwmchip0/export
		echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable
		echo 40000 > /sys/class/pwm/pwmchip0/pwm1/period
		echo $duty > /sys/class/pwm/pwmchip0/pwm1/duty_cycle
	else
		echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable
		echo 40000 > /sys/class/pwm/pwmchip0/pwm1/period
		echo $duty > /sys/class/pwm/pwmchip0/pwm1/duty_cycle
	fi
}

main()
{
    local set_duty

    CURRENT_TEMP=0
    TEMP_LIST=""
    TEMP_OLD_LIST="0,0,0,0,0,0"
    
    SET_DUTY=0
    DUTY_LIST=""
    DUTY_OLD_LIST="0,0,0,0,0,0"

    if [ $1 -eq 1 ]; then
	    DBG_MODE=1
	    [ -n "$2" ] && TEMP_LIST="$2" || TEMP_LIST="0,0,0,0,0,0"
    fi

    while [ true ];
    do
	    ## get duty by temp of all sensor type
	    get_duty_list
	    if [ -z "$DUTY_LIST" ]; then
		    dbg_print "main" "Duty list is EMPTY !!!"
		    break
	    fi

	    ## select max duty to set PWM
	    set_duty=0
	    for duty in $(echo "$DUTY_LIST" | tr "," " ")
	    do
	    	    [ $duty -gt $set_duty ] && set_duty=$duty
	    done

	    dbg_print "main" "Select duty: $set_duty to set PWM"

	    ## set PWM
	    #setPWM $set_duty
	    [ "$DBG_MODE" = "0" ] && setPWM $set_duty || dbg_print "setPWM" "set_duty: $duty"
		#setPWM $set_duty
#break
            sleep 30
    done
}

main $# $@

exit 0



```