Quadrature Decoder Failing to build on NCS v2.1

Hello,

When enabling the Sensor system and having a qdec device enabled in my dts I get the following error:

/home/ee/ncs/zephyr/drivers/sensor/qdec_nrfx/qdec_nrfx.c:98:21: error: expression in static assertion is not constant
   98 |  BUILD_ASSERT(steps > 0, "only positive number valid");
      |               ~~~~~~^~~
/home/ee/ncs/zephyr/drivers/sensor/qdec_nrfx/qdec_nrfx.c:99:21: error: expression in static assertion is not constant
   99 |  BUILD_ASSERT(steps <= 2048, "overflow possible");
      |               ~~~~~~^~~~~~~

This seems to be a bug with the Zephyr code checking a const local var at compile time. Has anyone else had this issue?

Best,

Archie

Parents
  • Hi,

    You need to configure the QDEC in the devicetree or overlay file for the project, see the example in this post.

    Can you check if you still get the build asserts with this set?

    Best regards,
    Jørgen

  • It has been configured as shown below:

    &qdec0{
    	compatible = "nordic,nrf-qdec";
    	status = "okay";
    	led-pre = <0>;
    	steps = <12>;
    
    	pinctrl-0 = <&qdec0_default>;
    	pinctrl-1 = <&qdec0_sleep>;
    	pinctrl-names = "default", "sleep";	
    };


    It seems to be the qdec code that is wrong as it's saying the expression isn't constant.

    Modifying the code to this:

    static int qdec_nrfx_channel_get(const struct device *dev,
    				 enum sensor_channel  chan,
    				 struct sensor_value *val)
    {
    	struct qdec_nrfx_data *data = &qdec_nrfx_data;
    	unsigned int key;
    	int32_t acc;
    	const int32_t steps = DT_INST_PROP(0, steps);
    
    	ARG_UNUSED(dev);
    	LOG_DBG("");
    
    	if (chan != SENSOR_CHAN_ROTATION) {
    		return -ENOTSUP;
    	}
    
    	key = irq_lock();
    	acc = data->acc;
    	data->acc = 0;
    	irq_unlock(key);
    
    	BUILD_ASSERT(DT_INST_PROP(0, steps) > 0, "only positive number valid");
    	BUILD_ASSERT(DT_INST_PROP(0, steps) <= 2048, "overflow possible");
    
    	val->val1 = (acc * FULL_ANGLE) / steps;
    	val->val2 = (acc * FULL_ANGLE) - (val->val1 * steps);
    	if (val->val2 != 0) {
    		val->val2 *= 1000000;
    		val->val2 /= steps;
    	}
    
    	return 0;
    }

    Fixes the error as now the value is a compile-time constant.

Reply
  • It has been configured as shown below:

    &qdec0{
    	compatible = "nordic,nrf-qdec";
    	status = "okay";
    	led-pre = <0>;
    	steps = <12>;
    
    	pinctrl-0 = <&qdec0_default>;
    	pinctrl-1 = <&qdec0_sleep>;
    	pinctrl-names = "default", "sleep";	
    };


    It seems to be the qdec code that is wrong as it's saying the expression isn't constant.

    Modifying the code to this:

    static int qdec_nrfx_channel_get(const struct device *dev,
    				 enum sensor_channel  chan,
    				 struct sensor_value *val)
    {
    	struct qdec_nrfx_data *data = &qdec_nrfx_data;
    	unsigned int key;
    	int32_t acc;
    	const int32_t steps = DT_INST_PROP(0, steps);
    
    	ARG_UNUSED(dev);
    	LOG_DBG("");
    
    	if (chan != SENSOR_CHAN_ROTATION) {
    		return -ENOTSUP;
    	}
    
    	key = irq_lock();
    	acc = data->acc;
    	data->acc = 0;
    	irq_unlock(key);
    
    	BUILD_ASSERT(DT_INST_PROP(0, steps) > 0, "only positive number valid");
    	BUILD_ASSERT(DT_INST_PROP(0, steps) <= 2048, "overflow possible");
    
    	val->val1 = (acc * FULL_ANGLE) / steps;
    	val->val2 = (acc * FULL_ANGLE) - (val->val1 * steps);
    	if (val->val2 != 0) {
    		val->val2 *= 1000000;
    		val->val2 /= steps;
    	}
    
    	return 0;
    }

    Fixes the error as now the value is a compile-time constant.

Children
No Data
Related