hello everyone:
I would like to use C language through the internal sensor to obtain the temperature of the cpu,rather than use “cat /sys/devices/virtual/thermal/thermal_zone*/temp ”。Can anyone give me a hint, what API interface can call?
You can use a pseudo file as a normal file.
Here is a quick attempt to do that :
#include <stdio.h> /* for FILE, fopen, fread, fclose, printf and sprintf */
#include <stdlib.h> /* for atof */
int main (int argc, char **argv) {
FILE *filePtr;
char therm_path[64]; /* 64 bytes buffer for building current pseudo file path */
char readBuf[16]; /* 16 bytes buffer for reading ASCII encoded value from pseudo file */
float temp;
for (unsigned int curZone = 0; curZone < 8; curZone++) {
unsigned int readBytes = 0;
sprintf(therm_path, "/sys/devices/virtual/thermal/thermal_zone%u/temp", curZone);
printf ("Reading %s: ", therm_path);
filePtr = fopen(therm_path, "r");
if (!filePtr) {
printf ("Error, failed to open file\n");
continue;
}
while (readBytes < 16) {
int curRead = fread(readBuf + readBytes, 1, 16 - readBytes, filePtr);
if (curRead > 0)
readBytes += curRead;
else
break; /* nothing more to be read */
}
fclose (filePtr);
if (readBytes > 0)
*(readBuf + readBytes - 1) = 0; /* Remove last char '\n' */
printf("[%s] ", readBuf);
temp = atof(readBuf);
temp /= 1000.f;
printf("%.02f\n", temp);
}
exit(0);
}
[EDIT: Output below comes from a TX2]
gcc -o test_thermal test_thermal.cpp
./test_thermal
Reading /sys/devices/virtual/thermal/thermal_zone0/temp: [41000] 41.00
Reading /sys/devices/virtual/thermal/thermal_zone1/temp: [41000] 41.00
Reading /sys/devices/virtual/thermal/thermal_zone2/temp: [47000] 47.00
Reading /sys/devices/virtual/thermal/thermal_zone3/temp: [41000] 41.00
Reading /sys/devices/virtual/thermal/thermal_zone4/temp: [41000] 41.00
Reading /sys/devices/virtual/thermal/thermal_zone5/temp: [41000] 41.00
Reading /sys/devices/virtual/thermal/thermal_zone6/temp: [41250] 41.25
Reading /sys/devices/virtual/thermal/thermal_zone7/temp: [100000] 100.00
Hi Honey_Patouceul:
can you explain this “100” ? thanks.
Reading /sys/devices/virtual/thermal/thermal_zone0/temp [38500] 38.50
Reading /sys/devices/virtual/thermal/thermal_zone1/temp [35500] 35.50
Reading /sys/devices/virtual/thermal/thermal_zone2/temp [31000] 31.00
Reading /sys/devices/virtual/thermal/thermal_zone3/temp [33000] 33.00
Reading /sys/devices/virtual/thermal/thermal_zone4/temp [100000] 100.00
Reading /sys/devices/virtual/thermal/thermal_zone5/temp [35750] 35.75
Reading /sys/devices/virtual/thermal/thermal_zone6/temp [34000] 34.00
Reading /sys/devices/virtual/thermal/thermal_zone7/temp [33250] 33.25
zone0
AO-therm
zone1
CPU-therm
zone2
GPU-therm
zone3
PLL-therm
zone4
PMIC-Die
zone5
Tdiode_tegra
zone6
Tboard_tegra
zone7
thermal-fan-est.38
Not really, but it looks normal for PMIC-Die.
You may check: [url]https://devtalk.nvidia.com/default/topic/957806/jetson-tx1/thermal_zone4-reports-100-degree-celcius-/[/url]
The output log I sent was in fact run on a TX2, sorry for the confusion.
On TX2, there are 9 thermal zones and their order is different:
0: BCPU-therm
1: MCPU-therm
2: GPU-therm
3: PLL-therm
4: AO-therm
5: Tboard_tegra
6: Tdiode_tegra
7: PMIC-Die
8: thermal-fan-est
thank you very much。
AO-therm?can you explain AO-therm ?I find many source,but not
Hi sgh, AO-therm means always-on thermal zone.
Could you please provide the recommended range of each zone?
I don’t really know what is recommended, but at first you may have a look into various trip points in sysfs :
find /sys/devices/virtual/thermal -name 'trip_point*'
i have to guess - the original returned temperature value is reported in units of “milli degrees Celsius”, aka: m°C.
thanks for the proposal. its doing a nice job for what it was written (up to 8 queries from 0 to 7, if existing).
now just for hinting other non-skilled readers and coding beginners:
- i see the issue, that the used buffer might overrun in cases where the unexpected will happen and a “long” answer is received.
- the codes are probably pure c, no c++/cpp needed. (but have not checked it on my own.)