move things around
This commit is contained in:
8
firmware/drivers/CMakeLists.txt
Normal file
8
firmware/drivers/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# Out-of-tree drivers for custom classes
|
||||
add_subdirectory_ifdef(CONFIG_BLINK blink)
|
||||
|
||||
# Out-of-tree drivers for existing driver classes
|
||||
add_subdirectory_ifdef(CONFIG_SENSOR sensor)
|
7
firmware/drivers/Kconfig
Normal file
7
firmware/drivers/Kconfig
Normal file
@ -0,0 +1,7 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
menu "Drivers"
|
||||
rsource "blink/Kconfig"
|
||||
rsource "sensor/Kconfig"
|
||||
endmenu
|
5
firmware/drivers/blink/CMakeLists.txt
Normal file
5
firmware/drivers/blink/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
# Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
zephyr_library_sources_ifdef(CONFIG_BLINK_GPIO_LED gpio_led.c)
|
23
firmware/drivers/blink/Kconfig
Normal file
23
firmware/drivers/blink/Kconfig
Normal file
@ -0,0 +1,23 @@
|
||||
# Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
menuconfig BLINK
|
||||
bool "Blink device drivers"
|
||||
help
|
||||
This option enables the blink custom driver class.
|
||||
|
||||
if BLINK
|
||||
|
||||
config BLINK_INIT_PRIORITY
|
||||
int "Blink device drivers init priority"
|
||||
default KERNEL_INIT_PRIORITY_DEVICE
|
||||
help
|
||||
Blink device drivers init priority.
|
||||
|
||||
module = BLINK
|
||||
module-str = blink
|
||||
source "subsys/logging/Kconfig.template.log_config"
|
||||
|
||||
rsource "Kconfig.gpio_led"
|
||||
|
||||
endif # BLINK
|
11
firmware/drivers/blink/Kconfig.gpio_led
Normal file
11
firmware/drivers/blink/Kconfig.gpio_led
Normal file
@ -0,0 +1,11 @@
|
||||
# Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config BLINK_GPIO_LED
|
||||
bool "GPIO-controlled LED blink driver"
|
||||
default y
|
||||
depends on DT_HAS_BLINK_GPIO_LED_ENABLED
|
||||
select GPIO
|
||||
help
|
||||
Enable this option to use the GPIO-controlled LED blink driver. This
|
||||
demonstrates how to implement a driver for a custom driver class.
|
101
firmware/drivers/blink/gpio_led.c
Normal file
101
firmware/drivers/blink/gpio_led.c
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT blink_gpio_led
|
||||
|
||||
#include <zephyr/device.h>
|
||||
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include <app/drivers/blink.h>
|
||||
|
||||
LOG_MODULE_REGISTER(blink_gpio_led, CONFIG_BLINK_LOG_LEVEL);
|
||||
|
||||
struct blink_gpio_led_data {
|
||||
struct k_timer timer;
|
||||
};
|
||||
|
||||
struct blink_gpio_led_config {
|
||||
struct gpio_dt_spec led;
|
||||
unsigned int period_ms;
|
||||
};
|
||||
|
||||
static void blink_gpio_led_on_timer_expire(struct k_timer *timer)
|
||||
{
|
||||
const struct device *dev = k_timer_user_data_get(timer);
|
||||
const struct blink_gpio_led_config *config = dev->config;
|
||||
int ret;
|
||||
|
||||
ret = gpio_pin_toggle_dt(&config->led);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Could not toggle LED GPIO (%d)", ret);
|
||||
}
|
||||
}
|
||||
|
||||
static int blink_gpio_led_set_period_ms(const struct device *dev,
|
||||
unsigned int period_ms)
|
||||
{
|
||||
const struct blink_gpio_led_config *config = dev->config;
|
||||
struct blink_gpio_led_data *data = dev->data;
|
||||
|
||||
if (period_ms == 0) {
|
||||
k_timer_stop(&data->timer);
|
||||
return gpio_pin_set_dt(&config->led, 0);
|
||||
}
|
||||
|
||||
k_timer_start(&data->timer, K_MSEC(period_ms), K_MSEC(period_ms));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct blink_driver_api blink_gpio_led_api = {
|
||||
.set_period_ms = &blink_gpio_led_set_period_ms,
|
||||
};
|
||||
|
||||
static int blink_gpio_led_init(const struct device *dev)
|
||||
{
|
||||
const struct blink_gpio_led_config *config = dev->config;
|
||||
struct blink_gpio_led_data *data = dev->data;
|
||||
int ret;
|
||||
|
||||
if (!gpio_is_ready_dt(&config->led)) {
|
||||
LOG_ERR("LED GPIO not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&config->led, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Could not configure LED GPIO (%d)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
k_timer_init(&data->timer, blink_gpio_led_on_timer_expire, NULL);
|
||||
k_timer_user_data_set(&data->timer, (void *)dev);
|
||||
|
||||
if (config->period_ms > 0) {
|
||||
k_timer_start(&data->timer, K_MSEC(config->period_ms),
|
||||
K_MSEC(config->period_ms));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define BLINK_GPIO_LED_DEFINE(inst) \
|
||||
static struct blink_gpio_led_data data##inst; \
|
||||
\
|
||||
static const struct blink_gpio_led_config config##inst = { \
|
||||
.led = GPIO_DT_SPEC_INST_GET(inst, led_gpios), \
|
||||
.period_ms = DT_INST_PROP_OR(inst, blink_period_ms, 0U), \
|
||||
}; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(inst, blink_gpio_led_init, NULL, &data##inst, \
|
||||
&config##inst, POST_KERNEL, \
|
||||
CONFIG_BLINK_INIT_PRIORITY, \
|
||||
&blink_gpio_led_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(BLINK_GPIO_LED_DEFINE)
|
6
firmware/drivers/sensor/CMakeLists.txt
Normal file
6
firmware/drivers/sensor/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
add_subdirectory_ifdef(CONFIG_EXAMPLE_SENSOR example_sensor)
|
||||
add_subdirectory_ifdef(CONFIG_BMA255 bma255)
|
||||
add_subdirectory_ifdef(CONFIG_HDC1080 hdc1080)
|
8
firmware/drivers/sensor/Kconfig
Normal file
8
firmware/drivers/sensor/Kconfig
Normal file
@ -0,0 +1,8 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if SENSOR
|
||||
rsource "example_sensor/Kconfig"
|
||||
rsource "bma255/Kconfig"
|
||||
rsource "hdc1080/Kconfig"
|
||||
endif # SENSOR
|
2
firmware/drivers/sensor/bma255/CMakeLists.txt
Normal file
2
firmware/drivers/sensor/bma255/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
zephyr_library()
|
||||
zephyr_library_sources(bma255.c)
|
17
firmware/drivers/sensor/bma255/Kconfig
Normal file
17
firmware/drivers/sensor/bma255/Kconfig
Normal file
@ -0,0 +1,17 @@
|
||||
config BMA255
|
||||
bool "BMA255"
|
||||
default y
|
||||
depends on DT_HAS_BOSCH_BMA255_ENABLED
|
||||
help
|
||||
Enable driver for BMA255.
|
||||
|
||||
if BMA255
|
||||
|
||||
config BMA255_TRIGGER
|
||||
bool "BMA255 trigger mode"
|
||||
depends on BMA255
|
||||
help
|
||||
Set to enable trigger mode using gpio interrupt, where
|
||||
interrupts are configured to line ALERT PIN.
|
||||
|
||||
endif # BMA255
|
257
firmware/drivers/sensor/bma255/bma255.c
Normal file
257
firmware/drivers/sensor/bma255/bma255.c
Normal file
@ -0,0 +1,257 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Brendan Haines
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT bosch_bma255
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/sys/byteorder.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(bma255, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
// Accelerometer registers
|
||||
const uint8_t ACCEL_REG_BWG_CHIPID = 0x00;
|
||||
const uint8_t ACCEL_REG_ACCD_X_LSB = 0x02;
|
||||
const uint8_t ACCEL_REG_ACCD_X_MSB = 0x03;
|
||||
const uint8_t ACCEL_REG_ACCD_Y_LSB = 0x04;
|
||||
const uint8_t ACCEL_REG_ACCD_Y_MSB = 0x05;
|
||||
const uint8_t ACCEL_REG_ACCD_Z_LSB = 0x06;
|
||||
const uint8_t ACCEL_REG_ACCD_Z_MSB = 0x07;
|
||||
const uint8_t ACCEL_REG_ACCD_TEMP = 0x08;
|
||||
const uint8_t ACCEL_REG_INT_STATUS_0 = 0x09;
|
||||
const uint8_t ACCEL_REG_INT_STATUS_1 = 0x0A;
|
||||
const uint8_t ACCEL_REG_INT_STATUS_2 = 0x0B;
|
||||
const uint8_t ACCEL_REG_INT_STATUS_3 = 0x0C;
|
||||
const uint8_t ACCEL_REG_FIFO_STATUS = 0x0E;
|
||||
const uint8_t ACCEL_REG_PMU_RANGE = 0x0F;
|
||||
typedef enum accel_pmu_range_t
|
||||
{
|
||||
PMU_RANGE_2G = 0b0011, ///< DEFAULT
|
||||
PMU_RANGE_4G = 0b0101,
|
||||
PMU_RANGE_8G = 0b1000,
|
||||
PMU_RANGE_16G = 0b1100,
|
||||
} accel_pmu_range_t;
|
||||
const uint8_t ACCEL_REG_PMU_BW = 0x10;
|
||||
typedef enum accel_pmu_bw_t
|
||||
{
|
||||
PMU_BW_8HZ = 0b01000,
|
||||
PMU_BW_16HZ = 0b01001,
|
||||
PMU_BW_31HZ = 0b01010,
|
||||
PMU_BW_63HZ = 0b01011,
|
||||
PMU_BW_125HZ = 0b01100,
|
||||
PMU_BW_250HZ = 0b01101,
|
||||
PMU_BW_500HZ = 0b01110,
|
||||
PMU_BW_1000HZ = 0b01111,
|
||||
} accel_pmu_bw_t;
|
||||
const uint8_t ACCEL_REG_PMU_LPW = 0x11;
|
||||
const uint8_t ACCEL_REG_PMU_LOW_POWER = 0x12;
|
||||
const uint8_t ACCEL_REG_ACCD_HBW = 0x13;
|
||||
const uint8_t ACCEL_REG_BGW_SOFTRESET = 0x14;
|
||||
const uint8_t BGW_SOFTRESET = 0xB6; ///< Soft reset occurs when this value is written to ACCEL_REG_BGW_SOFTRESET
|
||||
const uint8_t ACCEL_REG_INT_EN_0 = 0x16;
|
||||
const uint8_t ACCEL_REG_INT_EN_1 = 0x17;
|
||||
const uint8_t ACCEL_REG_INT_EN_2 = 0x18;
|
||||
const uint8_t ACCEL_REG_INT_MAP_0 = 0x19;
|
||||
const uint8_t ACCEL_REG_INT_MAP_1 = 0x1A;
|
||||
const uint8_t ACCEL_REG_INT_MAP_2 = 0x1B;
|
||||
const uint8_t ACCEL_REG_INT_SRC = 0x1E;
|
||||
const uint8_t ACCEL_REG_INT_OUT_CTRL = 0x20;
|
||||
const uint8_t ACCEL_REG_INT_RST_LATCH = 0x21;
|
||||
const uint8_t ACCEL_REG_INT_0 = 0x22;
|
||||
const uint8_t ACCEL_REG_INT_1 = 0x23;
|
||||
const uint8_t ACCEL_REG_INT_2 = 0x24;
|
||||
const uint8_t ACCEL_REG_INT_3 = 0x25;
|
||||
const uint8_t ACCEL_REG_INT_4 = 0x26;
|
||||
const uint8_t ACCEL_REG_INT_5 = 0x27;
|
||||
const uint8_t ACCEL_REG_INT_6 = 0x28;
|
||||
const uint8_t ACCEL_REG_INT_7 = 0x29;
|
||||
const uint8_t ACCEL_REG_INT_8 = 0x2A;
|
||||
const uint8_t ACCEL_REG_INT_9 = 0x2B;
|
||||
const uint8_t ACCEL_REG_INT_A = 0x2C;
|
||||
const uint8_t ACCEL_REG_INT_B = 0x2D;
|
||||
const uint8_t ACCEL_REG_INT_C = 0x2E;
|
||||
const uint8_t ACCEL_REG_INT_D = 0x2F;
|
||||
const uint8_t ACCEL_REG_FIFO_CONFIG_0 = 0x30;
|
||||
const uint8_t ACCEL_REG_PMU_SELF_TEST = 0x32;
|
||||
const uint8_t ACCEL_REG_TRIM_NVM_CTRL = 0x33;
|
||||
const uint8_t ACCEL_REG_BGW_SPI3_WDT = 0x34;
|
||||
const uint8_t ACCEL_REG_OFC_CTRL = 0x36;
|
||||
const uint8_t ACCEL_REG_OFC_SETTING = 0x37;
|
||||
const uint8_t ACCEL_REG_OFC_OFFSET_X = 0x38;
|
||||
const uint8_t ACCEL_REG_OFC_OFFSET_Y = 0x39;
|
||||
const uint8_t ACCEL_REG_OFC_OFFSET_Z = 0x3A;
|
||||
const uint8_t ACCEL_REG_TRIM_GP0 = 0x3B;
|
||||
const uint8_t ACCEL_REG_TRIM_GP1 = 0x3C;
|
||||
const uint8_t ACCEL_REG_FIFO_CONFIG_1 = 0x3E;
|
||||
const uint8_t ACCEL_REG_FIFO_DATA = 0x3F;
|
||||
|
||||
struct bma255_data
|
||||
{
|
||||
int16_t accel_x;
|
||||
int16_t accel_y;
|
||||
int16_t accel_z;
|
||||
int8_t temperature;
|
||||
};
|
||||
|
||||
struct bma255_config
|
||||
{
|
||||
struct i2c_dt_spec bus;
|
||||
#ifdef CONFIG_INA230_TRIGGER
|
||||
bool trig_enabled;
|
||||
uint16_t mask;
|
||||
const struct gpio_dt_spec alert_gpio;
|
||||
uint16_t alert_limit;
|
||||
#endif /* CONFIG_INA230_TRIGGER */
|
||||
};
|
||||
|
||||
static int bma255_sample_fetch(const struct device *dev,
|
||||
enum sensor_channel chan)
|
||||
{
|
||||
const struct bma255_config *config = dev->config;
|
||||
struct bma255_data *data = dev->data;
|
||||
int ret;
|
||||
|
||||
uint8_t accel[6];
|
||||
ret = i2c_burst_read_dt(&config->bus, ACCEL_REG_ACCD_X_LSB, accel, sizeof(accel));
|
||||
if (ret < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read acceleration registers!");
|
||||
return ret;
|
||||
}
|
||||
data->accel_x = ((int16_t)sys_get_le16(&accel[0])) >> 4;
|
||||
data->accel_y = ((int16_t)sys_get_le16(&accel[2])) >> 4;
|
||||
data->accel_z = ((int16_t)sys_get_le16(&accel[4])) >> 4;
|
||||
|
||||
ret = i2c_burst_read_dt(&config->bus, ACCEL_REG_ACCD_TEMP, &data->temperature, sizeof(data->temperature));
|
||||
if (ret < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read temperature register!");
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bma255_channel_get(const struct device *dev,
|
||||
enum sensor_channel chan,
|
||||
struct sensor_value *val)
|
||||
{
|
||||
struct bma255_data *data = dev->data;
|
||||
|
||||
switch (chan)
|
||||
{
|
||||
// degrees C
|
||||
case SENSOR_CHAN_DIE_TEMP:
|
||||
// 0.5K/LSB, center temperature is 23C
|
||||
val->val1 = 23 + data->temperature / 2;
|
||||
val->val2 = 0; // TODO: don't throw out LSB
|
||||
break;
|
||||
// m/s^2
|
||||
case SENSOR_CHAN_ACCEL_X:
|
||||
{
|
||||
float accel = data->accel_x * 0.00098 * 9.80665; // to gees, to m/s^2
|
||||
val->val1 = accel;
|
||||
val->val2 = (accel - val->val1) * 1000000;
|
||||
break;
|
||||
}
|
||||
case SENSOR_CHAN_ACCEL_Y:
|
||||
{
|
||||
float accel = data->accel_y * 0.00098 * 9.80665; // to gees, to m/s^2
|
||||
val->val1 = accel;
|
||||
val->val2 = (accel - val->val1) * 1000000;
|
||||
break;
|
||||
}
|
||||
case SENSOR_CHAN_ACCEL_Z:
|
||||
{
|
||||
// For now assume 2g since that's the default value
|
||||
// 2g 0.98mg/LSB
|
||||
// 4g 1.95mg/LSB
|
||||
// 8g 3.91mg/LSB
|
||||
// 16g 7.81mg/LSB
|
||||
// 1 g = 9.80665 m/s^2
|
||||
float accel = data->accel_z * 0.00098 * 9.80665; // to gees, to m/s^2
|
||||
val->val1 = accel;
|
||||
val->val2 = (accel - val->val1) * 1000000;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sensor_driver_api bma255_api = {
|
||||
.sample_fetch = &bma255_sample_fetch,
|
||||
.channel_get = &bma255_channel_get,
|
||||
};
|
||||
|
||||
static int bma255_init(const struct device *dev)
|
||||
{
|
||||
const struct bma255_config *const config = dev->config;
|
||||
int ret;
|
||||
|
||||
if (!device_is_ready(config->bus.bus))
|
||||
{
|
||||
LOG_ERR("I2C bus %s is not ready", config->bus.bus->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
uint8_t chip_id;
|
||||
ret = i2c_burst_read_dt(&config->bus, ACCEL_REG_BWG_CHIPID, &chip_id, sizeof(chip_id));
|
||||
if (ret < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read chip ID register!");
|
||||
return ret;
|
||||
}
|
||||
const uint8_t CHIP_ID = 0xfa;
|
||||
if (chip_id != CHIP_ID)
|
||||
{
|
||||
LOG_ERR("Chip ID read from %s incorrect. Read 0x%02X, expected 0x%02X", dev->name, chip_id, CHIP_ID);
|
||||
}
|
||||
|
||||
// // Reset the sensor
|
||||
// uint8_t reset_val = BGW_SOFTRESET;
|
||||
// i2c_burst_write_dt(&config->bus, ACCEL_REG_BGW_SOFTRESET, &reset_val, sizeof(reset_val));
|
||||
|
||||
// // Wait for device to reset
|
||||
|
||||
uint8_t lpw;
|
||||
ret = i2c_burst_read_dt(&config->bus, ACCEL_REG_PMU_LPW, &lpw, sizeof(lpw));
|
||||
if (ret < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read LPW register!");
|
||||
return ret;
|
||||
}
|
||||
LOG_INF("LPW register: 0x%02X", lpw);
|
||||
|
||||
// Write configuration
|
||||
uint8_t accel_range = PMU_RANGE_2G;
|
||||
i2c_burst_write_dt(&config->bus, ACCEL_REG_PMU_RANGE, &accel_range, sizeof(accel_range));
|
||||
|
||||
uint8_t accel_bw = PMU_BW_8HZ;
|
||||
i2c_burst_write_dt(&config->bus, ACCEL_REG_PMU_BW, &accel_bw, sizeof(accel_bw));
|
||||
|
||||
uint8_t hbw = (1 << 7); // data_high_bw (read filtered data) and enable lsb/msb shadowing
|
||||
i2c_burst_write_dt(&config->bus, ACCEL_REG_ACCD_HBW, &hbw, sizeof(hbw));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define BMA255_INIT(i) \
|
||||
static struct bma255_data bma255_data_##i; \
|
||||
\
|
||||
static const struct bma255_config bma255_config_##i = { \
|
||||
.bus = I2C_DT_SPEC_INST_GET(i), \
|
||||
}; \
|
||||
\
|
||||
SENSOR_DEVICE_DT_INST_DEFINE(i, &bma255_init, NULL, \
|
||||
&bma255_data_##i, \
|
||||
&bma255_config_##i, POST_KERNEL, \
|
||||
CONFIG_SENSOR_INIT_PRIORITY, &bma255_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(BMA255_INIT)
|
5
firmware/drivers/sensor/example_sensor/CMakeLists.txt
Normal file
5
firmware/drivers/sensor/example_sensor/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
zephyr_library_sources(example_sensor.c)
|
10
firmware/drivers/sensor/example_sensor/Kconfig
Normal file
10
firmware/drivers/sensor/example_sensor/Kconfig
Normal file
@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config EXAMPLE_SENSOR
|
||||
bool "Example sensor"
|
||||
default y
|
||||
depends on DT_HAS_ZEPHYR_EXAMPLE_SENSOR_ENABLED
|
||||
select GPIO
|
||||
help
|
||||
Enable example sensor
|
86
firmware/drivers/sensor/example_sensor/example_sensor.c
Normal file
86
firmware/drivers/sensor/example_sensor/example_sensor.c
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT zephyr_example_sensor
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(example_sensor, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
struct example_sensor_data {
|
||||
int state;
|
||||
};
|
||||
|
||||
struct example_sensor_config {
|
||||
struct gpio_dt_spec input;
|
||||
};
|
||||
|
||||
static int example_sensor_sample_fetch(const struct device *dev,
|
||||
enum sensor_channel chan)
|
||||
{
|
||||
const struct example_sensor_config *config = dev->config;
|
||||
struct example_sensor_data *data = dev->data;
|
||||
|
||||
data->state = gpio_pin_get_dt(&config->input);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int example_sensor_channel_get(const struct device *dev,
|
||||
enum sensor_channel chan,
|
||||
struct sensor_value *val)
|
||||
{
|
||||
struct example_sensor_data *data = dev->data;
|
||||
|
||||
if (chan != SENSOR_CHAN_PROX) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
val->val1 = data->state;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sensor_driver_api example_sensor_api = {
|
||||
.sample_fetch = &example_sensor_sample_fetch,
|
||||
.channel_get = &example_sensor_channel_get,
|
||||
};
|
||||
|
||||
static int example_sensor_init(const struct device *dev)
|
||||
{
|
||||
const struct example_sensor_config *config = dev->config;
|
||||
|
||||
int ret;
|
||||
|
||||
if (!device_is_ready(config->input.port)) {
|
||||
LOG_ERR("Input GPIO not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&config->input, GPIO_INPUT);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Could not configure input GPIO (%d)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define EXAMPLE_SENSOR_INIT(i) \
|
||||
static struct example_sensor_data example_sensor_data_##i; \
|
||||
\
|
||||
static const struct example_sensor_config example_sensor_config_##i = {\
|
||||
.input = GPIO_DT_SPEC_INST_GET(i, input_gpios), \
|
||||
}; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(i, example_sensor_init, NULL, \
|
||||
&example_sensor_data_##i, \
|
||||
&example_sensor_config_##i, POST_KERNEL, \
|
||||
CONFIG_SENSOR_INIT_PRIORITY, &example_sensor_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(EXAMPLE_SENSOR_INIT)
|
2
firmware/drivers/sensor/hdc1080/CMakeLists.txt
Normal file
2
firmware/drivers/sensor/hdc1080/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
zephyr_library()
|
||||
zephyr_library_sources(hdc1080.c)
|
6
firmware/drivers/sensor/hdc1080/Kconfig
Normal file
6
firmware/drivers/sensor/hdc1080/Kconfig
Normal file
@ -0,0 +1,6 @@
|
||||
config HDC1080
|
||||
bool "HDC1080"
|
||||
default y
|
||||
depends on DT_HAS_TI_HDC1080_ENABLED
|
||||
help
|
||||
Enable driver for HDC1080.
|
227
firmware/drivers/sensor/hdc1080/hdc1080.c
Normal file
227
firmware/drivers/sensor/hdc1080/hdc1080.c
Normal file
@ -0,0 +1,227 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT ti_hdc1080
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/sys/byteorder.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(hdc1080, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
const uint8_t REG_TEMPERATURE = 0x00;
|
||||
const uint8_t REG_HUMIDITY = 0x01;
|
||||
const uint8_t REG_CONFIGURATION = 0x02;
|
||||
const uint8_t REG_SERIAL_ID_0 = 0xFB;
|
||||
const uint8_t REG_SERIAL_ID_1 = 0xFC;
|
||||
const uint8_t REG_SERIAL_ID_2 = 0xFD;
|
||||
const uint8_t REG_MANUFACTURER_ID = 0xFE;
|
||||
const uint8_t REG_DEVICE_ID = 0xFF;
|
||||
|
||||
#define CONFIG_RST (1 << 15) ///< Software reset. This bit self clears
|
||||
#define CONFIG_HEAT (1 << 13) ///< Enable heater
|
||||
#define CONFIG_MODE_SEQUENTIAL (1 << 12) ///< Temperature and humidity measured in sequence, temperature first
|
||||
#define CONFIG_BTST (1 << 11) ///< 1 = battery voltage < 2.8V
|
||||
#define CONFIG_TRES_14BIT (0 << 10) ///< Temperature resolution
|
||||
#define CONFIG_TRES_11BIT (1 << 10) ///< Temperature resolution
|
||||
#define CONFIG_HRES_14BIT (0b00 << 8) ///< Humidity resolution
|
||||
#define CONFIG_HRES_11BIT (0b01 << 8) ///< Humidity resolution
|
||||
#define CONFIG_HRES_8BIT (0b10 << 8) ///< Humidity resolution
|
||||
|
||||
struct hdc1080_data
|
||||
{
|
||||
int16_t temperature;
|
||||
uint16_t humidity;
|
||||
};
|
||||
|
||||
struct hdc1080_config
|
||||
{
|
||||
struct i2c_dt_spec bus;
|
||||
uint8_t temperature_bits;
|
||||
uint8_t humidity_bits;
|
||||
};
|
||||
|
||||
static int hdc1080_sample_fetch(const struct device *dev,
|
||||
enum sensor_channel chan)
|
||||
{
|
||||
const struct hdc1080_config *config = dev->config;
|
||||
struct hdc1080_data *data = dev->data;
|
||||
int ret;
|
||||
|
||||
// Trigger measurement
|
||||
uint8_t reg_addr = REG_TEMPERATURE;
|
||||
i2c_write_dt(&config->bus, ®_addr, 1);
|
||||
|
||||
// Wait for conversion
|
||||
float temperature_conversion_time; ///< microseconds
|
||||
switch (config->temperature_bits)
|
||||
{
|
||||
case 11:
|
||||
temperature_conversion_time = 3650;
|
||||
break;
|
||||
case 14:
|
||||
temperature_conversion_time = 6350;
|
||||
break;
|
||||
default:
|
||||
temperature_conversion_time = 0;
|
||||
break;
|
||||
}
|
||||
float humidity_conversion_time; ///< microseconds
|
||||
switch (config->humidity_bits)
|
||||
{
|
||||
case 8:
|
||||
humidity_conversion_time = 2500;
|
||||
break;
|
||||
case 11:
|
||||
humidity_conversion_time = 3850;
|
||||
break;
|
||||
case 14:
|
||||
humidity_conversion_time = 6500;
|
||||
break;
|
||||
default:
|
||||
humidity_conversion_time = 0;
|
||||
break;
|
||||
}
|
||||
k_busy_wait(temperature_conversion_time + humidity_conversion_time);
|
||||
|
||||
// Read data
|
||||
uint8_t rdata[4];
|
||||
ret = i2c_read_dt(&config->bus, rdata, sizeof(rdata));
|
||||
if (ret < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read temperature and humidity!");
|
||||
return ret;
|
||||
}
|
||||
data->temperature = sys_get_be16(&rdata[0]);
|
||||
data->humidity = sys_get_be16(&rdata[2]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hdc1080_channel_get(const struct device *dev,
|
||||
enum sensor_channel chan,
|
||||
struct sensor_value *val)
|
||||
{
|
||||
struct hdc1080_data *data = dev->data;
|
||||
|
||||
switch (chan)
|
||||
{
|
||||
case SENSOR_CHAN_AMBIENT_TEMP:
|
||||
{
|
||||
double temperature = data->temperature * 160.0 / (1 << 16) - 40;
|
||||
val->val1 = temperature;
|
||||
val->val2 = (temperature - val->val1) * 1e6;
|
||||
break;
|
||||
}
|
||||
case SENSOR_CHAN_HUMIDITY:
|
||||
{
|
||||
double humidity = data->humidity * 100.0 / (1 << 16);
|
||||
val->val1 = humidity;
|
||||
val->val2 = (humidity - val->val1) * 1e6;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sensor_driver_api hdc1080_api = {
|
||||
.sample_fetch = &hdc1080_sample_fetch,
|
||||
.channel_get = &hdc1080_channel_get,
|
||||
};
|
||||
|
||||
static int hdc1080_init(const struct device *dev)
|
||||
{
|
||||
const struct hdc1080_config *const config = dev->config;
|
||||
int ret;
|
||||
|
||||
if (!device_is_ready(config->bus.bus))
|
||||
{
|
||||
LOG_ERR("I2C bus %s is not ready", config->bus.bus->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
uint8_t id[2];
|
||||
ret = i2c_burst_read_dt(&config->bus, REG_MANUFACTURER_ID, id, sizeof(id));
|
||||
if (ret < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read manufacturer/device ID registers");
|
||||
return ret;
|
||||
}
|
||||
uint16_t manufacturer_id = sys_get_be16(&id[0]);
|
||||
const uint16_t MANUFACTURER_ID = 0x5449;
|
||||
if (manufacturer_id != MANUFACTURER_ID)
|
||||
{
|
||||
LOG_ERR("Manufacturer ID read from %s incorrect. Read 0x%02X, expected 0x%02X", dev->name, manufacturer_id, MANUFACTURER_ID);
|
||||
}
|
||||
|
||||
ret = i2c_burst_read_dt(&config->bus, REG_DEVICE_ID, id, sizeof(id));
|
||||
if (ret < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read manufacturer/device ID registers");
|
||||
return ret;
|
||||
}
|
||||
uint16_t device_id = sys_get_be16(&id[0]);
|
||||
const uint16_t DEVICE_ID = 0x1050;
|
||||
if (device_id != DEVICE_ID)
|
||||
{
|
||||
LOG_ERR("Device ID read from %s incorrect. Read 0x%02X, expected 0x%02X", dev->name, device_id, DEVICE_ID);
|
||||
}
|
||||
|
||||
// Write configuration
|
||||
uint16_t cfg = 0;
|
||||
switch (config->temperature_bits)
|
||||
{
|
||||
case 14:
|
||||
cfg |= CONFIG_TRES_14BIT;
|
||||
break;
|
||||
case 11:
|
||||
cfg |= CONFIG_TRES_11BIT;
|
||||
break;
|
||||
default:
|
||||
LOG_ERR("Invalid temperature resolution: %d bits", config->temperature_bits);
|
||||
return -1;
|
||||
}
|
||||
switch (config->humidity_bits)
|
||||
{
|
||||
case 14:
|
||||
cfg |= CONFIG_HRES_14BIT;
|
||||
break;
|
||||
case 11:
|
||||
cfg |= CONFIG_HRES_11BIT;
|
||||
break;
|
||||
case 8:
|
||||
cfg |= CONFIG_HRES_8BIT;
|
||||
break;
|
||||
default:
|
||||
LOG_ERR("Invalid humidity resolution: %d bits", config->humidity_bits);
|
||||
return -1;
|
||||
}
|
||||
uint8_t wdata[2];
|
||||
sys_put_be16(cfg, wdata);
|
||||
i2c_burst_write_dt(&config->bus, REG_CONFIGURATION, wdata, sizeof(wdata));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define HDC1080_INIT(i) \
|
||||
static struct hdc1080_data hdc1080_data_##i; \
|
||||
\
|
||||
static const struct hdc1080_config hdc1080_config_##i = { \
|
||||
.bus = I2C_DT_SPEC_INST_GET(i), \
|
||||
.temperature_bits = 14, \
|
||||
.humidity_bits = 14, \
|
||||
}; \
|
||||
\
|
||||
SENSOR_DEVICE_DT_INST_DEFINE(i, &hdc1080_init, NULL, \
|
||||
&hdc1080_data_##i, \
|
||||
&hdc1080_config_##i, POST_KERNEL, \
|
||||
CONFIG_SENSOR_INIT_PRIORITY, &hdc1080_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(HDC1080_INIT)
|
Reference in New Issue
Block a user