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 <tokita.hiroshi@fujitsu.com>
This commit is contained in:
TOKITA Hiroshi 2023-01-08 17:56:35 +09:00 committed by Maureen Helm
commit b70d399a34
7 changed files with 128 additions and 0 deletions

View file

@ -0,0 +1,9 @@
# Copyright (c) 2023 TOKITA Hiroshi
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(die_temp_polling)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,32 @@
.. _die_temp_polling:
CPU Die Temperature polling
###########################
Overview
********
This sample periodically reads temperature from the CPU Die
temperature sensor and display the results.
Building and Running
********************
To run this sample, enable the sensor node that supports ``SENSOR_CHAN_DIE_TEMP``
and create an alias named ``die-temp0`` to link to the node.
The tail ``0`` is the sensor number. This sample support up to 15 sensors.
.. zephyr-app-commands::
:zephyr-app: samples/sensor/die_temp_polling
:board: rpi_pico
:goals: build
:compact:
Sample Output
=============
.. code-block:: console
CPU Die temperature[dietemp]: 22.6 °C
CPU Die temperature[dietemp]: 22.8 °C
CPU Die temperature[dietemp]: 23.1 °C

View file

@ -0,0 +1 @@
CONFIG_ADC=y

View file

@ -0,0 +1,9 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright (c) 2023 TOKITA Hiroshi
*/
&die_temp {
status = "okay";
};

View file

@ -0,0 +1,4 @@
CONFIG_SENSOR=y
CONFIG_ADC=y
CONFIG_PRINTK=y
CONFIG_CBPRINTF_FP_SUPPORT=y

View file

@ -0,0 +1,12 @@
sample:
description: CPU Die temperature polling sample
name: die_temp_polling
tests:
sample.sensor.die_temperature_polling:
tags: sensors tests
filter: dt_alias_exists("die-temp0")
harness: console
harness_config:
type: one_line
regex:
- "CPU Die temperature\\[[A-Za-z0-9_]+\\]: [1-9][0-9].[0-9] °C"

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2023 TOKITA Hiroshi
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/sys/printk.h>
#define DIE_TEMP_ALIAS(i) DT_ALIAS(_CONCAT(die_temp, i))
#define DIE_TEMPERATURE_SENSOR(i, _) \
IF_ENABLED(DT_NODE_EXISTS(DIE_TEMP_ALIAS(i)), (DEVICE_DT_GET(DIE_TEMP_ALIAS(i)),))
/* support up to 16 cpu die temperature sensors */
static const struct device *const sensors[] = {LISTIFY(16, DIE_TEMPERATURE_SENSOR, ())};
static int print_die_temperature(const struct device *dev)
{
struct sensor_value val;
int rc;
/* fetch sensor samples */
rc = sensor_sample_fetch(dev);
if (rc) {
printk("Failed to fetch sample (%d)\n", rc);
return rc;
}
rc = sensor_channel_get(dev, SENSOR_CHAN_DIE_TEMP, &val);
if (rc) {
printk("Failed to get data (%d)\n", rc);
return rc;
}
printk("CPU Die temperature[%s]: %.1f °C\n", dev->name, sensor_value_to_double(&val));
return 0;
}
void main(void)
{
int rc;
for (size_t i = 0; i < ARRAY_SIZE(sensors); i++) {
if (!device_is_ready(sensors[i])) {
printk("sensor: device %s not ready.\n", sensors[i]->name);
return;
}
}
while (1) {
for (size_t i = 0; i < ARRAY_SIZE(sensors); i++) {
rc = print_die_temperature(sensors[i]);
if (rc < 0) {
return;
}
}
k_msleep(300);
}
}