Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for defining the sensor channel in DT #61271

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cmake/modules/header_files.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2023, Google LLC

include_guard(GLOBAL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file ... feels wrong. @tejlmand

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind this is not the final version.
My intention with this is to submit an RFC to test the waters, and see if this approach is acceptable.
I've mentioned that in the comment above, but perhaps I should have been more clear about this.
So yes, it's not particularly pretty. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least I need to document it and probably add set it up so that the header is not regenerated every build.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why this is made into a CMake file, yet alone a CMake module file.

When introducing new Zephyr CMake modules, then those should be written in a re-usable way, which is surely not the case here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaureenHelm we discussed this a bit over a week ago. @kornelduleba and I couldn't figure out a way to make configure_file work for what we needed. This approach is a bit clunky but it does seem to work as long as the formatting is maintained. Though I wonder if as a temporary solution it might make sense to just not use C as the interim.

For context, we want to avoid having a separate constant for devicetree and enum. The options being considered are:

  1. Have duplication (both the #define and enum) but deprecate the enum for 1 release during which we have to make sure any new channels are added to both
  2. Create a common way to generate both from one source.

Personally, due to the complexity we're seeing I'm leaning towards lets just have duplication for 1 release and deprecate the enums.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, due to the complexity we're seeing I'm leaning towards lets just have duplication for 1 release and deprecate the enums.

+1 if it keeps this out of the build system

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will do that.


# Generate headers are used by the base system.
# The logic for generating devicetree_generated.h is kept separately
# in dts.cmake.
#
# Outcome:
# ${BINARY_DIR_INCLUDE_GENERATED}/sensor_channels.h exists
#
# Required variables:
# BINARY_DIR_INCLUDE_GENERATED: where to put generated include files
#
# Optional variables:
# None
#
# Optional environment variables:
# None

set(SENSOR_CHANNELS_INPUT ${ZEPHYR_BASE}/include/zephyr/dt-bindings/sensor/sensor.h)
set(SENSOR_CHANNELS_LINE_REGEX "^#define[ \t\r\n]*SENSOR_CHAN_.*$")
set(SENSOR_CHANNELS_ENUM_REGEX "^#define[ \t\r\n]+(SENSOR_CHAN_[^ \t\r\n]+)[ \t\r\n]+([^ \t\r\n]+).*$")
set(SENSOR_CHANNELS ${BINARY_DIR_INCLUDE_GENERATED}/sensor_channels.h)

# Generate an enum from an array of strings containing macro definitions
# TODO document this
function(zephyr_gen_enum_from_macros name list out)
# TODO Add header guards and "generated by" text
file(WRITE ${out} "enum " ${name} "{\n")
foreach(line ${list})
string(REGEX REPLACE ${SENSOR_CHANNELS_ENUM_REGEX} "\t\\1 = \\2,\n" symbol ${line})
file(APPEND ${out} ${symbol})
endforeach()
file(APPEND ${out} "};")
endfunction()

file(STRINGS ${SENSOR_CHANNELS_INPUT} channels REGEX ${SENSOR_CHANNELS_LINE_REGEX})
zephyr_gen_enum_from_macros("sensor_channel" "${channels}" ${SENSOR_CHANNELS})
1 change: 1 addition & 0 deletions cmake/modules/zephyr_default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ list(APPEND zephyr_cmake_modules "\${pre_dt_board}")

# DTS should be close to kconfig because CONFIG_ variables from
# kconfig and dts should be available at the same time.
list(APPEND zephyr_cmake_modules header_files)
list(APPEND zephyr_cmake_modules dts)
list(APPEND zephyr_cmake_modules kconfig)
list(APPEND zephyr_cmake_modules soc)
Expand Down
157 changes: 3 additions & 154 deletions include/zephyr/drivers/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include <zephyr/sys/iterable_sections.h>
#include <zephyr/types.h>

/* Autogenerated during build */
#include <sensor_channels.h>

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -53,160 +56,6 @@ struct sensor_value {
int32_t val2;
};

/**
* @brief Sensor channels.
*/
enum sensor_channel {
/** Acceleration on the X axis, in m/s^2. */
SENSOR_CHAN_ACCEL_X,
/** Acceleration on the Y axis, in m/s^2. */
SENSOR_CHAN_ACCEL_Y,
/** Acceleration on the Z axis, in m/s^2. */
SENSOR_CHAN_ACCEL_Z,
/** Acceleration on the X, Y and Z axes. */
SENSOR_CHAN_ACCEL_XYZ,
/** Angular velocity around the X axis, in radians/s. */
SENSOR_CHAN_GYRO_X,
/** Angular velocity around the Y axis, in radians/s. */
SENSOR_CHAN_GYRO_Y,
/** Angular velocity around the Z axis, in radians/s. */
SENSOR_CHAN_GYRO_Z,
/** Angular velocity around the X, Y and Z axes. */
SENSOR_CHAN_GYRO_XYZ,
/** Magnetic field on the X axis, in Gauss. */
SENSOR_CHAN_MAGN_X,
/** Magnetic field on the Y axis, in Gauss. */
SENSOR_CHAN_MAGN_Y,
/** Magnetic field on the Z axis, in Gauss. */
SENSOR_CHAN_MAGN_Z,
/** Magnetic field on the X, Y and Z axes. */
SENSOR_CHAN_MAGN_XYZ,
/** Device die temperature in degrees Celsius. */
SENSOR_CHAN_DIE_TEMP,
/** Ambient temperature in degrees Celsius. */
SENSOR_CHAN_AMBIENT_TEMP,
/** Pressure in kilopascal. */
SENSOR_CHAN_PRESS,
/**
* Proximity. Adimensional. A value of 1 indicates that an
* object is close.
*/
SENSOR_CHAN_PROX,
/** Humidity, in percent. */
SENSOR_CHAN_HUMIDITY,
/** Illuminance in visible spectrum, in lux. */
SENSOR_CHAN_LIGHT,
/** Illuminance in infra-red spectrum, in lux. */
SENSOR_CHAN_IR,
/** Illuminance in red spectrum, in lux. */
SENSOR_CHAN_RED,
/** Illuminance in green spectrum, in lux. */
SENSOR_CHAN_GREEN,
/** Illuminance in blue spectrum, in lux. */
SENSOR_CHAN_BLUE,
/** Altitude, in meters */
SENSOR_CHAN_ALTITUDE,

/** 1.0 micro-meters Particulate Matter, in ug/m^3 */
SENSOR_CHAN_PM_1_0,
/** 2.5 micro-meters Particulate Matter, in ug/m^3 */
SENSOR_CHAN_PM_2_5,
/** 10 micro-meters Particulate Matter, in ug/m^3 */
SENSOR_CHAN_PM_10,
/** Distance. From sensor to target, in meters */
SENSOR_CHAN_DISTANCE,

/** CO2 level, in parts per million (ppm) **/
SENSOR_CHAN_CO2,
/** VOC level, in parts per billion (ppb) **/
SENSOR_CHAN_VOC,
/** Gas sensor resistance in ohms. */
SENSOR_CHAN_GAS_RES,

/** Voltage, in volts **/
SENSOR_CHAN_VOLTAGE,

/** Current Shunt Voltage in milli-volts **/
SENSOR_CHAN_VSHUNT,

/** Current, in amps **/
SENSOR_CHAN_CURRENT,
/** Power in watts **/
SENSOR_CHAN_POWER,

/** Resistance , in Ohm **/
SENSOR_CHAN_RESISTANCE,

/** Angular rotation, in degrees */
SENSOR_CHAN_ROTATION,

/** Position change on the X axis, in points. */
SENSOR_CHAN_POS_DX,
/** Position change on the Y axis, in points. */
SENSOR_CHAN_POS_DY,
/** Position change on the Z axis, in points. */
SENSOR_CHAN_POS_DZ,

/** Revolutions per minute, in RPM. */
SENSOR_CHAN_RPM,

/** Voltage, in volts **/
SENSOR_CHAN_GAUGE_VOLTAGE,
/** Average current, in amps **/
SENSOR_CHAN_GAUGE_AVG_CURRENT,
/** Standby current, in amps **/
SENSOR_CHAN_GAUGE_STDBY_CURRENT,
/** Max load current, in amps **/
SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT,
/** Gauge temperature **/
SENSOR_CHAN_GAUGE_TEMP,
/** State of charge measurement in % **/
SENSOR_CHAN_GAUGE_STATE_OF_CHARGE,
/** Full Charge Capacity in mAh **/
SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY,
/** Remaining Charge Capacity in mAh **/
SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY,
/** Nominal Available Capacity in mAh **/
SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY,
/** Full Available Capacity in mAh **/
SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY,
/** Average power in mW **/
SENSOR_CHAN_GAUGE_AVG_POWER,
/** State of health measurement in % **/
SENSOR_CHAN_GAUGE_STATE_OF_HEALTH,
/** Time to empty in minutes **/
SENSOR_CHAN_GAUGE_TIME_TO_EMPTY,
/** Time to full in minutes **/
SENSOR_CHAN_GAUGE_TIME_TO_FULL,
/** Cycle count (total number of charge/discharge cycles) **/
SENSOR_CHAN_GAUGE_CYCLE_COUNT,
/** Design voltage of cell in V (max voltage)*/
SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE,
/** Desired voltage of cell in V (nominal voltage) */
SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE,
/** Desired charging current in mA */
SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT,

/** All channels. */
SENSOR_CHAN_ALL,

/**
* Number of all common sensor channels.
*/
SENSOR_CHAN_COMMON_COUNT,

/**
* This and higher values are sensor specific.
* Refer to the sensor header file.
*/
SENSOR_CHAN_PRIV_START = SENSOR_CHAN_COMMON_COUNT,

/**
* Maximum value describing a sensor channel type.
*/
SENSOR_CHAN_MAX = INT16_MAX,
};

/**
* @brief Sensor trigger types.
*/
Expand Down
152 changes: 152 additions & 0 deletions include/zephyr/dt-bindings/sensor/sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright 2023 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DT_BINDINGS_SENSOR_H_
#define ZEPHYR_INCLUDE_DT_BINDINGS_SENSOR_H_

#include <zephyr/dt-bindings/dt-util.h>

/** Acceleration on the X axis, in m/s^2. */
#define SENSOR_CHAN_ACCEL_X 0
/** Acceleration on the Y axis, in m/s^2. */
#define SENSOR_CHAN_ACCEL_Y 1
/** Acceleration on the Z axis, in m/s^2. */
#define SENSOR_CHAN_ACCEL_Z 2
/** Acceleration on the X, Y and Z axes. */
#define SENSOR_CHAN_ACCEL_XYZ 3
/** Angular velocity around the X axis, in radians/s. */
#define SENSOR_CHAN_GYRO_X 4
/** Angular velocity around the Y axis, in radians/s. */
#define SENSOR_CHAN_GYRO_Y 5
/** Angular velocity around the Z axis, in radians/s. */
#define SENSOR_CHAN_GYRO_Z 6
/** Angular velocity around the X, Y and Z axes. */
#define SENSOR_CHAN_GYRO_XYZ 7
/** Magnetic field on the X axis, in Gauss. */
#define SENSOR_CHAN_MAGN_X 8
/** Magnetic field on the Y axis, in Gauss. */
#define SENSOR_CHAN_MAGN_Y 9
/** Magnetic field on the Z axis, in Gauss. */
#define SENSOR_CHAN_MAGN_Z 10
/** Magnetic field on the X, Y and Z axes. */
#define SENSOR_CHAN_MAGN_XYZ 11
/** Device die temperature in degrees Celsius. */
#define SENSOR_CHAN_DIE_TEMP 12
/** Ambient temperature in degrees Celsius. */
#define SENSOR_CHAN_AMBIENT_TEMP 13
/** Pressure in kilopascal. */
#define SENSOR_CHAN_PRESS 14
/** Proximity. Adimensional. A value of 1 indicates that an object is close. */
#define SENSOR_CHAN_PROX 15
/** Humidity, in percent. */
#define SENSOR_CHAN_HUMIDITY 16
/** Illuminance in visible spectrum, in lux. */
#define SENSOR_CHAN_LIGHT 17
/** Illuminance in infra-red spectrum, in lux. */
#define SENSOR_CHAN_IR 18
/** Illuminance in red spectrum, in lux. */
#define SENSOR_CHAN_RED 19
/** Illuminance in green spectrum, in lux. */
#define SENSOR_CHAN_GREEN 20
/** Illuminance in blue spectrum, in lux. */
#define SENSOR_CHAN_BLUE 21
/** Altitude, in meters */
#define SENSOR_CHAN_ALTITUDE 22

/** 1.0 micro-meters Particulate Matter, in ug/m^3 */
#define SENSOR_CHAN_PM_1_0 23
/** 2.5 micro-meters Particulate Matter, in ug/m^3 */
#define SENSOR_CHAN_PM_2_5 24
/** 10 micro-meters Particulate Matter, in ug/m^3 */
#define SENSOR_CHAN_PM_10 25
/** Distance. From sensor to target, in meters */
#define SENSOR_CHAN_DISTANCE 26

/** CO2 level, in parts per million (ppm) **/
#define SENSOR_CHAN_CO2 27
/** VOC level, in parts per billion (ppb) **/
#define SENSOR_CHAN_VOC 28
/** Gas sensor resistance in ohms. */
#define SENSOR_CHAN_GAS_RES 29

/** Voltage, in volts **/
#define SENSOR_CHAN_VOLTAGE 30

/** Current Shunt Voltage in milli-volts **/
#define SENSOR_CHAN_VSHUNT 31

/** Current, in amps **/
#define SENSOR_CHAN_CURRENT 32
/** Power in watts **/
#define SENSOR_CHAN_POWER 33

/** Resistance , in Ohm **/
#define SENSOR_CHAN_RESISTANCE 34

/** Angular rotation, in degrees */
#define SENSOR_CHAN_ROTATION 35

/** Position change on the X axis, in points. */
#define SENSOR_CHAN_POS_DX 36
/** Position change on the Y axis, in points. */
#define SENSOR_CHAN_POS_DY 37
/** Position change on the Z axis, in points. */
#define SENSOR_CHAN_POS_DZ 38

/** Revolutions per minute, in RPM. */
#define SENSOR_CHAN_RPM 39

/** Voltage, in volts **/
#define SENSOR_CHAN_GAUGE_VOLTAGE 40
/** Average current, in amps **/
#define SENSOR_CHAN_GAUGE_AVG_CURRENT 41
/** Standby current, in amps **/
#define SENSOR_CHAN_GAUGE_STDBY_CURRENT 42
/** Max load current, in amps **/
#define SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT 43
/** Gauge temperature **/
#define SENSOR_CHAN_GAUGE_TEMP 44
/** State of charge measurement in % **/
#define SENSOR_CHAN_GAUGE_STATE_OF_CHARGE 45
/** Full Charge Capacity in mAh **/
#define SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY 46
/** Remaining Charge Capacity in mAh **/
#define SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY 47
/** Nominal Available Capacity in mAh **/
#define SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY 48
/** Full Available Capacity in mAh **/
#define SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY 49
/** Average power in mW **/
#define SENSOR_CHAN_GAUGE_AVG_POWER 50
/** State of health measurement in % **/
#define SENSOR_CHAN_GAUGE_STATE_OF_HEALTH 51
/** Time to empty in minutes **/
#define SENSOR_CHAN_GAUGE_TIME_TO_EMPTY 52
/** Time to full in minutes **/
#define SENSOR_CHAN_GAUGE_TIME_TO_FULL 53
/** Cycle count (total number of charge/discharge cycles) **/
#define SENSOR_CHAN_GAUGE_CYCLE_COUNT 54
/** Design voltage of cell in V (max voltage)*/
#define SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE 55
/** Desired voltage of cell in V (nominal voltage) */
#define SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE 56
/** Desired charging current in mA */
#define SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT 57

/** All channels. */
#define SENSOR_CHAN_ALL 58

/** Number of all common sensor channels. */
#define SENSOR_CHAN_COMMON_COUNT 59

/**
* This and higher values are sensor specific.
* Refer to the sensor header file.
*/
#define SENSOR_CHAN_PRIV_START SENSOR_CHAN_COMMON_COUNT

#define SENSOR_CHAN_MAX INT16_MAX

#endif