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

Can anybody please explain the blinky program in nrf51 examples?

this is the main program!

#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "boards.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);
        }
    }
}

The definition for LEDS INVERT is given in board.h

#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)

Can any1 explain this definition???

Parents
  • A '1' in the 32-bit number you pass to NRF_GPIO->OUTSET will set the corresponding bit in the NRF_GPIO->OUT register, a '0' will have no effect. Similarly, a '1' in the OUTCLR register will clear the bit, '0' will have no effect.

    Let's say you want to toggle NRF_GPIO->OUT[0] and NRF_GPIO->OUT[2], and [0] is '1' and [2] is '0' (using only 4 bits to keep it simple):

    NRF_GPIO->OUT: 0b0001 // initial state of the register
    

    .

    LEDS_INVERT(0b0101);  // pass the mask of the LEDs you want to invert
    

    LEDS_INVERT() does this:

    uint32_t gpio_state = NRF_GPIO->OUT;  // Reads the state of NRF_GPIO->OUT
                                          // It gets the value 0b0001
    
    NRF_GPIO->OUTSET = ((leds_mask) & ~gpio_state);  // 0b0101 & ~0b0001
                                                     // = 0b0101 & 0b1110 = 0b0100
                                                     // It sets NRF_GPIO->OUT[2] to 1 leaving the other bits
    
    NRF_GPIO->OUTCLR = ((leds_mask) & gpio_state);   // 0b0101 & 0b0001 = 0b0001
                                                     // It clears NRF_GPIO->OUT[0] to 0 leaving the other bits
    

    .

    NRF_GPIO->OUT: 0b0100 // resulting state of the register
    

    And the reason why this is put in a do {...} while(0) statement, you can read about here: stackoverflow.com/.../257425

Reply
  • A '1' in the 32-bit number you pass to NRF_GPIO->OUTSET will set the corresponding bit in the NRF_GPIO->OUT register, a '0' will have no effect. Similarly, a '1' in the OUTCLR register will clear the bit, '0' will have no effect.

    Let's say you want to toggle NRF_GPIO->OUT[0] and NRF_GPIO->OUT[2], and [0] is '1' and [2] is '0' (using only 4 bits to keep it simple):

    NRF_GPIO->OUT: 0b0001 // initial state of the register
    

    .

    LEDS_INVERT(0b0101);  // pass the mask of the LEDs you want to invert
    

    LEDS_INVERT() does this:

    uint32_t gpio_state = NRF_GPIO->OUT;  // Reads the state of NRF_GPIO->OUT
                                          // It gets the value 0b0001
    
    NRF_GPIO->OUTSET = ((leds_mask) & ~gpio_state);  // 0b0101 & ~0b0001
                                                     // = 0b0101 & 0b1110 = 0b0100
                                                     // It sets NRF_GPIO->OUT[2] to 1 leaving the other bits
    
    NRF_GPIO->OUTCLR = ((leds_mask) & gpio_state);   // 0b0101 & 0b0001 = 0b0001
                                                     // It clears NRF_GPIO->OUT[0] to 0 leaving the other bits
    

    .

    NRF_GPIO->OUT: 0b0100 // resulting state of the register
    

    And the reason why this is put in a do {...} while(0) statement, you can read about here: stackoverflow.com/.../257425

Children
No Data
Related