Skip to content

Commit

Permalink
🐛 fix wrong array indexing logic (#331)
Browse files Browse the repository at this point in the history
* 🐛 fix wrong array indexing logic

* using TIMER_MAX instead of TIMER_GROUP_MAX

* 🐛 wrong TIMER11 index definition

* update CHANGELOG

* rearange indexing to - "[ [T00,T01], [T10, T11]]"
  • Loading branch information
Vollbrecht authored Oct 29, 2023
1 parent 6ecb09b commit bdbba13
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.42.3] - 2023-10-29
* Fix Timer array index bug #331 - prevented the use of TIMER10 on devices that support only 2 Timers
* Fix wrong TIMER11 index definition that declared TIMER11 as TIMER10 #331

## [0.42.2] - 2023-10-28
* Support for latest ESP IDF 5.2 dev (master)

Expand Down
21 changes: 8 additions & 13 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ impl<'d> TimerDriver<'d> {
self.group(),
self.index(),
Some(Self::handle_isr),
(self.group() * timer_group_t_TIMER_GROUP_MAX + self.index())
as *mut core::ffi::c_void,
(self.group() * timer_idx_t_TIMER_MAX + self.index()) as *mut core::ffi::c_void,
0,
)
})?;
Expand Down Expand Up @@ -267,14 +266,12 @@ impl<'d> TimerDriver<'d> {
}

pub fn reset_wait(&mut self) {
let notif =
&PIN_NOTIF[(self.group() * timer_group_t_TIMER_GROUP_MAX + self.index()) as usize];
let notif = &PIN_NOTIF[(self.group() * timer_idx_t_TIMER_MAX + self.index()) as usize];
notif.reset();
}

pub async fn wait(&mut self) -> Result<(), EspError> {
let notif =
&PIN_NOTIF[(self.group() * timer_group_t_TIMER_GROUP_MAX + self.index()) as usize];
let notif = &PIN_NOTIF[(self.group() * timer_idx_t_TIMER_MAX + self.index()) as usize];

notif.wait().await;

Expand All @@ -297,7 +294,7 @@ impl<'d> TimerDriver<'d> {

let callback: Box<dyn FnMut() + Send + 'd> = Box::new(callback);

ISR_HANDLERS[(self.group() * timer_group_t_TIMER_GROUP_MAX + self.index()) as usize] =
ISR_HANDLERS[(self.group() * timer_idx_t_TIMER_MAX + self.index()) as usize] =
Some(unsafe { core::mem::transmute(callback) });

Ok(())
Expand All @@ -310,8 +307,7 @@ impl<'d> TimerDriver<'d> {
self.disable_interrupt()?;

unsafe {
ISR_HANDLERS[(self.group() * timer_group_t_TIMER_GROUP_MAX + self.index()) as usize] =
None;
ISR_HANDLERS[(self.group() * timer_idx_t_TIMER_MAX + self.index()) as usize] = None;
}

Ok(())
Expand Down Expand Up @@ -355,11 +351,10 @@ impl<'d> Drop for TimerDriver<'d> {

#[cfg(feature = "alloc")]
unsafe {
ISR_HANDLERS[(self.group() * timer_group_t_TIMER_GROUP_MAX + self.index()) as usize] =
None;
ISR_HANDLERS[(self.group() * timer_idx_t_TIMER_MAX + self.index()) as usize] = None;
}

PIN_NOTIF[(self.group() * timer_group_t_TIMER_GROUP_MAX + self.index()) as usize].reset();
PIN_NOTIF[(self.group() * timer_idx_t_TIMER_MAX + self.index()) as usize].reset();

esp!(unsafe { timer_deinit(self.group(), self.index()) }).unwrap();
}
Expand Down Expand Up @@ -432,4 +427,4 @@ impl_timer!(TIMER01: timer_group_t_TIMER_GROUP_0, timer_idx_t_TIMER_1);
#[cfg(not(esp32c2))]
impl_timer!(TIMER10: timer_group_t_TIMER_GROUP_1, timer_idx_t_TIMER_0);
#[cfg(any(esp32, esp32s2, esp32s3))]
impl_timer!(TIMER11: timer_group_t_TIMER_GROUP_1, timer_idx_t_TIMER_0);
impl_timer!(TIMER11: timer_group_t_TIMER_GROUP_1, timer_idx_t_TIMER_1);

0 comments on commit bdbba13

Please sign in to comment.