jkristell / infrared

Infrared remote control library for embedded Rust
Apache License 2.0
56 stars 10 forks source link

Overflow when setting up NecApple EventReceiver at 1_000_000 hz #67

Closed jhillyerd closed 3 years ago

jhillyerd commented 3 years ago

It looks like https://github.com/jkristell/infrared/blob/v0.11.0/src/protocols/utils.rs#L79 overflows with a microsecond sample rate:

   type IRInput = gpiob::PB6<Input<Floating>>;
   type IRRecv = infrared::hal::EventReceiver<infrared::protocols::NecApple, IRInput>;
   // ...
   let ir_recv = infrared::hal::EventReceiver::new(ir_input, 1_000_000);

is panicking on a blackpill stm32f411 board.

0 ERROR panicked at 'attempt to multiply with overflow', C:\Users\james\.cargo\registry\src\github.com-1ecc6299db9ec823\infrared-0.11.0\src\protocols\utils.rs:79:16
└─ panic_probe::print_defmt::print @ C:\Users\james\.cargo\registry\src\github.com-1ecc6299db9ec823\panic-probe-0.2.0\src\lib.rs:94
────────────────────────────────────────────────────────────────────────────────
stack backtrace:
   0: HardFaultTrampoline
      <exception entry>
...
   5: core::panicking::panic_fmt
        at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/core/src/panicking.rs:92:14
   6: core::panicking::panic
        at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/core/src/panicking.rs:50:5
   7: infrared::protocols::utils::InfraRange4::new
   8: <infrared::protocols::nec::receiver::NecReceiverState<C> as infrared::recv::InfraredReceiverState>::create
        at github.com-1ecc6299db9ec823\infrared-0.11.0\src\protocols\nec/receiver.rs:32:9
   9: infrared::recv::InfraredReceiver::receiver_state
        at github.com-1ecc6299db9ec823\infrared-0.11.0\src\recv/mod.rs:18:9
  10: infrared::recv::event::EventReceiver<Protocol>::new
        at github.com-1ecc6299db9ec823\infrared-0.11.0\src\recv/event.rs:18:20
  11: infrared::hal::receiver::EventReceiver<Protocol,Pin>::new
        at github.com-1ecc6299db9ec823\infrared-0.11.0\src\hal/receiver.rs:25:19
  12: rtic_infrared_int::app::init
        at src\bin/rtic-infrared-int.rs:57:23
...
jhillyerd commented 3 years ago

Just tried a sample rate of 100_000: resolved the crash, but seems clunky.

I'd also expect your bluepill interrupt example to crash similarly: https://github.com/jkristell/infrared-examples/blob/master/stm32f103-bluepill/examples/receiver_exti.rs#L63

jkristell commented 3 years ago

Hi

I noticed this as well some time ago when I was trying to use the library with higher timer resolutions.

I have a local branch with some cleanups to this area of the code, just need to find some time to finish it and write some tests. I could probably find some time during the weekend. The main difference from your branch is that I use a loop in the infra_range to find a suitable denominator.

jhillyerd commented 3 years ago

Cool, no urgency on my part, dividing by 10 seems to work well enough for the time being.

jkristell commented 3 years ago

If you have the time, you could try the branch in this pull request: https://github.com/jkristell/infrared/pull/69

The tests pass, but I have not tested it on real hardware.