Skip to content

Commit 0c4e9d2

Browse files
Delsiangalak
authored andcommitted
samples: sensor: Add stm32_temp sample application
Demonstating usage of STM32 temperature sensor. Signed-off-by: Eug Krashtan <[email protected]>
1 parent 05d798e commit 0c4e9d2

File tree

7 files changed

+138
-0
lines changed

7 files changed

+138
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(stm32_temp_sensor)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.. _stm32_temp_sensor:
2+
3+
STM32 Temperature Sensor
4+
########################
5+
6+
Overview
7+
********
8+
9+
This sample periodically reads temperature from the STM32 Internal
10+
Temperature Sensor and display the results.
11+
12+
Building and Running
13+
********************
14+
15+
In order to run this sample, make sure to enable ``stm32_temp`` node in your
16+
board DT file.
17+
18+
.. zephyr-app-commands::
19+
:zephyr-app: samples/sensor/stm32_temp_sensor
20+
:board: nucleo_f103rb
21+
:goals: build
22+
:compact:
23+
24+
Sample Output
25+
=============
26+
27+
.. code-block:: console
28+
29+
Current temperature: 22.6 °C
30+
Current temperature: 22.8 °C
31+
Current temperature: 23.1 °C
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2021 Eug Krashtan
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Application overlay for creating temperature sensor device instance
7+
*/
8+
9+
/ {
10+
stm-temp {
11+
label = "DIE_TEMP";
12+
compatible = "st,stm32-temp";
13+
io-channels = <&adc1 16>;
14+
status = "okay";
15+
};
16+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2021 Eug Krashtan
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Application overlay for creating temperature sensor device instance
7+
*/
8+
9+
/ {
10+
stm-temp {
11+
label = "DIE_TEMP";
12+
compatible = "st,stm32-temp";
13+
io-channels = <&adc1 16>;
14+
avgslope = <43>;
15+
v25 = <1430>;
16+
ntc;
17+
status = "okay";
18+
};
19+
};
20+
21+
&adc1 {
22+
status = "okay";
23+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_SENSOR=y
2+
CONFIG_ADC=y
3+
CONFIG_STM32_TEMP=y
4+
CONFIG_PRINTK=y
5+
CONFIG_CBPRINTF_FP_SUPPORT=y
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sample:
2+
description: Usage of STM32 temperature sensor
3+
name: stm32_temp_sensor
4+
tests:
5+
sample.sensor.stm32_temp_sensor:
6+
depends_on: adc
7+
tags: sensors
8+
build_only: true
9+
platform_allow: nucleo_f401re stm32f103_mini
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2021 Eug Krashtan
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <device.h>
9+
#include <drivers/sensor.h>
10+
#include <sys/printk.h>
11+
12+
13+
void main(void)
14+
{
15+
struct sensor_value val;
16+
int rc;
17+
const struct device *dev =
18+
DEVICE_DT_GET(DT_INST(0, st_stm32_temp));
19+
20+
if (!device_is_ready(dev)) {
21+
printk("Temperature sensor is not ready\n");
22+
return;
23+
}
24+
25+
printk("STM32 Die temperature sensor test\n");
26+
27+
while (1) {
28+
k_sleep(K_MSEC(300));
29+
30+
/* fetch sensor samples */
31+
rc = sensor_sample_fetch(dev);
32+
if (rc) {
33+
printk("Failed to fetch sample (%d)\n", rc);
34+
continue;
35+
}
36+
37+
rc = sensor_channel_get(dev, SENSOR_CHAN_DIE_TEMP, &val);
38+
if (rc) {
39+
printk("Failed to get data (%d)\n", rc);
40+
continue;
41+
}
42+
43+
printk("Current temperature: %.1f °C\n",
44+
sensor_value_to_double(&val));
45+
}
46+
}

0 commit comments

Comments
 (0)