嵌入式Linux设备读取CPU温度的方法

时间:2022-05-22 21:06:25

转自http://blog.csdn.net/lu_embedded/article/details/52465308

http://embedded.kleier.selfhost.me/raspberry_cpu_temp.php

http://blog.csdn.net/finewind/article/details/48732581

http://elinux.org/Jetson/Thermal

To observe the temperature changes with small heat capacity the timely resolution must be as high as possible. Therefore the measuring script must be efficient.

How does compiled code compare with interpreted code in terms of speed?

ARM 平台可能是这样:

# cat /sys/devices/virtual/thermal/thermal_zone0/temp
 
 
  • 1
  • 1
# cat /sys/class/thermal/thermal_zone0/temp
 
 
  • 1
  • 1

x86 平台可能是这样:

# cat /proc/acpi/thermal_zone/THRM/temperature
 
 
  • 1
  • 1

为了更方便读取 CPU 温度信息,我们可以编写一些小程序来提高效率。

一、Bash 脚本

cat /proc/uptime > ${OUTFILE}
for ((i=0;i<10000;++i)) ; do
( date "+%s.%3N" ; cat /sys/devices/virtual/thermal/thermal_zone0/temp) |\
awk '{if (NR%2) date = $1; else print date, $1;}'
done >> ${OUTFILE}
cat /proc/uptime >> ${OUTFILE}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

二、C 语言

#define _POSIX_C_SOURCE 199309L // enable interface for struct timespec ...

#include <time.h> // struct timespec, clock_gettime
#include <stdio.h> // FILE, fscanf, stdout, printf

const unsigned N = 10000;

typedef struct
{struct timespec t;
unsigned temp;}
RecT;

int main ()
{RecT r [N];
for (unsigned i = 0; i < N; ++i)
{(void) clock_gettime (CLOCK_REALTIME, &r [i].t);
FILE *fp = fopen ("/sys/devices/virtual/thermal/thermal_zone0/temp", "r");
(void) fscanf (fp, "%u", &r [i].temp);
(void) fclose (fp);}
for (unsigned i = 0; i < N; ++i)
printf ("%lu.%06llu %u\n", r [i].t.tv_sec, (r [i].t.tv_nsec + 500ull) / 1000ull, r [i].temp);
return 0;}


ubuntu方法

http://www.oschina.net/question/54100_11151