This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Set and clear GPIO pins at the same time

Hey,

currently I use nrf_gpio_pins_set() and and nrf_gpio_pins_clear() from nrf_gpio.h to set and clear specific GPIO ports, respectively. If I want to change the current state of -say- 4 GPIO ports at once (some of them may be cleared, some of them may be set), how do I do that? It would be important that the change on all pins happens at the same time.

Any ideas? Thanks, NewtoM

Parents
  • Hi NewtoM

    If you want to do this you probably have to bypass the nrf_gpio functions, and write directly to the GPIO registers. Fortunately it isn't as scary as it sounds ;)

    To set/clear many pins at once you can write to the OUTSET or OUTCLR registers in the NRF_GPIO peripheral.

    If you want to set and clear many pins at once you have to use the OUT register.

    Say you want to set pins P0.08-P0.011 to 0xA you could do it like this:

    pin_mask = 0xF << 8;
    NRF_GPIO->OUT = (NRF_GPIO->OUT & ~pin_mask) | (0xA << 8);
    

    You can still use the nrf_gpio driver to configure the outputs, and set/clear individual pins.

    -Torbjørn

  • Thanks RK, thanks Torbjørn,

    now I see the idea. If I look at the nrf_gpio_pins_toggle() function, I see how the GPIO->OUT register state read. Is there anything I should be aware of when directly reading/writing the OUT register?

    And can I accept both answers? :)

    NewtoM

Reply Children
No Data
Related