Error in building Event Driven State Machine Example

I am trying to program Event Driven State Machine Example using zephyr RTOS into nrf52833 Nordic Board. The issue I am facing is that code is not building (FAILED: zephyr/zephyr_pre0.elf zephyr/zephyr_pre0.map). Basically, all my transitions follow the same pattern. I tried to figure out this with exactly same sample code provided in this site (State Machine Framework — Zephyr Project documentation (nRF Connect SDK) (nordicsemi.com)) and I am using the SDK version is v5.2.2. could you please provide any insights, resources or methods that could help me to execute the sample code.

Parents
  • Hello,

    Could you please provide the path to the sample you're testing? I was unable to locate it. Also, could you share the error messages from the build log?

    Best regards,

    Vidar

  • #include <zephyr/kernel.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/smf.h>
    
    #define SW0_NODE        DT_ALIAS(sw0)
    
    /* List of events */
    #define EVENT_BTN_PRESS BIT(0)
    
    static const struct gpio_dt_spec button =
            GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios, {0});
    
    static struct gpio_callback button_cb_data;
    
    /* Forward declaration of state table */
    static const struct smf_state demo_states[];
    
    /* List of demo states */
    enum demo_state { S0, S1 };
    
    /* User defined object */
    struct s_object {
            /* This must be first */
            struct smf_ctx ctx;
    
            /* Events */
            struct k_event smf_event;
            int32_t events;
    
            /* Other state specific data add here */
    } s_obj;
    
    /* State S0 */
    static void s0_entry(void *o)
    {
            printk("STATE0\n");
    }
    
    static void s0_run(void *o)
    {
            struct s_object *s = (struct s_object *)o;
    
            /* Change states on Button Press Event */
            if (s->events & EVENT_BTN_PRESS) {
                    smf_set_state(SMF_CTX(&s_obj), &demo_states[S1]);
            }
    }
    
    /* State S1 */
    static void s1_entry(void *o)
    {
            printk("STATE1\n");
    }
    
    static void s1_run(void *o)
    {
            struct s_object *s = (struct s_object *)o;
    
            /* Change states on Button Press Event */
            if (s->events & EVENT_BTN_PRESS) {
                    smf_set_state(SMF_CTX(&s_obj), &demo_states[S0]);
            }
    }
    
    /* Populate state table */
    static const struct smf_state demo_states[] = {
            [S0] = SMF_CREATE_STATE(s0_entry, s0_run, NULL),
            [S1] = SMF_CREATE_STATE(s1_entry, s1_run, NULL),
    };
    
    void button_pressed(const struct device *dev,
                    struct gpio_callback *cb, uint32_t pins)
    {
            /* Generate Button Press Event */
            k_event_post(&s_obj.smf_event, EVENT_BTN_PRESS);
    }
    
    int main(void)
    {
            int ret;
    
            if (!gpio_is_ready_dt(&button)) {
                    printk("Error: button device %s is not ready\n",
                            button.port->name);
                    return;
            }
    
            ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
            if (ret != 0) {
                    printk("Error %d: failed to configure %s pin %d\n",
                            ret, button.port->name, button.pin);
                    return;
            }
    
            ret = gpio_pin_interrupt_configure_dt(&button,
                    GPIO_INT_EDGE_TO_ACTIVE);
            if (ret != 0) {
                    printk("Error %d: failed to configure interrupt on %s pin %d\n",
                            ret, button.port->name, button.pin);
                    return;
            }
    
            gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
            gpio_add_callback(button.port, &button_cb_data);
    
            /* Initialize the event */
            k_event_init(&s_obj.smf_event);
    
            /* Set initial state */
            smf_set_initial(SMF_CTX(&s_obj), &demo_states[S0]);
    
            /* Run the state machine */
            while(1) {
                    /* Block until an event is detected */
                    s_obj.events = k_event_wait(&s_obj.smf_event,
                                    EVENT_BTN_PRESS, true, K_FOREVER);
    
                    /* State machine terminates if a non-zero value is returned */
                    ret = smf_run_state(SMF_CTX(&s_obj));
                    if (ret) {
                            /* handle return code and terminate state machine */
                            break;
                    }
            }
    }

    This is the code i'm trying to build and it is from the webpage mentioned in the original question.

    And this is the error log from build terminal window.


    Building blinky_2
    C:\Windows\system32\cmd.exe /d /s /c "west build --build-dir c:/Users/Marina/blinky_1/blinky_2/build c:/Users/Marina/blinky_1/blinky_2"

    [1/6] Linking C executable zephyr\zephyr_pre0.elf
    FAILED: zephyr/zephyr_pre0.elf zephyr/zephyr_pre0.map
    cmd.exe /C "cd . && C:\ncs\toolchains\c57af46cb7\opt\zephyr-sdk\arm-zephyr-eabi\bin\arm-zephyr-eabi-gcc.exe -gdwarf-4 zephyr/CMakeFiles/zephyr_pre0.dir/misc/empty_file.c.obj -o zephyr\zephyr_pre0.elf -fuse-ld=bfd -T zephyr/linker_zephyr_pre0.cmd -Wl,-Map=C:/Users/Marina/blinky_1/blinky_2/build/zephyr/zephyr_pre0.map -Wl,--whole-archive app/libapp.a zephyr/libzephyr.a zephyr/arch/common/libarch__common.a zephyr/arch/arch/arm/core/aarch32/libarch__arm__core__aarch32.a zephyr/arch/arch/arm/core/aarch32/cortex_m/libarch__arm__core__aarch32__cortex_m.a zephyr/arch/arch/arm/core/aarch32/mpu/libarch__arm__core__aarch32__mpu.a zephyr/lib/libc/picolibc/liblib__libc__picolibc.a zephyr/lib/libc/common/liblib__libc__common.a zephyr/lib/smf/liblib__smf.a zephyr/soc/soc/arm/common/cortex_m/libsoc__arm__common__cortex_m.a zephyr/soc/soc/arm/nordic_nrf/nrf52/libsoc__arm__nordic_nrf__nrf52.a zephyr/subsys/usb/usb_c/libsubsys__usb__usb_c.a zephyr/drivers/clock_control/libdrivers__clock_control.a zephyr/drivers/console/libdrivers__console.a zephyr/drivers/gpio/libdrivers__gpio.a zephyr/drivers/i2c/libdrivers__i2c.a zephyr/drivers/pinctrl/libdrivers__pinctrl.a zephyr/drivers/serial/libdrivers__serial.a zephyr/drivers/timer/libdrivers__timer.a modules/nrf/lib/fatal_error/lib..__nrf__lib__fatal_error.a modules/hal_nordic/nrfx/libmodules__hal_nordic__nrfx.a modules/segger/libmodules__segger.a -Wl,--no-whole-archive zephyr/kernel/libkernel.a zephyr/CMakeFiles/offsets.dir/./arch/arm/core/offsets/offsets.c.obj -L"c:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/thumb/v7e-m/nofp" -LC:/Users/Marina/blinky_1/blinky_2/build/zephyr -lgcc zephyr/arch/common/libisr_tables.a -mcpu=cortex-m4 -mthumb -mabi=aapcs -mfp16-format=ieee -Wl,--gc-sections -Wl,--build-id=none -Wl,--sort-common=descending -Wl,--sort-section=alignment -Wl,-u,_OffsetAbsSyms -Wl,-u,_ConfigAbsSyms -nostdlib -static -Wl,-X -Wl,-N -Wl,--orphan-handling=warn -Wl,-no-pie -DPICOLIBC_INTEGER_PRINTF_SCANF --specs=picolibc.specs -lc -lgcc && cmd.exe /C "cd /D C:\Users\Marina\blinky_1\blinky_2\build\zephyr && C:\ncs\toolchains\c57af46cb7\opt\bin\cmake.exe -E true""
    c:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: app/libapp.a(main.c.obj): in function `k_event_set':
    C:\Users\Marina\blinky_1\blinky_2\build/zephyr/include/generated/syscalls/kernel.h:828: undefined reference to `z_impl_k_event_set'
    c:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: C:\Users\Marina\blinky_1\blinky_2\build/zephyr/include/generated/syscalls/kernel.h:828: undefined reference to `z_impl_k_event_set'
    c:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: app/libapp.a(main.c.obj): in function `k_event_init':
    C:\Users\Marina\blinky_1\blinky_2\build/zephyr/include/generated/syscalls/kernel.h:794: undefined reference to `z_impl_k_event_init'
    collect2.exe: error: ld returned 1 exit status
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: 'C:\ncs\toolchains\c57af46cb7\opt\bin\cmake.EXE' --build 'c:\Users\Marina\blinky_1\blinky_2\build'

  • So, this is a project you have created yourself. Have you added CONFIG_EVENTS=y to your prj.conf file to enable event objects ( this includes the file which implements `z_impl_k_event_init')

  • Tried by adding configuration (CONFIG_EVENTS=y), but still not building. Could you please figure out why the header is not taking. (#include <zephyr/kernel.h>)

    Thankyou!
  • What is the build error reported in the build output now? The errors shown in in your last screenshot should be resolved once you are able to build the project.

Reply Children
No Data
Related