Coder Social home page Coder Social logo

Comments (8)

Rahix avatar Rahix commented on July 30, 2024

It seems the out_toggle implementation between static pins and dynamic pins differ:

Static Pin

unsafe fn out_toggle(&mut self) {
(*<$port>::ptr()).[<pin $name:lower>].write(|w| w.bits(1 << $pin))
}

Dynamic Pin

unsafe fn out_toggle(&mut self) {
match self.port {
$(DynamicPort::[<PORT $name>] => (*<$port>::ptr()).[<port $name:lower>].write(|w| {
w.bits(self.mask)
}),)+
}
}

The dynamic pin impl is writing to the PORT register instead of the PIN register. This seems to be a regression from commit 063f845. Paging @LuigiPiucco :)

@gregoryholder, can you change [<port $name:lower>] to [<pin $name:lower>] in L647 and test whether that fixes the problem?

from avr-hal.

LuigiPiucco avatar LuigiPiucco commented on July 30, 2024

This seems a bit strange, at least upon first glance. The dynamic version is not working, but (in theory) the one that's wrong is the static one (out methods should write to port, not pin, since pin is for reading when input, no?). Also, I notice some of these use .write and others .modify. I didn't pay much mind to it before (just copy-pasted) but maybe it's similar to #494, where the former causes unintended consequences. I'll look into it and take this opportunity to apply a usage comment like the one for the ADC. Just note I don't have an Uno right now, so I'll try to reproduce on the Mega2560. If I can't I'll ask @gregoryholder to check the fix when I finish.

from avr-hal.

gregoryholder avatar gregoryholder commented on July 30, 2024

from avr-hal.

LuigiPiucco avatar LuigiPiucco commented on July 30, 2024

I'm reasonably sure on the solution (here), but I can't test right now, so I'll wait before opening a PR.

Reading through #284 I am a bit confused, and I think the source of this bug was similar confusion about the behavior of registers in Rust. The code of out_toggle before the mentioned issue was:

$<port pointer wrapper>.modify(|r, w| {
    w.bits(r.bits() | self.mask) // (1 << $pin_num) as the mask for the static version
})

... which, looking closely, is what out_set expands to now. And doing some truth table analysis, this can only ever set the bit, but never clear it. For toggle, we want something that does both, kind of like this:

match cur_bit {
    0b1 => 0b0, // Clear
    0b0 => 0b1, // Set
}

The proposed solution was:

$<port pointer wrapper>.write(|w| w.bits(self.mask))

... which could work, if the behavior of .write was to flip from the port every bit where the mask is 1. But I'm pretty sure it just writes whatever you pass unchanged, including the 0s. The only exception I know is if you use single field access instead of a bitmask, when it assumes the reset value for anything you don't touch. So we do need .modify, at least to keep other bits unmodified.

Another issue is what function to use between the current value and the mask to toggle the one bit, keeping others. The answer is an XOR. I think in this case, the notation of C makes thinks a bit more evident:

PORTD |= 1 << 3;    // Set bit 3
PORTD &= ~(1 << 3); // Clear bit 3
PORTD ^= 1 << 3;    // Toggle bit 3, for every 0 in the mask it does not change PORTD, but for the 1 it flips

So we end up with:

$<port pointer wrapper>.modify(|r, w| {
    w.bits(r.bits() ^ self.mask)
})

Please double check the logic here, it does seem easy to get wrong. There's also some analysis of assembly in #284, but due to the reasons above I think it doesn't apply. I can check the output of my suggestion if needed, though.

from avr-hal.

Rahix avatar Rahix commented on July 30, 2024

@LuigiPiucco, writing a single 1 bit into the PIN register toggles the pin:

image

That's why I was suggesting that the problem is writing to PORT instead of PIN. :)

from avr-hal.

LuigiPiucco avatar LuigiPiucco commented on July 30, 2024

Ok, I did not know that. That's an interesting piece of information. However, unlike in #284, the compiler never produces sbi for me (not even in the static case). It's always ldi-out. The XOR code as I suggested compiles down to in-modify-out on the PORTx register. Without this toggle feature on the PINx register, I think that's as good as we'd get. But since it exists, we can rule out the in and eor. Do you prefer to change to that or keep the more recognizable XOR approach? Also, I think the static case may be possible to optimize with assembly (guaranteeing it translates to sbi), but not the dynamic one, since sbi encodes the bit and register into the opcode.

from avr-hal.

Rahix avatar Rahix commented on July 30, 2024

We should use the "write bit to PIN reg to toggle" trick. Two reasons:

  1. It's faster than a read-modify-write cycle
  2. It's atomic. The read-modify-write cycle of writing to PORT would require to be put in a critical section to avoid a nasty race condition. This would be even more overhead.

However, unlike in #284, the compiler never produces sbi for me (not even in the static case). It's always ldi-out.

I assume you were testing the current implementation? That one indeed cannot be optimized to sbi anymore. sbi is emitted for a static version of the read-modify-write cycle. But we didn't want to rely on this optimization for correctness, so we decided to move to the current approach which leads to ldi-out. It matters for correctness for the same race-condition mentioned above.

from avr-hal.

LuigiPiucco avatar LuigiPiucco commented on July 30, 2024

@Rahix I updated my branch and I am writing the pull request with details. I deviated from just changing port to pin, so I'll comment here if you want to start checking it before I write the detailed explanation. I wrote it in a way that the optimal single sbi/cbi is always generated for setting and toggling, even in lower optimization levels. It's also "safer" (in the sense that it uses less unsafe {}s). The getters generate more code than the setters, but I think comparing to the prior implementation there's negative balance. But maybe there's something I'm missing, please check carefully. I also only looked at the bytecode, not real execution.

from avr-hal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.