2012-08-04 19:12:36 -03:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2012-08-05 17:43:16 -03:00
|
|
|
* @file fmu.cpp
|
|
|
|
*
|
|
|
|
* Driver/configurator for the PX4 FMU multi-purpose port.
|
2012-08-04 19:12:36 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2012-08-05 00:05:47 -03:00
|
|
|
#include <stdlib.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
|
|
|
|
#include <drivers/device/device.h>
|
|
|
|
#include <drivers/drv_pwm_output.h>
|
|
|
|
#include <drivers/drv_gpio.h>
|
2012-10-27 05:39:10 -03:00
|
|
|
#include <drivers/boards/px4fmu/px4fmu_internal.h>
|
2012-08-10 04:30:40 -03:00
|
|
|
|
2012-10-28 02:42:43 -03:00
|
|
|
#include <systemlib/systemlib.h>
|
2012-08-10 04:30:40 -03:00
|
|
|
#include <systemlib/mixer/mixer.h>
|
2012-08-05 00:05:47 -03:00
|
|
|
#include <drivers/drv_mixer.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
#include <uORB/topics/actuator_controls.h>
|
2012-08-22 00:14:43 -03:00
|
|
|
#include <uORB/topics/actuator_outputs.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
#include <systemlib/err.h>
|
2012-10-01 04:02:38 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
class PX4FMU : public device::CDev
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Mode {
|
|
|
|
MODE_2PWM,
|
|
|
|
MODE_4PWM,
|
|
|
|
MODE_NONE
|
|
|
|
};
|
2012-10-27 05:39:10 -03:00
|
|
|
PX4FMU();
|
|
|
|
~PX4FMU();
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
virtual int ioctl(file *filp, int cmd, unsigned long arg);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
virtual int init();
|
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
int set_mode(Mode mode);
|
|
|
|
int set_pwm_rate(unsigned rate);
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
private:
|
2012-08-05 00:05:47 -03:00
|
|
|
static const unsigned _max_actuators = 4;
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
Mode _mode;
|
2012-09-27 18:36:56 -03:00
|
|
|
int _update_rate;
|
2012-10-27 05:39:10 -03:00
|
|
|
int _current_update_rate;
|
2012-08-04 19:12:36 -03:00
|
|
|
int _task;
|
|
|
|
int _t_actuators;
|
|
|
|
int _t_armed;
|
2012-08-22 03:44:22 -03:00
|
|
|
orb_advert_t _t_outputs;
|
2012-08-04 19:12:36 -03:00
|
|
|
unsigned _num_outputs;
|
2012-10-20 20:53:52 -03:00
|
|
|
bool _primary_pwm_device;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
volatile bool _task_should_exit;
|
|
|
|
bool _armed;
|
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
MixerGroup *_mixers;
|
|
|
|
|
|
|
|
actuator_controls_s _controls;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
static void task_main_trampoline(int argc, char *argv[]);
|
2012-08-22 00:14:43 -03:00
|
|
|
void task_main() __attribute__((noreturn));
|
2012-08-10 04:30:40 -03:00
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
static int control_callback(uintptr_t handle,
|
2012-08-14 12:47:04 -03:00
|
|
|
uint8_t control_group,
|
|
|
|
uint8_t control_index,
|
|
|
|
float &input);
|
2012-10-27 05:39:10 -03:00
|
|
|
|
|
|
|
int pwm_ioctl(file *filp, int cmd, unsigned long arg);
|
|
|
|
|
|
|
|
struct GPIOConfig {
|
|
|
|
uint32_t input;
|
|
|
|
uint32_t output;
|
|
|
|
uint32_t alt;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GPIOConfig _gpio_tab[];
|
|
|
|
static const unsigned _ngpio;
|
|
|
|
|
|
|
|
void gpio_reset(void);
|
|
|
|
void gpio_set_function(uint32_t gpios, int function);
|
|
|
|
void gpio_write(uint32_t gpios, int function);
|
|
|
|
uint32_t gpio_read(void);
|
|
|
|
int gpio_ioctl(file *filp, int cmd, unsigned long arg);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const PX4FMU::GPIOConfig PX4FMU::_gpio_tab[] = {
|
|
|
|
{GPIO_GPIO0_INPUT, GPIO_GPIO0_OUTPUT, 0},
|
|
|
|
{GPIO_GPIO1_INPUT, GPIO_GPIO1_OUTPUT, 0},
|
|
|
|
{GPIO_GPIO2_INPUT, GPIO_GPIO2_OUTPUT, GPIO_USART2_CTS_1},
|
|
|
|
{GPIO_GPIO3_INPUT, GPIO_GPIO3_OUTPUT, GPIO_USART2_RTS_1},
|
|
|
|
{GPIO_GPIO4_INPUT, GPIO_GPIO4_OUTPUT, GPIO_USART2_TX_1},
|
|
|
|
{GPIO_GPIO5_INPUT, GPIO_GPIO5_OUTPUT, GPIO_USART2_RX_1},
|
|
|
|
{GPIO_GPIO6_INPUT, GPIO_GPIO6_OUTPUT, GPIO_CAN2_TX_2},
|
|
|
|
{GPIO_GPIO7_INPUT, GPIO_GPIO7_OUTPUT, GPIO_CAN2_RX_2},
|
2012-08-04 19:12:36 -03:00
|
|
|
};
|
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
const unsigned PX4FMU::_ngpio = sizeof(PX4FMU::_gpio_tab) / sizeof(PX4FMU::_gpio_tab[0]);
|
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
namespace
|
|
|
|
{
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
PX4FMU *g_fmu;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
PX4FMU::PX4FMU() :
|
2012-10-20 20:53:52 -03:00
|
|
|
CDev("fmuservo", "/dev/px4fmu"),
|
2012-10-27 05:39:10 -03:00
|
|
|
_mode(MODE_NONE),
|
|
|
|
_update_rate(50),
|
2012-08-04 19:12:36 -03:00
|
|
|
_task(-1),
|
|
|
|
_t_actuators(-1),
|
|
|
|
_t_armed(-1),
|
2012-09-04 03:21:17 -03:00
|
|
|
_t_outputs(0),
|
|
|
|
_num_outputs(0),
|
2012-10-20 20:53:52 -03:00
|
|
|
_primary_pwm_device(false),
|
2012-08-04 19:12:36 -03:00
|
|
|
_task_should_exit(false),
|
2012-08-10 04:30:40 -03:00
|
|
|
_armed(false),
|
|
|
|
_mixers(nullptr)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
2012-10-27 05:39:10 -03:00
|
|
|
_debug_enabled = true;
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
PX4FMU::~PX4FMU()
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
if (_task != -1) {
|
2012-10-20 20:53:52 -03:00
|
|
|
/* tell the task we want it to go away */
|
2012-08-04 19:12:36 -03:00
|
|
|
_task_should_exit = true;
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
unsigned i = 10;
|
2012-08-04 19:12:36 -03:00
|
|
|
do {
|
2012-10-20 20:53:52 -03:00
|
|
|
/* wait 50ms - it should wake every 100ms or so worst-case */
|
|
|
|
usleep(50000);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* if we have given up, kill it */
|
2012-10-20 20:53:52 -03:00
|
|
|
if (--i == 0) {
|
2012-08-04 19:12:36 -03:00
|
|
|
task_delete(_task);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (_task != -1);
|
|
|
|
}
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* clean up the alternate device node */
|
|
|
|
if (_primary_pwm_device)
|
|
|
|
unregister_driver(PWM_OUTPUT_DEVICE_PATH);
|
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
g_fmu = nullptr;
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-10-27 05:39:10 -03:00
|
|
|
PX4FMU::init()
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ASSERT(_task == -1);
|
|
|
|
|
|
|
|
/* do regular cdev init */
|
|
|
|
ret = CDev::init();
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (ret != OK)
|
|
|
|
return ret;
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* try to claim the generic PWM output device node as well - it's OK if we fail at this */
|
|
|
|
ret = register_driver(PWM_OUTPUT_DEVICE_PATH, &fops, 0666, (void *)this);
|
|
|
|
if (ret == OK) {
|
|
|
|
log("default PWM output device");
|
|
|
|
_primary_pwm_device = true;
|
|
|
|
}
|
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
/* reset GPIOs */
|
|
|
|
gpio_reset();
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* start the IO interface task */
|
2012-10-01 04:02:38 -03:00
|
|
|
_task = task_spawn("fmuservo",
|
2012-10-04 03:13:20 -03:00
|
|
|
SCHED_DEFAULT,
|
2012-10-01 04:02:38 -03:00
|
|
|
SCHED_PRIORITY_DEFAULT,
|
|
|
|
1024,
|
2012-10-27 05:39:10 -03:00
|
|
|
(main_t)&PX4FMU::task_main_trampoline,
|
2012-10-01 04:02:38 -03:00
|
|
|
nullptr);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (_task < 0) {
|
|
|
|
debug("task start failed: %d", errno);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-27 05:39:10 -03:00
|
|
|
PX4FMU::task_main_trampoline(int argc, char *argv[])
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
2012-10-27 05:39:10 -03:00
|
|
|
g_fmu->task_main();
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
int
|
|
|
|
PX4FMU::set_mode(Mode mode)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
2012-10-27 05:39:10 -03:00
|
|
|
/*
|
|
|
|
* Configure for PWM output.
|
|
|
|
*
|
|
|
|
* Note that regardless of the configured mode, the task is always
|
|
|
|
* listening and mixing; the mode just selects which of the channels
|
|
|
|
* are presented on the output pins.
|
|
|
|
*/
|
|
|
|
switch (mode) {
|
2012-08-04 19:12:36 -03:00
|
|
|
case MODE_2PWM:
|
2012-10-27 05:39:10 -03:00
|
|
|
debug("MODE_2PWM");
|
2012-08-04 19:12:36 -03:00
|
|
|
/* multi-port with flow control lines as PWM */
|
|
|
|
/* XXX magic numbers */
|
|
|
|
up_pwm_servo_init(0x3);
|
2012-10-27 05:39:10 -03:00
|
|
|
_update_rate = 50; /* default output rate */
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
case MODE_4PWM:
|
2012-10-27 05:39:10 -03:00
|
|
|
debug("MODE_4PWM");
|
2012-08-04 19:12:36 -03:00
|
|
|
/* multi-port as 4 PWM outs */
|
|
|
|
/* XXX magic numbers */
|
|
|
|
up_pwm_servo_init(0xf);
|
2012-10-27 05:39:10 -03:00
|
|
|
_update_rate = 50; /* default output rate */
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
case MODE_NONE:
|
2012-10-27 05:39:10 -03:00
|
|
|
debug("MODE_NONE");
|
|
|
|
/* disable servo outputs and set a very low update rate */
|
|
|
|
up_pwm_servo_deinit();
|
|
|
|
_update_rate = 10;
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
2012-10-27 05:39:10 -03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
2012-10-27 05:39:10 -03:00
|
|
|
_mode = mode;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PX4FMU::set_pwm_rate(unsigned rate)
|
|
|
|
{
|
|
|
|
if ((rate > 500) || (rate < 10))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
_update_rate = rate;
|
|
|
|
return OK;
|
|
|
|
}
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
void
|
|
|
|
PX4FMU::task_main()
|
|
|
|
{
|
2012-10-20 20:53:52 -03:00
|
|
|
/*
|
|
|
|
* Subscribe to the appropriate PWM output topic based on whether we are the
|
|
|
|
* primary PWM output or not.
|
|
|
|
*/
|
|
|
|
_t_actuators = orb_subscribe(_primary_pwm_device ? ORB_ID_VEHICLE_ATTITUDE_CONTROLS :
|
|
|
|
ORB_ID(actuator_controls_1));
|
2012-10-27 05:39:10 -03:00
|
|
|
/* force a reset of the update rate */
|
|
|
|
_current_update_rate = 0;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
_t_armed = orb_subscribe(ORB_ID(actuator_armed));
|
2012-09-02 06:16:44 -03:00
|
|
|
orb_set_interval(_t_armed, 200); /* 5Hz update rate */
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
/* advertise the mixed control outputs */
|
2012-10-20 20:53:52 -03:00
|
|
|
actuator_outputs_s outputs;
|
2012-08-22 00:14:43 -03:00
|
|
|
memset(&outputs, 0, sizeof(outputs));
|
2012-10-20 20:53:52 -03:00
|
|
|
/* advertise the mixed control outputs */
|
|
|
|
_t_outputs = orb_advertise(_primary_pwm_device ? ORB_ID_VEHICLE_CONTROLS : ORB_ID(actuator_outputs_1),
|
|
|
|
&outputs);
|
2012-08-22 00:14:43 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
pollfd fds[2];
|
2012-08-04 19:12:36 -03:00
|
|
|
fds[0].fd = _t_actuators;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
fds[1].fd = _t_armed;
|
|
|
|
fds[1].events = POLLIN;
|
|
|
|
|
|
|
|
unsigned num_outputs = (_mode == MODE_2PWM) ? 2 : 4;
|
|
|
|
|
2012-08-05 23:46:55 -03:00
|
|
|
log("starting");
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* loop until killed */
|
|
|
|
while (!_task_should_exit) {
|
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
/* handle update rate changes */
|
|
|
|
if (_current_update_rate != _update_rate) {
|
|
|
|
int update_rate_in_ms = int(1000 / _update_rate);
|
|
|
|
if (update_rate_in_ms < 2)
|
|
|
|
update_rate_in_ms = 2;
|
|
|
|
orb_set_interval(_t_actuators, update_rate_in_ms);
|
|
|
|
up_pwm_servo_set_rate(_update_rate);
|
|
|
|
_current_update_rate = _update_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sleep waiting for data, but no more than a second */
|
2012-08-11 14:34:02 -03:00
|
|
|
int ret = ::poll(&fds[0], 2, 1000);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* this would be bad... */
|
|
|
|
if (ret < 0) {
|
|
|
|
log("poll error %d", errno);
|
|
|
|
usleep(1000000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do we have a control update? */
|
|
|
|
if (fds[0].revents & POLLIN) {
|
|
|
|
|
2012-08-11 14:34:02 -03:00
|
|
|
/* get controls - must always do this to avoid spinning */
|
2012-08-10 04:30:40 -03:00
|
|
|
orb_copy(ORB_ID_VEHICLE_ATTITUDE_CONTROLS, _t_actuators, &_controls);
|
|
|
|
|
2012-08-11 14:34:02 -03:00
|
|
|
/* can we mix? */
|
|
|
|
if (_mixers != nullptr) {
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-11 14:34:02 -03:00
|
|
|
/* do mixing */
|
2012-08-22 00:14:43 -03:00
|
|
|
_mixers->mix(&outputs.output[0], num_outputs);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-11 14:34:02 -03:00
|
|
|
/* iterate actuators */
|
|
|
|
for (unsigned i = 0; i < num_outputs; i++) {
|
|
|
|
|
|
|
|
/* scale for PWM output 900 - 2100us */
|
2012-08-22 00:14:43 -03:00
|
|
|
outputs.output[i] = 1500 + (600 * outputs.output[i]);
|
|
|
|
|
|
|
|
/* output to the servo */
|
|
|
|
up_pwm_servo_set(i, outputs.output[i]);
|
2012-08-11 14:34:02 -03:00
|
|
|
}
|
2012-08-22 00:14:43 -03:00
|
|
|
|
|
|
|
/* and publish for anyone that cares to see */
|
|
|
|
orb_publish(ORB_ID_VEHICLE_CONTROLS, _t_outputs, &outputs);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* how about an arming update? */
|
|
|
|
if (fds[1].revents & POLLIN) {
|
2012-10-20 20:53:52 -03:00
|
|
|
actuator_armed_s aa;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* get new value */
|
|
|
|
orb_copy(ORB_ID(actuator_armed), _t_armed, &aa);
|
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
/* update PWM servo armed status */
|
2012-08-04 19:12:36 -03:00
|
|
|
up_pwm_servo_arm(aa.armed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::close(_t_actuators);
|
|
|
|
::close(_t_armed);
|
|
|
|
|
|
|
|
/* make sure servos are off */
|
2012-08-05 17:43:16 -03:00
|
|
|
up_pwm_servo_deinit();
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-05 23:46:55 -03:00
|
|
|
log("stopping");
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* note - someone else is responsible for restoring the GPIO config */
|
|
|
|
|
|
|
|
/* tell the dtor that we are exiting */
|
|
|
|
_task = -1;
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
int
|
2012-10-27 05:39:10 -03:00
|
|
|
PX4FMU::control_callback(uintptr_t handle,
|
2012-08-10 04:30:40 -03:00
|
|
|
uint8_t control_group,
|
|
|
|
uint8_t control_index,
|
|
|
|
float &input)
|
|
|
|
{
|
2012-08-22 00:14:43 -03:00
|
|
|
const actuator_controls_s *controls = (actuator_controls_s *)handle;
|
2012-08-10 04:30:40 -03:00
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
input = controls->control[control_index];
|
2012-08-10 04:30:40 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
int
|
2012-10-27 05:39:10 -03:00
|
|
|
PX4FMU::ioctl(file *filp, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-11-10 11:51:17 -04:00
|
|
|
// XXX disabled, confusing users
|
|
|
|
//debug("ioctl 0x%04x 0x%08x", cmd, arg);
|
2012-10-27 05:39:10 -03:00
|
|
|
|
|
|
|
/* try it as a GPIO ioctl first */
|
|
|
|
ret = gpio_ioctl(filp, cmd, arg);
|
|
|
|
if (ret != -ENOTTY)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* if we are in valid PWM mode, try it as a PWM ioctl as well */
|
|
|
|
switch(_mode) {
|
|
|
|
case MODE_2PWM:
|
|
|
|
case MODE_4PWM:
|
|
|
|
ret = pwm_ioctl(filp, cmd, arg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug("not in a PWM mode");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if nobody wants it, let CDev have it */
|
|
|
|
if (ret == -ENOTTY)
|
|
|
|
ret = CDev::ioctl(filp, cmd, arg);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PX4FMU::pwm_ioctl(file *filp, int cmd, unsigned long arg)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
int ret = OK;
|
2012-08-05 00:05:47 -03:00
|
|
|
int channel;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
lock();
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
switch (cmd) {
|
|
|
|
case PWM_SERVO_ARM:
|
|
|
|
up_pwm_servo_arm(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_DISARM:
|
|
|
|
up_pwm_servo_arm(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_SET(2):
|
|
|
|
case PWM_SERVO_SET(3):
|
|
|
|
if (_mode != MODE_4PWM) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case PWM_SERVO_SET(0):
|
|
|
|
case PWM_SERVO_SET(1):
|
|
|
|
if (arg < 2100) {
|
2012-08-05 00:05:47 -03:00
|
|
|
channel = cmd - PWM_SERVO_SET(0);
|
2012-08-04 19:12:36 -03:00
|
|
|
up_pwm_servo_set(channel, arg);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_GET(2):
|
|
|
|
case PWM_SERVO_GET(3):
|
|
|
|
if (_mode != MODE_4PWM) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case PWM_SERVO_GET(0):
|
|
|
|
case PWM_SERVO_GET(1): {
|
2012-08-05 17:43:16 -03:00
|
|
|
channel = cmd - PWM_SERVO_SET(0);
|
|
|
|
*(servo_position_t *)arg = up_pwm_servo_get(channel);
|
|
|
|
break;
|
|
|
|
}
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCGETOUTPUTCOUNT:
|
2012-08-05 00:05:47 -03:00
|
|
|
if (_mode == MODE_4PWM) {
|
|
|
|
*(unsigned *)arg = 4;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
} else {
|
|
|
|
*(unsigned *)arg = 2;
|
|
|
|
}
|
2012-08-14 12:47:04 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
break;
|
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCRESET:
|
|
|
|
if (_mixers != nullptr) {
|
|
|
|
delete _mixers;
|
|
|
|
_mixers = nullptr;
|
2012-08-05 06:09:11 -03:00
|
|
|
}
|
2012-08-14 12:47:04 -03:00
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
break;
|
2012-08-05 06:09:11 -03:00
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCADDSIMPLE: {
|
2012-08-14 12:47:04 -03:00
|
|
|
mixer_simple_s *mixinfo = (mixer_simple_s *)arg;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
SimpleMixer *mixer = new SimpleMixer(control_callback,
|
|
|
|
(uintptr_t)&_controls, mixinfo);
|
2012-08-14 12:47:04 -03:00
|
|
|
|
|
|
|
if (mixer->check()) {
|
|
|
|
delete mixer;
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (_mixers == nullptr)
|
2012-08-22 00:14:43 -03:00
|
|
|
_mixers = new MixerGroup(control_callback,
|
|
|
|
(uintptr_t)&_controls);
|
2012-08-14 12:47:04 -03:00
|
|
|
|
|
|
|
_mixers->add_mixer(mixer);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2012-08-05 00:05:47 -03:00
|
|
|
}
|
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCADDMULTIROTOR:
|
|
|
|
/* XXX not yet supported */
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
2012-08-05 23:46:55 -03:00
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCLOADFILE: {
|
2012-08-14 12:47:04 -03:00
|
|
|
const char *path = (const char *)arg;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-14 12:47:04 -03:00
|
|
|
if (_mixers != nullptr) {
|
|
|
|
delete _mixers;
|
|
|
|
_mixers = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
_mixers = new MixerGroup(control_callback, (uintptr_t)&_controls);
|
2012-10-27 05:39:10 -03:00
|
|
|
if (_mixers == nullptr) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
} else {
|
2012-08-14 12:47:04 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
debug("loading mixers from %s", path);
|
|
|
|
ret = _mixers->load_from_file(path);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
debug("mixer load failed with %d", ret);
|
|
|
|
delete _mixers;
|
|
|
|
_mixers = nullptr;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2012-08-14 12:47:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2012-08-05 00:05:47 -03:00
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
unlock();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4FMU::gpio_reset(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Setup default GPIO config - all pins as GPIOs, GPIO driver chip
|
|
|
|
* to input mode.
|
|
|
|
*/
|
|
|
|
for (unsigned i = 0; i < _ngpio; i++)
|
|
|
|
stm32_configgpio(_gpio_tab[i].input);
|
|
|
|
|
|
|
|
stm32_gpiowrite(GPIO_GPIO_DIR, 0);
|
|
|
|
stm32_configgpio(GPIO_GPIO_DIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4FMU::gpio_set_function(uint32_t gpios, int function)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* GPIOs 0 and 1 must have the same direction as they are buffered
|
|
|
|
* by a shared 2-port driver. Any attempt to set either sets both.
|
|
|
|
*/
|
|
|
|
if (gpios & 3) {
|
|
|
|
gpios |= 3;
|
|
|
|
|
|
|
|
/* flip the buffer to output mode if required */
|
|
|
|
if (GPIO_SET_OUTPUT == function)
|
|
|
|
stm32_gpiowrite(GPIO_GPIO_DIR, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* configure selected GPIOs as required */
|
|
|
|
for (unsigned i = 0; i < _ngpio; i++) {
|
|
|
|
if (gpios & (1<<i)) {
|
|
|
|
switch (function) {
|
|
|
|
case GPIO_SET_INPUT:
|
|
|
|
stm32_configgpio(_gpio_tab[i].input);
|
|
|
|
break;
|
|
|
|
case GPIO_SET_OUTPUT:
|
|
|
|
stm32_configgpio(_gpio_tab[i].output);
|
|
|
|
break;
|
|
|
|
case GPIO_SET_ALT_1:
|
|
|
|
if (_gpio_tab[i].alt != 0)
|
|
|
|
stm32_configgpio(_gpio_tab[i].alt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flip buffer to input mode if required */
|
|
|
|
if ((GPIO_SET_INPUT == function) && (gpios & 3))
|
|
|
|
stm32_gpiowrite(GPIO_GPIO_DIR, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4FMU::gpio_write(uint32_t gpios, int function)
|
|
|
|
{
|
|
|
|
int value = (function == GPIO_SET) ? 1 : 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < _ngpio; i++)
|
|
|
|
if (gpios & (1<<i))
|
|
|
|
stm32_gpiowrite(_gpio_tab[i].output, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
PX4FMU::gpio_read(void)
|
|
|
|
{
|
|
|
|
uint32_t bits = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < _ngpio; i++)
|
|
|
|
if (stm32_gpioread(_gpio_tab[i].input))
|
|
|
|
bits |= (1 << i);
|
|
|
|
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PX4FMU::gpio_ioctl(struct file *filp, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
lock();
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
|
|
|
|
case GPIO_RESET:
|
|
|
|
gpio_reset();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GPIO_SET_OUTPUT:
|
|
|
|
case GPIO_SET_INPUT:
|
|
|
|
case GPIO_SET_ALT_1:
|
|
|
|
gpio_set_function(arg, cmd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GPIO_SET_ALT_2:
|
|
|
|
case GPIO_SET_ALT_3:
|
|
|
|
case GPIO_SET_ALT_4:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GPIO_SET:
|
|
|
|
case GPIO_CLEAR:
|
|
|
|
gpio_write(arg, cmd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GPIO_GET:
|
|
|
|
*(uint32_t *)arg = gpio_read();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
namespace
|
|
|
|
{
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
enum PortMode {
|
2012-08-05 06:09:11 -03:00
|
|
|
PORT_MODE_UNSET = 0,
|
2012-08-04 19:12:36 -03:00
|
|
|
PORT_FULL_GPIO,
|
|
|
|
PORT_FULL_SERIAL,
|
|
|
|
PORT_FULL_PWM,
|
|
|
|
PORT_GPIO_AND_SERIAL,
|
|
|
|
PORT_PWM_AND_SERIAL,
|
|
|
|
PORT_PWM_AND_GPIO,
|
|
|
|
};
|
|
|
|
|
|
|
|
PortMode g_port_mode;
|
|
|
|
|
|
|
|
int
|
2012-09-27 18:36:56 -03:00
|
|
|
fmu_new_mode(PortMode new_mode, int update_rate)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
uint32_t gpio_bits;
|
2012-10-27 05:39:10 -03:00
|
|
|
PX4FMU::Mode servo_mode;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* reset to all-inputs */
|
2012-10-27 05:39:10 -03:00
|
|
|
g_fmu->ioctl(0, GPIO_RESET, 0);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
gpio_bits = 0;
|
2012-10-27 05:39:10 -03:00
|
|
|
servo_mode = PX4FMU::MODE_NONE;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
switch (new_mode) {
|
|
|
|
case PORT_FULL_GPIO:
|
|
|
|
case PORT_MODE_UNSET:
|
|
|
|
/* nothing more to do here */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_FULL_SERIAL:
|
|
|
|
/* set all multi-GPIOs to serial mode */
|
|
|
|
gpio_bits = GPIO_MULTI_1 | GPIO_MULTI_2 | GPIO_MULTI_3 | GPIO_MULTI_4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_FULL_PWM:
|
|
|
|
/* select 4-pin PWM mode */
|
2012-10-27 05:39:10 -03:00
|
|
|
servo_mode = PX4FMU::MODE_4PWM;
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_GPIO_AND_SERIAL:
|
|
|
|
/* set RX/TX multi-GPIOs to serial mode */
|
|
|
|
gpio_bits = GPIO_MULTI_3 | GPIO_MULTI_4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_PWM_AND_SERIAL:
|
|
|
|
/* select 2-pin PWM mode */
|
2012-10-27 05:39:10 -03:00
|
|
|
servo_mode = PX4FMU::MODE_2PWM;
|
2012-08-04 19:12:36 -03:00
|
|
|
/* set RX/TX multi-GPIOs to serial mode */
|
|
|
|
gpio_bits = GPIO_MULTI_3 | GPIO_MULTI_4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_PWM_AND_GPIO:
|
|
|
|
/* select 2-pin PWM mode */
|
2012-10-27 05:39:10 -03:00
|
|
|
servo_mode = PX4FMU::MODE_2PWM;
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust GPIO config for serial mode(s) */
|
|
|
|
if (gpio_bits != 0)
|
2012-10-27 05:39:10 -03:00
|
|
|
g_fmu->ioctl(0, GPIO_SET_ALT_1, gpio_bits);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
/* (re)set the PWM output mode */
|
|
|
|
g_fmu->set_mode(servo_mode);
|
|
|
|
if ((servo_mode != PX4FMU::MODE_NONE) && (update_rate != 0))
|
|
|
|
g_fmu->set_pwm_rate(update_rate);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
return OK;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
int
|
|
|
|
fmu_start(void)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
if (g_fmu == nullptr) {
|
|
|
|
|
|
|
|
g_fmu = new PX4FMU;
|
|
|
|
|
|
|
|
if (g_fmu == nullptr) {
|
2012-08-04 19:12:36 -03:00
|
|
|
ret = -ENOMEM;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else {
|
2012-10-27 05:39:10 -03:00
|
|
|
ret = g_fmu->init();
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (ret != OK) {
|
2012-10-27 05:39:10 -03:00
|
|
|
delete g_fmu;
|
|
|
|
g_fmu = nullptr;
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-05 23:46:55 -03:00
|
|
|
void
|
|
|
|
test(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(PWM_OUTPUT_DEVICE_PATH, 0);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
puts("open fail");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ioctl(fd, PWM_SERVO_ARM, 0);
|
|
|
|
ioctl(fd, PWM_SERVO_SET(0), 1000);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fake(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc < 5) {
|
2012-09-02 07:21:54 -03:00
|
|
|
puts("fmu fake <roll> <pitch> <yaw> <thrust> (values -100 .. 100)");
|
2012-08-05 23:46:55 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
actuator_controls_s ac;
|
2012-08-05 23:46:55 -03:00
|
|
|
|
|
|
|
ac.control[0] = strtol(argv[1], 0, 0) / 100.0f;
|
|
|
|
|
|
|
|
ac.control[1] = strtol(argv[2], 0, 0) / 100.0f;
|
|
|
|
|
|
|
|
ac.control[2] = strtol(argv[3], 0, 0) / 100.0f;
|
|
|
|
|
|
|
|
ac.control[3] = strtol(argv[4], 0, 0) / 100.0f;
|
|
|
|
|
2012-08-22 03:44:22 -03:00
|
|
|
orb_advert_t handle = orb_advertise(ORB_ID_VEHICLE_ATTITUDE_CONTROLS, &ac);
|
2012-08-05 23:46:55 -03:00
|
|
|
|
|
|
|
if (handle < 0) {
|
|
|
|
puts("advertise failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} // namespace
|
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
extern "C" __EXPORT int fmu_main(int argc, char *argv[]);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
int
|
|
|
|
fmu_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
PortMode new_mode = PORT_MODE_UNSET;
|
2012-10-27 05:39:10 -03:00
|
|
|
int pwm_update_rate_in_hz = 0;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-05 23:46:55 -03:00
|
|
|
if (!strcmp(argv[1], "test"))
|
|
|
|
test();
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "fake"))
|
|
|
|
fake(argc - 1, argv + 1);
|
|
|
|
|
2012-10-27 05:39:10 -03:00
|
|
|
if (fmu_start() != OK)
|
|
|
|
errx(1, "failed to start the FMU driver");
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/*
|
|
|
|
* Mode switches.
|
|
|
|
*
|
2012-08-05 23:46:55 -03:00
|
|
|
* XXX use getopt?
|
2012-08-04 19:12:36 -03:00
|
|
|
*/
|
2012-09-27 18:36:56 -03:00
|
|
|
for (int i = 1; i < argc; i++) { /* argv[0] is "fmu" */
|
|
|
|
if (!strcmp(argv[i], "mode_gpio")) {
|
|
|
|
new_mode = PORT_FULL_GPIO;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-09-27 18:36:56 -03:00
|
|
|
} else if (!strcmp(argv[i], "mode_serial")) {
|
|
|
|
new_mode = PORT_FULL_SERIAL;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-09-27 18:36:56 -03:00
|
|
|
} else if (!strcmp(argv[i], "mode_pwm")) {
|
|
|
|
new_mode = PORT_FULL_PWM;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-09-27 18:36:56 -03:00
|
|
|
} else if (!strcmp(argv[i], "mode_gpio_serial")) {
|
|
|
|
new_mode = PORT_GPIO_AND_SERIAL;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-09-27 18:36:56 -03:00
|
|
|
} else if (!strcmp(argv[i], "mode_pwm_serial")) {
|
|
|
|
new_mode = PORT_PWM_AND_SERIAL;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-09-27 18:36:56 -03:00
|
|
|
} else if (!strcmp(argv[i], "mode_pwm_gpio")) {
|
|
|
|
new_mode = PORT_PWM_AND_GPIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look for the optional pwm update rate for the supported modes */
|
|
|
|
if (strcmp(argv[i], "-u") == 0 || strcmp(argv[i], "--update-rate") == 0) {
|
2012-09-28 03:37:24 -03:00
|
|
|
if (new_mode == PORT_FULL_PWM || new_mode == PORT_PWM_AND_GPIO) {
|
2012-09-27 18:36:56 -03:00
|
|
|
if (argc > i + 1) {
|
|
|
|
pwm_update_rate_in_hz = atoi(argv[i + 1]);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "missing argument for pwm update rate (-u)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
2012-09-28 03:37:24 -03:00
|
|
|
fprintf(stderr, "pwm update rate currently only supported for mode_pwm, mode_pwm_gpio\n");
|
2012-09-27 18:36:56 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* was a new mode set? */
|
|
|
|
if (new_mode != PORT_MODE_UNSET) {
|
|
|
|
|
|
|
|
/* yes but it's the same mode */
|
|
|
|
if (new_mode == g_port_mode)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* switch modes */
|
2012-09-27 18:36:56 -03:00
|
|
|
return fmu_new_mode(new_mode, pwm_update_rate_in_hz);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test, etc. here */
|
|
|
|
|
|
|
|
fprintf(stderr, "FMU: unrecognised command, try:\n");
|
2012-10-27 05:39:10 -03:00
|
|
|
fprintf(stderr, " mode_gpio, mode_serial, mode_pwm [-u pwm_update_rate_in_hz], mode_gpio_serial, mode_pwm_serial, mode_pwm_gpio\n");
|
2012-08-04 19:12:36 -03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|