Skip to content

Commit 0f77322

Browse files
author
Yuval Peress
committed
Implement angle sensor
1 parent 8790633 commit 0f77322

File tree

33 files changed

+1021
-68
lines changed

33 files changed

+1021
-68
lines changed

cmake/linker_script/common/common-ram.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ endif()
5656

5757
if(CONFIG_SENSING)
5858
zephyr_iterable_section(NAME sensing_sensor GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4)
59+
zephyr_iterable_section(NAME sensing_connection GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4)
5960
endif()
6061

6162
if(CONFIG_UART_MUX)

drivers/sensor/icm42688/icm42688_decoder.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ static int icm42688_get_shift(enum sensor_channel channel, int accel_fs, int gyr
2323
case SENSOR_CHAN_ACCEL_Z:
2424
switch (accel_fs) {
2525
case ICM42688_ACCEL_FS_2G:
26-
*shift = 5;
26+
*shift = 4;
2727
return 0;
2828
case ICM42688_ACCEL_FS_4G:
29-
*shift = 6;
29+
*shift = 5;
3030
return 0;
3131
case ICM42688_ACCEL_FS_8G:
32-
*shift = 7;
32+
*shift = 6;
3333
return 0;
3434
case ICM42688_ACCEL_FS_16G:
35-
*shift = 8;
35+
*shift = 7;
3636
return 0;
3737
default:
3838
return -EINVAL;
@@ -132,12 +132,9 @@ int icm42688_convert_raw_to_q31(struct icm42688_cfg *cfg, enum sensor_channel ch
132132
default:
133133
return -ENOTSUP;
134134
}
135+
135136
intermediate = ((int64_t)whole * INT64_C(1000000) + fraction);
136-
if (shift < 0) {
137-
intermediate = intermediate * INT32_MAX * (1 << -shift) / INT64_C(1000000);
138-
} else if (shift > 0) {
139-
intermediate = intermediate * INT32_MAX / (((1 << shift) - 1) * INT64_C(1000000));
140-
}
137+
intermediate = intermediate * ((INT64_C(1) << (31 - shift)) - 1) / INT64_C(1000000);
141138
*out = CLAMP(intermediate, INT32_MIN, INT32_MAX);
142139

143140
return 0;
@@ -191,6 +188,9 @@ static uint8_t icm42688_encode_channel(enum sensor_channel chan)
191188
BIT(icm42688_get_channel_position(SENSOR_CHAN_GYRO_Y)) |
192189
BIT(icm42688_get_channel_position(SENSOR_CHAN_GYRO_Z));
193190
break;
191+
case SENSOR_CHAN_ALL:
192+
encode_bmask = 0x7f;
193+
break;
194194
default:
195195
break;
196196
}

drivers/sensor/icm42688/icm42688_rtio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static void icm42688_fifo_count_cb(struct rtio *r, const struct rtio_sqe *sqe, v
203203
read_len = pkts * packet_size;
204204
((struct icm42688_fifo_data *)buf)->fifo_count = read_len;
205205

206-
__ASSERT_NO_MSG(read_len % pkt_size == 0);
206+
__ASSERT_NO_MSG(read_len % packet_size == 0);
207207

208208
uint8_t *read_buf = buf + sizeof(hdr);
209209

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2023 Google LLC
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Accelerometer based lid angle virtual sensor
5+
6+
compatible: "sensing,accel-based-angle"
7+
8+
include: [sensor-device.yaml, base.yaml]
9+
10+
properties:
11+
plane0:
12+
type: phandle
13+
required: true
14+
description: |
15+
Node owning the sensing_sensor_info for the first plane's accelerometer
16+
17+
plane1:
18+
type: phandle
19+
required: true
20+
description: |
21+
Node owning the sensing_sensor_info for the second plane's accelerometer
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (c) 2023 Google LLC
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Emulated virtual sensor
5+
6+
compatible: "sensing,emul"
7+
8+
include: [sensor-device.yaml, base.yaml]
9+
10+
properties:
11+
12+
sensor-types:
13+
type: array
14+
required: true
15+
description: |
16+
One or more sensor data types that are provided by this device. For
17+
example, a 'dev' pointing to a bmi160 may report accel/gyro/die_temp or
18+
any subset of those types.
19+
20+
rotation-matrix:
21+
type: array
22+
description: |
23+
3x3 matrix to rotation x, y, and z axes.
24+
In order to make application logic agnostic to how the device was placed
25+
on the board, it's possible to enter the rotation matrix which will be
26+
applied to every sample produced by this sensor. The final orientation
27+
will be:
28+
* X-axis is horizontal and positive toward the right
29+
* Y-axis is vertical and positive toward the top
30+
* Z-axis is depth and positive toward the user
31+
32+
If not provided, the rotation matrix will be the identity matrix.
33+
Otherwise, the following will be used:
34+
35+
+- -+ +- -+ +- -+
36+
| v1 v2 v3 | | sensor_X | = | X |
37+
| v4 v5 v6 | * | sensor_Y | = | Y |
38+
| v7 v8 v9 | | sensor_Z | = | Z |
39+
+- -+ +- -+ +- -+

dts/bindings/vendor-prefixes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ segger SEGGER Microcontroller GmbH
531531
seeed Seeed Technology Co., Ltd
532532
seirobotics Shenzhen SEI Robotics Co., Ltd
533533
semtech Semtech Corporation
534+
sensing Zephyr Sensing Subsystem
534535
sensirion Sensirion AG
535536
sensortek Sensortek Technology Corporation
536537
sff Small Form Factor Committee

include/zephyr/sensing/datatypes.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,25 @@ struct sensing_sensor_value_header {
6464
* SENSING_SENSOR_TYPE_MOTION_GYROMETER_3D,
6565
* q31 version
6666
*/
67-
struct sensing_sensor_value_3d_q31 {
67+
struct sensing_sensor_three_axis_data {
6868
struct sensing_sensor_value_header header;
6969
int8_t shift;
7070
struct {
7171
uint32_t timestamp_delta;
7272
union {
73+
q31_t values[3];
7374
q31_t v[3];
7475
struct {
7576
q31_t x;
7677
q31_t y;
7778
q31_t z;
7879
};
80+
q31_t bias[3];
81+
struct {
82+
q31_t x_bias;
83+
q31_t y_bias;
84+
q31_t z_bias;
85+
};
7986
};
8087
} readings[1];
8188
};
@@ -98,7 +105,7 @@ struct sensing_sensor_value_uint32 {
98105
* struct sensing_sensor_value_q31 can be used by SENSING_SENSOR_TYPE_MOTION_HINGE_ANGLE sensor
99106
* q31 version
100107
*/
101-
struct sensing_sensor_value_q31 {
108+
struct sensing_sensor_float_data {
102109
int8_t shift;
103110
struct sensing_sensor_value_header header;
104111
struct {

include/zephyr/sensing/sensing.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ typedef void *sensing_sensor_handle_t;
166166
*
167167
* @param buf The data buffer with sensor data.
168168
*/
169-
typedef void (*sensing_data_event_t)(sensing_sensor_handle_t handle, const void *buf);
169+
typedef void (*sensing_data_event_t)(sensing_sensor_handle_t handle, const void *buf, void *userdata);
170170

171171
#include <zephyr/rtio/rtio.h>
172172
/**
@@ -232,7 +232,8 @@ int sensing_get_sensors(int *num_sensors, const struct sensing_sensor_info **inf
232232
*/
233233
int sensing_open_sensor(const struct sensing_sensor_info *info,
234234
const struct sensing_callback_list *cb_list,
235-
sensing_sensor_handle_t *handle);
235+
sensing_sensor_handle_t *handle,
236+
void *userdata);
236237

237238
/**
238239
* @brief Close sensor instance.

include/zephyr/sensing/sensor.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,36 @@
1111
#include <zephyr/drivers/sensor.h>
1212
#include <zephyr/sensing/sensing.h>
1313

14+
struct sensing_connection {
15+
const struct sensing_sensor_info *info;
16+
const struct sensing_callback_list *cb_list;
17+
void *userdata;
18+
enum sensing_sensor_mode mode;
19+
q31_t attributes[SENSOR_ATTR_COMMON_COUNT];
20+
uint32_t attribute_mask;
21+
} __packed __aligned(4);
22+
23+
#define SENSING_CONNECTION_DT_DEFINE(node_id, target_node_id, type, _cb_list) \
24+
SENSING_DMEM STRUCT_SECTION_ITERABLE(sensing_connection, node_id##_sensing_connection) = { \
25+
.info = SENSING_SENSOR_INFO_GET(target_node_id, type), \
26+
.cb_list = (_cb_list), \
27+
}
28+
29+
STRUCT_SECTION_START_EXTERN(sensing_connection);
30+
STRUCT_SECTION_END_EXTERN(sensing_connection);
31+
1432
#define SENSING_SENSOR_INFO_DT_NAME(node_id, type) \
1533
_CONCAT(_CONCAT(__sensing_sensor_info_, DEVICE_DT_NAME_GET(node_id)), type)
1634

1735
#define SENSING_SENSOR_INFO_INST_DEFINE_NAMED(node_id, name, prop, idx, _iodev) \
18-
IF_ENABLED(CONFIG_SENSING_SHELL, (static char node_id##_##idx##_name_buffer[5];)) \
36+
IF_ENABLED(CONFIG_SENSING_SHELL, (static char node_id##_##idx##_name_buffer[5];)) \
1937
const STRUCT_SECTION_ITERABLE(sensing_sensor_info, name) = { \
2038
.info = &SENSOR_INFO_DT_NAME(DT_PHANDLE(node_id, dev)), \
2139
.dev = DEVICE_DT_GET(node_id), \
2240
.type = DT_PROP_BY_IDX(node_id, prop, idx), \
2341
.iodev = &(_iodev), \
24-
IF_ENABLED(CONFIG_SENSING_SHELL, (.shell_name = node_id##_##idx##_name_buffer, )) \
25-
};
42+
IF_ENABLED(CONFIG_SENSING_SHELL, \
43+
(.shell_name = node_id##_##idx##_name_buffer, ))};
2644

2745
#define SENSING_SENSOR_INFO_INST_DEFINE(node_id, prop, idx, _iodev) \
2846
SENSING_SENSOR_INFO_INST_DEFINE_NAMED( \
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Copyright (c) 2023 Google LLC
22
# SPDX-License-Identifier: Apache-2.0
33
CONFIG_SPI_RTIO=y
4+
CONFIG_CMSIS_DSP=y

0 commit comments

Comments
 (0)