can.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #pragma once
  5. #include <uavcan/driver/can.hpp>
  6. namespace uavcan_lpc11c24
  7. {
  8. /**
  9. * This class implements CAN driver interface for libuavcan.
  10. * No configuration needed other than CAN baudrate.
  11. * This class is a singleton.
  12. */
  13. class CanDriver
  14. : public uavcan::ICanDriver
  15. , public uavcan::ICanIface
  16. , uavcan::Noncopyable
  17. {
  18. static CanDriver self;
  19. CanDriver() { }
  20. public:
  21. /**
  22. * Returns the singleton reference.
  23. * No other copies can be created.
  24. */
  25. static CanDriver& instance() { return self; }
  26. /**
  27. * Attempts to detect bit rate of the CAN bus.
  28. * This function may block for up to X seconds, where X is the number of bit rates to try.
  29. * This function is NOT guaranteed to reset the CAN controller upon return.
  30. * @return On success: detected bit rate, in bits per second.
  31. * On failure: zero.
  32. */
  33. static uavcan::uint32_t detectBitRate(void (*idle_callback)() = nullptr);
  34. /**
  35. * Returns negative value if the requested baudrate can't be used.
  36. * Returns zero if OK.
  37. */
  38. int init(uavcan::uint32_t bitrate);
  39. bool hasReadyRx() const;
  40. bool hasEmptyTx() const;
  41. /**
  42. * This method will return true only if there was any CAN bus activity since previous call of this method.
  43. * This is intended to be used for LED iface activity indicators.
  44. */
  45. bool hadActivity();
  46. /**
  47. * Returns the number of times the RX queue was overrun.
  48. */
  49. uavcan::uint32_t getRxQueueOverflowCount() const;
  50. /**
  51. * Whether the controller is currently in bus off state.
  52. * Note that the driver recovers the CAN controller from the bus off state automatically!
  53. * Therefore, this method serves only monitoring purposes and is not necessary to use.
  54. */
  55. bool isInBusOffState() const;
  56. uavcan::int16_t send(const uavcan::CanFrame& frame,
  57. uavcan::MonotonicTime tx_deadline,
  58. uavcan::CanIOFlags flags) override;
  59. uavcan::int16_t receive(uavcan::CanFrame& out_frame,
  60. uavcan::MonotonicTime& out_ts_monotonic,
  61. uavcan::UtcTime& out_ts_utc,
  62. uavcan::CanIOFlags& out_flags) override;
  63. uavcan::int16_t select(uavcan::CanSelectMasks& inout_masks,
  64. const uavcan::CanFrame* (&)[uavcan::MaxCanIfaces],
  65. uavcan::MonotonicTime blocking_deadline) override;
  66. uavcan::int16_t configureFilters(const uavcan::CanFilterConfig* filter_configs,
  67. uavcan::uint16_t num_configs) override;
  68. uavcan::uint64_t getErrorCount() const override;
  69. uavcan::uint16_t getNumFilters() const override;
  70. uavcan::ICanIface* getIface(uavcan::uint8_t iface_index) override;
  71. uavcan::uint8_t getNumIfaces() const override;
  72. };
  73. }