Skip to content

Commit cbc340c

Browse files
gmarullcarlescufi
authored andcommitted
drivers: sensor: add support for MAX6675
Add support for MAX6675 cold-junction-compensated K-thermocouple to digital converter. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 24d9e31 commit cbc340c

File tree

6 files changed

+169
-0
lines changed

6 files changed

+169
-0
lines changed

drivers/sensor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ add_subdirectory_ifdef(CONFIG_LSM9DS0_MFD lsm9ds0_mfd)
5252
add_subdirectory_ifdef(CONFIG_MAX17055 max17055)
5353
add_subdirectory_ifdef(CONFIG_MAX30101 max30101)
5454
add_subdirectory_ifdef(CONFIG_MAX44009 max44009)
55+
add_subdirectory_ifdef(CONFIG_MAX6675 max6675)
5556
add_subdirectory_ifdef(CONFIG_MCP9808 mcp9808)
5657
add_subdirectory_ifdef(CONFIG_MPR mpr)
5758
add_subdirectory_ifdef(CONFIG_MPU6050 mpu6050)

drivers/sensor/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ source "drivers/sensor/max30101/Kconfig"
142142

143143
source "drivers/sensor/max44009/Kconfig"
144144

145+
source "drivers/sensor/max6675/Kconfig"
146+
145147
source "drivers/sensor/mchp_tach_xec/Kconfig"
146148

147149
source "drivers/sensor/mcp9808/Kconfig"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2021 Teslabs Engineering S.L.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
zephyr_library()
5+
zephyr_library_sources_ifdef(CONFIG_MAX6675 max6675.c)

drivers/sensor/max6675/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2021 Teslabs Engineering S.L.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config MAX6675
5+
bool "MAX6675 K-thermocouple to digital converter"
6+
depends on SPI
7+
help
8+
Enable MAX6675 cold-junction-compensated K-thermocouple to digital
9+
converter.

drivers/sensor/max6675/max6675.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2021 Teslabs Engineering S.L.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT maxim_max6675
8+
9+
#include <drivers/sensor.h>
10+
#include <drivers/spi.h>
11+
#include <sys/byteorder.h>
12+
13+
#include <logging/log.h>
14+
LOG_MODULE_REGISTER(max6675, CONFIG_SENSOR_LOG_LEVEL);
15+
16+
/** Thermocouple input bit (goes high if thermocouple is disconnected). */
17+
#define THERMOCOUPLE_INPUT BIT(2)
18+
/** Temperature position. */
19+
#define TEMPERATURE_POS 3U
20+
/** Temperature resolution (cDeg/LSB). */
21+
#define TEMPERATURE_RES 25U
22+
23+
struct max6675_config {
24+
const char *spi_label;
25+
struct spi_config spi_config;
26+
const char *spi_cs_label;
27+
gpio_pin_t spi_cs_pin;
28+
gpio_dt_flags_t spi_cs_flags;
29+
};
30+
31+
struct max6675_data {
32+
const struct device *spi;
33+
struct spi_cs_control cs_ctrl;
34+
uint16_t sample;
35+
};
36+
37+
static int max6675_sample_fetch(const struct device *dev,
38+
enum sensor_channel chan)
39+
{
40+
struct max6675_data *data = dev->data;
41+
const struct max6675_config *config = dev->config;
42+
43+
int ret;
44+
uint8_t buf_rx[2];
45+
const struct spi_buf rx_buf = {
46+
.buf = buf_rx,
47+
.len = ARRAY_SIZE(buf_rx)
48+
};
49+
const struct spi_buf_set rx_bufs = {
50+
.buffers = &rx_buf,
51+
.count = 1U
52+
};
53+
54+
if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_AMBIENT_TEMP) {
55+
return -ENOTSUP;
56+
}
57+
58+
ret = spi_read(data->spi, &config->spi_config, &rx_bufs);
59+
if (ret < 0) {
60+
return ret;
61+
}
62+
63+
data->sample = sys_get_be16(buf_rx);
64+
65+
if (data->sample & THERMOCOUPLE_INPUT) {
66+
LOG_INF("Thermocouple not connected");
67+
return -ENOENT;
68+
}
69+
70+
return 0;
71+
}
72+
73+
static int max6675_channel_get(const struct device *dev, enum sensor_channel chan,
74+
struct sensor_value *val)
75+
{
76+
struct max6675_data *data = dev->data;
77+
78+
int32_t temperature;
79+
80+
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
81+
return -ENOTSUP;
82+
}
83+
84+
temperature = (data->sample >> TEMPERATURE_POS) * TEMPERATURE_RES;
85+
val->val1 = temperature / 100;
86+
val->val2 = (temperature - val->val1 * 100) * 10000;
87+
88+
return 0;
89+
}
90+
91+
static const struct sensor_driver_api max6675_api = {
92+
.sample_fetch = &max6675_sample_fetch,
93+
.channel_get = &max6675_channel_get,
94+
};
95+
96+
static int max6675_init(const struct device *dev)
97+
{
98+
struct max6675_data *data = dev->data;
99+
const struct max6675_config *config = dev->config;
100+
101+
data->spi = device_get_binding(config->spi_label);
102+
if (data->spi == NULL) {
103+
LOG_ERR("Could not get SPI device %s", config->spi_label);
104+
return -ENODEV;
105+
}
106+
107+
data->cs_ctrl.gpio_dev = device_get_binding(config->spi_cs_label);
108+
if (data->cs_ctrl.gpio_dev != NULL) {
109+
data->cs_ctrl.gpio_pin = config->spi_cs_pin;
110+
data->cs_ctrl.gpio_dt_flags = config->spi_cs_flags;
111+
data->cs_ctrl.delay = 0U;
112+
}
113+
114+
return 0;
115+
}
116+
117+
#define MAX6675_INIT(n) \
118+
static struct max6675_data max6675_data_##n; \
119+
\
120+
static const struct max6675_config max6675_config_##n = { \
121+
.spi_label = DT_INST_BUS_LABEL(n), \
122+
.spi_config = { \
123+
.frequency = DT_INST_PROP_OR(n, spi_max_frequency, 0U),\
124+
.operation = SPI_OP_MODE_MASTER | SPI_WORD_SET(8U), \
125+
.slave = DT_INST_REG_ADDR(n), \
126+
.cs = &max6675_data_##n.cs_ctrl, \
127+
}, \
128+
.spi_cs_label = UTIL_AND( \
129+
DT_INST_SPI_DEV_HAS_CS_GPIOS(n), \
130+
DT_INST_SPI_DEV_CS_GPIOS_LABEL(n)), \
131+
.spi_cs_pin = UTIL_AND( \
132+
DT_INST_SPI_DEV_HAS_CS_GPIOS(n), \
133+
DT_INST_SPI_DEV_CS_GPIOS_PIN(n)), \
134+
.spi_cs_flags = UTIL_AND( \
135+
DT_INST_SPI_DEV_HAS_CS_GPIOS(n), \
136+
DT_INST_SPI_DEV_CS_GPIOS_FLAGS(n)), \
137+
}; \
138+
\
139+
DEVICE_DT_INST_DEFINE(n, &max6675_init, device_pm_control_nop, \
140+
&max6675_data_##n, &max6675_config_##n, \
141+
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
142+
&max6675_api);
143+
144+
DT_INST_FOREACH_STATUS_OKAY(MAX6675_INIT)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2021, Teslabs Engineering S.L.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: MAX6675 K-thermocouple to digital converter
5+
6+
compatible: "maxim,max6675"
7+
8+
include: spi-device.yaml

0 commit comments

Comments
 (0)