|
| 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) |
0 commit comments