Skip to content

Commit ae91933

Browse files
gmarullcarlescufi
authored andcommitted
drivers: pwm: always use nanoseconds for set
In order to be consistent with what is possible in Devicetree, always take a period in nanoseconds. Other scales or units may be specified by using, e.g., the PWM_MSEC() macros (all of them converting down to nanoseconds). This change then deletes the "_nsec" and "_usec" versions of the pwm_set call. Note that this change limits the period to UINT32_MAX nanoseconds, ~4.3s. PWM is, in generali, used with periods below the second so it should not be a problem. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 2bc6279 commit ae91933

File tree

12 files changed

+53
-150
lines changed

12 files changed

+53
-150
lines changed

drivers/led/led_pwm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static int led_pwm_blink(const struct device *dev, uint32_t led,
4949

5050
dt_led = &config->led[led];
5151

52-
return pwm_set_usec_dt(dt_led, period_usec, pulse_usec);
52+
return pwm_set_dt(dt_led, PWM_USEC(period_usec), PWM_USEC(pulse_usec));
5353
}
5454

5555
static int led_pwm_set_brightness(const struct device *dev,
@@ -64,8 +64,8 @@ static int led_pwm_set_brightness(const struct device *dev,
6464

6565
dt_led = &config->led[led];
6666

67-
return pwm_set_nsec_pulse_dt(&config->led[led],
68-
dt_led->period * value / 100);
67+
return pwm_set_pulse_dt(&config->led[led],
68+
dt_led->period * value / 100);
6969
}
7070

7171
static int led_pwm_on(const struct device *dev, uint32_t led)

include/zephyr/drivers/pwm.h

Lines changed: 23 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -417,100 +417,12 @@ static inline int z_impl_pwm_get_cycles_per_sec(const struct device *dev,
417417
return api->get_cycles_per_sec(dev, channel, cycles);
418418
}
419419

420-
/**
421-
* @brief Set the period and pulse width in microseconds for a single PWM
422-
* output.
423-
*
424-
* @param[in] dev PWM device instance.
425-
* @param channel PWM channel.
426-
* @param period Period (in microseconds) set to the PWM.
427-
* @param pulse Pulse width (in microseconds) set to the PWM.
428-
* @param flags Flags for pin configuration (polarity).
429-
*
430-
* @retval 0 If successful.
431-
* @retval -ENOTSUP If requested period or pulse cycles are not supported.
432-
* @retval -errno Other negative errno code on failure.
433-
*/
434-
static inline int pwm_set_usec(const struct device *dev, uint32_t channel,
435-
uint32_t period, uint32_t pulse,
436-
pwm_flags_t flags)
437-
{
438-
int err;
439-
uint64_t pulse_cycles;
440-
uint64_t period_cycles;
441-
uint64_t cycles_per_sec;
442-
443-
err = pwm_get_cycles_per_sec(dev, channel, &cycles_per_sec);
444-
if (err < 0) {
445-
return err;
446-
}
447-
448-
period_cycles = (period * cycles_per_sec) / USEC_PER_SEC;
449-
if (period_cycles > UINT32_MAX) {
450-
return -ENOTSUP;
451-
}
452-
453-
pulse_cycles = (pulse * cycles_per_sec) / USEC_PER_SEC;
454-
if (pulse_cycles > UINT32_MAX) {
455-
return -ENOTSUP;
456-
}
457-
458-
return pwm_set_cycles(dev, channel, (uint32_t)period_cycles,
459-
(uint32_t)pulse_cycles, flags);
460-
}
461-
462-
/**
463-
* @brief Set the period and pulse width in microseconds from a struct
464-
* pwm_dt_spec (with custom period).
465-
*
466-
* This is equivalent to:
467-
*
468-
* pwm_set_usec(spec->dev, spec->channel, period, pulse, spec->flags)
469-
*
470-
* The period specified in @p spec is ignored. This API call can be used when
471-
* the period specified in Devicetree needs to be changed at runtime.
472-
*
473-
* @param[in] spec PWM specification from devicetree.
474-
* @param period Period (in microseconds) set to the PWM.
475-
* @param pulse Pulse width (in microseconds) set to the PWM.
476-
*
477-
* @return A value from pwm_set_usec().
478-
*
479-
* @see pwm_set_usec_pulse_dt()
480-
*/
481-
static inline int pwm_set_usec_dt(const struct pwm_dt_spec *spec,
482-
uint32_t period, uint32_t pulse)
483-
{
484-
return pwm_set_usec(spec->dev, spec->channel, period, pulse,
485-
spec->flags);
486-
}
487-
488-
/**
489-
* @brief Set the period and pulse width in microseconds from a struct
490-
* pwm_dt_spec.
491-
*
492-
* This is equivalent to:
493-
*
494-
* pwm_set_usec(spec->dev, spec->channel, spec->period / NSEC_PER_USEC,
495-
* pulse, spec->flags)
496-
*
497-
* @param[in] spec PWM specification from devicetree.
498-
* @param pulse Pulse width (in microseconds) set to the PWM.
499-
*
500-
* @return A value from pwm_set_usec().
501-
*
502-
* @see pwm_set_usec_dt()
503-
*/
504-
static inline int pwm_set_usec_pulse_dt(const struct pwm_dt_spec *spec,
505-
uint32_t pulse)
506-
{
507-
return pwm_set_usec(spec->dev, spec->channel,
508-
spec->period / NSEC_PER_USEC, pulse, spec->flags);
509-
}
510-
511420
/**
512421
* @brief Set the period and pulse width in nanoseconds for a single PWM output.
513422
*
423+
* @note Utility macros such as PWM_MSEC() can be used to convert from other
424+
* scales or units to nanoseconds, the units used by this function.
425+
*
514426
* @param[in] dev PWM device instance.
515427
* @param channel PWM channel.
516428
* @param period Period (in nanoseconds) set to the PWM.
@@ -521,9 +433,8 @@ static inline int pwm_set_usec_pulse_dt(const struct pwm_dt_spec *spec,
521433
* @retval -ENOTSUP If requested period or pulse cycles are not supported.
522434
* @retval -errno Other negative errno code on failure.
523435
*/
524-
static inline int pwm_set_nsec(const struct device *dev, uint32_t channel,
525-
uint32_t period, uint32_t pulse,
526-
pwm_flags_t flags)
436+
static inline int pwm_set(const struct device *dev, uint32_t channel,
437+
uint32_t period, uint32_t pulse, pwm_flags_t flags)
527438
{
528439
int err;
529440
uint64_t pulse_cycles;
@@ -555,7 +466,7 @@ static inline int pwm_set_nsec(const struct device *dev, uint32_t channel,
555466
*
556467
* This is equivalent to:
557468
*
558-
* pwm_set_nsec(spec->dev, spec->channel, period, pulse, spec->flags)
469+
* pwm_set(spec->dev, spec->channel, period, pulse, spec->flags)
559470
*
560471
* The period specified in @p spec is ignored. This API call can be used when
561472
* the period specified in Devicetree needs to be changed at runtime.
@@ -564,15 +475,14 @@ static inline int pwm_set_nsec(const struct device *dev, uint32_t channel,
564475
* @param period Period (in nanoseconds) set to the PWM.
565476
* @param pulse Pulse width (in nanoseconds) set to the PWM.
566477
*
567-
* @return A value from pwm_set_nsec().
478+
* @return A value from pwm_set().
568479
*
569-
* @see pwm_set_nsec_pulse_dt()
480+
* @see pwm_set_pulse_dt()
570481
*/
571-
static inline int pwm_set_nsec_dt(const struct pwm_dt_spec *spec,
572-
uint32_t period, uint32_t pulse)
482+
static inline int pwm_set_dt(const struct pwm_dt_spec *spec, uint32_t period,
483+
uint32_t pulse)
573484
{
574-
return pwm_set_nsec(spec->dev, spec->channel, period, pulse,
575-
spec->flags);
485+
return pwm_set(spec->dev, spec->channel, period, pulse, spec->flags);
576486
}
577487

578488
/**
@@ -581,20 +491,20 @@ static inline int pwm_set_nsec_dt(const struct pwm_dt_spec *spec,
581491
*
582492
* This is equivalent to:
583493
*
584-
* pwm_set_nsec(spec->dev, spec->channel, spec->period, pulse, spec->flags)
494+
* pwm_set(spec->dev, spec->channel, spec->period, pulse, spec->flags)
585495
*
586496
* @param[in] spec PWM specification from devicetree.
587497
* @param pulse Pulse width (in nanoseconds) set to the PWM.
588498
*
589-
* @return A value from pwm_set_nsec().
499+
* @return A value from pwm_set().
590500
*
591-
* @see pwm_set_nsec_pulse_dt()
501+
* @see pwm_set_pulse_dt()
592502
*/
593-
static inline int pwm_set_nsec_pulse_dt(const struct pwm_dt_spec *spec,
594-
uint32_t pulse)
503+
static inline int pwm_set_pulse_dt(const struct pwm_dt_spec *spec,
504+
uint32_t pulse)
595505
{
596-
return pwm_set_nsec(spec->dev, spec->channel, spec->period, pulse,
597-
spec->flags);
506+
return pwm_set(spec->dev, spec->channel, spec->period, pulse,
507+
spec->flags);
598508
}
599509

600510
/**
@@ -934,26 +844,27 @@ pwm_pin_set_cycles(const struct device *dev, uint32_t channel, uint32_t period,
934844

935845
/**
936846
* @brief Set the period and pulse width for a single PWM output.
937-
* @deprecated Use pwm_set_usec() instead.
847+
* @deprecated Use pwm_set() with PWM_USEC() instead.
938848
*/
939849
__deprecated static inline int pwm_pin_set_usec(const struct device *dev,
940850
uint32_t channel,
941851
uint32_t period, uint32_t pulse,
942852
pwm_flags_t flags)
943853
{
944-
return pwm_set_usec(dev, channel, period, pulse, flags);
854+
return pwm_set(dev, channel, period * NSEC_PER_USEC,
855+
pulse * NSEC_PER_USEC, flags);
945856
}
946857

947858
/**
948859
* @brief Set the period and pulse width for a single PWM output.
949-
* @deprecated Use pwm_set_nsec() instead.
860+
* @deprecated Use pwm_set() instead.
950861
*/
951862
__deprecated static inline int pwm_pin_set_nsec(const struct device *dev,
952863
uint32_t channel,
953864
uint32_t period, uint32_t pulse,
954865
pwm_flags_t flags)
955866
{
956-
return pwm_set_nsec(dev, channel, period, pulse, flags);
867+
return pwm_set(dev, channel, period, pulse, flags);
957868
}
958869

959870
/**

include/zephyr/dt-bindings/pwm/pwm.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@
3030

3131
/**
3232
* @name PWM polarity flags
33-
* The `PWM_POLARITY_*` flags are used with pwm_set_cycles(), pwm_set_usec(),
34-
* pwm_set_nsec() or pwm_configure_capture() to specify the polarity of a PWM
35-
* channel.
33+
* The `PWM_POLARITY_*` flags are used with pwm_set_cycles(), pwm_set()
34+
* or pwm_configure_capture() to specify the polarity of a PWM channel.
3635
*
3736
* The flags are on the lower 8bits of the pwm_flags_t
3837
* @{

samples/basic/blinky_pwm/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void main(void)
4343
*/
4444
printk("Calibrating for channel %d...\n", pwm_led0.channel);
4545
max_period = MAX_PERIOD;
46-
while (pwm_set_nsec_dt(&pwm_led0, max_period, max_period / 2U)) {
46+
while (pwm_set_dt(&pwm_led0, max_period, max_period / 2U)) {
4747
max_period /= 2U;
4848
if (max_period < (4U * MIN_PERIOD)) {
4949
printk("Error: PWM device "
@@ -58,7 +58,7 @@ void main(void)
5858

5959
period = max_period;
6060
while (1) {
61-
ret = pwm_set_nsec_dt(&pwm_led0, period, period / 2U);
61+
ret = pwm_set_dt(&pwm_led0, period, period / 2U);
6262
if (ret) {
6363
printk("Error %d: failed to set pulse width\n", ret);
6464
return;

samples/basic/fade_led/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void main(void)
3535
}
3636

3737
while (1) {
38-
ret = pwm_set_nsec_pulse_dt(&pwm_led0, pulse_width);
38+
ret = pwm_set_pulse_dt(&pwm_led0, pulse_width);
3939
if (ret) {
4040
printk("Error %d: failed to set pulse width\n", ret);
4141
return;

samples/basic/rgb_led/src/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void main(void)
3939
while (1) {
4040
for (pulse_red = 0U; pulse_red <= red_pwm_led.period;
4141
pulse_red += STEP_SIZE) {
42-
ret = pwm_set_nsec_pulse_dt(&red_pwm_led, pulse_red);
42+
ret = pwm_set_pulse_dt(&red_pwm_led, pulse_red);
4343
if (ret != 0) {
4444
printk("Error %d: red write failed\n", ret);
4545
return;
@@ -48,8 +48,8 @@ void main(void)
4848
for (pulse_green = 0U;
4949
pulse_green <= green_pwm_led.period;
5050
pulse_green += STEP_SIZE) {
51-
ret = pwm_set_nsec_pulse_dt(&green_pwm_led,
52-
pulse_green);
51+
ret = pwm_set_pulse_dt(&green_pwm_led,
52+
pulse_green);
5353
if (ret != 0) {
5454
printk("Error %d: green write failed\n",
5555
ret);
@@ -59,8 +59,8 @@ void main(void)
5959
for (pulse_blue = 0U;
6060
pulse_blue <= blue_pwm_led.period;
6161
pulse_blue += STEP_SIZE) {
62-
ret = pwm_set_nsec_pulse_dt(
63-
&blue_pwm_led, pulse_blue);
62+
ret = pwm_set_pulse_dt(&blue_pwm_led,
63+
pulse_blue);
6464
if (ret != 0) {
6565
printk("Error %d: "
6666
"blue write failed\n",

samples/basic/servo_motor/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void main(void)
3838
}
3939

4040
while (1) {
41-
ret = pwm_set_nsec_pulse_dt(&servo, pulse_width);
41+
ret = pwm_set_pulse_dt(&servo, pulse_width);
4242
if (ret < 0) {
4343
printk("Error %d: failed to set pulse width\n", ret);
4444
return;

samples/bluetooth/mesh_demo/src/microbit.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,14 @@ void board_play_tune(const char *str)
129129
}
130130

131131
if (period) {
132-
pwm_set_usec(pwm, BUZZER_PWM_CHANNEL, period, period / 2U, 0);
132+
pwm_set(pwm, BUZZER_PWM_CHANNEL, PWM_USEC(period),
133+
PWM_USEC(period) / 2U, 0);
133134
}
134135

135136
k_sleep(K_MSEC(duration));
136137

137138
/* Disable the PWM */
138-
pwm_set_usec(pwm, BUZZER_PWM_CHANNEL, 0, 0, 0);
139+
pwm_set(pwm, BUZZER_PWM_CHANNEL, 0, 0, 0);
139140
}
140141
}
141142

samples/boards/bbc_microbit/pong/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static enum sound_state {
120120

121121
static inline void beep(int period)
122122
{
123-
pwm_set_usec(pwm, SOUND_PWM_CHANNEL, period, period / 2, 0);
123+
pwm_set(pwm, SOUND_PWM_CHANNEL, PWM_USEC(period), PWM_USEC(period) / 2, 0);
124124
}
125125

126126
static void sound_set(enum sound_state state)

samples/boards/bbc_microbit/sound/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ static void beep(struct k_work *work)
3333
/* The "period / 2" pulse duration gives 50% duty cycle, which
3434
* should result in the maximum sound volume.
3535
*/
36-
pwm_set_usec(pwm, BUZZER_PWM_CHANNEL, period, period / 2U, 0);
36+
pwm_set(pwm, BUZZER_PWM_CHANNEL, PWM_USEC(period), PWM_USEC(period) / 2U, 0);
3737
k_sleep(BEEP_DURATION);
3838

3939
/* Disable the PWM */
40-
pwm_set_usec(pwm, BUZZER_PWM_CHANNEL, 0, 0, 0);
40+
pwm_set(pwm, BUZZER_PWM_CHANNEL, 0, 0, 0);
4141

4242
/* Ensure there's a clear silent period between two tones */
4343
k_sleep(K_MSEC(50));

0 commit comments

Comments
 (0)