Skip to content

Commit 56f09e0

Browse files
committed
samples: sensor: Add CPU temperature monitor sample
Add a polling sample for CPU temperature monitor. This sample demonstrates how to data fetch and print to the console. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 5b52b6f commit 56f09e0

File tree

7 files changed

+128
-0
lines changed

7 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2023 TOKITA Hiroshi
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(die_temp_polling)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.. _die_temp_polling:
2+
3+
CPU Die Temperature polling
4+
###########################
5+
6+
Overview
7+
********
8+
9+
This sample periodically reads temperature from the CPU Die
10+
temperature sensor and display the results.
11+
12+
Building and Running
13+
********************
14+
15+
To run this sample, enable the sensor node that supports ``SENSOR_CHAN_DIE_TEMP``
16+
and create an alias named ``die-temp0`` to link to the node.
17+
The tail ``0`` is the sensor number. This sample support up to 15 sensors.
18+
19+
.. zephyr-app-commands::
20+
:zephyr-app: samples/sensor/die_temp_polling
21+
:board: rpi_pico
22+
:goals: build
23+
:compact:
24+
25+
Sample Output
26+
=============
27+
28+
.. code-block:: console
29+
30+
CPU Die temperature[dietemp]: 22.6 °C
31+
CPU Die temperature[dietemp]: 22.8 °C
32+
CPU Die temperature[dietemp]: 23.1 °C
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ADC=y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Copyright (c) 2023 TOKITA Hiroshi
5+
*/
6+
7+
&die_temp {
8+
status = "okay";
9+
};
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_SENSOR=y
2+
CONFIG_ADC=y
3+
CONFIG_PRINTK=y
4+
CONFIG_CBPRINTF_FP_SUPPORT=y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sample:
2+
description: CPU Die temperature polling sample
3+
name: die_temp_polling
4+
tests:
5+
sample.sensor.die_temperature_polling:
6+
tags: sensors tests
7+
filter: dt_alias_exists("die-temp0")
8+
harness: console
9+
harness_config:
10+
type: one_line
11+
regex:
12+
- "CPU Die temperature\\[[A-Za-z0-9_]+\\]: [1-9][0-9].[0-9] °C"
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2023 TOKITA Hiroshi
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/sensor.h>
10+
#include <zephyr/sys/printk.h>
11+
12+
#define DIE_TEMP_ALIAS(i) DT_ALIAS(_CONCAT(die_temp, i))
13+
#define DIE_TEMPERATURE_SENSOR(i, _) \
14+
IF_ENABLED(DT_NODE_EXISTS(DIE_TEMP_ALIAS(i)), (DEVICE_DT_GET(DIE_TEMP_ALIAS(i)),))
15+
16+
/* support up to 16 cpu die temperature sensors */
17+
static const struct device *const sensors[] = {LISTIFY(16, DIE_TEMPERATURE_SENSOR, ())};
18+
19+
static int print_die_temperature(const struct device *dev)
20+
{
21+
struct sensor_value val;
22+
int rc;
23+
24+
/* fetch sensor samples */
25+
rc = sensor_sample_fetch(dev);
26+
if (rc) {
27+
printk("Failed to fetch sample (%d)\n", rc);
28+
return rc;
29+
}
30+
31+
rc = sensor_channel_get(dev, SENSOR_CHAN_DIE_TEMP, &val);
32+
if (rc) {
33+
printk("Failed to get data (%d)\n", rc);
34+
return rc;
35+
}
36+
37+
printk("CPU Die temperature[%s]: %.1f °C\n", dev->name, sensor_value_to_double(&val));
38+
return 0;
39+
}
40+
41+
void main(void)
42+
{
43+
int rc;
44+
45+
for (size_t i = 0; i < ARRAY_SIZE(sensors); i++) {
46+
if (!device_is_ready(sensors[i])) {
47+
printk("sensor: device %s not ready.\n", sensors[i]->name);
48+
return;
49+
}
50+
}
51+
52+
while (1) {
53+
for (size_t i = 0; i < ARRAY_SIZE(sensors); i++) {
54+
rc = print_die_temperature(sensors[i]);
55+
if (rc < 0) {
56+
return;
57+
}
58+
}
59+
k_msleep(300);
60+
}
61+
}

0 commit comments

Comments
 (0)