2016-11-06 21:42:47 -04:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
handle device operations over MAVLink
|
|
|
|
*/
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_HAL/Device.h>
|
2016-11-30 00:49:48 -04:00
|
|
|
#include <AP_HAL/I2CDevice.h>
|
2016-11-06 21:42:47 -04:00
|
|
|
#include "GCS.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle DEVICE_OP_READ message
|
|
|
|
*/
|
2019-07-11 05:31:45 -03:00
|
|
|
void GCS_MAVLINK::handle_device_op_read(const mavlink_message_t &msg)
|
2016-11-06 21:42:47 -04:00
|
|
|
{
|
|
|
|
mavlink_device_op_read_t packet;
|
2019-07-11 05:31:45 -03:00
|
|
|
mavlink_msg_device_op_read_decode(&msg, &packet);
|
2016-11-06 21:42:47 -04:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::Device> dev = nullptr;
|
|
|
|
uint8_t retcode = 0;
|
|
|
|
uint8_t data[sizeof(mavlink_device_op_read_reply_t::data)] {};
|
2019-07-19 01:52:59 -03:00
|
|
|
bool ret = false;
|
|
|
|
uint8_t regstart = packet.regstart;
|
|
|
|
|
2016-11-06 21:42:47 -04:00
|
|
|
if (packet.bustype == DEVICE_OP_BUSTYPE_I2C) {
|
|
|
|
dev = hal.i2c_mgr->get_device(packet.bus, packet.address);
|
|
|
|
} else if (packet.bustype == DEVICE_OP_BUSTYPE_SPI) {
|
|
|
|
dev = hal.spi->get_device(packet.busname);
|
|
|
|
} else {
|
|
|
|
retcode = 1;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!dev) {
|
|
|
|
retcode = 2;
|
|
|
|
goto fail;
|
|
|
|
}
|
2022-04-02 19:11:08 -03:00
|
|
|
if (packet.count > sizeof(data)) {
|
|
|
|
retcode = 5;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-11-06 21:42:47 -04:00
|
|
|
if (!dev->get_semaphore()->take(10)) {
|
|
|
|
retcode = 3;
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-07-19 01:52:59 -03:00
|
|
|
if (regstart == 0xff) {
|
|
|
|
// assume raw transfer, non-register interface
|
2020-07-30 11:06:39 -03:00
|
|
|
ret = dev->transfer_bank(packet.bank, nullptr, 0, data, packet.count);
|
2019-07-19 01:52:59 -03:00
|
|
|
// reply using register start 0 for display purposes
|
|
|
|
regstart = 0;
|
|
|
|
} else {
|
2020-07-30 11:06:39 -03:00
|
|
|
ret = dev->read_bank_registers(packet.bank, packet.regstart, data, packet.count);
|
2019-07-19 01:52:59 -03:00
|
|
|
}
|
|
|
|
dev->get_semaphore()->give();
|
|
|
|
if (!ret) {
|
2016-11-06 21:42:47 -04:00
|
|
|
retcode = 4;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
mavlink_msg_device_op_read_reply_send(
|
|
|
|
chan,
|
|
|
|
packet.request_id,
|
|
|
|
retcode,
|
2019-07-19 01:52:59 -03:00
|
|
|
regstart,
|
2016-11-06 21:42:47 -04:00
|
|
|
packet.count,
|
2020-07-30 11:06:39 -03:00
|
|
|
data,
|
|
|
|
packet.bank);
|
2016-11-06 21:42:47 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
mavlink_msg_device_op_read_reply_send(
|
|
|
|
chan,
|
|
|
|
packet.request_id,
|
|
|
|
retcode,
|
|
|
|
packet.regstart,
|
|
|
|
0,
|
2020-07-30 11:06:39 -03:00
|
|
|
nullptr,
|
|
|
|
packet.bank);
|
2016-11-06 21:42:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle DEVICE_OP_WRITE message
|
|
|
|
*/
|
2019-07-11 05:31:45 -03:00
|
|
|
void GCS_MAVLINK::handle_device_op_write(const mavlink_message_t &msg)
|
2016-11-06 21:42:47 -04:00
|
|
|
{
|
|
|
|
mavlink_device_op_write_t packet;
|
2019-07-11 05:31:45 -03:00
|
|
|
mavlink_msg_device_op_write_decode(&msg, &packet);
|
2016-11-06 21:42:47 -04:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::Device> dev = nullptr;
|
|
|
|
uint8_t retcode = 0;
|
|
|
|
|
|
|
|
if (packet.bustype == DEVICE_OP_BUSTYPE_I2C) {
|
|
|
|
dev = hal.i2c_mgr->get_device(packet.bus, packet.address);
|
|
|
|
} else if (packet.bustype == DEVICE_OP_BUSTYPE_SPI) {
|
|
|
|
dev = hal.spi->get_device(packet.busname);
|
|
|
|
} else {
|
|
|
|
retcode = 1;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!dev) {
|
|
|
|
retcode = 2;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!dev->get_semaphore()->take(10)) {
|
|
|
|
retcode = 3;
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-07-19 01:52:59 -03:00
|
|
|
if (packet.regstart == 0xff) {
|
|
|
|
// assume raw transfer, non-register interface
|
2020-07-30 11:06:39 -03:00
|
|
|
if (!dev->transfer_bank(packet.bank, packet.data, packet.count, nullptr, 0)) {
|
2016-11-06 21:42:47 -04:00
|
|
|
retcode = 4;
|
2019-07-19 01:52:59 -03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (uint8_t i=0; i<packet.count; i++) {
|
2020-07-30 11:06:39 -03:00
|
|
|
if (!dev->write_bank_register(packet.bank, packet.regstart+i, packet.data[i])) {
|
2019-07-19 01:52:59 -03:00
|
|
|
retcode = 4;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-06 21:42:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dev->get_semaphore()->give();
|
|
|
|
|
|
|
|
fail:
|
|
|
|
mavlink_msg_device_op_write_reply_send(
|
|
|
|
chan,
|
|
|
|
packet.request_id,
|
|
|
|
retcode);
|
|
|
|
}
|