Skip to content

zephyrCommon: Make configurable the max number of tones#152

Merged
DhruvaG2000 merged 3 commits intozephyrproject-rtos:nextfrom
soburi:use_available_timer
Feb 10, 2026
Merged

zephyrCommon: Make configurable the max number of tones#152
DhruvaG2000 merged 3 commits intozephyrproject-rtos:nextfrom
soburi:use_available_timer

Conversation

@soburi
Copy link
Copy Markdown
Member

@soburi soburi commented Feb 1, 2026

  • Fixed a bug where tone() with duration=0 would stop the tone immediately instead of ringing infinitely.

  • Allows you to change the maximum number of notes that can be played with tone().

  • Add a tone_doremi sample to demonstrate how to sounding with tone API

@soburi soburi force-pushed the use_available_timer branch 4 times, most recently from d1fa1d4 to 41a8a6b Compare February 3, 2026 23:16
@soburi soburi marked this pull request as ready for review February 4, 2026 01:17
Copilot AI review requested due to automatic review settings February 4, 2026 01:17
@soburi soburi marked this pull request as draft February 4, 2026 01:18
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Zephyr Arduino core’s tone() implementation to support more flexible timer allocation by dynamically selecting unused timers, and adds a Kconfig option to control the maximum number of concurrent tones.

Changes:

  • Add CONFIG_ARDUINO_MAX_TONES (default -1) to configure the tone timer pool size.
  • Track timer usage via a per-timer pin field and allocate/release timers dynamically in tone()/noTone().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
cores/arduino/zephyrCommon.cpp Implements dynamic allocation/release of tone timers and makes the pool size configurable.
Kconfig Adds ARDUINO_MAX_TONES configuration option (default -1 for auto sizing).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@soburi soburi force-pushed the use_available_timer branch 5 times, most recently from f424836 to 2c4afe2 Compare February 5, 2026 11:53
@soburi soburi requested a review from Copilot February 5, 2026 11:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

cores/arduino/zephyrCommon.cpp:281

  • When frequency is 0, the function returns early without clearing the timer state (setting pt->pin = pin_size_t(-1)). This means the timer slot remains allocated to this pin even though it's no longer active. If this pin calls tone() again later, it will find and reuse the same timer, which is fine. However, if the maximum number of timers is reached and this pin's timer is not properly freed, it could prevent other pins from allocating timers. Consider adding pt->pin = pin_size_t(-1); before the return on line 281 to properly free the timer slot.
  if (frequency == 0) {
    gpio_pin_set_dt(spec, 0);
    return;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@soburi soburi changed the title zephyrCommon: Use available tone timers zephyrCommon: Make configurable the max number of tones Feb 5, 2026
@soburi soburi force-pushed the use_available_timer branch from ca91026 to 00dc900 Compare February 5, 2026 14:11
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@soburi soburi force-pushed the use_available_timer branch 6 times, most recently from b837aad to 6fffccc Compare February 9, 2026 09:52


if (pin == pin_size_t(-1)) {
unusedIndex = i;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This slot could be claimed by another thread right?


k_spin_unlock(&arduino_pin_timers[i].lock, key);


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

remove extra newline please

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've overhauled this entire function.

k_spin_unlock(&arduino_pin_timers[i].lock, key);


if (pin == pin_size_t(-1)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The function releases the lock immediately after reading pin, then uses that stale value. Between unlock and the check, another thread could claim that slot right?

Comment on lines +234 to +253
size_t unusedIndex = size_t(-1);

for (size_t i = 0; i < ARRAY_SIZE(arduino_pin_timers); i++) {
k_spinlock_key_t key = k_spin_lock(&arduino_pin_timers[i].lock);
size_t pin = arduino_pin_timers[i].pin;

k_spin_unlock(&arduino_pin_timers[i].lock, key);


if (pin == pin_size_t(-1)) {
unusedIndex = i;
}

if (pin == pinNumber) {
return &arduino_pin_timers[i];
}
}

if (unusedIndex != size_t(-1) && !active_only) {
return &arduino_pin_timers[unusedIndex];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh I see copilot had already caught this? What was the reason for resolving?

@soburi soburi marked this pull request as draft February 9, 2026 13:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Member

@DhruvaG2000 DhruvaG2000 left a comment

Choose a reason for hiding this comment

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

just one final comment

pt->pin = pin_size_t(-1);
} else {
if (pin >= 0) {
if (pin != pin_size_t(-1)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why are these changes inside add tone_doremi sample? Can be very misleading, please split into another commit.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oops!
It was misoperation, Fixed. Thank you for catching.

Fixed a bug where tone() with duration=0 would stop the tone immediately
instead of ringing infinitely.

Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
Allows you to change the maximum number of notes that can be
played with `tone()`.

Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
Add a tone_doremi sample to demonstrate how to sounding with tone API

Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
@soburi soburi mentioned this pull request Feb 14, 2026
16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants