Comments (8)
It seems the out_toggle
implementation between static pins and dynamic pins differ:
Static Pin
avr-hal/avr-hal-generic/src/port.rs
Lines 723 to 725 in c1b78e9
Dynamic Pin
avr-hal/avr-hal-generic/src/port.rs
Lines 645 to 651 in c1b78e9
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.
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.
from avr-hal.
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.
@LuigiPiucco, writing a single 1 bit into the PIN register toggles the pin:
That's why I was suggesting that the problem is writing to PORT instead of PIN. :)
from avr-hal.
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.
We should use the "write bit to PIN reg to toggle" trick. Two reasons:
- It's faster than a read-modify-write cycle
- 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.
@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)
- Panic while trying to write to serial using `ufmt::uwriteln` HOT 4
- Add configurable board options to ravedude HOT 3
- Get pin number HOT 6
- Program freezes if compiled in debug mode.
- ravedude should check rust target against selected MCU HOT 3
- Document clock type design in more detail HOT 1
- cargo build - error[E0658] - proc_macro::Literal::byte_character(byte) HOT 9
- can't find crate for `core` HOT 12
- cargo build - error[E0658] - proc_macro::Literal::byte_character(byte) HOT 4
- Very weird error when compiling a very basic program: __addsf3 multiple defenitions HOT 8
- Ravedude freezes on programmer uploading HOT 5
- Arduino Nano: avrdude error: programmer is not responding HOT 11
- Watchdog intterupt mode HOT 1
- [Solved] Cannot find `pwm` in `embedded_hal` HOT 2
- peripherals type alias HOT 2
- Enhance `avr_hal_generic::renamed_pins! {}` to also generate type aliases for each pin
- tier 3 target and broken
- Utilising the `atmega2560`'s "USART in SPI Mode" HOT 10
- Issue with math calculations with floats to serial port output HOT 4
- Incorrect ADC readings after toolchain bump HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from avr-hal.