Notes19检查i2c设备在位和访问

Posted 码农编程录

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Notes19检查i2c设备在位和访问相关的知识,希望对你有一定的参考价值。


1.check.sh

#check.sh脚本运行在BMC下用于检查i2c设备。
#会自动打印检查结果,须在脚本后添加检查次数,否则一直循环。
#使用方法:./check.sh num。

#!/bin/bash
#stop ipmistack
stopipmi(){
    cnt=1
    while true
    do  
		/etc/init.d/ipmistack stop >/dev/null 2>&1
        if [ $? = 0 ]
        then
            echo "ipmistack stop  Succes!" 
            echo "ipmistack stop  Succes!" >>/tmp/i2c_check.log
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to stop ipmistack !" 
            echo "Unable to stop ipmistack !" >>/tmp/i2c_check.log
			/etc/init.d/ipmistack start
            exit
        fi
        cnt=$((cnt+1))
		sleep 1
    done
}

#write to device
wdevice(){
    cnt=1
    while true
    do  
        echo 0x73 > /sys/bus/i2c/devices/i2c-7/delete_device 2>&1
        if [ $? = 0 ]
        then
            echo "write 0x73  Succes!" 
            echo "write 0x73  Succes!" >>/tmp/i2c_check.log
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to write 0x73 !" 
            echo "Unable to write 0x73 !" >>/tmp/i2c_check.log
			/etc/init.d/ipmistack start
            exit
        fi
        cnt=$((cnt+1))
		sleep 1
    done
}

#switch to 9548
switch9548(){
    cnt=1
    while true
    do   
		i2c-test -b 7 -s 0x73 -w -d 0x08 >/dev/null 2>&1
        if [ $? = 0 ]
        then
            echo "9548 switch  Succes!" 
            echo "9548 switch  Succes!" >>/tmp/i2c_check.log
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to switch 9548 !" 
            echo "Unable to switch 9548 !" >>/tmp/i2c_check.log
			/etc/init.d/ipmistack start
            exit
        fi
        cnt=$((cnt+1))
		sleep 1
    done
}

#将9548上的所有通路打开
OpenChannel(){
    cnt=1
    while true
    do  
        i2c-test -b 7 -s 0x71 -w -d 0x00 0xff >/dev/null 2>&1
        if [ $? = 0 ]
        then
            echo "All Channel Open Succes!" 
            echo "All Channel Open Succes!" >>/tmp/i2c_check.log
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to access 9548 !" 
            echo "Unable to access 9548 !" >>/tmp/i2c_check.log
			/etc/init.d/ipmistack start
            exit
        fi
        cnt=$((cnt+1))
    done
}

#切换设备
switch(){
    cnt=0
    while true
    do  
        i2c-test -b 7 -s 0x71 -w -d 0x00 $1 >/dev/null 2>&1
        if [ $? = 0 ]
        then
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to switch $1 !" 
            echo "Unable to switch $1 !" >>/tmp/i2c_check.log
            break
        fi
        cnt=$((cnt+1))
    done
}

#查找设备
find(){
    cnt=1
    while true
    do  
        i2c-test --scan -b 7 | grep $1 >/dev/null 2>&1
        if [ $? = 0 ]
        then
            echo "addres:$1 $2 is ready!" 
            echo "addres:$1 $2 is ready!" >>/tmp/i2c_check.log
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to find addres:$1 $2 !" 
            echo "Unable to find addres:$1 $2 !" >>/tmp/i2c_check.log
            break
        fi
        cnt=$((cnt+1))
    done

}

#检查所有设备
check(){
    for addres in 0x01 0x02 0x04 0x08 0x10 0x20 0x40 0x80 #打开9548 channel
    do
        switch $addres
        case $addres in
            0x01)
                find 0x80 "CPLD Config Port(HW)"
                find 0x20 "CPLD Slave Port(SW)"
            ;;
            0x02)
                find 0xc4 "PXE1410CDM-G003"
                find 0x82 "INA220"
            ;;
            0x04)
                find 0x90 "ADS7830"
                find 0x92 "ADS7830"
            ;;
            0x08)
                find 0x90 "TMP112"
            ;;
            0x10)
                find 0xe8 "Si5391"
            ;;
            0x20)
                find 0xa0 "CAT24C64"
            ;;
            0x40)
                find 0x92 "TMP112"
            ;;
            0x80)
                find 0x80 "MPS5023"
                find 0xc0 "PXE1410CDM-G003"
            ;;
        esac
    done
}

cpld_Version="15 09 10 01 "
#init
InitChannel(){
    echo "init ing..."
    i2c-test -b 7 -s 0x71 -w -d 0x01 >/dev/null 2>&1
    echo "init ok"
}

#scan devise
ScanSlave_list(){
    echo "Scan slave list ..."
    i2c-test --scan -b 7 >/dev/null 2>&1
}

#read_check
ReadCheck(){
    i2c-test -b 7 -s 0x10 -m 1 -rc 4 -d 0x3 > 1   #输出到文件1
    sed -n 2P 1   #找到1文件第二行
    if [ "$cpld_Version" = "$(sed -n 2P 1)" ];then
        echo "Check CPLD version success !" 
        echo "Check CPLD version success !" >>/tmp/i2c_check.log
    else 
        echo "error:Check CPLD version error !" 
        echo "error:Check CPLD version error !" >>/tmp/i2c_check.log
    fi
	rm 1
}

rm /tmp/i2c_check.log >/dev/null 2>&1
stopipmi
wdevice
switch9548
num=1
while true
do 
	echo "*********loop$num*********"
	echo "*********loop$num*********" >> /tmp/i2c_check.log
	OpenChannel
	check
	InitChannel
	ScanSlave_list
	ReadCheck
	if [ $num = $1 ]
	then
		break
	fi
		num=$((num+1))
done
/etc/init.d/ipmistack start

2.check.py

#check.py脚本运行在OS下 用于检查DDR SFP CPLD设备。
#会自动打印检查结果,需要在脚本后指定DDR的bus。
#使用方法:python check.py bus。

import os
import sys
import subprocess

# 1.DDR check
DDR_list=['0','1','2','3']
read_count=''
write_count=''
for i in DDR_list:
	var = subprocess.check_output('./fpga_test /sys/bus/pci/devices/0000\\:'+sys.argv[1]+'\\:00.0/resource2 --ddr -n '+i+' -C', shell=True).decode('utf-8')
	fp = open('ddr_test.txt', 'w')
	fp.write(var)
	fp.close()

	fp = open('ddr_test.txt', 'r')
	sample = fp.readlines()
	for line in sample:
		line = line.strip()
		if 'Write count low:' in line:
			write_count = line[16:24]
		line = line.strip()
		if 'Read return count low:' in line:
			read_count = line[22:30]		
	fp.close()
	os.system('rm ddr_test.txt')

	if write_count == read_count:
		print('DDR'+i+' is ok !')
	else:
		print('Please check the DDR'+i+' !')
                     
# 2.SFP check
i2clist={
    '2':'port 1 SFP',
    '3':'port 2 SFP',
    '4':'port 3 SFP',
    '5':'port 4 SFP'
}
index=''
for i in i2clist:
    status=subprocess.check_output('i2cdetect -y '+i,shell=True).decode('utf-8')
    print status
    state=subprocess.check_output('echo $?',shell=True)
    if(state=='0\\n'):
        index=status[321:323]
        if (index == '--'):
            print "device "+i2clist[i]+" not ready!\\n"
        else:
            print "device "+i2clist[i]+" is ready!\\n"
    else:
        print 'i2cdetect err'

# 3.CPLD check
status2=subprocess.check_output('i2cdetect -y 6',shell=True).decode('utf-8')
print status2
state=subprocess.check_output('echo $?',shell=True)
if(state=='0\\n'):
    index=status2[109:111]
    if (index == '--'):
        print "device CPLD not ready!\\n"
    else:
        print "device CPLD is ready!\\n"
else:
    print 'i2cdetect err'

3.read_test.sh

# read_test.sh脚本为BMC下i2c设备轮询访问脚本。
# 当设备出错时会将错误设备信息输出到控制台,无输出即为正常。
#./read_test.sh num 。

#!/bin/bash
#stop ipmistack
stopipmi(){
    cnt=1
    while true
    do  
		/etc/init.d/ipmistack stop >/dev/null 2>&1
        if [ $? = 0 ]
        then
            echo "ipmistack stop  Succes!" 
            echo "ipmistack stop  Succes!" >>/tmp/i2c_check.log
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to stop ipmistack !" 
            echo "Unable to stop ipmistack !" >>/tmp/i2c_check.log
			/etc/init.d/ipmistack start
            exit
        fi
        cnt=$((cnt+1))
		sleep 1
    done
}

#write to device
wdevice(){
    cnt=1
    while true
    do  
        echo 0x73 > /sys/bus/i2c/devices/i2c-7/delete_device 2>&1
        if [ $? = 0 ]
        then
            echo "write 0x73  Succes!" 
            echo "write 0x73  Succes!" >>/tmp/i2c_check.log
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to write 0x73 !" 
            echo "Unable to write 0x73 !" >>/tmp/i2c_check.log
			/etc/init.d/ipmistack start
            exit
        fi
        cnt=$((cnt+1))
		sleep 1
    done
}

#switch to 9548
switch9548(){
    cnt=1
    while true
    do  
		i2c-test -b 7 -s 0x73 -w -d 0x08 >/dev/null 2>&1
        if [ $? = 0 ]
        then
            echo "9548 switch  Succes!" 
            echo "9548 switch  Succes!" >>/tmp/i2c_check.log
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to switch 9548 !" 
            echo "Unable to switch 9548 !" >>/tmp/i2c_check.log
			/etc/init.d/ipmistack start
            exit
        fi
        cnt=$((cnt+1))
		sleep 1
    done
}

#将9548上的所有通路打开
OpenChannel(){
    cnt=1
    while true
    do  
        i2c-test -b 7 -s 0x71 -w -d 0x00 0xff >/dev/null 2>&1
        if [ $? = 0 ]
        then
            echo "All Channel Open Succes!"
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to access 9548 !"
			/etc/init.d/ipmistack start
            exit
        fi
        cnt=$((cnt+1))
    done
}

#切换设备
switch(){
    cnt=0
    while true
    do  
        i2c-test -b 7 -s 0x71 -w -d 0x00 $1 >/dev/null 2>&1
        if [ $? = 0 ]
        then
            break
        fi
        if [ $cnt = 10 ]
        then
            echo "Unable to switch $1 !"
            break
        fi
        cnt=$((cnt+1))
    done
}

#读取设备
read(){
    cnt=0
    while true
    do  
        i2c-test -b 7 -s $1 -m 1 -rc 2 -d 0x0$cnt >/dev/null 2>&1
        if [ $? = 0 ]
        then
            echo "addres:$1 $2 read succes!"
            break
        fi
        if [ $cnt = 9 ]
        then
            echo "Unable to read addres:$1 $2!"
            break
        fi
        cnt=$((cnt+1))
    done
}

#设备轮询读取
check(){
    for addres in 0x01 0x02 0x04 0x08 0x10 0x20 0x40 0x80
    do
        switch $addres
        case $addres in
            0x01)
                read 0x40 "CPLD Config Port(HW)"
                read 0x10 "CPLD Slave Port(SW)"
            ;;
            0x02)
                read 0x62 "PXE1410CDM-G003"
                read 0x41 "INA220"
            ;;
            0x04)
                read 0x48 "ADS7830"
                read 0x49 "ADS7830"
            ;;
            0x08)
                read 0x48 "TMP112"
            ;;
            0x10)
                read 0x74 "Si5391"
            ;;
            0x20)
                read 0x50 "CAT24C64"
            ;;
            0x40)
                read 0x49 "TMP112"
            ;;
            0x80)
                read 0x40 "MPS5023"
                read 0x60 "PXE1410CDM-G003"
            ;;
        esac
    done
}

stopipmi
wdevice
switch9548
num=1
while true
do
	echo "******loop$num**********"
	OpenChannel
	check
	if [ $num = $1 ]
    then
            echo "$1 read tests completed!"
            break
    fi
	num=$((num+1))
done
/etc/init.d/ipmistack start

4.read_test.py

# read_test.py脚本为OS下i2c设备轮询访问脚本,python read_test.py num。

import subprocess
import os
import sys

i2clist={
    '2':'port 1 SFP',
    '3':'port 2 SFP',
    '4':'port 3 SFP',
    '5':'port 4 SFP'
}
print "read  Testing........."
for num in range(0,int(sys.argv[1])):
    for i in i2clist:
        state=os.system('i2cget -y -f '+i+' 0x50 0x00 >/dev/null 2>&1')
        if (state != 0):
            print "Unable to read "+i2clist[i]+" !"
        else:
            print "Read "+i2clist[i]+"Success !"

    state2=os.system('i2cget -y -f 6 0x10 0x03 >/dev/null 2>&1')
    if (state2 != 0):
        print "Unable to read CPLD!"
    else:
        print "Read CPLD Succes !"
print sys.argv[1]+" read tests completed"

以上是关于Notes19检查i2c设备在位和访问的主要内容,如果未能解决你的问题,请参考以下文章

Note2MPS/PXE/ADS/INA电流电压,i2c设备在位和读,samba/nfs,ntp/log/me/树莓派,pip/office,vr,i2ctool,大数据,pam

Linux驱动开发19-I2C子系统之客户驱动分析与移植

markdown PHPExcel Notes和代码片段

RT-Thread 设备驱动I2C浅析及使用

第四季-专题19-I2C驱动程序设计

Linux系统下i2c工具 i2c-tool 的使用以及源码操作i2c设备