drivers/samples/tests: remove usage of deprecated PWM APIs

Use the new API calls that remove pin naming.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-04-01 11:22:31 +02:00 committed by Carles Cufí
commit 10ee44c94b
11 changed files with 68 additions and 81 deletions

View file

@ -56,8 +56,8 @@ static int led_pwm_blink(const struct device *dev, uint32_t led,
led_pwm = &config->led[led]; led_pwm = &config->led[led];
return pwm_pin_set_usec(led_pwm->dev, led_pwm->channel, return pwm_set_usec(led_pwm->dev, led_pwm->channel, period_usec,
period_usec, pulse_usec, led_pwm->flags); pulse_usec, led_pwm->flags);
} }
static int led_pwm_set_brightness(const struct device *dev, static int led_pwm_set_brightness(const struct device *dev,
@ -75,8 +75,8 @@ static int led_pwm_set_brightness(const struct device *dev,
pulse = led_pwm->period * value / 100; pulse = led_pwm->period * value / 100;
return pwm_pin_set_nsec(led_pwm->dev, led_pwm->channel, return pwm_set_nsec(led_pwm->dev, led_pwm->channel, led_pwm->period,
led_pwm->period, pulse, led_pwm->flags); pulse, led_pwm->flags);
} }
static int led_pwm_on(const struct device *dev, uint32_t led) static int led_pwm_on(const struct device *dev, uint32_t led)

View file

@ -52,7 +52,7 @@ static int cmd_cycles(const struct shell *shell, size_t argc, char **argv)
flags = strtoul(argv[args_indx.flags], NULL, 0); flags = strtoul(argv[args_indx.flags], NULL, 0);
} }
err = pwm_pin_set_cycles(dev, channel, period, pulse, flags); err = pwm_set_cycles(dev, channel, period, pulse, flags);
if (err) { if (err) {
shell_error(shell, "failed to setup PWM (err %d)", shell_error(shell, "failed to setup PWM (err %d)",
err); err);
@ -85,7 +85,7 @@ static int cmd_usec(const struct shell *shell, size_t argc, char **argv)
flags = strtoul(argv[args_indx.flags], NULL, 0); flags = strtoul(argv[args_indx.flags], NULL, 0);
} }
err = pwm_pin_set_usec(dev, channel, period, pulse, flags); err = pwm_set_cycles_usec(dev, channel, period, pulse, flags);
if (err) { if (err) {
shell_error(shell, "failed to setup PWM (err %d)", err); shell_error(shell, "failed to setup PWM (err %d)", err);
return err; return err;
@ -117,7 +117,7 @@ static int cmd_nsec(const struct shell *shell, size_t argc, char **argv)
flags = strtoul(argv[args_indx.flags], NULL, 0); flags = strtoul(argv[args_indx.flags], NULL, 0);
} }
err = pwm_pin_set_nsec(dev, channel, period, pulse, flags); err = pwm_set_cycles_nsec(dev, channel, period, pulse, flags);
if (err) { if (err) {
shell_error(shell, "failed to setup PWM (err %d)", err); shell_error(shell, "failed to setup PWM (err %d)", err);
return err; return err;

View file

@ -55,8 +55,8 @@ void main(void)
*/ */
printk("Calibrating for channel %d...\n", PWM_CHANNEL); printk("Calibrating for channel %d...\n", PWM_CHANNEL);
max_period = MAX_PERIOD_USEC; max_period = MAX_PERIOD_USEC;
while (pwm_pin_set_usec(pwm, PWM_CHANNEL, while (pwm_set_usec(pwm, PWM_CHANNEL, max_period, max_period / 2U,
max_period, max_period / 2U, PWM_FLAGS)) { PWM_FLAGS)) {
max_period /= 2U; max_period /= 2U;
if (max_period < (4U * MIN_PERIOD_USEC)) { if (max_period < (4U * MIN_PERIOD_USEC)) {
printk("Error: PWM device " printk("Error: PWM device "
@ -71,8 +71,8 @@ void main(void)
period = max_period; period = max_period;
while (1) { while (1) {
ret = pwm_pin_set_usec(pwm, PWM_CHANNEL, ret = pwm_set_usec(pwm, PWM_CHANNEL, period, period / 2U,
period, period / 2U, PWM_FLAGS); PWM_FLAGS);
if (ret) { if (ret) {
printk("Error %d: failed to set pulse width\n", ret); printk("Error %d: failed to set pulse width\n", ret);
return; return;

View file

@ -53,8 +53,8 @@ void main(void)
} }
while (1) { while (1) {
ret = pwm_pin_set_usec(pwm, PWM_CHANNEL, PERIOD_USEC, ret = pwm_set_usec(pwm, PWM_CHANNEL, PERIOD_USEC, pulse_width,
pulse_width, PWM_FLAGS); PWM_FLAGS);
if (ret) { if (ret) {
printk("Error %d: failed to set pulse width\n", ret); printk("Error %d: failed to set pulse width\n", ret);
return; return;

View file

@ -64,8 +64,7 @@
static int pwm_set(const struct device *pwm_dev, uint32_t pwm_pin, static int pwm_set(const struct device *pwm_dev, uint32_t pwm_pin,
uint32_t pulse_width, pwm_flags_t flags) uint32_t pulse_width, pwm_flags_t flags)
{ {
return pwm_pin_set_usec(pwm_dev, pwm_pin, PERIOD_USEC, return pwm_set_usec(pwm_dev, pwm_pin, PERIOD_USEC, pulse_width, flags);
pulse_width, flags);
} }
enum { RED, GREEN, BLUE }; enum { RED, GREEN, BLUE };

View file

@ -49,7 +49,7 @@ void main(void)
} }
while (1) { while (1) {
ret = pwm_pin_set_usec(pwm, 0, PERIOD_USEC, pulse_width, 0); ret = pwm_set_usec(pwm, 0, PERIOD_USEC, pulse_width, 0);
if (ret < 0) { if (ret < 0) {
printk("Error %d: failed to set pulse width\n", ret); printk("Error %d: failed to set pulse width\n", ret);
return; return;

View file

@ -129,14 +129,13 @@ void board_play_tune(const char *str)
} }
if (period) { if (period) {
pwm_pin_set_usec(pwm, BUZZER_PWM_CHANNEL, pwm_set_usec(pwm, BUZZER_PWM_CHANNEL, period, period / 2U, 0);
period, period / 2U, 0);
} }
k_sleep(K_MSEC(duration)); k_sleep(K_MSEC(duration));
/* Disable the PWM */ /* Disable the PWM */
pwm_pin_set_usec(pwm, BUZZER_PWM_CHANNEL, 0, 0, 0); pwm_set_usec(pwm, BUZZER_PWM_CHANNEL, 0, 0, 0);
} }
} }

View file

@ -120,7 +120,7 @@ static enum sound_state {
static inline void beep(int period) static inline void beep(int period)
{ {
pwm_pin_set_usec(pwm, SOUND_PWM_CHANNEL, period, period / 2, 0); pwm_set_usec(pwm, SOUND_PWM_CHANNEL, period, period / 2, 0);
} }
static void sound_set(enum sound_state state) static void sound_set(enum sound_state state)

View file

@ -33,11 +33,11 @@ static void beep(struct k_work *work)
/* The "period / 2" pulse duration gives 50% duty cycle, which /* The "period / 2" pulse duration gives 50% duty cycle, which
* should result in the maximum sound volume. * should result in the maximum sound volume.
*/ */
pwm_pin_set_usec(pwm, BUZZER_PWM_CHANNEL, period, period / 2U, 0); pwm_set_usec(pwm, BUZZER_PWM_CHANNEL, period, period / 2U, 0);
k_sleep(BEEP_DURATION); k_sleep(BEEP_DURATION);
/* Disable the PWM */ /* Disable the PWM */
pwm_pin_set_usec(pwm, BUZZER_PWM_CHANNEL, 0, 0, 0); pwm_set_usec(pwm, BUZZER_PWM_CHANNEL, 0, 0, 0);
/* Ensure there's a clear silent period between two tones */ /* Ensure there's a clear silent period between two tones */
k_sleep(K_MSEC(50)); k_sleep(K_MSEC(50));

View file

@ -12,8 +12,8 @@
* @details * @details
* - Test Steps * - Test Steps
* -# Bind PWM_0 port 0. * -# Bind PWM_0 port 0.
* -# Set PWM period and pulse using pwm_pin_set_cycles(), * -# Set PWM period and pulse using pwm_set_cycles(),
* pwm_pin_set_usec(), or pwm_pin_set_nsec(). * pwm_set_usec(), or pwm_set_nsec().
* -# Use multimeter or other instruments to measure the output * -# Use multimeter or other instruments to measure the output
* from PWM_OUT_0. * from PWM_OUT_0.
* - Expected Results * - Expected Results
@ -116,20 +116,20 @@ static int test_task(uint32_t port, uint32_t period, uint32_t pulse, uint8_t uni
} }
if (unit == UNIT_CYCLES) { if (unit == UNIT_CYCLES) {
/* Verify pwm_pin_set_cycles() */ /* Verify pwm_set_cycles() */
if (pwm_pin_set_cycles(pwm_dev, port, period, pulse, 0)) { if (pwm_set_cycles(pwm_dev, port, period, pulse, 0)) {
TC_PRINT("Fail to set the period and pulse width\n"); TC_PRINT("Fail to set the period and pulse width\n");
return TC_FAIL; return TC_FAIL;
} }
} else if (unit == UNIT_USECS) { } else if (unit == UNIT_USECS) {
/* Verify pwm_pin_set_usec() */ /* Verify pwm_set_usec() */
if (pwm_pin_set_usec(pwm_dev, port, period, pulse, 0)) { if (pwm_set_usec(pwm_dev, port, period, pulse, 0)) {
TC_PRINT("Fail to set the period and pulse width\n"); TC_PRINT("Fail to set the period and pulse width\n");
return TC_FAIL; return TC_FAIL;
} }
} else { /* unit == UNIT_NSECS */ } else { /* unit == UNIT_NSECS */
/* Verify pwm_pin_set_nsec() */ /* Verify pwm_set_nsec() */
if (pwm_pin_set_nsec(pwm_dev, port, period, pulse, 0)) { if (pwm_set_nsec(pwm_dev, port, period, pulse, 0)) {
TC_PRINT("Fail to set the period and pulse width\n"); TC_PRINT("Fail to set the period and pulse width\n");
return TC_FAIL; return TC_FAIL;
} }

View file

@ -50,17 +50,17 @@ void test_capture(uint32_t period, uint32_t pulse, enum test_pwm_unit unit,
case TEST_PWM_UNIT_NSEC: case TEST_PWM_UNIT_NSEC:
TC_PRINT("Testing PWM capture @ %u/%u nsec\n", TC_PRINT("Testing PWM capture @ %u/%u nsec\n",
pulse, period); pulse, period);
err = pwm_pin_set_nsec(out.dev, out.pwm, period, err = pwm_set_nsec(out.dev, out.pwm, period,
pulse, out.flags ^= pulse, out.flags ^=
(flags & PWM_POLARITY_MASK)); (flags & PWM_POLARITY_MASK));
break; break;
case TEST_PWM_UNIT_USEC: case TEST_PWM_UNIT_USEC:
TC_PRINT("Testing PWM capture @ %u/%u usec\n", TC_PRINT("Testing PWM capture @ %u/%u usec\n",
pulse, period); pulse, period);
err = pwm_pin_set_usec(out.dev, out.pwm, period, err = pwm_set_usec(out.dev, out.pwm, period,
pulse, out.flags ^= pulse, out.flags ^=
(flags & PWM_POLARITY_MASK)); (flags & PWM_POLARITY_MASK));
break; break;
default: default:
@ -72,15 +72,13 @@ void test_capture(uint32_t period, uint32_t pulse, enum test_pwm_unit unit,
switch (unit) { switch (unit) {
case TEST_PWM_UNIT_NSEC: case TEST_PWM_UNIT_NSEC:
err = pwm_pin_capture_nsec(in.dev, in.pwm, flags, err = pwm_capture_nsec(in.dev, in.pwm, flags, &period_capture,
&period_capture, &pulse_capture, &pulse_capture, K_NSEC(period * 10));
K_NSEC(period * 10));
break; break;
case TEST_PWM_UNIT_USEC: case TEST_PWM_UNIT_USEC:
err = pwm_pin_capture_usec(in.dev, in.pwm, flags, err = pwm_capture_usec(in.dev, in.pwm, flags, &period_capture,
&period_capture, &pulse_capture, &pulse_capture, K_USEC(period * 10));
K_USEC(period * 10));
break; break;
default: default:
@ -166,18 +164,17 @@ void test_capture_timeout(void)
get_test_pwms(&out, &in); get_test_pwms(&out, &in);
err = pwm_pin_set_cycles(out.dev, out.pwm, 100, 0, out.flags); err = pwm_set_cycles(out.dev, out.pwm, 100, 0, out.flags);
zassert_equal(err, 0, "failed to set pwm output (err %d)", err); zassert_equal(err, 0, "failed to set pwm output (err %d)", err);
err = pwm_pin_capture_cycles(in.dev, in.pwm, err = pwm_capture_cycles(in.dev, in.pwm, PWM_CAPTURE_TYPE_PULSE,
PWM_CAPTURE_TYPE_PULSE, &period, &pulse, K_MSEC(1000));
&period, &pulse, K_MSEC(1000));
if (err == -ENOTSUP) { if (err == -ENOTSUP) {
TC_PRINT("Pulse capture not supported, " TC_PRINT("Pulse capture not supported, "
"trying period capture\n"); "trying period capture\n");
err = pwm_pin_capture_cycles(in.dev, in.pwm, err = pwm_capture_cycles(in.dev, in.pwm,
PWM_CAPTURE_TYPE_PERIOD, PWM_CAPTURE_TYPE_PERIOD, &period,
&period, &pulse, K_MSEC(1000)); &pulse, K_MSEC(1000));
} }
zassert_equal(err, -EAGAIN, "pwm capture did not timeout (err %d)", zassert_equal(err, -EAGAIN, "pwm capture did not timeout (err %d)",
@ -238,42 +235,40 @@ void test_continuous_capture(void)
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
k_sem_init(&data.sem, 0, 1); k_sem_init(&data.sem, 0, 1);
err = pwm_pin_set_usec(out.dev, out.pwm, period_usec, pulse_usec, err = pwm_set_usec(out.dev, out.pwm, period_usec, pulse_usec,
out.flags); out.flags);
zassert_equal(err, 0, "failed to set pwm output (err %d)", err); zassert_equal(err, 0, "failed to set pwm output (err %d)", err);
err = pwm_pin_configure_capture(in.dev, in.pwm, err = pwm_configure_capture(in.dev, in.pwm,
in.flags | in.flags |
PWM_CAPTURE_MODE_CONTINUOUS | PWM_CAPTURE_MODE_CONTINUOUS |
PWM_CAPTURE_TYPE_PULSE, PWM_CAPTURE_TYPE_PULSE,
continuous_capture_callback, continuous_capture_callback, &data);
&data);
if (err == -ENOTSUP) { if (err == -ENOTSUP) {
TC_PRINT("Pulse capture not supported, " TC_PRINT("Pulse capture not supported, "
"trying period capture\n"); "trying period capture\n");
err = pwm_pin_configure_capture(in.dev, in.pwm, err = pwm_configure_capture(in.dev, in.pwm,
in.flags | in.flags |
PWM_CAPTURE_MODE_CONTINUOUS | PWM_CAPTURE_MODE_CONTINUOUS |
PWM_CAPTURE_TYPE_PERIOD, PWM_CAPTURE_TYPE_PERIOD,
continuous_capture_callback, continuous_capture_callback, &data);
&data);
zassert_equal(err, 0, "failed to configure pwm input (err %d)", zassert_equal(err, 0, "failed to configure pwm input (err %d)",
err); err);
data.pulse_capture = false; data.pulse_capture = false;
} }
err = pwm_pin_enable_capture(in.dev, in.pwm); err = pwm_enable_capture(in.dev, in.pwm);
zassert_equal(err, 0, "failed to enable pwm capture (err %d)", err); zassert_equal(err, 0, "failed to enable pwm capture (err %d)", err);
err = k_sem_take(&data.sem, K_USEC(period_usec * data.buffer_len * 10)); err = k_sem_take(&data.sem, K_USEC(period_usec * data.buffer_len * 10));
zassert_equal(err, 0, "pwm capture timed out (err %d)", err); zassert_equal(err, 0, "pwm capture timed out (err %d)", err);
zassert_equal(data.status, 0, "pwm capture failed (err %d)", err); zassert_equal(data.status, 0, "pwm capture failed (err %d)", err);
err = pwm_pin_disable_capture(in.dev, in.pwm); err = pwm_disable_capture(in.dev, in.pwm);
zassert_equal(err, 0, "failed to disable pwm capture (err %d)", err); zassert_equal(err, 0, "failed to disable pwm capture (err %d)", err);
for (i = 0; i < data.buffer_len; i++) { for (i = 0; i < data.buffer_len; i++) {
err = pwm_pin_cycles_to_usec(in.dev, in.pwm, buffer[i], &usec); err = pwm_cycles_to_usec(in.dev, in.pwm, buffer[i], &usec);
zassert_equal(err, 0, "failed to calculate usec (err %d)", err); zassert_equal(err, 0, "failed to calculate usec (err %d)", err);
if (data.pulse_capture) { if (data.pulse_capture) {
@ -306,38 +301,32 @@ void test_capture_busy(void)
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
k_sem_init(&data.sem, 0, 1); k_sem_init(&data.sem, 0, 1);
err = pwm_pin_set_cycles(out.dev, out.pwm, 100, 0, out.flags); err = pwm_set_cycles(out.dev, out.pwm, 100, 0, out.flags);
zassert_equal(err, 0, "failed to set pwm output (err %d)", err); zassert_equal(err, 0, "failed to set pwm output (err %d)", err);
err = pwm_pin_configure_capture(in.dev, in.pwm, err = pwm_configure_capture(in.dev, in.pwm, in.flags | flags,
in.flags | flags, continuous_capture_callback, &data);
continuous_capture_callback,
&data);
if (err == -ENOTSUP) { if (err == -ENOTSUP) {
TC_PRINT("Pulse capture not supported, " TC_PRINT("Pulse capture not supported, "
"trying period capture\n"); "trying period capture\n");
flags = PWM_CAPTURE_MODE_SINGLE | PWM_CAPTURE_TYPE_PERIOD; flags = PWM_CAPTURE_MODE_SINGLE | PWM_CAPTURE_TYPE_PERIOD;
err = pwm_pin_configure_capture(in.dev, in.pwm, err = pwm_configure_capture(in.dev, in.pwm, in.flags | flags,
in.flags | flags, continuous_capture_callback, &data);
continuous_capture_callback,
&data);
zassert_equal(err, 0, "failed to configure pwm input (err %d)", zassert_equal(err, 0, "failed to configure pwm input (err %d)",
err); err);
data.pulse_capture = false; data.pulse_capture = false;
} }
err = pwm_pin_enable_capture(in.dev, in.pwm); err = pwm_enable_capture(in.dev, in.pwm);
zassert_equal(err, 0, "failed to enable pwm capture (err %d)", err); zassert_equal(err, 0, "failed to enable pwm capture (err %d)", err);
err = pwm_pin_configure_capture(in.dev, in.pwm, err = pwm_configure_capture(in.dev, in.pwm, in.flags | flags,
in.flags | flags, continuous_capture_callback, &data);
continuous_capture_callback,
&data);
zassert_equal(err, -EBUSY, "pwm capture not busy (err %d)", err); zassert_equal(err, -EBUSY, "pwm capture not busy (err %d)", err);
err = pwm_pin_enable_capture(in.dev, in.pwm); err = pwm_enable_capture(in.dev, in.pwm);
zassert_equal(err, -EBUSY, "pwm capture not busy (err %d)", err); zassert_equal(err, -EBUSY, "pwm capture not busy (err %d)", err);
err = pwm_pin_disable_capture(in.dev, in.pwm); err = pwm_disable_capture(in.dev, in.pwm);
zassert_equal(err, 0, "failed to disable pwm capture (err %d)", err); zassert_equal(err, 0, "failed to disable pwm capture (err %d)", err);
} }