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

What is the use of the OUT register of GPIO in nRF52?

In the very first blinky example from the nRF52_SDK_0.9.1:

#include "stdint.h"  
 #include "stdint.h"  
 #include "nrf_delay.h"  
 #include "nrf_gpio.h"  
 #include "boards.h"  
 #include "nrf.h"  

const uint8_t leds_list[LEDS_NUMBER] = LEDS_LIST;  

/**
 * @brief Function for application main entry.
 */  
int main(void)  
{  
    // Configure LED-pins as outputs.  
    LEDS_CONFIGURE(LEDS_MASK);  
    // Toggle LEDs.  
    while (true)  
    {  
        for (int i = 0; i < LEDS_NUMBER; i++)  
        {  
            LEDS_INVERT(1 << leds_list[i]);  
            nrf_delay_ms(500);  
        }  
    }  
}  

For this one:

LEDS_INVERT(1 << leds_list[i]);  

In the board.h you could find the #define here:

#define LEDS_INVERT(leds_mask) do { uint32_t gpio_state = NRF_GPIO->OUT;      \  
                              NRF_GPIO->OUTSET = ((leds_mask) & ~gpio_state); \  
                              NRF_GPIO->OUTCLR = ((leds_mask) & gpio_state); } while (0)  

The first line means that, give the value in the OUT register of the GPIO to the variable gpio_state.
You could find the related #define in the nrf52.h and nrf51_to_nrf5.h
And these are the result I print out about the gpio_state value:
gpio_state is: 20060
gpio_state is: 60060
gpio_state is: e0060
gpio_state is: 1e0060
gpio_state is: 1c0060
gpio_state is: 180060
gpio_state is: 100060
gpio_state is: 60
gpio_state is: 20060
gpio_state is: 60060
...

Here is my question: which part of this function changes the value in the OUT register of GPIO? Why it would be change like this way?
Thank you all.

  • The OUTSET and OUTCLR change the values in the OUT register of GPIO. This is a standard pattern repeated all over the nrf51 and nrf52 series, an xxx, an xxxSET and an xxxCLR register to read/write, set and clear bits in the 'xxx' register. this is documented very well in the nRF51 series reference guide.

    Please when you post code use the 'post code' button, otherwise the message is very hard to read.

  • One more comment, the OUT register can also be used to write to the GPIO pins. However, it's better to use the *SET and *CLR register because writing to OUT affect all the pins, when with *SET AND *CLR we can choose the pin(s) to modify without touching the others.

  • Thank you so much. It helps me a lot. It's my first time to ask question here. So... XD I will do that next time. Thanks both of you again.

  • Hi Ljyun, Where can we find the definition of LEDS_LIST and declaration of LEDS_NUMBER?

  • Sowmya, LEDS_LIST and LEDS_NUMBER are defined in the pca10036.h file