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

I am looking for simple example of HW driver of QDEC for nrf5340.

Hi.
I am looking for simple example of using QDEC for nrf5340.
I want to use the Segger Embedded Studio.


Where can I find the example of some guide for HW driver for nrf5340pdk?
When I compile the project “blinky_pwm” I get the error “Unsupported board: pwm-led0 devicetree alias is not defined”. How can I define the correspond HW?

  • Hi Sigurd, could you explain how to use this using the callback handler?

    I have it set up for the NRF52840 using CONFIG_QDEC_NRFX (and CONFIG_SENSOR although I guess I rather use it wihout).

    The pins etc are all setup in an overlay file.

    &qdec {
        status = "okay";
        a-pin = <42>;
        b-pin = <43>;
        enable-pin = <30>;
        led-pin = <0xFFFFFFFF>;
        led-pre = <0>;
        steps = <12>;
    };

    When I call this:

     

           int ret = nrfx_qdec_init(conf, rotaryHandler);

    The rotaryHandler is never called, what am I missing?

    I have added a simple read function in a while loop that actually works, but not in the way I want. at least it tells me the hardware setup seems ok.

          void readRotary(){
              int *acc;
              int *accd;
              nrfx_qdec_accumulators_read(&acc, &accd);
              if (acc != prevacc)  printf("acc: %d, Acc double: %d\n", acc, accd);
              prevacc = acc;
              prevaccd = accd;
          }

    running that code show this:

    turning one side: 

    acc: 0, Acc double: 0
    acc: 1, Acc double: 0
    acc: 0, Acc double: 0
    acc: 1, Acc double: 0

    other side:

    acc: 65535, Acc double: 0
    acc: 0, Acc double: 0
    acc: 65535, Acc double: 0

  • Hi Steven,

    It could be that you forgot nrfx_qdec_enable(). If that does not solve the issue, then please create a new post for your question: https://devzone.nordicsemi.com/support/add

  • Hi.

    Can you create a simple sample for QDEC using?
    I read the data from encoder and it always zeros. And all HW registers of qdec also are zeros.

  • Hi.

    I have trouble with QDEC example implementation on 53840. I see the value of QDEC registers are 0.
    The same test application works on NRF52840.

    The test application principle: two LEDs are set to simulate QDEC input. The QDEC whole cycle (revolution) is 40 Ms. The LEDs output are connected to QDEC inputs.

    Ones per 1sec the QDEC is read. 
    In 5340 I always get value 0.

    #include <assert.h>
    #include <zephyr/types.h>
    
    #include <device.h>
    #include <drivers/gpio.h>
    #include <drivers/sensor.h>
    #include <soc.h>
    
    #include <logging/log.h>
    
    #define LOG_ENABLE 4
    LOG_MODULE_REGISTER(QDEC, LOG_ENABLE);
    
    #define SLEEP_TIME_MS 10
    #define TIME_1_SEC_FROMSLEEPTMMS (1000 / SLEEP_TIME_MS)
    
    //Time in seconds before sensor goes to idle state. If set to zero sensor is always active.
    #define CONFIG_DESKTOP_WHEEL_SENSOR_IDLE_TIMEOUT (0)
    
    #define SENSOR_IDLE_TIMEOUT \
        (CONFIG_DESKTOP_WHEEL_SENSOR_IDLE_TIMEOUT * MSEC_PER_SEC) /* ms */
    
    static const uint32_t qdec_pin[] = {
        DT_PROP(DT_NODELABEL(qdec), a_pin),
        DT_PROP(DT_NODELABEL(qdec), b_pin)};
    
    static const struct sensor_trigger qdec_trig = {
        .type = SENSOR_TRIG_DATA_READY,
        .chan = SENSOR_CHAN_ROTATION,
    };
    
    static const struct device *qdec_dev;
    
    /* The devicetree node identifier for the "led0" alias. */
    #define LED0_NODE DT_ALIAS(led0)
    #define LED1_NODE DT_ALIAS(led1) 
    
    #if DT_NODE_HAS_STATUS(LED0_NODE, okay)
    #define LED0 DT_GPIO_LABEL(LED0_NODE, gpios)
    #define PIN DT_GPIO_PIN(LED0_NODE, gpios)
    #define FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios)
    #else
    /* A build error here means your board isn't set up to blink an LED. */
    #error "Unsupported board: led0 devicetree alias is not defined"
    #define LED0 ""
    #define PIN 0
    #define FLAGS 0
    #endif
    
    #if DT_NODE_HAS_STATUS(LED1_NODE, okay)
    #define LED1 DT_GPIO_LABEL(LED1_NODE, gpios)
    #define PIN1 DT_GPIO_PIN(LED1_NODE, gpios)
    #define FLAGS1 DT_GPIO_FLAGS(LED1_NODE, gpios)
    #else
    /* A build error here means your board isn't set up to blink an LED. */
    #error "Unsupported board: led1 devicetree alias is not defined"
    #define LED1 ""
    #define PIN1 0
    #define FLAGS1 0
    #endif
    
    static uint8_t cycle_cnt = 0;
    
    //////////////////////////////////////////////////////////////////////////////
    
    void main(void)
    {
    
        int err = 0;
        struct sensor_value qdec_value;
        LOG_INF("QDEC example");
        qdec_dev = device_get_binding(DT_LABEL(DT_NODELABEL(qdec)));
        if (!qdec_dev)
        {
            LOG_ERR("Cannot get qdec device");
            return;
        }
    
        const struct device *dev_led;
        const struct device *dev_led1;
        int led_stm_state = 0;
        int ret;
    
        dev_led = device_get_binding(LED0);
        if (dev_led == NULL)
        {
            return;
        }
    
        ret = gpio_pin_configure(dev_led, PIN, GPIO_OUTPUT_ACTIVE | FLAGS);
        if (ret < 0)
        {
            return;
        }
    
        dev_led1 = device_get_binding(LED1);
        if (dev_led1 == NULL)
        {
            return;
        }
    
        ret = gpio_pin_configure(dev_led1, PIN1, GPIO_OUTPUT_ACTIVE | FLAGS1);
        if (ret < 0)
        {
            return;
        }
    
        while (1)
        {
    
            if (cycle_cnt++ >= (TIME_1_SEC_FROMSLEEPTMMS))
            {
                cycle_cnt = 0;
    
                err = sensor_sample_fetch(qdec_dev);
                if (err == 0)
                {
                    err = sensor_channel_get(qdec_dev, qdec_trig.chan, &qdec_value);
                    if (err == 0)
                    {
                        LOG_INF("Value %d", qdec_value.val1);
                    }
                    else
                    {
                        LOG_ERR("Issue to sensor_channel_get %d", err);
                    }
                }
                else
                {
                    LOG_ERR("Issue to sensor_sample_fetch %d", err);
                }
            }
    
            switch (led_stm_state)
            {
                case 0:
                    gpio_pin_set(dev_led, PIN, (int)false);
                    gpio_pin_set(dev_led1, PIN1, (int)false);
                    led_stm_state++;
                    break;
                case 1:
                    gpio_pin_set(dev_led, PIN, (int)true);
                    gpio_pin_set(dev_led1, PIN1, (int)false);
                    led_stm_state++;
                    break;
                case 2:
                    gpio_pin_set(dev_led, PIN, (int)true);
                    gpio_pin_set(dev_led1, PIN1, (int)true);
                    led_stm_state++;
                    break;
                case 3:
                    gpio_pin_set(dev_led, PIN, (int)false);
                    gpio_pin_set(dev_led1, PIN1, (int)true);
                    led_stm_state = 0;
                    break;
                default:
                    led_stm_state = 0;
                    break;
            }
    
            k_msleep(SLEEP_TIME_MS);
        }
    }

Related