[riot-notifications] [RIOT-OS/RIOT] cpu/atmega_common: pseudomodule-based pin change interrupt implementation (#11122)

Marian Buschsieweke notifications at github.com
Wed Jul 24 11:46:27 CEST 2019

maribu commented on this pull request.

> +        if (enabled_pcints & 0x1) {
+            /* re-construct mask from pin number*/
+            uint8_t pin_mask = (1 << pin_num);
+            /* get pin from mapping (assumes 8 entries per bank!) */
+            gpio_t pin = pcint_mapping[ bank * 8 + pin_num ];
+            uint8_t port_value = (_SFR_MEM8(_pin_addr( pin )));
+            uint8_t pin_value = ((port_value & pin_mask) != 0);
+            uint8_t old_state = ((pcint_state[ bank ] & pin_mask) != 0);
+            gpio_isr_ctx_pcint_t *conf = &pcint_config[ bank * 8 + pin_num ];
+            if (old_state != pin_value) {
+                if (pin_value) {
+                    pcint_state[ bank ] |= (pin_mask);
+                }
+                else {
+                    pcint_state[ bank ] &= ~(pin_mask);
+                }

This is only entered if the current value of the pin is different from the value read from the pin differs from the value stored in `pcint_state`.

If `pin_value` is not 0 (then-case of the inner `if`) and also `pin_value` differs from what it is stored (then-case of outer `if`), then the pin is set. If both `then`-branches are taken, the value in `pcint_state` for that bit has to be zero and is not set. This is equivalent to toggling.

if `pin_value` is zero (else-case of inner `if`) and differs from what is stored in `pcint_state` (then-case of outer if), than the bit is clear. But to take the then-branch of the outer `if` and the else-branch of the inner `if`, the corresponding bit had to be `1`. So this is again equivalent to toggling the bit.

In both cases the bit is just toggled, so the code could be written like that to be more efficient. (If you don't believe me: Change it to toggling and run the test ;-))

You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190724/d090707a/attachment.htm>

More information about the notifications mailing list