drivers: pwm_nrfx: Add support for center-aligned mode

Add support for up-and-down counter mode, which aligns the center of
each channel's pulses instead of their initial edges.  This is enabled
on a PWM periphral by adding the "center-aligned" property to the
device tree, e.g.:

    &pwm0 {
        status = "okay";
        center-aligned;
        ch0-pin = <15>;
        ch1-pin = <17>;
        ch1-inverted;
    };

Signed-off-by: Jim Paris <jim@jtan.com>
This commit is contained in:
Jim Paris 2019-04-24 11:05:54 -04:00 committed by Carles Cufí
commit 4aaa08acfd
2 changed files with 31 additions and 1 deletions

View file

@ -158,6 +158,15 @@ static int pwm_nrfx_pin_set(struct device *dev, u32_t pwm,
return -EINVAL;
}
/* If this PWM is in center-aligned mode, pulse and period lengths
* are effectively doubled by the up-down count, so halve them here
* to compensate.
*/
if (config->initial_config.count_mode == NRF_PWM_MODE_UP_AND_DOWN) {
period_cycles /= 2;
pulse_cycles /= 2;
}
/* Check if period_cycle is either matching currently used, or
* possible to use with our prescaler options.
*/
@ -350,6 +359,10 @@ static int pwm_nrfx_pm_control(struct device *dev,
(DT_NORDIC_NRF_PWM_PWM_##dev_idx##_CH##ch_idx##_INVERTED ? \
PWM_NRFX_CH_VALUE_INVERTED : PWM_NRFX_CH_VALUE_NORMAL)
#define PWM_NRFX_COUNT_MODE(dev_idx) \
(DT_NORDIC_NRF_PWM_PWM_##dev_idx##_CENTER_ALIGNED ? \
NRF_PWM_MODE_UP_AND_DOWN : NRF_PWM_MODE_UP)
#define PWM_NRFX_DEVICE(idx) \
static struct pwm_nrfx_data pwm_nrfx_##idx##_data = { \
.current = { \
@ -371,7 +384,7 @@ static int pwm_nrfx_pm_control(struct device *dev,
PWM_NRFX_OUTPUT_PIN(idx, 3), \
}, \
.base_clock = NRFX_PWM_DEFAULT_CONFIG_BASE_CLOCK, \
.count_mode = NRF_PWM_MODE_UP, \
.count_mode = PWM_NRFX_COUNT_MODE(idx), \
.top_value = NRFX_PWM_DEFAULT_CONFIG_TOP_VALUE, \
.load_mode = NRF_PWM_LOAD_INDIVIDUAL, \
.step_mode = NRF_PWM_STEP_TRIGGERED, \
@ -389,6 +402,9 @@ static int pwm_nrfx_pm_control(struct device *dev,
&pwm_nrfx_drv_api_funcs)
#ifdef CONFIG_PWM_0
#ifndef DT_NORDIC_NRF_PWM_PWM_0_CENTER_ALIGNED
#define DT_NORDIC_NRF_PWM_PWM_0_CENTER_ALIGNED 0
#endif
#ifndef DT_NORDIC_NRF_PWM_PWM_0_CH0_PIN
#define DT_NORDIC_NRF_PWM_PWM_0_CH0_PIN NRFX_PWM_PIN_NOT_USED
#endif
@ -417,6 +433,9 @@ PWM_NRFX_DEVICE(0);
#endif
#ifdef CONFIG_PWM_1
#ifndef DT_NORDIC_NRF_PWM_PWM_1_CENTER_ALIGNED
#define DT_NORDIC_NRF_PWM_PWM_1_CENTER_ALIGNED 0
#endif
#ifndef DT_NORDIC_NRF_PWM_PWM_1_CH0_PIN
#define DT_NORDIC_NRF_PWM_PWM_1_CH0_PIN NRFX_PWM_PIN_NOT_USED
#endif
@ -445,6 +464,9 @@ PWM_NRFX_DEVICE(1);
#endif
#ifdef CONFIG_PWM_2
#ifndef DT_NORDIC_NRF_PWM_PWM_2_CENTER_ALIGNED
#define DT_NORDIC_NRF_PWM_PWM_2_CENTER_ALIGNED 0
#endif
#ifndef DT_NORDIC_NRF_PWM_PWM_2_CH0_PIN
#define DT_NORDIC_NRF_PWM_PWM_2_CH0_PIN NRFX_PWM_PIN_NOT_USED
#endif
@ -473,6 +495,9 @@ PWM_NRFX_DEVICE(2);
#endif
#ifdef CONFIG_PWM_3
#ifndef DT_NORDIC_NRF_PWM_PWM_3_CENTER_ALIGNED
#define DT_NORDIC_NRF_PWM_PWM_3_CENTER_ALIGNED 0
#endif
#ifndef DT_NORDIC_NRF_PWM_PWM_3_CH0_PIN
#define DT_NORDIC_NRF_PWM_PWM_3_CH0_PIN NRFX_PWM_PIN_NOT_USED
#endif