GPIO conflict

I have a nRF9160DK board that is used for development of a custom nRF9160 board. The hardware designers have assigned GPIO P0.27 to be a general purpose output pin. This pin is also used as uart0_cts. when I try to toggle this pin with a simple test it does not work with the code shown below. If I use another pin such as P0.23 the pin toggles fine. I am assuming there is a pin conflict by assigning 2 things to the same pin. The boards are already manufactured with P0.27 as a general purpose output control pin. How can I get this to work? Is there something in the overlay file I need to add or remove?

    int test = 0;
    while(1)
    {
        printf("test GPIO BAT_MON_EN %d\r\n",test & 0x01);
        /* toggle test */

        /* set/clear test */
        if(test++ & 0x01)
        {
//          printf("set\r\n");
            gpio_pin_set(bat_mon_en_dev, BAT_MON_EN_DEV_PIN, 1);
        }
        else
        {
//          printf("clear\r\n");
            gpio_pin_set(bat_mon_en_dev, BAT_MON_EN_DEV_PIN, 0);
        }
        k_sleep(K_MSEC(1000));
    }
Parents
  • Hello,

    Did you do anything to disable the UART0 CTS pin before trying to use it as an output pin? If not, then the GPIO is probably controlled by the UART, and you will not be able to control it. 

    If you look in the file nrf9160dk_nrf9160_common-pinctrl.dtsi, you will see the definition of uart0_default and uart0_sleep close to the top. Don't change this file (you can for testing, but I don't recommend doing so permanently), but copy the part about uart0:

    &pinctrl {
    	uart0_default: uart0_default {
    		group1 {
    			psels = <NRF_PSEL(UART_TX, 0, 29)>,
    				<NRF_PSEL(UART_RTS, 0, 27)>;
    		};
    		group2 {
    			psels = <NRF_PSEL(UART_RX, 0, 28)>,
    				<NRF_PSEL(UART_CTS, 0, 26)>;
    			bias-pull-up;
    		};
    	};
    
    	uart0_sleep: uart0_sleep {
    		group1 {
    			psels = <NRF_PSEL(UART_TX, 0, 29)>,
    				<NRF_PSEL(UART_RX, 0, 28)>,
    				<NRF_PSEL(UART_RTS, 0, 27)>,
    				<NRF_PSEL(UART_CTS, 0, 26)>;
    			low-power-enable;
    		};
    	};

    And create a file called "nrf9160dk_nrf9160.overlay" in your application folder (same folder as your prj.conf). Then paste this snippet that you just copied, and add a final line with "};" at the end to close off the bracket. Then try to remove everything about UART_RTS and UART_CTS (make it look like the uart2 definition in the original nrf9160dk_nrf9160_common-pinctrl.dtsi file):

    &pinctrl {
    	uart0_default: uart0_default {
    		group1 {
    			psels = <NRF_PSEL(UART_TX, 0, 29)>,
    				<NRF_PSEL(UART_RX, 0, 28)>;
    		};
    	};
    
    	uart0_sleep: uart0_sleep {
    		group1 {
    			psels = <NRF_PSEL(UART_TX, 0, 29)>,
    				<NRF_PSEL(UART_RX, 0, 28)>;
    			low-power-enable;
    		};
    	};
    };

    save the file, and delete your build folder, and build from scratch. Does that allow you to toggle pin P0.27?

    Best regards,

    Edvin

  • I have attached my overlay file. The BAT_MON_EN (P0.27) does not work when I toggle with the code above. The other pins seem to work just fine. what am I doing wrong?

    6278.nrf9160dk_nrf9160_ns.overlay

  • Edvin

    this is my setup for output GPIOs. all others work just a problem with BAT_MON_EN. The overlay file was given earlier.

    /* outputs */
    #define SW2CTRL_DEV_NODE            DT_GPIO_CTLR(DT_ALIAS(sw2ctrl), gpios)
    #define SW2CTRL_DEV_PIN             DT_GPIO_PIN(DT_ALIAS(sw2ctrl), gpios)
    #define SW2CTRL_DEV_PIN_DTS_FLAGS   DT_GPIO_FLAGS(DT_ALIAS(sw2ctrl), gpios)

    #define CS2N_DEV_NODE            DT_GPIO_CTLR(DT_ALIAS(spi_cs2n), gpios)
    #define CS2N_DEV_PIN             DT_GPIO_PIN(DT_ALIAS(spi_cs2n), gpios)
    #define CS2N_DEV_PIN_DTS_FLAGS   DT_GPIO_FLAGS(DT_ALIAS(spi_cs2n), gpios)

    #define CS3N_DEV_NODE            DT_GPIO_CTLR(DT_ALIAS(spi_cs3n), gpios)
    #define CS3N_DEV_PIN             DT_GPIO_PIN(DT_ALIAS(spi_cs3n), gpios)
    #define CS3N_DEV_PIN_DTS_FLAGS   DT_GPIO_FLAGS(DT_ALIAS(spi_cs3n), gpios)

    #define BAT_MON_EN_DEV_NODE            DT_GPIO_CTLR(DT_ALIAS(batmonen), gpios)
    #define BAT_MON_EN_DEV_PIN             DT_GPIO_PIN(DT_ALIAS(batmonen), gpios)
    #define BAT_MON_EN_DEV_PIN_DTS_FLAGS   DT_GPIO_FLAGS(DT_ALIAS(batmonen), gpios)

    const struct device *sw2ctrl_dev = DEVICE_DT_GET(SW2CTRL_DEV_NODE);
    const struct device *spi2_csn_dev = DEVICE_DT_GET(CS2N_DEV_NODE);
    const struct device *spi3_csn_dev = DEVICE_DT_GET(CS3N_DEV_NODE);
    const struct device *bat_mon_en_dev = DEVICE_DT_GET(BAT_MON_EN_DEV_NODE);

        /* set up BAT_MON_EN as output/pullup */
        if (!device_is_ready(bat_mon_en_dev))
        {
            myPrintfW("BAT MON EN device is not ready\r\n");
        }
        else
            myPrintfS("BAT MON EN device is ready\r\n");
        gpio_pin_configure(bat_mon_en_dev, BAT_MON_EN_DEV_PIN, GPIO_OUTPUT | GPIO_PULL_UP);
        gpio_pin_set(bat_mon_en_dev, BAT_MON_EN_DEV_PIN, 1);
  • Edvin

    I have it working as shown below. I changed Pin 27 to Pin 23 (an unused pin on custom board) in the file C:\Nordic1\v2.5.0\zephyr\boards\arm\nrf9160dk_nrf9160\nrf9160dk_nrf9160_common-pinctrl.dtsi.

    so it definitely is uart0 messing up pin 27. any idea what is going on?

    I should have taken care of this in my overlay file. It does remove uart0 and redefine it without Pin 26/27. 

    I can live with this change if I have to but I really did not want to alter any V2.5.0 files at all.

    > batmon 0
    batmon 0
    Battery Monitor: 0 Disabled
    > adc
    adc
    BAT: 0.12 V ADC: 2.85 V
    > batmon 1
    batmon 1
    Battery Monitor: 1 Enabled
    > adc
    adc
    BAT: 3.31 V ADC: 2.84 V
  • Hello,

    I agree that it is preferable not to change any of the SDK files. 

    The simple workaround is to do the change that you just did, but do it in the overlay file instead. Add something like this to your own overlay file:

    &pinctrl {
    	uart0_default: uart0_default {
    		group1 {
    			psels = <NRF_PSEL(UART_TX, 0, 29)>,
    				<NRF_PSEL(UART_RTS, 0, 23)>;
    		};
    		group2 {
    			psels = <NRF_PSEL(UART_RX, 0, 28)>,
    				<NRF_PSEL(UART_CTS, 0, 26)>;
    			bias-pull-up;
    		};
    	};
    
    	uart0_sleep: uart0_sleep {
    		group1 {
    			psels = <NRF_PSEL(UART_TX, 0, 29)>,
    				<NRF_PSEL(UART_RX, 0, 28)>,
    				<NRF_PSEL(UART_RTS, 0, 23)>,
    				<NRF_PSEL(UART_CTS, 0, 26)>;
    			low-power-enable;
    		};
    	};
    };

    I see that you tried to delete the node uart0 in your overlay file, but later in the same file, you state:

    / {
    	chosen {
    		zephyr,bt-uart=&lpuart;
            nordic,nus-uart=&uart0;
    	};
    };

    So without building and looking into your application, it is a bit difficult to say how this would behave.

    If you upload the entire project as a zipped folder, I can have a look. If that is not possible, can you please try to do that pin change in yoru prj.conf?

    Best regars,

    Edvin

  • Edvin

    I removed all other references to uart0 in prj.conf. The only reference to uart0 is the pinctrl above and an alias. The change worked perfectly. There are no changes to any V2.5.0 file now. Thank you for your help. I will test for a few days before we close it, but all should be OK. Pin 23/26 are defined but not used on the custom board.

  • Edvin

    this is working nicely. This can be closed. Thank you for your support.

Reply Children
Related