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

Gpio configuration and usage

Hello,

I am using the chip nrf52840. I am trying to configure three pins (P0.09, P0.10 and P0.11) as output pins and I am trying to make set and reset functions for them. I used the gpio_toggle example to help me do it. However, I am not doing something right even though I pretty much just copied the example code into my own project and made a few adjustments. Can some please help find me the problem? No matter what I do, the voltage of the pins on P0.10 and P0.11 is low and on P0.09 is high. The short and simple code is listed below.  Best regards.

#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#define CS_FLASH_MEMORY_PIN (9UL)
#define HOLD_PIN (10UL)
#define WRITE_PROTECT_PIN (11UL)

void configure_pin_direction_as_output(unsigned long pin){

NRF_GPIO->PIN_CNF[pin] = (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) |
                         (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
                         (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
                         (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
                         (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);

}

void set_pin(unsigned long pin){

	 NRF_GPIO->OUTSET = (1UL << pin);

}

void reset_pin(unsigned long pin){

	 NRF_GPIO->OUTCLR = (1UL << pin);

}

void configure_chip_select_pin_of_the_flash_memory(void){

  configure_pin_direction_as_output(CS_FLASH_MEMORY_PIN);

}

void configure_hold_pin_of_the_flash_memory(void){

  configure_pin_direction_as_output(HOLD_PIN);

}

void configure_write_protect_pin_of_the_flash_memory(void){

  configure_pin_direction_as_output(WRITE_PROTECT_PIN);

}

void deactivate_chip_select_pin_of_the_flash_memory(void){

  set_pin(CS_FLASH_MEMORY_PIN);

}


void activate_chip_select_pin_of_the_flash_memory(void){

  reset_pin(CS_FLASH_MEMORY_PIN);

}

void deactivate_hold_pin(void){

	set_pin(HOLD_PIN);

}

void activate_hold_pin(void){

	reset_pin(HOLD_PIN);

}

void deactivate_write_protect_pin(void){

	set_pin(WRITE_PROTECT_PIN);

}

void activate_write_protect_pin(void){

	reset_pin(WRITE_PROTECT_PIN);

}

int main(void)
{	
    
	
		deactivate_chip_select_pin_of_the_flash_memory();
	    deactivate_hold_pin();
		deactivate_write_protect_pin();
	
    while (1)
    {

    }
}

  • Hey,

    The pins P0.09 and P0.10 are by default set as NFC pins on the nRF52840. To use them as GPIOs please see the documentation provided here.

    Best regards,

    Simon

  • Hello Simonr,

    Thank you for the fast reply. First of all i'm sorry, in the previous code I didn't configure the pins as output in the main loop even though I made the functions to do it. Second, I changed the GPIO pins to pins P0.17, P0.18 and P0.19. However, I still can't seem to read high voltage on those pins. This is the new code, it has a very slight difference from the code above. I try to set and reset the three pins every 6 seconds. Any suggestions why it is still not working? I am checking the voltage using my multimeter. 

    Best regards

    #include "nrf_drv_spi.h"
    #include "app_util_platform.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_error.h"
    #include <string.h>
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #define CS_FLASH_MEMORY_PIN (17UL)
    #define HOLD_PIN (18UL)
    #define WRITE_PROTECT_PIN (19UL)
    
    void configure_pin_direction_as_output(unsigned long pin){
    
    NRF_GPIO->PIN_CNF[pin] = (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) |
                             (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
                             (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
                             (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
                             (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
    
    }
    
    void set_pin(unsigned long pin){
    
    	 NRF_GPIO->OUTSET = (1UL << pin);
    
    }
    
    void reset_pin(unsigned long pin){
    
    	 NRF_GPIO->OUTCLR = (1UL << pin);
    
    }
    
    void configure_chip_select_pin_of_the_flash_memory(void){
    
      configure_pin_direction_as_output(CS_FLASH_MEMORY_PIN);
    
    }
    
    void configure_hold_pin_of_the_flash_memory(void){
    
      configure_pin_direction_as_output(HOLD_PIN);
    
    }
    
    void configure_write_protect_pin_of_the_flash_memory(void){
    
      configure_pin_direction_as_output(WRITE_PROTECT_PIN);
    
    }
    
    void deactivate_chip_select_pin_of_the_flash_memory(void){
    
      set_pin(CS_FLASH_MEMORY_PIN);
    
    }
    
    
    void activate_chip_select_pin_of_the_flash_memory(void){
    
      reset_pin(CS_FLASH_MEMORY_PIN);
    
    }
    
    void deactivate_hold_pin(void){
    
    	set_pin(HOLD_PIN);
    
    }
    
    void activate_hold_pin(void){
    
    	reset_pin(HOLD_PIN);
    
    }
    
    void deactivate_write_protect_pin(void){
    
    	set_pin(WRITE_PROTECT_PIN);
    
    }
    
    void activate_write_protect_pin(void){
    
    	reset_pin(WRITE_PROTECT_PIN);
    
    }
    
    void initialize_pin_configuration_of_the_flash_memory(void){
    
    	configure_chip_select_pin_of_the_flash_memory();
    	configure_hold_pin_of_the_flash_memory();
    	configure_write_protect_pin_of_the_flash_memory();
    
    }
    
    int main(void)
    {	
    	initialize_pin_configuration_of_the_flash_memory();
    	
        while (1)
        {
    				deactivate_chip_select_pin_of_the_flash_memory();
    				deactivate_hold_pin();
    				deactivate_write_protect_pin();
    			
    				nrf_delay_ms(6000);
    			
    				activate_chip_select_pin_of_the_flash_memory();
    				activate_hold_pin();
    				activate_write_protect_pin();
    			
    				nrf_delay_ms(6000);
        }
    }

  • Hi Johnny

    P0.18 should not be used as a GPIO as it is the RESET button and the only pin that can be used like that. I suggest using P0.15, P0.16, and P0.17 instead, these pins have no other functions than being GPIO pins. Please refer to the product specification when assigning GPIO pins. This post should be helpful to let you toggle your pins, please look at that. My colleague Martin also links to a very helpful tutorial on the matter of using the various nRF52 peripherals.

    Best regards,

    Simon

Related