Skip to content

Commit c43cef0

Browse files
ananglcarlescufi
authored andcommitted
drivers: pwm_nrf5_sw: Make proper use of 32-bit timers
When a 32-bit timer is configured as the generator, use its full bit width. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent b82f2d9 commit c43cef0

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

drivers/pwm/pwm_nrf5_sw.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ LOG_MODULE_REGISTER(pwm_nrf5_sw, CONFIG_PWM_LOG_LEVEL);
2525
#if DT_NODE_HAS_COMPAT(GENERATOR_NODE, nordic_nrf_rtc)
2626
#define USE_RTC 1
2727
#define GENERATOR_ADDR ((NRF_RTC_Type *) DT_REG_ADDR(GENERATOR_NODE))
28+
#define GENERATOR_BITS 24
2829
BUILD_ASSERT(DT_INST_PROP(0, clock_prescaler) == 0,
2930
"Only clock-prescaler = <0> is supported when used with RTC");
3031
#else
3132
#define USE_RTC 0
3233
#define GENERATOR_ADDR ((NRF_TIMER_Type *) DT_REG_ADDR(GENERATOR_NODE))
34+
#define GENERATOR_BITS DT_PROP(GENERATOR_NODE, max_bit_width)
3335
#endif
3436

3537
#define PWM_0_MAP_SIZE DT_INST_PROP_LEN(0, channel_gpios)
@@ -145,10 +147,8 @@ static int pwm_nrf5_sw_set_cycles(const struct device *dev, uint32_t channel,
145147
return -EINVAL;
146148
}
147149
} else {
148-
/* TODO: if the assigned NRF_TIMER supports higher bit
149-
* resolution, use that info in config struct.
150-
*/
151-
if (period_cycles > UINT16_MAX) {
150+
if (GENERATOR_BITS < 32 &&
151+
period_cycles > BIT_MASK(GENERATOR_BITS)) {
152152
LOG_ERR("Too long period (%u), adjust PWM prescaler!",
153153
period_cycles);
154154
return -EINVAL;
@@ -369,7 +369,9 @@ static int pwm_nrf5_sw_init(const struct device *dev)
369369
/* setup HF timer */
370370
nrf_timer_mode_set(timer, NRF_TIMER_MODE_TIMER);
371371
nrf_timer_prescaler_set(timer, config->prescaler);
372-
nrf_timer_bit_width_set(timer, NRF_TIMER_BIT_WIDTH_16);
372+
nrf_timer_bit_width_set(timer,
373+
GENERATOR_BITS == 32 ? NRF_TIMER_BIT_WIDTH_32
374+
: NRF_TIMER_BIT_WIDTH_16);
373375
nrf_timer_shorts_enable(timer,
374376
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK);
375377
}

0 commit comments

Comments
 (0)