CAN.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2017 Eugene Shamaev
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #pragma once
  18. #include <inttypes.h>
  19. #include <string.h>
  20. #include <assert.h>
  21. #include <cmath>
  22. #if HAL_WITH_UAVCAN
  23. #include "AP_HAL_Namespace.h"
  24. #include "utility/functor.h"
  25. #include <uavcan/driver/can.hpp>
  26. #include <uavcan/time.hpp>
  27. #define MAX_NUMBER_OF_CAN_INTERFACES 2
  28. #define MAX_NUMBER_OF_CAN_DRIVERS 2
  29. /**
  30. * Interface that CAN protocols need to implement
  31. */
  32. class AP_HAL::CANProtocol {
  33. public:
  34. /* method called when initializing the CAN interfaces
  35. *
  36. * if initialization doesn't have errors, protocol class
  37. * should create a thread to do send and receive operations
  38. */
  39. virtual void init(uint8_t driver_index, bool enable_filters) = 0;
  40. };
  41. /**
  42. * Single non-blocking CAN interface.
  43. */
  44. class AP_HAL::CAN: public uavcan::ICanIface {
  45. public:
  46. /* CAN port open method
  47. bitrate Selects the speed that the port will be configured to. If zero, the port speed is left unchanged.
  48. return false - CAN port open failed
  49. true - CAN port open succeeded
  50. */
  51. virtual bool begin(uint32_t bitrate) = 0;
  52. /*
  53. CAN port close
  54. */
  55. virtual void end() = 0;
  56. /*
  57. Reset opened CAN port
  58. Pending messages to be transmitted are deleted and receive state and FIFO also reset.
  59. All pending errors are cleared.
  60. */
  61. virtual void reset() = 0;
  62. /*
  63. Test if CAN port is opened and initialized
  64. return false - CAN port not initialized
  65. true - CAN port is initialized
  66. */
  67. virtual bool is_initialized() = 0;
  68. /*
  69. Return if CAN port has some untransmitted pending messages
  70. return -1 - CAN port is not opened or initialized
  71. 0 - no messages are pending
  72. positive - number of pending messages
  73. */
  74. virtual int32_t tx_pending() = 0;
  75. /*
  76. Return if CAN port has received but not yet read messages
  77. return -1 - CAN port is not opened or initialized
  78. 0 - no messages are pending for read
  79. positive - number of pending messages for read
  80. */
  81. virtual int32_t available() = 0;
  82. };
  83. /**
  84. * Generic CAN driver.
  85. */
  86. class AP_HAL::CANManager {
  87. public:
  88. CANManager(uavcan::ICanDriver* driver) : _driver(driver) {}
  89. /* CAN port open method
  90. Opens port with specified bit rate
  91. bitrate - selects the speed that the port will be configured to. If zero, the port speed is left
  92. unchanged.
  93. can_number - the index of can interface to be opened
  94. return false - CAN port open failed
  95. true - CAN port open succeeded
  96. */
  97. virtual bool begin(uint32_t bitrate, uint8_t can_number) = 0;
  98. /*
  99. Test if CAN manager is ready and initialized
  100. return false - CAN manager not initialized
  101. true - CAN manager is initialized
  102. */
  103. virtual bool is_initialized() = 0;
  104. virtual void initialized(bool val) = 0;
  105. uavcan::ICanDriver* get_driver() { return _driver; }
  106. private:
  107. uavcan::ICanDriver* _driver;
  108. };
  109. #endif