#[solved] macro expansion in kscan [wasn't detecting dts bindings]

1 messages · Page 1 of 1 (latest)

haughty harness
#

I am getting an error when trying to compile my custom kscan, due to an error in the expansion of this macro (error and devicetree bindings attached in next message):

#define KSCAN_JOYSTICK_INIT(n)                                                                      \
    static struct kscan_joystick_data kscan_joystick_data_##n = {};                                 \
                                                                                                    \
    static const struct kscan_joystick_config kscan_joystick_config_##n = {                         \
        .idle_period_ms = DT_INST_PROP_OR(n, idle_period_ms, 100),                                  \
        .poll_period_ms = DT_INST_PROP_OR(n, poll_period_ms, 10),                                   \
        .adc_0 = ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(n), 0),                                         \
        .adc_1 = ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(n), 1),                                         \
        .angle_offset  = DT_INST_PROP_OR(n, angle_offset, 0),                                       \
    };                                                                                              \
                                                                                                    \
    PM_DEVICE_DT_INST_DEFINE(n, kscan_joystick_pm_action);                                          \
                                                                                                    \
    DEVICE_DT_INST_DEFINE(n, &kscan_joystick_init, PM_DEVICE_DT_INST_GET(n),                        \
                          &kscan_joystick_data_##n, &kscan_joystick_config_##n, POST_KERNEL,        \
                          CONFIG_KSCAN_INIT_PRIORITY, &kscan_joystick_api);

DT_INST_FOREACH_STATUS_OKAY(KSCAN_JOYSTICK_INIT);
#

devicetree bindings

# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT

description: Joystick as a directional switch

compatible: "zmk,kscan-joystick"

include: kscan.yaml

properties:
  io-channels:
    type: phandle-array
    required: true
    description: |
      ADC channels connected to joystick axes.
      First channel is for X-axis, second for Y-axis.
  idle-period-ms:
    type: int
    default: 100
    description: Time between reads in milliseconds when joystick is in the center.
  poll-period-ms:
    type: int
    default: 10
    description: Time between reads in milliseconds when joystick is not in the center.
  angle-offset:
    type: int
    default: 0
    description: The amount of degrees which the angle will be offset by. Must not be negative.
#

help with macro expansion in kscan

#

oh wait

#

my devicetree is wrong
but that doesn't change the error

haughty harness
#

I have no clue what this means

haughty harness
#

this is the devicetree_generated.h when I tried to compile with the following macro definition

#define KSCAN_JOYSTICK_INIT(n)                                                                 \
    static struct kscan_joystick_data kscan_joystick_data_##n = {};                            \
                                                                                               \
    const struct device *adc_dev = DT_DRV_INST(n);                                             \
                                                                                               \
    static const struct kscan_joystick_config kscan_joystick_config_##n = {                    \
        .idle_period_ms = DT_INST_PROP_OR(n, idle_period_ms, 100),                             \
        .poll_period_ms = DT_INST_PROP_OR(n, poll_period_ms, 10),                              \
        /*.adc_0 = ADC_DT_SPEC_GET_BY_IDX(adc_dev, 0),                                         \
        .adc_1 = ADC_DT_SPEC_GET_BY_IDX(adc_dev, 1),*/                                         \
        .angle_offset  = DT_INST_PROP_OR(n, angle_offset, 0),                                  \
    };                                                                                         \
                                                                                               \
    PM_DEVICE_DT_INST_DEFINE(n, kscan_joystick_pm_action);                                     \
                                                                                               \
    DEVICE_DT_INST_DEFINE(n, &kscan_joystick_init, PM_DEVICE_DT_INST_GET(n),                   \
                          &kscan_joystick_data_##n, &kscan_joystick_config_##n, POST_KERNEL,   \
                          CONFIG_KSCAN_INIT_PRIORITY, &kscan_joystick_api);

DT_INST_FOREACH_STATUS_OKAY(KSCAN_JOYSTICK_INIT);

line 4475 is the devicetree node for kscan_joystick in the attached file:

haughty harness
#

it compiles just fine if I comment out the code which fetches the ADC using ADC_DT_SPEC_GET_BY_IDX
but when I tried to add in const struct device *adc_dev = DT_DRV_INST(n); to see why getting the device node identifier fails, then the above error occurs

opal bridge
#

need patching oversampling value for nrf5 mcu for your kscan bits

haughty harness
#

the "compiles just fine" is when there's nothing related to DT_DRV_INST(n) or DT_INST(n, zmk_kscan_joystick) or ADC_DT_SPEC_GET_BY_IDX

#define KSCAN_JOYSTICK_INIT(n)                                                                 \
                                                                                               \
    static struct kscan_joystick_data kscan_joystick_data_##n = {};                            \
                                                                                               \
    static const struct kscan_joystick_config kscan_joystick_config_##n = {                    \
        .idle_period_ms = DT_INST_PROP_OR(n, idle_period_ms, 100),                             \
        .poll_period_ms = DT_INST_PROP_OR(n, poll_period_ms, 10),                              \
        .angle_offset  = DT_INST_PROP_OR(n, angle_offset, 0),                                  \
    };                                                                                         \
                                                                                               \
    PM_DEVICE_DT_INST_DEFINE(n, kscan_joystick_pm_action);                                     \
                                                                                               \
    DEVICE_DT_INST_DEFINE(n, &kscan_joystick_init, PM_DEVICE_DT_INST_GET(n),                   \
                          &kscan_joystick_data_##n, &kscan_joystick_config_##n, POST_KERNEL,   \
                          CONFIG_KSCAN_INIT_PRIORITY, &kscan_joystick_api);

DT_INST_FOREACH_STATUS_OKAY(KSCAN_JOYSTICK_INIT);
#

but I do need to fetch the io-channels

haughty harness
opal bridge
#

better to usezephyr way to obtain ADC device by ADC_DT_SPEC_GET_BY_IDX

haughty harness
# haughty harness I am getting an error when trying to compile my custom kscan, due to an error in...

I was using ADC_DT_SPEC_GET_BY_IDX but regardless of whether I use

ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(n), 0)
ADC_DT_SPEC_GET_BY_IDX(DT_INST(n, zmk_kscan_joystick), 0)
// DT_DRV_INST(n) or DT_INST(n, zmk_kscan_joystick) should return a node identifier but the macro doesn't expand properly i.e. either can't find the node or the compiler is bugging out
ADC_DT_SPEC_GET_BY_IDX(n, 0)
// this shouldn't work as n isn't a node

it has some issue with macro expansion

opal bridge
#

i think you can useADC_DT_SPEC_GET_BY_IDX(n, 0) only if it was called in a macro argument of DT_INST_FOREACH_STATUS_OKAY. Here is an another example from George.

haughty harness
#

KSCAN_JOYSTICK_INIT is being called by DT_INST_FOREACH_STATUS_OKAY(KSCAN_JOYSTICK_INIT);
I'll have a look at the example you sent

opal bridge
#

FWIW ADC_DT_SPEC_INST_GET_BY_IDX is hardcoded to read attributes fromio-channels = <&adc 5>, <&adc 7>;

haughty harness
#

my overlay defines them like this inside the root? node (the one that starts with /{):

    kscan3: kscan_joystick {
        compatible = "zmk,kscan-joystick";
        status = "okay";
        /* Add this back in after it can compile... wakeup-source; */
        io-channels = <&adc 0>, <&adc 7>;
        io-channel-names = "x-axis", "y-axis";
    };
#

ignore what it was before the edit, I was commenting it out to see if it would give a different error

haughty harness
#

ohmygod

#

this is such a stupid mistake

#

I had the dts_root misconfigured in build.yaml

#

welp at least I learnt a lot more about zephyr

#

closing the post