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

sensor: sensing: unified api #64478

Merged
merged 15 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions dts/bindings/sensor/zephyr,sensing-hinge-angle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023, Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

description: Sensing subsystem hinge angle sensor bindings.

compatible: "zephyr,sensing-hinge-angle"

# Common sensor subsystem sensor properties.
include: ["zephyr,sensing-sensor.yaml"]
12 changes: 10 additions & 2 deletions dts/bindings/sensor/zephyr,sensing-sensor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ description: Sensing subsystem sensor common properties bindings.
include: sensor-device.yaml

properties:
sensor-type:
type: int
sensor-types:
type: array
required: true
description: sensor type id (follow HID spec definition)

Expand All @@ -23,3 +23,11 @@ properties:
reporters:
type: phandles
description: sensor reporters

reporters-index:
type: array
description: the index in sensor-types of reporter if the reporter support multiple sensor-types

stream-mode:
type: boolean
description: sensor works on stream mode or poll mode
40 changes: 40 additions & 0 deletions include/zephyr/drivers/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,46 @@ static inline int64_t sensor_value_to_micro(const struct sensor_value *val)
return ((int64_t)val->val1 * 1000000) + val->val2;
}

/**
* @brief Helper function for converting integer milli units to struct sensor_value.
*
* @param val A pointer to a sensor_value struct.
* @param milli The converted value.
* @return 0 if successful, negative errno code if failure.
*/
static inline int sensor_value_from_milli(struct sensor_value *val, int64_t milli)
{
if (milli < ((int64_t)INT32_MIN - 1) * 1000LL ||
milli > ((int64_t)INT32_MAX + 1) * 1000LL) {
return -ERANGE;
}

val->val1 = (int32_t)(milli / 1000);
val->val2 = (int32_t)(milli % 1000) * 1000;

return 0;
}

/**
* @brief Helper function for converting integer micro units to struct sensor_value.
*
* @param val A pointer to a sensor_value struct.
* @param micro The converted value.
* @return 0 if successful, negative errno code if failure.
*/
static inline int sensor_value_from_micro(struct sensor_value *val, int64_t micro)
{
if (micro < ((int64_t)INT32_MIN - 1) * 1000000LL ||
micro > ((int64_t)INT32_MAX + 1) * 1000000LL) {
return -ERANGE;
}

val->val1 = (int32_t)(micro / 1000000LL);
val->val2 = (int32_t)(micro % 1000000LL);

return 0;
}

/**
* @}
*/
Expand Down
22 changes: 17 additions & 5 deletions include/zephyr/sensing/sensing.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ struct sensing_sensor_version {
*/
#define SENSING_SENSOR_FLAG_REPORT_ON_CHANGE BIT(1)

/**
* @brief SENSING_SENSITIVITY_INDEX_ALL indicating sensitivity of each data field should be set
*
*/
#define SENSING_SENSITIVITY_INDEX_ALL -1

/**
* @brief Sensing subsystem sensor state.
Expand Down Expand Up @@ -110,7 +115,8 @@ typedef void *sensing_sensor_handle_t;
*/
typedef void (*sensing_data_event_t)(
sensing_sensor_handle_t handle,
const void *buf);
const void *buf,
void *context);
Copy link
Collaborator

Choose a reason for hiding this comment

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

needs doxygen comment

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks. Submitted #68317 to fix it.


/**
* @struct sensing_sensor_info
Expand Down Expand Up @@ -144,6 +150,7 @@ struct sensing_sensor_info {
*/
struct sensing_callback_list {
sensing_data_event_t on_data_event;
void *context;
Comment on lines 152 to +153
Copy link
Collaborator

Choose a reason for hiding this comment

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

need doxygen comment

};
/**
* @struct sensing_sensor_config
Expand All @@ -152,7 +159,10 @@ struct sensing_callback_list {
*/
struct sensing_sensor_config {
enum sensing_sensor_attribute attri;

/** \ref SENSING_SENSITIVITY_INDEX_ALL */
int8_t data_field;

union {
uint32_t interval;
uint32_t sensitivity;
Expand Down Expand Up @@ -186,15 +196,16 @@ int sensing_get_sensors(int *num_sensors, const struct sensing_sensor_info **inf
*
* @param info The sensor info got from \ref sensing_get_sensors
*
* @param cb_list callback list to be registered to sensing.
* @param cb_list callback list to be registered to sensing, must have a static
* lifetime.
*
* @param handle The opened instance handle, if failed will be set to NULL.
*
* @return 0 on success or negative error value on failure.
*/
int sensing_open_sensor(
const struct sensing_sensor_info *info,
const struct sensing_callback_list *cb_list,
struct sensing_callback_list *cb_list,
sensing_sensor_handle_t *handle);

/**
Expand All @@ -207,14 +218,15 @@ int sensing_open_sensor(
*
* @param dev pointer device get from device tree.
*
* @param cb_list callback list to be registered to sensing.
* @param cb_list callback list to be registered to sensing, must have a static
* lifetime.
*
* @param handle The opened instance handle, if failed will be set to NULL.
*
* @return 0 on success or negative error value on failure.
*/
int sensing_open_sensor_by_dt(
const struct device *dev, const struct sensing_callback_list *cb_list,
const struct device *dev, struct sensing_callback_list *cb_list,
sensing_sensor_handle_t *handle);

/**
Expand Down
2 changes: 1 addition & 1 deletion include/zephyr/sensing/sensing_datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ struct sensing_sensor_value_uint32 {
* q31 version
*/
struct sensing_sensor_value_q31 {
int8_t shift;
struct sensing_sensor_value_header header;
int8_t shift;
struct {
uint32_t timestamp_delta;
q31_t v;
Expand Down
Loading
Loading