aboutsummaryrefslogtreecommitdiff
path: root/apps/drivers/px4io
diff options
context:
space:
mode:
Diffstat (limited to 'apps/drivers/px4io')
-rw-r--r--apps/drivers/px4io/px4io.cpp36
-rw-r--r--apps/drivers/px4io/uploader.cpp47
2 files changed, 73 insertions, 10 deletions
diff --git a/apps/drivers/px4io/px4io.cpp b/apps/drivers/px4io/px4io.cpp
index 66db1c360..995c9393f 100644
--- a/apps/drivers/px4io/px4io.cpp
+++ b/apps/drivers/px4io/px4io.cpp
@@ -110,7 +110,7 @@ private:
bool _primary_pwm_device; ///< true if we are the default PWM output
volatile bool _switch_armed; ///< PX4IO switch armed state
- // XXX how should this work?
+ // XXX how should this work?
bool _send_needed; ///< If true, we need to send a packet to IO
@@ -149,13 +149,13 @@ private:
* group/index during mixing.
*/
static int control_callback(uintptr_t handle,
- uint8_t control_group,
- uint8_t control_index,
- float &input);
+ uint8_t control_group,
+ uint8_t control_index,
+ float &input);
};
-namespace
+namespace
{
PX4IO *g_dev;
@@ -190,6 +190,7 @@ PX4IO::~PX4IO()
/* spin waiting for the thread to stop */
unsigned i = 10;
+
do {
/* wait 50ms - it should wake every 100ms or so worst-case */
usleep(50000);
@@ -223,11 +224,13 @@ PX4IO::init()
/* do regular cdev init */
ret = CDev::init();
+
if (ret != OK)
return ret;
/* 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;
@@ -235,6 +238,7 @@ PX4IO::init()
/* start the IO interface task */
_task = task_create("px4io", SCHED_PRIORITY_DEFAULT, 4096, (main_t)&PX4IO::task_main_trampoline, nullptr);
+
if (_task < 0) {
debug("task start failed: %d", errno);
return -errno;
@@ -256,6 +260,7 @@ PX4IO::task_main()
/* open the serial port */
_serial_fd = ::open("/dev/ttyS2", O_RDWR);
+
if (_serial_fd < 0) {
debug("failed to open serial port for IO: %d", errno);
_task = -1;
@@ -343,6 +348,7 @@ PX4IO::task_main()
_send_needed = true;
}
}
+
if (fds[2].revents & POLLIN) {
orb_copy(ORB_ID(actuator_armed), _t_armed, &_controls);
@@ -364,9 +370,9 @@ PX4IO::task_main()
int
PX4IO::control_callback(uintptr_t handle,
- uint8_t control_group,
- uint8_t control_index,
- float &input)
+ uint8_t control_group,
+ uint8_t control_index,
+ float &input)
{
const actuator_controls_s *controls = (actuator_controls_s *)handle;
@@ -458,13 +464,16 @@ PX4IO::ioctl(file *filep, int cmd, unsigned long arg)
break;
case PWM_SERVO_SET(0) ... PWM_SERVO_SET(_max_actuators - 1):
+
/* fake an update to the selected servo channel */
if ((arg >= 900) && (arg <= 2100)) {
_outputs.output[cmd - PWM_SERVO_SET(0)] = arg;
_send_needed = true;
+
} else {
ret = -EINVAL;
}
+
break;
case PWM_SERVO_GET(0) ... PWM_SERVO_GET(_max_actuators - 1):
@@ -481,6 +490,7 @@ PX4IO::ioctl(file *filep, int cmd, unsigned long arg)
delete _mixers;
_mixers = nullptr;
}
+
break;
case MIXERIOCADDSIMPLE: {
@@ -519,6 +529,7 @@ PX4IO::ioctl(file *filep, int cmd, unsigned long arg)
/* allocate a new mixer group and load it from the file */
newmixers = new MixerGroup(control_callback, (uintptr_t)&_controls);
+
if (newmixers->load_from_file(path) != 0) {
delete newmixers;
ret = -EINVAL;
@@ -528,6 +539,7 @@ PX4IO::ioctl(file *filep, int cmd, unsigned long arg)
if (_mixers != nullptr) {
delete _mixers;
}
+
_mixers = newmixers;
}
@@ -537,6 +549,7 @@ PX4IO::ioctl(file *filep, int cmd, unsigned long arg)
/* not a recognised value */
ret = -ENOTTY;
}
+
unlock();
return ret;
@@ -576,6 +589,7 @@ px4io_main(int argc, char *argv[])
if (argc > 2) {
fn[0] = argv[2];
fn[1] = nullptr;
+
} else {
fn[0] = "/fs/microsd/px4io.bin";
fn[1] = "/etc/px4io.bin";
@@ -589,18 +603,24 @@ px4io_main(int argc, char *argv[])
switch (ret) {
case OK:
break;
+
case -ENOENT:
errx(1, "PX4IO firmware file not found");
+
case -EEXIST:
case -EIO:
errx(1, "error updating PX4IO - check that bootloader mode is enabled");
+
case -EINVAL:
errx(1, "verify failed - retry the update");
+
case -ETIMEDOUT:
errx(1, "timed out waiting for bootloader - power-cycle and try again");
+
default:
errx(1, "unexpected error %d", ret);
}
+
return ret;
}
diff --git a/apps/drivers/px4io/uploader.cpp b/apps/drivers/px4io/uploader.cpp
index 0fbbac839..5669aeb01 100644
--- a/apps/drivers/px4io/uploader.cpp
+++ b/apps/drivers/px4io/uploader.cpp
@@ -67,6 +67,7 @@ PX4IO_Uploader::upload(const char *filenames[])
int ret;
_io_fd = open("/dev/ttyS2", O_RDWR);
+
if (_io_fd < 0) {
log("could not open interface");
return -errno;
@@ -74,6 +75,7 @@ PX4IO_Uploader::upload(const char *filenames[])
/* look for the bootloader */
ret = sync();
+
if (ret != OK) {
/* this is immediately fatal */
log("bootloader not responding");
@@ -87,17 +89,20 @@ PX4IO_Uploader::upload(const char *filenames[])
log("failed to open %s", filenames[i]);
continue;
}
+
log("using firmware from %s", filenames[i]);
break;
}
+
if (_fw_fd == -1)
return -ENOENT;
/* do the usual program thing - allow for failure */
for (unsigned retries = 0; retries < 1; retries++) {
if (retries > 0) {
- log("retrying update...");
+ log("retrying update...");
ret = sync();
+
if (ret != OK) {
/* this is immediately fatal */
log("bootloader not responding");
@@ -106,25 +111,33 @@ PX4IO_Uploader::upload(const char *filenames[])
}
ret = erase();
+
if (ret != OK) {
log("erase failed");
continue;
}
+
ret = program();
+
if (ret != OK) {
log("program failed");
continue;
}
+
ret = verify();
+
if (ret != OK) {
log("verify failed");
continue;
}
+
ret = reboot();
+
if (ret != OK) {
log("reboot failed");
return ret;
}
+
log("update complete");
ret = OK;
@@ -145,6 +158,7 @@ PX4IO_Uploader::recv(uint8_t &c, unsigned timeout)
/* wait 100 ms for a character */
int ret = ::poll(&fds[0], 1, timeout);
+
if (ret < 1) {
//log("poll timeout %d", ret);
return -ETIMEDOUT;
@@ -160,9 +174,11 @@ PX4IO_Uploader::recv(uint8_t *p, unsigned count)
{
while (count--) {
int ret = recv(*p++);
+
if (ret != OK)
return ret;
}
+
return OK;
}
@@ -175,7 +191,7 @@ PX4IO_Uploader::drain()
do {
ret = recv(c, 10);
//log("discard 0x%02x", c);
- } while(ret == OK);
+ } while (ret == OK);
}
int
@@ -184,6 +200,7 @@ PX4IO_Uploader::send(uint8_t c)
//log("send 0x%02x", c);
if (write(_io_fd, &c, 1) != 1)
return -errno;
+
return OK;
}
@@ -192,9 +209,11 @@ PX4IO_Uploader::send(uint8_t *p, unsigned count)
{
while (count--) {
int ret = send(*p++);
+
if (ret != OK)
return ret;
}
+
return OK;
}
@@ -205,15 +224,20 @@ PX4IO_Uploader::get_sync(unsigned timeout)
int ret;
ret = recv(c[0], timeout);
+
if (ret != OK)
return ret;
+
ret = recv(c[1], timeout);
+
if (ret != OK)
return ret;
+
if ((c[0] != PROTO_INSYNC) || (c[1] != PROTO_OK)) {
log("bad sync 0x%02x,0x%02x", c[0], c[1]);
return -EIO;
}
+
return OK;
}
@@ -221,9 +245,11 @@ int
PX4IO_Uploader::sync()
{
drain();
+
/* complete any pending program operation */
for (unsigned i = 0; i < (PROG_MULTI_MAX + 6); i++)
send(0);
+
send(PROTO_GET_SYNC);
send(PROTO_EOC);
return get_sync();
@@ -239,8 +265,10 @@ PX4IO_Uploader::get_info(int param, uint32_t &val)
send(PROTO_EOC);
ret = recv((uint8_t *)&val, sizeof(val));
+
if (ret != OK)
return ret;
+
return get_sync();
}
@@ -267,10 +295,13 @@ PX4IO_Uploader::program()
/* get more bytes to program */
//log(" %d", (int)lseek(_fw_fd, 0, SEEK_CUR));
count = read(_fw_fd, file_buf, sizeof(file_buf));
+
if (count == 0)
return OK;
+
if (count < 0)
return -errno;
+
ASSERT((count % 4) == 0);
send(PROTO_PROG_MULTI);
@@ -279,6 +310,7 @@ PX4IO_Uploader::program()
send(PROTO_EOC);
ret = get_sync(1000);
+
if (ret != OK)
return ret;
}
@@ -297,6 +329,7 @@ PX4IO_Uploader::verify()
send(PROTO_CHIP_VERIFY);
send(PROTO_EOC);
ret = get_sync();
+
if (ret != OK)
return ret;
@@ -304,19 +337,24 @@ PX4IO_Uploader::verify()
/* get more bytes to verify */
int base = (int)lseek(_fw_fd, 0, SEEK_CUR);
count = read(_fw_fd, file_buf, sizeof(file_buf));
+
if (count == 0)
break;
+
if (count < 0)
return -errno;
+
ASSERT((count % 4) == 0);
send(PROTO_READ_MULTI);
send(count);
send(PROTO_EOC);
+
for (ssize_t i = 0; i < count; i++) {
uint8_t c;
ret = recv(c);
+
if (ret != OK) {
log("%d: got %d waiting for bytes", base + i, ret);
return ret;
@@ -327,12 +365,15 @@ PX4IO_Uploader::verify()
return -EINVAL;
}
}
+
ret = get_sync();
+
if (ret != OK) {
log("timeout waiting for post-verify sync");
return ret;
}
}
+
return OK;
}
@@ -358,6 +399,7 @@ PX4IO_Uploader::compare(bool &identical)
send(PROTO_CHIP_VERIFY);
send(PROTO_EOC);
ret = get_sync();
+
if (ret != OK)
return ret;
@@ -365,6 +407,7 @@ PX4IO_Uploader::compare(bool &identical)
send(sizeof(fw_vectors));
send(PROTO_EOC);
ret = recv((uint8_t *)&fw_vectors[0], sizeof(fw_vectors));
+
if (ret != OK)
return ret;