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

How do I use the GPIO pin on nrf52 devices?

Hello, just a few quick short questions regarding nrf52 devices:

  1. Do I have to configure and initialize them before using?

  2. Which function should I use? I'm going with nrf_gpio_pin_write, hope its alright.

  3. The most important one: I'm using QIAA package, and I want to operate P22 pin or pin 1.07, how do I do that? Should I write:

    nrf_gpio_pin_write(22, 1)

or

nrf_gpio_pin_write(103,1)

???

Edit: Make sure you use nrf_gpio_cfg_output properly first, before setting and resetting target pins.

  • For P0.22:

    nrf_gpio_pin_write(22, 1)
    

    should be correct.

    For P1.07:

    nrf_gpio_pin_write(39, 1)
    

    where 32+7 = 39.

    You can also use the NRF_GPIO_PIN_MAP macro which is defined as:

    #define 	NRF_GPIO_PIN_MAP(port, pin)   ((port << 5) | (pin & 0x1F))
    // Macro for mapping port and pin numbers to values understandable for nrf_gpio functions. 
    
  • Don't you need to call

     nrf_gpio_cfg_output(PIN_NUMBER]);
    

    first?

  • @koniho Yes I used this:

    You can also use the NRF_GPIO_PIN_MAP macro which is defined as:

    And @ Roger Clark I called that first, but it didn't work, as I feared most.

    Is their any register magic possible? like hypothetically this:

    GPIO_P1->Output_Lat=0x0080; ???

  • Come on, no function from components\drivers_nrf\hal\nrf_gpio.h have ever let me down. Simply init PIN or range as output by

    nrf_gpio_cfg_output
    

    or

    nrf_gpio_range_cfg_output(...)
    

    and then set it high or low by one of functions

    nrf_gpio_pin_set(...)
    nrf_gpio_pin_clear(...)
    
  • You are right, I tried to use my mock-up version of the pin map macro function, and there was an error in it. nrf_gpio_cfg_output is the way to go. Once that got done right, everything is AOK.

1 2