UARTDriver.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #pragma once
  2. #include <AP_HAL/utility/OwnPtr.h>
  3. #include <AP_HAL/utility/RingBuffer.h>
  4. #include "AP_HAL_Linux.h"
  5. #include "SerialDevice.h"
  6. #include "Semaphores.h"
  7. namespace Linux {
  8. class UARTDriver : public AP_HAL::UARTDriver {
  9. public:
  10. UARTDriver(bool default_console);
  11. static UARTDriver *from(AP_HAL::UARTDriver *uart) {
  12. return static_cast<UARTDriver*>(uart);
  13. }
  14. /* Linux implementations of UARTDriver virtual methods */
  15. void begin(uint32_t b) override;
  16. void begin(uint32_t b, uint16_t rxS, uint16_t txS) override;
  17. void end() override;
  18. void flush() override;
  19. bool is_initialized() override;
  20. void set_blocking_writes(bool blocking) override;
  21. bool tx_pending() override;
  22. /* Linux implementations of Stream virtual methods */
  23. uint32_t available() override;
  24. uint32_t txspace() override;
  25. int16_t read() override;
  26. /* Linux implementations of Print virtual methods */
  27. size_t write(uint8_t c) override;
  28. size_t write(const uint8_t *buffer, size_t size) override;
  29. void set_device_path(const char *path);
  30. bool _write_pending_bytes(void);
  31. virtual void _timer_tick(void) override;
  32. virtual enum flow_control get_flow_control(void) override
  33. {
  34. return _device->get_flow_control();
  35. }
  36. void configure_parity(uint8_t v) override;
  37. virtual void set_flow_control(enum flow_control flow_control_setting) override
  38. {
  39. _device->set_flow_control(flow_control_setting);
  40. }
  41. /*
  42. return timestamp estimate in microseconds for when the start of
  43. a nbytes packet arrived on the uart. This should be treated as a
  44. time constraint, not an exact time. It is guaranteed that the
  45. packet did not start being received after this time, but it
  46. could have been in a system buffer before the returned time.
  47. This takes account of the baudrate of the link. For transports
  48. that have no baudrate (such as USB) the time estimate may be
  49. less accurate.
  50. A return value of zero means the HAL does not support this API
  51. */
  52. uint64_t receive_time_constraint_us(uint16_t nbytes) override;
  53. private:
  54. AP_HAL::OwnPtr<SerialDevice> _device;
  55. bool _nonblocking_writes;
  56. bool _console;
  57. volatile bool _in_timer;
  58. uint16_t _base_port;
  59. uint32_t _baudrate;
  60. char *_ip;
  61. char *_flag;
  62. bool _connected; // true if a client has connected
  63. bool _packetise; // true if writes should try to be on mavlink boundaries
  64. void _allocate_buffers(uint16_t rxS, uint16_t txS);
  65. void _deallocate_buffers();
  66. AP_HAL::OwnPtr<SerialDevice> _parseDevicePath(const char *arg);
  67. // timestamp for receiving data on the UART, avoiding a lock
  68. uint64_t _receive_timestamp[2];
  69. uint8_t _receive_timestamp_idx;
  70. protected:
  71. const char *device_path;
  72. volatile bool _initialised;
  73. // we use in-task ring buffers to reduce the system call cost
  74. // of ::read() and ::write() in the main loop
  75. ByteBuffer _readbuf{0};
  76. ByteBuffer _writebuf{0};
  77. virtual int _write_fd(const uint8_t *buf, uint16_t n);
  78. virtual int _read_fd(uint8_t *buf, uint16_t n);
  79. Linux::Semaphore _write_mutex;
  80. };
  81. }