From b70d399a346d78300240378514c24a038825a27d Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Sun, 8 Jan 2023 17:56:35 +0900 Subject: [PATCH] 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 --- .../sensor/die_temp_polling/CMakeLists.txt | 9 +++ samples/sensor/die_temp_polling/README.rst | 32 ++++++++++ .../die_temp_polling/boards/rpi_pico.conf | 1 + .../die_temp_polling/boards/rpi_pico.overlay | 9 +++ samples/sensor/die_temp_polling/prj.conf | 4 ++ samples/sensor/die_temp_polling/sample.yaml | 12 ++++ samples/sensor/die_temp_polling/src/main.c | 61 +++++++++++++++++++ 7 files changed, 128 insertions(+) create mode 100644 samples/sensor/die_temp_polling/CMakeLists.txt create mode 100644 samples/sensor/die_temp_polling/README.rst create mode 100644 samples/sensor/die_temp_polling/boards/rpi_pico.conf create mode 100644 samples/sensor/die_temp_polling/boards/rpi_pico.overlay create mode 100644 samples/sensor/die_temp_polling/prj.conf create mode 100644 samples/sensor/die_temp_polling/sample.yaml create mode 100644 samples/sensor/die_temp_polling/src/main.c diff --git a/samples/sensor/die_temp_polling/CMakeLists.txt b/samples/sensor/die_temp_polling/CMakeLists.txt new file mode 100644 index 00000000000..618e2dccece --- /dev/null +++ b/samples/sensor/die_temp_polling/CMakeLists.txt @@ -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}) diff --git a/samples/sensor/die_temp_polling/README.rst b/samples/sensor/die_temp_polling/README.rst new file mode 100644 index 00000000000..96bed6062fa --- /dev/null +++ b/samples/sensor/die_temp_polling/README.rst @@ -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 diff --git a/samples/sensor/die_temp_polling/boards/rpi_pico.conf b/samples/sensor/die_temp_polling/boards/rpi_pico.conf new file mode 100644 index 00000000000..488a81dca52 --- /dev/null +++ b/samples/sensor/die_temp_polling/boards/rpi_pico.conf @@ -0,0 +1 @@ +CONFIG_ADC=y diff --git a/samples/sensor/die_temp_polling/boards/rpi_pico.overlay b/samples/sensor/die_temp_polling/boards/rpi_pico.overlay new file mode 100644 index 00000000000..7f355c8b860 --- /dev/null +++ b/samples/sensor/die_temp_polling/boards/rpi_pico.overlay @@ -0,0 +1,9 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2023 TOKITA Hiroshi + */ + +&die_temp { + status = "okay"; +}; diff --git a/samples/sensor/die_temp_polling/prj.conf b/samples/sensor/die_temp_polling/prj.conf new file mode 100644 index 00000000000..e8cc03d24e2 --- /dev/null +++ b/samples/sensor/die_temp_polling/prj.conf @@ -0,0 +1,4 @@ +CONFIG_SENSOR=y +CONFIG_ADC=y +CONFIG_PRINTK=y +CONFIG_CBPRINTF_FP_SUPPORT=y diff --git a/samples/sensor/die_temp_polling/sample.yaml b/samples/sensor/die_temp_polling/sample.yaml new file mode 100644 index 00000000000..2e8829f835e --- /dev/null +++ b/samples/sensor/die_temp_polling/sample.yaml @@ -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" diff --git a/samples/sensor/die_temp_polling/src/main.c b/samples/sensor/die_temp_polling/src/main.c new file mode 100644 index 00000000000..f71a1697947 --- /dev/null +++ b/samples/sensor/die_temp_polling/src/main.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +#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); + } +}