drivers: sensor: qdec_nrfx: move to new DT API

And add "quadrature decoder" to the binding description just have it
written down.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2020-04-17 16:12:08 -07:00 committed by Carles Cufí
commit cd1387d6cf
2 changed files with 18 additions and 20 deletions

View file

@ -12,6 +12,7 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(qdec_nrfx, CONFIG_SENSOR_LOG_LEVEL);
#define DT_DRV_COMPAT nordic_nrf_qdec
#define FULL_ANGLE 360
@ -77,6 +78,7 @@ static int qdec_nrfx_channel_get(struct device *dev,
struct qdec_nrfx_data *data = &qdec_nrfx_data;
unsigned int key;
s32_t acc;
const s32_t steps = DT_INST_PROP(0, steps);
ARG_UNUSED(dev);
LOG_DBG("");
@ -90,17 +92,14 @@ static int qdec_nrfx_channel_get(struct device *dev,
data->acc = 0;
irq_unlock(key);
BUILD_ASSERT(DT_NORDIC_NRF_QDEC_QDEC_0_STEPS > 0,
"only positive number valid");
BUILD_ASSERT(DT_NORDIC_NRF_QDEC_QDEC_0_STEPS <= 2148,
"overflow possible");
BUILD_ASSERT(steps > 0, "only positive number valid");
BUILD_ASSERT(steps <= 2148, "overflow possible");
val->val1 = (acc * FULL_ANGLE) / DT_NORDIC_NRF_QDEC_QDEC_0_STEPS;
val->val2 = (acc * FULL_ANGLE)
- (val->val1 * DT_NORDIC_NRF_QDEC_QDEC_0_STEPS);
val->val1 = (acc * FULL_ANGLE) / steps;
val->val2 = (acc * FULL_ANGLE) - (val->val1 * steps);
if (val->val2 != 0) {
val->val2 *= 1000000;
val->val2 /= DT_NORDIC_NRF_QDEC_QDEC_0_STEPS;
val->val2 /= steps;
}
return 0;
@ -163,11 +162,11 @@ static void qdec_nrfx_event_handler(nrfx_qdec_event_t event)
static void qdec_nrfx_gpio_ctrl(bool enable)
{
#if defined(DT_NORDIC_NRF_QDEC_QDEC_0_ENABLE_PIN)
#if DT_INST_NODE_HAS_PROP(0, enable_pin)
uint32_t val = (enable)?(0):(1);
nrf_gpio_pin_write(DT_NORDIC_NRF_QDEC_QDEC_0_ENABLE_PIN, val);
nrf_gpio_cfg_output(DT_NORDIC_NRF_QDEC_QDEC_0_ENABLE_PIN);
nrf_gpio_pin_write(DT_INST_PROP(0, enable_pin), val);
nrf_gpio_cfg_output(DT_INST_PROP(0, enable_pin));
#endif
}
@ -176,14 +175,14 @@ static int qdec_nrfx_init(struct device *dev)
static const nrfx_qdec_config_t config = {
.reportper = NRF_QDEC_REPORTPER_40,
.sampleper = NRF_QDEC_SAMPLEPER_2048us,
.psela = DT_NORDIC_NRF_QDEC_QDEC_0_A_PIN,
.pselb = DT_NORDIC_NRF_QDEC_QDEC_0_B_PIN,
#if defined(DT_NORDIC_NRF_QDEC_QDEC_0_LED_PIN)
.pselled = DT_NORDIC_NRF_QDEC_QDEC_0_LED_PIN,
.psela = DT_INST_PROP(0, a_pin),
.pselb = DT_INST_PROP(0, b_pin),
#if DT_INST_NODE_HAS_PROP(0, led_pin)
.pselled = DT_INST_PROP(0, led_pin),
#else
.pselled = 0xFFFFFFFF, /* disabled */
#endif
.ledpre = DT_NORDIC_NRF_QDEC_QDEC_0_LED_PRE,
.ledpre = DT_INST_PROP(0, led_pre),
.ledpol = NRF_QDEC_LEPOL_ACTIVE_HIGH,
.interrupt_priority = NRFX_QDEC_DEFAULT_CONFIG_IRQ_PRIORITY,
.dbfen = 0, /* disabled */
@ -194,8 +193,7 @@ static int qdec_nrfx_init(struct device *dev)
LOG_DBG("");
IRQ_CONNECT(DT_NORDIC_NRF_QDEC_QDEC_0_IRQ_0,
DT_NORDIC_NRF_QDEC_QDEC_0_IRQ_0_PRIORITY,
IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
nrfx_isr, nrfx_qdec_irq_handler, 0);
nerr = nrfx_qdec_init(&config, qdec_nrfx_event_handler);
@ -308,6 +306,6 @@ static const struct sensor_driver_api qdec_nrfx_driver_api = {
.trigger_set = qdec_nrfx_trigger_set,
};
DEVICE_DEFINE(qdec_nrfx, DT_NORDIC_NRF_QDEC_QDEC_0_LABEL, qdec_nrfx_init,
DEVICE_DEFINE(qdec_nrfx, DT_INST_LABEL(0), qdec_nrfx_init,
qdec_nrfx_pm_control, NULL, NULL, POST_KERNEL,
CONFIG_SENSOR_INIT_PRIORITY, &qdec_nrfx_driver_api);

View file

@ -1,7 +1,7 @@
# Copyright (c) 2018, Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
description: Nordic nRF QDEC node
description: Nordic nRF quadrature decoder (QDEC) node
compatible: "nordic,nrf-qdec"