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?

Parents
  • 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);
        }
    }

  • Hi VadymS ,

    What version of the nRF5340 PDK/DK do you have ?

  • NRF 1.5.1-rc1 Commit: c7a379d97a7abca7fbf58bef3e9d19137421ea9f 
    Zephyr 2.4.99-nc2-rc1. Commit: 31ed762d83e2ed1737ee593420b6a47c0366ae06 

  • Sorry for the misunderstanding. I have PDK 0.8.0 (I wrote it before). And I didn't see the second ERRATA about QDEC1. When you are new in some subject it it is difficult to determine where is problem. 
    Thanks.

Reply Children
No Data
Related