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

nrf9160-dk getting value from switches

This compiles with warnings and the value always comes back as 0

#define SW1_NODE DT_ALIAS(sw1)
#if DT_NODE_HAS_STATUS(SW1_NODE, okay)
#define SW1 DT_GPIO_LABEL(SW1_NODE, gpios)
#define SW1_PIN DT_GPIO_PIN(SW1_NODE, gpios)
#define SW1_FLAGS DT_GPIO_FLAGS(SW1_NODE, gpios)
#else
#error "Unsupported board: sw1 devicetree alias is not defined"
#endif

const struct device *sw1 = device_get_binding(SW1);
if (sw1 != NULL) {
  printk("Found device \"%s\"\n", SW1);
} else {
  printk("No \"%s\" found; is it connected?\n", SW1);
}
gpio_pin_configure(sw1, GPIO_INPUT, GPIO_PULL_DOWN);
while (1) {
  int sw1_val = gpio_pin_get(sw1,SW1_PIN);
  printk("Switch: %d\n", sw1_val);
  k_msleep(1000);
}

The warnings are:

warning: conversion from 'unsigned int' to 'gpio_pin_t' {aka 'unsigned char'} changes value from '256' to '0' [-Woverflow]

43 | #define GPIO_INPUT              (1U << 8)

      |                                 ^~~~~~~~~

../src/main.c:749:26: note: in expansion of macro 'GPIO_INPUT'

  749 |  gpio_pin_configure(sw1, GPIO_INPUT, GPIO_PULL_DOWN);

I checked that GPIO_INPUT is correct for gpio_pin_configure in zephyr docs.

Presumably the nrfConnect SDK is the same in this regard? 

Parents
  • Hello, 

    Can you please elaborate on what you are trying to achieve? What version of NCS are you working on? 

    What pin number are you trying to configure? 

    //This is you function call
    gpio_pin_configure(sw1, GPIO_INPUT, GPIO_PULL_DOWN);
    
    //From what I can see, you should change it to the following:
    gpio_pin_configure(sw1, SW1_PIN, GPIO_PULL_DOWN);

    Looking at how gpio_pin_configure() is configured in e.g. http_update sample:

    #define SW1_PIN (DT_GPIO_PIN(DT_ALIAS(sw1), gpios))
    #define SW1_FLAGS (DT_GPIO_FLAGS(DT_ALIAS(sw1), gpios))
    
    static int button_init(void)
    {
    	int err;
    
    	gpiob = device_get_binding(DT_GPIO_LABEL(DT_ALIAS(sw1), gpios));
    	if (gpiob == 0) {
    		printk("Nordic nRF GPIO driver was not found!\n");
    		return 1;
    	}
    	err = gpio_pin_configure(gpiob, SW1_PIN, GPIO_INPUT | SW1_FLAGS);
    	if (err == 0) {
    		gpio_init_callback(&gpio_cb, fmfu_button_pressed, BIT(SW1_PIN));
    		err = gpio_add_callback(gpiob, &gpio_cb);
    	}
    	if (err == 0) {
    		fmfu_button_irq_enable();
    	}
    	if (err != 0) {
    		printk("Unable to configure SW1 GPIO pin!\n");
    		return 1;
    	}
    	return 0;
    }


    From zephyr\include\drivers\gpio.h

    /**
     * @brief Configure a single pin.
     *
     * @param port Pointer to device structure for the driver instance.
     * @param pin Pin number to configure.
     * @param flags Flags for pin configuration: 'GPIO input/output configuration
     *        flags', 'GPIO drive strength flags', 'GPIO pin drive flags', 'GPIO pin
     *        bias flags', GPIO_INT_DEBOUNCE.
     *
     * @retval 0 If successful.
     * @retval -ENOTSUP if any of the configuration options is not supported
     *                  (unless otherwise directed by flag documentation).
     * @retval -EINVAL Invalid argument.
     * @retval -EIO I/O error when accessing an external GPIO chip.
     * @retval -EWOULDBLOCK if operation would block.
     */
    static inline int gpio_pin_configure(const struct device *port,
    				     gpio_pin_t pin,
    				     gpio_flags_t flags)


    Please provide full build output.

    Kind regards,
    Øyvind

  • Hi @Øyvind

    On the 9160dk the default for Switch 1 is pin 8 with alias sw1.

    I'm using ncs 2.4.  It seems it doesn't include an http_update sample.  (I could not find any example of polling a switch.)

    I just want to be able to poll the switch to get its value.

    The build output is:

    Including boilerplate (Zephyr base): /Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr/cmake/app/boilerplate.cmake -- Application: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp -- Zephyr version: 2.4.0 (/Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr) -- Found Python3: /usr/local/bin/python3.7 (found suitable exact version "3.7.7") found components: Interpreter -- Found west (found suitable version "0.7.2", minimum required is "0.7.1") -- Board: nrf9160dk_nrf9160ns -- Cache files will be written to: /Users/paul_tanner/Library/Caches/zephyr -- Found dtc: /usr/local/bin/dtc (found suitable version "1.6.0", minimum required is "1.4.6") -- Found toolchain: gnuarmemb (/Applications/ARM) -- Found BOARD.dts: /Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr/boards/arm/nrf9160dk_nrf9160/nrf9160dk_nrf9160ns.dts -- Found devicetree overlay: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/boards/nrf9160dk_nrf9160ns.overlay nrf9160dk_nrf9160ns.dts.pre.tmp:357.32-361.5: Warning (simple_bus_reg): /soc/cryptocell-sw: missing or empty reg/ranges property nrf9160dk_nrf9160ns.dts.pre.tmp:601.12-606.4: Warning (spi_bus_reg): /soc/peripheral@40000000/spi@b000/bme280@13: SPI bus unit address format error, expected "0" nrf9160dk_nrf9160ns.dts.pre.tmp:56.42-68.3: Warning (unique_unit_address_if_enabled): /soc/peripheral@40000000/flash-controller@39000: duplicate unit-address (also used in node /soc/peripheral@40000000/kmu@39000) nrf9160dk_nrf9160ns.dts.pre.tmp:305.19-311.3: Warning (unique_unit_address_if_enabled): /soc/peripheral@40000000/clock@5000: duplicate unit-address (also used in node /soc/peripheral@40000000/power@5000) warning: unit address and first address in 'reg' (0x0) don't match for /soc/peripheral@40000000/spi@b000/bme280@13 -- Generated zephyr.dts: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/zephyr/zephyr.dts -- Generated devicetree_unfixed.h: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/zephyr/include/generated/devicetree_unfixed.h Parsing /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/Kconfig Loaded configuration '/Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr/boards/arm/nrf9160dk_nrf9160/nrf9160dk_nrf9160ns_defconfig' Merged configuration '/Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/prj.conf' Configuration saved to '/Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/zephyr/.config' Kconfig header saved to '/Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/zephyr/include/generated/autoconf.h' -- The C compiler identification is GNU 9.3.1 -- The CXX compiler identification is GNU 9.3.1 -- The ASM compiler identification is GNU -- Found assembler: /Applications/ARM/bin/arm-none-eabi-gcc Changed board to secure nrf9160dk_nrf9160 (NOT NS) === child image spm - begin === Including boilerplate (Zephyr base): /Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr/cmake/app/boilerplate.cmake -- Application: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/spm -- Zephyr version: 2.4.0 (/Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr) -- Found Python3: /usr/local/bin/python3.7 (found suitable exact version "3.7.7") found components: Interpreter -- Found west (found suitable version "0.7.2", minimum required is "0.7.1") -- Board: nrf9160dk_nrf9160 -- Cache files will be written to: /Users/paul_tanner/Library/Caches/zephyr -- Found dtc: /usr/local/bin/dtc (found suitable version "1.6.0", minimum required is "1.4.6") -- Found toolchain: gnuarmemb (/Applications/ARM) -- Found BOARD.dts: /Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr/boards/arm/nrf9160dk_nrf9160/nrf9160dk_nrf9160.dts -- Found devicetree overlay: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/spm/nrf9160dk_nrf9160.overlay nrf9160dk_nrf9160.dts.pre.tmp:56.42-68.3: Warning (unique_unit_address_if_enabled): /soc/peripheral@50000000/flash-controller@39000: duplicate unit-address (also used in node /soc/peripheral@50000000/kmu@39000) nrf9160dk_nrf9160.dts.pre.tmp:305.19-311.3: Warning (unique_unit_address_if_enabled): /soc/peripheral@50000000/clock@5000: duplicate unit-address (also used in node /soc/peripheral@50000000/power@5000) -- Generated zephyr.dts: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/spm/zephyr/zephyr.dts -- Generated devicetree_unfixed.h: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/spm/zephyr/include/generated/devicetree_unfixed.h Parsing /Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr/Kconfig Loaded configuration '/Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr/boards/arm/nrf9160dk_nrf9160/nrf9160dk_nrf9160_defconfig' Merged configuration '/Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/spm.conf' Merged configuration '/Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/spm/zephyr/misc/generated/extra_kconfig_options.conf' Configuration saved to '/Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/spm/zephyr/.config' Kconfig header saved to '/Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/spm/zephyr/include/generated/autoconf.h' -- The C compiler identification is GNU 9.3.1 -- The CXX compiler identification is GNU 9.3.1 -- The ASM compiler identification is GNU -- Found assembler: /Applications/ARM/bin/arm-none-eabi-gcc -- Configuring done -- Generating done -- Build files have been written to: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build/spm === child image spm - end === CMake Warning at /Users/paul_tanner/Nordic/nrfsrc/ncs/zephyr/CMakeLists.txt:1331 (message): __ASSERT() statements are globally ENABLED -- Configuring done -- Generating done -- Build files have been written to: /Users/paul_tanner/Nordic/nrfsrc/ncs/nrf/samples/nrf9160/gps_udp/build -- west build: building application [1/266] Preparing syscall dependency handling [8/266] Performing build step for 'spm_subimage' [1/197] Preparing syscall dependency handling [192/197] Linking C executable zephyr/zephyr_prebuilt.elf Memory region Used Size Region Size %age Used FLASH: 32 KB 48 KB 66.67% SRAM: 5600 B 64 KB 8.54% IDT_LIST: 56 B 2 KB 2.73% [197/197] Linking C executable zephyr/zephyr.elf [260/266] Linking C executable zephyr/zephyr_prebuilt.elf Memory region Used Size Region Size %age Used FLASH: 149600 B 976 KB 14.97% SRAM: 37752 B 128 KB 28.80% IDT_LIST: 168 B 2 KB 8.20% [266/266] Generating zephyr/merged.hex

  • Hi, 

    ...

    You have Zephyr OS v2.4, assuming you are using NCS v1.4.x.

    Have you configured correct PIN as per my last comment? From gpio.h

    /**
     * @brief Get logical level of an input pin.
     *
     * Get logical level of an input pin taking into account GPIO_ACTIVE_LOW flag.
     * If pin is configured as Active High, a low physical level will be interpreted
     * as logical value 0. If pin is configured as Active Low, a low physical level
     * will be interpreted as logical value 1.
     *
     * Note: If pin is configured as Active High, the default, gpio_pin_get()
     *       function is equivalent to gpio_pin_get_raw().
     *
     * @param port Pointer to the device structure for the driver instance.
     * @param pin Pin number.
     *
     * @retval 1 If pin logical value is 1 / active.
     * @retval 0 If pin logical value is 0 / inactive.
     * @retval -EIO I/O error when accessing an external GPIO chip.
     * @retval -EWOULDBLOCK if operation would block.
     */
    static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin)

Reply Children
No Data
Related