Skip to content

Commit 46d9b14

Browse files
Feat: Add driver for TMP102 temperature sensor for Skynode-N (#26241)
* feat: added driver for tmp102 temperature sensor * style: removed new line * style: adjusted date in header * style: removed duplicated logging * fix: moved start-up command from rc.board_sensors to rc.sensors * style: used consexpr for expected config reg value * feat: added retry logic to probe function * style: added _ as prefix to global variable * style: used make format * fix: corrected temperature calculation * fix: mask AL-bit in probe function * style: removed header files from CMakeLists * style: used correct english in comments * refactor: return error right after failure * style: moved init call to correct place * fix: corrected temperature calculation (again) * refactor: removed _curr_pr variable => always have to set PR to desired register on read * fix: add multi logged topic
1 parent 66e2149 commit 46d9b14

File tree

9 files changed

+365
-1
lines changed

9 files changed

+365
-1
lines changed

ROMFS/px4fmu_common/init.d/rc.sensors

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ then
237237
tla2528 start -X
238238
fi
239239

240+
# Start TMP102 temperature sensor
241+
if param compare SENS_EN_TMP102 1
242+
then
243+
tmp102 start -X
244+
fi
245+
240246
# probe for optional external I2C devices
241247
if param compare SENS_EXT_I2C_PRB 1
242248
then

src/drivers/drv_sensor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267

268268

269269
#define DRV_TEMP_DEVTYPE_MCP9808 0xEE
270+
#define DRV_TEMP_DEVTYPE_TMP102 0xF0
270271

271272
#define DRV_DEVTYPE_UNUSED 0xff
272273

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
############################################################################
2+
#
3+
# Copyright (c) 2026 PX4 Development Team. All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions
7+
# are met:
8+
#
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in
13+
# the documentation and/or other materials provided with the
14+
# distribution.
15+
# 3. Neither the name PX4 nor the names of its contributors may be
16+
# used to endorse or promote products derived from this software
17+
# without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
# POSSIBILITY OF SUCH DAMAGE.
31+
#
32+
############################################################################
33+
34+
px4_add_module(
35+
MODULE drivers__temperature_sensor__tmp102
36+
MAIN tmp102
37+
COMPILE_FLAGS
38+
SRCS
39+
tmp102_main.cpp
40+
tmp102.cpp
41+
MODULE_CONFIG
42+
module.yaml
43+
DEPENDS
44+
px4_work_queue
45+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
menuconfig DRIVERS_TEMPERATURE_SENSOR_TMP102
2+
bool "TMP102 temperature sensor"
3+
default n
4+
---help---
5+
Enable support for the TMP102 temperature sensor
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
__max_num_config_instances: &max_num_config_instances 1
2+
3+
module_name: TMP102
4+
5+
parameters:
6+
- group: Sensors
7+
definitions:
8+
SENS_EN_TMP102:
9+
description:
10+
short: Enable TMP102
11+
long: |
12+
Enable the driver for the TMP102 temperature sensor
13+
type: boolean
14+
reboot_required: true
15+
default: 0
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2026 PX4 Development Team. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
* 3. Neither the name PX4 nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
****************************************************************************/
33+
34+
#include "tmp102.h"
35+
36+
TMP102::TMP102(const I2CSPIDriverConfig &config) :
37+
I2C(config),
38+
I2CSPIDriver(config),
39+
_cycle_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": single-sample")),
40+
_comms_errors(perf_alloc(PC_COUNT, MODULE_NAME": comms errors"))
41+
{
42+
}
43+
44+
TMP102::~TMP102()
45+
{
46+
ScheduleClear();
47+
perf_free(_cycle_perf);
48+
perf_free(_comms_errors);
49+
}
50+
51+
void TMP102::RunImpl()
52+
{
53+
if (should_exit()) {
54+
exit_and_cleanup();
55+
return;
56+
}
57+
58+
perf_begin(_cycle_perf);
59+
60+
float temperature = read_temperature();
61+
62+
if (std::isnan(temperature)) {
63+
perf_count(_comms_errors);
64+
65+
} else {
66+
sensor_temp_s _sensor_temp{};
67+
_sensor_temp.timestamp = hrt_absolute_time();
68+
_sensor_temp.temperature = temperature;
69+
_sensor_temp.device_id = get_device_id();
70+
_sensor_temp_pub.publish(_sensor_temp);
71+
}
72+
73+
perf_end(_cycle_perf);
74+
}
75+
76+
int TMP102::probe()
77+
{
78+
uint16_t conf_reg;
79+
80+
for (int i = 0; i < 3; i++) {
81+
if (read_reg(TMP102_CONFIG_REG, conf_reg) == PX4_OK && (conf_reg | 0x0020) == DEFAULT_CONFIG_REG) { // Mask the AL bit
82+
return PX4_OK;
83+
}
84+
85+
px4_sleep(1);
86+
}
87+
88+
return PX4_ERROR;
89+
}
90+
91+
int TMP102::init()
92+
{
93+
int ret = I2C::init();
94+
95+
if (ret != PX4_OK) {
96+
PX4_ERR("TMP102, I2C init failed");
97+
return ret;
98+
}
99+
100+
_sensor_temp_pub.advertise();
101+
ScheduleOnInterval(250_ms); // DEFAULT SAMPLE RATE IS 4HZ => 250ms INTERVAL
102+
return PX4_OK;
103+
}
104+
105+
float TMP102::read_temperature()
106+
{
107+
uint16_t tmp_data;
108+
109+
if (read_reg(TMP102_TEMP_REG, tmp_data) != PX4_OK) {
110+
return NAN;
111+
}
112+
113+
float temperature = ((int16_t)(tmp_data) >> 4) * 0.0625f;
114+
return temperature;
115+
}
116+
117+
int TMP102::read_reg(uint8_t reg, uint16_t &data)
118+
{
119+
uint8_t tmp_data[2];
120+
tmp_data[0] = reg;
121+
122+
if (transfer(tmp_data, 1, nullptr, 0) != PX4_OK) { // Set the PR to the desired register
123+
return PX4_ERROR;
124+
}
125+
126+
int ret = transfer(nullptr, 0, tmp_data, 2); // Read the data from the desired register
127+
data = (tmp_data[0] << 8) | tmp_data[1];
128+
return ret;
129+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2026 PX4 Development Team. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
* 3. Neither the name PX4 nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
****************************************************************************/
33+
#pragma once
34+
35+
#include <stdint.h>
36+
#include <drivers/device/i2c.h>
37+
#include <px4_platform_common/i2c_spi_buses.h>
38+
#include <uORB/topics/sensor_temp.h>
39+
#include <uORB/PublicationMulti.hpp>
40+
#include <lib/perf/perf_counter.h>
41+
#include <drivers/drv_hrt.h>
42+
43+
using namespace time_literals;
44+
45+
#define TMP102_TEMP_REG 0x00
46+
#define TMP102_CONFIG_REG 0x01
47+
#define TMP102_TLOW_REG 0x02
48+
#define TMP102_THIGH_REG 0x03
49+
50+
constexpr uint16_t DEFAULT_CONFIG_REG = 0x60A0; // 12-bit resolution, comparator mode, active low, 4Hz
51+
52+
class TMP102 : public device::I2C, public I2CSPIDriver<TMP102>
53+
{
54+
public:
55+
TMP102(const I2CSPIDriverConfig &config);
56+
~TMP102() override;
57+
58+
int init() override;
59+
int probe() override;
60+
void RunImpl();
61+
static void print_usage();
62+
63+
protected:
64+
void print_status();
65+
66+
private:
67+
uORB::PublicationMulti<sensor_temp_s> _sensor_temp_pub{ORB_ID(sensor_temp)};
68+
perf_counter_t _cycle_perf;
69+
perf_counter_t _comms_errors;
70+
71+
float read_temperature();
72+
int read_reg(uint8_t address, uint16_t &data);
73+
};
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2026 PX4 Development Team. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
* 3. Neither the name PX4 nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
****************************************************************************/
33+
34+
/**
35+
* @file tmp102_main.cpp
36+
* @author Philipp Engljaehringer
37+
*
38+
* Driver for the TMP102 Temperature Sensor connected via I2C.
39+
*/
40+
41+
#include "tmp102.h"
42+
#include <px4_platform_common/module.h>
43+
44+
void TMP102::print_usage()
45+
{
46+
PRINT_MODULE_USAGE_NAME("tmp102", "driver");
47+
PRINT_MODULE_USAGE_COMMAND("start");
48+
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
49+
PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x48);
50+
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
51+
}
52+
53+
void TMP102::print_status()
54+
{
55+
I2CSPIDriverBase::print_status();
56+
perf_print_counter(_cycle_perf);
57+
perf_print_counter(_comms_errors);
58+
}
59+
60+
extern "C" int tmp102_main(int argc, char *argv[])
61+
{
62+
using ThisDriver = TMP102;
63+
BusCLIArguments cli{true, false};
64+
cli.default_i2c_frequency = 400000;
65+
cli.i2c_address = 0x48;
66+
67+
const char *verb = cli.parseDefaultArguments(argc, argv);
68+
69+
if (!verb) {
70+
ThisDriver::print_usage();
71+
return -1;
72+
}
73+
74+
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_TEMP_DEVTYPE_TMP102);
75+
76+
if (!strcmp(verb, "start")) {
77+
return ThisDriver::module_start(cli, iterator);
78+
}
79+
80+
if (!strcmp(verb, "stop")) {
81+
return ThisDriver::module_stop(iterator);
82+
}
83+
84+
if (!strcmp(verb, "status")) {
85+
return ThisDriver::module_status(iterator);
86+
}
87+
88+
ThisDriver::print_usage();
89+
return -1;
90+
}

src/modules/logger/logged_topics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ void LoggedTopics::add_default_topics()
125125
add_optional_topic("sensor_gyro_fft", 50);
126126
add_topic("sensor_selection");
127127
add_topic("sensors_status_imu", 200);
128-
add_optional_topic("sensor_temp", 100);
129128
add_optional_topic("spoilers_setpoint", 1000);
130129
add_topic("system_power", 500);
131130
add_optional_topic("takeoff_status", 1000);
@@ -167,6 +166,7 @@ void LoggedTopics::add_default_topics()
167166
add_optional_topic_multi("control_allocator_status", 200, 2);
168167
add_optional_topic_multi("rate_ctrl_status", 200, 2);
169168
add_optional_topic_multi("sensor_hygrometer", 500, 4);
169+
add_optional_topic_multi("sensor_temp", 100, 4);
170170
add_optional_topic_multi("rpm", 200);
171171
add_topic_multi("timesync_status", 1000, 3);
172172
add_optional_topic_multi("telemetry_status", 1000, 4);

0 commit comments

Comments
 (0)