I have been working through an analysis of timing specific words such as mset vs asm-based words and I've come to realize this....I'm not sure what to call it as its not an issue, it is more of an artifact.
From my blog...
As for the toggle issue, this is easy to confirm. Enter the following in your serial console and watch the values for the pins change:
BIT4ddrd2constantD4\ Board ConnectorBIT5ddrd2constantD5\ Board Connector:tog( bit port -- )1-mset;\ toggle output valueD4tog\ D4 goes high, D5 no changeD5tog\ D5 goes high, D4 goes lowD4tog\ D4 goes high, D5 goes low
This is an artifact of using the mset word in FlashForth. The mset word will read the address, then or it with the bit mask. If a pin has been toggled, a 1 has been written to the PINn register and when read, it will read high, this gets or'd with the mask, toggling its value as well as the desired value.
Based on our previous discussions, I will probably use an asm word which wouldn't suffer from this artifact, however, its interesting to note. I was previously under the belief that using the mask would ensure only the desired bit is changed. In testing, I've come to realize any bit (on the same port) which has been previously toggled, will toggle again because its bit in the PIN port will be read back as a 1.
As I said, I don't see it as a bug or an issue, simply as an artifact of how the AVR family toggles pins.
Thoughts?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
As I understand, in order to read a pin it must be done from the PINx register.
And to set pin high or low you have to write to the PORTx register.
MSET and MCLR are designed to work with just one register or memory location so it does not really work with how the PORT/PIN system is designed.
On AVR8 you can toggle a pin by just writing to the PIN register. Like this.
Hi Mikael,
I have been working through an analysis of timing specific words such as mset vs asm-based words and I've come to realize this....I'm not sure what to call it as its not an issue, it is more of an artifact.
From my blog...
As for the toggle issue, this is easy to confirm. Enter the following in your serial console and watch the values for the pins change:
This is an artifact of using the mset word in FlashForth. The mset word will read the address, then or it with the bit mask. If a pin has been toggled, a 1 has been written to the PINn register and when read, it will read high, this gets or'd with the mask, toggling its value as well as the desired value.
Based on our previous discussions, I will probably use an asm word which wouldn't suffer from this artifact, however, its interesting to note. I was previously under the belief that using the mask would ensure only the desired bit is changed. In testing, I've come to realize any bit (on the same port) which has been previously toggled, will toggle again because its bit in the PIN port will be read back as a 1.
As I said, I don't see it as a bug or an issue, simply as an artifact of how the AVR family toggles pins.
Thoughts?
As I understand, in order to read a pin it must be done from the PINx register.
And to set pin high or low you have to write to the PORTx register.
MSET and MCLR are designed to work with just one register or memory location so it does not really work with how the PORT/PIN system is designed.
On AVR8 you can toggle a pin by just writing to the PIN register. Like this.
Of course the inline assembler solution is at least 10 times faster.
Last edit: Mikael Nordman 2024-04-03
Yes, its a mute point given the inline assembler version is significantly faster.
And c! works well for toggling the pin.
I wanted to document it, in case others have a similar results.