Configure GPIO pin within MCUBoot

We need to set a GPIO pin in MCUBoot to activate an external USB hub chip so the USB DFU will work.  I would like to do this with code in our project rather than directly modifying the bootloader code in the NCS directories, so it stays in our version control.

Sigurd shows a clever way to do this in this case:   RE: Modify the MCUboot's booting process 

Basically, a CMakeLists.txt file is added to the board folder that adds a board.c file to the MCUBoot build.  And the board.c file is created with a function that initializes the LED pin, and has a SYS_INIT() line to have the init function be called as MCUBoot is starting up.

But the example says to put the make file and board.c file in the boards folder at `zephyr\boards\arm\nrf52840dk_nrf52840\`, and that is also in the NCS folders.  We have custom board overlays in a `boards` folder in our own project.  That works very well for compiling for a couple different boards we have.  But when I put a CMakeLists.txt and board.c file in our local `boards` folder it does not seem to get recognized and the board.c file included in the MCUBoot build.

The build configuration specifies the selected board overlay file under "Base Devicetree overlays", and the device tree is correctly built.  But that isn't enough to make the build process look in that folder for the make and C code files.

How can I get this to work?  Do I need to add something to the project level CMakeLists.txt file to tell it to look in the local `boards` folder?

  • Hi Saxman58,

    What version of the nRF Connect SDK (NCS) are you working with?

    If it is a NCS version with the new board hierarchy (nrf52840dk/nrf52840 vs nrf52840dk_nrf52840), do you have a minimal custom board files to help kickstart me? I don't have one right now.

    Hieu

  • We are using NCS v2.6.1.  The relevant code in our board.overlay file (in our project /boards folder) is:

    / {
    	hub_reset: hub-reset {
    		compatible = "nordic,gpio-pins";
    		gpios = <&gpio1 9 GPIO_ACTIVE_HIGH>;
    		status = "okay";
    	};
    };
    
    &gpio1 {
    	status = "okay";
    };
    

    The build configuration uses nrf7002dk_nrf5340_cpuapp as the board target, and then `boards/our_board.overlay` as the Base Devicetree overlays.  The overlay has a lot more customization for our application, but the above is all I need to access from the bootloader.

    Here is the our_board.c file with the code we want to add to the MCUBoot bootloader build, but not the main application:

    #include <zephyr/init.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/sys/printk.h>
    
    #define HUB_RESET_NODE  DT_ALIAS(hub_reset)
    
    #if DT_NODE_HAS_STATUS(HUB_RESET_NODE, okay)
    #define HUB_RESET   DT_GPIO_LABEL(HUB_RESET_NODE, gpios)
    #define PIN         DT_GPIO_PIN(HUB_RESET_NODE, gpios)
    #define FLAGS       DT_GPIO_FLAGS(HUB_RESET_NODE, gpios)
    #else
    #error "Board doesn't have a USB Hub Reset"
    #define HUB_RESET   ""
    #define PIN         0
    #define FLAGS       0
    #endif
    
    static int board_init(void) {
        int err;
        printk("board_init\n");
        printk("PIN %d\n", PIN);
    
        static const struct device *hub_rst;
        hub_rst = device_get_binding(HUB_RESET);
    
        err = gpio_pin_configure(hub_rst, PIN, GPIO_OUTPUT_ACTIVE);
        printk("gpio_pin_configure() returned: %d\n", err);
        if (err < 0) {
            return 1;
        }
    
        err = gpio_pin_set(hub_rst, PIN, false);
        printk("gpio_pin_set() returned: %d\n", err);
        return 0;
    }
    
    SYS_INIT(board_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);



    And here is the CMakeLists file that I hoped would add this to the MCUBoot build:

    if(CONFIG_GPIO AND DEFINED CONFIG_MCUBOOT)
        zephyr_library()
        zephyr_library_sources(our_board.c)
    endif()

    I put both of these in the project /boards folder, but the CMake system isn't finding them.  That is the main problem, getting this to be included in the MCUBoot build.

    Glen

  • I was following Sigurd's example to put the above files in the board folder (though his was in the NCS folders).  But it may make more sense to put the .c and make file in our project /child_image folder since that's where the mcuboot.conf and mcuboot.overlay files are.  And the above code in our board .overlay file would probably make sense to put in mcuboot.overlay since that has all the devicetree info that is needed by the MCUBoot build.

    I tried that, but still don't know how to get the build process to compile the our_board.c code and link it in with the rest of the MCUBoot build.

Related