-
Notifications
You must be signed in to change notification settings - Fork 91
Update button-interrupt project #219
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
Merged
SergioGasquez
merged 4 commits into
esp-rs:main
from
SergioGasquez:feat/update-interrupt
Nov 27, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,70 +1,36 @@ | ||
// Reference: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/freertos.html | ||
|
||
use anyhow::Result; | ||
use esp_idf_svc::sys::{ | ||
esp, gpio_config, gpio_config_t, gpio_install_isr_service, gpio_int_type_t_GPIO_INTR_POSEDGE, | ||
gpio_isr_handler_add, gpio_mode_t_GPIO_MODE_INPUT, xQueueGenericCreate, xQueueGiveFromISR, | ||
xQueueReceive, QueueHandle_t, ESP_INTR_FLAG_IRAM, | ||
use esp_idf_svc::hal::{ | ||
gpio::{InterruptType, PinDriver, Pull}, | ||
peripherals::Peripherals, | ||
task::notification::Notification, | ||
}; | ||
use std::ptr; | ||
|
||
// This `static mut` holds the queue handle we are going to get from `xQueueGenericCreate`. | ||
// This is unsafe, but we are careful not to enable our GPIO interrupt handler until after this value has been initialised, and then never modify it again | ||
static mut EVENT_QUEUE: Option<QueueHandle_t> = None; | ||
|
||
#[link_section = ".iram0.text"] | ||
unsafe extern "C" fn button_interrupt(_: *mut core::ffi::c_void) { | ||
xQueueGiveFromISR(EVENT_QUEUE.unwrap(), std::ptr::null_mut()); | ||
} | ||
use std::num::NonZeroU32; | ||
|
||
fn main() -> Result<()> { | ||
const GPIO_NUM: i32 = 9; | ||
esp_idf_svc::sys::link_patches(); | ||
|
||
let peripherals = Peripherals::take()?; | ||
|
||
// Configures the button | ||
let io_conf = gpio_config_t { | ||
pin_bit_mask: 1 << GPIO_NUM, | ||
mode: gpio_mode_t_GPIO_MODE_INPUT, | ||
pull_up_en: true.into(), | ||
pull_down_en: false.into(), | ||
intr_type: gpio_int_type_t_GPIO_INTR_POSEDGE, // Positive edge trigger = button down | ||
}; | ||
let mut button = PinDriver::input(peripherals.pins.gpio9)?; | ||
button.set_pull(Pull::Up)?; | ||
button.set_interrupt_type(InterruptType::PosEdge)?; | ||
|
||
// Queue configurations | ||
const QUEUE_TYPE_BASE: u8 = 0; | ||
const ITEM_SIZE: u32 = 0; // We're not posting any actual data, just notifying | ||
const QUEUE_SIZE: u32 = 1; | ||
// Configures the notification | ||
let notification = Notification::new(); | ||
let notifier = notification.notifier(); | ||
|
||
// Safety: make sure the `Notification` object is not dropped while the subscription is active | ||
unsafe { | ||
// Writes the button configuration to the registers | ||
esp!(gpio_config(&io_conf))?; | ||
|
||
// Installs the generic GPIO interrupt handler | ||
esp!(gpio_install_isr_service(ESP_INTR_FLAG_IRAM as i32))?; | ||
|
||
// Instantiates the event queue | ||
EVENT_QUEUE = Some(xQueueGenericCreate(QUEUE_SIZE, ITEM_SIZE, QUEUE_TYPE_BASE)); | ||
|
||
// Registers our function with the generic GPIO interrupt handler we installed earlier. | ||
esp!(gpio_isr_handler_add( | ||
GPIO_NUM, | ||
Some(button_interrupt), | ||
std::ptr::null_mut() | ||
))?; | ||
button.subscribe(move || { | ||
notifier.notify_and_yield(NonZeroU32::new(1).unwrap()); | ||
SergioGasquez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
})?; | ||
} | ||
|
||
// Reads the queue in a loop. | ||
loop { | ||
unsafe { | ||
// Maximum delay | ||
const QUEUE_WAIT_TICKS: u32 = 1000; | ||
|
||
// Reads the event item out of the queue | ||
let res = xQueueReceive(EVENT_QUEUE.unwrap(), ptr::null_mut(), QUEUE_WAIT_TICKS); | ||
|
||
// If the event has the value 0, nothing happens. if it has a different value, the button was pressed. | ||
if res == 1 { | ||
println!("Button pressed!") | ||
} | ||
} | ||
// enable_interrupt should also be called after each received notification from non-ISR context | ||
button.enable_interrupt()?; | ||
notification.wait(esp_idf_svc::hal::delay::BLOCK); | ||
println!("Button pressed!"); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,62 +1,34 @@ | ||
// Reference: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/freertos.html | ||
use anyhow::Result; | ||
use esp_idf_svc::sys::{ | ||
esp, esp_random, gpio_config, gpio_config_t, gpio_install_isr_service, | ||
gpio_int_type_t_GPIO_INTR_POSEDGE, gpio_isr_handler_add, gpio_mode_t_GPIO_MODE_INPUT, | ||
xQueueGenericCreate, xQueueGiveFromISR, xQueueReceive, QueueHandle_t, ESP_INTR_FLAG_IRAM, | ||
use esp_idf_svc::{ | ||
hal::{ | ||
gpio::{InterruptType, PinDriver, Pull}, | ||
peripherals::Peripherals, | ||
task::notification::Notification, | ||
}, | ||
sys::esp_random, | ||
}; | ||
use std::ptr; | ||
|
||
// These imports are needed for part 2. | ||
use rgb_led::{RGB8, WS2812RMT}; | ||
|
||
// 4. Create a `static mut` that holds the queue handle. | ||
static mut EVENT_QUEUE: Option<QueueHandle_t> = None; | ||
|
||
// 6. Define what the interrupt handler does, once the button is pushed. Button_interrupt sends a message into the queue. | ||
#[link_section = ".iram0.text"] | ||
unsafe extern "C" fn button_interrupt(_: *mut core::ffi::c_void) { | ||
xQueueGiveFromISR(EVENT_QUEUE.unwrap(), std::ptr::null_mut()); | ||
} | ||
use std::num::NonZeroU32; | ||
|
||
fn main() -> Result<()> { | ||
const GPIO_NUM: i32 = 9; | ||
|
||
// 1. Add GPIO configuration C struct | ||
// let io_conf = gpio_config_t { | ||
// ... | ||
// }; | ||
esp_idf_svc::sys::link_patches(); | ||
|
||
// Queue configurations | ||
const QUEUE_TYPE_BASE: u8 = 0; | ||
const ITEM_SIZE: u32 = 0; | ||
const QUEUE_SIZE: u32 = 1; | ||
let peripherals = Peripherals::take()?; | ||
|
||
unsafe { | ||
// 2. Write the GPIO configuration into the register | ||
// esp!(...)?; | ||
|
||
// 3. Install the global GPIO interrupt handler | ||
// esp!(...)?; | ||
// 1. Configure the button using PinDriver | ||
// let mut button = PinDriver... | ||
|
||
// 4. Create an event queue | ||
// EVENT_QUEUE = Some(...); | ||
// 2. Instantiate a new notification and notifier | ||
|
||
// 5. Add the button GPIO and the function to the interrupt handler | ||
// esp!(...)?; | ||
unsafe { | ||
// 3. Create a subscription and its callback function that notifies and yields. | ||
} | ||
|
||
// The loop in main waits until it gets a message through the rx ("receiver") part of the channel | ||
loop { | ||
unsafe { | ||
// Maximum delay | ||
const QUEUE_WAIT_TICKS: u32 = 1000;; | ||
|
||
// 6. Receive the event from the queue. | ||
// let res = ...; | ||
|
||
// 7. Handle the value of res. | ||
// ... | ||
// 4. Enable the interrupt for the button | ||
// 5. Wait for notification using `esp_idf_svc::hal::delay::BLOCK` | ||
// 6. Print a "button pressed" message | ||
} | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.