-
Notifications
You must be signed in to change notification settings - Fork 7.5k
rtio: Real-Time Stream and Sensor API #17434
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b51be5
rtio: Real-Time Stream API
teburd d51aa8e
rtio: Context common driver functionality
teburd 53192f4
rtio: Ramp device and samples for API validation
teburd 548e19d
rtio_sensor: Sensor API using RTIO
teburd a97bc20
fixup! rtio: Real-Time Stream API
mbolivar-nordic 3a6cb4c
fixup! rtio: Context common driver functionality
mbolivar-nordic 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 hidden or 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,16 @@ | ||
# | ||
# RTIO | ||
# | ||
# | ||
# include/rtio.h warnings | ||
# | ||
^(?P<filename>([\-:\\/\w\.])+[/\\]doc[/\\]reference[/\\]peripherals[/\\]rtio.rst):(?P<lineno>[0-9]+): WARNING: Invalid definition: Expected identifier in nested name. \[error at [0-9]+] | ||
^[ \t]* | ||
^[ \t]*\^ | ||
^(?P=filename):(?P=lineno): WARNING: Invalid definition: Expected identifier in nested name. \[error at [0-9]+] | ||
^[ \t]* | ||
^[ \t]*\^ | ||
^(?P=filename):(?P=lineno): WARNING: Invalid definition: Expected end of definition. \[error at [0-9]+] | ||
^.*rtio_trigger_config.options | ||
^[- \t]*\^ | ||
# |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -21,6 +21,7 @@ Peripherals | |
pinmux.rst | ||
pwm.rst | ||
ps2.rst | ||
rtio.rst | ||
sensor.rst | ||
spi.rst | ||
uart.rst | ||
|
This file contains hidden or 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,59 @@ | ||
.. _rtio_interface: | ||
|
||
Real-Time IO | ||
############ | ||
|
||
The real-time I/O or RTIO subsystem exposes an API for efficiently | ||
moving information in a latency and processing friendly way, between | ||
a Zephyr application and external I2C or SPI devices (such as sensors). | ||
|
||
Overview | ||
******** | ||
|
||
RTIO provides an API for allocating and describing continguous blocks of | ||
memory as well as an API for sourcing these blocks from a physical device. | ||
|
||
Blocks, defined by :c:type:`struct rtio_block`, provide the unit of data | ||
being transferred. These blocks contain a layout identifier provided by | ||
the source of the block, timestamps for when the block | ||
began and finished being created, as well as the length and size in bytes. | ||
|
||
Blocks are used to allow for simple DMA transfers as well as controlling the | ||
latency between a source and sink. | ||
|
||
The block layout identifier is a way for the source of the block to describe | ||
whats in the block. This can be used to identify which channels of information | ||
are in the block, the sample size, and more. It is source defined and | ||
not unique between different sources. | ||
|
||
The begin and end timestamps may be used to interpolate when events have | ||
occurred without needing a timestamp for every reading. For example when | ||
reading a hardware FIFO from a device sampling at a constant rate the | ||
begin and and timestamps may be used to infer when each sample in the block | ||
happened in your application by doing simple linear interpolation. | ||
|
||
They may also be used to understand the sample rate of the device better | ||
as often times devices have nominal sampling rates that vary device to device. | ||
|
||
Sources of blocks can be sensors such as a high sample rate accelerometer, gyro, | ||
and magnetometer. In these cases the driver will typically provide blocks | ||
encoded in the sensor's native format which is often times the most compact | ||
representation. | ||
|
||
Intermediary processing steps representing both sinks and sources of data are | ||
also possible. One possible task is taking 6 or 9 axis IMU sensor and | ||
producing a orientation quaternion vectors relative to earth. | ||
|
||
The driving event of actually starting the read from a device is the | ||
`rtio_trigger_read` function. It is meant to be an ISR safe function, meaning | ||
it can be called in any interrupt handler such as a GPIO interrupt callback. | ||
|
||
The RTIO subsystem lets you both efficiently move data around and | ||
efficiently process chunks of data at a time in a flexible way. | ||
|
||
|
||
API Reference | ||
************* | ||
|
||
.. doxygengroup:: rtio_interface | ||
:project: Zephyr |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -89,4 +89,7 @@ source "drivers/video/Kconfig" | |
|
||
source "drivers/eeprom/Kconfig" | ||
|
||
source "drivers/rtio/Kconfig" | ||
|
||
|
||
endmenu |
This file contains hidden or 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,6 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
zephyr_library() | ||
|
||
zephyr_library_sources_ifdef(CONFIG_RTIO_CONTEXT rtio_context.c) | ||
zephyr_library_sources_ifdef(CONFIG_RTIO_RAMP rtio_ramp.c) |
This file contains hidden or 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,38 @@ | ||
# Kconfig - rtio configuration options | ||
|
||
# | ||
# Copyright (c) 2019 Tom Burdick <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
menuconfig RTIO | ||
bool "RTIO Drivers" | ||
select NET_BUF | ||
help | ||
Include rtio drivers in system config | ||
|
||
if RTIO | ||
|
||
module = RTIO | ||
module-str = rtio | ||
source "subsys/logging/Kconfig.template.log_config" | ||
|
||
config RTIO_INIT_PRIORITY | ||
int "RTIO init priority" | ||
default 90 | ||
help | ||
RTIO initialization priority. | ||
|
||
config RTIO_CONTEXT | ||
# Private option; will be selected by the drivers if | ||
# this feature is required. | ||
bool | ||
|
||
config RTIO_RAMP | ||
bool "RTIO ramp driver" | ||
select RTIO_CONTEXT | ||
help | ||
RTIO ramp driver | ||
|
||
endif # RTIO |
This file contains hidden or 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,134 @@ | ||
/* | ||
* Copyright (c) 2019 Tom Burdick <[email protected]> | ||
* Copyright (c) 2019 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <kernel.h> | ||
#include <drivers/rtio.h> | ||
#include "rtio_context.h" | ||
|
||
#define LOG_DOMAIN "rtio_context" | ||
#define LOG_LEVEL CONFIG_RTIO_LOG_LEVEL | ||
#include <logging/log.h> | ||
LOG_MODULE_REGISTER(rtio_context); | ||
|
||
static void rtio_context_output_timeout(struct k_timer *timer) | ||
{ | ||
struct rtio_context *ctx = | ||
CONTAINER_OF(timer, struct rtio_context, output_timer); | ||
|
||
int res = k_sem_take(&ctx->sem, K_NO_WAIT); | ||
|
||
if (res == 0) { | ||
if (ctx->block != NULL && | ||
ctx->config.output_config.fifo != NULL) { | ||
k_fifo_put(ctx->config.output_config.fifo, ctx->block); | ||
ctx->block = NULL; | ||
} | ||
k_sem_give(&ctx->sem); | ||
} | ||
} | ||
|
||
void rtio_context_init(struct rtio_context *ctx) | ||
{ | ||
k_sem_init(&ctx->sem, 0, 1); | ||
rtio_output_config_init(&ctx->config.output_config); | ||
ctx->block = NULL; | ||
k_timer_init(&ctx->output_timer, rtio_context_output_timeout, NULL); | ||
k_sem_give(&ctx->sem); | ||
} | ||
|
||
int rtio_context_configure_begin(struct rtio_context *ctx) | ||
{ | ||
k_sem_take(&ctx->sem, K_FOREVER); | ||
if (ctx->block != NULL && ctx->config.output_config.fifo != NULL) { | ||
k_fifo_put(ctx->config.output_config.fifo, ctx->block); | ||
ctx->block = NULL; | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
void rtio_context_configure_end(struct rtio_context *ctx, | ||
struct rtio_config *config) | ||
{ | ||
s32_t timeout; | ||
|
||
memcpy(&ctx->config, config, sizeof(struct rtio_config)); | ||
|
||
/* Setup timer if needed */ | ||
k_timer_stop(&ctx->output_timer); | ||
timeout = ctx->config.output_config.timeout; | ||
if (timeout != K_FOREVER && timeout != K_NO_WAIT) { | ||
k_timer_start(&ctx->output_timer, timeout, timeout); | ||
} | ||
|
||
k_sem_give(&ctx->sem); | ||
} | ||
|
||
int rtio_context_trigger_read_begin(struct rtio_context *ctx, | ||
struct rtio_block **block, | ||
s32_t timeout) | ||
{ | ||
int res = 0; | ||
|
||
res = k_sem_take(&ctx->sem, timeout); | ||
if (res != 0) { | ||
return -EBUSY; | ||
} | ||
|
||
if (ctx->block == NULL) { | ||
res = rtio_block_alloc(ctx->config.output_config.allocator, | ||
&ctx->block, | ||
ctx->config.output_config.byte_size, | ||
timeout); | ||
if (res != 0) { | ||
goto error; | ||
} | ||
} | ||
|
||
*block = ctx->block; | ||
return res; | ||
|
||
error: | ||
k_sem_give(&ctx->sem); | ||
return res; | ||
} | ||
|
||
/** | ||
* @brief Check if a block has met an output policy expectations | ||
* | ||
* @return 1 if the policy has been met, 0 otherwise | ||
*/ | ||
static inline int rtio_context_output_check(struct rtio_output_config *cfg, | ||
struct rtio_block *block) | ||
{ | ||
/* | ||
if (k_cycle_get_32() - block->begin_tstamp > cfg->timeout) { | ||
return true; | ||
} | ||
*/ | ||
if (rtio_block_used(block) >= cfg->byte_size) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
int rtio_context_trigger_read_end(struct rtio_context *ctx) | ||
{ | ||
struct rtio_output_config *out_cfg = &ctx->config.output_config; | ||
int res; | ||
|
||
if (rtio_context_output_check(out_cfg, ctx->block) == 1) { | ||
k_fifo_put(out_cfg->fifo, ctx->block); | ||
ctx->block = NULL; | ||
res = 1; | ||
} else { | ||
res = 0; | ||
} | ||
|
||
k_sem_give(&ctx->sem); | ||
return res; | ||
} |
Oops, something went wrong.
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.
Thanks for this documentation. It would be nice to add a few sentences about triggers to this.
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.
Definitely, I have some function comments on rtio_trigger but nothing in the general docs explaining what its there for.