Skip to content

Adds some simple methods to Timer. #52

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
merged 7 commits into from
Mar 29, 2019

Conversation

thalesfragoso
Copy link
Member

Very simple methods but potentially useful, especially the one to clear the update interrupt flag.
I was having trouble using the timer with interrupts, because the Timer takes ownership of the registers and the tim field is private.
Sorry if I got something wrong, I am kinda new to rust.

@therealprof
Copy link
Member

Looks okay to me but there's no need to release Clocks since it's a copyable resource.

@thalesfragoso
Copy link
Member Author

Looks okay to me but there's no need to release Clocks since it's a copyable resource.

It makes sense, I did not realize that before. I have changed that bit in the new commit.

src/timer.rs Outdated
self.tim.disable_counter();
}

/// Releases the SYST and clocks
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Releases the SYST and clocks
/// Releases the SYST

src/timer.rs Outdated

/// Releases the TIM Peripheral and clocks
pub fn release(self) -> $TIMX {
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
Copy link
Member

Choose a reason for hiding this comment

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

Can't we simply call stop() from above?

Copy link
Member

@therealprof therealprof left a comment

Choose a reason for hiding this comment

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

Please also add an entry to CHANGELOG.md.

@TheZoq2 Any comments from your side?

@TheZoq2
Copy link
Member

TheZoq2 commented Mar 28, 2019

Looks good to me

@thalesfragoso
Copy link
Member Author

I updated the Changelog, don't know if I over-explained it, feel free to modify.

@TheZoq2
Copy link
Member

TheZoq2 commented Mar 28, 2019

Looks good to me, though I did think of another improvement which would be nice. Could you replace the already existing calls to uif.clear_bit() with the new clear_uif function?

Also, I would argue that calling that function clear_update_interrupt might be more intuitive than clear_uif

@thalesfragoso
Copy link
Member Author

Looks good to me, though I did think of another improvement which would be nice. Could you replace the already existing calls to uif.clear_bit() with the new clear_uif function?

Also, I would argue that calling that function clear_update_interrupt might be more intuitive than clear_uif

How about clear_update_interrupt_flag ? Flag is a very common word and would be the first one I would search for in a function to do this procedure.

@TheZoq2
Copy link
Member

TheZoq2 commented Mar 28, 2019

Yea, that is probably even better!

Copy link
Contributor

@TeXitoi TeXitoi left a comment

Choose a reason for hiding this comment

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

Great work, will use it as soon as it's published!

CHANGELOG.md Outdated
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Add feature for using STM32F101 chip
- Add gpio pins corresponding to LQFP-100 package
- Implement `core::fmt::Write` for `serial::Tx`
- Add methods `stop`, `release` and `clear_uif` to `Timer` (`clear_uif` only on `Timer<TIMX>`)
Copy link
Contributor

Choose a reason for hiding this comment

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

unsync

src/timer.rs Outdated

/// Releases the SYST
pub fn release(mut self) -> SYST {
self.tim.disable_counter();
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: better to self.stop() as any bug on stop must be propagated here.

@thalesfragoso
Copy link
Member Author

I will modify the code to use the new methods as proposed and I think I will write an example for the Timer interrupt with rtfm and add it to the PR.
I will probably do it over the weekend.

@thalesfragoso
Copy link
Member Author

I ended up getting some spare time today, so there you go. Sorry for the bunch of commits, but I guess you gonna squash them all anyways.
I tested the example on a bluepill and it worked as expected.

@TheZoq2
Copy link
Member

TheZoq2 commented Mar 29, 2019

Thanks, looks good to me!

@therealprof What's our policy on adding dependencies for examples, #2 didn't really come to a conclusion. I believe that rtfm was originally removed because it didn't compile on stable, but that's solved now. I'm fine with having rtfm as a dependency since it's a pretty common crate in the embedded rust world, but at the same time, adding a bunch of dependencies might be a bad idea for compilation times and security.

@thalesfragoso
Copy link
Member Author

I kinda understand the dependencies for examples thing, but in the present scenario I would think that it is somewhat bad to push newcomers to implement interrupts (with handlers and stuff) without rtfm.

@therealprof
Copy link
Member

@TheZoq2 I'm generally not too hot on having examples in this crate (apart from in the docs). Ideally we should have BSP crates which go nuts on examples. I do have a a basic https://github.com/therealprof/nucleo-f103rb crate, if people are interested I could make this free for all, or just create a new blue pill crate.

However for now it's okay for me to just add back the RTFM dependency.

@therealprof
Copy link
Member

@thalesfragoso The problem is that a universal HAL crate is somewhat tricky to use anyway, especially for beginners; so instead of dragging newcomers into examples in here we really should demonstrate how to get properly started with your own projects by having a proper BSP crate.

@TheZoq2
Copy link
Member

TheZoq2 commented Mar 29, 2019

Yea, eventually we should probably take care of this example stuff, and a BSP with examples does sound like a good idea. I do agree that having a RTFM example for interrupts is much better than one without RTFM :)

I guess all that's left to do then is to merge this, thanks for the help! @thalesfragoso

@TheZoq2 TheZoq2 merged commit 9f95056 into stm32-rs:master Mar 29, 2019
resources.LED.set_high();
*resources.LED_STATE = false;
} else {
resources.LED.set_low();
Copy link
Contributor

Choose a reason for hiding this comment

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

You can call resources.LED.toggle(); and remove LED_STATE

Copy link
Member Author

@thalesfragoso thalesfragoso Mar 29, 2019

Choose a reason for hiding this comment

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

Does PC13<Output<MODE>> have toggle ? I remember looking at the docs when I was writing other code and could not find it.

Copy link
Contributor

Choose a reason for hiding this comment

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


loop {
// Waits for interrupt
wfi();
Copy link
Contributor

Choose a reason for hiding this comment

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

You can just not define an idle() function.

Copy link
Member Author

Choose a reason for hiding this comment

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

Does it automatically call wfi() ?

Copy link
Contributor

Choose a reason for hiding this comment

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

https://japaric.github.io/cortex-m-rtfm/book/en/by-example/app.html#idle

When no idle function is declared, the runtime sets the SLEEPONEXIT bit and then sends the microcontroller to sleep after running init.

@thalesfragoso thalesfragoso deleted the timer-upgrade branch June 11, 2019 01:26
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.

4 participants