Skip to content

drivers/sgp30: use event_timeout, to avoid long ISR code block#22149

Open
fjmolinas wants to merge 4 commits intoRIOT-OS:masterfrom
fjmolinas:pr/sgp30_use_events
Open

drivers/sgp30: use event_timeout, to avoid long ISR code block#22149
fjmolinas wants to merge 4 commits intoRIOT-OS:masterfrom
fjmolinas:pr/sgp30_use_events

Conversation

@fjmolinas
Copy link
Copy Markdown
Contributor

Contribution description

This avoid reading measurements in ISR context, which will behave differently depending on the ISR context size. To avoid this use events posting to the lowest priority event loop.

I've been away for a while so maybe there is a better way of handling this now?

Testing procedure

I have an nrf52840-mdk with the an sgp30 connected, after waiting for 15s I get readings:

2026-03-22 20:30:08,589 # eCO2 [ppm]: 439
2026-03-22 20:30:08,592 # +-------------------------------------+
2026-03-22 20:30:08,695 # TVOC [ppb]: 4
2026-03-22 20:30:08,696 # eCO2 [ppm]: 439
2026-03-22 20:30:08,706 # +-------------------------------------+
2026-03-22 20:30:08,802 # TVOC [ppb]: 4
2026-03-22 20:30:08,804 # eCO2 [ppm]: 439
2026-03-22 20:30:08,807 # +-------------------------------------+

Without this PR hardfault:

2026-03-22 20:31:28,124 # Device is not yet ready
2026-03-22 20:31:28,226 # TVOC [ppb]: 86
2026-03-22 20:31:28,228 # Context before hardfault:
2026-03-22 20:31:28,234 # main(): This is RIOT! (Version: 2017.10-devel-34759-g66dbcd)
2026-03-22 20:31:28,236 # SGP30 test application
2026-03-22 20:31:28,236 #
2026-03-22 20:31:28,239 # +------------Initializing------------+

Issues/PRs references

None

@github-actions github-actions bot added the Area: drivers Area: Device drivers label Mar 22, 2026
@fjmolinas fjmolinas requested a review from benpicco March 22, 2026 19:32
@crasbe crasbe added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Mar 22, 2026
@riot-ci
Copy link
Copy Markdown

riot-ci commented Mar 22, 2026

Murdock results

FAILED

a516421 fixup! fixup! drivers/sgp30: use event_timeout, to avoid long ISR code block

Success Failures Total Runtime
615 0 10048 02m:21s

Artifacts

dev->_timer.callback = _read_cb;
dev->_timer.arg = dev;
event_callback_init(&dev->_event, _read_cb, dev);
event_timeout_init(&dev->_timeout, EVENT_PRIO_LOWEST, (event_t *)&dev->_event);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

better make the queue configurable

Suggested change
event_timeout_init(&dev->_timeout, EVENT_PRIO_LOWEST, (event_t *)&dev->_event);
event_timeout_init(&dev->_timeout, CONFIG_SGP30_EVENT_QUEUE, (event_t *)&dev->_event);
#ifndef CONFIG_SGP30_EVENT_QUEUE
#define CONFIG_SGP30_EVENT_QUEUE EVENT_PRIO_LOWEST
#endif

Why EVENT_PRIO_LOWEST instead of EVENT_PRIO_MEDIUM?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

None, Is EVENT_PRIO_MEDIUM the best default?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I still think it makes sense to make it configurable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, Ok to squash?

extern "C" {
#endif


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change

Comment on lines +65 to +66
#ifndef SGP30_STRICT_EVENT_THREAD_QUEUE
#define SGP30_STRICT_EVENT_THREAD_QUEUE EVENT_PRIO_MEDIUM
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
#ifndef SGP30_STRICT_EVENT_THREAD_QUEUE
#define SGP30_STRICT_EVENT_THREAD_QUEUE EVENT_PRIO_MEDIUM
#ifndef CONFIG_SGP30_STRICT_EVENT_THREAD_QUEUE
#define CONFIG_SGP30_STRICT_EVENT_THREAD_QUEUE EVENT_PRIO_MEDIUM

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please also add indentation to the #define here.

dev->_timer.arg = dev;
event_callback_init(&dev->_event, _read_cb, dev);
event_timeout_init(&dev->_timeout, SGP30_STRICT_EVENT_THREAD_QUEUE, (event_t *)&dev->_event);
#error
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This would unconditionally error out when MODULE_SGP30_STRICT is defined?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: drivers Area: Device drivers CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants