stm32f3-discovery icon indicating copy to clipboard operation
stm32f3-discovery copied to clipboard

Simplify Button interrupt logic.

Open andy31415 opened this issue 3 years ago • 0 comments

I was looking to learn how to use my STM32F3Discovery board by looking over the stm32f3-discovery implementation and attempted to implement my own button interrupt (on PB1 using EXTI1). I encountered some difficulty because:

  • EXTICR1 changes did not seem to take effect (always 0) and eventually it turned out that SYSCFGEN has to be set before changes take effect. Examples in the repo work because PA is the default in that registry (due to 0-initialization)
  • I found the usage of set_bit() and unsafe bits() calls a bit harder to adapt, so I updated the code to use unmasked/pa0/enable

I am learning this stuff, so feedback would be highly appreciated. Thank you!

Actual changes from the log:

  1. SYSCFG needs to be constrained. The reason is that constrain looks like
 fn constrain(self, apb2: &mut APB2) -> SysCfg {
        apb2.enr().modify(|_, w| w.syscfgen().enabled());
        ///....

which enables the SYSCFG. Without it, SYSCFG.exticr1 changes will not take effect (everything will still be 0s), which is not noticeable for the user button because it just happesn that PA0 config is the default, however for generic code consistency it seems we want to set a usable example

  1. Try to use the seemingly clearer/safer alternatives when setting IMR1/RTSR1/FTSR1/EXTICR1:
  • use enable and unmasked instead of set_bit
  • use pa0() instead of a custom value write, which also removes the need for an unsafe block.

andy31415 avatar Dec 22 '21 04:12 andy31415