Move PWM rate configuration, etc. into a separate utility and out of the individual drivers.

This commit is contained in:
px4dev
2013-03-12 22:22:50 -07:00
parent 57429fd12c
commit 7011fe563b
6 changed files with 289 additions and 92 deletions
+3 -22
View File
@@ -655,7 +655,7 @@ enum PortMode {
PortMode g_port_mode;
int
hil_new_mode(PortMode new_mode, int update_rate)
hil_new_mode(PortMode new_mode)
{
// uint32_t gpio_bits;
@@ -713,8 +713,6 @@ hil_new_mode(PortMode new_mode, int update_rate)
/* (re)set the PWM output mode */
g_hil->set_mode(servo_mode);
if ((servo_mode != HIL::MODE_NONE) && (update_rate != 0))
g_hil->set_pwm_rate(update_rate);
return OK;
}
@@ -800,7 +798,6 @@ int
hil_main(int argc, char *argv[])
{
PortMode new_mode = PORT_MODE_UNDEFINED;
unsigned pwm_rate = 0;
const char *verb = argv[1];
if (hil_start() != OK)
@@ -812,22 +809,6 @@ hil_main(int argc, char *argv[])
// this was all cut-and-pasted from the FMU driver; it's junk
if (!strcmp(verb, "mode_pwm")) {
int ch;
while ((ch = getopt(argc - 1, argv + 1, "u:")) != EOF) {
switch (ch) {
case 'u':
pwm_rate = strtol(optarg, nullptr, 0);
break;
case ':':
errx(1, "missing parameter");
default:
errx(1, "unrecognised option");
}
}
new_mode = PORT1_FULL_PWM;
} else if (!strcmp(verb, "mode_pwm_serial")) {
@@ -854,7 +835,7 @@ hil_main(int argc, char *argv[])
return OK;
/* switch modes */
return hil_new_mode(new_mode, pwm_rate);
return hil_new_mode(new_mode);
}
if (!strcmp(verb, "test"))
@@ -865,6 +846,6 @@ hil_main(int argc, char *argv[])
fprintf(stderr, "HIL: unrecognized command, try:\n");
fprintf(stderr, " mode_pwm [-u pwm_update_rate_in_hz], mode_gpio_serial, mode_pwm_serial, mode_pwm_gpio, mode_port2_pwm8, mode_port2_pwm12, mode_port2_pwm16\n");
fprintf(stderr, " mode_pwm, mode_gpio_serial, mode_pwm_serial, mode_pwm_gpio, mode_port2_pwm8, mode_port2_pwm12, mode_port2_pwm16\n");
return -EINVAL;
}
+14 -42
View File
@@ -309,6 +309,8 @@ PX4FMU::set_mode(Mode mode)
int
PX4FMU::set_pwm_rate(uint32_t rate_map, unsigned default_rate, unsigned alt_rate)
{
debug("set_pwm_rate %x %u %u", rate_map, default_rate, alt_rate);
for (unsigned pass = 0; pass < 2; pass++) {
for (unsigned group = 0; group < _max_actuators; group++) {
@@ -323,17 +325,22 @@ PX4FMU::set_pwm_rate(uint32_t rate_map, unsigned default_rate, unsigned alt_rate
if (pass == 0) {
// preflight
if ((alt != 0) && (alt != mask)) {
warn("rate group %u mask %x bad overlap %x", group, mask, alt);
// not a legal map, bail
return -EINVAL;
}
} else {
// set it - errors here are unexpected
if (alt != 0) {
if (up_pwm_servo_set_rate_group_update(group, _pwm_alt_rate) != OK)
if (up_pwm_servo_set_rate_group_update(group, _pwm_alt_rate) != OK) {
warn("rate group set alt failed");
return -EINVAL;
}
} else {
if (up_pwm_servo_set_rate_group_update(group, _pwm_default_rate) != OK)
if (up_pwm_servo_set_rate_group_update(group, _pwm_default_rate) != OK) {
warn("rate group set default failed");
return -EINVAL;
}
}
}
}
@@ -428,6 +435,7 @@ PX4FMU::task_main()
_current_update_rate = 50;
}
debug("adjusted actuator update interval to %ums", update_rate_in_ms);
orb_set_interval(_t_actuators, update_rate_in_ms);
}
@@ -643,6 +651,7 @@ PX4FMU::pwm_ioctl(file *filp, int cmd, unsigned long arg)
*(uint32_t *)arg = up_pwm_servo_get_rate_group(cmd - PWM_SERVO_GET_RATEGROUP(0));
break;
case PWM_SERVO_GET_COUNT:
case MIXERIOCGETOUTPUTCOUNT:
if (_mode == MODE_4PWM) {
*(unsigned *)arg = 4;
@@ -1023,9 +1032,6 @@ int
fmu_main(int argc, char *argv[])
{
PortMode new_mode = PORT_MODE_UNSET;
unsigned pwm_rate = 0;
uint32_t alt_channels = 0;
bool alt_channels_set = false;
const char *verb = argv[1];
if (fmu_start() != OK)
@@ -1041,29 +1047,6 @@ fmu_main(int argc, char *argv[])
new_mode = PORT_FULL_SERIAL;
} else if (!strcmp(verb, "mode_pwm")) {
int ch;
char *ap;
while ((ch = getopt(argc - 1, argv + 1, "c:u:")) != EOF) {
switch (ch) {
case 'u':
pwm_rate = strtol(optarg, nullptr, 0);
break;
case 'c':
alt_channels = strtol(optarg, &ap, 0);
if (*ap == '\0')
alt_channels_set = true;
break;
case ':':
errx(1, "missing parameter");
default:
errx(1, "unrecognised option");
}
}
new_mode = PORT_FULL_PWM;
} else if (!strcmp(verb, "mode_gpio_serial")) {
@@ -1084,19 +1067,8 @@ fmu_main(int argc, char *argv[])
return OK;
/* switch modes */
return fmu_new_mode(new_mode);
/* if new modes are PWM modes, respect the -u and -c options */
if ((new_mode == PORT_FULL_PWM) || (new_mode == PORT_PWM_AND_GPIO)) {
if (pwm_rate != 0) {
if (g_fmu->set_pwm_alt_rate(pwm_rate) != OK)
errx(1, "error setting alternate PWM rate");
}
if (alt_channels_set) {
if (g_fmu->set_pwm_alt_channels(alt_channels))
errx(1, "serror setting alternate PWM rate channels");
}
}
int ret = fmu_new_mode(new_mode);
exit(ret == OK ? 0 : 1);
}
if (!strcmp(verb, "test"))
@@ -1106,6 +1078,6 @@ fmu_main(int argc, char *argv[])
fake(argc - 1, argv + 1);
fprintf(stderr, "FMU: unrecognised command, try:\n");
fprintf(stderr, " mode_gpio, mode_serial, mode_pwm [-u pwm_alt_rate_in_hz] [-c pwm_alt_rate_channel_mask], mode_gpio_serial, mode_pwm_serial, mode_pwm_gpio\n");
fprintf(stderr, " mode_gpio, mode_serial, mode_pwm, mode_gpio_serial, mode_pwm_serial, mode_pwm_gpio\n");
exit(1);
}
+22 -28
View File
@@ -1501,6 +1501,26 @@ extern "C" __EXPORT int px4io_main(int argc, char *argv[]);
namespace
{
void
start(int argc, char *argv[])
{
if (g_dev != nullptr)
errx(1, "already loaded");
/* create the driver - it will set g_dev */
(void)new PX4IO();
if (g_dev == nullptr)
errx(1, "driver alloc failed");
if (OK != g_dev->init()) {
delete g_dev;
errx(1, "driver init failed");
}
exit(0);
}
void
test(void)
{
@@ -1595,34 +1615,8 @@ px4io_main(int argc, char *argv[])
if (argc < 2)
goto out;
if (!strcmp(argv[1], "start")) {
if (g_dev != nullptr)
errx(1, "already loaded");
/* create the driver - it will set g_dev */
(void)new PX4IO();
if (g_dev == nullptr)
errx(1, "driver alloc failed");
if (OK != g_dev->init()) {
delete g_dev;
errx(1, "driver init failed");
}
/* look for the optional pwm update rate for the supported modes */
if ((argc > 2) && (strcmp(argv[2], "-u") == 0 || strcmp(argv[2], "--update-rate") == 0)) {
if (argc > 2 + 1) {
#warning implement this
} else {
fprintf(stderr, "missing argument for pwm update rate (-u)\n");
return 1;
}
}
exit(0);
}
if (!strcmp(argv[1], "start"))
start(argc - 1, argv + 1);
if (!strcmp(argv[1], "recovery")) {