-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_ina3221_sensors.sh
More file actions
executable file
·137 lines (116 loc) · 4.54 KB
/
Copy pathread_ina3221_sensors.sh
File metadata and controls
executable file
·137 lines (116 loc) · 4.54 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# Load custom channel labels if available
CONFIG_FILE="/etc/ina3221-external/ina3221_labels.conf"
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
else
# Default labels if no config file exists
CHANNEL_0_LABEL="CUSTOM_RAIL_1"
CHANNEL_1_LABEL="CUSTOM_RAIL_2"
CHANNEL_2_LABEL="CUSTOM_RAIL_3"
fi
# Script to read both INA3221 sensors on Jetson Orin Nano
echo "=== INA3221 Power Monitor Reading ==="
echo
# Function to read INA3221 with custom labels for external device
read_external_ina3221() {
local hwmon_path="$1"
local device_name="$2"
local address="$3"
echo "--- $device_name (Address: $address) ---"
# Channel 1
local voltage1=$(cat "$hwmon_path/in1_input" 2>/dev/null || echo "N/A")
local current1=$(cat "$hwmon_path/curr1_input" 2>/dev/null || echo "N/A")
echo " Channel 0 ($CHANNEL_0_LABEL):"
echo " Voltage: ${voltage1} mV"
echo " Current: ${current1} mA"
if [ "$voltage1" != "N/A" ] && [ "$current1" != "N/A" ]; then
echo " Power: $((voltage1 * current1 / 1000)) mW"
fi
echo
# Channel 2
local voltage2=$(cat "$hwmon_path/in2_input" 2>/dev/null || echo "N/A")
local current2=$(cat "$hwmon_path/curr2_input" 2>/dev/null || echo "N/A")
echo " Channel 1 ($CHANNEL_1_LABEL):"
echo " Voltage: ${voltage2} mV"
echo " Current: ${current2} mA"
if [ "$voltage2" != "N/A" ] && [ "$current2" != "N/A" ]; then
echo " Power: $((voltage2 * current2 / 1000)) mW"
fi
echo
# Channel 3
local voltage3=$(cat "$hwmon_path/in3_input" 2>/dev/null || echo "N/A")
local current3=$(cat "$hwmon_path/curr3_input" 2>/dev/null || echo "N/A")
echo " Channel 2 ($CHANNEL_2_LABEL):"
echo " Voltage: ${voltage3} mV"
echo " Current: ${current3} mA"
if [ "$voltage3" != "N/A" ] && [ "$current3" != "N/A" ]; then
echo " Power: $((voltage3 * current3 / 1000)) mW"
fi
echo
}
# Function to read built-in INA3221 sensors with system labels
read_builtin_ina3221() {
local hwmon_path="$1"
local device_name="$2"
local address="$3"
echo "--- $device_name (Address: $address) ---"
# Read channels 1-3 (INA3221 has 3 channels)
for ch in 1 2 3; do
local voltage=$(cat "$hwmon_path/in${ch}_input" 2>/dev/null || echo "N/A")
local current=$(cat "$hwmon_path/curr${ch}_input" 2>/dev/null || echo "N/A")
# Try to get label if available
local label=""
if [ -f "$hwmon_path/in${ch}_label" ]; then
label=" ($(cat "$hwmon_path/in${ch}_label" 2>/dev/null))"
fi
echo " Channel $((ch-1))$label:"
if [ "$voltage" != "N/A" ]; then
echo " Voltage: ${voltage} mV"
else
echo " Voltage: N/A"
fi
if [ "$current" != "N/A" ]; then
echo " Current: ${current} mA"
else
echo " Current: N/A"
fi
# Calculate power if both values are available
if [ "$voltage" != "N/A" ] && [ "$current" != "N/A" ] && [ "$voltage" -ne 0 ] && [ "$current" -ne 0 ]; then
local power=$((voltage * current / 1000))
echo " Power: ${power} mW"
fi
echo
done
}
# Find INA3221 devices
echo "Scanning for INA3221 devices..."
echo
# Check original INA3221 (usually hwmon3)
original_found=false
second_found=false
for hwmon in /sys/class/hwmon/hwmon*; do
if [ -f "$hwmon/name" ] && [ "$(cat $hwmon/name)" = "ina3221" ]; then
# Try to determine which one this is by checking the device path
device_link=$(readlink -f $hwmon/device 2>/dev/null)
if [[ "$device_link" == *"1-0040"* ]]; then
read_builtin_ina3221 "$hwmon" "Built-in INA3221" "0x40"
original_found=true
elif [[ "$device_link" == *"1-0041"* ]]; then
read_external_ina3221 "$hwmon" "External INA3221" "0x41"
second_found=true
else
# Fallback: just read it as unknown
addr=$(basename "$device_link" | cut -d'-' -f2)
read_builtin_ina3221 "$hwmon" "INA3221" "0x$addr"
fi
fi
done
echo "=== Summary ==="
echo "Original INA3221 (0x40): $([ "$original_found" = true ] && echo "Found" || echo "Not found")"
echo "Second INA3221 (0x41): $([ "$second_found" = true ] && echo "Found" || echo "Not found")"
if [ "$second_found" = false ]; then
echo
echo "To manually create the second INA3221 device:"
echo "sudo echo ina3221 0x41 > /sys/bus/i2c/devices/i2c-1/new_device"
fi