Home Shell Records - Si5341-I2C配置
Post
Cancel

Shell Records - Si5341-I2C配置

在命令行使用i2c-tools配置Si5341,将配置写入文件读取。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh

I2C_BUS_No=0
I2C_DEV_Si5341_ADDR=0x72
Si5341_PAGE_No=0      # Si5341寄存器关键信息页

cmds=()              # 将命令作为数组形式
num_cmds=0

function config_i2c_hub(){
# i2cdetect -r -y ${I2C_BUS_No}
# 打开i2c hub 通道2 ,i2c 总线上可以扫描到目标设备
#(Si5341挂在i2c hub后,需要先打开hub通道)
i2cset -y ${I2C_BUS_No} 0x74 0x02 
}

# ================================================================================


# ["wrreg" "page" "u8addr" "u8var"]
# ["delay" "time_ms"]
function line_to_cmd(){
    echo $LINE | grep delay 1>/dev/null
    check_delay=`echo $?`
    if [[ $check_delay == "0" ]];then
        # echo "seen as delay cmd"
        echo "delay ${LINE##* }"
    else
        # echo "seen as wrreg cmd"
        substr1=${LINE#*0x}
        regaddr=${substr1:0:4}
        substr2=${substr1#*0x}
        val=${substr2:0:2}
        # echo "substr1 :$substr1"
        # echo "substr2 :$substr2"
        # echo "regaddr :$regaddr"
        # echo "val :$val"
        
        echo "wrreg 0x${regaddr:0:2} 0x${regaddr:2:2} 0x${val}"
    fi
}


function traverse_file_to_bottm(){
    while read LINE
    do
        if [[ $LINE == "" ]];then
            continue
        fi
        # echo "This line is : $LINE"
        cmds[${num_cmds}]=`line_to_cmd $LINE`
        num_cmds=$((num_cmds+1))
    done  < ${1}
}


function Si5341_read_reg(){
    i2cget -y ${I2C_BUS_No} ${I2C_DEV_Si5341_ADDR} $1
}

function Si5341_write_reg(){
    dst_page_No=${1}
    reg_addr=${2}
    val=${3}

    # echo "dst_page_No is ${dst_page_No}"
    # echo "reg_addr is ${reg_addr}"
    # echo "val is ${val}"
    if [[ $dst_page_No != $Si5341_PAGE_No ]];then
        i2cset -y ${I2C_BUS_No} ${I2C_DEV_Si5341_ADDR} 0x1 ${dst_page_No}
        echo "chang page to ${dst_page_No}"
        Si5341_PAGE_No=$dst_page_No
    fi

    i2cset -y ${I2C_BUS_No} ${I2C_DEV_Si5341_ADDR} ${reg_addr} ${val}
}


function exec_cmd(){
    if [[ $1 == "wrreg" ]];then
        # echo "wrreg cmd"
        Si5341_write_reg $2 $3 $4
    elif [[ $1 == "delay" ]];then
        # echo "delay cmd"
        sleep 1
    fi
}

# main()
config_i2c_hub
Si5341_PAGE_No=`Si5341_read_reg 0x01`
echo "current Si5341_PAGE_No: $Si5341_PAGE_No"


traverse_file_to_bottm $1

for((i=0;i<num_cmds;i++))
do
    # echo "cmds[${i}] is ${cmds[$i]}"
    exec_cmd ${cmds[$i]}
done

参考的配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{ 0x0B24, 0xC0 },
{ 0x0B25, 0x00 },
{ 0x0502, 0x01 },
{ 0x0505, 0x03 },
{ 0x0957, 0x17 },
{ 0x0B4E, 0x1A },
delay 300
{ 0x0006, 0x00 },
{ 0x0007, 0x00 },
{ 0x0008, 0x00 },
{ 0x000B, 0x74 },
{ 0x0017, 0xD0 },
{ 0x0018, 0xFF },
{ 0x0021, 0x0F },
{ 0x0022, 0x00 },
{ 0x002B, 0x02 },
{ 0x002C, 0x20 },
{ 0x002D, 0x00 },
{ 0x002E, 0x00 },
{ 0x002F, 0x00 },
{ 0x0030, 0x00 },
{ 0x0031, 0x00 },
{ 0x0032, 0x00 },
{ 0x0033, 0x00 },
...
...
...
{ 0x0B57, 0x0E },
{ 0x0B58, 0x01 },
{ 0x001C, 0x01 },
{ 0x0B24, 0xC3 },
{ 0x0B25, 0x02 },
This post is licensed under CC BY 4.0 by the author.