Setting Up GPIO Interrupts in nRF52xx
Image for post

Prerequisites

This is tutorial is not intended to be a guide for learning C language or about the Nordic SDK platform. It’s primary target is to provide developers a concise guide about integrating peripheral modules and features into active applications.

If you are a beginner, I would recommend you look into an nRF52 Project Setup guide like this one.

https://medium.com/vicara-hardware-university/nrf52-project-setup-with-segger-embedded-system-f64958052a2d

Another easy way to get started with coding, without bothering with all basic stuff like files and driver inclusion, check out this Code Generation Tool

nrf52 Code Generator: https://vicara.co/nrf52-code-generator

GPIOTE

GPIOTE is a method to execute a section of code or steps when there is a specific input at the GPIO pin. This is different from the normal inputs as interrupts stop the current executing code, to execute an interrupt process before resuming regular execution.

Image for post

Implementing SPI in nRF52832

In the following section, I will provide a guide as it has been tested in the nRF52832 Dev Kit. However, the same structure will remain common across all nRF52 devices.

Including correct Headers

Image for post

Include the SPI Driver files. This file should be titled, nrf_drv_gpiote.c It is at SDK/integration/nrfx/legacy folder.

Update sdk_config.h File

  • GPIOTE_ENABLED has to be set to set to 1.
  • GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS has to be set to the number of pins which have are configured as low accuracy

In main.c File

Header File

#include "nrf_drv_gpiote.h"Callback Functionstaticvoidin_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{//Callback function when the interrupt is triggered

}

Note: There can be one common callback function for all pins or a separate callback function for each pin.

Init Function

staticvoidgpiote_init()
{//VCE: This block is a one time configuration

ret_code_t err_code;
if(!nrf_drv_gpiote_is_init())
   {
       err_code = nrf_drv_gpiote_init();
       APP_ERROR_CHECK(err_code);
   }

//VCE: The below block needs to be called for each pin
nrf_drv_gpiote_in_config_t in_config_1;
   in_config_1.pull = NRF_GPIO_PIN_PULLUP; //User defined
   in_config_1.sense = GPIOTE_CONFIG_POLARITY_Toggle; //User defined
   in_config_1.hi_accuracy = true; //User defined
   in_config_1.is_watcher = false; //Don't change this
   in_config_1.skip_gpio_setup = false; //Don't change this

//VCE: Configuring
   err_code = nrf_drv_gpiote_in_init(13, &in_config_1, in_pin_handler);
   APP_ERROR_CHECK(err_code);

   nrf_drv_gpiote_in_event_enable(13, true);
}

Note: Do not change is_watcher and skip_gpio_setup in nrf_drv_gpiote_in_config_t. The rest of the parameters are user defined.

Add gpiote_init() to main()

gpiote_init();

Conclusion

With the above steps anyone can easily get started with incorporating GPIOTE.

NOTE

There is another easier method to initialize and auto-generate code for nRF52. This tool, will handle all library additions and code generations for a variety of peripherals like SPI, I2C, UART etc.

Link: https://vicara.co/nrf52-code-generator