2015-08-17 23:29:11 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RASPILOT
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <assert.h>
|
2015-08-17 23:29:11 -03:00
|
|
|
#include <errno.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2015-08-17 23:29:11 -03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <sys/types.h>
|
2015-08-17 23:29:11 -03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "RCInput_Raspilot.h"
|
|
|
|
|
|
|
|
#include "px4io_protocol.h"
|
|
|
|
|
2015-10-16 17:22:11 -03:00
|
|
|
static const AP_HAL::HAL& hal = AP_HAL::get_HAL();
|
2015-08-17 23:29:11 -03:00
|
|
|
|
|
|
|
using namespace Linux;
|
|
|
|
|
2015-12-02 11:14:20 -04:00
|
|
|
void RCInput_Raspilot::init()
|
2015-08-17 23:29:11 -03:00
|
|
|
{
|
2016-04-26 15:33:22 -03:00
|
|
|
_dev = hal.spi->get_device("raspio");
|
2015-08-17 23:29:11 -03:00
|
|
|
|
|
|
|
// start the timer process to read samples
|
2015-10-20 18:13:25 -03:00
|
|
|
hal.scheduler->register_timer_process(FUNCTOR_BIND_MEMBER(&RCInput_Raspilot::_poll_data, void));
|
2015-08-17 23:29:11 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCInput_Raspilot::_poll_data(void)
|
2015-08-17 23:29:11 -03:00
|
|
|
{
|
|
|
|
// Throttle read rate to 100hz maximum.
|
2015-11-19 23:10:58 -04:00
|
|
|
if (AP_HAL::micros() - _last_timer < 10000) {
|
2015-08-17 23:29:11 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-19 23:10:58 -04:00
|
|
|
_last_timer = AP_HAL::micros();
|
2015-08-17 23:29:11 -03:00
|
|
|
|
2016-04-26 15:33:22 -03:00
|
|
|
if (!_dev->get_semaphore()->take_nonblocking()) {
|
2015-08-17 23:29:11 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct IOPacket _dma_packet_tx, _dma_packet_rx;
|
|
|
|
uint16_t count = LINUX_RC_INPUT_NUM_CHANNELS;
|
|
|
|
_dma_packet_tx.count_code = count | PKT_CODE_READ;
|
|
|
|
_dma_packet_tx.page = 4;
|
|
|
|
_dma_packet_tx.offset = 0;
|
|
|
|
_dma_packet_tx.crc = 0;
|
|
|
|
_dma_packet_tx.crc = crc_packet(&_dma_packet_tx);
|
|
|
|
/* set raspilotio to read reg4 */
|
2016-04-26 15:33:22 -03:00
|
|
|
_dev->transfer((uint8_t *)&_dma_packet_tx, sizeof(_dma_packet_tx),
|
|
|
|
(uint8_t *)&_dma_packet_rx, sizeof(_dma_packet_rx));
|
2015-08-17 23:29:11 -03:00
|
|
|
/* get reg4 data from raspilotio */
|
2016-04-26 15:33:22 -03:00
|
|
|
_dev->transfer((uint8_t *)&_dma_packet_tx, sizeof(_dma_packet_tx),
|
|
|
|
(uint8_t *)&_dma_packet_rx, sizeof(_dma_packet_rx));
|
2015-08-17 23:29:11 -03:00
|
|
|
|
|
|
|
uint16_t num_values = _dma_packet_rx.regs[0];
|
|
|
|
uint16_t rc_ok = _dma_packet_rx.regs[1] & (1 << 4);
|
|
|
|
uint8_t rx_crc = _dma_packet_rx.crc;
|
|
|
|
|
|
|
|
_dma_packet_rx.crc = 0;
|
|
|
|
|
|
|
|
if ( rc_ok && (rx_crc == crc_packet(&_dma_packet_rx)) ) {
|
|
|
|
_update_periods(&_dma_packet_rx.regs[6], (uint8_t)num_values);
|
|
|
|
}
|
|
|
|
|
2016-04-26 15:33:22 -03:00
|
|
|
_dev->get_semaphore()->give();
|
2015-08-17 23:29:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD_SUBTYPE
|