-
Notifications
You must be signed in to change notification settings - Fork 181
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
add queue example #460
Open
Vollbrecht
wants to merge
1
commit into
esp-rs:master
Choose a base branch
from
Vollbrecht:queue-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add queue example #460
Changes from all commits
Commits
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 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,91 @@ | ||
/// FreeRTOS queue example | ||
/// The core concepts covered in this example include: | ||
/// - Creating and using queues to exchange data between an ISR(Interrupt Service Routine) and a thread (both directions). | ||
/// - Efficiently waiting for messages in the thread using queues. | ||
/// | ||
/// This is demonstrated with a periodic timer ISR and the rust main thread, but it can be used in any thread or ISR context. | ||
use esp_idf_hal::delay::BLOCK; | ||
use esp_idf_hal::peripherals::Peripherals; | ||
use esp_idf_hal::task; | ||
use esp_idf_hal::task::queue::Queue; | ||
use esp_idf_hal::timer::{TimerConfig, TimerDriver}; | ||
use esp_idf_sys::{uxQueueMessagesWaiting, EspError}; | ||
|
||
fn main() -> Result<(), EspError> { | ||
// It is necessary to call this function once. Otherwise some patches to the runtime | ||
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 | ||
esp_idf_hal::sys::link_patches(); | ||
|
||
let per = Peripherals::take()?; | ||
|
||
let mut timer = TimerDriver::new(per.timer00, &TimerConfig::new().auto_reload(true))?; | ||
|
||
// create two queue's that can hold 20/100 elements with the element size of an u8 | ||
let queue_send = Queue::<u8>::new(20); | ||
let queue_recv = Queue::<u8>::new(100); | ||
|
||
// SAFETY: as long as queue_send/recv live we can use isr_receive & isr_send. | ||
let isr_recv: Queue<u8> = unsafe { Queue::new_borrowed(queue_send.as_raw()) }; | ||
let isr_send: Queue<u8> = unsafe { Queue::new_borrowed(queue_recv.as_raw()) }; | ||
|
||
// Every half a second | ||
timer.set_alarm(timer.tick_hz() / 2)?; | ||
|
||
// SAFTEY: make sure the timer is droped and thouse end the subscription before we drop queue_send/recv. | ||
// E,g don't send stuff/recv stuff from a borrowd handle after that point. When the timer object is droped it will auto unsubscribe. | ||
// | ||
// Safe since we never end main and thouse never unsubscribe nor drop queue_send/queue_recv | ||
unsafe { | ||
timer.subscribe(move || { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since
|
||
// timeout value is ignored in ISR context by the API, a ISR cannot ever be allowed to block. | ||
if let Some((value, higher_isr_awoken)) = isr_recv.recv_front(BLOCK) { | ||
for i in 0..5 { | ||
let send_back = value.wrapping_add(i); | ||
// returns a error when queue is full | ||
if isr_send.send_back(send_back, BLOCK).is_err() { | ||
break; | ||
} | ||
} | ||
|
||
// good practice and benifical for system performance | ||
if higher_isr_awoken { | ||
task::do_yield(); | ||
} | ||
} | ||
})?; | ||
} | ||
|
||
println!("Start Timer!"); | ||
timer.enable_interrupt()?; | ||
timer.enable_alarm(true)?; | ||
timer.enable(true)?; | ||
|
||
let mut val = 42; | ||
loop { | ||
println!("Sending {val}"); | ||
if queue_send.send_back(val, BLOCK).is_err() { | ||
println!("Qeueu is full, could not send next value to ISR"); | ||
} | ||
|
||
// Waiting from the ISR to send us stuff back. | ||
// | ||
// We will go into sleep and get automatically awoken by the scheduler | ||
// as soon as something was added to the queue. | ||
match queue_recv.recv_front(BLOCK) { | ||
Some((value, _)) => { | ||
println!("Got {value} from ISR!"); | ||
} | ||
None => println!("Timeout while waiting for a new message"), | ||
} | ||
|
||
// we can now check if there are still more items in the queue and read the rest. | ||
let unread_item_count = unsafe { uxQueueMessagesWaiting(queue_recv.as_raw()) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we expose this as a safe method? |
||
for _ in 0..unread_item_count { | ||
// we read exactly the number of unread items so we can unwrap and we know its not None | ||
let (val, _) = queue_recv.recv_front(BLOCK).unwrap(); | ||
println!("Got additional {val} from ISR!"); | ||
} | ||
|
||
val = val.wrapping_add(1); | ||
} | ||
} |
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.
Nit - as per my comment in the main PR thread, this is just not necessary. Just borrowing the queues with e.g.
&queue_send
and&queue_recv
is good enough.