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:
parent
10ef1a7cba
commit
b70d399a34
7 changed files with 128 additions and 0 deletions
9
samples/sensor/die_temp_polling/CMakeLists.txt
Normal file
9
samples/sensor/die_temp_polling/CMakeLists.txt
Normal 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})
|
32
samples/sensor/die_temp_polling/README.rst
Normal file
32
samples/sensor/die_temp_polling/README.rst
Normal 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
|
1
samples/sensor/die_temp_polling/boards/rpi_pico.conf
Normal file
1
samples/sensor/die_temp_polling/boards/rpi_pico.conf
Normal file
|
@ -0,0 +1 @@
|
|||
CONFIG_ADC=y
|
9
samples/sensor/die_temp_polling/boards/rpi_pico.overlay
Normal file
9
samples/sensor/die_temp_polling/boards/rpi_pico.overlay
Normal file
|
@ -0,0 +1,9 @@
|
|||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Copyright (c) 2023 TOKITA Hiroshi
|
||||
*/
|
||||
|
||||
&die_temp {
|
||||
status = "okay";
|
||||
};
|
4
samples/sensor/die_temp_polling/prj.conf
Normal file
4
samples/sensor/die_temp_polling/prj.conf
Normal file
|
@ -0,0 +1,4 @@
|
|||
CONFIG_SENSOR=y
|
||||
CONFIG_ADC=y
|
||||
CONFIG_PRINTK=y
|
||||
CONFIG_CBPRINTF_FP_SUPPORT=y
|
12
samples/sensor/die_temp_polling/sample.yaml
Normal file
12
samples/sensor/die_temp_polling/sample.yaml
Normal 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"
|
61
samples/sensor/die_temp_polling/src/main.c
Normal file
61
samples/sensor/die_temp_polling/src/main.c
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue