-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
387 additions
and
58 deletions.
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
.\#* | ||
\#*\# | ||
build*/ | ||
/build* | ||
!doc/build/ | ||
!scripts/build | ||
!tests/drivers/build_all | ||
|
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
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,36 @@ | ||
// | ||
// Created by peress on 27/06/23. | ||
// | ||
|
||
#ifndef ZEPHYR_SUBSYS_SENSING_INCLUDE_SENSING_INTERNAL_SENSING_H | ||
#define ZEPHYR_SUBSYS_SENSING_INCLUDE_SENSING_INTERNAL_SENSING_H | ||
|
||
#include <zephyr/drivers/sensor.h> | ||
#include <zephyr/sensing/sensing.h> | ||
#include <zephyr/dsp/types.h> | ||
|
||
#define __SENSING_POOL_MASK_BUNDLE_COUNT \ | ||
(DIV_ROUND_UP(DIV_ROUND_UP(CONFIG_SENSING_MAX_CONNECTIONS, 8), sizeof(uint32_t))) | ||
|
||
struct sensing_connection { | ||
const struct sensing_sensor_info *info; | ||
const struct sensing_callback_list *cb_list; | ||
q31_t attributes[SENSOR_ATTR_COMMON_COUNT]; | ||
uint32_t attribute_mask; | ||
struct { | ||
uint8_t in_use: 1; | ||
uint8_t reserved: 7; | ||
} flags; | ||
} __packed __aligned(4); | ||
|
||
extern struct sensing_connection_pool { | ||
struct sensing_connection pool[CONFIG_SENSING_MAX_CONNECTIONS]; | ||
sys_bitarray_t *bitarray; | ||
struct k_mutex *lock; | ||
} __sensing_connection_pool; | ||
|
||
BUILD_ASSERT(SENSOR_ATTR_COMMON_COUNT <= 32, "Too many sensor attributes"); | ||
|
||
void __sensing_arbitrate(); | ||
|
||
#endif // ZEPHYR_SUBSYS_SENSING_INCLUDE_SENSING_INTERNAL_SENSING_H |
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,96 @@ | ||
// | ||
// Created by peress on 27/06/23. | ||
// | ||
|
||
#include <zephyr/sensing/sensing.h> | ||
#include <zephyr/sys/util.h> | ||
#include <zephyr/logging/log.h> | ||
|
||
#include "sensing/internal/sensing.h" | ||
|
||
LOG_MODULE_REGISTER(sensing_arbitrate, CONFIG_SENSING_LOG_LEVEL); | ||
|
||
static q31_t arbitrate_attribute_value(enum sensor_attribute attribute, q31_t current_value, | ||
q31_t new_value) | ||
{ | ||
switch (attribute) { | ||
case SENSOR_ATTR_SAMPLING_FREQUENCY: | ||
return MAX(current_value, new_value); | ||
default: | ||
return current_value; | ||
} | ||
} | ||
|
||
static void set_arbitrated_value(const struct device *dev, int32_t type, | ||
enum sensor_attribute attribute, q31_t value) | ||
{ | ||
enum sensor_channel chan; | ||
switch (type) { | ||
case SENSING_SENSOR_TYPE_MOTION_ACCELEROMETER_3D: | ||
chan = SENSOR_CHAN_ACCEL_XYZ; | ||
break; | ||
case SENSING_SENSOR_TYPE_MOTION_GYROMETER_3D: | ||
chan = SENSOR_CHAN_GYRO_XYZ; | ||
break; | ||
default: | ||
return; | ||
} | ||
|
||
struct sensor_value val = { | ||
.val1 = FIELD_GET(GENMASK(31, 16), value), | ||
.val2 = (FIELD_GET(GENMASK(15, 0), value) * 1000000) / INT16_MAX, | ||
}; | ||
LOG_DBG("Updating attribute chan=%d, value=%d/%d", chan, val.val1, val.val2); | ||
((const struct sensor_driver_api *)(dev->api))->attr_set(dev, chan, attribute, &val); | ||
} | ||
|
||
static int arbitrate_sensor_attribute(const struct sensing_sensor_info *info, | ||
enum sensor_attribute attribute) | ||
{ | ||
const struct sensing_connection *connections = __sensing_connection_pool.pool; | ||
int connection_count = 0; | ||
q31_t value; | ||
|
||
for (int i = 0; i < CONFIG_SENSING_MAX_CONNECTIONS; ++i) { | ||
if (connections[i].info != info || !connections[i].flags.in_use) { | ||
// LOG_DBG("Skipping connection %p, info mismatch", (void*)&connections[i]); | ||
continue; | ||
} | ||
if (FIELD_GET(BIT(attribute), connections[i].attribute_mask) == 0) { | ||
// LOG_DBG("Skipping connection %p, attribute not set", (void*)&connections[i]); | ||
continue; | ||
} | ||
if (connection_count == 0) { | ||
/* First connection */ | ||
value = connections[i].attributes[attribute]; | ||
LOG_DBG("Arbitrating '%s'@%p type=%d attribute=%d", info->info->dev->name, | ||
info->info->dev, info->type, attribute); | ||
LOG_DBG(" First connection %p, value=0x%08x", (void*)&connections[i], value); | ||
} else { | ||
value = arbitrate_attribute_value(attribute, value, | ||
connections[i].attributes[attribute]); | ||
LOG_DBG(" Updating %p, value=0x%08x", (void*)&connections[i], value); | ||
} | ||
connection_count++; | ||
} | ||
|
||
if (connection_count != 0) { | ||
set_arbitrated_value(info->dev, info->type, attribute, value); | ||
} | ||
|
||
return connection_count; | ||
} | ||
|
||
static void arbitrate_sensor_instance(const struct sensing_sensor_info *info) | ||
{ | ||
for (int i = 0; i < SENSOR_ATTR_COMMON_COUNT; ++i) { | ||
arbitrate_sensor_attribute(info, i); | ||
} | ||
} | ||
|
||
void __sensing_arbitrate(void) | ||
{ | ||
STRUCT_SECTION_FOREACH(sensing_sensor_info, info) { | ||
arbitrate_sensor_instance(info); | ||
} | ||
} |
Oops, something went wrong.