Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | /* * Copyright (c) 2019 Vestas Wind Systems A/S * * SPDX-License-Identifier: Apache-2.0 */ /** * @file * @brief PWM shell commands. */ #include <zephyr/shell/shell.h> #include <zephyr/drivers/pwm.h> #include <stdlib.h> struct args_index { uint8_t device; uint8_t channel; uint8_t period; uint8_t pulse; uint8_t flags; }; static const struct args_index args_indx = { .device = 1, .channel = 2, .period = 3, .pulse = 4, .flags = 5, }; static int cmd_cycles(const struct shell *shell, size_t argc, char **argv) { pwm_flags_t flags = 0; const struct device *dev; uint32_t period; uint32_t pulse; uint32_t channel; int err; dev = device_get_binding(argv[args_indx.device]); if (!dev) { shell_error(shell, "PWM device not found"); return -EINVAL; } channel = strtoul(argv[args_indx.channel], NULL, 0); period = strtoul(argv[args_indx.period], NULL, 0); pulse = strtoul(argv[args_indx.pulse], NULL, 0); if (argc == (args_indx.flags + 1)) { flags = strtoul(argv[args_indx.flags], NULL, 0); } err = pwm_set_cycles(dev, channel, period, pulse, flags); if (err) { shell_error(shell, "failed to setup PWM (err %d)", err); return err; } return 0; } static int cmd_usec(const struct shell *shell, size_t argc, char **argv) { pwm_flags_t flags = 0; const struct device *dev; uint32_t period; uint32_t pulse; uint32_t channel; int err; dev = device_get_binding(argv[args_indx.device]); if (!dev) { shell_error(shell, "PWM device not found"); return -EINVAL; } channel = strtoul(argv[args_indx.channel], NULL, 0); period = strtoul(argv[args_indx.period], NULL, 0); pulse = strtoul(argv[args_indx.pulse], NULL, 0); if (argc == (args_indx.flags + 1)) { flags = strtoul(argv[args_indx.flags], NULL, 0); } err = pwm_set(dev, channel, PWM_USEC(period), PWM_USEC(pulse), flags); if (err) { shell_error(shell, "failed to setup PWM (err %d)", err); return err; } return 0; } static int cmd_nsec(const struct shell *shell, size_t argc, char **argv) { pwm_flags_t flags = 0; const struct device *dev; uint32_t period; uint32_t pulse; uint32_t channel; int err; dev = device_get_binding(argv[args_indx.device]); if (!dev) { shell_error(shell, "PWM device not found"); return -EINVAL; } channel = strtoul(argv[args_indx.channel], NULL, 0); period = strtoul(argv[args_indx.period], NULL, 0); pulse = strtoul(argv[args_indx.pulse], NULL, 0); if (argc == (args_indx.flags + 1)) { flags = strtoul(argv[args_indx.flags], NULL, 0); } err = pwm_set(dev, channel, period, pulse, flags); if (err) { shell_error(shell, "failed to setup PWM (err %d)", err); return err; } return 0; } SHELL_STATIC_SUBCMD_SET_CREATE(pwm_cmds, SHELL_CMD_ARG(cycles, NULL, "<device> <channel> <period in cycles> " "<pulse width in cycles> [flags]", cmd_cycles, 5, 1), SHELL_CMD_ARG(usec, NULL, "<device> <channel> <period in usec> " "<pulse width in usec> [flags]", cmd_usec, 5, 1), SHELL_CMD_ARG(nsec, NULL, "<device> <channel> <period in nsec> " "<pulse width in nsec> [flags]", cmd_nsec, 5, 1), SHELL_SUBCMD_SET_END ); SHELL_CMD_REGISTER(pwm, &pwm_cmds, "PWM shell commands", NULL); |