2016-02-17 21:25:26 -04:00
|
|
|
#pragma once
|
2013-01-02 20:03:05 -04:00
|
|
|
|
2016-07-30 08:54:37 -03:00
|
|
|
#include <AP_HAL/utility/RingBuffer.h>
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_HAL_PX4.h"
|
2013-01-24 00:01:48 -04:00
|
|
|
#include <systemlib/perf_counter.h>
|
2017-09-25 19:03:35 -03:00
|
|
|
#include "Semaphores.h"
|
2013-01-02 20:03:05 -04:00
|
|
|
|
|
|
|
class PX4::PX4UARTDriver : public AP_HAL::UARTDriver {
|
|
|
|
public:
|
2013-01-24 00:01:48 -04:00
|
|
|
PX4UARTDriver(const char *devpath, const char *perf_name);
|
2013-01-02 20:03:05 -04:00
|
|
|
/* PX4 implementations of UARTDriver virtual methods */
|
|
|
|
void begin(uint32_t b);
|
|
|
|
void begin(uint32_t b, uint16_t rxS, uint16_t txS);
|
|
|
|
void end();
|
|
|
|
void flush();
|
|
|
|
bool is_initialized();
|
|
|
|
void set_blocking_writes(bool blocking);
|
|
|
|
bool tx_pending();
|
|
|
|
|
|
|
|
/* PX4 implementations of Stream virtual methods */
|
2016-08-02 10:42:50 -03:00
|
|
|
uint32_t available() override;
|
|
|
|
uint32_t txspace() override;
|
|
|
|
int16_t read() override;
|
2013-01-02 20:03:05 -04:00
|
|
|
|
|
|
|
/* PX4 implementations of Print virtual methods */
|
|
|
|
size_t write(uint8_t c);
|
2013-01-21 19:50:26 -04:00
|
|
|
size_t write(const uint8_t *buffer, size_t size);
|
2013-01-02 22:16:41 -04:00
|
|
|
|
2013-01-14 00:59:54 -04:00
|
|
|
void set_device_path(const char *path) {
|
|
|
|
_devpath = path;
|
|
|
|
}
|
|
|
|
|
2018-01-21 15:45:31 -04:00
|
|
|
void _timer_tick(void) override;
|
2013-01-22 05:11:12 -04:00
|
|
|
|
2013-02-07 00:03:54 -04:00
|
|
|
int _get_fd(void) {
|
|
|
|
return _fd;
|
|
|
|
}
|
|
|
|
|
2014-02-10 20:22:08 -04:00
|
|
|
void set_flow_control(enum flow_control flow_control);
|
2014-02-13 22:15:24 -04:00
|
|
|
enum flow_control get_flow_control(void) { return _flow_control; }
|
2014-02-09 21:54:50 -04:00
|
|
|
|
2017-11-22 13:39:05 -04:00
|
|
|
void configure_parity(uint8_t v);
|
|
|
|
void set_stop_bits(int n);
|
|
|
|
bool set_unbuffered_writes(bool on);
|
|
|
|
|
2018-05-15 21:42:49 -03:00
|
|
|
/*
|
|
|
|
return timestamp estimate in microseconds for when the start of
|
|
|
|
a nbytes packet arrived on the uart. This should be treated as a
|
|
|
|
time constraint, not an exact time. It is guaranteed that the
|
|
|
|
packet did not start being received after this time, but it
|
|
|
|
could have been in a system buffer before the returned time.
|
|
|
|
|
|
|
|
This takes account of the baudrate of the link. For transports
|
|
|
|
that have no baudrate (such as USB) the time estimate may be
|
|
|
|
less accurate.
|
|
|
|
|
|
|
|
A return value of zero means the HAL does not support this API
|
|
|
|
*/
|
2018-05-16 18:01:14 -03:00
|
|
|
uint64_t receive_time_constraint_us(uint16_t nbytes) override;
|
2018-05-15 21:42:49 -03:00
|
|
|
|
2013-01-02 22:16:41 -04:00
|
|
|
private:
|
2013-01-03 06:12:10 -04:00
|
|
|
const char *_devpath;
|
|
|
|
int _fd;
|
2013-10-05 02:47:28 -03:00
|
|
|
uint32_t _baudrate;
|
|
|
|
volatile bool _initialised;
|
|
|
|
volatile bool _in_timer;
|
2013-01-21 19:34:04 -04:00
|
|
|
|
2013-01-22 06:52:21 -04:00
|
|
|
bool _nonblocking_writes;
|
2017-11-22 13:39:05 -04:00
|
|
|
bool _unbuffered_writes;
|
2013-01-22 06:52:21 -04:00
|
|
|
|
2013-01-22 05:11:12 -04:00
|
|
|
// we use in-task ring buffers to reduce the system call cost
|
|
|
|
// of ::read() and ::write() in the main loop
|
2016-07-30 08:54:37 -03:00
|
|
|
ByteBuffer _readbuf{0};
|
|
|
|
ByteBuffer _writebuf{0};
|
2013-01-24 00:01:48 -04:00
|
|
|
perf_counter_t _perf_uart;
|
2013-02-18 21:02:46 -04:00
|
|
|
|
|
|
|
int _write_fd(const uint8_t *buf, uint16_t n);
|
2013-03-20 01:41:52 -03:00
|
|
|
int _read_fd(uint8_t *buf, uint16_t n);
|
2015-07-13 20:52:48 -03:00
|
|
|
uint64_t _first_write_time;
|
2013-02-18 21:02:46 -04:00
|
|
|
uint64_t _last_write_time;
|
2013-10-05 02:47:28 -03:00
|
|
|
|
|
|
|
void try_initialise(void);
|
|
|
|
uint32_t _last_initialise_attempt_ms;
|
2014-02-10 20:22:08 -04:00
|
|
|
|
2015-07-13 20:52:48 -03:00
|
|
|
uint32_t _os_start_auto_space;
|
2014-02-10 20:22:08 -04:00
|
|
|
uint32_t _total_read;
|
|
|
|
uint32_t _total_written;
|
|
|
|
enum flow_control _flow_control;
|
2014-09-06 13:24:41 -03:00
|
|
|
|
2018-05-15 21:42:49 -03:00
|
|
|
// timestamp for receiving data on the UART, avoiding a lock
|
|
|
|
uint64_t _receive_timestamp[2];
|
|
|
|
uint8_t _receive_timestamp_idx;
|
|
|
|
|
2017-09-25 19:03:35 -03:00
|
|
|
Semaphore _semaphore;
|
2018-05-15 21:42:49 -03:00
|
|
|
|
|
|
|
bool _is_usb;
|
2013-01-02 20:03:05 -04:00
|
|
|
};
|