-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
kornelduleba
wants to merge
5
commits into
zephyrproject-rtos:main
from
kornelduleba:sensor-channel-DT
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c52b509
sensor: Expose sensor channels to DT
kornelduleba acc8d3c
sensor: Add support for defining sensor channel in DT
kornelduleba d597244
tests: lib: devicetree: Implement tests for DT sensor helpers
kornelduleba 00dcc2c
sensor: Add support for declaring supported channels
kornelduleba 7737af0
sensor_shell: Print channels supported by a sensor
kornelduleba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
# 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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @nordicjm
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
Personally, due to the complexity we're seeing I'm leaning towards lets just have duplication for 1 release and deprecate the enums.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 if it keeps this out of the build system
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, will do that.